generated from mattermost/mattermost-plugin-starter-template
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MM-21688] Use dedicated bot account in Azure (#32)
* most things updated for bot account. need to resolve some dependencies like IsAuthorizedAdmin * add connect and disconnect command tests * finish PR * implement IsAuthorizedAdmin, and expose through MSCalendar interface * ensure non-admins cannot connect the bot account * lint * PR feedback: * reword user error strings * rename vars
- Loading branch information
1 parent
c96158e
commit 864a161
Showing
25 changed files
with
731 additions
and
79 deletions.
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: "[email protected]"}, nil).Times(1) | ||
}, | ||
expectedOutput: "Your Mattermost account is already connected to Microsoft Calendar account `[email protected]`. 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: "[email protected]"}, 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 `[email protected]`. 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.