Skip to content

Commit

Permalink
Respond with a 500 error when an unknown store creation failure occurs
Browse files Browse the repository at this point in the history
  • Loading branch information
claudealdric committed Sep 16, 2024
1 parent d50219b commit 04f1c33
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
6 changes: 5 additions & 1 deletion api/post_user_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ func (s *Server) HandlePostUser(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
user, _ := s.store.CreateUser(dto)
user, err := s.store.CreateUser(dto)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(user)
}
40 changes: 22 additions & 18 deletions api/post_user_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,26 @@ func TestHandlePostUser(t *testing.T) {
assert.Calls(t, data.CreateUserCalls, 0)
})

// t.Run("responds with a 500 error when the store user creation fails", func(t *testing.T) {
// data := testutils.NewMockStore(true)
// server := NewServer(data)
//
// newUser := models.NewUser(2, "Exercise")
// jsonData, err := json.Marshal(newUser)
// assert.HasNoError(t, err)
// request := httptest.NewRequest(
// http.MethodPost,
// "/users",
// bytes.NewBuffer(jsonData),
// )
// response := httptest.NewRecorder()
// server.Handler.ServeHTTP(response, request)
//
// assert.Status(t, response.Code, http.StatusInternalServerError)
// assert.Calls(t, data.CreateUserCalls, 1)
// })
t.Run("responds with a 500 error when the store user creation fails", func(t *testing.T) {
data := testutils.NewMockStore(true)
server := NewServer(data)

dto := models.CreateUserDTO{
Name: "Claude Aldric",
Email: "[email protected]",
Password: "password",
}
jsonData, err := json.Marshal(dto)
assert.HasNoError(t, err)
request := httptest.NewRequest(
http.MethodPost,
"/users",
bytes.NewBuffer(jsonData),
)
response := httptest.NewRecorder()
server.Handler.ServeHTTP(response, request)

assert.Status(t, response.Code, http.StatusInternalServerError)
assert.Calls(t, data.CreateUserCalls, 1)
})
}

0 comments on commit 04f1c33

Please sign in to comment.