Skip to content

Commit

Permalink
feat: add scopes to client view.
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag committed Aug 2, 2022
1 parent d4fd70b commit 8d40fbe
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
26 changes: 19 additions & 7 deletions pkg/api/clients_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func TestCreateClient(t *testing.T) {

require.Equal(t, http.StatusCreated, res.Code)

createdClient := readObject[client](t, res)
createdClient := readTestResponse[client](t, res)
require.NotEmpty(t, createdClient.ID)
require.Equal(t, tc.options, createdClient.ClientOptions)

Expand Down Expand Up @@ -166,7 +166,7 @@ func TestUpdateClient(t *testing.T) {

require.Equal(t, http.StatusOK, res.Code)

updatedClient := readObject[client](t, res)
updatedClient := readTestResponse[client](t, res)
require.NotEmpty(t, updatedClient.ID)
require.Equal(t, tc.options, updatedClient.ClientOptions)

Expand Down Expand Up @@ -194,22 +194,34 @@ func TestListClients(t *testing.T) {

require.Equal(t, http.StatusOK, res.Code)

clients := readObject[[]client](t, res)
clients := readTestResponse[[]client](t, res)
require.Len(t, clients, 2)
})
}

func TestReadClient(t *testing.T) {
withDbAndClientRouter(t, func(router *mux.Router, db *gorm.DB) {
client := auth.NewClient(auth.ClientOptions{})
require.NoError(t, db.Create(client).Error)

req := httptest.NewRequest(http.MethodGet, "/clients/"+client.Id, nil)
scope1 := auth.NewScope("XXX")
require.NoError(t, db.Create(scope1).Error)

client1 := auth.NewClient(auth.ClientOptions{})
client1.Scopes = append(client1.Scopes, *scope1)
require.NoError(t, db.Create(client1).Error)

req := httptest.NewRequest(http.MethodGet, "/clients/"+client1.Id, nil)
res := httptest.NewRecorder()

router.ServeHTTP(res, req)

require.Equal(t, http.StatusOK, res.Code)

ret := readTestResponse[client](t, res)
require.Equal(t, client{
ClientOptions: auth.ClientOptions{},
ID: client1.Id,
Scopes: []string{scope1.ID},
}, ret)
})
}

Expand All @@ -225,7 +237,7 @@ func TestGenerateNewSecret(t *testing.T) {

router.ServeHTTP(res, req)

result := readObject[secretCreateResult](t, res)
result := readTestResponse[secretCreateResult](t, res)
require.NotEmpty(t, result.Clear)
require.Equal(t, result.LastDigits, result.Clear[len(result.Clear)-4:])
require.Equal(t, result.Name, "secret1")
Expand Down
8 changes: 4 additions & 4 deletions pkg/api/scopes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestCreateScope(t *testing.T) {

require.Equal(t, http.StatusCreated, res.Code)

createdScope := readObject[scope](t, res)
createdScope := readTestResponse[scope](t, res)
require.NotEmpty(t, createdScope.ID)
require.Equal(t, "XXX", createdScope.Label)

Expand All @@ -61,7 +61,7 @@ func TestUpdateScope(t *testing.T) {

require.Equal(t, http.StatusOK, res.Code)

updatedScope := readObject[scope](t, res)
updatedScope := readTestResponse[scope](t, res)
require.NotEmpty(t, updatedScope.ID)
require.Equal(t, "YYY", updatedScope.Label)

Expand All @@ -86,7 +86,7 @@ func TestListScopes(t *testing.T) {

require.Equal(t, http.StatusOK, res.Code)

scopes := readObject[[]scope](t, res)
scopes := readTestResponse[[]scope](t, res)
require.Len(t, scopes, 2)
require.Len(t, scopes[1].Triggers, 1)
require.Equal(t, scopes[1].Triggers[0], scopes[0].ID)
Expand All @@ -108,7 +108,7 @@ func TestReadScope(t *testing.T) {

require.Equal(t, http.StatusOK, res.Code)

scope := readObject[scope](t, res)
scope := readTestResponse[scope](t, res)
require.Len(t, scope.Triggers, 1)
require.Equal(t, scope.Triggers[0], scope1.ID)
})
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func createJSONBuffer(t *testing.T, v any) io.Reader {
return bytes.NewBuffer(data)
}

func readObject[T any](t *testing.T, recorder *httptest.ResponseRecorder) T {
func readTestResponse[T any](t *testing.T, recorder *httptest.ResponseRecorder) T {
body := sharedapi.BaseResponse[T]{}
require.NoError(t, json.NewDecoder(recorder.Body).Decode(&body))
return *body.Data
Expand Down

0 comments on commit 8d40fbe

Please sign in to comment.