Skip to content

Commit 8c8df81

Browse files
addede testcase for some functions in server/command.go
1 parent bcdca49 commit 8c8df81

2 files changed

Lines changed: 326 additions & 4 deletions

File tree

server/plugin/command.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func (p *Plugin) getMutedUsernames(userInfo *GitHubUserInfo) []string {
151151
return mutedUsers
152152
}
153153

154-
func (p *Plugin) handleMuteList(args *model.CommandArgs, userInfo *GitHubUserInfo) string {
154+
func (p *Plugin) handleMuteList(_ *model.CommandArgs, userInfo *GitHubUserInfo) string {
155155
mutedUsernames := p.getMutedUsernames(userInfo)
156156
var mutedUsers string
157157
for _, user := range mutedUsernames {
@@ -172,7 +172,7 @@ func contains(s []string, e string) bool {
172172
return false
173173
}
174174

175-
func (p *Plugin) handleMuteAdd(args *model.CommandArgs, username string, userInfo *GitHubUserInfo) string {
175+
func (p *Plugin) handleMuteAdd(_ *model.CommandArgs, username string, userInfo *GitHubUserInfo) string {
176176
mutedUsernames := p.getMutedUsernames(userInfo)
177177
if contains(mutedUsernames, username) {
178178
return username + " is already muted"
@@ -198,7 +198,7 @@ func (p *Plugin) handleMuteAdd(args *model.CommandArgs, username string, userInf
198198
return fmt.Sprintf("`%v`", username) + " is now muted. You'll no longer receive notifications for comments in your PRs and issues."
199199
}
200200

201-
func (p *Plugin) handleUnmute(args *model.CommandArgs, username string, userInfo *GitHubUserInfo) string {
201+
func (p *Plugin) handleUnmute(_ *model.CommandArgs, username string, userInfo *GitHubUserInfo) string {
202202
mutedUsernames := p.getMutedUsernames(userInfo)
203203
userToMute := []string{username}
204204
newMutedList := arrayDifference(mutedUsernames, userToMute)
@@ -211,7 +211,7 @@ func (p *Plugin) handleUnmute(args *model.CommandArgs, username string, userInfo
211211
return fmt.Sprintf("`%v`", username) + " is no longer muted"
212212
}
213213

214-
func (p *Plugin) handleUnmuteAll(args *model.CommandArgs, userInfo *GitHubUserInfo) string {
214+
func (p *Plugin) handleUnmuteAll(_ *model.CommandArgs, userInfo *GitHubUserInfo) string {
215215
_, err := p.store.Set(userInfo.UserID+"-muted-users", []byte(""))
216216
if err != nil {
217217
return "Error occurred unmuting users"

server/plugin/command_test.go

Lines changed: 322 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/mattermost/mattermost/server/public/plugin"
1010
"github.com/mattermost/mattermost/server/public/plugin/plugintest"
1111
"github.com/mattermost/mattermost/server/public/pluginapi"
12+
"github.com/pkg/errors"
1213
"github.com/stretchr/testify/assert"
1314
"github.com/stretchr/testify/mock"
1415
"github.com/stretchr/testify/require"
@@ -333,3 +334,324 @@ func TestExecuteCommand(t *testing.T) {
333334
})
334335
}
335336
}
337+
338+
func TestGetMutedUsernames(t *testing.T) {
339+
mockKvStore, mockAPI, _, _, _ := GetTestSetup(t)
340+
p := getPluginTest(mockAPI, mockKvStore)
341+
userInfo, err := GetMockGHUserInfo(p)
342+
assert.NoError(t, err)
343+
344+
tests := []struct {
345+
name string
346+
setup func()
347+
assertions func(t *testing.T, result []string)
348+
}{
349+
{
350+
name: "Error retrieving muted usernames",
351+
setup: func() {
352+
mockKvStore.EXPECT().Get("mockUserID-muted-users", gomock.Any()).Return(errors.New("error retrieving muted users")).Times(1)
353+
},
354+
assertions: func(t *testing.T, result []string) {
355+
assert.Nil(t, result)
356+
},
357+
},
358+
{
359+
name: "No muted usernames set for user",
360+
setup: func() {
361+
mockKvStore.EXPECT().Get("mockUserID-muted-users", gomock.Any()).DoAndReturn(func(key string, value *[]byte) error {
362+
*value = []byte("")
363+
return nil
364+
}).Times(1)
365+
},
366+
assertions: func(t *testing.T, result []string) {
367+
assert.Equal(t, []string(nil), result)
368+
},
369+
},
370+
{
371+
name: "Successfully retrieves muted usernames",
372+
setup: func() {
373+
mutedUsernames := []byte("user1,user2,user3")
374+
mockKvStore.EXPECT().Get("mockUserID-muted-users", gomock.Any()).DoAndReturn(func(key string, value *[]byte) error {
375+
*value = mutedUsernames
376+
return nil
377+
}).Times(1)
378+
},
379+
assertions: func(t *testing.T, result []string) {
380+
assert.Equal(t, []string{"user1", "user2", "user3"}, result)
381+
},
382+
},
383+
}
384+
for _, tc := range tests {
385+
t.Run(tc.name, func(t *testing.T) {
386+
tc.setup()
387+
388+
mutedUsernames := p.getMutedUsernames(userInfo)
389+
390+
tc.assertions(t, mutedUsernames)
391+
})
392+
}
393+
}
394+
395+
func TestHandleMuteList(t *testing.T) {
396+
mockKvStore, mockAPI, _, _, _ := GetTestSetup(t)
397+
p := getPluginTest(mockAPI, mockKvStore)
398+
userInfo, err := GetMockGHUserInfo(p)
399+
assert.NoError(t, err)
400+
401+
tests := []struct {
402+
name string
403+
setup func()
404+
assertions func(t *testing.T, result string)
405+
}{
406+
{
407+
name: "Error retrieving muted usernames",
408+
setup: func() {
409+
mockKvStore.EXPECT().Get("mockUserID-muted-users", gomock.Any()).Return(errors.New("error retrieving muted users")).Times(1)
410+
},
411+
assertions: func(t *testing.T, result string) {
412+
assert.Equal(t, "You have no muted users", result)
413+
},
414+
},
415+
{
416+
name: "No muted usernames set for user",
417+
setup: func() {
418+
mockKvStore.EXPECT().Get("mockUserID-muted-users", gomock.Any()).DoAndReturn(func(key string, value *[]byte) error {
419+
*value = []byte("")
420+
return nil
421+
}).Times(1)
422+
},
423+
assertions: func(t *testing.T, result string) {
424+
assert.Equal(t, "You have no muted users", result)
425+
},
426+
},
427+
{
428+
name: "Successfully retrieves and formats muted usernames",
429+
setup: func() {
430+
mutedUsernames := []byte("user1,user2,user3")
431+
mockKvStore.EXPECT().Get("mockUserID-muted-users", gomock.Any()).DoAndReturn(func(key string, value *[]byte) error {
432+
*value = mutedUsernames
433+
return nil
434+
}).Times(1)
435+
},
436+
assertions: func(t *testing.T, result string) {
437+
expectedOutput := "Your muted users:\n- user1\n- user2\n- user3\n"
438+
assert.Equal(t, expectedOutput, result)
439+
},
440+
},
441+
}
442+
for _, tc := range tests {
443+
t.Run(tc.name, func(t *testing.T) {
444+
tc.setup()
445+
446+
result := p.handleMuteList(nil, userInfo)
447+
448+
tc.assertions(t, result)
449+
})
450+
}
451+
}
452+
453+
func TestContains(t *testing.T) {
454+
tests := []struct {
455+
name string
456+
slice []string
457+
element string
458+
assertions func(t *testing.T, result bool)
459+
}{
460+
{
461+
name: "Element is present in slice",
462+
slice: []string{"expectedElement1", "expectedElement2", "expectedElement3"},
463+
element: "expectedElement2",
464+
assertions: func(t *testing.T, result bool) {
465+
assert.True(t, result)
466+
},
467+
},
468+
{
469+
name: "Element is not present in slice",
470+
slice: []string{"expectedElement1", "expectedElement2", "expectedElement3"},
471+
element: "expectedElement4",
472+
assertions: func(t *testing.T, result bool) {
473+
assert.False(t, result)
474+
},
475+
},
476+
{
477+
name: "Empty slice",
478+
slice: []string{},
479+
element: "expectedElement1",
480+
assertions: func(t *testing.T, result bool) {
481+
assert.False(t, result)
482+
},
483+
},
484+
}
485+
for _, tc := range tests {
486+
t.Run(tc.name, func(t *testing.T) {
487+
result := contains(tc.slice, tc.element)
488+
tc.assertions(t, result)
489+
})
490+
}
491+
}
492+
493+
func TestHandleMuteAdd(t *testing.T) {
494+
mockKvStore, mockAPI, _, _, _ := GetTestSetup(t)
495+
p := getPluginTest(mockAPI, mockKvStore)
496+
userInfo, err := GetMockGHUserInfo(p)
497+
assert.NoError(t, err)
498+
499+
tests := []struct {
500+
name string
501+
username string
502+
setup func()
503+
assertions func(t *testing.T, result string)
504+
}{
505+
{
506+
name: "Error saving the new muted username",
507+
username: "errorUser",
508+
setup: func() {
509+
mockKvStore.EXPECT().Get(userInfo.UserID+"-muted-users", gomock.Any()).DoAndReturn(func(key string, value *[]byte) error {
510+
*value = []byte("existingUser")
511+
return nil
512+
}).Times(1)
513+
mockKvStore.EXPECT().Set(userInfo.UserID+"-muted-users", []byte("existingUser,errorUser")).Return(false, errors.New("store error")).Times(1)
514+
},
515+
assertions: func(t *testing.T, result string) {
516+
assert.Equal(t, "Error occurred saving list of muted users", result)
517+
},
518+
},
519+
{
520+
name: "Username is already muted",
521+
username: "alreadyMutedUser",
522+
setup: func() {
523+
mockKvStore.EXPECT().Get(userInfo.UserID+"-muted-users", gomock.Any()).DoAndReturn(func(key string, value *[]byte) error {
524+
*value = []byte("alreadyMutedUser")
525+
return nil
526+
}).Times(1)
527+
},
528+
assertions: func(t *testing.T, result string) {
529+
assert.Equal(t, "alreadyMutedUser is already muted", result)
530+
},
531+
},
532+
{
533+
name: "Invalid username with comma",
534+
username: "invalid,user",
535+
setup: func() {
536+
mockKvStore.EXPECT().Get(userInfo.UserID+"-muted-users", gomock.Any()).DoAndReturn(func(key string, value *[]byte) error {
537+
*value = []byte("")
538+
return nil
539+
}).Times(1)
540+
},
541+
assertions: func(t *testing.T, result string) {
542+
assert.Equal(t, "Invalid username provided", result)
543+
},
544+
},
545+
{
546+
name: "Successfully adds new muted username",
547+
username: "newUser",
548+
setup: func() {
549+
mockKvStore.EXPECT().Get(userInfo.UserID+"-muted-users", gomock.Any()).DoAndReturn(func(key string, value *[]byte) error {
550+
*value = []byte("existingUser")
551+
return nil
552+
}).Times(1)
553+
mockKvStore.EXPECT().Set(userInfo.UserID+"-muted-users", []byte("existingUser,newUser")).Return(true, nil).Times(1)
554+
},
555+
assertions: func(t *testing.T, result string) {
556+
expectedMessage := "`newUser` is now muted. You'll no longer receive notifications for comments in your PRs and issues."
557+
assert.Equal(t, expectedMessage, result)
558+
},
559+
},
560+
}
561+
562+
for _, tc := range tests {
563+
t.Run(tc.name, func(t *testing.T) {
564+
tc.setup()
565+
result := p.handleMuteAdd(nil, tc.username, userInfo)
566+
tc.assertions(t, result)
567+
})
568+
}
569+
}
570+
571+
func TestHandleUnmute(t *testing.T) {
572+
mockKvStore, mockAPI, _, _, _ := GetTestSetup(t)
573+
p := getPluginTest(mockAPI, mockKvStore)
574+
userInfo, err := GetMockGHUserInfo(p)
575+
assert.NoError(t, err)
576+
577+
tests := []struct {
578+
name string
579+
username string
580+
setup func()
581+
expectedResult string
582+
}{
583+
{
584+
name: "Error occurred while unmuting the user",
585+
username: "user1",
586+
setup: func() {
587+
mutedUsernames := []byte("user1,user2,user3")
588+
mockKvStore.EXPECT().Get("mockUserID-muted-users", gomock.Any()).DoAndReturn(func(key string, value *[]byte) error {
589+
*value = mutedUsernames
590+
return nil
591+
}).Times(1)
592+
mockKvStore.EXPECT().Set(userInfo.UserID+"-muted-users", gomock.Any()).Return(false, errors.New("error saving muted users")).Times(1)
593+
},
594+
expectedResult: "Error occurred unmuting users",
595+
},
596+
{
597+
name: "Successfully unmute a user",
598+
username: "user1",
599+
setup: func() {
600+
mutedUsernames := []byte("user1,user2,user3")
601+
mockKvStore.EXPECT().Get("mockUserID-muted-users", gomock.Any()).DoAndReturn(func(key string, value *[]byte) error {
602+
*value = mutedUsernames
603+
return nil
604+
}).Times(1)
605+
mockKvStore.EXPECT().Set(userInfo.UserID+"-muted-users", gomock.Any()).Return(true, nil).Times(1)
606+
},
607+
expectedResult: "`user1` is no longer muted",
608+
},
609+
}
610+
for _, tc := range tests {
611+
t.Run(tc.name, func(t *testing.T) {
612+
tc.setup()
613+
result := p.handleUnmute(nil, tc.username, userInfo)
614+
assert.Equal(t, tc.expectedResult, result)
615+
})
616+
}
617+
}
618+
619+
func TestHandleUnmuteAll(t *testing.T) {
620+
mockKvStore, mockAPI, _, _, _ := GetTestSetup(t)
621+
p := getPluginTest(mockAPI, mockKvStore)
622+
userInfo, err := GetMockGHUserInfo(p)
623+
assert.NoError(t, err)
624+
625+
tests := []struct {
626+
name string
627+
setup func()
628+
assertions func(string)
629+
expectedResult string
630+
}{
631+
{
632+
name: "Error occurred while unmuting all users",
633+
setup: func() {
634+
mockKvStore.EXPECT().Set(userInfo.UserID+"-muted-users", []byte("")).Return(false, errors.New("error saving muted users")).Times(1)
635+
},
636+
assertions: func(expectedResult string) {
637+
assert.Equal(t, expectedResult, "Error occurred unmuting users")
638+
},
639+
},
640+
{
641+
name: "Successfully unmute all users",
642+
setup: func() {
643+
mockKvStore.EXPECT().Set(userInfo.UserID+"-muted-users", []byte("")).Return(true, nil).Times(1)
644+
},
645+
assertions: func(expectedResult string) {
646+
assert.Equal(t, expectedResult, "Unmuted all users")
647+
},
648+
},
649+
}
650+
for _, tc := range tests {
651+
t.Run(tc.name, func(t *testing.T) {
652+
tc.setup()
653+
result := p.handleUnmuteAll(nil, userInfo)
654+
tc.assertions(result)
655+
})
656+
}
657+
}

0 commit comments

Comments
 (0)