Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added testcase for more functions in server/command.go #846

Open
wants to merge 1 commit into
base: add_server/command.go_testcases
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions server/plugin/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ func (p *Plugin) handleSubscriptions(c *plugin.Context, args *model.CommandArgs,
}
}

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

return previousFeatures, nil
}

func (p *Plugin) handleUnsubscribe(_ *plugin.Context, args *model.CommandArgs, parameters []string, _ *GitHubUserInfo) string {
if len(parameters) == 0 {
return "Please specify a repository."
Expand Down Expand Up @@ -706,7 +707,7 @@ func (p *Plugin) handleIssue(_ *plugin.Context, args *model.CommandArgs, paramet
}
}

func (p *Plugin) handleSetup(c *plugin.Context, args *model.CommandArgs, parameters []string) string {
func (p *Plugin) handleSetup(_ *plugin.Context, args *model.CommandArgs, parameters []string) string {
userID := args.UserId
isSysAdmin, err := p.isAuthorizedSysAdmin(userID)
if err != nil {
Expand Down
350 changes: 350 additions & 0 deletions server/plugin/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -655,3 +655,353 @@ func TestHandleUnmuteAll(t *testing.T) {
})
}
}

func TestHandleMuteCommand(t *testing.T) {
mockKvStore, mockAPI, _, _, _ := GetTestSetup(t)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mockAPI is never used

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)
},
},
{
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().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
a []string
b []string
Comment on lines +769 to +770
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
a []string
b []string
arr1 []string
arr2 []string

expected []string
}{
{
name: "No difference - all elements in a are in b",
a: []string{"apple", "banana", "cherry"},
b: []string{"apple", "banana", "cherry"},
expected: []string{},
},
{
name: "Difference - some elements in a are not in b",
a: []string{"apple", "banana", "cherry", "date"},
b: []string{"apple", "banana"},
expected: []string{"cherry", "date"},
},
{
name: "All elements different - no elements in a are in b",
a: []string{"apple", "banana"},
b: []string{"cherry", "date"},
expected: []string{"apple", "banana"},
},
{
name: "Empty a - no elements to compare",
a: []string{},
b: []string{"apple", "banana"},
expected: []string{},
},
{
name: "Empty b - all elements in a should be returned",
a: []string{"apple", "banana"},
b: []string{},
expected: []string{"apple", "banana"},
},
{
name: "Both a and b empty - no elements to compare",
a: []string{},
b: []string{},
expected: []string{},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
result := arrayDifference(tc.a, tc.b)
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{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

create a util for this, a similar thing is used below as well

"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)
},
},
}

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)
},
},
{
name: "No subscriptions in the channel",
channelID: "channel2",
owner: "owner2",
repo: "repo2",
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, features Features, err error) {
assert.NoError(t, err)
assert.Empty(t, features)
},
},
{
name: "Subscribed features found for repo",
channelID: "channel3",
owner: "owner3",
repo: "repo3",
setup: func() {
mockKvStore.EXPECT().Get(SubscriptionsKey, gomock.Any()).DoAndReturn(func(key string, value **Subscriptions) error {
*value = &Subscriptions{
Repositories: map[string][]*Subscription{
"owner3/repo3": {
{
ChannelID: "channel3",
Repository: "owner3/repo3",
Features: Features("FeatureA"),
},
},
"owner4/repo4": {
{
ChannelID: "channel4",
Repository: "owner4/repo4",
Features: Features("FeatureB"),
},
},
},
}
return nil
}).Times(1)
},
assertions: func(t *testing.T, features Features, err error) {
assert.NoError(t, err)
expectedFeatures := Features("FeatureA")
assert.Equal(t, expectedFeatures, features)
},
},
{
name: "Subscribed features not found for repo",
channelID: "channel4",
owner: "owner5",
repo: "repo5",
setup: func() {
mockKvStore.EXPECT().Get(SubscriptionsKey, gomock.Any()).DoAndReturn(func(key string, value **Subscriptions) error {
*value = &Subscriptions{
Repositories: map[string][]*Subscription{
"owner6/repo6": {
{
ChannelID: "channel4",
Repository: "owner6/repo6",
Features: Features("FeatureC"),
},
},
},
}
return nil
}).Times(1)
},
assertions: func(t *testing.T, features Features, err error) {
assert.NoError(t, err)
assert.Empty(t, features)
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
tc.setup()
features, err := p.getSubscribedFeatures(tc.channelID, tc.owner, tc.repo)
tc.assertions(t, features, err)
})
}
}
2 changes: 1 addition & 1 deletion server/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func (p *Plugin) GetGitHubClient(ctx context.Context, userID string) (*github.Cl
return p.githubConnectUser(ctx, userInfo), nil
}

func (p *Plugin) githubConnectUser(ctx context.Context, info *GitHubUserInfo) *github.Client {
func (p *Plugin) githubConnectUser(_ context.Context, info *GitHubUserInfo) *github.Client {
tok := *info.Token
return p.githubConnectToken(tok)
}
Expand Down