-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add .nvmrc * GitLab subscriptions RHS Expose a RHS experience for GitLab that showcases the current channels's subscriptions, as well as information on the user currently logged in. * fix PropTypes * ensure subscription key is unique * refactor subscriptionsr response * show subscription features * implement empty webapp test script * avoid re-render on new array * avoid unnecessary Promise.all * nvm v14.21.1 * consistent action return types * enhance PropTypes with isRequired * ensure unique subscriptions key * avoid depending on url.JoinPath from Go 1.19 * switch to styled-components * log error on failed marshal * switch to vanilla CSS * revert package.json * restore npm test
- Loading branch information
1 parent
609aed6
commit 2275492
Showing
19 changed files
with
1,249 additions
and
11 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 @@ | ||
14.21.1 |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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,141 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
|
||
"github.com/mattermost/mattermost-server/v6/model" | ||
"github.com/mattermost/mattermost-server/v6/plugin/plugintest" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"golang.org/x/oauth2" | ||
|
||
"github.com/mattermost/mattermost-plugin-gitlab/server/gitlab" | ||
) | ||
|
||
func TestGetChannelSubscriptions(t *testing.T) { | ||
setupPlugin := func(t *testing.T) (*Plugin, *plugintest.API) { | ||
t.Helper() | ||
|
||
config := configuration{ | ||
GitlabURL: "https://example.com", | ||
GitlabOAuthClientID: "client_id", | ||
GitlabOAuthClientSecret: "secret", | ||
EncryptionKey: "aaaaaaaaaaaaaaaa", | ||
} | ||
|
||
plugin := Plugin{configuration: &config} | ||
plugin.initializeAPI() | ||
|
||
token := oauth2.Token{ | ||
AccessToken: "access_token", | ||
Expiry: time.Now().Add(1 * time.Hour), | ||
} | ||
info := gitlab.UserInfo{ | ||
UserID: "user_id", | ||
Token: &token, | ||
GitlabUsername: "gitlab_username", | ||
GitlabUserID: 0, | ||
} | ||
encryptedToken, err := encrypt([]byte(config.EncryptionKey), info.Token.AccessToken) | ||
require.NoError(t, err) | ||
|
||
info.Token.AccessToken = encryptedToken | ||
|
||
jsonInfo, err := json.Marshal(info) | ||
require.NoError(t, err) | ||
|
||
mock := &plugintest.API{} | ||
plugin.SetAPI(mock) | ||
|
||
mock.On("KVGet", "user_id_gitlabtoken").Return(jsonInfo, nil).Once() | ||
|
||
return &plugin, mock | ||
} | ||
|
||
t.Run("no permission to channel", func(t *testing.T) { | ||
plugin, mock := setupPlugin(t) | ||
|
||
mock.On("HasPermissionToChannel", "user_id", "id", model.PermissionReadChannel).Return(false, nil).Once() | ||
|
||
w := httptest.NewRecorder() | ||
r := httptest.NewRequest(http.MethodGet, "/api/v1/channel/id/subscriptions", nil) | ||
r.Header.Set("Mattermost-User-ID", "user_id") | ||
|
||
plugin.ServeHTTP(nil, w, r) | ||
|
||
result := w.Result() | ||
assert.NotNil(t, result) | ||
assert.Equal(t, http.StatusUnauthorized, result.StatusCode) | ||
}) | ||
|
||
t.Run("no subscriptions", func(t *testing.T) { | ||
plugin, mock := setupPlugin(t) | ||
|
||
mock.On("HasPermissionToChannel", "user_id", "id", model.PermissionReadChannel).Return(true, nil).Once() | ||
mock.On("KVGet", SubscriptionsKey).Return([]byte(`{"Repositories":{"repo1":[]}}`), nil) | ||
|
||
w := httptest.NewRecorder() | ||
r := httptest.NewRequest(http.MethodGet, "/api/v1/channel/id/subscriptions", nil) | ||
r.Header.Set("Mattermost-User-ID", "user_id") | ||
|
||
plugin.ServeHTTP(nil, w, r) | ||
|
||
result := w.Result() | ||
defer result.Body.Close() | ||
data, err := io.ReadAll(result.Body) | ||
require.NoError(t, err) | ||
|
||
assert.NotNil(t, result) | ||
assert.Equal(t, http.StatusOK, result.StatusCode) | ||
assert.Equal(t, "[]", string(data)) | ||
}) | ||
|
||
t.Run("no subscriptions for channel", func(t *testing.T) { | ||
plugin, mock := setupPlugin(t) | ||
|
||
mock.On("HasPermissionToChannel", "user_id", "id", model.PermissionReadChannel).Return(true, nil).Once() | ||
mock.On("KVGet", SubscriptionsKey).Return([]byte(`{"Repositories":{"namespace":[{"ChannelID":"other","Repository":"repo1"}]}}`), nil) | ||
|
||
w := httptest.NewRecorder() | ||
r := httptest.NewRequest(http.MethodGet, "/api/v1/channel/id/subscriptions", nil) | ||
r.Header.Set("Mattermost-User-ID", "user_id") | ||
|
||
plugin.ServeHTTP(nil, w, r) | ||
|
||
result := w.Result() | ||
defer result.Body.Close() | ||
data, err := io.ReadAll(result.Body) | ||
require.NoError(t, err) | ||
|
||
assert.NotNil(t, result) | ||
assert.Equal(t, http.StatusOK, result.StatusCode) | ||
assert.Equal(t, "[]", string(data)) | ||
}) | ||
|
||
t.Run("subscriptions for channel", func(t *testing.T) { | ||
plugin, mock := setupPlugin(t) | ||
|
||
mock.On("HasPermissionToChannel", "user_id", "id", model.PermissionReadChannel).Return(true, nil).Once() | ||
mock.On("KVGet", SubscriptionsKey).Return([]byte(`{"Repositories":{"namespace":[{"ChannelID":"other","Repository":"repo1","Features":"feature1,feature2","CreatorID":"creator1"},{"ChannelID":"id","Repository":"repo2","Features":"feature3,feature4","CreatorID":"creator2"},{"ChannelID":"id", "Repository":"repo3","Features":"feature5","CreatorID":"creator3"},{"ChannelID":"id","Repository":"repo4-empty"}]}}`), nil) | ||
|
||
w := httptest.NewRecorder() | ||
r := httptest.NewRequest(http.MethodGet, "/api/v1/channel/id/subscriptions", nil) | ||
r.Header.Set("Mattermost-User-ID", "user_id") | ||
|
||
plugin.ServeHTTP(nil, w, r) | ||
|
||
result := w.Result() | ||
defer result.Body.Close() | ||
data, err := io.ReadAll(result.Body) | ||
require.NoError(t, err) | ||
|
||
assert.NotNil(t, result) | ||
assert.Equal(t, http.StatusOK, result.StatusCode) | ||
assert.Equal(t, `[{"repository_name":"repo2","repository_url":"https://example.com/repo2","features":["feature3","feature4"],"creator_id":"creator2"},{"repository_name":"repo3","repository_url":"https://example.com/repo3","features":["feature5"],"creator_id":"creator3"},{"repository_name":"repo4-empty","repository_url":"https://example.com/repo4-empty","features":[],"creator_id":""}]`, string(data)) | ||
}) | ||
} |
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
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
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
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
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
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
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.