ft/adds first test for api.CreateAccount

This commit is contained in:
itsscb 2023-09-22 15:19:06 +02:00
parent d8c8963347
commit 933347095a

View File

@ -2,13 +2,16 @@ package api
import ( import (
"bytes" "bytes"
"database/sql"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"testing" "testing"
"time"
"github.com/gin-gonic/gin"
mockdb "github.com/itsscb/df/db/mock" mockdb "github.com/itsscb/df/db/mock"
db "github.com/itsscb/df/db/sqlc" db "github.com/itsscb/df/db/sqlc"
"github.com/itsscb/df/util" "github.com/itsscb/df/util"
@ -16,6 +19,127 @@ import (
"go.uber.org/mock/gomock" "go.uber.org/mock/gomock"
) )
func TestCreateAccountAPI(t *testing.T) {
account := randomAccount()
testCases := []struct {
name string
body gin.H
buildStubs func(store *mockdb.MockStore)
checkResponse func(recoder *httptest.ResponseRecorder)
}{
{
name: "OK",
body: gin.H{
"passwordhash": account.Passwordhash,
"firstname": account.Firstname,
"lastname": account.Lastname,
"birthday": account.Birthday,
"email": account.Email,
"city": account.City,
"zip": account.Zip,
"street": account.Street,
"country": account.Country,
"phone": account.Phone.String,
"creator": account.Creator,
},
buildStubs: func(store *mockdb.MockStore) {
arg := db.CreateAccountParams{
Passwordhash: account.Passwordhash,
Firstname: account.Firstname,
Lastname: account.Lastname,
Birthday: account.Birthday,
Email: account.Email,
City: account.City,
Zip: account.Zip,
Street: account.Street,
Country: account.Country,
Phone: account.Phone,
Creator: account.Creator,
}
store.EXPECT().
CreateAccount(gomock.Any(), gomock.Eq(arg)).
Times(1).
Return(account, nil)
},
checkResponse: func(recorder *httptest.ResponseRecorder) {
require.Equal(t, http.StatusOK, recorder.Code)
requireBodyMatchAccount(t, recorder.Body, account)
},
},
// {
// name: "NoAuthorization",
// body: gin.H{
// "currency": account.Currency,
// },
// buildStubs: func(store *mockdb.MockStore) {
// store.EXPECT().
// CreateAccount(gomock.Any(), gomock.Any()).
// Times(0)
// },
// checkResponse: func(recorder *httptest.ResponseRecorder) {
// 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: "InvalidCurrency",
body: gin.H{
"currency": "invalid",
},
buildStubs: func(store *mockdb.MockStore) {
store.EXPECT().
CreateAccount(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 := "/accounts"
request, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(data))
require.NoError(t, err)
server.router.ServeHTTP(recorder, request)
tc.checkResponse(recorder)
})
}
}
func TestGetAccountAPI(t *testing.T) { func TestGetAccountAPI(t *testing.T) {
account := randomAccount() account := randomAccount()
@ -48,10 +172,19 @@ func randomAccount() db.Account {
Firstname: util.RandomName(), Firstname: util.RandomName(),
Lastname: util.RandomName(), Lastname: util.RandomName(),
Email: util.RandomEmail(), Email: util.RandomEmail(),
Phone: sql.NullString{
String: util.RandomPhone(),
Valid: true,
},
Zip: util.RandomName(), Zip: util.RandomName(),
Street: util.RandomName(), Street: util.RandomName(),
City: util.RandomName(), City: util.RandomName(),
Country: util.RandomName(), Country: util.RandomName(),
Creator: "system",
Changer: util.RandomName(),
Created: time.Date(1990, 1, 1, 0, 0, 0, 0, time.UTC),
Changed: time.Date(1990, 1, 1, 0, 0, 0, 0, time.UTC),
Birthday: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC),
} }
} }