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

Review fixes of #872 #7

Merged
merged 19 commits into from
Oct 13, 2022
Merged
Show file tree
Hide file tree
Changes from 14 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
10 changes: 5 additions & 5 deletions server/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ func getMockUserStoreKV() mockUserStoreKV {
withNotifications := connection // copy
withNotifications.Settings = &ConnectionSettings{
Notifications: trueValue,
RolesForDMNotification: map[string]*bool{
subCommandAssignee: &trueValue,
subCommandMention: &trueValue,
subCommandReporter: &trueValue,
subCommandWatching: &trueValue,
RolesForDMNotification: map[string]bool{
subCommandAssignee: trueValue,
Nityanand13 marked this conversation as resolved.
Show resolved Hide resolved
subCommandMention: trueValue,
subCommandReporter: trueValue,
subCommandWatching: trueValue,
},
}

Expand Down
43 changes: 26 additions & 17 deletions server/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -1133,13 +1133,37 @@ func (p *Plugin) GetUserSetting(wh *webhook, instanceID types.ID, jiraAccountID,

func (s *ConnectionSettings) ShouldReceiveNotification(role string) bool {
if val, ok := s.RolesForDMNotification[role]; ok {
return *val
return val
}

// Check old setting for backwards compatibility
return s.Notifications
}

func (p *Plugin) fetchConnectedUserFromAccount(account map[string]string, instance Instance) (Client, *Connection, error) {

Choose a reason for hiding this comment

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

Why is this needed?

Copy link
Author

Choose a reason for hiding this comment

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

@javaguirre because this was one of your review comments

var mattermostUserID types.ID
var err error
if account["AccountID"] != "" {
mattermostUserID, err = p.userStore.LoadMattermostUserID(instance.GetID(), account["AccountID"])
} else {
mattermostUserID, err = p.userStore.LoadMattermostUserID(instance.GetID(), account["Name"])
}
if err != nil {
return nil, nil, err
}

connection, err := p.userStore.LoadConnection(instance.GetID(), mattermostUserID)
if err != nil {
return nil, nil, err
}

client, err := instance.GetClient(connection)
if err != nil {
return nil, connection, err
}
Nityanand13 marked this conversation as resolved.
Show resolved Hide resolved
return client, connection, nil
}

func (wh *webhook) fetchConnectedUser(p *Plugin, instanceID types.ID) (Client, *Connection, error) {
var accountInformation []map[string]string

Expand Down Expand Up @@ -1169,22 +1193,7 @@ func (wh *webhook) fetchConnectedUser(p *Plugin, instanceID types.ID) (Client, *
return nil, nil, err
}
for _, account := range accountInformation {
var mattermostUserID types.ID
if account["AccountID"] != "" {
mattermostUserID, err = p.userStore.LoadMattermostUserID(instance.GetID(), account["AccountID"])
} else {
mattermostUserID, err = p.userStore.LoadMattermostUserID(instance.GetID(), account["Name"])
}
if err != nil {
continue
}

connection, err := p.userStore.LoadConnection(instance.GetID(), mattermostUserID)
if err != nil {
continue
}

client, err := instance.GetClient(connection)
client, connection, err := p.fetchConnectedUserFromAccount(account, instance)
if err != nil {
continue
}
Expand Down
250 changes: 250 additions & 0 deletions server/issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/stretchr/testify/assert"

"github.com/mattermost/mattermost-plugin-jira/server/utils/kvstore"
"github.com/mattermost/mattermost-plugin-jira/server/utils/types"
)

const (
Expand Down Expand Up @@ -279,6 +280,255 @@ func TestRouteShareIssuePublicly(t *testing.T) {
}
}

func TestShouldReceiveNotification(t *testing.T) {
cs := ConnectionSettings{}
cs.RolesForDMNotification = make(map[string]bool)
cs.RolesForDMNotification[subCommandAssignee] = true
cs.RolesForDMNotification[subCommandMention] = true
tests := map[string]struct {
Nityanand13 marked this conversation as resolved.
Show resolved Hide resolved
role string
notification bool
RolesForDMNotification map[string]bool
Nityanand13 marked this conversation as resolved.
Show resolved Hide resolved
}{
subCommandAssignee: {
role: subCommandAssignee,
notification: true,
},
subCommandMention: {
role: subCommandMention,
notification: true,
},
subCommandReporter: {
role: subCommandReporter,
notification: false,
},
subCommandWatching: {
role: subCommandWatching,
notification: false,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
val := cs.ShouldReceiveNotification(tt.role)
assert.Equal(t, tt.notification, val)
})
}

}

func TestFetchConnectedUser(t *testing.T) {
api := &plugintest.API{}

api.On("LogError", mockAnythingOfTypeBatch("string", 13)...).Return(nil)
Nityanand13 marked this conversation as resolved.
Show resolved Hide resolved

api.On("LogDebug", mockAnythingOfTypeBatch("string", 11)...).Return(nil)
p := &Plugin{}
p.SetAPI(api)
p.instanceStore = p.getMockInstanceStoreKV(1)
p.userStore = getMockUserStoreKV()
tests := map[string]struct {
instanceID types.ID
client Client
connection *Connection
wh webhook
expectedErr error
}{
"Issue Feild not found": {
Nityanand13 marked this conversation as resolved.
Show resolved Hide resolved
instanceID: testInstance1.InstanceID,
client: nil,
Kshitij-Katiyar marked this conversation as resolved.
Show resolved Hide resolved
connection: nil,
wh: webhook{
JiraWebhook: &JiraWebhook{
Issue: jira.Issue{},
},
},
expectedErr: nil,
Nityanand13 marked this conversation as resolved.
Show resolved Hide resolved
},
"Unable to load instance": {
instanceID: "instanceID",
Nityanand13 marked this conversation as resolved.
Show resolved Hide resolved
client: nil,
Kshitij-Katiyar marked this conversation as resolved.
Show resolved Hide resolved
connection: nil,
wh: webhook{
JiraWebhook: &JiraWebhook{
Issue: jira.Issue{
Fields: &jira.IssueFields{
Creator: &jira.User{},
},
},
},
},
expectedErr: errors.New("instance " + fmt.Sprintf("\"%s\"", "instanceID") + " not found"),
},
"Success": {
Nityanand13 marked this conversation as resolved.
Show resolved Hide resolved
instanceID: testInstance1.InstanceID,
client: testClient{},
connection: &Connection{
Settings: &ConnectionSettings{
Notifications: true,
RolesForDMNotification: map[string]bool{
subCommandAssignee: true,
subCommandMention: true,
subCommandReporter: true,
subCommandWatching: true,
},
},
User: jira.User{
AccountID: "test",
Nityanand13 marked this conversation as resolved.
Show resolved Hide resolved
},
},
wh: webhook{
JiraWebhook: &JiraWebhook{
Issue: jira.Issue{
Fields: &jira.IssueFields{
Creator: &jira.User{},
},
},
},
},
expectedErr: nil,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
client, connection, error := tt.wh.fetchConnectedUser(
Nityanand13 marked this conversation as resolved.
Show resolved Hide resolved
p,
tt.instanceID,
)
assert.Equal(t, tt.connection, connection)
assert.Equal(t, tt.client, client)
if tt.expectedErr != nil {
assert.Error(t, tt.expectedErr, error)
}
})
}
}

func TestApplyReporterNotification(t *testing.T) {
api := &plugintest.API{}

api.On("LogError", mockAnythingOfTypeBatch("string", 13)...).Return(nil)

api.On("LogDebug", mockAnythingOfTypeBatch("string", 11)...).Return(nil)
p := Plugin{}
Nityanand13 marked this conversation as resolved.
Show resolved Hide resolved
p.SetAPI(api)
p.instanceStore = p.getMockInstanceStoreKV(1)
p.userStore = getMockUserStoreKV()
wh := &webhook{
eventTypes: map[string]bool{createdCommentEvent: true},
JiraWebhook: &JiraWebhook{
Comment: jira.Comment{
UpdateAuthor: jira.User{},
},
Issue: jira.Issue{
Key: "TEST-10",
Nityanand13 marked this conversation as resolved.
Show resolved Hide resolved
Fields: &jira.IssueFields{
Type: jira.IssueType{
Name: "Story",
},
Summary: "",
},
Self: "Abc",
Nityanand13 marked this conversation as resolved.
Show resolved Hide resolved
},
},
notifications: []webhookUserNotification{},
}
tests := map[string]struct {
instanceID types.ID
reporter *jira.User
}{
"Unable to load instance": {
instanceID: "instanceID",
reporter: &jira.User{},
},
"No reporter": {
Nityanand13 marked this conversation as resolved.
Show resolved Hide resolved
instanceID: testInstance1.InstanceID,
reporter: nil,
},
"Success": {
instanceID: testInstance1.InstanceID,
reporter: &jira.User{},
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
totalNotifications := len(wh.notifications)
p.applyReporterNotification(
Nityanand13 marked this conversation as resolved.
Show resolved Hide resolved
wh,
tt.instanceID,
Nityanand13 marked this conversation as resolved.
Show resolved Hide resolved
tt.reporter,
)
if tt.reporter == nil || tt.instanceID == "instanceID" {
assert.Equal(t, len(wh.notifications)-totalNotifications, 0)
Nityanand13 marked this conversation as resolved.
Show resolved Hide resolved
} else {
assert.Equal(t, len(wh.notifications)-totalNotifications, 1)
Nityanand13 marked this conversation as resolved.
Show resolved Hide resolved
}
})
}
}

func TestGetUserSetting(t *testing.T) {
api := &plugintest.API{}

api.On("LogError", mockAnythingOfTypeBatch("string", 13)...).Return(nil)

api.On("LogDebug", mockAnythingOfTypeBatch("string", 11)...).Return(nil)
p := Plugin{}
p.SetAPI(api)
p.instanceStore = p.getMockInstanceStoreKV(1)
p.userStore = getMockUserStoreKV()

tests := map[string]struct {
wh *webhook
instanceID types.ID
jiraAccountID string
jiraUsername string
connection *Connection
expectedErr error
}{
"Unable to load instance": {
wh: &webhook{},
instanceID: "instanceID",
jiraAccountID: "",
Nityanand13 marked this conversation as resolved.
Show resolved Hide resolved
jiraUsername: "",
connection: nil,
expectedErr: errors.New("instance " + fmt.Sprintf("\"%s\"", "instanceID") + " not found"),
},
"Success": {
wh: &webhook{},
instanceID: testInstance1.InstanceID,
jiraAccountID: "",
jiraUsername: "",
Nityanand13 marked this conversation as resolved.
Show resolved Hide resolved
connection: &Connection{
User: jira.User{AccountID: "test"},
Settings: &ConnectionSettings{
Notifications: true,
RolesForDMNotification: (map[string]bool{
subCommandAssignee: true,
subCommandMention: true,
subCommandReporter: true,
subCommandWatching: true,
}),
},
},
expectedErr: nil,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
connection, error := p.GetUserSetting(
tt.wh,
tt.instanceID,
tt.jiraAccountID,
tt.jiraUsername,
)
assert.Equal(t, tt.connection, connection)
if tt.expectedErr != nil {
assert.Error(t, tt.expectedErr, error)
}
})
}
}

func TestRouteAttachCommentToIssue(t *testing.T) {
api := &plugintest.API{}

Expand Down
4 changes: 2 additions & 2 deletions server/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (connection *Connection) sendNotification(role string, hasNotification bool
if role != subCommandAssignee && role != subCommandMention && role != subCommandReporter && role != subCommandWatching {
return false
}
connection.Settings.RolesForDMNotification[role] = &hasNotification
connection.Settings.RolesForDMNotification[role] = hasNotification
return true
}
func (p *Plugin) settingsNotifications(header *model.CommandArgs, instanceID, mattermostUserID types.ID, connection *Connection, args []string) *model.CommandResponse {
Expand Down Expand Up @@ -62,7 +62,7 @@ func (p *Plugin) settingsNotifications(header *model.CommandArgs, instanceID, ma
return p.responsef(header, errConnectToJira, err)
}
notifications := settingOff
if *updatedConnection.Settings.RolesForDMNotification[args[1]] {
if updatedConnection.Settings.RolesForDMNotification[args[1]] {
notifications = settingOn
}

Expand Down
2 changes: 1 addition & 1 deletion server/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (c *Connection) JiraAccountID() types.ID {

type ConnectionSettings struct {
Notifications bool `json:"notifications"`
RolesForDMNotification map[string]*bool
RolesForDMNotification map[string]bool
}

func (s *ConnectionSettings) String() string {
Expand Down
10 changes: 5 additions & 5 deletions server/user_cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ func (p *Plugin) httpACUserInteractive(w http.ResponseWriter, r *http.Request, i
// Set default settings the first time a user connects
Settings: &ConnectionSettings{
Notifications: trueValue,
RolesForDMNotification: map[string]*bool{
subCommandMention: &trueValue,
subCommandAssignee: &trueValue,
subCommandReporter: &trueValue,
subCommandWatching: &trueValue,
RolesForDMNotification: map[string]bool{
subCommandMention: trueValue,
subCommandAssignee: trueValue,
subCommandReporter: trueValue,
subCommandWatching: trueValue,
},
},
}
Expand Down
Loading