-
Notifications
You must be signed in to change notification settings - Fork 177
Expand file tree
/
Copy pathcommand_test.go
More file actions
1958 lines (1871 loc) · 59.2 KB
/
Copy pathcommand_test.go
File metadata and controls
1958 lines (1871 loc) · 59.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2018-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package plugin
import (
"fmt"
"os"
"path/filepath"
"testing"
"github.com/golang/mock/gomock"
"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/public/plugin"
"github.com/mattermost/mattermost/server/public/plugin/plugintest"
"github.com/mattermost/mattermost/server/public/pluginapi"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/mattermost/mattermost-plugin-github/server/mocks"
)
// Function to get the plugin object for test cases.
func getPluginTest(api *plugintest.API, mockKvStore *mocks.MockKvStore) *Plugin {
p := NewPlugin()
p.setConfiguration(
&Configuration{
GitHubOrg: "mockOrg",
GitHubOAuthClientID: "mockID",
GitHubOAuthClientSecret: "mockSecret",
EncryptionKey: "mockKey123456789",
})
p.initializeAPI()
p.store = mockKvStore
p.BotUserID = MockBotID
p.SetAPI(api)
p.client = pluginapi.NewClient(api, p.Driver)
return p
}
func TestValidateFeatures(t *testing.T) {
type output struct {
valid bool
invalidFeatures []string
}
tests := []struct {
name string
args []string
want output
}{
{
name: "all features valid",
args: []string{"creates", "pushes", "issue_comments"},
want: output{true, []string{}},
},
{
name: "all features invalid",
args: []string{"create", "push"},
want: output{false, []string{"create", "push"}},
},
{
name: "first feature invalid",
args: []string{"create", "pushes", "issue_comments"},
want: output{false, []string{"create"}},
},
{
name: "last feature invalid",
args: []string{"creates", "push"},
want: output{false, []string{"push"}},
},
{
name: "multiple features invalid",
args: []string{"create", "pushes", "issue"},
want: output{false, []string{"create", "issue"}},
},
{
name: "all features valid with label but issues and pulls missing",
args: []string{"pushes", `label:"ruby"`},
want: output{false, []string{}},
},
{
name: "all features valid with label and issues in features",
args: []string{"issues", `label:"ruby"`},
want: output{true, []string{}},
},
{
name: "all features valid with label and pulls in features",
args: []string{"pulls", `label:"ruby"`},
want: output{true, []string{}},
},
{
name: "multiple features invalid with label but issues and pulls missing",
args: []string{"issue", "push", `label:"ruby"`},
want: output{false, []string{"issue", "push"}},
},
{
name: "multiple features invalid with label and issues in features",
args: []string{"issues", "push", "create", `label:"ruby"`},
want: output{false, []string{"push", "create"}},
},
{
name: "multiple features invalid with label and pulls in features",
args: []string{"pulls", "push", "create", `label:"ruby"`},
want: output{false, []string{"push", "create"}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ok, fs := validateFeatures(tt.args)
got := output{ok, fs}
testFailureMessage := fmt.Sprintf("validateFeatures() = %v, want %v", got, tt.want)
assert.EqualValues(t, tt.want, got, testFailureMessage)
})
}
}
func TestParseCommand(t *testing.T) {
type output struct {
command string
action string
parameters []string
}
tt := []struct {
name string
input string
want output
}{
{
name: "no parameters",
input: "/github subscribe",
want: output{
"/github",
"subscribe",
[]string(nil),
},
},
{
name: "no action and no parameters",
input: "/github",
want: output{
"/github",
"",
[]string(nil),
},
},
{
name: "simple one-word label",
input: `/github subscribe DHaussermann/hello-world issues,label:"Help"`,
want: output{
"/github",
"subscribe",
[]string{"DHaussermann/hello-world", `issues,label:"Help"`},
},
},
{
name: "two-word label",
input: `/github subscribe DHaussermann/hello-world issues,label:"Help Wanted"`,
want: output{
"/github",
"subscribe",
[]string{"DHaussermann/hello-world", `issues,label:"Help Wanted"`},
},
},
{
name: "multi-word label",
input: `/github subscribe DHaussermann/hello-world issues,label:"Good First Issue"`,
want: output{
"/github",
"subscribe",
[]string{"DHaussermann/hello-world", `issues,label:"Good First Issue"`},
},
},
{
name: "multiple spaces inside double-quotes",
input: `/github subscribe DHaussermann/hello-world issues,label:"Help Wanted"`,
want: output{
"/github",
"subscribe",
[]string{"DHaussermann/hello-world", `issues,label:"Help Wanted"`},
},
},
{
name: "multiple spaces outside of double-quotes",
input: ` /github subscribe DHaussermann/hello-world issues,label:"Help Wanted"`,
want: output{
"/github",
"subscribe",
[]string{"DHaussermann/hello-world", `issues,label:"Help Wanted"`},
},
},
{
name: "trailing whitespaces",
input: `/github subscribe DHaussermann/hello-world issues,label:"Help Wanted" `,
want: output{
"/github",
"subscribe",
[]string{"DHaussermann/hello-world", `issues,label:"Help Wanted"`},
},
},
{
name: "non-ASCII characters",
input: `/github subscribe طماطم issues,label:"日本語"`,
want: output{
"/github",
"subscribe",
[]string{"طماطم", `issues,label:"日本語"`},
},
},
{
name: "line breaks",
input: "/github \nsubscribe\nDHaussermann/hello-world\nissues,label:\"Good First Issue\"",
want: output{
"/github",
"subscribe",
[]string{"DHaussermann/hello-world", `issues,label:"Good First Issue"`},
},
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
command, action, parameters := parseCommand(tc.input)
got := output{command, action, parameters}
testFailureMessage := fmt.Sprintf("validateFeatures() = %v, want %v", got, tc.want)
assert.EqualValues(t, tc.want, got, testFailureMessage)
})
}
}
func TestCheckConflictingFeatures(t *testing.T) {
type output struct {
valid bool
conflictingFeatures []string
}
tests := []struct {
name string
args []string
want output
}{
{
name: "no conflicts",
args: []string{"creates", "pushes", "issue_comments"},
want: output{true, nil},
},
{
name: "conflict with issue and issue creation",
args: []string{"pulls", "issues", "issue_creations"},
want: output{false, []string{"issues", "issue_creations"}},
},
{
name: "conflict with pulls and pulls created",
args: []string{"pulls", "issues", "pulls_created"},
want: output{false, []string{"pulls", "pulls_created"}},
},
{
name: "conflict with pulls and pulls merged",
args: []string{"pulls", "pushes", "pulls_merged"},
want: output{false, []string{"pulls", "pulls_merged"}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ok, fs := checkFeatureConflict(tt.args)
got := output{ok, fs}
testFailureMessage := fmt.Sprintf("checkFeatureConflict() = %v, want %v", got, tt.want)
assert.EqualValues(t, tt.want, got, testFailureMessage)
})
}
}
func TestExecuteCommand(t *testing.T) {
tests := map[string]struct {
commandArgs *model.CommandArgs
expectedMsg string
SetupMockStore func(*mocks.MockKvStore)
}{
"about command": {
commandArgs: &model.CommandArgs{Command: "/github about"},
expectedMsg: "GitHub version",
SetupMockStore: func(mks *mocks.MockKvStore) {},
},
"help command": {
commandArgs: &model.CommandArgs{Command: "/github help", ChannelId: "test-channelID", RootId: "test-rootID", UserId: "test-userID"},
expectedMsg: "###### Mattermost GitHub Plugin - Slash Command Help\n",
SetupMockStore: func(mks *mocks.MockKvStore) {},
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
isSendEphemeralPostCalled := false
// Controller for the mocks generated using mockgen
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
mockKvStore := mocks.NewMockKvStore(mockCtrl)
tt.SetupMockStore(mockKvStore)
currentTestAPI := &plugintest.API{}
currentTestAPI.On("SendEphemeralPost", mock.AnythingOfType("string"), mock.AnythingOfType("*model.Post")).Run(func(args mock.Arguments) {
isSendEphemeralPostCalled = true
post := args.Get(1).(*model.Post)
// Checking the contents of the post
assert.Contains(t, post.Message, tt.expectedMsg)
}).Once().Return(&model.Post{})
p := getPluginTest(currentTestAPI, mockKvStore)
_, err := p.ExecuteCommand(&plugin.Context{}, tt.commandArgs)
require.Nil(t, err)
assert.Equal(t, true, isSendEphemeralPostCalled)
})
}
}
func TestGetMutedUsernames(t *testing.T) {
mockKvStore, mockAPI, _, _, _ := GetTestSetup(t)
p := getPluginTest(mockAPI, mockKvStore)
userInfo, err := GetMockGHUserInfo(p)
assert.NoError(t, err)
tests := []struct {
name string
setup func()
assertions func(t *testing.T, result []string, err error)
}{
{
name: "Error retrieving muted usernames",
setup: func() {
mockKvStore.EXPECT().Get("mockUserID-muted-users", gomock.Any()).Return(errors.New("error retrieving muted users")).Times(1)
},
assertions: func(t *testing.T, result []string, err error) {
assert.Nil(t, result)
assert.ErrorContains(t, err, "error retrieving muted users")
},
},
{
name: "No muted usernames set for user",
setup: func() {
mockKvStore.EXPECT().Get("mockUserID-muted-users", gomock.Any()).DoAndReturn(func(key string, value *[]byte) error {
*value = []byte("")
return nil
}).Times(1)
},
assertions: func(t *testing.T, result []string, _ error) {
assert.Equal(t, []string(nil), result)
},
},
{
name: "Successfully retrieves muted usernames",
setup: func() {
mutedUsernames := []byte("user1,user2,user3")
mockKvStore.EXPECT().Get("mockUserID-muted-users", gomock.Any()).DoAndReturn(func(key string, value *[]byte) error {
*value = mutedUsernames
return nil
}).Times(1)
},
assertions: func(t *testing.T, result []string, _ error) {
assert.Equal(t, []string{"user1", "user2", "user3"}, result)
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
tc.setup()
mutedUsernames, err := p.getMutedUsernames(userInfo)
tc.assertions(t, mutedUsernames, err)
})
}
}
func TestHandleMuteList(t *testing.T) {
mockKvStore, mockAPI, _, _, _ := GetTestSetup(t)
p := getPluginTest(mockAPI, mockKvStore)
userInfo, err := GetMockGHUserInfo(p)
assert.NoError(t, err)
tests := []struct {
name string
setup func()
assertions func(t *testing.T, result string)
}{
{
name: "Error retrieving muted usernames",
setup: func() {
mockAPI.On("LogError", "error occurred getting muted users.", "UserID", userInfo.UserID, "Error", mock.Anything)
mockKvStore.EXPECT().Get("mockUserID-muted-users", gomock.Any()).Return(errors.New("error retrieving muted users")).Times(1)
},
assertions: func(t *testing.T, result string) {
assert.Equal(t, "An error occurred getting muted users. Please try again later", result)
},
},
{
name: "No muted usernames set for user",
setup: func() {
mockKvStore.EXPECT().Get("mockUserID-muted-users", gomock.Any()).DoAndReturn(func(key string, value *[]byte) error {
*value = []byte("")
return nil
}).Times(1)
},
assertions: func(t *testing.T, result string) {
assert.Equal(t, "You have no muted users", result)
},
},
{
name: "Successfully retrieves and formats muted usernames",
setup: func() {
mutedUsernames := []byte("user1,user2,user3")
mockKvStore.EXPECT().Get("mockUserID-muted-users", gomock.Any()).DoAndReturn(func(key string, value *[]byte) error {
*value = mutedUsernames
return nil
}).Times(1)
},
assertions: func(t *testing.T, result string) {
expectedOutput := "Your muted users:\n- user1\n- user2\n- user3\n"
assert.Equal(t, expectedOutput, result)
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
tc.setup()
result := p.handleMuteList(nil, userInfo)
tc.assertions(t, result)
})
}
}
func TestContains(t *testing.T) {
tests := []struct {
name string
slice []string
element string
assertions func(t *testing.T, result bool)
}{
{
name: "Element is present in slice",
slice: []string{"expectedElement1", "expectedElement2", "expectedElement3"},
element: "expectedElement2",
assertions: func(t *testing.T, result bool) {
assert.True(t, result)
},
},
{
name: "Element is not present in slice",
slice: []string{"expectedElement1", "expectedElement2", "expectedElement3"},
element: "expectedElement4",
assertions: func(t *testing.T, result bool) {
assert.False(t, result)
},
},
{
name: "Empty slice",
slice: []string{},
element: "expectedElement1",
assertions: func(t *testing.T, result bool) {
assert.False(t, result)
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
result := contains(tc.slice, tc.element)
tc.assertions(t, result)
})
}
}
func TestHandleMuteAdd(t *testing.T) {
mockKvStore, mockAPI, _, _, _ := GetTestSetup(t)
p := getPluginTest(mockAPI, mockKvStore)
userInfo, err := GetMockGHUserInfo(p)
assert.NoError(t, err)
tests := []struct {
name string
username string
setup func()
assertions func(t *testing.T, result string)
}{
{
name: "Error retrieving muted usernames",
setup: func() {
mockAPI.On("LogError", "error occurred getting muted users.", "UserID", userInfo.UserID, "Error", mock.Anything)
mockKvStore.EXPECT().Get("mockUserID-muted-users", gomock.Any()).Return(errors.New("error retrieving muted users")).Times(1)
},
assertions: func(t *testing.T, result string) {
assert.Equal(t, "An error occurred getting muted users. Please try again later", result)
},
},
{
name: "Username is already muted",
username: "alreadyMutedUser",
setup: func() {
mockKvStore.EXPECT().Get(userInfo.UserID+"-muted-users", gomock.Any()).DoAndReturn(func(key string, value *[]byte) error {
*value = []byte("alreadyMutedUser")
return nil
}).Times(1)
},
assertions: func(t *testing.T, result string) {
assert.Equal(t, "alreadyMutedUser is already muted", result)
},
},
// Can not mock API call using github client
// {
// name: "Error saving the new muted username",
// username: "errorUser",
// setup: func() {
// mockKvStore.EXPECT().Get(userInfo.UserID+"-muted-users", gomock.Any()).DoAndReturn(func(key string, value *[]byte) error {
// *value = []byte("existingUser")
// return nil
// }).Times(1)
// mockKvStore.EXPECT().Set(userInfo.UserID+"-muted-users", []byte("existingUser,errorUser")).Return(false, errors.New("store error")).Times(1)
// },
// assertions: func(t *testing.T, result string) {
// assert.Equal(t, "Error occurred saving list of muted users", result)
// },
// },
// {
// name: "Invalid username with comma",
// username: "invalid,user",
// setup: func() {
// mockKvStore.EXPECT().Get(userInfo.UserID+"-muted-users", gomock.Any()).DoAndReturn(func(key string, value *[]byte) error {
// *value = []byte("")
// return nil
// }).Times(1)
// },
// assertions: func(t *testing.T, result string) {
// assert.Equal(t, "Invalid username provided", result)
// },
// },
// {
// name: "Successfully adds first muted username",
// username: "firstUser",
// setup: func() {
// mockKvStore.EXPECT().Get(userInfo.UserID+"-muted-users", gomock.Any()).DoAndReturn(func(key string, value *[]byte) error {
// *value = []byte("")
// return nil
// }).Times(1)
// mockKvStore.EXPECT().Set(userInfo.UserID+"-muted-users", []byte("firstUser")).Return(true, nil).Times(1)
// },
// assertions: func(t *testing.T, result string) {
// expectedMessage := "`firstUser` is now muted. You'll no longer receive notifications for comments in your PRs and issues."
// assert.Equal(t, expectedMessage, result)
// },
// },
// {
// name: "Successfully adds new muted username",
// username: "newUser",
// setup: func() {
// mockKvStore.EXPECT().Get(userInfo.UserID+"-muted-users", gomock.Any()).DoAndReturn(func(key string, value *[]byte) error {
// *value = []byte("existingUser")
// return nil
// }).Times(1)
// mockKvStore.EXPECT().Set(userInfo.UserID+"-muted-users", []byte("existingUser,newUser")).Return(true, nil).Times(1)
// },
// assertions: func(t *testing.T, result string) {
// expectedMessage := "`newUser` is now muted. You'll no longer receive notifications for comments in your PRs and issues."
// assert.Equal(t, expectedMessage, result)
// },
// },
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
tc.setup()
result := p.handleMuteAdd(nil, tc.username, userInfo)
tc.assertions(t, result)
})
}
}
func TestHandleUnmute(t *testing.T) {
mockKvStore, mockAPI, _, _, _ := GetTestSetup(t)
p := getPluginTest(mockAPI, mockKvStore)
userInfo, err := GetMockGHUserInfo(p)
assert.NoError(t, err)
tests := []struct {
name string
username string
setup func()
expectedResult string
}{
{
name: "Error retrieving muted usernames",
setup: func() {
mockAPI.On("LogError", "error occurred getting muted users.", "UserID", userInfo.UserID, "Error", mock.Anything)
mockKvStore.EXPECT().Get("mockUserID-muted-users", gomock.Any()).Return(errors.New("error retrieving muted users")).Times(1)
},
expectedResult: "An error occurred getting muted users. Please try again later",
},
{
name: "Error occurred while unmuting the user",
username: "user1",
setup: func() {
mutedUsernames := []byte("user1,user2,user3")
mockKvStore.EXPECT().Get("mockUserID-muted-users", gomock.Any()).DoAndReturn(func(key string, value *[]byte) error {
*value = mutedUsernames
return nil
}).Times(1)
mockKvStore.EXPECT().Set(userInfo.UserID+"-muted-users", gomock.Any()).Return(false, errors.New("error saving muted users")).Times(1)
},
expectedResult: "Error occurred unmuting users",
},
{
name: "Successfully unmute a user",
username: "user1",
setup: func() {
mutedUsernames := []byte("user1,user2,user3")
mockKvStore.EXPECT().Get("mockUserID-muted-users", gomock.Any()).DoAndReturn(func(key string, value *[]byte) error {
*value = mutedUsernames
return nil
}).Times(1)
mockKvStore.EXPECT().Set(userInfo.UserID+"-muted-users", gomock.Any()).Return(true, nil).Times(1)
},
expectedResult: "`user1` is no longer muted",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
tc.setup()
result := p.handleUnmute(nil, tc.username, userInfo)
assert.Equal(t, tc.expectedResult, result)
})
}
}
func TestHandleUnmuteAll(t *testing.T) {
mockKvStore, mockAPI, _, _, _ := GetTestSetup(t)
p := getPluginTest(mockAPI, mockKvStore)
userInfo, err := GetMockGHUserInfo(p)
assert.NoError(t, err)
tests := []struct {
name string
setup func()
assertions func(string)
expectedResult string
}{
{
name: "No muted users",
setup: func() {
mockKvStore.EXPECT().Get(userInfo.UserID+"-muted-users", gomock.Any()).Return(nil).Times(1)
},
assertions: func(expectedResult string) {
assert.Equal(t, "You have no muted users", expectedResult)
},
},
{
name: "Error occurred while unmuting all users",
setup: func() {
mockKvStore.EXPECT().
Get(userInfo.UserID+"-muted-users", gomock.Any()).
DoAndReturn(func(key string, value *[]byte) error {
*value = []byte("user1,user2,user3")
return nil
}).Times(1)
mockKvStore.EXPECT().Set(userInfo.UserID+"-muted-users", []byte("")).Return(false, errors.New("error saving muted users")).Times(1)
},
assertions: func(expectedResult string) {
assert.Equal(t, "Error occurred unmuting users", expectedResult)
},
},
{
name: "Successfully unmute all users",
setup: func() {
mockKvStore.EXPECT().
Get(userInfo.UserID+"-muted-users", gomock.Any()).
DoAndReturn(func(key string, value *[]byte) error {
*value = []byte("user1,user2,user3")
return nil
}).Times(1)
mockKvStore.EXPECT().Set(userInfo.UserID+"-muted-users", []byte("")).Return(true, nil).Times(1)
},
assertions: func(expectedResult string) {
assert.Equal(t, expectedResult, "Unmuted all users")
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
tc.setup()
result := p.handleUnmuteAll(nil, userInfo)
tc.assertions(result)
})
}
}
func TestHandleMuteCommand(t *testing.T) {
mockKvStore, mockAPI, _, _, _ := GetTestSetup(t)
p := getPluginTest(mockAPI, mockKvStore)
userInfo, err := GetMockGHUserInfo(p)
assert.NoError(t, err)
tests := []struct {
name string
parameters []string
setup func()
assertions func(*testing.T, string)
}{
{
name: "Success - list muted users",
parameters: []string{"list"},
setup: func() {
mutedUsernames := []byte("user1,user2,user3")
mockKvStore.EXPECT().Get("mockUserID-muted-users", gomock.Any()).DoAndReturn(func(key string, value *[]byte) error {
*value = mutedUsernames
return nil
}).Times(1)
},
assertions: func(t *testing.T, response string) {
assert.Equal(t, "Your muted users:\n- user1\n- user2\n- user3\n", response)
},
},
// Can not mock API call using github client
// {
// name: "Success - add new muted user",
// parameters: []string{"add", "newUser"},
// setup: func() {
// mockKvStore.EXPECT().Get(userInfo.UserID+"-muted-users", gomock.Any()).DoAndReturn(func(key string, value *[]byte) error {
// *value = []byte("existingUser")
// return nil
// }).Times(1)
// mockKvStore.EXPECT().Set(userInfo.UserID+"-muted-users", []byte("existingUser,newUser")).Return(true, nil).Times(1)
// },
// assertions: func(t *testing.T, response string) {
// assert.Equal(t, "`newUser` is now muted. You'll no longer receive notifications for comments in your PRs and issues.", response)
// },
// },
{
name: "Error - invalid number of parameters for add",
parameters: []string{"add"},
setup: func() {},
assertions: func(t *testing.T, response string) {
assert.Equal(t, "Invalid number of parameters supplied to add", response)
},
},
{
name: "Success - delete muted user",
parameters: []string{"delete", "user1"},
setup: func() {
mutedUsernames := []byte("user1,user2,user3")
mockKvStore.EXPECT().Get("mockUserID-muted-users", gomock.Any()).DoAndReturn(func(key string, value *[]byte) error {
*value = mutedUsernames
return nil
}).Times(1)
mockKvStore.EXPECT().Set(userInfo.UserID+"-muted-users", gomock.Any()).Return(true, nil).Times(1)
},
assertions: func(t *testing.T, response string) {
assert.Equal(t, "`user1` is no longer muted", response)
},
},
{
name: "Error - invalid number of parameters for delete",
parameters: []string{"delete"},
setup: func() {},
assertions: func(t *testing.T, response string) {
assert.Equal(t, "Invalid number of parameters supplied to delete", response)
},
},
{
name: "Success - delete all muted users",
parameters: []string{"delete-all"},
setup: func() {
mockKvStore.EXPECT().
Get(userInfo.UserID+"-muted-users", gomock.Any()).
DoAndReturn(func(key string, value *[]byte) error {
*value = []byte("user1,user2,user3")
return nil
}).Times(1)
mockKvStore.EXPECT().Set(userInfo.UserID+"-muted-users", []byte("")).Return(true, nil).Times(1)
},
assertions: func(t *testing.T, response string) {
assert.Equal(t, "Unmuted all users", response)
},
},
{
name: "Error - unknown subcommand",
parameters: []string{"unknown"},
setup: func() {},
assertions: func(t *testing.T, response string) {
assert.Equal(t, "Unknown subcommand unknown", response)
},
},
{
name: "Error - no parameters provided",
parameters: []string{},
setup: func() {},
assertions: func(t *testing.T, response string) {
assert.Equal(t, "Invalid mute command. Available commands are 'list', 'add' and 'delete'.", response)
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
tc.setup()
result := p.handleMuteCommand(nil, nil, tc.parameters, userInfo)
tc.assertions(t, result)
})
}
}
func TestArrayDifference(t *testing.T) {
tests := []struct {
name string
arr1 []string
arr2 []string
expected []string
}{
{
name: "No difference - all elements in a are in b",
arr1: []string{"apple", "banana", "cherry"},
arr2: []string{"apple", "banana", "cherry"},
expected: []string{},
},
{
name: "Difference - some elements in a are not in b",
arr1: []string{"apple", "banana", "cherry", "date"},
arr2: []string{"apple", "banana"},
expected: []string{"cherry", "date"},
},
{
name: "All elements different - no elements in a are in b",
arr1: []string{"apple", "banana"},
arr2: []string{"cherry", "date"},
expected: []string{"apple", "banana"},
},
{
name: "Empty a - no elements to compare",
arr1: []string{},
arr2: []string{"apple", "banana"},
expected: []string{},
},
{
name: "Empty b - all elements in a should be returned",
arr1: []string{"apple", "banana"},
arr2: []string{},
expected: []string{"apple", "banana"},
},
{
name: "Both a and b empty - no elements to compare",
arr1: []string{},
arr2: []string{},
expected: []string{},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
result, _ := arrayDifference(tc.arr1, tc.arr2)
assert.ElementsMatch(t, tc.expected, result)
})
}
}
func TestHandleSubscriptionsList(t *testing.T) {
mockKvStore, mockAPI, _, _, _ := GetTestSetup(t)
p := getPluginTest(mockAPI, mockKvStore)
tests := []struct {
name string
channelID string
setup func()
assertions func(t *testing.T, result string)
}{
{
name: "Error retrieving subscriptions",
channelID: "channel1",
setup: func() {
mockKvStore.EXPECT().Get(SubscriptionsKey, gomock.Any()).Return(errors.New("store error")).Times(1)
},
assertions: func(t *testing.T, result string) {
assert.Contains(t, result, "could not get subscriptions from KVStore: store error")
},
},
{
name: "No subscriptions in the channel",
channelID: "channel2",
setup: func() {
mockKvStore.EXPECT().Get(SubscriptionsKey, gomock.Any()).DoAndReturn(func(key string, value **Subscriptions) error {
*value = &Subscriptions{Repositories: map[string][]*Subscription{}}
return nil
}).Times(1)
},
assertions: func(t *testing.T, result string) {
assert.Equal(t, "Currently there are no subscriptions in this channel", result)
},
},
{
name: "Multiple subscriptions in the channel",
channelID: "channel3",
setup: func() {
mockKvStore.EXPECT().Get(SubscriptionsKey, gomock.Any()).DoAndReturn(func(key string, value **Subscriptions) error {
*value = &Subscriptions{
Repositories: map[string][]*Subscription{
"repo1": {
{
ChannelID: "channel3",
Repository: "repo1",
},
{
ChannelID: "channel4",
Repository: "repo1",
},
},
"repo2": {
{
ChannelID: "channel3",
Repository: "repo2",
},
},
},
}
return nil
}).Times(1)
},
assertions: func(t *testing.T, result string) {
expected := "### Subscriptions in this channel\n" +
"* `repo1` - \n" +
"* `repo2` - \n"
assert.Equal(t, expected, result)
},
},
{
name: "Subscriptions with flags",
channelID: "channel4",
setup: func() {
mockKvStore.EXPECT().Get(SubscriptionsKey, gomock.Any()).DoAndReturn(func(key string, value **Subscriptions) error {
*value = &Subscriptions{
Repositories: map[string][]*Subscription{
"repo3": {
{
ChannelID: "channel4",
Repository: "repo3",
Flags: SubscriptionFlags{
ExcludeOrgMembers: true,
RenderStyle: "compact",
ExcludeRepository: []string{"repoA", "repoB"},
},
},
},
},
}
return nil
}).Times(1)
},
assertions: func(t *testing.T, result string) {
expected := "### Subscriptions in this channel\n* `repo3` - --exclude-org-member true,--render-style compact,--exclude repoA,repoB\n"
assert.Equal(t, expected, result)
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
tc.setup()
result := p.handleSubscriptionsList(nil, &model.CommandArgs{ChannelId: tc.channelID}, nil, nil)
tc.assertions(t, result)
})
}
}
func TestGetSubscribedFeatures(t *testing.T) {
mockKvStore, mockAPI, _, _, _ := GetTestSetup(t)
p := getPluginTest(mockAPI, mockKvStore)
tests := []struct {
name string
channelID string
owner string
repo string
setup func()
assertions func(t *testing.T, features Features, err error)
}{
{
name: "Error retrieving subscriptions",
channelID: "channel1",
owner: "owner1",
repo: "repo1",
setup: func() {
mockKvStore.EXPECT().Get(SubscriptionsKey, gomock.Any()).Return(errors.New("store error")).Times(1)
},
assertions: func(t *testing.T, features Features, err error) {
assert.Error(t, err)
assert.ErrorContains(t, err, "store error")
assert.Empty(t, features)
},