Skip to content

Commit

Permalink
feat: added unit tests for function GetDefaultSchemas
Browse files Browse the repository at this point in the history
feat: added unit tests for default schema handlers
  • Loading branch information
bencekov committed Nov 2, 2023
1 parent 7bbb49d commit 6968a28
Show file tree
Hide file tree
Showing 7 changed files with 728 additions and 537 deletions.
4 changes: 0 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ require (
github.com/go-openapi/swag v0.22.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.2.0 // indirect
Expand All @@ -60,10 +59,8 @@ require (
github.com/prometheus/client_model v0.4.1-0.20230718164431-9a2bf3000d16 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.11.1 // indirect
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.16.0 // indirect
go.opentelemetry.io/otel/metric v1.19.0 // indirect
go.opentelemetry.io/proto/otlp v1.0.0 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.10.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/oauth2 v0.11.0 // indirect
Expand All @@ -72,7 +69,6 @@ require (
golang.org/x/text v0.13.0 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230711160842-782d3b101e98 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230711160842-782d3b101e98 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98 // indirect
google.golang.org/grpc v1.58.2 // indirect
Expand Down
538 changes: 5 additions & 533 deletions go.sum

Large diffs are not rendered by default.

85 changes: 85 additions & 0 deletions pkg/schemas/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ func (a *API) RegisterEndpoints(mux *chi.Mux) {
mux.Post("/api/v0/schemas", a.handleCreate)
mux.Patch("/api/v0/schemas/{id}", a.handlePartialUpdate)
mux.Delete("/api/v0/schemas/{id}", a.handleRemove)
mux.Get("/api/v0/schemas/default", a.handleDetailDefault)
mux.Put("/api/v0/schemas/default", a.handleUpdateDefault)
}

func (a *API) handleList(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -227,6 +229,89 @@ func (a *API) handleRemove(w http.ResponseWriter, r *http.Request) {
)
}

func (a *API) handleDetailDefault(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")

defaultSchema, err := a.service.GetDefaultSchema(r.Context())

if err != nil {
rr := types.Response{
Status: http.StatusInternalServerError,
Message: err.Error(),
}

w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(rr)

return
}

w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(
types.Response{
Data: defaultSchema,
Message: "Detail of Default Identity Schema",
Status: http.StatusOK,
},
)
}

func (a *API) handleUpdateDefault(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")

defer r.Body.Close()
body, err := io.ReadAll(r.Body)

if err != nil {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(
types.Response{
Message: "Error parsing request payload",
Status: http.StatusBadRequest,
},
)

return
}

schema := new(DefaultSchema)
if err := json.Unmarshal(body, schema); err != nil {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(
types.Response{
Message: "Error parsing JSON payload",
Status: http.StatusBadRequest,
},
)

return

}

defaultSchema, err := a.service.UpdateDefaultSchema(r.Context(), schema)

if err != nil {
rr := types.Response{
Status: http.StatusInternalServerError,
Message: err.Error(),
}

w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(rr)

return
}

w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(
types.Response{
Data: defaultSchema,
Message: "Default Identity Schema updated",
Status: http.StatusOK,
},
)
}

// TODO @shipperizer encapsulate kClient.GenericError into a service error to remove library dependency
func (a *API) error(e *kClient.GenericError) types.Response {
r := types.Response{
Expand Down
233 changes: 233 additions & 0 deletions pkg/schemas/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -964,3 +964,236 @@ func TestHandleRemoveFails(t *testing.T) {
t.Errorf("expected code to be %v got %v", http.StatusInternalServerError, rr.Status)
}
}

func TestHandleDetailDefaultSuccess(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockLogger := NewMockLoggerInterface(ctrl)
mockService := NewMockServiceInterface(ctrl)

defaultSchemaID := "mock_default"
defaultSchema := new(DefaultSchema)
defaultSchema.ID = defaultSchemaID

req := httptest.NewRequest(http.MethodGet, "/api/v0/schemas/default", nil)

mockService.EXPECT().GetDefaultSchema(gomock.Any()).Return(defaultSchema, nil)

w := httptest.NewRecorder()
mux := chi.NewMux()
NewAPI(mockService, mockLogger).RegisterEndpoints(mux)

mux.ServeHTTP(w, req)

res := w.Result()
defer res.Body.Close()
data, err := io.ReadAll(res.Body)

if err != nil {
t.Errorf("expected error to be nil got %v", err)
}

if res.StatusCode != http.StatusOK {
t.Fatalf("expected HTTP status code 200 got %v", res.StatusCode)
}

type Response struct {
Data *DefaultSchema `json:"data"`
}

rr := new(Response)

if err := json.Unmarshal(data, rr); err != nil {
t.Errorf("expected error to be nil got %v", err)
}

if rr.Data.ID != defaultSchemaID {
t.Fatalf("invalid result, expected default id %s, got: %v", defaultSchemaID, rr.Data.ID)
}
}

func TestHandleDetailDefaultFail(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockLogger := NewMockLoggerInterface(ctrl)
mockService := NewMockServiceInterface(ctrl)

req := httptest.NewRequest(http.MethodGet, "/api/v0/schemas/default", nil)

mockService.EXPECT().GetDefaultSchema(gomock.Any()).Return(nil, fmt.Errorf("mock_error"))

w := httptest.NewRecorder()
mux := chi.NewMux()
NewAPI(mockService, mockLogger).RegisterEndpoints(mux)

mux.ServeHTTP(w, req)

res := w.Result()
defer res.Body.Close()
data, err := io.ReadAll(res.Body)

if err != nil {
t.Errorf("expected error to be nil got %v", err)
}

if res.StatusCode != http.StatusInternalServerError {
t.Fatalf("expected HTTP status code %v got %v", http.StatusInternalServerError, res.StatusCode)
}

type Response struct {
Message string `json:"message"`
}

rr := new(Response)

if err := json.Unmarshal(data, rr); err != nil {
t.Errorf("expected error to be nil got %v", err)
}

if rr.Message != "mock_error" {
t.Fatalf("invalid result, expected error message %s, got: %v", "mock_error", rr.Message)
}
}

func TestHandleUpdateDefaultSuccess(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockLogger := NewMockLoggerInterface(ctrl)
mockService := NewMockServiceInterface(ctrl)

defaultSchemaID := "mock_default"
defaultSchema := new(DefaultSchema)
defaultSchema.ID = defaultSchemaID

payload, _ := json.Marshal(defaultSchema)

req := httptest.NewRequest(http.MethodPut, "/api/v0/schemas/default", bytes.NewReader(payload))

mockService.EXPECT().UpdateDefaultSchema(gomock.Any(), gomock.Any()).Return(defaultSchema, nil)

w := httptest.NewRecorder()
mux := chi.NewMux()
NewAPI(mockService, mockLogger).RegisterEndpoints(mux)

mux.ServeHTTP(w, req)

res := w.Result()
defer res.Body.Close()
data, err := io.ReadAll(res.Body)

if err != nil {
t.Errorf("expected error to be nil got %v", err)
}

if res.StatusCode != http.StatusOK {
t.Fatalf("expected HTTP status code 200 got %v", res.StatusCode)
}

type Response struct {
Data *DefaultSchema `json:"data"`
}

rr := new(Response)

if err := json.Unmarshal(data, rr); err != nil {
t.Errorf("expected error to be nil got %v", err)
}

if rr.Data.ID != defaultSchemaID {
t.Fatalf("invalid result, expected default id %s, got: %v", defaultSchemaID, rr.Data.ID)
}
}

func TestHandleUpdateDefaultBadRequest(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockLogger := NewMockLoggerInterface(ctrl)
mockService := NewMockServiceInterface(ctrl)

req := httptest.NewRequest(http.MethodPut, "/api/v0/schemas/default", strings.NewReader("test"))

w := httptest.NewRecorder()
mux := chi.NewMux()
NewAPI(mockService, mockLogger).RegisterEndpoints(mux)

mux.ServeHTTP(w, req)

res := w.Result()
defer res.Body.Close()
data, err := io.ReadAll(res.Body)

if err != nil {
t.Errorf("expected error to be nil got %v", err)
}

if res.StatusCode != http.StatusBadRequest {
t.Fatalf("expected HTTP status code 400 got %v", res.StatusCode)
}

rr := new(types.Response)
if err := json.Unmarshal(data, rr); err != nil {
t.Errorf("expected error to be nil got %v", err)
}

if rr.Status != http.StatusBadRequest {
t.Errorf("expected code to be %v got %v", http.StatusBadRequest, rr.Status)
}
}

func TestHandleUpdateDefaultFail(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockLogger := NewMockLoggerInterface(ctrl)
mockService := NewMockServiceInterface(ctrl)

defaultSchemaID := "mock_default"
defaultSchema := new(DefaultSchema)
defaultSchema.ID = defaultSchemaID

payload, _ := json.Marshal(defaultSchema)

req := httptest.NewRequest(http.MethodPut, "/api/v0/schemas/default", bytes.NewReader(payload))

mockService.EXPECT().UpdateDefaultSchema(gomock.Any(), gomock.Any()).DoAndReturn(
func(ctx context.Context, schema *DefaultSchema) (*DefaultSchema, error) {

if schema.ID != defaultSchemaID {
t.Fatalf("invalid ID, expected %s got %s", defaultSchemaID, schema.ID)
}

return nil, fmt.Errorf("mock_error")
},
)

w := httptest.NewRecorder()
mux := chi.NewMux()
NewAPI(mockService, mockLogger).RegisterEndpoints(mux)

mux.ServeHTTP(w, req)

res := w.Result()
defer res.Body.Close()
data, err := io.ReadAll(res.Body)

if err != nil {
t.Errorf("expected error to be nil got %v", err)
}

if res.StatusCode != http.StatusInternalServerError {
t.Fatalf("expected HTTP status code 500 got %v", res.StatusCode)
}

rr := new(types.Response)
if err := json.Unmarshal(data, rr); err != nil {
t.Errorf("expected error to be nil got %v", err)
}

if rr.Status != http.StatusInternalServerError {
t.Errorf("expected code to be %v got %v", http.StatusInternalServerError, rr.Status)
}
}
2 changes: 2 additions & 0 deletions pkg/schemas/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ type ServiceInterface interface {
EditSchema(context.Context, string, *kClient.IdentitySchemaContainer) (*IdentitySchemaData, error)
CreateSchema(context.Context, *kClient.IdentitySchemaContainer) (*IdentitySchemaData, error)
DeleteSchema(context.Context, string) error
GetDefaultSchema(context.Context) (*DefaultSchema, error)
UpdateDefaultSchema(context.Context, *DefaultSchema) (*DefaultSchema, error)
}
Loading

0 comments on commit 6968a28

Please sign in to comment.