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

Add store/subscription store.go testcases #433

Open
wants to merge 1 commit into
base: add_store/user_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
182 changes: 182 additions & 0 deletions calendar/store/subscription_store_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
package store

import (
"testing"

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

"github.com/mattermost/mattermost-plugin-mscalendar/calendar/testutil"
"github.com/mattermost/mattermost-plugin-mscalendar/calendar/utils/bot/mock_bot"

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

func TestLoadSubscription(t *testing.T) {
tests := []struct {
name string
setup func(*testutil.MockPluginAPI)
assertions func(*testing.T, *Subscription, error)
}{
{
name: "Error loading subscription",
setup: func(mockAPI *testutil.MockPluginAPI) {
mockAPI.On("KVGet", "sub_0c47c5b7e2a88ec9256c8ac0e71b0f6e").Return(nil, &model.AppError{Message: "Subscription not found"}).Times(1)
},
assertions: func(t *testing.T, sub *Subscription, err error) {
require.Error(t, err)
require.Nil(t, sub)
require.EqualError(t, err, "failed plugin KVGet: Subscription not found")
},
},
{
name: "Successful Load",
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
name: "Successful Load",
name: "Subscription successfully loaded",

setup: func(mockAPI *testutil.MockPluginAPI) {
mockAPI.On("KVGet", "sub_0c47c5b7e2a88ec9256c8ac0e71b0f6e").Return([]byte(`{"PluginVersion":"1.0","Remote":{"ID":"mockRemoteUserID","CreatorID":"mockCreatorID"}}`), nil).Times(1)
Copy link
Contributor

Choose a reason for hiding this comment

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

Use the IDs from existing constants

},
assertions: func(t *testing.T, sub *Subscription, err error) {
require.NoError(t, err)
require.NotNil(t, sub)
require.Equal(t, "1.0", sub.PluginVersion)
require.Equal(t, MockRemoteUserID, sub.Remote.ID)
require.Equal(t, MockCreatorID, sub.Remote.CreatorID)
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mockAPI, store, _, _, _ := GetMockSetup(t)
tt.setup(mockAPI)

sub, err := store.LoadSubscription(MockSubscriptionID)

tt.assertions(t, sub, err)
mockAPI.AssertExpectations(t)
})
}
}

func TestStoreUserSubscription(t *testing.T) {
mockUser := GetMockUser()
mockSubscription := GetMockSubscription()

tests := []struct {
name string
setup func(*testutil.MockPluginAPI, *mock_bot.MockLogger, *mock_bot.MockLogger)
assertions func(*testing.T, error)
}{
{
name: "User does not match subscription creator",
setup: func(_ *testutil.MockPluginAPI, _ *mock_bot.MockLogger, _ *mock_bot.MockLogger) {},
assertions: func(t *testing.T, err error) {
require.Error(t, err)
require.EqualError(t, err, `user "mockRemoteID" does not match the subscription creator "mockCreatorID"`)
},
},
{
name: "Error storing subscription",
setup: func(mockAPI *testutil.MockPluginAPI, _ *mock_bot.MockLogger, _ *mock_bot.MockLogger) {
mockSubscription.Remote.CreatorID = mockUser.Remote.ID
mockAPI.On("KVSet", "sub_0c47c5b7e2a88ec9256c8ac0e71b0f6e", mock.Anything).Return(&model.AppError{Message: "Failed to store subscription"}).Times(1)
},
assertions: func(t *testing.T, err error) {
require.Error(t, err)
require.ErrorContains(t, err, "Failed to store subscription")
},
},
{
name: "Error storing user settings",
setup: func(mockAPI *testutil.MockPluginAPI, _ *mock_bot.MockLogger, _ *mock_bot.MockLogger) {
mockAPI.ExpectedCalls = nil
mockAPI.On("KVSet", "sub_0c47c5b7e2a88ec9256c8ac0e71b0f6e", mock.Anything).Return(nil).Times(1)
mockAPI.On("KVSet", "user_c3b5020d58a049787bc969768465b890", mock.Anything).Return(&model.AppError{Message: "Failed to store user settings"}).Times(1)
},
assertions: func(t *testing.T, err error) {
require.Error(t, err)
require.ErrorContains(t, err, "Failed to store user settings")
},
},
{
name: "Successful Store",
setup: func(mockAPI *testutil.MockPluginAPI, mockLogger *mock_bot.MockLogger, mockLoggerWith *mock_bot.MockLogger) {
mockAPI.ExpectedCalls = nil
mockAPI.On("KVSet", "sub_0c47c5b7e2a88ec9256c8ac0e71b0f6e", mock.Anything).Return(nil).Times(1)
mockAPI.On("KVSet", "user_c3b5020d58a049787bc969768465b890", mock.Anything).Return(nil).Times(1)
mockLogger.EXPECT().With(gomock.Any()).Return(mockLoggerWith).Times(1)
mockLoggerWith.EXPECT().Debugf("store: stored mattermost user subscription.").Times(1)
},
assertions: func(t *testing.T, err error) {
require.NoError(t, err)
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mockAPI, store, mockLogger, mockLoggerWith, _ := GetMockSetup(t)
tt.setup(mockAPI, mockLogger, mockLoggerWith)

err := store.StoreUserSubscription(mockUser, mockSubscription)

tt.assertions(t, err)
mockAPI.AssertExpectations(t)
})
}
}

func TestDeleteUserSubscription(t *testing.T) {
tests := []struct {
name string
setup func(*testutil.MockPluginAPI, *mock_bot.MockLogger, *mock_bot.MockLogger)
assertions func(*testing.T, error)
}{
{
name: "Error deleting subscription",
setup: func(mockAPI *testutil.MockPluginAPI, _ *mock_bot.MockLogger, _ *mock_bot.MockLogger) {
mockAPI.On("KVDelete", "sub_0c47c5b7e2a88ec9256c8ac0e71b0f6e").Return(&model.AppError{Message: "Failed to delete subscription"}).Times(1)
},
assertions: func(t *testing.T, err error) {
require.Error(t, err)
require.ErrorContains(t, err, "Failed to delete subscription")
},
},
{
name: "Error updating user settings",
setup: func(mockAPI *testutil.MockPluginAPI, _ *mock_bot.MockLogger, _ *mock_bot.MockLogger) {
mockAPI.ExpectedCalls = nil
mockAPI.On("KVDelete", "sub_0c47c5b7e2a88ec9256c8ac0e71b0f6e").Return(nil).Times(1)
mockAPI.On("KVSet", "user_c3b5020d58a049787bc969768465b890", mock.Anything).Return(&model.AppError{Message: "Failed to update user settings"}).Times(1)
},
assertions: func(t *testing.T, err error) {
require.Error(t, err)
require.ErrorContains(t, err, "Failed to update user settings")
},
},
{
name: "Successful Delete",
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
name: "Successful Delete",
name: "Subscription successfully deleted",

setup: func(mockAPI *testutil.MockPluginAPI, mockLogger *mock_bot.MockLogger, mockLoggerWith *mock_bot.MockLogger) {
mockAPI.ExpectedCalls = nil
mockAPI.On("KVDelete", "sub_0c47c5b7e2a88ec9256c8ac0e71b0f6e").Return(nil).Times(1)
mockAPI.On("KVSet", "user_c3b5020d58a049787bc969768465b890", mock.Anything).Return(nil).Times(1)
mockAPI.On("KVSet", "mmuid_e138a0f218087f9324d8c77f87d5f3a0", mock.Anything).Return(nil).Times(1)
mockLogger.EXPECT().With(gomock.Any()).Return(mockLoggerWith).Times(1)
mockLoggerWith.EXPECT().Debugf("store: deleted mattermost user subscription.").Times(1)
},
assertions: func(t *testing.T, err error) {
require.NoError(t, err)
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mockAPI, store, mockLogger, mockLoggerWith, _ := GetMockSetup(t)
tt.setup(mockAPI, mockLogger, mockLoggerWith)

mockUser := GetMockUser()
err := store.DeleteUserSubscription(mockUser, MockSubscriptionID)

tt.assertions(t, err)
mockAPI.AssertExpectations(t)
})
}
}
39 changes: 27 additions & 12 deletions calendar/store/test_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import (
)

const (
MockEventSubscriptionID = "mockEventSubscriptionID"
MockSubscriptionID = "mockSubscriptionID"
MockCreatorID = "mockCreatorID"
MockMMUsername = "mockMMUsername"
MockMMDisplayName = "mockMMDisplayName"
MockMMUserID = "mockMMUserID"
Expand Down Expand Up @@ -41,6 +44,30 @@ func GetMockSetup(t *testing.T) (*testutil.MockPluginAPI, Store, *mock_bot.MockL
return mockAPI, store, mockLogger, mockLoggerWith, mockTracker
}

func GetMockUser() *User {
return &User{
MattermostUserID: MockMMUserID,
MattermostUsername: MockMMUsername,
MattermostDisplayName: MockMMDisplayName,
Settings: Settings{
EventSubscriptionID: MockEventSubscriptionID,
},
Remote: &remote.User{
ID: MockRemoteID,
Mail: MockRemoteMail,
},
}
}

func GetMockSubscription() *Subscription {
return &Subscription{
Remote: &remote.Subscription{
ID: MockSubscriptionID,
CreatorID: MockCreatorID,
},
}
}

func GetRemoteUserJSON(noOfUsers int) string {
type RemoteUser struct {
MMUsername string `json:"mm_username"`
Expand All @@ -63,15 +90,3 @@ func GetRemoteUserJSON(noOfUsers int) string {
result, _ := json.Marshal(users)
return string(result)
}

func GetMockUser() *User {
return &User{
MattermostUserID: MockMMUserID,
MattermostUsername: MockMMUsername,
MattermostDisplayName: MockMMDisplayName,
Remote: &remote.User{
ID: MockRemoteID,
Mail: MockRemoteMail,
},
}
}
Loading