2023-09-12 17:08:17 +07:00
|
|
|
package api_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
mockdb "git.nochill.in/nochill/hiling_go/db/mock"
|
|
|
|
db "git.nochill.in/nochill/hiling_go/db/sqlc"
|
|
|
|
"git.nochill.in/nochill/hiling_go/util"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.uber.org/mock/gomock"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSignupAPI(t *testing.T) {
|
|
|
|
user, pass := createUser(t)
|
|
|
|
t.Run("OK", func(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
|
|
|
store := mockdb.NewMockStore(ctrl)
|
2023-09-13 21:42:31 +07:00
|
|
|
// store.EXPECT().
|
|
|
|
// CreateUser(gomock.Any(), gomock.Any()).
|
|
|
|
// Times(1).
|
|
|
|
// Return(user, nil)
|
2023-09-12 17:08:17 +07:00
|
|
|
|
|
|
|
server := newTestServer(t, store)
|
|
|
|
recorder := httptest.NewRecorder()
|
|
|
|
|
|
|
|
data, err := json.Marshal(gin.H{
|
|
|
|
"username": user.Username,
|
|
|
|
"password": pass,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
url := "/user/signup"
|
|
|
|
request, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(data))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
server.Router.ServeHTTP(recorder, request)
|
|
|
|
require.Equal(t, http.StatusOK, recorder.Code)
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func createUser(t *testing.T) (user db.User, password string) {
|
|
|
|
passw := util.RandomString(10)
|
|
|
|
hashedPassword, err := util.HashPassword(passw)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
user = db.User{
|
|
|
|
Username: util.RandomString(10),
|
|
|
|
Password: hashedPassword,
|
|
|
|
}
|
|
|
|
|
2023-09-13 21:42:31 +07:00
|
|
|
return user, passw
|
2023-09-12 17:08:17 +07:00
|
|
|
}
|