Skip to content

Commit 3cf5ba9

Browse files
Added testcase for more functions in api/command.go
1 parent df4695f commit 3cf5ba9

3 files changed

Lines changed: 354 additions & 3 deletions

File tree

server/plugin/command.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ func (p *Plugin) handleSubscriptions(c *plugin.Context, args *model.CommandArgs,
293293
}
294294
}
295295

296-
func (p *Plugin) handleSubscriptionsList(_ *plugin.Context, args *model.CommandArgs, parameters []string, _ *GitHubUserInfo) string {
296+
func (p *Plugin) handleSubscriptionsList(_ *plugin.Context, args *model.CommandArgs, _ []string, _ *GitHubUserInfo) string {
297297
txt := ""
298298
subs, err := p.GetSubscriptionsByChannel(args.ChannelId)
299299
if err != nil {
@@ -538,6 +538,7 @@ func (p *Plugin) getSubscribedFeatures(channelID, owner, repo string) (Features,
538538

539539
return previousFeatures, nil
540540
}
541+
541542
func (p *Plugin) handleUnsubscribe(_ *plugin.Context, args *model.CommandArgs, parameters []string, _ *GitHubUserInfo) string {
542543
if len(parameters) == 0 {
543544
return "Please specify a repository."
@@ -706,7 +707,7 @@ func (p *Plugin) handleIssue(_ *plugin.Context, args *model.CommandArgs, paramet
706707
}
707708
}
708709

709-
func (p *Plugin) handleSetup(c *plugin.Context, args *model.CommandArgs, parameters []string) string {
710+
func (p *Plugin) handleSetup(_ *plugin.Context, args *model.CommandArgs, parameters []string) string {
710711
userID := args.UserId
711712
isSysAdmin, err := p.isAuthorizedSysAdmin(userID)
712713
if err != nil {

server/plugin/command_test.go

Lines changed: 350 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,3 +655,353 @@ func TestHandleUnmuteAll(t *testing.T) {
655655
})
656656
}
657657
}
658+
659+
func TestHandleMuteCommand(t *testing.T) {
660+
mockKvStore, mockAPI, _, _, _ := GetTestSetup(t)
661+
p := getPluginTest(mockAPI, mockKvStore)
662+
userInfo, err := GetMockGHUserInfo(p)
663+
assert.NoError(t, err)
664+
665+
tests := []struct {
666+
name string
667+
parameters []string
668+
setup func()
669+
assertions func(*testing.T, string)
670+
}{
671+
{
672+
name: "Success - list muted users",
673+
parameters: []string{"list"},
674+
setup: func() {
675+
mutedUsernames := []byte("user1,user2,user3")
676+
mockKvStore.EXPECT().Get("mockUserID-muted-users", gomock.Any()).DoAndReturn(func(key string, value *[]byte) error {
677+
*value = mutedUsernames
678+
return nil
679+
}).Times(1)
680+
},
681+
assertions: func(t *testing.T, response string) {
682+
assert.Equal(t, "Your muted users:\n- user1\n- user2\n- user3\n", response)
683+
},
684+
},
685+
{
686+
name: "Success - add new muted user",
687+
parameters: []string{"add", "newUser"},
688+
setup: func() {
689+
mockKvStore.EXPECT().Get(userInfo.UserID+"-muted-users", gomock.Any()).DoAndReturn(func(key string, value *[]byte) error {
690+
*value = []byte("existingUser")
691+
return nil
692+
}).Times(1)
693+
mockKvStore.EXPECT().Set(userInfo.UserID+"-muted-users", []byte("existingUser,newUser")).Return(true, nil).Times(1)
694+
},
695+
assertions: func(t *testing.T, response string) {
696+
assert.Equal(t, "`newUser` is now muted. You'll no longer receive notifications for comments in your PRs and issues.", response)
697+
},
698+
},
699+
{
700+
name: "Error - invalid number of parameters for add",
701+
parameters: []string{"add"},
702+
setup: func() {},
703+
assertions: func(t *testing.T, response string) {
704+
assert.Equal(t, "Invalid number of parameters supplied to add", response)
705+
},
706+
},
707+
{
708+
name: "Success - delete muted user",
709+
parameters: []string{"delete", "user1"},
710+
setup: func() {
711+
mutedUsernames := []byte("user1,user2,user3")
712+
mockKvStore.EXPECT().Get("mockUserID-muted-users", gomock.Any()).DoAndReturn(func(key string, value *[]byte) error {
713+
*value = mutedUsernames
714+
return nil
715+
}).Times(1)
716+
mockKvStore.EXPECT().Set(userInfo.UserID+"-muted-users", gomock.Any()).Return(true, nil).Times(1)
717+
},
718+
assertions: func(t *testing.T, response string) {
719+
assert.Equal(t, "`user1` is no longer muted", response)
720+
},
721+
},
722+
{
723+
name: "Error - invalid number of parameters for delete",
724+
parameters: []string{"delete"},
725+
setup: func() {},
726+
assertions: func(t *testing.T, response string) {
727+
assert.Equal(t, "Invalid number of parameters supplied to delete", response)
728+
},
729+
},
730+
{
731+
name: "Success - delete all muted users",
732+
parameters: []string{"delete-all"},
733+
setup: func() {
734+
mockKvStore.EXPECT().Set(userInfo.UserID+"-muted-users", []byte("")).Return(true, nil).Times(1)
735+
},
736+
assertions: func(t *testing.T, response string) {
737+
assert.Equal(t, "Unmuted all users", response)
738+
},
739+
},
740+
{
741+
name: "Error - unknown subcommand",
742+
parameters: []string{"unknown"},
743+
setup: func() {},
744+
assertions: func(t *testing.T, response string) {
745+
assert.Equal(t, "Unknown subcommand unknown", response)
746+
},
747+
},
748+
{
749+
name: "Error - no parameters provided",
750+
parameters: []string{},
751+
setup: func() {},
752+
assertions: func(t *testing.T, response string) {
753+
assert.Equal(t, "Invalid mute command. Available commands are 'list', 'add' and 'delete'.", response)
754+
},
755+
},
756+
}
757+
for _, tc := range tests {
758+
t.Run(tc.name, func(t *testing.T) {
759+
tc.setup()
760+
result := p.handleMuteCommand(nil, nil, tc.parameters, userInfo)
761+
tc.assertions(t, result)
762+
})
763+
}
764+
}
765+
766+
func TestArrayDifference(t *testing.T) {
767+
tests := []struct {
768+
name string
769+
a []string
770+
b []string
771+
expected []string
772+
}{
773+
{
774+
name: "No difference - all elements in a are in b",
775+
a: []string{"apple", "banana", "cherry"},
776+
b: []string{"apple", "banana", "cherry"},
777+
expected: []string{},
778+
},
779+
{
780+
name: "Difference - some elements in a are not in b",
781+
a: []string{"apple", "banana", "cherry", "date"},
782+
b: []string{"apple", "banana"},
783+
expected: []string{"cherry", "date"},
784+
},
785+
{
786+
name: "All elements different - no elements in a are in b",
787+
a: []string{"apple", "banana"},
788+
b: []string{"cherry", "date"},
789+
expected: []string{"apple", "banana"},
790+
},
791+
{
792+
name: "Empty a - no elements to compare",
793+
a: []string{},
794+
b: []string{"apple", "banana"},
795+
expected: []string{},
796+
},
797+
{
798+
name: "Empty b - all elements in a should be returned",
799+
a: []string{"apple", "banana"},
800+
b: []string{},
801+
expected: []string{"apple", "banana"},
802+
},
803+
{
804+
name: "Both a and b empty - no elements to compare",
805+
a: []string{},
806+
b: []string{},
807+
expected: []string{},
808+
},
809+
}
810+
for _, tc := range tests {
811+
t.Run(tc.name, func(t *testing.T) {
812+
result := arrayDifference(tc.a, tc.b)
813+
assert.ElementsMatch(t, tc.expected, result)
814+
})
815+
}
816+
}
817+
818+
func TestHandleSubscriptionsList(t *testing.T) {
819+
mockKvStore, mockAPI, _, _, _ := GetTestSetup(t)
820+
p := getPluginTest(mockAPI, mockKvStore)
821+
822+
tests := []struct {
823+
name string
824+
channelID string
825+
setup func()
826+
assertions func(t *testing.T, result string)
827+
}{
828+
{
829+
name: "Error retrieving subscriptions",
830+
channelID: "channel1",
831+
setup: func() {
832+
mockKvStore.EXPECT().Get(SubscriptionsKey, gomock.Any()).Return(errors.New("store error")).Times(1)
833+
},
834+
assertions: func(t *testing.T, result string) {
835+
assert.Contains(t, result, "could not get subscriptions from KVStore: store error")
836+
},
837+
},
838+
{
839+
name: "No subscriptions in the channel",
840+
channelID: "channel2",
841+
setup: func() {
842+
mockKvStore.EXPECT().Get(SubscriptionsKey, gomock.Any()).DoAndReturn(func(key string, value **Subscriptions) error {
843+
*value = &Subscriptions{Repositories: map[string][]*Subscription{}}
844+
return nil
845+
}).Times(1)
846+
},
847+
assertions: func(t *testing.T, result string) {
848+
assert.Equal(t, "Currently there are no subscriptions in this channel", result)
849+
},
850+
},
851+
{
852+
name: "Multiple subscriptions in the channel",
853+
channelID: "channel3",
854+
setup: func() {
855+
mockKvStore.EXPECT().Get(SubscriptionsKey, gomock.Any()).DoAndReturn(func(key string, value **Subscriptions) error {
856+
*value = &Subscriptions{
857+
Repositories: map[string][]*Subscription{
858+
"repo1": {
859+
{
860+
ChannelID: "channel3",
861+
Repository: "repo1",
862+
},
863+
{
864+
ChannelID: "channel4",
865+
Repository: "repo1",
866+
},
867+
},
868+
"repo2": {
869+
{
870+
ChannelID: "channel3",
871+
Repository: "repo2",
872+
},
873+
},
874+
},
875+
}
876+
return nil
877+
}).Times(1)
878+
},
879+
assertions: func(t *testing.T, result string) {
880+
expected := "### Subscriptions in this channel\n" +
881+
"* `repo1` - \n" +
882+
"* `repo2` - \n"
883+
assert.Equal(t, expected, result)
884+
},
885+
},
886+
}
887+
888+
for _, tc := range tests {
889+
t.Run(tc.name, func(t *testing.T) {
890+
tc.setup()
891+
result := p.handleSubscriptionsList(nil, &model.CommandArgs{ChannelId: tc.channelID}, nil, nil)
892+
tc.assertions(t, result)
893+
})
894+
}
895+
}
896+
897+
func TestGetSubscribedFeatures(t *testing.T) {
898+
mockKvStore, mockAPI, _, _, _ := GetTestSetup(t)
899+
p := getPluginTest(mockAPI, mockKvStore)
900+
901+
tests := []struct {
902+
name string
903+
channelID string
904+
owner string
905+
repo string
906+
setup func()
907+
assertions func(t *testing.T, features Features, err error)
908+
}{
909+
{
910+
name: "Error retrieving subscriptions",
911+
channelID: "channel1",
912+
owner: "owner1",
913+
repo: "repo1",
914+
setup: func() {
915+
mockKvStore.EXPECT().Get(SubscriptionsKey, gomock.Any()).Return(errors.New("store error")).Times(1)
916+
},
917+
assertions: func(t *testing.T, features Features, err error) {
918+
assert.Error(t, err)
919+
assert.ErrorContains(t, err, "store error")
920+
assert.Empty(t, features)
921+
},
922+
},
923+
{
924+
name: "No subscriptions in the channel",
925+
channelID: "channel2",
926+
owner: "owner2",
927+
repo: "repo2",
928+
setup: func() {
929+
mockKvStore.EXPECT().Get(SubscriptionsKey, gomock.Any()).DoAndReturn(func(key string, value **Subscriptions) error {
930+
*value = &Subscriptions{Repositories: map[string][]*Subscription{}}
931+
return nil
932+
}).Times(1)
933+
},
934+
assertions: func(t *testing.T, features Features, err error) {
935+
assert.NoError(t, err)
936+
assert.Empty(t, features)
937+
},
938+
},
939+
{
940+
name: "Subscribed features found for repo",
941+
channelID: "channel3",
942+
owner: "owner3",
943+
repo: "repo3",
944+
setup: func() {
945+
mockKvStore.EXPECT().Get(SubscriptionsKey, gomock.Any()).DoAndReturn(func(key string, value **Subscriptions) error {
946+
*value = &Subscriptions{
947+
Repositories: map[string][]*Subscription{
948+
"owner3/repo3": {
949+
{
950+
ChannelID: "channel3",
951+
Repository: "owner3/repo3",
952+
Features: Features("FeatureA"),
953+
},
954+
},
955+
"owner4/repo4": {
956+
{
957+
ChannelID: "channel4",
958+
Repository: "owner4/repo4",
959+
Features: Features("FeatureB"),
960+
},
961+
},
962+
},
963+
}
964+
return nil
965+
}).Times(1)
966+
},
967+
assertions: func(t *testing.T, features Features, err error) {
968+
assert.NoError(t, err)
969+
expectedFeatures := Features("FeatureA")
970+
assert.Equal(t, expectedFeatures, features)
971+
},
972+
},
973+
{
974+
name: "Subscribed features not found for repo",
975+
channelID: "channel4",
976+
owner: "owner5",
977+
repo: "repo5",
978+
setup: func() {
979+
mockKvStore.EXPECT().Get(SubscriptionsKey, gomock.Any()).DoAndReturn(func(key string, value **Subscriptions) error {
980+
*value = &Subscriptions{
981+
Repositories: map[string][]*Subscription{
982+
"owner6/repo6": {
983+
{
984+
ChannelID: "channel4",
985+
Repository: "owner6/repo6",
986+
Features: Features("FeatureC"),
987+
},
988+
},
989+
},
990+
}
991+
return nil
992+
}).Times(1)
993+
},
994+
assertions: func(t *testing.T, features Features, err error) {
995+
assert.NoError(t, err)
996+
assert.Empty(t, features)
997+
},
998+
},
999+
}
1000+
for _, tc := range tests {
1001+
t.Run(tc.name, func(t *testing.T) {
1002+
tc.setup()
1003+
features, err := p.getSubscribedFeatures(tc.channelID, tc.owner, tc.repo)
1004+
tc.assertions(t, features, err)
1005+
})
1006+
}
1007+
}

server/plugin/plugin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ func (p *Plugin) GetGitHubClient(ctx context.Context, userID string) (*github.Cl
170170
return p.githubConnectUser(ctx, userInfo), nil
171171
}
172172

173-
func (p *Plugin) githubConnectUser(ctx context.Context, info *GitHubUserInfo) *github.Client {
173+
func (p *Plugin) githubConnectUser(_ context.Context, info *GitHubUserInfo) *github.Client {
174174
tok := *info.Token
175175
return p.githubConnectToken(tok)
176176
}

0 commit comments

Comments
 (0)