Skip to content

Commit

Permalink
Add auto complete for slash command (#1072)
Browse files Browse the repository at this point in the history
* Remove /jira settings command

* Uncomment commented code

* Fix ci

* Add autocomplete for connect, disconnect and settings command

* [fix_issue_1037]: Added testcase for instance settigns

* Add new test case for instance settings and update other failing test cases

---------

Co-authored-by: kshitij katiyar <[email protected]>
  • Loading branch information
ayusht2810 and Kshitij-Katiyar authored Jul 2, 2024
1 parent 1b9490b commit 126ad10
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 27 deletions.
3 changes: 3 additions & 0 deletions server/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ func addSubCommands(jira *model.AutocompleteData, optInstance bool) {
jira.AddCommand(createTransitionCommand(optInstance))
jira.AddCommand(createAssignCommand(optInstance))
jira.AddCommand(createUnassignCommand(optInstance))
jira.AddCommand(createConnectCommand())
jira.AddCommand(createDisconnectCommand())
jira.AddCommand(createSettingsCommand(optInstance))

// Generic commands
jira.AddCommand(createIssueCommand(optInstance))
Expand Down
96 changes: 96 additions & 0 deletions server/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,102 @@ func TestPlugin_ExecuteCommand_Settings(t *testing.T) {
}
}

func TestPlugin_ExecuteCommand_Instance_Settings(t *testing.T) {
p := &Plugin{}
tc := TestConfiguration{}
p.updateConfig(func(conf *config) {
conf.Secret = tc.Secret
conf.mattermostSiteURL = mattermostSiteURL
})
api := &plugintest.API{}
api.On("LogError", mock.AnythingOfTypeArgument("string")).Return(nil)

tests := map[string]struct {
commandArgs *model.CommandArgs
numInstances int
expectedMsg string
}{
"no storage": {
commandArgs: &model.CommandArgs{Command: "/jira instance settings", UserId: mockUserIDUnknown},
numInstances: 2,
expectedMsg: "Failed to load your connection to Jira. Error: TESTING user \"3\" not found.",
},
"user not found": {
commandArgs: &model.CommandArgs{Command: "/jira instance settings", UserId: mockUserIDUnknown},
numInstances: 0,
expectedMsg: "Failed to load your connection to Jira. Error: TESTING user \"3\" not found.",
},
"no params, with notifications": {
commandArgs: &model.CommandArgs{Command: "/jira instance settings", UserId: mockUserIDWithNotifications},
numInstances: 1,
expectedMsg: "Current settings:\n\tNotifications: on",
},
"no params, without notifications": {
commandArgs: &model.CommandArgs{Command: "/jira instance settings", UserId: mockUserIDWithoutNotifications},
numInstances: 1,
expectedMsg: "Current settings:\n\tNotifications: off",
},
"unknown setting": {
commandArgs: &model.CommandArgs{Command: "/jira instance settings" + " test", UserId: mockUserIDWithoutNotifications},
numInstances: 1,
expectedMsg: "Unknown setting.",
},
"set notifications without value": {
commandArgs: &model.CommandArgs{Command: "/jira instance settings" + " notifications", UserId: mockUserIDWithoutNotifications},
numInstances: 1,
expectedMsg: "`/jira settings notifications [value]`\n* Invalid value. Accepted values are: `on` or `off`.",
},
"set notification with unknown value": {
commandArgs: &model.CommandArgs{Command: "/jira instance settings notifications test", UserId: mockUserIDWithoutNotifications},
numInstances: 1,
expectedMsg: "`/jira settings notifications [value]`\n* Invalid value. Accepted values are: `on` or `off`.",
},
"enable notifications": {
commandArgs: &model.CommandArgs{Command: "/jira instance settings notifications on", UserId: mockUserIDWithoutNotifications},
numInstances: 1,
expectedMsg: "Settings updated. Notifications on.",
},
"disable notifications": {
commandArgs: &model.CommandArgs{Command: "/jira instance settings notifications off", UserId: mockUserIDWithNotifications},
numInstances: 1,
expectedMsg: "Settings updated. Notifications off.",
},
"multiple instances are present: Notifications off": {
commandArgs: &model.CommandArgs{Command: "/jira instance settings notifications off --instance https://jiraurl1.com", UserId: mockUserIDWithNotifications},
numInstances: 2,
expectedMsg: "Settings updated. Notifications off.",
},
"multiple instances are present: Notifications on": {
commandArgs: &model.CommandArgs{Command: "/jira instance settings notifications on --instance https://jiraurl2.com", UserId: mockUserIDWithNotifications},
numInstances: 2,
expectedMsg: "Settings updated. Notifications on.",
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
isSendEphemeralPostCalled := false

currentTestAPI := api
currentTestAPI.On("SendEphemeralPost", mock.AnythingOfType("string"), mock.AnythingOfType("*model.Post")).Run(func(args mock.Arguments) {
isSendEphemeralPostCalled = true

post := args.Get(1).(*model.Post)
assert.Equal(t, tt.expectedMsg, post.Message)
}).Once().Return(&model.Post{})

p.SetAPI(currentTestAPI)
p.client = pluginapi.NewClient(p.API, p.Driver)
p.instanceStore = p.getMockInstanceStoreKV(tt.numInstances)
p.userStore = getMockUserStoreKV()

_, err := p.ExecuteCommand(&plugin.Context{}, tt.commandArgs)
require.Nil(t, err)

assert.Equal(t, true, isSendEphemeralPostCalled)
})
}
}

func TestPlugin_ExecuteCommand_Installation(t *testing.T) {
api := &plugintest.API{}
api.On("LogError", mock.AnythingOfTypeArgument("string")).Return(nil)
Expand Down
36 changes: 18 additions & 18 deletions server/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func TestSubscribe(t *testing.T) {
},
},
"Initial Subscription happy": {
subscription: `{"instance_id": "jiraurl1", "name": "some name", "channel_id": "aaaaaaaaaaaaaaaaaaaaaaaaab", "filters": {"events": ["jira:issue_created"], "projects": ["myproject"], "issue_types": ["10001"]}}`,
subscription: `{"instance_id": "https://jiraurl1.com", "name": "some name", "channel_id": "aaaaaaaaaaaaaaaaaaaaaaaaab", "filters": {"events": ["jira:issue_created"], "projects": ["myproject"], "issue_types": ["10001"]}}`,
expectedStatusCode: http.StatusOK,
apiCalls: checkHasSubscriptions([]ChannelSubscription{
{
Expand All @@ -188,37 +188,37 @@ func TestSubscribe(t *testing.T) {
}, nil, t),
},
"Initial Subscription, GetProject mocked error": {
subscription: fmt.Sprintf(`{"instance_id": "jiraurl1", "name": "some name", "channel_id": "aaaaaaaaaaaaaaaaaaaaaaaaab", "filters": {"events": ["jira:issue_created"], "projects": ["%s"], "issue_types": ["10001"]}}`, nonExistantProjectKey),
subscription: fmt.Sprintf(`{"instance_id": "https://jiraurl1.com", "name": "some name", "channel_id": "aaaaaaaaaaaaaaaaaaaaaaaaab", "filters": {"events": ["jira:issue_created"], "projects": ["%s"], "issue_types": ["10001"]}}`, nonExistantProjectKey),
expectedStatusCode: http.StatusInternalServerError,
apiCalls: hasSubscriptions([]ChannelSubscription{}, t),
},
"Initial Subscription, empty name provided": {
subscription: `{"instance_id": "jiraurl1", "name": "", "channel_id": "aaaaaaaaaaaaaaaaaaaaaaaaab", "filters": {"events": ["jira:issue_created"], "projects": ["myproject"], "issue_types": ["10001"]}}`,
subscription: `{"instance_id": "https://jiraurl1.com", "name": "", "channel_id": "aaaaaaaaaaaaaaaaaaaaaaaaab", "filters": {"events": ["jira:issue_created"], "projects": ["myproject"], "issue_types": ["10001"]}}`,
expectedStatusCode: http.StatusInternalServerError,
apiCalls: hasSubscriptions([]ChannelSubscription{}, t),
},
"Initial Subscription, long name provided": {
subscription: `{"instance_id": "jiraurl1", "name": "` + TestDataLongSubscriptionName + `", "channel_id": "aaaaaaaaaaaaaaaaaaaaaaaaab", "filters": {"events": ["jira:issue_created"], "projects": ["myproject"], "issue_types": ["10001"]}}`,
subscription: `{"instance_id": "https://jiraurl1.com", "name": "` + TestDataLongSubscriptionName + `", "channel_id": "aaaaaaaaaaaaaaaaaaaaaaaaab", "filters": {"events": ["jira:issue_created"], "projects": ["myproject"], "issue_types": ["10001"]}}`,
expectedStatusCode: http.StatusInternalServerError,
apiCalls: hasSubscriptions([]ChannelSubscription{}, t),
},
"Initial Subscription, no project provided": {
subscription: `{"instance_id": "jiraurl1", "name": "somename", "channel_id": "aaaaaaaaaaaaaaaaaaaaaaaaab", "filters": {"events": ["jira:issue_created"], "projects": [], "issue_types": ["10001"]}}`,
subscription: `{"instance_id": "https://jiraurl1.com", "name": "somename", "channel_id": "aaaaaaaaaaaaaaaaaaaaaaaaab", "filters": {"events": ["jira:issue_created"], "projects": [], "issue_types": ["10001"]}}`,
expectedStatusCode: http.StatusInternalServerError,
apiCalls: hasSubscriptions([]ChannelSubscription{}, t),
},
"Initial Subscription, no events provided": {
subscription: `{"instance_id": "jiraurl1", "name": "somename", "channel_id": "aaaaaaaaaaaaaaaaaaaaaaaaab", "filters": {"events": [], "projects": ["myproject"], "issue_types": ["10001"]}}`,
subscription: `{"instance_id": "https://jiraurl1.com", "name": "somename", "channel_id": "aaaaaaaaaaaaaaaaaaaaaaaaab", "filters": {"events": [], "projects": ["myproject"], "issue_types": ["10001"]}}`,
expectedStatusCode: http.StatusInternalServerError,
apiCalls: hasSubscriptions([]ChannelSubscription{}, t),
},
"Initial Subscription, no issue types provided": {
subscription: `{"instance_id": "jiraurl1", "name": "somename", "channel_id": "aaaaaaaaaaaaaaaaaaaaaaaaab", "filters": {"events": ["jira:issue_created"], "projects": ["myproject"], "issue_types": []}}`,
subscription: `{"instance_id": "https://jiraurl1.com", "name": "somename", "channel_id": "aaaaaaaaaaaaaaaaaaaaaaaaab", "filters": {"events": ["jira:issue_created"], "projects": ["myproject"], "issue_types": []}}`,
expectedStatusCode: http.StatusInternalServerError,
apiCalls: hasSubscriptions([]ChannelSubscription{}, t),
},
"Adding to existing with other channel": {
subscription: `{"instance_id": "jiraurl1", "name": "some name", "channel_id": "aaaaaaaaaaaaaaaaaaaaaaaaab", "filters": {"events": ["jira:issue_created"], "projects": ["myproject"], "issue_types": ["10001"]}}`,
subscription: `{"instance_id": "https://jiraurl1.com", "name": "some name", "channel_id": "aaaaaaaaaaaaaaaaaaaaaaaaab", "filters": {"events": ["jira:issue_created"], "projects": ["myproject"], "issue_types": ["10001"]}}`,
expectedStatusCode: http.StatusOK,
apiCalls: checkHasSubscriptions([]ChannelSubscription{
{
Expand Down Expand Up @@ -252,7 +252,7 @@ func TestSubscribe(t *testing.T) {
}), t),
},
"Adding to existing in same channel": {
subscription: `{"instance_id": "jiraurl1", "name": "subscription name", "channel_id": "aaaaaaaaaaaaaaaaaaaaaaaaab", "filters": {"events": ["jira:issue_created"], "projects": ["myproject"], "issue_types": ["10001"]}}`,
subscription: `{"instance_id": "https://jiraurl1.com", "name": "subscription name", "channel_id": "aaaaaaaaaaaaaaaaaaaaaaaaab", "filters": {"events": ["jira:issue_created"], "projects": ["myproject"], "issue_types": ["10001"]}}`,
expectedStatusCode: http.StatusOK,
apiCalls: checkHasSubscriptions([]ChannelSubscription{
{
Expand Down Expand Up @@ -286,7 +286,7 @@ func TestSubscribe(t *testing.T) {
}), t),
},
"Adding to existing with same name in same channel": {
subscription: `{"instance_id": "jiraurl1", "name": "SubscriptionName", "channel_id": "aaaaaaaaaaaaaaaaaaaaaaaaab", "filters": {"events": ["jira:issue_created"], "projects": ["myproject"], "issue_types": ["10001"]}}`,
subscription: `{"instance_id": "https://jiraurl1.com", "name": "SubscriptionName", "channel_id": "aaaaaaaaaaaaaaaaaaaaaaaaab", "filters": {"events": ["jira:issue_created"], "projects": ["myproject"], "issue_types": ["10001"]}}`,
expectedStatusCode: http.StatusInternalServerError,
apiCalls: checkHasSubscriptions([]ChannelSubscription{
{
Expand Down Expand Up @@ -503,7 +503,7 @@ func TestEditSubscription(t *testing.T) {
},
},
"Editing subscription": {
subscription: `{"instance_id": "jiraurl1", "name": "some name", "id": "aaaaaaaaaaaaaaaaaaaaaaaaab", "channel_id": "aaaaaaaaaaaaaaaaaaaaaaaaac", "filters": {"events": ["jira:issue_created"], "projects": ["otherproject"], "issue_types": ["10001"]}}`,
subscription: `{"instance_id": "https://jiraurl1.com", "name": "some name", "id": "aaaaaaaaaaaaaaaaaaaaaaaaab", "channel_id": "aaaaaaaaaaaaaaaaaaaaaaaaac", "filters": {"events": ["jira:issue_created"], "projects": ["otherproject"], "issue_types": ["10001"]}}`,
expectedStatusCode: http.StatusOK,
apiCalls: checkHasSubscriptions([]ChannelSubscription{
{
Expand All @@ -530,7 +530,7 @@ func TestEditSubscription(t *testing.T) {
}), t),
},
"Editing subscription, no name provided": {
subscription: `{"instance_id": "jiraurl1", "name": "", "id": "aaaaaaaaaaaaaaaaaaaaaaaaab", "channel_id": "aaaaaaaaaaaaaaaaaaaaaaaaac", "filters": {"events": ["jira:issue_created"], "projects": ["otherproject"], "issue_types": ["10001"]}}`,
subscription: `{"instance_id": "https://jiraurl1.com", "name": "", "id": "aaaaaaaaaaaaaaaaaaaaaaaaab", "channel_id": "aaaaaaaaaaaaaaaaaaaaaaaaac", "filters": {"events": ["jira:issue_created"], "projects": ["otherproject"], "issue_types": ["10001"]}}`,
expectedStatusCode: http.StatusInternalServerError,
apiCalls: checkHasSubscriptions([]ChannelSubscription{},
withExistingChannelSubscriptions(
Expand All @@ -547,7 +547,7 @@ func TestEditSubscription(t *testing.T) {
}), t),
},
"Editing subscription, name too long": {
subscription: `{"instance_id": "jiraurl1", "name": "` + TestDataLongSubscriptionName + `", "id": "aaaaaaaaaaaaaaaaaaaaaaaaab", "channel_id": "aaaaaaaaaaaaaaaaaaaaaaaaac", "filters": {"events": ["jira:issue_created"], "projects": ["otherproject"], "issue_types": ["10001"]}}`,
subscription: `{"instance_id": "https://jiraurl1.com", "name": "` + TestDataLongSubscriptionName + `", "id": "aaaaaaaaaaaaaaaaaaaaaaaaab", "channel_id": "aaaaaaaaaaaaaaaaaaaaaaaaac", "filters": {"events": ["jira:issue_created"], "projects": ["otherproject"], "issue_types": ["10001"]}}`,
expectedStatusCode: http.StatusInternalServerError,
apiCalls: checkHasSubscriptions([]ChannelSubscription{},
withExistingChannelSubscriptions(
Expand All @@ -564,7 +564,7 @@ func TestEditSubscription(t *testing.T) {
}), t),
},
"Editing subscription, no project provided": {
subscription: `{"instance_id": "jiraurl1", "name": "somename", "id": "aaaaaaaaaaaaaaaaaaaaaaaaab", "channel_id": "aaaaaaaaaaaaaaaaaaaaaaaaac", "filters": {"events": ["jira:issue_created"], "projects": [], "issue_types": ["10001"]}}`,
subscription: `{"instance_id": "https://jiraurl1.com", "name": "somename", "id": "aaaaaaaaaaaaaaaaaaaaaaaaab", "channel_id": "aaaaaaaaaaaaaaaaaaaaaaaaac", "filters": {"events": ["jira:issue_created"], "projects": [], "issue_types": ["10001"]}}`,
expectedStatusCode: http.StatusInternalServerError,
apiCalls: checkHasSubscriptions([]ChannelSubscription{},
withExistingChannelSubscriptions(
Expand All @@ -581,7 +581,7 @@ func TestEditSubscription(t *testing.T) {
}), t),
},
"Editing subscription, no events provided": {
subscription: `{"instance_id": "jiraurl1", "name": "somename", "id": "aaaaaaaaaaaaaaaaaaaaaaaaab", "channel_id": "aaaaaaaaaaaaaaaaaaaaaaaaac", "filters": {"events": [], "projects": ["otherproject"], "issue_types": ["10001"]}}`,
subscription: `{"instance_id": "https://jiraurl1.com", "name": "somename", "id": "aaaaaaaaaaaaaaaaaaaaaaaaab", "channel_id": "aaaaaaaaaaaaaaaaaaaaaaaaac", "filters": {"events": [], "projects": ["otherproject"], "issue_types": ["10001"]}}`,
expectedStatusCode: http.StatusInternalServerError,
apiCalls: checkHasSubscriptions([]ChannelSubscription{},
withExistingChannelSubscriptions(
Expand All @@ -598,7 +598,7 @@ func TestEditSubscription(t *testing.T) {
}), t),
},
"Editing subscription, no issue types provided": {
subscription: `{"instance_id": "jiraurl1", "name": "somename", "id": "aaaaaaaaaaaaaaaaaaaaaaaaab", "channel_id": "aaaaaaaaaaaaaaaaaaaaaaaaac", "filters": {"events": ["jira:issue_created"], "projects": ["otherproject"], "issue_types": []}}`,
subscription: `{"instance_id": "https://jiraurl1.com", "name": "somename", "id": "aaaaaaaaaaaaaaaaaaaaaaaaab", "channel_id": "aaaaaaaaaaaaaaaaaaaaaaaaac", "filters": {"events": ["jira:issue_created"], "projects": ["otherproject"], "issue_types": []}}`,
expectedStatusCode: http.StatusInternalServerError,
apiCalls: checkHasSubscriptions([]ChannelSubscription{},
withExistingChannelSubscriptions(
Expand All @@ -615,7 +615,7 @@ func TestEditSubscription(t *testing.T) {
}), t),
},
"Editing subscription, GetProject mocked error. Existing sub has nonexistent project.": {
subscription: fmt.Sprintf(`{"instance_id": "jiraurl1", "id": "subaaaaaaaaaabbbbbbbbbbccc", "name": "subscription name", "channel_id": "channelaaaaaaaaaabbbbbbbbb", "filters": {"events": ["jira:issue_created"], "projects": ["%s"], "issue_types": ["10001"]}}`, nonExistantProjectKey),
subscription: fmt.Sprintf(`{"instance_id": "https://jiraurl1.com", "id": "subaaaaaaaaaabbbbbbbbbbccc", "name": "subscription name", "channel_id": "channelaaaaaaaaaabbbbbbbbb", "filters": {"events": ["jira:issue_created"], "projects": ["%s"], "issue_types": ["10001"]}}`, nonExistantProjectKey),
expectedStatusCode: http.StatusInternalServerError,
apiCalls: checkHasSubscriptions([]ChannelSubscription{},
withExistingChannelSubscriptions(
Expand All @@ -632,7 +632,7 @@ func TestEditSubscription(t *testing.T) {
}), t),
},
"Editing subscription, GetProject mocked error. Existing sub has existing project.": {
subscription: fmt.Sprintf(`{"instance_id": "jiraurl1", "id": "subaaaaaaaaaabbbbbbbbbbccc", "name": "subscription name", "channel_id": "channelaaaaaaaaaabbbbbbbbb", "filters": {"events": ["jira:issue_created"], "projects": ["%s"], "issue_types": ["10001"]}}`, nonExistantProjectKey),
subscription: fmt.Sprintf(`{"instance_id": "https://jiraurl1.com", "id": "subaaaaaaaaaabbbbbbbbbbccc", "name": "subscription name", "channel_id": "channelaaaaaaaaaabbbbbbbbb", "filters": {"events": ["jira:issue_created"], "projects": ["%s"], "issue_types": ["10001"]}}`, nonExistantProjectKey),
expectedStatusCode: http.StatusInternalServerError,
apiCalls: checkHasSubscriptions([]ChannelSubscription{},
withExistingChannelSubscriptions(
Expand Down
8 changes: 4 additions & 4 deletions server/issue_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestAsSlackAttachment(t *testing.T) {
},
},
expectedAttachment: &model.SlackAttachment{
Text: "[MM-57208: (Open)](jiraurl2/browse/MM-57208)",
Text: "[MM-57208: (Open)](https://jiraurl2.com/browse/MM-57208)",
Color: "#95b7d0",
},
},
Expand All @@ -48,7 +48,7 @@ func TestAsSlackAttachment(t *testing.T) {
},
},
expectedAttachment: &model.SlackAttachment{
Text: "[MM-57208: A Summary (Open)](jiraurl2/browse/MM-57208)",
Text: "[MM-57208: A Summary (Open)](https://jiraurl2.com/browse/MM-57208)",
Color: "#95b7d0",
},
},
Expand Down Expand Up @@ -76,7 +76,7 @@ func TestAsSlackAttachment(t *testing.T) {
},
},
expectedAttachment: &model.SlackAttachment{
Text: "[MM-57208: A Summary (Open)](jiraurl2/browse/MM-57208)\n\nA Description\n",
Text: "[MM-57208: A Summary (Open)](https://jiraurl2.com/browse/MM-57208)\n\nA Description\n",
Color: "#95b7d0",
Fields: []*model.SlackAttachmentField{
{
Expand Down Expand Up @@ -110,7 +110,7 @@ func TestAsSlackAttachment(t *testing.T) {
},
showActions: true,
expectedAttachment: &model.SlackAttachment{
Text: "[MM-57208: (Open)](jiraurl2/browse/MM-57208)",
Text: "[MM-57208: (Open)](https://jiraurl2.com/browse/MM-57208)",
Color: "#95b7d0",
Actions: []*model.PostAction{
{
Expand Down
6 changes: 3 additions & 3 deletions server/kv_mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ type testInstance struct {
var _ Instance = (*testInstance)(nil)

const (
mockInstance1URL = "jiraurl1"
mockInstance2URL = "jiraurl2"
mockInstance3URL = "jiraurl3"
mockInstance1URL = "https://jiraurl1.com"
mockInstance2URL = "https://jiraurl2.com"
mockInstance3URL = "https://jiraurl3.com"
)

var testInstance1 = &testInstance{
Expand Down
Loading

0 comments on commit 126ad10

Please sign in to comment.