generated from mattermost/mattermost-plugin-starter-template
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MM-862]: Added server testcases for api/notification.go (#419)
* [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
1 parent
ef7d055
commit 890746b
Showing
2 changed files
with
82 additions
and
0 deletions.
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,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) | ||
}) | ||
} | ||
} |
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