Skip to content

Commit

Permalink
test: put user endpoint changes the users password (#19)
Browse files Browse the repository at this point in the history
* test: put user endpoint changes the users password

* test: fmt
  • Loading branch information
logan-bobo authored Aug 5, 2024
1 parent 3a462b2 commit a9b1b00
Showing 1 changed file with 92 additions and 17 deletions.
109 changes: 92 additions & 17 deletions handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"testing"

"github.com/pressly/goose/v3"
"golang.org/x/crypto/bcrypt"

"url-short/internal/database"
)
Expand Down Expand Up @@ -254,14 +255,6 @@ func TestPostLogin(t *testing.T) {

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))
Expand Down Expand Up @@ -370,7 +363,7 @@ func TestRefreshEndpoint(t *testing.T) {

dbQueries := database.New(db)

// setup a user to user for this test case
// setup a user
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 @@ -383,14 +376,6 @@ func TestRefreshEndpoint(t *testing.T) {

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 valid user can get a new access token based on a valid refresh token", func(t *testing.T) {
// make a request to the login endpoint to be given our token data, refresh and access
loginRequestJSON := []byte(`{"email": "[email protected]", "password": "test"}`)
Expand Down Expand Up @@ -432,3 +417,93 @@ func TestRefreshEndpoint(t *testing.T) {
}
})
}

func TestPutUser(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
createUserRequestJSON := []byte(`{"email": "[email protected]", "password": "test"}`)
createUserRequest, _ := http.NewRequest(http.MethodPost, "/api/v1/users", bytes.NewBuffer(createUserRequestJSON))
createUserRequest.Header.Set("Content-Type", "application/json")

apiCfg := apiConfig{
DB: dbQueries,
}

createUserResponse := httptest.NewRecorder()

apiCfg.postAPIUsers(createUserResponse, createUserRequest)

// login user endpoint
loginRequestJSON := []byte(`{"email": "[email protected]", "password": "test"}`)
loginRequest, _ := http.NewRequest(http.MethodPost, "/api/v1/login", bytes.NewBuffer(loginRequestJSON))
loginRequest.Header.Set("Content-Type", "application/json")

loginResponse := httptest.NewRecorder()

apiCfg.postAPILogin(loginResponse, loginRequest)

loginGot := APIUsersResponse{}

err = json.NewDecoder(loginResponse.Body).Decode(&loginGot)

if err != nil {
t.Errorf("could not parse login request")
}

t.Run("test user can be updated via the put user endpoint", func(t *testing.T) {
putUserRequestJSON := []byte(`{"email": "[email protected]", "password":"new-password"}`)

putUserRequest, _ := http.NewRequest(http.MethodPut, "/api/v1/users", bytes.NewBuffer(putUserRequestJSON))

buildHeader := fmt.Sprintf("Bearer %s", loginGot.RefreshToken)
putUserRequest.Header.Set("Authorization", buildHeader)

putUserResponse := httptest.NewRecorder()

user, err := dbQueries.SelectUser(putUserRequest.Context(), "[email protected]")

if err != nil {
t.Error("could not find user that was expected to exist")
}

apiCfg.putAPIUsers(putUserResponse, putUserRequest, user)

gotPUTUser := APIUserResponseNoToken{}

err = json.NewDecoder(putUserResponse.Body).Decode(&gotPUTUser)

if err != nil {
t.Error("coult not parse response")
}

if gotPUTUser.Email == "" || gotPUTUser.ID == 0 {
t.Errorf("did not get expected email and ID on post user request")
}

userPostUpdate, err := dbQueries.SelectUser(putUserRequest.Context(), "[email protected]")

if err != nil {
t.Error("could not get user post password change")
}

err = bcrypt.CompareHashAndPassword([]byte(userPostUpdate.Password), []byte("new-password"))

if err != nil {
t.Errorf("hashed password did not match new password got error %q", err)
}
})
}

0 comments on commit a9b1b00

Please sign in to comment.