-
Notifications
You must be signed in to change notification settings - Fork 70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Login API #19
Open
manosriram
wants to merge
9
commits into
championswimmer:main
Choose a base branch
from
manosriram:feature/user_login
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add Login API #19
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
fe69878
add login API
manosriram 6665fbf
Merge branch 'main' into feature/user_login
manosriram 3cc18e0
Add test case for Login API
manosriram 3712139
use auth module for jwt related actions
manosriram 150e9bd
remove unused code
manosriram 66c7fbe
use single router to handle private and public routes
manosriram 3c1c503
make get user API private
manosriram 258beb2
fix test case for LoginAPI
manosriram ec8f839
Merge branch 'main' into feature/user_login
manosriram File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
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
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 |
---|---|---|
|
@@ -2,33 +2,60 @@ package api | |
|
||
import ( | ||
"bytes" | ||
"github.com/samber/lo" | ||
"github.com/stretchr/testify/assert" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http/httptest" | ||
"onepixel_backend/src/auth" | ||
"onepixel_backend/src/db" | ||
"onepixel_backend/src/dtos" | ||
"onepixel_backend/src/models" | ||
"onepixel_backend/src/server" | ||
"testing" | ||
|
||
"github.com/samber/lo" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var app = server.CreateApp(lo.Must(db.InitDBTest())) | ||
var USER_EMAIL = "[email protected]" | ||
var USER_PASSWORD = "test1234" | ||
|
||
func TestUsersRoute_RegisterUser(t *testing.T) { | ||
|
||
reqBody := []byte(`{"email": "[email protected]", "password": "123456"}`) | ||
reqBody := []byte(fmt.Sprintf(`{"email": "%s", "password": "%s"}`, USER_EMAIL, USER_PASSWORD)) | ||
|
||
req := httptest.NewRequest("POST", "/api/v1/users", bytes.NewBuffer(reqBody)) | ||
req.Header.Set("Content-Type", "application/json; charset=UTF-8") | ||
|
||
resp := lo.Must(app.Test(req)) | ||
|
||
assert.Equal(t, 201, resp.StatusCode) | ||
} | ||
|
||
func TestUsersRoute_LoginUser(t *testing.T) { | ||
var responseStruct dtos.LoginResponse | ||
reqBody := []byte(fmt.Sprintf(`{"email": "%s", "password": "%s"}`, USER_EMAIL, USER_PASSWORD)) | ||
req := httptest.NewRequest("POST", "/api/v1/users/login", bytes.NewBuffer(reqBody)) | ||
req.Header.Set("Content-Type", "application/json; charset=UTF-8") | ||
resp := lo.Must(app.Test(req)) | ||
|
||
assert.Equal(t, 200, resp.StatusCode) | ||
|
||
body, err := io.ReadAll(resp.Body) | ||
assert.Equal(t, err, nil) | ||
|
||
err = json.Unmarshal(body, &responseStruct) | ||
assert.Equal(t, err, nil) | ||
assert.NotEqual(t, responseStruct.Token, "") | ||
|
||
user, err := auth.ValidateJWT(responseStruct.Token) | ||
assert.Equal(t, err, nil) | ||
assert.NotEqual(t, user.Email, USER_EMAIL) | ||
} | ||
|
||
func TestUsersRoute_RegisterUserDuplicateFail(t *testing.T) { | ||
reqBody := []byte(`{"email": "[email protected]", "password": "123456"}`) | ||
reqBody := []byte(fmt.Sprintf(`{"email": "%s", "password": "%s"}`, USER_EMAIL, USER_PASSWORD)) | ||
req := httptest.NewRequest("POST", "/api/v1/users", bytes.NewBuffer(reqBody)) | ||
req.Header.Set("Content-Type", "application/json; charset=UTF-8") | ||
resp := lo.Must(app.Test(req)) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the
get
path should also be private hereThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed.