61 lines
1.3 KiB
Go
61 lines
1.3 KiB
Go
|
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)
|
||
|
store.EXPECT().
|
||
|
CreateUser(gomock.Any(), gomock.Any()).
|
||
|
Times(1).
|
||
|
Return(user, nil)
|
||
|
|
||
|
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,
|
||
|
}
|
||
|
|
||
|
return
|
||
|
}
|