diff --git a/pkg/api/clients_test.go b/pkg/api/clients_test.go index 48f0a6a..9aa5c30 100644 --- a/pkg/api/clients_test.go +++ b/pkg/api/clients_test.go @@ -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) @@ -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) @@ -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) }) } @@ -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") diff --git a/pkg/api/scopes_test.go b/pkg/api/scopes_test.go index 51b23f5..1e5a877 100644 --- a/pkg/api/scopes_test.go +++ b/pkg/api/scopes_test.go @@ -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) @@ -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) @@ -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) @@ -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) }) diff --git a/pkg/api/util_test.go b/pkg/api/util_test.go index f595810..af2fa97 100644 --- a/pkg/api/util_test.go +++ b/pkg/api/util_test.go @@ -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