Skip to content

Commit

Permalink
add more http tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adelowo committed Jan 29, 2025
1 parent a6c37df commit 60e8838
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"message":"could not fetch preferences"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"message":"could not update preferences"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"preferences":{"id":"00000000-0000-0000-0000-000000000000","workspace_id":"00000000-0000-0000-0000-000000000000","communication":{},"billing":{},"created_at":"0001-01-01T00:00:00Z","updated_at":"0001-01-01T00:00:00Z"},"message":"workspace preferences updated"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"preferences":{"id":"00000000-0000-0000-0000-000000000000","workspace_id":"00000000-0000-0000-0000-000000000000","communication":{"enable_marketing":true,"enable_product_updates":true},"billing":{},"created_at":"0001-01-01T00:00:00Z","updated_at":"0001-01-01T00:00:00Z"},"message":"workspace preferences updated"}
2 changes: 1 addition & 1 deletion server/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ func (wo *workspaceHandler) updatePreferences(
preferences, err := wo.preferenceRepo.Get(ctx, workspace)
if err != nil {
logger.Error("could not fetch preferences", zap.Error(err))
return newAPIStatus(http.StatusBadRequest, "could not fetch preferences"), StatusFailed
return newAPIStatus(http.StatusInternalServerError, "could not fetch preferences"), StatusFailed
}

pref := req.Make(preferences)
Expand Down
117 changes: 117 additions & 0 deletions server/workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,123 @@ func TestWorkspaceHandler_GetPreferences(t *testing.T) {
}
}

func TestWorkspaceHandler_UpdatePreferences(t *testing.T) {
for _, v := range generateWorkspacePreferencesUpdateTestTable() {

t.Run(v.name, func(t *testing.T) {

controller := gomock.NewController(t)
defer controller.Finish()

prefRepo := malak_mocks.NewMockPreferenceRepository(controller)

v.mockFn(prefRepo)

a := &workspaceHandler{
cfg: getConfig(),
preferenceRepo: prefRepo,
referenceGenerationFunc: func(e malak.EntityType) string {
return "workspace_tt7-YieIgz"
},
}

var b = bytes.NewBuffer(nil)

require.NoError(t, json.NewEncoder(b).Encode(&v.req))

rr := httptest.NewRecorder()

req := httptest.NewRequest(http.MethodPost, "/", b)
req.Header.Add("Content-Type", "application/json")

req = req.WithContext(writeUserToCtx(req.Context(), &malak.User{}))
req = req.WithContext(writeWorkspaceToCtx(req.Context(), &malak.Workspace{}))

WrapMalakHTTPHandler(getLogger(t),
a.updatePreferences, getConfig(), "workspaces.update").
ServeHTTP(rr, req)

require.Equal(t, v.expectedStatusCode, rr.Code)
verifyMatch(t, rr)
})
}
}

func generateWorkspacePreferencesUpdateTestTable() []struct {
name string
mockFn func(preferencesRepo *malak_mocks.MockPreferenceRepository)
expectedStatusCode int
req updatePreferencesRequest
} {

return []struct {
name string
mockFn func(preferencesRepo *malak_mocks.MockPreferenceRepository)
expectedStatusCode int
req updatePreferencesRequest
}{
{
name: "could not fetch workspace preferences",
mockFn: func(preferencesRepo *malak_mocks.MockPreferenceRepository) {
preferencesRepo.EXPECT().Get(gomock.Any(), gomock.Any()).
Times(1).
Return(nil, errors.New("could not fetch preferences"))
},
expectedStatusCode: http.StatusInternalServerError,
},
{
name: "update fails",
mockFn: func(preferencesRepo *malak_mocks.MockPreferenceRepository) {
preferencesRepo.EXPECT().Get(gomock.Any(), gomock.Any()).
Times(1).
Return(&malak.Preference{}, nil)

preferencesRepo.EXPECT().Update(gomock.Any(), gomock.Any()).
Times(1).
Return(errors.New("updating prefernces failed"))
},
expectedStatusCode: http.StatusInternalServerError,
},
{
name: "update succeeds",
mockFn: func(preferencesRepo *malak_mocks.MockPreferenceRepository) {
preferencesRepo.EXPECT().Get(gomock.Any(), gomock.Any()).
Times(1).
Return(&malak.Preference{}, nil)

preferencesRepo.EXPECT().Update(gomock.Any(), gomock.Any()).
Times(1).
Return(nil)
},
expectedStatusCode: http.StatusOK,
},
{
name: "update succeeds with request data",
mockFn: func(preferencesRepo *malak_mocks.MockPreferenceRepository) {
preferencesRepo.EXPECT().Get(gomock.Any(), gomock.Any()).
Times(1).
Return(&malak.Preference{}, nil)

preferencesRepo.EXPECT().Update(gomock.Any(), gomock.Any()).
Times(1).
Return(nil)
},
expectedStatusCode: http.StatusOK,
req: updatePreferencesRequest{
Preferences: struct {
Billing malak.BillingPreferences "json:\"billing,omitempty\" validate:\"required\""
Newsletter malak.CommunicationPreferences "json:\"newsletter,omitempty\" validate:\"required\""
}{
Newsletter: malak.CommunicationPreferences{
EnableMarketing: true,
EnableProductUpdates: true,
},
},
},
},
}
}

func generateWorkspacePreferencesTestTable() []struct {
name string
mockFn func(preferencesRepo *malak_mocks.MockPreferenceRepository)
Expand Down

0 comments on commit 60e8838

Please sign in to comment.