|
| 1 | +package slack |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/oursky/github-actions-manager/pkg/kv" |
| 8 | + "github.com/slack-go/slack" |
| 9 | + . "github.com/smartystreets/goconvey/convey" |
| 10 | + |
| 11 | + "go.uber.org/zap" |
| 12 | +) |
| 13 | + |
| 14 | +func commandChan1(command string) slack.SlashCommand { |
| 15 | + return slack.SlashCommand{ |
| 16 | + ChannelID: "TestChannelID1", |
| 17 | + Command: "/test-gha", |
| 18 | + Text: command, |
| 19 | + } |
| 20 | +} |
| 21 | +func commandChan2(command string) slack.SlashCommand { |
| 22 | + return slack.SlashCommand{ |
| 23 | + ChannelID: "TestChannelID2", |
| 24 | + Command: "/test-gha", |
| 25 | + Text: command, |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +func TestSpec(t *testing.T) { |
| 30 | + |
| 31 | + Convey("When receiving commands, the bot", t, func() { |
| 32 | + ctx, cancel := context.WithCancel(context.Background()) |
| 33 | + defer cancel() |
| 34 | + |
| 35 | + testApp := &App{ |
| 36 | + logger: zap.NewNop(), |
| 37 | + store: kv.NewInMemoryStore(), |
| 38 | + commandName: "test-gha", |
| 39 | + } |
| 40 | + |
| 41 | + cli := NewCLI(testApp.logger) |
| 42 | + cli.SetContext(ctx, testApp) |
| 43 | + |
| 44 | + Convey("responds", func() { |
| 45 | + response := cli.Parse(commandChan1("meow")) |
| 46 | + So(response["text"], ShouldEqual, "meow") |
| 47 | + }) |
| 48 | + Convey("rejects unrecognised commmands", func() { |
| 49 | + response := cli.Parse(commandChan1("fhqwhgads")) |
| 50 | + So(response["response_type"], ShouldBeNil) |
| 51 | + So(response["text"], ShouldContainSubstring, "fhqwhgads") |
| 52 | + }) |
| 53 | + Convey("When asked to subscribe", func() { |
| 54 | + Convey("rejects an insufficient number of arguments", func() { |
| 55 | + response := cli.Parse(commandChan1("subscribe")) |
| 56 | + So(response["printToChannel"], ShouldBeNil) |
| 57 | + So(response["text"], ShouldContainSubstring, "repo") |
| 58 | + }) |
| 59 | + Convey("rejects an unrecognised conclusion", func() { |
| 60 | + response := cli.Parse(commandChan1("subscribe owner/repo foo")) |
| 61 | + So(response["printToChannel"], ShouldBeNil) |
| 62 | + So(response["text"], ShouldContainSubstring, "conclusion") |
| 63 | + }) |
| 64 | + Convey("rejects a malformed filter", func() { |
| 65 | + response := cli.Parse(commandChan1("subscribe owner/repo foo:bar")) |
| 66 | + So(response["printToChannel"], ShouldBeNil) |
| 67 | + So(response["text"], ShouldContainSubstring, "filter") |
| 68 | + |
| 69 | + response = cli.Parse(commandChan1("subscribe owner/repo foo:bar:success")) |
| 70 | + So(response["printToChannel"], ShouldBeNil) |
| 71 | + So(response["text"], ShouldContainSubstring, "filter") |
| 72 | + }) |
| 73 | + Convey("rejects a duplicated filter", func() { |
| 74 | + response := cli.Parse(commandChan1("subscribe owner/repo success failure")) |
| 75 | + So(response["printToChannel"], ShouldBeNil) |
| 76 | + So(response["text"], ShouldContainSubstring, "duplicated") |
| 77 | + |
| 78 | + response = cli.Parse(commandChan1("subscribe owner/repo workflows:bar:success workflows:bar:failure")) |
| 79 | + So(response["printToChannel"], ShouldBeNil) |
| 80 | + So(response["text"], ShouldContainSubstring, "duplicated") |
| 81 | + }) |
| 82 | + Convey("accepts a well-formed filter", func() { |
| 83 | + response := cli.Parse(commandChan1("subscribe owner/repo workflows:workflow1:success failure")) |
| 84 | + So(response["response_type"], ShouldEqual, "in_channel") |
| 85 | + So(response["text"], ShouldContainSubstring, "Subscribed") |
| 86 | + So(response["text"], ShouldContainSubstring, "workflow1") |
| 87 | + }) |
| 88 | + Convey("overrides an existing subscription", func() { |
| 89 | + response := cli.Parse(commandChan1("subscribe owner/repo workflows:workflow2:success failure")) |
| 90 | + So(response["response_type"], ShouldEqual, "in_channel") |
| 91 | + So(response["text"], ShouldContainSubstring, "Subscribed") |
| 92 | + So(response["text"], ShouldContainSubstring, "workflow2") |
| 93 | + |
| 94 | + // Anachronistic usage of list command, this needs to be fixed |
| 95 | + response = cli.Parse(commandChan1("list owner/repo")) |
| 96 | + So(response["response_type"], ShouldEqual, "in_channel") |
| 97 | + So(response["text"], ShouldNotContainSubstring, "workflow1") |
| 98 | + }) |
| 99 | + }) |
| 100 | + Convey("When asked to list", func() { |
| 101 | + Convey("rejects an insufficient number of arguments", func() { |
| 102 | + response := cli.Parse(commandChan1("list")) |
| 103 | + So(response["response_type"], ShouldBeNil) |
| 104 | + So(response["text"], ShouldContainSubstring, "repo") |
| 105 | + }) |
| 106 | + Convey("correct lists subscribed channels", func() { |
| 107 | + cli.Parse(commandChan1("subscribe owner/repo")) |
| 108 | + cli.Parse(commandChan2("subscribe owner/repo workflows:workflow1 failure")) |
| 109 | + response := cli.Parse(commandChan1("list owner/repo")) |
| 110 | + So(response["response_type"], ShouldEqual, "in_channel") |
| 111 | + So(response["text"], ShouldContainSubstring, "TestChannelID1") |
| 112 | + So(response["text"], ShouldContainSubstring, "TestChannelID2") |
| 113 | + So(response["text"], ShouldContainSubstring, "workflow1") |
| 114 | + So(response["text"], ShouldContainSubstring, "failure") |
| 115 | + }) |
| 116 | + Convey("responds correctly if no channels are subscribed", func() { |
| 117 | + response := cli.Parse(commandChan1("list owner/repo")) |
| 118 | + So(response["response_type"], ShouldEqual, "in_channel") |
| 119 | + So(response["text"], ShouldContainSubstring, " no") |
| 120 | + }) |
| 121 | + }) |
| 122 | + Convey("When asked to unsubscribe", func() { |
| 123 | + Convey("rejects an insufficient number of arguments", func() { |
| 124 | + response := cli.Parse(commandChan1("unsubscribe")) |
| 125 | + So(response["response_type"], ShouldBeNil) |
| 126 | + So(response["text"], ShouldContainSubstring, "repo") |
| 127 | + }) |
| 128 | + Convey("notifies if the channel is not subscribed to the repo", func() { |
| 129 | + response := cli.Parse(commandChan1("unsubscribe owner/repo")) |
| 130 | + So(response["response_type"], ShouldBeNil) |
| 131 | + So(response["text"], ShouldContainSubstring, "subscribed") |
| 132 | + }) |
| 133 | + Convey("correctly unsubscribes from a channel", func() { |
| 134 | + cli.Parse(commandChan1("subscribe owner/repo")) |
| 135 | + cli.Parse(commandChan1("unsubscribe owner/repo")) |
| 136 | + response := cli.Parse(commandChan1("list owner/repo")) |
| 137 | + So(response["response_type"], ShouldEqual, "in_channel") |
| 138 | + So(response["text"], ShouldContainSubstring, " no") |
| 139 | + }) |
| 140 | + Convey("correctly unsubscribes from only the requested channel", func() { |
| 141 | + cli.Parse(commandChan1("subscribe owner/repo")) |
| 142 | + cli.Parse(commandChan2("subscribe owner/repo workflows:workflow1 failure")) |
| 143 | + cli.Parse(commandChan1("unsubscribe owner/repo")) |
| 144 | + response := cli.Parse(commandChan1("list owner/repo")) |
| 145 | + So(response["response_type"], ShouldEqual, "in_channel") |
| 146 | + So(response["text"], ShouldContainSubstring, "TestChannelID2") |
| 147 | + So(response["text"], ShouldContainSubstring, "workflow1") |
| 148 | + So(response["text"], ShouldContainSubstring, "failure") |
| 149 | + }) |
| 150 | + Convey("is able to resubscribe", func() { |
| 151 | + cli.Parse(commandChan1("subscribe owner/repo")) |
| 152 | + cli.Parse(commandChan1("unsubscribe owner/repo")) |
| 153 | + cli.Parse(commandChan1("subscribe owner/repo")) |
| 154 | + response := cli.Parse(commandChan1("list owner/repo")) |
| 155 | + So(response["response_type"], ShouldEqual, "in_channel") |
| 156 | + So(response["text"], ShouldContainSubstring, "TestChannelID1") |
| 157 | + }) |
| 158 | + }) |
| 159 | + }) |
| 160 | + Convey("When receiving webhooks, the bot", t, func() { |
| 161 | + Convey("has no tests at the moment", func() { |
| 162 | + }) |
| 163 | + }) |
| 164 | +} |
0 commit comments