-
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.
- Loading branch information
1 parent
0fe2bfa
commit 4ba71de
Showing
1 changed file
with
224 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,224 @@ | ||
package api | ||
|
||
import ( | ||
"bytes" | ||
"database/sql" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"reflect" | ||
"testing" | ||
|
||
mockdb "github.com/ayushrakesh/go-bank/db/mock" | ||
db "github.com/ayushrakesh/go-bank/db/sqlc" | ||
"github.com/ayushrakesh/go-bank/util" | ||
"github.com/gin-gonic/gin" | ||
"github.com/golang/mock/gomock" | ||
"github.com/lib/pq" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type eqCreateUserParamsMatcher struct { | ||
arg db.CreateUserParams | ||
password string | ||
} | ||
|
||
func (e eqCreateUserParamsMatcher) Matches(x interface{}) bool { | ||
arg, ok := x.(db.CreateUserParams) | ||
if !ok { | ||
return false | ||
} | ||
err := util.CheckPassword(e.password, arg.HashedPassword) | ||
if err != nil { | ||
return false | ||
} | ||
e.arg.HashedPassword = arg.HashedPassword | ||
return reflect.DeepEqual((e.arg), arg) | ||
} | ||
|
||
func (e eqCreateUserParamsMatcher) String() string { | ||
return fmt.Sprintf("matches arg %v and %v", e.arg, e.password) | ||
} | ||
|
||
func eqCreateUserParams(arg db.CreateUserParams, password string) gomock.Matcher { | ||
return eqCreateUserParamsMatcher{arg, password} | ||
} | ||
|
||
func TestCreateUserAPI(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, | ||
"full_name": user.FullName, | ||
"email": user.Email, | ||
}, | ||
buildStubs: func(store *mockdb.MockStore) { | ||
arg := db.CreateUserParams{ | ||
Username: user.Username, | ||
FullName: user.FullName, | ||
Email: user.Email, | ||
} | ||
store.EXPECT(). | ||
CreateUser(gomock.Any(), eqCreateUserParams(arg, password)). | ||
Times(1). | ||
Return(user, nil) | ||
}, | ||
checkResponse: func(recorder *httptest.ResponseRecorder) { | ||
require.Equal(t, http.StatusOK, recorder.Code) | ||
requireBodyMatchUser(t, recorder.Body, user) | ||
}, | ||
}, | ||
{ | ||
name: "InternalError", | ||
body: gin.H{ | ||
"username": user.Username, | ||
"password": password, | ||
"full_name": user.FullName, | ||
"email": user.Email, | ||
}, | ||
buildStubs: func(store *mockdb.MockStore) { | ||
store.EXPECT(). | ||
CreateUser(gomock.Any(), gomock.Any()). | ||
Times(1). | ||
Return(db.User{}, sql.ErrConnDone) | ||
}, | ||
checkResponse: func(recorder *httptest.ResponseRecorder) { | ||
require.Equal(t, http.StatusInternalServerError, recorder.Code) | ||
}, | ||
}, | ||
{ | ||
name: "DuplicateUsername", | ||
body: gin.H{ | ||
"username": user.Username, | ||
"password": password, | ||
"full_name": user.FullName, | ||
"email": user.Email, | ||
}, | ||
buildStubs: func(store *mockdb.MockStore) { | ||
store.EXPECT(). | ||
CreateUser(gomock.Any(), gomock.Any()). | ||
Times(1). | ||
Return(db.User{}, &pq.Error{Code: "23505"}) | ||
}, | ||
checkResponse: func(recorder *httptest.ResponseRecorder) { | ||
require.Equal(t, http.StatusForbidden, recorder.Code) | ||
}, | ||
}, | ||
{ | ||
name: "InvalidUsername", | ||
body: gin.H{ | ||
"username": "invalid-user#1", | ||
"password": password, | ||
"full_name": user.FullName, | ||
"email": user.Email, | ||
}, | ||
buildStubs: func(store *mockdb.MockStore) { | ||
store.EXPECT(). | ||
CreateUser(gomock.Any(), gomock.Any()). | ||
Times(0) | ||
}, | ||
checkResponse: func(recorder *httptest.ResponseRecorder) { | ||
require.Equal(t, http.StatusBadRequest, recorder.Code) | ||
}, | ||
}, | ||
{ | ||
name: "InvalidEmail", | ||
body: gin.H{ | ||
"username": user.Username, | ||
"password": password, | ||
"full_name": user.FullName, | ||
"email": "invalid-email", | ||
}, | ||
buildStubs: func(store *mockdb.MockStore) { | ||
store.EXPECT(). | ||
CreateUser(gomock.Any(), gomock.Any()). | ||
Times(0) | ||
}, | ||
checkResponse: func(recorder *httptest.ResponseRecorder) { | ||
require.Equal(t, http.StatusBadRequest, recorder.Code) | ||
}, | ||
}, | ||
{ | ||
name: "TooShortPassword", | ||
body: gin.H{ | ||
"username": user.Username, | ||
"password": "123", | ||
"full_name": user.FullName, | ||
"email": user.Email, | ||
}, | ||
buildStubs: func(store *mockdb.MockStore) { | ||
store.EXPECT(). | ||
CreateUser(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 := NewServer(store) | ||
recorder := httptest.NewRecorder() | ||
|
||
// Marshal body data to JSON | ||
data, err := json.Marshal(tc.body) | ||
require.NoError(t, err) | ||
|
||
url := "/users" | ||
request, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(data)) | ||
require.NoError(t, err) | ||
|
||
server.router.ServeHTTP(recorder, request) | ||
tc.checkResponse(recorder) | ||
}) | ||
} | ||
} | ||
|
||
func randomUser(t *testing.T) (user db.User, password string) { | ||
password = util.RandomString(6) | ||
hashedPassword, err := util.HashPassword(password) | ||
require.NoError(t, err) | ||
|
||
user = db.User{ | ||
Username: util.RandomOwner(), | ||
HashedPassword: hashedPassword, | ||
FullName: util.RandomOwner(), | ||
Email: util.RandomEmail(), | ||
} | ||
return | ||
} | ||
|
||
func requireBodyMatchUser(t *testing.T, body *bytes.Buffer, user db.User) { | ||
data, err := io.ReadAll(body) | ||
require.NoError(t, err) | ||
|
||
var gotUser db.User | ||
err = json.Unmarshal(data, &gotUser) | ||
|
||
require.NoError(t, err) | ||
require.Equal(t, user.Username, gotUser.Username) | ||
require.Equal(t, user.FullName, gotUser.FullName) | ||
require.Equal(t, user.Email, gotUser.Email) | ||
require.Empty(t, gotUser.HashedPassword) | ||
} |