generated from mattermost/mattermost-plugin-starter-template
-
Notifications
You must be signed in to change notification settings - Fork 25
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
[MM-21688] Use dedicated bot account in Azure #32
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package command | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/golang/mock/gomock" | ||
"github.com/mattermost/mattermost-plugin-mscalendar/server/config" | ||
"github.com/mattermost/mattermost-plugin-mscalendar/server/mscalendar" | ||
"github.com/mattermost/mattermost-plugin-mscalendar/server/mscalendar/mock_mscalendar" | ||
"github.com/mattermost/mattermost-plugin-mscalendar/server/remote" | ||
"github.com/mattermost/mattermost-server/v5/model" | ||
"github.com/mattermost/mattermost-server/v5/plugin" | ||
"github.com/pkg/errors" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestConnect(t *testing.T) { | ||
tcs := []struct { | ||
name string | ||
command string | ||
setup func(m mscalendar.MSCalendar) | ||
expectedOutput string | ||
expectedError string | ||
}{ | ||
{ | ||
name: "user already connected", | ||
command: "connect", | ||
setup: func(m mscalendar.MSCalendar) { | ||
mscal := m.(*mock_mscalendar.MockMSCalendar) | ||
mscal.EXPECT().GetRemoteUser("user_id").Return(&remote.User{Mail: "user@email.com"}, nil).Times(1) | ||
}, | ||
expectedOutput: "Your Mattermost account is already connected to Microsoft Calendar account `user@email.com`. To connect to a different account, first run `/mscalendar disconnect`.", | ||
expectedError: "", | ||
}, | ||
{ | ||
name: "user not connected", | ||
command: "connect", | ||
setup: func(m mscalendar.MSCalendar) { | ||
mscal := m.(*mock_mscalendar.MockMSCalendar) | ||
mscal.EXPECT().GetRemoteUser("user_id").Return(nil, errors.New("remote user not found")).Times(1) | ||
}, | ||
expectedOutput: "[Click here to link your Microsoft Calendar account.](http://localhost/oauth2/connect)", | ||
expectedError: "", | ||
}, | ||
{ | ||
name: "non-admin connecting bot account", | ||
command: "connect_bot", | ||
setup: func(m mscalendar.MSCalendar) { | ||
mscal := m.(*mock_mscalendar.MockMSCalendar) | ||
mscal.EXPECT().IsAuthorizedAdmin("user_id").Return(false, nil).Times(1) | ||
}, | ||
expectedOutput: "", | ||
expectedError: "Command /mscalendar connect_bot failed: non-admin user attempting to connect bot account", | ||
}, | ||
{ | ||
name: "bot user already connected", | ||
command: "connect_bot", | ||
setup: func(m mscalendar.MSCalendar) { | ||
mscal := m.(*mock_mscalendar.MockMSCalendar) | ||
mscal.EXPECT().IsAuthorizedAdmin("user_id").Return(true, nil).Times(1) | ||
mscal.EXPECT().GetRemoteUser("bot_user_id").Return(&remote.User{Mail: "bot@email.com"}, nil).Times(1) | ||
}, | ||
//The bot account is already connected to %s account `%s`. To connect to a different account, first run `/%s disconnect_bot | ||
expectedOutput: "The bot account is already connected to Microsoft Calendar account `bot@email.com`. To connect to a different account, first run `/mscalendar disconnect_bot`.", | ||
expectedError: "", | ||
}, | ||
{ | ||
name: "bot user not connected", | ||
command: "connect_bot", | ||
setup: func(m mscalendar.MSCalendar) { | ||
mscal := m.(*mock_mscalendar.MockMSCalendar) | ||
mscal.EXPECT().IsAuthorizedAdmin("user_id").Return(true, nil).Times(1) | ||
mscal.EXPECT().GetRemoteUser("bot_user_id").Return(nil, errors.New("remote user not found")).Times(1) | ||
}, | ||
expectedOutput: "[Click here to link the bot's Microsoft Calendar account.](http://localhost/oauth2/connect_bot)", | ||
expectedError: "", | ||
}, | ||
} | ||
|
||
for _, tc := range tcs { | ||
t.Run(tc.name, func(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
conf := &config.Config{ | ||
PluginURL: "http://localhost", | ||
BotUserID: "bot_user_id", | ||
} | ||
|
||
mscal := mock_mscalendar.NewMockMSCalendar(ctrl) | ||
command := Command{ | ||
Context: &plugin.Context{}, | ||
Args: &model.CommandArgs{ | ||
Command: "/mscalendar " + tc.command, | ||
UserId: "user_id", | ||
}, | ||
ChannelID: "channel_id", | ||
Config: conf, | ||
MSCalendar: mscal, | ||
} | ||
|
||
if tc.setup != nil { | ||
tc.setup(mscal) | ||
} | ||
|
||
out, err := command.Handle() | ||
if tc.expectedOutput != "" { | ||
require.Equal(t, tc.expectedOutput, out) | ||
} | ||
|
||
if tc.expectedError != "" { | ||
require.Equal(t, tc.expectedError, err.Error()) | ||
} else { | ||
require.Nil(t, err) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright (c) 2019-present Mattermost, Inc. All Rights Reserved. | ||
// See License for license information. | ||
|
||
package command | ||
|
||
import "github.com/pkg/errors" | ||
|
||
func (c *Command) disconnect(parameters ...string) (string, error) { | ||
err := c.MSCalendar.DisconnectUser(c.Args.UserId) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return "Successfully disconnected your account", nil | ||
} | ||
|
||
func (c *Command) disconnectBot(parameters ...string) (string, error) { | ||
isAdmin, err := c.MSCalendar.IsAuthorizedAdmin(c.Args.UserId) | ||
if err != nil || !isAdmin { | ||
return "", errors.New("non-admin user attempting to disconnect bot account") | ||
} | ||
|
||
err = c.MSCalendar.DisconnectUser(c.Config.BotUserID) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return "Successfully disconnected bot user", nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package command | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/golang/mock/gomock" | ||
"github.com/mattermost/mattermost-plugin-mscalendar/server/config" | ||
"github.com/mattermost/mattermost-plugin-mscalendar/server/mscalendar" | ||
"github.com/mattermost/mattermost-plugin-mscalendar/server/mscalendar/mock_mscalendar" | ||
"github.com/mattermost/mattermost-server/v5/model" | ||
"github.com/mattermost/mattermost-server/v5/plugin" | ||
"github.com/pkg/errors" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestDisconnect(t *testing.T) { | ||
tcs := []struct { | ||
name string | ||
command string | ||
setup func(mscalendar.MSCalendar) | ||
expectedOutput string | ||
expectedError string | ||
}{ | ||
{ | ||
name: "disconnect failed", | ||
command: "disconnect", | ||
setup: func(m mscalendar.MSCalendar) { | ||
mscal := m.(*mock_mscalendar.MockMSCalendar) | ||
mscal.EXPECT().DisconnectUser("user_id").Return(errors.New("Some error")).Times(1) | ||
}, | ||
expectedOutput: "", | ||
expectedError: "Command /mscalendar disconnect failed: Some error", | ||
}, | ||
{ | ||
name: "disconnect successful", | ||
command: "disconnect", | ||
setup: func(m mscalendar.MSCalendar) { | ||
mscal := m.(*mock_mscalendar.MockMSCalendar) | ||
mscal.EXPECT().DisconnectUser("user_id").Return(nil).Times(1) | ||
}, | ||
expectedOutput: "Successfully disconnected your account", | ||
expectedError: "", | ||
}, | ||
{ | ||
name: "non-admin disconnecting bot account", | ||
command: "disconnect_bot", | ||
setup: func(m mscalendar.MSCalendar) { | ||
mscal := m.(*mock_mscalendar.MockMSCalendar) | ||
mscal.EXPECT().IsAuthorizedAdmin("user_id").Return(false, nil).Times(1) | ||
}, | ||
expectedOutput: "", | ||
expectedError: "Command /mscalendar disconnect_bot failed: non-admin user attempting to disconnect bot account", | ||
}, | ||
{ | ||
name: "bot disconnect failed", | ||
command: "disconnect_bot", | ||
setup: func(m mscalendar.MSCalendar) { | ||
mscal := m.(*mock_mscalendar.MockMSCalendar) | ||
mscal.EXPECT().IsAuthorizedAdmin("user_id").Return(true, nil).Times(1) | ||
mscal.EXPECT().DisconnectUser("bot_user_id").Return(errors.New("Some error")).Times(1) | ||
}, | ||
expectedOutput: "", | ||
expectedError: "Command /mscalendar disconnect_bot failed: Some error", | ||
}, | ||
{ | ||
name: "bot disconnect successful", | ||
command: "disconnect_bot", | ||
setup: func(m mscalendar.MSCalendar) { | ||
mscal := m.(*mock_mscalendar.MockMSCalendar) | ||
mscal.EXPECT().IsAuthorizedAdmin("user_id").Return(true, nil).Times(1) | ||
mscal.EXPECT().DisconnectUser("bot_user_id").Return(nil).Times(1) | ||
}, | ||
expectedOutput: "Successfully disconnected bot user", | ||
expectedError: "", | ||
}, | ||
} | ||
|
||
for _, tc := range tcs { | ||
t.Run(tc.name, func(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
conf := &config.Config{ | ||
PluginURL: "http://localhost", | ||
BotUserID: "bot_user_id", | ||
} | ||
|
||
mscal := mock_mscalendar.NewMockMSCalendar(ctrl) | ||
command := Command{ | ||
Context: &plugin.Context{}, | ||
Args: &model.CommandArgs{ | ||
Command: "/mscalendar " + tc.command, | ||
UserId: "user_id", | ||
}, | ||
ChannelID: "channel_id", | ||
Config: conf, | ||
MSCalendar: mscal, | ||
} | ||
|
||
if tc.setup != nil { | ||
tc.setup(mscal) | ||
} | ||
|
||
out, err := command.Handle() | ||
if tc.expectedOutput != "" { | ||
require.Equal(t, tc.expectedOutput, out) | ||
} | ||
|
||
if tc.expectedError != "" { | ||
require.Equal(t, tc.expectedError, err.Error()) | ||
} else { | ||
require.Nil(t, err) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
only if you do another push anyway... a nit of a nit, but maybe inline
userIndex.GetMattermostUserIDs()
and just useids
for the return? :)