-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update go.mod and go.sum to add goconvey
- Loading branch information
Showing
3 changed files
with
178 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
package slack | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/oursky/github-actions-manager/pkg/kv" | ||
"github.com/slack-go/slack" | ||
. "github.com/smartystreets/goconvey/convey" | ||
|
||
"go.uber.org/zap" | ||
) | ||
|
||
func TestSpec(t *testing.T) { | ||
testCommand := "test-gha" | ||
|
||
NewTestSlackChannel := func(channelID string) (string, func(string) slack.SlashCommand) { | ||
return channelID, func(command string) slack.SlashCommand { | ||
return slack.SlashCommand{ | ||
ChannelID: channelID, | ||
Command: "/" + testCommand, | ||
Text: command, | ||
} | ||
} | ||
} | ||
channelID1, commandFromChannel1 := NewTestSlackChannel("TestChannelID1") | ||
channelID2, commandFromChannel2 := NewTestSlackChannel("TestChannelID2") | ||
|
||
Convey("When receiving commands, the bot", t, func() { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
testApp := &App{ | ||
logger: zap.NewNop(), | ||
store: kv.NewInMemoryStore(), | ||
commandName: testCommand, | ||
} | ||
|
||
cli := NewCLI(testApp.logger) | ||
cli.SetContext(ctx, testApp) | ||
|
||
Convey("responds", func() { | ||
response := cli.Parse(commandFromChannel1("meow")) | ||
So(response["text"], ShouldEqual, "meow") | ||
}) | ||
Convey("rejects unrecognised commmands", func() { | ||
response := cli.Parse(commandFromChannel1("fhqwhgads")) | ||
So(response["response_type"], ShouldBeNil) | ||
So(response["text"], ShouldContainSubstring, "fhqwhgads") | ||
}) | ||
Convey("When asked to subscribe", func() { | ||
Convey("rejects an insufficient number of arguments", func() { | ||
response := cli.Parse(commandFromChannel1("subscribe")) | ||
So(response["printToChannel"], ShouldBeNil) | ||
So(response["text"], ShouldContainSubstring, "repo") | ||
}) | ||
Convey("rejects an unrecognised conclusion", func() { | ||
response := cli.Parse(commandFromChannel1("subscribe owner/repo foo")) | ||
So(response["printToChannel"], ShouldBeNil) | ||
So(response["text"], ShouldContainSubstring, "conclusion") | ||
}) | ||
Convey("rejects a malformed filter", func() { | ||
response := cli.Parse(commandFromChannel1("subscribe owner/repo foo:bar")) | ||
So(response["printToChannel"], ShouldBeNil) | ||
So(response["text"], ShouldContainSubstring, "filter") | ||
|
||
response = cli.Parse(commandFromChannel1("subscribe owner/repo foo:bar:success")) | ||
So(response["printToChannel"], ShouldBeNil) | ||
So(response["text"], ShouldContainSubstring, "filter") | ||
}) | ||
Convey("rejects a duplicated filter", func() { | ||
response := cli.Parse(commandFromChannel1("subscribe owner/repo success failure")) | ||
So(response["printToChannel"], ShouldBeNil) | ||
So(response["text"], ShouldContainSubstring, "duplicated") | ||
|
||
response = cli.Parse(commandFromChannel1("subscribe owner/repo workflows:bar:success workflows:bar:failure")) | ||
So(response["printToChannel"], ShouldBeNil) | ||
So(response["text"], ShouldContainSubstring, "duplicated") | ||
}) | ||
Convey("accepts a well-formed filter", func() { | ||
response := cli.Parse(commandFromChannel1("subscribe owner/repo workflows:workflow1:success failure")) | ||
So(response["response_type"], ShouldEqual, "in_channel") | ||
So(response["text"], ShouldContainSubstring, "Subscribed") | ||
So(response["text"], ShouldContainSubstring, "workflow1") | ||
}) | ||
Convey("overrides an existing subscription", func() { | ||
response := cli.Parse(commandFromChannel1("subscribe owner/repo workflows:workflow2:success failure")) | ||
So(response["response_type"], ShouldEqual, "in_channel") | ||
So(response["text"], ShouldContainSubstring, "Subscribed") | ||
So(response["text"], ShouldContainSubstring, "workflow2") | ||
|
||
// Anachronistic usage of list command, this needs to be fixed | ||
response = cli.Parse(commandFromChannel1("list owner/repo")) | ||
So(response["response_type"], ShouldEqual, "in_channel") | ||
So(response["text"], ShouldNotContainSubstring, "workflow1") | ||
}) | ||
}) | ||
Convey("When asked to list", func() { | ||
Convey("rejects an insufficient number of arguments", func() { | ||
response := cli.Parse(commandFromChannel1("list")) | ||
So(response["response_type"], ShouldBeNil) | ||
So(response["text"], ShouldContainSubstring, "repo") | ||
}) | ||
Convey("correct lists subscribed channels", func() { | ||
cli.Parse(commandFromChannel1("subscribe owner/repo")) | ||
cli.Parse(commandFromChannel2("subscribe owner/repo workflows:workflow1 failure")) | ||
response := cli.Parse(commandFromChannel1("list owner/repo")) | ||
So(response["response_type"], ShouldEqual, "in_channel") | ||
So(response["text"], ShouldContainSubstring, channelID1) | ||
So(response["text"], ShouldContainSubstring, channelID2) | ||
So(response["text"], ShouldContainSubstring, "workflow1") | ||
So(response["text"], ShouldContainSubstring, "failure") | ||
}) | ||
Convey("responds correctly if no channels are subscribed", func() { | ||
response := cli.Parse(commandFromChannel1("list owner/repo")) | ||
So(response["response_type"], ShouldEqual, "in_channel") | ||
So(response["text"], ShouldContainSubstring, " no") | ||
}) | ||
}) | ||
Convey("When asked to unsubscribe", func() { | ||
Convey("rejects an insufficient number of arguments", func() { | ||
response := cli.Parse(commandFromChannel1("unsubscribe")) | ||
So(response["response_type"], ShouldBeNil) | ||
So(response["text"], ShouldContainSubstring, "repo") | ||
}) | ||
Convey("notifies if the channel is not subscribed to the repo", func() { | ||
response := cli.Parse(commandFromChannel1("unsubscribe owner/repo")) | ||
So(response["response_type"], ShouldBeNil) | ||
So(response["text"], ShouldContainSubstring, "subscribed") | ||
}) | ||
Convey("correctly unsubscribes from a channel", func() { | ||
cli.Parse(commandFromChannel1("subscribe owner/repo")) | ||
cli.Parse(commandFromChannel1("unsubscribe owner/repo")) | ||
response := cli.Parse(commandFromChannel1("list owner/repo")) | ||
So(response["response_type"], ShouldEqual, "in_channel") | ||
So(response["text"], ShouldContainSubstring, " no") | ||
}) | ||
Convey("correctly unsubscribes from only the requested channel", func() { | ||
cli.Parse(commandFromChannel1("subscribe owner/repo")) | ||
cli.Parse(commandFromChannel2("subscribe owner/repo workflows:workflow1 failure")) | ||
cli.Parse(commandFromChannel1("unsubscribe owner/repo")) | ||
response := cli.Parse(commandFromChannel1("list owner/repo")) | ||
So(response["response_type"], ShouldEqual, "in_channel") | ||
So(response["text"], ShouldContainSubstring, channelID2) | ||
So(response["text"], ShouldContainSubstring, "workflow1") | ||
So(response["text"], ShouldContainSubstring, "failure") | ||
}) | ||
Convey("is able to resubscribe", func() { | ||
cli.Parse(commandFromChannel1("subscribe owner/repo")) | ||
cli.Parse(commandFromChannel1("unsubscribe owner/repo")) | ||
cli.Parse(commandFromChannel1("subscribe owner/repo")) | ||
response := cli.Parse(commandFromChannel1("list owner/repo")) | ||
So(response["response_type"], ShouldEqual, "in_channel") | ||
So(response["text"], ShouldContainSubstring, channelID1) | ||
}) | ||
}) | ||
}) | ||
Convey("When receiving webhooks, the bot", t, func() { | ||
Convey("has no tests at the moment", func() { | ||
}) | ||
}) | ||
} |