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.
Merge branch 'master' of github.com:mattermost/mattermost-plugin-msca…
…lendar into MM-844
- Loading branch information
Showing
7 changed files
with
1,453 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,80 @@ | ||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/golang/mock/gomock" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/mattermost/mattermost-plugin-mscalendar/calendar/store" | ||
|
||
"github.com/mattermost/mattermost/server/public/model" | ||
) | ||
|
||
func TestAutocompleteConnectedUsers(t *testing.T) { | ||
api, mockStore, _, _, _, mockLogger, mockLoggerWith, _ := GetMockSetup(t) | ||
|
||
tests := []struct { | ||
name string | ||
setup func() | ||
assertions func(t *testing.T, rec *httptest.ResponseRecorder) | ||
}{ | ||
{ | ||
name: "Unauthorized user", | ||
setup: func() { | ||
mockStore.EXPECT().LoadUser(gomock.Any()).Return(nil, store.ErrNotFound).Times(1) | ||
mockLogger.EXPECT().With(gomock.Any()).Return(mockLoggerWith).Times(1) | ||
mockLoggerWith.EXPECT().Errorf("user unauthorized").Times(1) | ||
}, | ||
assertions: func(t *testing.T, rec *httptest.ResponseRecorder) { | ||
assert.Equal(t, http.StatusUnauthorized, rec.Result().StatusCode) | ||
}, | ||
}, | ||
{ | ||
name: "Search error", | ||
setup: func() { | ||
mockStore.EXPECT().LoadUser(gomock.Any()).Return(&store.User{}, nil).Times(1) | ||
mockStore.EXPECT().SearchInUserIndex(gomock.Any(), gomock.Any()).Return(nil, errors.New("search error")).Times(1) | ||
mockLogger.EXPECT().With(gomock.Any()).Return(mockLoggerWith).Times(1) | ||
mockLoggerWith.EXPECT().Errorf("unable to search in user index").Times(1) | ||
}, | ||
assertions: func(t *testing.T, rec *httptest.ResponseRecorder) { | ||
// status code for this scenario should be 500, but it is 200 somehow | ||
var response model.PostActionIntegrationResponse | ||
err := json.NewDecoder(rec.Body).Decode(&response) | ||
assert.NoError(t, err) | ||
assert.Contains(t, response.EphemeralText, "unable to search in user index") | ||
}, | ||
}, | ||
{ | ||
name: "Successful search", | ||
setup: func() { | ||
mockStore.EXPECT().LoadUser(gomock.Any()).Return(&store.User{}, nil).Times(1) | ||
mockStore.EXPECT().SearchInUserIndex(gomock.Any(), 10).Return(store.UserIndex{}, nil).Times(1) | ||
}, | ||
assertions: func(t *testing.T, rec *httptest.ResponseRecorder) { | ||
assert.Equal(t, http.StatusOK, rec.Result().StatusCode) | ||
}, | ||
}, | ||
} | ||
for _, tc := range tests { | ||
t.Run(tc.name, func(t *testing.T) { | ||
tc.setup() | ||
|
||
req := httptest.NewRequest(http.MethodGet, "/autocomplete?search=test", nil) | ||
req.Header.Set(MMUserIDHeader, MockUserID) | ||
rec := httptest.NewRecorder() | ||
|
||
api.autocompleteConnectedUsers(rec, req) | ||
|
||
fmt.Print("rec=", rec) | ||
|
||
tc.assertions(t, rec) | ||
}) | ||
} | ||
} |
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,79 @@ | ||
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/store" | ||
) | ||
|
||
func TestConnectedUserHandler(t *testing.T) { | ||
api, mockStore, _, _, _, mockLogger, mockLoggerWith, _ := GetMockSetup(t) | ||
|
||
tests := []struct { | ||
name string | ||
setup func(req *http.Request) | ||
assertions func(t *testing.T, rec *httptest.ResponseRecorder) | ||
}{ | ||
{ | ||
name: "Missing MattermostUserId in header", | ||
setup: func(req *http.Request) { | ||
mockLogger.EXPECT().Errorf("connectedUserHandler, unauthorized user").Times(1) | ||
}, | ||
assertions: func(t *testing.T, rec *httptest.ResponseRecorder) { | ||
assert.Equal(t, http.StatusUnauthorized, rec.Result().StatusCode) | ||
}, | ||
}, | ||
{ | ||
name: "Error loading user from store", | ||
setup: func(req *http.Request) { | ||
req.Header.Set(MMUserIDHeader, MockUserID) | ||
mockStore.EXPECT().LoadUser(MockUserID).Return(nil, errors.New("store error")).Times(1) | ||
mockLogger.EXPECT().With(gomock.Any()).Return(mockLoggerWith).Times(1) | ||
mockLoggerWith.EXPECT().Errorf("connectedUserHandler, error occurred while loading user from store").Times(1) | ||
}, | ||
assertions: func(t *testing.T, rec *httptest.ResponseRecorder) { | ||
assert.Equal(t, http.StatusInternalServerError, rec.Result().StatusCode) | ||
}, | ||
}, | ||
{ | ||
name: "User not found in store", | ||
setup: func(req *http.Request) { | ||
req.Header.Set(MMUserIDHeader, MockUserID) | ||
mockStore.EXPECT().LoadUser(MockUserID).Return(nil, store.ErrNotFound).Times(1) | ||
mockLogger.EXPECT().With(gomock.Any()).Return(mockLoggerWith).Times(1) | ||
mockLoggerWith.EXPECT().Errorf("connectedUserHandler, user not found in store").Times(1) | ||
}, | ||
assertions: func(t *testing.T, rec *httptest.ResponseRecorder) { | ||
assert.Equal(t, http.StatusUnauthorized, rec.Result().StatusCode) | ||
}, | ||
}, | ||
{ | ||
name: "User successfully connected", | ||
setup: func(req *http.Request) { | ||
req.Header.Set(MMUserIDHeader, MockUserID) | ||
mockStore.EXPECT().LoadUser(MockUserID).Return(&store.User{}, nil).Times(1) | ||
}, | ||
assertions: func(t *testing.T, rec *httptest.ResponseRecorder) { | ||
assert.Equal(t, http.StatusOK, rec.Result().StatusCode) | ||
assert.JSONEq(t, `{"is_connected": true}`, rec.Body.String()) | ||
}, | ||
}, | ||
} | ||
for _, tc := range tests { | ||
t.Run(tc.name, func(t *testing.T) { | ||
req := httptest.NewRequest(http.MethodGet, "/connected", nil) | ||
tc.setup(req) | ||
rec := httptest.NewRecorder() | ||
|
||
api.connectedUserHandler(rec, req) | ||
|
||
tc.assertions(t, rec) | ||
}) | ||
} | ||
} |
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) | ||
}) | ||
} | ||
} |
Oops, something went wrong.