Skip to content

Commit e435c76

Browse files
Added testcase for more functions in server/command.go (#846)
* Template PR for server unit test cases * Fixed lint errors * added testcase for some functions in server/api.go * addede testcase for some functions in server/command.go * Fixed lint issue * added testcase for server/api.go UpdateSetting function * Added testcase for more functions in api/command.go * review fixes * fixed lint * added license to new file
1 parent 6c8df66 commit e435c76

File tree

2 files changed

+351
-0
lines changed

2 files changed

+351
-0
lines changed

server/plugin/command.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,7 @@ func (p *Plugin) getSubscribedFeatures(channelID, owner, repo string) (Features,
584584

585585
return previousFeatures, nil
586586
}
587+
587588
func (p *Plugin) handleUnsubscribe(_ *plugin.Context, args *model.CommandArgs, parameters []string, _ *GitHubUserInfo) string {
588589
if len(parameters) == 0 {
589590
return "Please specify a repository."

server/plugin/command_test.go

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

0 commit comments

Comments
 (0)