Skip to content

Commit

Permalink
[MM-820]: Added testcases for multiple server files
Browse files Browse the repository at this point in the history
  • Loading branch information
Kshitij-Katiyar committed Oct 8, 2024
1 parent f8242e9 commit e74683b
Show file tree
Hide file tree
Showing 4 changed files with 371 additions and 41 deletions.
104 changes: 64 additions & 40 deletions server/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ func TestHandleConnect(t *testing.T) {
mockSetup: func(_ *plugintest.API, _ []byte, _ *MockClient) {
},
expectedOutput: tooManyParametersText,
expectError: false,
},
{
name: "Error connecting user",
Expand All @@ -80,9 +79,8 @@ func TestHandleConnect(t *testing.T) {
api.On("GetConfig").Return(&model.Config{ServiceSettings: model.ServiceSettings{SiteURL: model.NewString("https://example.com")}})
mockClient.On("GetMe").Return(&msgraph.User{}, errors.New("error getting user details"))
},
expectedOutput: "",
expectError: true,
expectedError: "error getting user details",
expectError: true,
expectedError: "error getting user details",
},
{
name: "Successful connection",
Expand All @@ -93,23 +91,16 @@ func TestHandleConnect(t *testing.T) {
api.On("GetConfig").Return(&model.Config{ServiceSettings: model.ServiceSettings{SiteURL: model.NewString("https://example.com")}})
mockClient.On("GetMe").Return(&msgraph.User{}, nil)
},
expectedOutput: "",
expectError: false,
expectError: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
api := &plugintest.API{}
mockAPI := &plugintest.API{}
mockClient := &MockClient{}

p := &Plugin{
MattermostPlugin: plugin.MattermostPlugin{
API: api,
},
client: mockClient,
}

p := SetupMockPlugin(mockAPI, nil, mockClient)
p.setConfiguration(&configuration{
EncryptionKey: "demo_encrypt_key",
OAuth2ClientID: "demo_oauth2_client_id",
Expand All @@ -127,7 +118,7 @@ func TestHandleConnect(t *testing.T) {
encryptedUserInfo, err := userInfo.EncryptedJSON([]byte("demo_encrypt_key"))
require.NoError(t, err)

tt.mockSetup(api, encryptedUserInfo, mockClient)
tt.mockSetup(mockAPI, encryptedUserInfo, mockClient)

resp, err := p.handleConnect(tt.args, tt.commandArgs)
if tt.expectError {
Expand All @@ -137,7 +128,7 @@ func TestHandleConnect(t *testing.T) {
require.Contains(t, resp, tt.expectedOutput)
}

api.AssertExpectations(t)
mockAPI.AssertExpectations(t)
})
}
}
Expand Down Expand Up @@ -184,16 +175,10 @@ func TestHandleDisconnect(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
api := &plugintest.API{}
mockAPI := &plugintest.API{}
mockTracker := &MockTracker{}

p := &Plugin{
MattermostPlugin: plugin.MattermostPlugin{
API: api,
},
tracker: mockTracker,
}

p := SetupMockPlugin(mockAPI, mockTracker, nil)
p.setConfiguration(&configuration{
EncryptionKey: "demo_encrypt_key",
})
Expand All @@ -208,13 +193,13 @@ func TestHandleDisconnect(t *testing.T) {
encryptedUserInfo, err := userInfo.EncryptedJSON([]byte("demo_encrypt_key"))
require.NoError(t, err)

tt.mockSetup(api, encryptedUserInfo, mockTracker)
tt.mockSetup(mockAPI, encryptedUserInfo, mockTracker)

resp, err := p.handleDisconnect(tt.args, tt.commandArgs)
require.NoError(t, err)
require.Contains(t, resp, tt.expectedOutput)

api.AssertExpectations(t)
mockAPI.AssertExpectations(t)
mockTracker.AssertExpectations(t)
})
}
Expand Down Expand Up @@ -290,8 +275,6 @@ func TestHandleStart(t *testing.T) {
api.On("SendEphemeralPost", "demoUserID", mock.Anything).Return(&model.Post{})
mockTracker.On("TrackUserEvent", mock.Anything, "demoUserID", mock.Anything).Return(nil)
},
expectError: false,
expectedOutput: "",
},
{
name: "Authentication error",
Expand Down Expand Up @@ -351,24 +334,16 @@ func TestHandleStart(t *testing.T) {
mockClient.On("CreateMeeting", mock.Anything, mock.Anything, mock.Anything).Return(&msgraph.OnlineMeeting{JoinURL: &joinURL}, nil)
mockTracker.On("TrackUserEvent", "meeting_started", "demoUserID", mock.Anything).Return(nil)
},
expectError: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
api := &plugintest.API{}
mockAPI := &plugintest.API{}
mockTracker := &MockTracker{}
mockClient := &MockClient{}

p := &Plugin{
MattermostPlugin: plugin.MattermostPlugin{
API: api,
},
tracker: mockTracker,
client: mockClient,
}

p := SetupMockPlugin(mockAPI, mockTracker, mockClient)
p.setConfiguration(&configuration{
EncryptionKey: "demo_encrypt_key",
})
Expand All @@ -383,7 +358,7 @@ func TestHandleStart(t *testing.T) {
encryptedUserInfo, err := userInfo.EncryptedJSON([]byte("demo_encrypt_key"))
require.NoError(t, err)

tt.mockSetup(api, encryptedUserInfo, mockTracker, mockClient)
tt.mockSetup(mockAPI, encryptedUserInfo, mockTracker, mockClient)

resp, err := p.handleStart(tt.args, tt.commandArgs)
if tt.expectError {
Expand All @@ -393,7 +368,7 @@ func TestHandleStart(t *testing.T) {
require.Contains(t, resp, tt.expectedOutput)
}

api.AssertExpectations(t)
mockAPI.AssertExpectations(t)
mockTracker.AssertExpectations(t)
})
}
Expand All @@ -410,3 +385,52 @@ func TestGetHelpText(t *testing.T) {
actual := p.getHelpText()
require.Equal(t, expected, actual)
}

func TestExecuteCommand(t *testing.T) {
mockAPI := &plugintest.API{}
p := SetupMockPlugin(mockAPI, nil, nil)

var dummyPluginContext plugin.Context

tests := []struct {
name string
commandArgs model.CommandArgs
expectedMsg string
}{
{
name: "Invalid Command",
commandArgs: model.CommandArgs{
Command: "/dummyCommand start",
ChannelId: "dummyChannelID",
UserId: "dummyUserID",
},
expectedMsg: "Command '/dummyCommand' is not /mstmeetings. Please try again.",
},
{
name: "Successful execution of Help Command",
commandArgs: model.CommandArgs{
Command: "/mstmeetings help",
ChannelId: "dummyChannelID",
UserId: "dummyUserID",
},
expectedMsg: "###### Mattermost MS Teams Meetings Plugin - Slash Command Help\n* `/mstmeetings start` - Start an MS Teams meeting. \n* `/mstmeetings connect` - Connect to MS Teams meeting. \n* `/mstmeetings disconnect` - Disconnect your Mattermost account from MS Teams. \n* `/mstmeetings help` - Display this help text.",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
post := &model.Post{
UserId: p.botUserID,
ChannelId: tt.commandArgs.ChannelId,
Message: tt.expectedMsg,
}

mockAPI.On("SendEphemeralPost", tt.commandArgs.UserId, post).Return(&model.Post{}).Once()

response, _ := p.ExecuteCommand(&dummyPluginContext, &tt.commandArgs) // nolint:gosec

require.Equal(t, &model.CommandResponse{}, response)
mockAPI.AssertExpectations(t)
})
}
}
Loading

0 comments on commit e74683b

Please sign in to comment.