Skip to content

Commit

Permalink
[MM-862]: Added server testcases for api/notification.go (#419)
Browse files Browse the repository at this point in the history
* [MM-860]: Added server testcases for api/autocomplete.go

* [MM-860]: Updated the api/test_utils

* [MM-861]: Added server testcases for api/connected_user.go

* [MM-860]: Updated the api/test_utils

* [MM-862]: Added server testcases for api/notification.go

* [MM-862]: review fixes
  • Loading branch information
Kshitij-Katiyar authored Oct 24, 2024
1 parent ef7d055 commit 890746b
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
65 changes: 65 additions & 0 deletions calendar/api/notifcation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package api

import (
"errors"
"net/http"
"net/http/httptest"
"testing"

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

"github.com/mattermost/mattermost-plugin-mscalendar/calendar/remote"
)

func TestNotification(t *testing.T) {
api, _, _, mockRemote, _, mockLogger, mockLoggerWith, _ := GetMockSetup(t)
mockProcessor := &MockNotificationProcessor{}
api.NotificationProcessor = mockProcessor

tests := []struct {
name string
setup func(*MockNotificationProcessor)
assertions func(*httptest.ResponseRecorder, *MockNotificationProcessor)
}{
{
name: "Error while adding event to notification queue",
setup: func(mockProcessor *MockNotificationProcessor) {
mockProcessor.err = errors.New("queue error")
mockRemote.EXPECT().HandleWebhook(gomock.Any(), gomock.Any()).
Return([]*remote.Notification{{}}).Times(1)

mockLogger.EXPECT().With(gomock.Any()).Return(mockLoggerWith).Times(1)
mockLoggerWith.EXPECT().Errorf("notification, error occurred while adding webhook event to notification queue").Times(1)
},
assertions: func(rec *httptest.ResponseRecorder, mockProcessor *MockNotificationProcessor) {
assert.Equal(t, http.StatusInternalServerError, rec.Result().StatusCode)
assert.Equal(t, 0, len(mockProcessor.queue))
},
},
{
name: "Successful notification processing",
setup: func(mockProcessor *MockNotificationProcessor) {
mockProcessor.err = nil
mockRemote.EXPECT().HandleWebhook(gomock.Any(), gomock.Any()).
Return([]*remote.Notification{{}, {}}).Times(1)
},
assertions: func(rec *httptest.ResponseRecorder, mockProcessor *MockNotificationProcessor) {
assert.Equal(t, http.StatusOK, rec.Result().StatusCode)
assert.Equal(t, 2, len(mockProcessor.queue))
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
tc.setup(mockProcessor)

req := httptest.NewRequest(http.MethodPost, "/notification", nil)
rec := httptest.NewRecorder()

api.notification(rec, req)

tc.assertions(rec, mockProcessor)
})
}
}
17 changes: 17 additions & 0 deletions calendar/api/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/mattermost/mattermost-plugin-mscalendar/calendar/engine"
"github.com/mattermost/mattermost-plugin-mscalendar/calendar/engine/mock_plugin_api"
"github.com/mattermost/mattermost-plugin-mscalendar/calendar/remote"
"github.com/mattermost/mattermost-plugin-mscalendar/calendar/remote/mock_remote"
"github.com/mattermost/mattermost-plugin-mscalendar/calendar/store/mock_store"
"github.com/mattermost/mattermost-plugin-mscalendar/calendar/utils/bot/mock_bot"
Expand All @@ -22,6 +23,22 @@ const (
MockRemoteUserID = "mockRemoteUserID"
)

type MockNotificationProcessor struct {
queue []*remote.Notification
err error
}

func (m *MockNotificationProcessor) Enqueue(notifications ...*remote.Notification) error {
if m.err != nil {
return m.err
}
m.queue = append(m.queue, notifications...)
return nil
}

func (m *MockNotificationProcessor) Configure(env engine.Env) {}
func (m *MockNotificationProcessor) Quit() {}

// revive:disable-next-line:unexported-return
func GetMockSetup(t *testing.T) (*api, *mock_store.MockStore, *mock_bot.MockPoster, *mock_remote.MockRemote, *mock_plugin_api.MockPluginAPI, *mock_bot.MockLogger, *mock_bot.MockLogger, *mock_remote.MockClient) {
ctrl := gomock.NewController(t)
Expand Down

0 comments on commit 890746b

Please sign in to comment.