Skip to content

Commit

Permalink
refactoring of user service test
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinGallauner committed Jul 16, 2024
1 parent 8bac5e6 commit 84f6849
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions internal/users/user_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func TestCreateNewUser(t *testing.T) {
assert.Equal(t, got.Email, "[email protected]")
assert.Equal(t, got.ID, uint(1))

AssertStatus(t, response.Code, http.StatusOK)
assertStatus(t, response.Code, http.StatusOK)
}

// Tests to create link request
Expand Down Expand Up @@ -140,7 +140,7 @@ func TestRequestLink(t *testing.T) {
if err != nil {
t.Fatalf("Unable to parse response from server %q, '%v'", response.Body, err)
}
test.AssertStatus(t, response.Code, http.StatusOK)
assertStatus(t, response.Code, http.StatusOK)
assert.Equal(t, got.SenderId, user1.ID)
assert.Equal(t, got.ReceiverId, user2.ID)
savedLink, err := s.LinkRepository.Get(user1.ID, user2.ID)
Expand Down Expand Up @@ -185,7 +185,7 @@ func TestGetLinks(t *testing.T) {
t.Errorf("User 1 is not supposed to have any link to user 3")
}
}
test.AssertStatus(t, response.Code, http.StatusOK)
assertStatus(t, response.Code, http.StatusOK)
}

// Tests to accept an existing link request
Expand Down Expand Up @@ -221,7 +221,7 @@ func TestAcceptLink(t *testing.T) {
if err != nil {
t.Fatalf("Unable to parse response from server %q, '%v'", response.Body, err)
}
test.AssertStatus(t, response.Code, http.StatusOK)
assertStatus(t, response.Code, http.StatusOK)

fetchLink, _ := s.LinkRepository.Get(user1.ID, user2.ID)

Expand Down Expand Up @@ -257,7 +257,7 @@ func TestLogin(t *testing.T) {
if err != nil {
t.Fatalf("Unable to parse response from server %q, '%v'", response.Body, err)
}
test.AssertStatus(t, response.Code, http.StatusOK)
assertStatus(t, response.Code, http.StatusOK)

//persisted
persistedUser, err := s.UserRepository.GetByEmail(testUser.Email)
Expand Down Expand Up @@ -289,7 +289,7 @@ func TestLoginOfNewUser(t *testing.T) {
if err != nil {
t.Fatalf("Unable to parse response from server %q, '%v'", response.Body, err)
}
test.AssertStatus(t, response.Code, http.StatusOK)
assertStatus(t, response.Code, http.StatusOK)

//persisted
persistedUser, err := s.UserRepository.GetByEmail("[email protected]")
Expand All @@ -301,3 +301,17 @@ func TestLoginOfNewUser(t *testing.T) {
assert.Equal(t, got.Email, "[email protected]")
assert.Equal(t, got.Jwt, "mock token")
}

func assertResponseBody(t testing.TB, got, want string) {
t.Helper()
if got != want {
t.Errorf("response body is wrong, got %q want %q", got, want)
}
}

func assertStatus(t testing.TB, got, want int) { //TODO: consider removing that helper and assert inline
t.Helper()
if got != want {
t.Errorf("did not get correct status, got %d, want %d", got, want)
}
}

0 comments on commit 84f6849

Please sign in to comment.