Skip to content

Commit

Permalink
Write integration test for the POST /users endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
claudealdric committed Sep 28, 2024
1 parent 4d24658 commit 720987a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
36 changes: 36 additions & 0 deletions main_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,24 @@ func TestServerWithSqliteStore(t *testing.T) {
fmt.Println("tasks", tasks)
assert.DoesNotContain(t, tasks, *unwantedTask)
})

t.Run("users", func(t *testing.T) {
createUserDTO := models.NewCreateUserDTO(
"Sherlock",
"[email protected]",
"sherlocked",
)
postUserResponse, err := sendPostUser(server, createUserDTO)
assert.HasNoError(t, err)
createdUser := testutils.GetUserFromResponse(t, postUserResponse.Body)
wantedUser := models.NewUser(
createdUser.Id,
createUserDTO.Name,
createdUser.Email,
createUserDTO.Password,
)
assert.Equals(t, createdUser, *wantedUser)
})
}

func TestServerWithFileSystemStore(t *testing.T) {
Expand Down Expand Up @@ -233,6 +251,24 @@ func sendPostTask(server *api.Server, dto *models.CreateTaskDTO) (
return response, nil
}

func sendPostUser(server *api.Server, dto *models.CreateUserDTO) (
*httptest.ResponseRecorder,
error,
) {
jsonBody, err := json.Marshal(dto)
if err != nil {
return nil, err
}
request := httptest.NewRequest(
http.MethodPost,
"/users",
bytes.NewBuffer(jsonBody),
)
response := httptest.NewRecorder()
server.ServeHTTP(response, request)
return response, nil
}

func cleanSqliteDatabase(path string) {
os.Remove(path)
}
9 changes: 9 additions & 0 deletions models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,12 @@ type CreateUserDTO struct {
Email string `json:"email"`
Password string `json:"password"`
}

func NewCreateUserDTO(
name string,
email string,
password string,
) *CreateUserDTO {
dto := CreateUserDTO{Name: name, Email: email, Password: password}
return &dto
}

0 comments on commit 720987a

Please sign in to comment.