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

Added server test cases for store/oauth2_store.go #434

Open
wants to merge 1 commit into
base: add_store/subscription_store.go_testcases
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
103 changes: 103 additions & 0 deletions calendar/store/oauth2_store_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package store

import (
"testing"

"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"

"github.com/mattermost/mattermost-plugin-mscalendar/calendar/testutil"

"github.com/mattermost/mattermost/server/public/model"
)

func TestVerifyOAuth2State(t *testing.T) {
tests := []struct {
name string
setup func(*testutil.MockPluginAPI)
assertions func(*testing.T, error)
}{
{
name: "Error loading state",
setup: func(mockAPI *testutil.MockPluginAPI) {
mockAPI.On("KVGet", "oauth2_fb89cea34670836627b56ad5b94ce5e3").Return(nil, &model.AppError{Message: "Error getting state"}).Times(1)
mockAPI.On("KVDelete", "oauth2_fb89cea34670836627b56ad5b94ce5e3").Return(nil)
},
assertions: func(t *testing.T, err error) {
require.Error(t, err)
require.EqualError(t, err, "failed plugin KVGet: Error getting state")
},
},
{
name: "Invalid Oauth state",
setup: func(mockAPI *testutil.MockPluginAPI) {
mockAPI.On("KVGet", "oauth2_fb89cea34670836627b56ad5b94ce5e3").Return([]byte("invalidState"), nil).Times(1)
},
assertions: func(t *testing.T, err error) {
require.Error(t, err)
require.EqualError(t, err, "invalid oauth state, please try again")
},
},
{
name: "Successfull Oauth state verification",
setup: func(mockAPI *testutil.MockPluginAPI) {
mockAPI.On("KVGet", "oauth2_fb89cea34670836627b56ad5b94ce5e3").Return([]byte(MockState), nil).Times(1)
},
assertions: func(t *testing.T, err error) {
require.Nil(t, err)
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mockAPI, store, _, _, _ := GetMockSetup(t)
tt.setup(mockAPI)

err := store.VerifyOAuth2State(MockState)

tt.assertions(t, err)

mockAPI.AssertExpectations(t)
})
}
}

func TestStoreOAuth2State(t *testing.T) {
tests := []struct {
name string
setup func(*testutil.MockPluginAPI)
assertions func(*testing.T, error)
}{
{
name: "Error loading state",
setup: func(mockAPI *testutil.MockPluginAPI) {
mockAPI.On("KVSetWithExpiry", "oauth2_fb89cea34670836627b56ad5b94ce5e3", mock.Anything, int64(oAuth2StateTimeToLive)).Return(&model.AppError{Message: "Error loading state"}).Times(1)
},
assertions: func(t *testing.T, err error) {
require.Error(t, err)
require.ErrorContains(t, err, "Error loading state")
},
},
{
name: "Successfull Oauth state verification",
setup: func(mockAPI *testutil.MockPluginAPI) {
mockAPI.On("KVSetWithExpiry", "oauth2_fb89cea34670836627b56ad5b94ce5e3", mock.Anything, int64(oAuth2StateTimeToLive)).Return(nil).Times(1)
},
assertions: func(t *testing.T, err error) {
require.Nil(t, err)
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mockAPI, store, _, _, _ := GetMockSetup(t)
tt.setup(mockAPI)

err := store.StoreOAuth2State(MockState)

tt.assertions(t, err)

mockAPI.AssertExpectations(t)
})
}
}
1 change: 1 addition & 0 deletions calendar/store/test_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const (
MockRemoteJSON = `{"remote": {"id": "mockRemoteID"}}`
MockUserJSON = `[{"MattermostUserID":"mockMMUserID","RemoteID":"mockRemoteID"}]`
MockUserDetailsWithEventJSON = `{"mm_id":"mockUserID","active_events": []}`
MockState = "mockState"
)

func GetMockSetup(t *testing.T) (*testutil.MockPluginAPI, Store, *mock_bot.MockLogger, *mock_bot.MockLogger, *mock_tracker.MockTracker) {
Expand Down
Loading