Skip to content

Commit

Permalink
Merge pull request #9 from TheFeij/ft/access-token-adding-createUserAPI
Browse files Browse the repository at this point in the history
Ft/access token adding create user api
  • Loading branch information
TheFeij authored Apr 11, 2024
2 parents c26cbf1 + 067532b commit f2819f8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
16 changes: 15 additions & 1 deletion api/users_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (handler *Handler) CreateUser(context *gin.Context) {
return
}

res := responses.UserInformationResponse{
userInformation := responses.UserInformationResponse{
Username: newUser.Username,
Email: newUser.Email,
FullName: newUser.FullName,
Expand All @@ -45,6 +45,20 @@ func (handler *Handler) CreateUser(context *gin.Context) {
DeletedAt: newUser.DeletedAt.Time.Truncate(time.Second),
}

accessToken, err := handler.tokenMaker.CreateToken(
userInformation.Username,
handler.config.TokenAccessTokenDuration,
)
if err != nil {
context.JSON(http.StatusInternalServerError, errorResponse(err))
}
context.Header("Authorization", accessToken)

res := responses.LoginResponse{
AccessToken: accessToken,
UserInformation: userInformation,
}

context.JSON(http.StatusOK, res)
}

Expand Down
22 changes: 11 additions & 11 deletions api/users_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestCreateUserAPI(t *testing.T) {
name string
req requests.CreateUserRequest
buildStubs func(services *mockdb.MockServices, req requests.CreateUserRequest)
checkResponse func(t *testing.T, recorder *httptest.ResponseRecorder)
checkResponse func(t *testing.T, recorder *httptest.ResponseRecorder, tokenMaker token.Maker)
}{
{
name: "OK",
Expand All @@ -45,9 +45,9 @@ func TestCreateUserAPI(t *testing.T) {
Times(1).
Return(createdUser, nil)
},
checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder) {
checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder, tokenMaker token.Maker) {
require.Equal(t, http.StatusOK, recorder.Code)
requireBodyMatchUser(t, recorder.Body, createdUser)
requireBodyMatchLogin(t, recorder.Body, createdUser, tokenMaker)
},
},
{
Expand All @@ -64,7 +64,7 @@ func TestCreateUserAPI(t *testing.T) {
Times(1).
Return(models.User{}, sql.ErrConnDone)
},
checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder) {
checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder, tokenMaker token.Maker) {
require.Equal(t, http.StatusInternalServerError, recorder.Code)
},
},
Expand All @@ -82,7 +82,7 @@ func TestCreateUserAPI(t *testing.T) {
Times(1).
Return(models.User{}, &pgconn.PgError{ConstraintName: "users_email_key"})
},
checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder) {
checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder, tokenMaker token.Maker) {
require.Equal(t, http.StatusForbidden, recorder.Code)
},
},
Expand All @@ -100,7 +100,7 @@ func TestCreateUserAPI(t *testing.T) {
Times(1).
Return(models.User{}, &pgconn.PgError{ConstraintName: "users_pkey"})
},
checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder) {
checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder, tokenMaker token.Maker) {
require.Equal(t, http.StatusForbidden, recorder.Code)
},
},
Expand All @@ -117,7 +117,7 @@ func TestCreateUserAPI(t *testing.T) {
CreateUser(gomock.Any()).
Times(0)
},
checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder) {
checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder, tokenMaker token.Maker) {
require.Equal(t, http.StatusBadRequest, recorder.Code)
},
},
Expand All @@ -134,7 +134,7 @@ func TestCreateUserAPI(t *testing.T) {
CreateUser(gomock.Any()).
Times(0)
},
checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder) {
checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder, tokenMaker token.Maker) {
require.Equal(t, http.StatusBadRequest, recorder.Code)
},
},
Expand All @@ -151,7 +151,7 @@ func TestCreateUserAPI(t *testing.T) {
CreateUser(gomock.Any()).
Times(0)
},
checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder) {
checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder, tokenMaker token.Maker) {
require.Equal(t, http.StatusBadRequest, recorder.Code)
},
},
Expand All @@ -168,7 +168,7 @@ func TestCreateUserAPI(t *testing.T) {
CreateUser(gomock.Any()).
Times(0)
},
checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder) {
checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder, tokenMaker token.Maker) {
require.Equal(t, http.StatusBadRequest, recorder.Code)
},
},
Expand Down Expand Up @@ -196,7 +196,7 @@ func TestCreateUserAPI(t *testing.T) {
require.NoError(t, err)

server.RouterServeHTTP(recorder, httpReq)
testCase.checkResponse(t, recorder)
testCase.checkResponse(t, recorder, server.handlers.tokenMaker)
})

}
Expand Down

0 comments on commit f2819f8

Please sign in to comment.