Skip to content

Commit

Permalink
feat(user-auth): adding tests for user creation and login (#16)
Browse files Browse the repository at this point in the history
* feat(user-auth): test error with incorrect json body to user creation endpoint

* feat(user-auth): adding tests for bad email address supplied and invalid json body

* feat(user-auth): fmt

* feat(user-login): add test to ensure duplicate users can not be created

* feat(user-login): test login fails with incorrect parameters

* feat(user-login): test login with invalid user

* feat(user-login): fmt

* feat(user-login): test user login endpoint returns an ID, email, access and refresh token
  • Loading branch information
logan-bobo authored Jul 30, 2024
1 parent 75349a3 commit 9200229
Show file tree
Hide file tree
Showing 4 changed files with 258 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ stop:

test:
COMPOSE_FILE=${COMPOSE_TEST_FILE} docker compose up -d
COMPOSE_FILE=${COMPOSE_TEST_FILE} docker compose run --rm --remove-orphans api-test go test ./...
COMPOSE_FILE=${COMPOSE_TEST_FILE} docker compose run --rm --remove-orphans api-test go test -cover ./...
COMPOSE_FILE=${COMPOSE_TEST_FILE} docker compose down
.PHONY:test

Expand Down
11 changes: 10 additions & 1 deletion handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,11 @@ func (apiCfg *apiConfig) postAPIUsers(w http.ResponseWriter, r *http.Request) {

if err != nil {
log.Println(err)
respondWithError(w, http.StatusBadRequest, "could not parse request")
return
}

if payload.Email == "" || payload.Password == "" {
respondWithError(w, http.StatusBadRequest, "incorrect parameters for user creation")
return
}
Expand Down Expand Up @@ -294,10 +299,14 @@ func (apiCfg *apiConfig) postAPILogin(w http.ResponseWriter, r *http.Request) {
err := json.NewDecoder(r.Body).Decode(&payload)

if err != nil {
respondWithError(w, http.StatusBadRequest, "invalid parameters for login")
respondWithError(w, http.StatusBadRequest, "could not parse request")
return
}

if payload.Email == "" || payload.Password == "" {
respondWithError(w, http.StatusBadRequest, "invalid parameters for user login")
}

user, err := apiCfg.DB.SelectUser(r.Context(), payload.Email)

if err == sql.ErrNoRows {
Expand Down
243 changes: 242 additions & 1 deletion handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ func TestPostUser(t *testing.T) {
t.Errorf("can not open database connection")
}

defer db.Close()

err = resetDB(db)

if err != nil {
Expand All @@ -87,7 +89,7 @@ func TestPostUser(t *testing.T) {

dbQueries := database.New(db)

t.Run("test user creation", func(t *testing.T) {
t.Run("test user creation passes with correct parameters", func(t *testing.T) {
requestJSON := []byte(`{"email": "[email protected]", "password": "test"}`)
request, _ := http.NewRequest(http.MethodPost, "/api/v1/users", bytes.NewBuffer(requestJSON))
request.Header.Set("Content-Type", "application/json")
Expand All @@ -111,4 +113,243 @@ func TestPostUser(t *testing.T) {
t.Errorf("unexpected email in response, got %q, wanted %q", got.Email, "[email protected]")
}
})

t.Run("test user creation with bad parameters", func(t *testing.T) {
requestJSON := []byte(`{"gmail":"[email protected]", "auth": "test", "extra_data": "data"}`)
request, _ := http.NewRequest(http.MethodPost, "/api/v1/users", bytes.NewBuffer(requestJSON))
request.Header.Set("Content-Type", "application/json")

response := httptest.NewRecorder()

apiCfg := apiConfig{
DB: dbQueries,
}

apiCfg.postAPIUsers(response, request)

got := errorResponse{}

err := json.NewDecoder(response.Body).Decode(&got)

if err != nil {
t.Errorf("unable to parse response %q into %q", response.Body, got)
}

want := "incorrect parameters for user creation"
if got.Error != want {
t.Errorf("incorrect error when invalid json used got %q wanted %q", got.Error, want)
}
})

t.Run("test user creation with no body", func(t *testing.T) {
requestJSON := []byte(``)
request, _ := http.NewRequest(http.MethodPost, "/api/v1/users", bytes.NewBuffer(requestJSON))
request.Header.Set("Content-Type", "application/json")

response := httptest.NewRecorder()

apiCfg := apiConfig{
DB: dbQueries,
}

apiCfg.postAPIUsers(response, request)

got := errorResponse{}

err := json.NewDecoder(response.Body).Decode(&got)

if err != nil {
t.Errorf("unable to parse response %q into %q", response.Body, got)
}

want := "could not parse request"
if got.Error != want {
t.Errorf("incorrect error when invalid json used got %q wanted %q", got.Error, want)
}

})

t.Run("test user creation with bad email address", func(t *testing.T) {
requestJSON := []byte(`{"email": "test1mail.com", "password": "test"}`)
request, _ := http.NewRequest(http.MethodPost, "/api/v1/users", bytes.NewBuffer(requestJSON))
request.Header.Set("Content-Type", "application/json")

response := httptest.NewRecorder()

apiCfg := apiConfig{
DB: dbQueries,
}

apiCfg.postAPIUsers(response, request)

got := errorResponse{}

err := json.NewDecoder(response.Body).Decode(&got)

if err != nil {
t.Errorf("unable to parse response %q into %q", response.Body, got)
}

want := "invalid email address"
if got.Error != want {
t.Errorf("incorrect error when passing invalid email address %q wanted %q", got.Error, want)
}
})

t.Run("test a duplicate user can not be created", func(t *testing.T) {
requestJSON := []byte(`{"email": "[email protected]", "password": "test"}`)
request, _ := http.NewRequest(http.MethodPost, "/api/v1/users", bytes.NewBuffer(requestJSON))
request.Header.Set("Content-Type", "application/json")

response := httptest.NewRecorder()

apiCfg := apiConfig{
DB: dbQueries,
}

apiCfg.postAPIUsers(response, request)

got := errorResponse{}
err := json.NewDecoder(response.Body).Decode(&got)

if err != nil {
t.Errorf("unable to parse response %q into %q", response.Body, got)
}

want := "could not create user in database"
if got.Error != want {
t.Errorf("expected duplicate user to fail got %q wanted %q", got.Error, want)
}

})
}

func TestPostLogin(t *testing.T) {
dbURL := os.Getenv("PG_CONN")
db, err := sql.Open("postgres", dbURL)

if err != nil {
t.Errorf("can not open database connection")
}

err = resetDB(db)

if err != nil {
t.Errorf("could not resetDB %q", err)
}

dbQueries := database.New(db)

// setup a user to user for this test case
requestJSON := []byte(`{"email": "[email protected]", "password": "test"}`)
request, _ := http.NewRequest(http.MethodPost, "/api/v1/users", bytes.NewBuffer(requestJSON))
request.Header.Set("Content-Type", "application/json")

apiCfg := apiConfig{
DB: dbQueries,
}

response := httptest.NewRecorder()

apiCfg.postAPIUsers(response, request)

got := APIUsersResponse{}

err = json.NewDecoder(response.Body).Decode(&got)

if err != nil {
t.Errorf("could not setup user for this test case %q", err)
}

t.Run("test user login fails with incorrect payload", func(t *testing.T) {
requestJSON := []byte(`{"email": "[email protected]", "invalid": "test"}`)
request, _ := http.NewRequest(http.MethodPost, "/api/v1/login", bytes.NewBuffer(requestJSON))
request.Header.Set("Content-Type", "application/json")

response := httptest.NewRecorder()

apiCfg.postAPILogin(response, request)

got := errorResponse{}

err := json.NewDecoder(response.Body).Decode(&got)

if err != nil {
t.Errorf("could not parse response %q", err)
}

want := "invalid parameters for user login"
if got.Error != want {
t.Errorf("incorrect error when passing invalid login parameters got %q want %q", got.Error, want)
}
})

t.Run("test user login fails when user can not be found", func(t *testing.T) {
requestJSON := []byte(`{"email": "[email protected]", "password": "test"}`)
request, _ := http.NewRequest(http.MethodPost, "/api/v1/login", bytes.NewBuffer(requestJSON))
request.Header.Set("Content-Type", "application/json")

response := httptest.NewRecorder()

apiCfg.postAPILogin(response, request)

got := errorResponse{}

err := json.NewDecoder(response.Body).Decode(&got)

if err != nil {
t.Errorf("could not parse response %q", err)
}

want := "could not find user"
if got.Error != want {
t.Errorf("incorrect error when non existent user attempts to login got %q want %q", got.Error, want)
}
})

t.Run("test user login fails with invalid password", func(t *testing.T) {
requestJSON := []byte(`{"email": "[email protected]", "password": "testerrrrr"}`)
request, _ := http.NewRequest(http.MethodPost, "/api/v1/login", bytes.NewBuffer(requestJSON))
request.Header.Set("Content-Type", "application/json")

response := httptest.NewRecorder()

apiCfg.postAPILogin(response, request)

got := errorResponse{}

err := json.NewDecoder(response.Body).Decode(&got)

if err != nil {
t.Errorf("could not parse response %q", err)
}

want := "invalid password"
if got.Error != want {
t.Errorf("incorrect error when incorrect password is supplied got %q want %q", got.Error, want)
}
})

t.Run("test user is returned correct ID, Email, Token and a Refresh Token", func(t *testing.T) {
requestJSON := []byte(`{"email": "[email protected]", "password": "test"}`)
request, _ := http.NewRequest(http.MethodPost, "/api/v1/login", bytes.NewBuffer(requestJSON))
request.Header.Set("Content-Type", "application/json")

response := httptest.NewRecorder()

apiCfg.postAPILogin(response, request)

got := APIUsersResponse{}

err := json.NewDecoder(response.Body).Decode(&got)

if err != nil {
t.Errorf("could not parse response %q", err)
}

if got.ID != 1 || got.Email != "[email protected]" || got.Token == "" || got.RefreshToken == "" {
t.Errorf("user login does not return expected results got %q", got)
}
})

}
8 changes: 5 additions & 3 deletions httphelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import (
"net/http"
)

type errorResponse struct {
Error string `json:"error"`
}

func respondWithJSON(w http.ResponseWriter, status int, payload interface{}) {
data, err := json.Marshal(payload)

Expand All @@ -24,9 +28,7 @@ func respondWithJSON(w http.ResponseWriter, status int, payload interface{}) {
}

func respondWithError(w http.ResponseWriter, code int, msg string) {
errorResponse := struct {
Error string `json:"error"`
}{
errorResponse := errorResponse{
Error: msg,
}

Expand Down

0 comments on commit 9200229

Please sign in to comment.