Skip to content

Commit

Permalink
✨ add login tests
Browse files Browse the repository at this point in the history
  • Loading branch information
codermuss committed Jul 28, 2024
1 parent 7c7d360 commit 4df1100
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 2 deletions.
139 changes: 139 additions & 0 deletions api/login_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package api

import (
"bytes"
"database/sql"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"

"github.com/gin-gonic/gin"
"github.com/golang/mock/gomock"
mockdb "github.com/mustafayilmazdev/musarchive/db/mock"
db "github.com/mustafayilmazdev/musarchive/db/sqlc"
"github.com/stretchr/testify/require"
)

// Test case for creating a user via the API
func TestLoginUserAPI(t *testing.T) {
user, password := RandomUser(t)

testCases := []struct {
name string
body gin.H
buildStubs func(store *mockdb.MockStore)
checkResponse func(recoder *httptest.ResponseRecorder)
}{
{
name: "OK",
body: gin.H{
"username": user.Username,
"password": password,
},
buildStubs: func(store *mockdb.MockStore) {
store.EXPECT().
GetUser(gomock.Any(), gomock.Eq(user.Username)).
Times(1).
Return(user, nil)
store.EXPECT().
InsertSession(gomock.Any(), gomock.Any()).
Times(1)
},
checkResponse: func(recorder *httptest.ResponseRecorder) {
require.Equal(t, http.StatusOK, recorder.Code)
},
},
{
name: "UserNotFound",
body: gin.H{
"username": "NotFound",
"password": password,
},
buildStubs: func(store *mockdb.MockStore) {
store.EXPECT().
GetUser(gomock.Any(), gomock.Any()).
Times(1).
//TODO [codermuss]: Will be change with db.ErrorRecordNotFound
Return(db.User{}, fmt.Errorf("Not Found"))
},
checkResponse: func(recorder *httptest.ResponseRecorder) {
require.Equal(t, http.StatusInternalServerError, recorder.Code)
},
},
{
name: "IncorrectPassword",
body: gin.H{
"username": user.Username,
"password": "incorrect",
},
buildStubs: func(store *mockdb.MockStore) {
store.EXPECT().
GetUser(gomock.Any(), gomock.Eq(user.Username)).
Times(1).
Return(user, nil)
},
checkResponse: func(recorder *httptest.ResponseRecorder) {
require.Equal(t, http.StatusUnauthorized, recorder.Code)
},
},
{
name: "InternalError",
body: gin.H{
"username": user.Username,
"password": password,
},
buildStubs: func(store *mockdb.MockStore) {
store.EXPECT().
GetUser(gomock.Any(), gomock.Any()).
Times(1).
Return(db.User{}, sql.ErrConnDone)
},
checkResponse: func(recorder *httptest.ResponseRecorder) {
require.Equal(t, http.StatusInternalServerError, recorder.Code)
},
},
{
name: "InvalidUsername",
body: gin.H{
"username": "invalid-user#1",
"password": password,
},
buildStubs: func(store *mockdb.MockStore) {
store.EXPECT().
GetUser(gomock.Any(), gomock.Any()).
Times(0)
},
checkResponse: func(recorder *httptest.ResponseRecorder) {
require.Equal(t, http.StatusBadRequest, recorder.Code)
},
},
}

for i := range testCases {
tc := testCases[i]

t.Run(tc.name, func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

store := mockdb.NewMockStore(ctrl)
tc.buildStubs(store)

server := newTestServer(t, store)
recorder := httptest.NewRecorder()

// Marshal body data to JSON
data, err := json.Marshal(tc.body)
require.NoError(t, err)

url := "/v1/login"
request, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(data))
require.NoError(t, err)

server.Router.ServeHTTP(recorder, request)
tc.checkResponse(recorder)
})
}
}
4 changes: 2 additions & 2 deletions api/register_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func EqCreateUserParams(arg db.InsertUserParams, password string) gomock.Matcher

// Test case for creating a user via the API
func TestCreateUserAPI(t *testing.T) {
user, password := randomUser(t)
user, password := RandomUser(t)
testCases := []struct {
name string
body gin.H
Expand Down Expand Up @@ -209,7 +209,7 @@ func TestCreateUserAPI(t *testing.T) {
}

// Helper function to generate a random user for testing
func randomUser(t *testing.T) (user db.User, password string) {
func RandomUser(t *testing.T) (user db.User, password string) {
password = util.RandomString(6)
hashedPassword, err := util.HashPassword(password)
require.NoError(t, err)
Expand Down

0 comments on commit 4df1100

Please sign in to comment.