67 lines
1.5 KiB
Go
67 lines
1.5 KiB
Go
package api
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
mockdb "github.com/itsscb/df/db/mock"
|
|
db "github.com/itsscb/df/db/sqlc"
|
|
"github.com/itsscb/df/util"
|
|
"github.com/stretchr/testify/require"
|
|
"go.uber.org/mock/gomock"
|
|
)
|
|
|
|
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(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 randomAccount() db.Account {
|
|
return db.Account{
|
|
ID: util.RandomInt(1, 1000),
|
|
Passwordhash: util.RandomString(250),
|
|
Firstname: util.RandomName(),
|
|
Lastname: util.RandomName(),
|
|
Email: util.RandomEmail(),
|
|
Zip: util.RandomName(),
|
|
Street: util.RandomName(),
|
|
City: util.RandomName(),
|
|
Country: util.RandomName(),
|
|
}
|
|
}
|
|
|
|
func requireBodyMatchAccount(t *testing.T, body *bytes.Buffer, account db.Account) {
|
|
data, err := io.ReadAll(body)
|
|
require.NoError(t, err)
|
|
|
|
var getAccount db.Account
|
|
err = json.Unmarshal(data, &getAccount)
|
|
require.NoError(t, err)
|
|
require.Equal(t, account, getAccount)
|
|
}
|