Created
April 4, 2023 20:43
-
-
Save CodeNinjaUG/e123e9104103ad34e3b46f8f7d45edba to your computer and use it in GitHub Desktop.
usertest.go
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
package api | |
import ( | |
"bytes" | |
"encoding/json" | |
"fmt" | |
"io/ioutil" | |
"net/http" | |
"net/http/httptest" | |
"reflect" | |
"testing" | |
"github.com/gin-gonic/gin" | |
"github.com/golang/mock/gomock" | |
"github.com/stretchr/testify/require" | |
mockdb "github.com/thinkIt-africa/SPS-Web/db/Mock" | |
db "github.com/thinkIt-africa/SPS-Web/db/sqlc" | |
"github.com/thinkIt-africa/SPS-Web/util" | |
) | |
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.Password) | |
if err != nil{ | |
return false | |
} | |
e.arg.Password = arg.Password | |
return reflect.DeepEqual(e.arg, arg) | |
} | |
func (e eqCreateUserParamsMatcher) String() string{ | |
return fmt.Sprintf("matches arg %v and password %v", e.arg, e.password) | |
} | |
func EqCreateUserParams(arg db.CreateUserParams, password string) gomock.Matcher { | |
return eqCreateUserParamsMatcher{arg, password} | |
} | |
// func TestGetUserAPI(t *testing.T){ | |
// user := randomUser() | |
// test_cases := []struct { | |
// name string | |
// userID int64 | |
// buildStubs func(store *mockdb.MockStore) | |
// checkResponse func(t *testing.T, recorder *httptest.ResponseRecorder) | |
// }{ | |
// { | |
// name : "OK", | |
// userID : user.ID, | |
// buildStubs: func (store *mockdb.MockStore){ | |
// store.EXPECT().GetUser(gomock.Any(), gomock.Eq(user.ID)).Times(1).Return(user, nil) | |
// }, | |
// checkResponse: func (t *testing.T, recorder *httptest.ResponseRecorder){ | |
// require.Equal(t, http.StatusOK, recorder.Code) | |
// requireBodyMatchUser(t , recorder.Body, user) | |
// }, | |
// }, | |
// { | |
// name : "NotFound", | |
// userID : user.ID, | |
// buildStubs: func (store *mockdb.MockStore){ | |
// store.EXPECT().GetUser(gomock.Any(), gomock.Eq(user.ID)).Times(1).Return(db.User{}, sql.ErrNoRows) | |
// }, | |
// checkResponse: func (t *testing.T, recorder *httptest.ResponseRecorder){ | |
// require.Equal(t, http.StatusNotFound, recorder.Code) | |
// }, | |
// }, | |
// { | |
// name : "InternalServerError", | |
// userID : user.ID, | |
// buildStubs: func (store *mockdb.MockStore){ | |
// store.EXPECT().GetUser(gomock.Any(), gomock.Eq(user.ID)).Times(1).Return(db.User{}, sql.ErrConnDone) | |
// }, | |
// checkResponse: func (t *testing.T, recorder *httptest.ResponseRecorder){ | |
// require.Equal(t, http.StatusInternalServerError, recorder.Code) | |
// }, | |
// }, | |
// { | |
// name : "InvalidID", | |
// userID : 0, | |
// buildStubs: func (store *mockdb.MockStore){ | |
// store.EXPECT().GetUser(gomock.Any(), gomock.Any()).Times(0) | |
// }, | |
// checkResponse: func (t *testing.T, recorder *httptest.ResponseRecorder){ | |
// require.Equal(t, http.StatusBadRequest, recorder.Code) | |
// }, | |
// }, | |
// } | |
// for i := range test_cases { | |
// tc := test_cases[i] | |
// t.Run(tc.name, func(t *testing.T) { | |
// ctrl := gomock.NewController(t) | |
// defer ctrl.Finish() //avoid memory leaks | |
// store := mockdb.NewMockStore(ctrl) | |
// tc.buildStubs(store) | |
// server := NewServer(store) | |
// recorder := httptest.NewRecorder() | |
// url := fmt.Sprintf("/users/%d", tc.userID) | |
// request , err := http.NewRequest(http.MethodGet, url, nil) | |
// require.NoError(t, err) | |
// server.router.ServeHTTP(recorder, request) | |
// tc.checkResponse(t, recorder) | |
// }) | |
// } | |
// } | |
func TestCreateUserAPI(t *testing.T){ | |
user, password := randomUser(t) | |
test_cases := []struct { | |
name string | |
body gin.H | |
buildStubs func(store *mockdb.MockStore) | |
checkResponse func(recorder *httptest.ResponseRecorder) | |
}{ | |
{ | |
name : "OK", | |
body : gin.H{ | |
"id" : user.ID, | |
"first_name": user.FirstName, | |
"last_name": user.LastName, | |
"phone" : user.Phone, | |
"email" : user.Email, | |
"password" : password, | |
}, | |
buildStubs: func (store *mockdb.MockStore){ | |
arg := db.CreateUserParams{ | |
FirstName: user.FirstName, | |
LastName: user.LastName, | |
Email: user.Email, | |
Phone: user.Phone, | |
Password: user.Password, | |
} | |
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{ | |
// "id" : user.ID, | |
// "first_name": user.FirstName, | |
// "last_name": user.LastName, | |
// "phone" : user.Phone, | |
// "email" : user.Email, | |
// "password" : password, | |
// }, | |
// buildStubs: func (store *mockdb.MockStore){ | |
// store.EXPECT().CreateUser(gomock.Any(), gomock.Any()).Times(1).Return(db.User{}, sql.ErrConnDone) | |
// }, | |
// checkResponse: func (t *testing.T, recorder *httptest.ResponseRecorder){ | |
// require.Equal(t, http.StatusInternalServerError, recorder.Code) | |
// }, | |
// }, | |
// { | |
// name : "BadRequest", | |
// body : gin.H{ | |
// "first_name": "invalid", | |
// }, | |
// // buildStubs: func(store *mockdb.MockStore){ | |
// // store.EXPECT().CreateUser(gomock.Any(), gomock.Any()).Times(0) | |
// // }, | |
// checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder){ | |
// require.Equal(t, http.StatusBadRequest, recorder.Code) | |
// }, | |
// }, | |
// { | |
// name : "InternalServerError", | |
// userID : user.ID, | |
// buildStubs: func (store *mockdb.MockStore){ | |
// store.EXPECT().GetUser(gomock.Any(), gomock.Eq(user.ID)).Times(1).Return(db.User{}, sql.ErrConnDone) | |
// }, | |
// checkResponse: func (t *testing.T, recorder *httptest.ResponseRecorder){ | |
// require.Equal(t, http.StatusInternalServerError, recorder.Code) | |
// }, | |
// }, | |
// { | |
// name : "InvalidID", | |
// userID : 0, | |
// buildStubs: func (store *mockdb.MockStore){ | |
// store.EXPECT().GetUser(gomock.Any(), gomock.Any()).Times(0) | |
// }, | |
// checkResponse: func (t *testing.T, recorder *httptest.ResponseRecorder){ | |
// require.Equal(t, http.StatusBadRequest, recorder.Code) | |
// }, | |
// }, | |
} | |
for i := range test_cases { | |
tc := test_cases[i] | |
t.Run(tc.name, func(t *testing.T) { | |
ctrl := gomock.NewController(t) | |
defer ctrl.Finish() //avoid memory leaks | |
store := mockdb.NewMockStore(ctrl) | |
tc.buildStubs(store) | |
server := NewServer(store) | |
recorder := httptest.NewRecorder() | |
url := "/users" | |
data, err := json.Marshal(tc.body) | |
require.NoError(t, err) | |
request, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(data)) | |
require.NoError(t, err) | |
server.router.ServeHTTP(recorder, request) | |
tc.checkResponse(recorder) | |
}) | |
} | |
} | |
// func TestGetUsersAPI(t *testing.T){ | |
// user, password := randomUser() | |
// test_cases := []struct { | |
// name string | |
// body gin.H | |
// buildStubs func(store *mockdb.MockStore) | |
// checkResponse func(t *testing.T, recorder *httptest.ResponseRecorder) | |
// }{ | |
// { | |
// name : "OK", | |
// body : gin.H{ | |
// "id" : user.ID, | |
// "first_name": user.FirstName, | |
// "last_name": user.LastName, | |
// "phone" : user.Phone, | |
// "email" : user.Email, | |
// "password" : user.Password, | |
// }, | |
// buildStubs: func (store *mockdb.MockStore){ | |
// user_params := db.CreateUserParams{ | |
// FirstName: user.FirstName, | |
// LastName: user.LastName, | |
// Email: user.Email, | |
// Phone: user.Phone, | |
// Password: user.Password, | |
// } | |
// store.EXPECT().CreateUser(gomock.Any(), gomock.Eq(user_params)).Times(1).Return(user, nil) | |
// }, | |
// checkResponse: func (t *testing.T, recorder *httptest.ResponseRecorder){ | |
// require.Equal(t, http.StatusOK, recorder.Code) | |
// requireBodyMatchUser(t , recorder.Body, user) | |
// }, | |
// }, | |
// { | |
// name : "InternalError", | |
// body : gin.H{ | |
// "id" : user.ID, | |
// "first_name": user.FirstName, | |
// "last_name": user.LastName, | |
// "phone" : user.Phone, | |
// "email" : user.Email, | |
// "password" : user.Password, | |
// }, | |
// buildStubs: func (store *mockdb.MockStore){ | |
// store.EXPECT().CreateUser(gomock.Any(), gomock.Any()).Times(1).Return(db.User{}, sql.ErrConnDone) | |
// }, | |
// checkResponse: func (t *testing.T, recorder *httptest.ResponseRecorder){ | |
// require.Equal(t, http.StatusInternalServerError, recorder.Code) | |
// }, | |
// }, | |
// { | |
// name : "BadRequest", | |
// body : gin.H{ | |
// "first_name": "invalid", | |
// }, | |
// buildStubs: func (store *mockdb.MockStore){ | |
// store.EXPECT().CreateUser(gomock.Any(), gomock.Any()).Times(0) | |
// }, | |
// checkResponse: func (t *testing.T, recorder *httptest.ResponseRecorder){ | |
// require.Equal(t, http.StatusBadRequest, recorder.Code) | |
// }, | |
// }, | |
// // { | |
// // name : "InternalServerError", | |
// // userID : user.ID, | |
// // buildStubs: func (store *mockdb.MockStore){ | |
// // store.EXPECT().GetUser(gomock.Any(), gomock.Eq(user.ID)).Times(1).Return(db.User{}, sql.ErrConnDone) | |
// // }, | |
// // checkResponse: func (t *testing.T, recorder *httptest.ResponseRecorder){ | |
// // require.Equal(t, http.StatusInternalServerError, recorder.Code) | |
// // }, | |
// // }, | |
// // { | |
// // name : "InvalidID", | |
// // userID : 0, | |
// // buildStubs: func (store *mockdb.MockStore){ | |
// // store.EXPECT().GetUser(gomock.Any(), gomock.Any()).Times(0) | |
// // }, | |
// // checkResponse: func (t *testing.T, recorder *httptest.ResponseRecorder){ | |
// // require.Equal(t, http.StatusBadRequest, recorder.Code) | |
// // }, | |
// // }, | |
// } | |
// for i := range test_cases { | |
// tc := test_cases[i] | |
// t.Run(tc.name, func(t *testing.T) { | |
// ctrl := gomock.NewController(t) | |
// defer ctrl.Finish() //avoid memory leaks | |
// store := mockdb.NewMockStore(ctrl) | |
// tc.buildStubs(store) | |
// server := NewServer(store) | |
// recorder := httptest.NewRecorder() | |
// url := "/users" | |
// data, err := json.Marshal(tc.body) | |
// require.NoError(t, err) | |
// request , err := http.NewRequest(http.MethodPost, url, bytes.NewReader(data)) | |
// require.NoError(t, err) | |
// server.router.ServeHTTP(recorder, request) | |
// tc.checkResponse(t, recorder) | |
// }) | |
// } | |
// } | |
// func randomUser() db.User{ | |
// return db.User{ | |
// ID : util.RandomInt(1,100), | |
// FirstName: util.RandomOwner(), | |
// LastName: util.RandomOwner(), | |
// Phone: util.RandomOwner(), | |
// Email: util.RandomEmail(), | |
// Password: util.RandomOwner(), | |
// } | |
// } | |
func randomUser(t *testing.T)(user db.User, password string){ | |
password = util.RandomString(6) | |
hashed_password, err := util.HashedPassword(password) | |
require.NoError(t, err) | |
user = db.User{ | |
FirstName: util.RandomString(6), | |
LastName: util.RandomString(6), | |
Phone: util.RandomString(7), | |
Email: util.RandomEmail(), | |
Password: hashed_password, | |
} | |
return | |
} | |
func requireBodyMatchUser(t *testing.T , body *bytes.Buffer, user db.User){ | |
data, err := ioutil.ReadAll(body) | |
require.NoError(t, err) | |
var gt_account db.User | |
err = json.Unmarshal(data, >_account) | |
require.NoError(t, err) | |
require.Equal(t, user, gt_account) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment