naise_pos/api/product_test.go

176 lines
5.3 KiB
Go
Raw Normal View History

2023-03-12 11:01:43 +07:00
package api
import (
"bytes"
"database/sql"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
2023-03-15 15:00:36 +07:00
"time"
2023-03-12 11:01:43 +07:00
mockdb "git.nochill.in/nochill/naice_pos/db/mock"
db "git.nochill.in/nochill/naice_pos/db/sqlc"
2023-03-15 15:00:36 +07:00
"git.nochill.in/nochill/naice_pos/token"
2023-03-12 11:01:43 +07:00
"git.nochill.in/nochill/naice_pos/util"
"github.com/golang/mock/gomock"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
)
func TestGetProductApi(t *testing.T) {
2023-03-15 15:00:36 +07:00
merchantID := "f9ca13cf-8ab3-4ee3-9530-521ae505caa2"
product := randomProduct(merchantID)
2023-03-12 11:01:43 +07:00
testCases := []struct {
name string
productID string
buildStubs func(store *mockdb.MockStore)
2023-03-15 15:00:36 +07:00
setupAuth func(t *testing.T, request *http.Request, tokenMaker token.Maker)
2023-03-12 11:01:43 +07:00
checkResponse func(t *testing.T, recorder *httptest.ResponseRecorder)
}{
{
name: "OK",
productID: product.ID.String(),
buildStubs: func(store *mockdb.MockStore) {
store.EXPECT().
GetProduct(gomock.Any(), gomock.Eq(product.ID)).
Times(1).
Return(product, nil)
},
2023-03-15 15:00:36 +07:00
setupAuth: func(t *testing.T, request *http.Request, tokenMaker token.Maker) {
addAuthorization(t, request, tokenMaker, authorizationTypeBearer, "email", merchantID, time.Minute)
},
2023-03-12 11:01:43 +07:00
checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder) {
require.Equal(t, http.StatusOK, recorder.Code)
requireBodyMatchAccount(t, recorder.Body, product)
},
},
{
name: "Not found",
productID: product.ID.String(),
buildStubs: func(store *mockdb.MockStore) {
store.EXPECT().
GetProduct(gomock.Any(), gomock.Eq(product.ID)).
Times(1).
Return(db.Product{}, sql.ErrNoRows)
},
2023-03-15 15:00:36 +07:00
setupAuth: func(t *testing.T, request *http.Request, tokenMaker token.Maker) {
addAuthorization(t, request, tokenMaker, authorizationTypeBearer, "email", merchantID, time.Minute)
},
checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder) {
require.Equal(t, http.StatusNotFound, recorder.Code)
},
},
{
name: "Unauthorized",
productID: product.ID.String(),
buildStubs: func(store *mockdb.MockStore) {
store.EXPECT().
GetProduct(gomock.Any(), gomock.Eq(product.ID)).
Times(1).
Return(db.Product{}, sql.ErrNoRows)
},
setupAuth: func(t *testing.T, request *http.Request, tokenMaker token.Maker) {
addAuthorization(t, request, tokenMaker, authorizationTypeBearer, "email", uuid.New().String(), time.Minute)
},
2023-03-12 11:01:43 +07:00
checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder) {
require.Equal(t, http.StatusNotFound, recorder.Code)
},
},
2023-03-15 15:00:36 +07:00
{
name: "NoAuthorization",
productID: product.ID.String(),
buildStubs: func(store *mockdb.MockStore) {
store.EXPECT().
GetProduct(gomock.Any(), gomock.Any()).
Times(0)
},
setupAuth: func(t *testing.T, request *http.Request, tokenMaker token.Maker) {
},
checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder) {
require.Equal(t, http.StatusUnauthorized, recorder.Code)
},
},
2023-03-12 11:01:43 +07:00
{
name: "Internal Error",
productID: product.ID.String(),
buildStubs: func(store *mockdb.MockStore) {
store.EXPECT().
GetProduct(gomock.Any(), gomock.Eq(product.ID)).
Times(1).
Return(db.Product{}, sql.ErrConnDone)
},
2023-03-15 15:00:36 +07:00
setupAuth: func(t *testing.T, request *http.Request, tokenMaker token.Maker) {
addAuthorization(t, request, tokenMaker, authorizationTypeBearer, "email", merchantID, time.Minute)
},
2023-03-12 11:01:43 +07:00
checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder) {
require.Equal(t, http.StatusInternalServerError, recorder.Code)
},
},
{
name: "Bad request",
productID: "0",
buildStubs: func(store *mockdb.MockStore) {
store.EXPECT().
GetProduct(gomock.Any(), gomock.Any()).
Times(0)
},
2023-03-15 15:00:36 +07:00
setupAuth: func(t *testing.T, request *http.Request, tokenMaker token.Maker) {
addAuthorization(t, request, tokenMaker, authorizationTypeBearer, "email", merchantID, time.Minute)
},
2023-03-12 11:01:43 +07:00
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)
tc.buildStubs(store)
2023-03-14 17:39:40 +07:00
server := newTestServer(t, store)
2023-03-12 11:01:43 +07:00
recorder := httptest.NewRecorder()
2023-03-15 15:00:36 +07:00
url := fmt.Sprintf("/api/product/%s", tc.productID)
2023-03-12 11:01:43 +07:00
request, err := http.NewRequest(http.MethodGet, url, nil)
require.NoError(t, err)
2023-03-15 15:00:36 +07:00
tc.setupAuth(t, request, server.tokenMaker)
2023-03-12 11:01:43 +07:00
server.router.ServeHTTP(recorder, request)
tc.checkResponse(t, recorder)
})
}
}
2023-03-15 15:00:36 +07:00
func randomProduct(merchantID string) db.Product {
2023-03-12 11:01:43 +07:00
return db.Product{
ID: uuid.New(),
2023-03-15 15:00:36 +07:00
MerchantID: uuid.MustParse("f9ca13cf-8ab3-4ee3-9530-521ae505caa2"),
2023-03-12 11:01:43 +07:00
Name: util.RandomString(5),
SellingPrice: util.RandomFloat(1000, 99999),
PurchasePrice: util.RandomFloat(999, 9999),
Stock: util.RandomFloat(100, 99999),
}
}
func requireBodyMatchAccount(t *testing.T, body *bytes.Buffer, product db.Product) {
data, err := ioutil.ReadAll(body)
require.NoError(t, err)
var gotProduct db.Product
err = json.Unmarshal(data, &gotProduct)
require.NoError(t, err)
require.Equal(t, product, gotProduct)
}