ft/adds more tests for api.account
This commit is contained in:
parent
411d3ff6a9
commit
560fc3fbcc
@ -82,21 +82,21 @@ func TestCreateAccountAPI(t *testing.T) {
|
||||
// require.Equal(t, http.StatusUnauthorized, recorder.Code)
|
||||
// },
|
||||
// },
|
||||
// {
|
||||
// name: "BadRequest",
|
||||
// body: gin.H{
|
||||
// "email": account.Email,
|
||||
// },
|
||||
// buildStubs: func(store *mockdb.MockStore) {
|
||||
// store.EXPECT().
|
||||
// CreateAccount(gomock.Any(), gomock.Any()).
|
||||
// Times(1).
|
||||
// Return(db.Account{}, sql.ErrConnDone)
|
||||
// },
|
||||
// checkResponse: func(recorder *httptest.ResponseRecorder) {
|
||||
// require.Equal(t, http.StatusBadRequest, recorder.Code)
|
||||
// },
|
||||
// },
|
||||
{
|
||||
name: "BadRequest",
|
||||
body: gin.H{
|
||||
"email": account.Email,
|
||||
},
|
||||
buildStubs: func(store *mockdb.MockStore) {
|
||||
store.EXPECT().
|
||||
CreateAccount(gomock.Any(), gomock.Any()).
|
||||
Times(0).
|
||||
Return(db.Account{}, sql.ErrConnDone)
|
||||
},
|
||||
checkResponse: func(recorder *httptest.ResponseRecorder) {
|
||||
require.Equal(t, http.StatusBadRequest, recorder.Code)
|
||||
},
|
||||
},
|
||||
/* {
|
||||
name: "InvalidCurrency",
|
||||
body: gin.H{
|
||||
@ -140,29 +140,272 @@ func TestCreateAccountAPI(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// func TestGetAccountAPI(t *testing.T) {
|
||||
// account := randomAccount()
|
||||
|
||||
// ctrl := gomock.NewController(t)
|
||||
// defer ctrl.Finish()
|
||||
|
||||
// store := mockdb.NewMockStore(ctrl)
|
||||
|
||||
// store.EXPECT().GetAccount(gomock.Any(), gomock.Eq(account.ID)).
|
||||
// Times(1).
|
||||
// Return(account, nil)
|
||||
|
||||
// server := NewServer(config, store)
|
||||
// recorder := httptest.NewRecorder()
|
||||
|
||||
// uri := fmt.Sprintf("/accounts/%d", account.ID)
|
||||
// req, err := http.NewRequest(http.MethodGet, uri, nil)
|
||||
// require.NoError(t, err)
|
||||
|
||||
// server.router.ServeHTTP(recorder, req)
|
||||
|
||||
// require.Equal(t, http.StatusOK, recorder.Code)
|
||||
// requireBodyMatchAccount(t, recorder.Body, account)
|
||||
// }
|
||||
|
||||
func TestGetAccountAPI(t *testing.T) {
|
||||
account := randomAccount()
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
accountID int64
|
||||
buildStubs func(store *mockdb.MockStore)
|
||||
checkResponse func(t *testing.T, recoder *httptest.ResponseRecorder)
|
||||
}{
|
||||
{
|
||||
name: "OK",
|
||||
accountID: account.ID,
|
||||
buildStubs: func(store *mockdb.MockStore) {
|
||||
store.EXPECT().
|
||||
GetAccount(gomock.Any(), gomock.Eq(account.ID)).
|
||||
Times(1).
|
||||
Return(account, nil)
|
||||
},
|
||||
checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder) {
|
||||
require.Equal(t, http.StatusOK, recorder.Code)
|
||||
requireBodyMatchAccount(t, recorder.Body, account)
|
||||
},
|
||||
},
|
||||
// {
|
||||
// name: "UnauthorizedUser",
|
||||
// accountID: account.ID,
|
||||
// setupAuth: func(t *testing.T, request *http.Request, tokenMaker token.Maker) {
|
||||
// addAuthorization(t, request, tokenMaker, authorizationTypeBearer, "unauthorized_user", time.Minute)
|
||||
// },
|
||||
// buildStubs: func(store *mockdb.MockStore) {
|
||||
// store.EXPECT().
|
||||
// GetAccount(gomock.Any(), gomock.Eq(account.ID)).
|
||||
// Times(1).
|
||||
// Return(account, nil)
|
||||
// },
|
||||
// checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder) {
|
||||
// require.Equal(t, http.StatusUnauthorized, recorder.Code)
|
||||
// },
|
||||
// },
|
||||
// {
|
||||
// name: "NoAuthorization",
|
||||
// accountID: account.ID,
|
||||
// buildStubs: func(store *mockdb.MockStore) {
|
||||
// store.EXPECT().
|
||||
// GetAccount(gomock.Any(), gomock.Any()).
|
||||
// Times(0)
|
||||
// },
|
||||
// checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder) {
|
||||
// require.Equal(t, http.StatusUnauthorized, recorder.Code)
|
||||
// },
|
||||
// },
|
||||
{
|
||||
name: "NotFound",
|
||||
accountID: account.ID,
|
||||
buildStubs: func(store *mockdb.MockStore) {
|
||||
store.EXPECT().
|
||||
GetAccount(gomock.Any(), gomock.Eq(account.ID)).
|
||||
Times(1).
|
||||
Return(db.Account{}, sql.ErrNoRows)
|
||||
},
|
||||
checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder) {
|
||||
require.Equal(t, http.StatusNotFound, recorder.Code)
|
||||
},
|
||||
},
|
||||
// {
|
||||
// name: "InternalError",
|
||||
// accountID: account.ID,
|
||||
// buildStubs: func(store *mockdb.MockStore) {
|
||||
// store.EXPECT().
|
||||
// GetAccount(gomock.Any(), gomock.Eq(account.ID)).
|
||||
// Times(1).
|
||||
// Return(db.Account{}, sql.ErrConnDone)
|
||||
// },
|
||||
// checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder) {
|
||||
// require.Equal(t, http.StatusInternalServerError, recorder.Code)
|
||||
// },
|
||||
// },
|
||||
{
|
||||
name: "InvalidID",
|
||||
accountID: 0,
|
||||
buildStubs: func(store *mockdb.MockStore) {
|
||||
store.EXPECT().
|
||||
GetAccount(gomock.Any(), gomock.Any()).
|
||||
Times(0)
|
||||
},
|
||||
checkResponse: func(t *testing.T, 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)
|
||||
|
||||
store.EXPECT().GetAccount(gomock.Any(), gomock.Eq(account.ID)).
|
||||
Times(1).
|
||||
Return(account, nil)
|
||||
tc.buildStubs(store)
|
||||
|
||||
server := NewServer(config, store)
|
||||
recorder := httptest.NewRecorder()
|
||||
|
||||
uri := fmt.Sprintf("/accounts/%d", account.ID)
|
||||
req, err := http.NewRequest(http.MethodGet, uri, nil)
|
||||
url := fmt.Sprintf("/accounts/%d", tc.accountID)
|
||||
request, err := http.NewRequest(http.MethodGet, url, nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
server.router.ServeHTTP(recorder, req)
|
||||
server.router.ServeHTTP(recorder, request)
|
||||
tc.checkResponse(t, recorder)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestListAccountsAPI(t *testing.T) {
|
||||
|
||||
n := 5
|
||||
accounts := make([]db.Account, n)
|
||||
for i := 0; i < n; i++ {
|
||||
accounts[i] = randomAccount()
|
||||
}
|
||||
|
||||
type Query struct {
|
||||
pageID int
|
||||
pageSize int
|
||||
}
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
query Query
|
||||
buildStubs func(store *mockdb.MockStore)
|
||||
checkResponse func(recoder *httptest.ResponseRecorder)
|
||||
}{
|
||||
{
|
||||
name: "OK",
|
||||
query: Query{
|
||||
pageID: 1,
|
||||
pageSize: n,
|
||||
},
|
||||
buildStubs: func(store *mockdb.MockStore) {
|
||||
arg := db.ListAccountsParams{
|
||||
Limit: int32(n),
|
||||
Offset: 0,
|
||||
}
|
||||
|
||||
store.EXPECT().
|
||||
ListAccounts(gomock.Any(), gomock.Eq(arg)).
|
||||
Times(1).
|
||||
Return(accounts, nil)
|
||||
},
|
||||
checkResponse: func(recorder *httptest.ResponseRecorder) {
|
||||
require.Equal(t, http.StatusOK, recorder.Code)
|
||||
requireBodyMatchAccount(t, recorder.Body, account)
|
||||
requireBodyMatchAccounts(t, recorder.Body, accounts)
|
||||
},
|
||||
},
|
||||
// {
|
||||
// name: "NoAuthorization",
|
||||
// query: Query{
|
||||
// pageID: 1,
|
||||
// pageSize: n,
|
||||
// },
|
||||
// buildStubs: func(store *mockdb.MockStore) {
|
||||
// store.EXPECT().
|
||||
// ListAccounts(gomock.Any(), gomock.Any()).
|
||||
// Times(0)
|
||||
// },
|
||||
// checkResponse: func(recorder *httptest.ResponseRecorder) {
|
||||
// require.Equal(t, http.StatusUnauthorized, recorder.Code)
|
||||
// },
|
||||
// },
|
||||
{
|
||||
name: "EmptyQuery",
|
||||
query: Query{},
|
||||
buildStubs: func(store *mockdb.MockStore) {
|
||||
store.EXPECT().
|
||||
ListAccounts(gomock.Any(), gomock.Any()).
|
||||
Times(0)
|
||||
},
|
||||
checkResponse: func(recorder *httptest.ResponseRecorder) {
|
||||
require.Equal(t, http.StatusBadRequest, recorder.Code)
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "InvalidPageID",
|
||||
query: Query{
|
||||
pageID: -1,
|
||||
pageSize: n,
|
||||
},
|
||||
buildStubs: func(store *mockdb.MockStore) {
|
||||
store.EXPECT().
|
||||
ListAccounts(gomock.Any(), gomock.Any()).
|
||||
Times(0)
|
||||
},
|
||||
checkResponse: func(recorder *httptest.ResponseRecorder) {
|
||||
require.Equal(t, http.StatusBadRequest, recorder.Code)
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "InvalidPageSize",
|
||||
query: Query{
|
||||
pageID: 1,
|
||||
pageSize: 100000,
|
||||
},
|
||||
buildStubs: func(store *mockdb.MockStore) {
|
||||
store.EXPECT().
|
||||
ListAccounts(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(config, store)
|
||||
recorder := httptest.NewRecorder()
|
||||
|
||||
url := "/accounts"
|
||||
request, err := http.NewRequest(http.MethodGet, url, nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Add query parameters to request URL
|
||||
q := request.URL.Query()
|
||||
q.Add("pageid", fmt.Sprintf("%d", tc.query.pageID))
|
||||
q.Add("pagesize", fmt.Sprintf("%d", tc.query.pageSize))
|
||||
request.URL.RawQuery = q.Encode()
|
||||
|
||||
server.router.ServeHTTP(recorder, request)
|
||||
tc.checkResponse(recorder)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func randomAccount() db.Account {
|
||||
@ -197,3 +440,13 @@ func requireBodyMatchAccount(t *testing.T, body *bytes.Buffer, account db.Accoun
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, account, getAccount)
|
||||
}
|
||||
|
||||
func requireBodyMatchAccounts(t *testing.T, body *bytes.Buffer, accounts []db.Account) {
|
||||
data, err := io.ReadAll(body)
|
||||
require.NoError(t, err)
|
||||
|
||||
var gotAccounts []db.Account
|
||||
err = json.Unmarshal(data, &gotAccounts)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, accounts, gotAccounts)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user