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
Add store/subscription store.go testcases #433
Open
Kshitij-Katiyar
wants to merge
1
commit into
add_store/user_store.go_testcases
Choose a base branch
from
add_store/subscription_store.go_testcases
base: add_store/user_store.go_testcases
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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", | ||||||
setup: func(mockAPI *testutil.MockPluginAPI) { | ||||||
mockAPI.On("KVGet", "sub_0c47c5b7e2a88ec9256c8ac0e71b0f6e").Return([]byte(`{"PluginVersion":"1.0","Remote":{"ID":"mockRemoteUserID","CreatorID":"mockCreatorID"}}`), nil).Times(1) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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", | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
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) | ||||||
}) | ||||||
} | ||||||
} |
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.