-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
⚡️ Implemented /auth/signup/v1 endpoint
- Loading branch information
1 parent
69b4062
commit 4828186
Showing
5 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package auth | ||
|
||
import ( | ||
"ahbcc/internal/log" | ||
"encoding/json" | ||
"net/http" | ||
|
||
"ahbcc/cmd/api/users" | ||
) | ||
|
||
// SignUpHandlerV1 HTTP Handler of the endpoint /auth/signup | ||
func SignUpHandlerV1(signUp SignUp) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
ctx := r.Context() | ||
|
||
var user users.UserDTO | ||
err := json.NewDecoder(r.Body).Decode(&user) | ||
if err != nil { | ||
log.Error(ctx, err.Error()) | ||
http.Error(w, InvalidRequestBody, http.StatusBadRequest) | ||
return | ||
} | ||
ctx = log.With(ctx, log.Param("username", user.Username)) | ||
|
||
err = validateBody(user) | ||
if err != nil { | ||
log.Error(ctx, err.Error()) | ||
http.Error(w, InvalidRequestBody, http.StatusBadRequest) | ||
} | ||
|
||
err = signUp(ctx, user) | ||
if err != nil { | ||
log.Error(ctx, err.Error()) | ||
http.Error(w, FailedToSignUp, http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
w.WriteHeader(http.StatusOK) | ||
_, _ = w.Write([]byte("User successfully signed up")) | ||
} | ||
} | ||
|
||
// validateBody validates that mandatory fields are present | ||
func validateBody(user users.UserDTO) error { | ||
if user.Username == "" { | ||
return MissingUsername | ||
} | ||
|
||
if user.Password == "" { | ||
return MissingPassword | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package auth_test | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"ahbcc/cmd/api/auth" | ||
"ahbcc/cmd/api/users" | ||
) | ||
|
||
func TestSignUpHandlerV1_success(t *testing.T) { | ||
mockSignUp := auth.MockSignUp(nil) | ||
mockResponseWriter := httptest.NewRecorder() | ||
mockUser := users.MockUserDTO() | ||
mockBody, _ := json.Marshal(mockUser) | ||
mockRequest, _ := http.NewRequestWithContext(context.Background(), http.MethodPost, "/auth/signup/v1", bytes.NewReader(mockBody)) | ||
|
||
signUpHandlerV1 := auth.SignUpHandlerV1(mockSignUp) | ||
|
||
signUpHandlerV1(mockResponseWriter, mockRequest) | ||
|
||
want := http.StatusOK | ||
got := mockResponseWriter.Result().StatusCode | ||
|
||
assert.Equal(t, want, got) | ||
} | ||
|
||
func TestSignUpHandlerV1_failsWhenTheBodyCantBeParsed(t *testing.T) { | ||
mockSignUp := auth.MockSignUp(nil) | ||
mockResponseWriter := httptest.NewRecorder() | ||
mockBody, _ := json.Marshal(`{"wrong": "body"}`) | ||
mockRequest, _ := http.NewRequestWithContext(context.Background(), http.MethodPost, "/auth/signup/v1", bytes.NewReader(mockBody)) | ||
|
||
signUpHandlerV1 := auth.SignUpHandlerV1(mockSignUp) | ||
|
||
signUpHandlerV1(mockResponseWriter, mockRequest) | ||
|
||
want := http.StatusBadRequest | ||
got := mockResponseWriter.Result().StatusCode | ||
|
||
assert.Equal(t, want, got) | ||
} | ||
|
||
func TestSignUpHandlerV1_failsWhenValidateBodyThrowsError(t *testing.T) { | ||
mockSignUp := auth.MockSignUp(nil) | ||
mockResponseWriter := httptest.NewRecorder() | ||
|
||
for _, test := range []struct { | ||
mockUser users.UserDTO | ||
}{ | ||
{mockUser: users.UserDTO{Username: "username"}}, | ||
{mockUser: users.UserDTO{Password: "password"}}, | ||
} { | ||
mockBody, _ := json.Marshal(test.mockUser) | ||
mockRequest, _ := http.NewRequestWithContext(context.Background(), http.MethodPost, "/auth/signup/v1", bytes.NewReader(mockBody)) | ||
|
||
signUpHandlerV1 := auth.SignUpHandlerV1(mockSignUp) | ||
|
||
signUpHandlerV1(mockResponseWriter, mockRequest) | ||
|
||
want := http.StatusBadRequest | ||
got := mockResponseWriter.Result().StatusCode | ||
|
||
assert.Equal(t, want, got) | ||
} | ||
} | ||
|
||
func TestSignUpHandlerV1_failsWhenSignUpThrowsError(t *testing.T) { | ||
mockSignUp := auth.MockSignUp(errors.New("failed to sign up")) | ||
mockResponseWriter := httptest.NewRecorder() | ||
mockUser := users.MockUserDTO() | ||
mockBody, _ := json.Marshal(mockUser) | ||
mockRequest, _ := http.NewRequestWithContext(context.Background(), http.MethodPost, "/auth/signup/v1", bytes.NewReader(mockBody)) | ||
|
||
signUpHandlerV1 := auth.SignUpHandlerV1(mockSignUp) | ||
|
||
signUpHandlerV1(mockResponseWriter, mockRequest) | ||
|
||
want := http.StatusInternalServerError | ||
got := mockResponseWriter.Result().StatusCode | ||
|
||
assert.Equal(t, want, got) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package auth | ||
|
||
import ( | ||
"context" | ||
|
||
"ahbcc/cmd/api/users" | ||
) | ||
|
||
// MockSignUp mocks SignUp function | ||
func MockSignUp(err error) SignUp { | ||
return func(ctx context.Context, user users.UserDTO) error { | ||
return err | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters