Remove mock & Use database util in token api
This commit is contained in:
parent
785aa7e5b3
commit
b9b98f0ce9
|
|
@ -1,162 +0,0 @@
|
||||||
// Code generated by mockery v1.0.0
|
|
||||||
package mock
|
|
||||||
|
|
||||||
import "github.com/stretchr/testify/mock"
|
|
||||||
import "github.com/gotify/server/model"
|
|
||||||
|
|
||||||
// MockTokenDatabase is an autogenerated mock type for the TokenDatabase type
|
|
||||||
type MockTokenDatabase struct {
|
|
||||||
mock.Mock
|
|
||||||
}
|
|
||||||
|
|
||||||
// CreateApplication provides a mock function with given fields: application
|
|
||||||
func (_m *MockTokenDatabase) CreateApplication(application *model.Application) error {
|
|
||||||
ret := _m.Called(application)
|
|
||||||
|
|
||||||
var r0 error
|
|
||||||
if rf, ok := ret.Get(0).(func(*model.Application) error); ok {
|
|
||||||
r0 = rf(application)
|
|
||||||
} else {
|
|
||||||
r0 = ret.Error(0)
|
|
||||||
}
|
|
||||||
|
|
||||||
return r0
|
|
||||||
}
|
|
||||||
|
|
||||||
// CreateClient provides a mock function with given fields: client
|
|
||||||
func (_m *MockTokenDatabase) CreateClient(client *model.Client) error {
|
|
||||||
ret := _m.Called(client)
|
|
||||||
|
|
||||||
var r0 error
|
|
||||||
if rf, ok := ret.Get(0).(func(*model.Client) error); ok {
|
|
||||||
r0 = rf(client)
|
|
||||||
} else {
|
|
||||||
r0 = ret.Error(0)
|
|
||||||
}
|
|
||||||
|
|
||||||
return r0
|
|
||||||
}
|
|
||||||
|
|
||||||
// DeleteApplicationByID provides a mock function with given fields: id
|
|
||||||
func (_m *MockTokenDatabase) DeleteApplicationByID(id uint) error {
|
|
||||||
ret := _m.Called(id)
|
|
||||||
|
|
||||||
var r0 error
|
|
||||||
if rf, ok := ret.Get(0).(func(uint) error); ok {
|
|
||||||
r0 = rf(id)
|
|
||||||
} else {
|
|
||||||
r0 = ret.Error(0)
|
|
||||||
}
|
|
||||||
|
|
||||||
return r0
|
|
||||||
}
|
|
||||||
|
|
||||||
// DeleteClientByID provides a mock function with given fields: id
|
|
||||||
func (_m *MockTokenDatabase) DeleteClientByID(id uint) error {
|
|
||||||
ret := _m.Called(id)
|
|
||||||
|
|
||||||
var r0 error
|
|
||||||
if rf, ok := ret.Get(0).(func(uint) error); ok {
|
|
||||||
r0 = rf(id)
|
|
||||||
} else {
|
|
||||||
r0 = ret.Error(0)
|
|
||||||
}
|
|
||||||
|
|
||||||
return r0
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetApplicationByID provides a mock function with given fields: id
|
|
||||||
func (_m *MockTokenDatabase) GetApplicationByID(id uint) *model.Application {
|
|
||||||
ret := _m.Called(id)
|
|
||||||
|
|
||||||
var r0 *model.Application
|
|
||||||
if rf, ok := ret.Get(0).(func(uint) *model.Application); ok {
|
|
||||||
r0 = rf(id)
|
|
||||||
} else {
|
|
||||||
if ret.Get(0) != nil {
|
|
||||||
r0 = ret.Get(0).(*model.Application)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return r0
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetApplicationByToken provides a mock function with given fields: token
|
|
||||||
func (_m *MockTokenDatabase) GetApplicationByToken(token string) *model.Application {
|
|
||||||
ret := _m.Called(token)
|
|
||||||
|
|
||||||
var r0 *model.Application
|
|
||||||
if rf, ok := ret.Get(0).(func(string) *model.Application); ok {
|
|
||||||
r0 = rf(token)
|
|
||||||
} else {
|
|
||||||
if ret.Get(0) != nil {
|
|
||||||
r0 = ret.Get(0).(*model.Application)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return r0
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetApplicationsByUser provides a mock function with given fields: userID
|
|
||||||
func (_m *MockTokenDatabase) GetApplicationsByUser(userID uint) []*model.Application {
|
|
||||||
ret := _m.Called(userID)
|
|
||||||
|
|
||||||
var r0 []*model.Application
|
|
||||||
if rf, ok := ret.Get(0).(func(uint) []*model.Application); ok {
|
|
||||||
r0 = rf(userID)
|
|
||||||
} else {
|
|
||||||
if ret.Get(0) != nil {
|
|
||||||
r0 = ret.Get(0).([]*model.Application)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return r0
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetClientByID provides a mock function with given fields: id
|
|
||||||
func (_m *MockTokenDatabase) GetClientByID(id uint) *model.Client {
|
|
||||||
ret := _m.Called(id)
|
|
||||||
|
|
||||||
var r0 *model.Client
|
|
||||||
if rf, ok := ret.Get(0).(func(uint) *model.Client); ok {
|
|
||||||
r0 = rf(id)
|
|
||||||
} else {
|
|
||||||
if ret.Get(0) != nil {
|
|
||||||
r0 = ret.Get(0).(*model.Client)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return r0
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetClientByToken provides a mock function with given fields: token
|
|
||||||
func (_m *MockTokenDatabase) GetClientByToken(token string) *model.Client {
|
|
||||||
ret := _m.Called(token)
|
|
||||||
|
|
||||||
var r0 *model.Client
|
|
||||||
if rf, ok := ret.Get(0).(func(string) *model.Client); ok {
|
|
||||||
r0 = rf(token)
|
|
||||||
} else {
|
|
||||||
if ret.Get(0) != nil {
|
|
||||||
r0 = ret.Get(0).(*model.Client)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return r0
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetClientsByUser provides a mock function with given fields: userID
|
|
||||||
func (_m *MockTokenDatabase) GetClientsByUser(userID uint) []*model.Client {
|
|
||||||
ret := _m.Called(userID)
|
|
||||||
|
|
||||||
var r0 []*model.Client
|
|
||||||
if rf, ok := ret.Get(0).(func(uint) []*model.Client); ok {
|
|
||||||
r0 = rf(userID)
|
|
||||||
} else {
|
|
||||||
if ret.Get(0) != nil {
|
|
||||||
r0 = ret.Get(0).([]*model.Client)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return r0
|
|
||||||
}
|
|
||||||
|
|
@ -1,20 +1,18 @@
|
||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"io/ioutil"
|
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
apimock "github.com/gotify/server/api/mock"
|
|
||||||
"github.com/gotify/server/model"
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"github.com/stretchr/testify/mock"
|
|
||||||
"github.com/stretchr/testify/suite"
|
|
||||||
"github.com/gotify/server/mode"
|
"github.com/gotify/server/mode"
|
||||||
|
"github.com/gotify/server/model"
|
||||||
|
"github.com/gotify/server/test"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/suite"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
@ -30,7 +28,7 @@ func TestTokenSuite(t *testing.T) {
|
||||||
|
|
||||||
type TokenSuite struct {
|
type TokenSuite struct {
|
||||||
suite.Suite
|
suite.Suite
|
||||||
db *apimock.MockTokenDatabase
|
db *test.Database
|
||||||
a *TokenAPI
|
a *TokenAPI
|
||||||
ctx *gin.Context
|
ctx *gin.Context
|
||||||
recorder *httptest.ResponseRecorder
|
recorder *httptest.ResponseRecorder
|
||||||
|
|
@ -40,254 +38,246 @@ func (s *TokenSuite) BeforeTest(suiteName, testName string) {
|
||||||
mode.Set(mode.TestDev)
|
mode.Set(mode.TestDev)
|
||||||
rand.Seed(50)
|
rand.Seed(50)
|
||||||
s.recorder = httptest.NewRecorder()
|
s.recorder = httptest.NewRecorder()
|
||||||
|
s.db = test.NewDB(s.T())
|
||||||
s.ctx, _ = gin.CreateTestContext(s.recorder)
|
s.ctx, _ = gin.CreateTestContext(s.recorder)
|
||||||
s.db = &apimock.MockTokenDatabase{}
|
|
||||||
s.a = &TokenAPI{DB: s.db}
|
s.a = &TokenAPI{DB: s.db}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *TokenSuite) AfterTest(suiteName, testName string) {
|
||||||
|
s.db.Close()
|
||||||
|
}
|
||||||
|
|
||||||
// test application api
|
// test application api
|
||||||
|
|
||||||
func (s *TokenSuite) Test_CreateApplication_mapAllParameters() {
|
func (s *TokenSuite) Test_CreateApplication_mapAllParameters() {
|
||||||
expected := &model.Application{Token: firstApplicationToken, UserID: 5, Name: "custom_name", Description: "description_text"}
|
s.db.User(5)
|
||||||
|
|
||||||
s.ctx.Set("user", &model.User{ID: 5})
|
test.WithUser(s.ctx, 5)
|
||||||
s.withFormData("name=custom_name&description=description_text")
|
s.withFormData("name=custom_name&description=description_text")
|
||||||
|
|
||||||
s.db.On("GetApplicationByToken", firstApplicationToken).Return(nil)
|
|
||||||
s.db.On("CreateApplication", expected).Return(nil)
|
|
||||||
|
|
||||||
s.a.CreateApplication(s.ctx)
|
s.a.CreateApplication(s.ctx)
|
||||||
|
|
||||||
s.db.AssertCalled(s.T(), "CreateApplication", expected)
|
expected := &model.Application{ID: 1, Token: firstApplicationToken, UserID: 5, Name: "custom_name", Description: "description_text"}
|
||||||
assert.Equal(s.T(), 200, s.recorder.Code)
|
assert.Equal(s.T(), 200, s.recorder.Code)
|
||||||
|
assert.Equal(s.T(), expected, s.db.GetApplicationByID(1))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *TokenSuite) Test_CreateApplication_expectBadRequestOnEmptyName() {
|
func (s *TokenSuite) Test_CreateApplication_expectBadRequestOnEmptyName() {
|
||||||
s.ctx.Set("user", &model.User{ID: 5})
|
s.db.User(5)
|
||||||
s.withFormData("name=&description=description_text")
|
|
||||||
|
|
||||||
|
test.WithUser(s.ctx, 5)
|
||||||
|
s.withFormData("name=&description=description_text")
|
||||||
s.a.CreateApplication(s.ctx)
|
s.a.CreateApplication(s.ctx)
|
||||||
|
|
||||||
s.db.AssertNotCalled(s.T(), "CreateApplication", mock.Anything)
|
|
||||||
assert.Equal(s.T(), 400, s.recorder.Code)
|
assert.Equal(s.T(), 400, s.recorder.Code)
|
||||||
|
assert.Empty(s.T(), s.db.GetApplicationsByUser(5))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *TokenSuite) Test_DeleteApplication_expectNotFoundOnCurrentUserIsNotOwner() {
|
func (s *TokenSuite) Test_DeleteApplication_expectNotFoundOnCurrentUserIsNotOwner() {
|
||||||
s.ctx.Set("user", &model.User{ID: 2})
|
s.db.User(2)
|
||||||
|
s.db.User(5).App(5)
|
||||||
|
|
||||||
|
test.WithUser(s.ctx, 2)
|
||||||
s.ctx.Request = httptest.NewRequest("DELETE", "/token/5", nil)
|
s.ctx.Request = httptest.NewRequest("DELETE", "/token/5", nil)
|
||||||
s.ctx.Params = gin.Params{{Key: "id", Value: "5"}}
|
s.ctx.Params = gin.Params{{Key: "id", Value: "5"}}
|
||||||
|
|
||||||
s.db.On("GetApplicationByID", uint(5)).Return(&model.Application{ID: 5, Token: firstApplicationToken, UserID: 5})
|
|
||||||
|
|
||||||
s.a.DeleteApplication(s.ctx)
|
s.a.DeleteApplication(s.ctx)
|
||||||
|
|
||||||
s.db.AssertNotCalled(s.T(), "DeleteApplicationByID", mock.Anything)
|
|
||||||
assert.Equal(s.T(), 404, s.recorder.Code)
|
assert.Equal(s.T(), 404, s.recorder.Code)
|
||||||
|
s.db.AssertAppExist(5)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *TokenSuite) Test_CreateApplication_onlyRequiredParameters() {
|
func (s *TokenSuite) Test_CreateApplication_onlyRequiredParameters() {
|
||||||
expected := &model.Application{Token: firstApplicationToken, Name: "custom_name", UserID: 5}
|
s.db.User(5)
|
||||||
|
|
||||||
s.ctx.Set("user", &model.User{ID: 5})
|
test.WithUser(s.ctx, 5)
|
||||||
s.withFormData("name=custom_name")
|
s.withFormData("name=custom_name")
|
||||||
|
|
||||||
s.db.On("GetApplicationByToken", firstApplicationToken).Return(nil)
|
|
||||||
s.db.On("CreateApplication", expected).Return(nil)
|
|
||||||
|
|
||||||
s.a.CreateApplication(s.ctx)
|
s.a.CreateApplication(s.ctx)
|
||||||
|
|
||||||
s.db.AssertCalled(s.T(), "CreateApplication", expected)
|
expected := &model.Application{ID: 1, Token: firstApplicationToken, Name: "custom_name", UserID: 5}
|
||||||
assert.Equal(s.T(), 200, s.recorder.Code)
|
assert.Equal(s.T(), 200, s.recorder.Code)
|
||||||
|
assert.Contains(s.T(), s.db.GetApplicationsByUser(5), expected)
|
||||||
|
}
|
||||||
|
func (s *TokenSuite) Test_ensureApplicationHasCorrectJsonRepresentation() {
|
||||||
|
actual := &model.Application{ID: 1, UserID: 2, Token: "Aasdasfgeeg", Name: "myapp", Description: "mydesc"}
|
||||||
|
test.JSONEquals(s.T(), actual, `{"id":1,"token":"Aasdasfgeeg","name":"myapp","description":"mydesc"}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *TokenSuite) Test_CreateApplication_returnsApplicationWithID() {
|
func (s *TokenSuite) Test_CreateApplication_returnsApplicationWithID() {
|
||||||
expected := &model.Application{Token: firstApplicationToken, Name: "custom_name", UserID: 5}
|
s.db.User(5)
|
||||||
|
|
||||||
s.ctx.Set("user", &model.User{ID: 5})
|
test.WithUser(s.ctx, 5)
|
||||||
s.withFormData("name=custom_name")
|
s.withFormData("name=custom_name")
|
||||||
|
|
||||||
s.db.On("GetApplicationByToken", firstApplicationToken).Return(nil)
|
|
||||||
s.db.On("CreateApplication", expected).Return(nil)
|
|
||||||
|
|
||||||
s.a.CreateApplication(s.ctx)
|
s.a.CreateApplication(s.ctx)
|
||||||
|
|
||||||
|
expected := &model.Application{ID: 1, Token: firstApplicationToken, Name: "custom_name", UserID: 5}
|
||||||
assert.Equal(s.T(), 200, s.recorder.Code)
|
assert.Equal(s.T(), 200, s.recorder.Code)
|
||||||
bytes, _ := ioutil.ReadAll(s.recorder.Body)
|
test.BodyEquals(s.T(), expected, s.recorder)
|
||||||
|
|
||||||
assert.Equal(s.T(), `{"id":0,"token":"APorrUa5b1IIK3y","name":"custom_name","description":""}`, string(bytes))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *TokenSuite) Test_CreateApplication_withExistingToken() {
|
func (s *TokenSuite) Test_CreateApplication_withExistingToken() {
|
||||||
expected := &model.Application{Token: secondApplicationToken, Name: "custom_name", UserID: 5}
|
s.db.User(5)
|
||||||
|
s.db.User(6).AppWithToken(1, firstApplicationToken)
|
||||||
|
|
||||||
s.ctx.Set("user", &model.User{ID: 5})
|
test.WithUser(s.ctx, 5)
|
||||||
s.withFormData("name=custom_name")
|
s.withFormData("name=custom_name")
|
||||||
|
|
||||||
s.db.On("GetApplicationByToken", firstApplicationToken).Return(&model.Application{Token: firstApplicationToken})
|
|
||||||
s.db.On("GetApplicationByToken", secondApplicationToken).Return(nil)
|
|
||||||
s.db.On("CreateApplication", expected).Return(nil)
|
|
||||||
|
|
||||||
s.a.CreateApplication(s.ctx)
|
s.a.CreateApplication(s.ctx)
|
||||||
|
|
||||||
s.db.AssertCalled(s.T(), "CreateApplication", expected)
|
expected := &model.Application{ID: 2, Token: secondApplicationToken, Name: "custom_name", UserID: 5}
|
||||||
assert.Equal(s.T(), 200, s.recorder.Code)
|
assert.Equal(s.T(), 200, s.recorder.Code)
|
||||||
|
assert.Contains(s.T(), s.db.GetApplicationsByUser(5), expected)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *TokenSuite) Test_GetApplications() {
|
func (s *TokenSuite) Test_GetApplications() {
|
||||||
s.ctx.Set("user", &model.User{ID: 5})
|
userBuilder := s.db.User(5)
|
||||||
|
first := userBuilder.NewAppWithToken(1, "perfper")
|
||||||
|
second := userBuilder.NewAppWithToken(2, "asdasd")
|
||||||
|
|
||||||
|
test.WithUser(s.ctx, 5)
|
||||||
s.ctx.Request = httptest.NewRequest("GET", "/tokens", nil)
|
s.ctx.Request = httptest.NewRequest("GET", "/tokens", nil)
|
||||||
|
|
||||||
s.db.On("GetApplicationsByUser", uint(5)).Return([]*model.Application{
|
|
||||||
{Token: "perfper", Name: "first", Description: "desc"},
|
|
||||||
{Token: "asdasd", Name: "second", Description: "desc2"},
|
|
||||||
})
|
|
||||||
s.a.GetApplications(s.ctx)
|
s.a.GetApplications(s.ctx)
|
||||||
|
|
||||||
assert.Equal(s.T(), 200, s.recorder.Code)
|
assert.Equal(s.T(), 200, s.recorder.Code)
|
||||||
bytes, _ := ioutil.ReadAll(s.recorder.Body)
|
test.BodyEquals(s.T(), []*model.Application{first, second}, s.recorder)
|
||||||
|
|
||||||
assert.Equal(s.T(), `[{"id":0,"token":"perfper","name":"first","description":"desc"},{"id":0,"token":"asdasd","name":"second","description":"desc2"}]`, string(bytes))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *TokenSuite) Test_DeleteApplication_expectNotFound() {
|
func (s *TokenSuite) Test_DeleteApplication_expectNotFound() {
|
||||||
s.ctx.Set("user", &model.User{ID: 5})
|
s.db.User(5)
|
||||||
|
|
||||||
|
test.WithUser(s.ctx, 5)
|
||||||
s.ctx.Request = httptest.NewRequest("DELETE", "/token/"+firstApplicationToken, nil)
|
s.ctx.Request = httptest.NewRequest("DELETE", "/token/"+firstApplicationToken, nil)
|
||||||
s.ctx.Params = gin.Params{{Key: "id", Value: "4"}}
|
s.ctx.Params = gin.Params{{Key: "id", Value: "4"}}
|
||||||
|
|
||||||
s.db.On("DeleteApplicationByID", uint(4)).Return(errors.New("what? that does not exist"))
|
|
||||||
s.db.On("GetApplicationByID", uint(4)).Return(nil)
|
|
||||||
|
|
||||||
s.a.DeleteApplication(s.ctx)
|
s.a.DeleteApplication(s.ctx)
|
||||||
|
|
||||||
assert.Equal(s.T(), 404, s.recorder.Code)
|
assert.Equal(s.T(), 404, s.recorder.Code)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *TokenSuite) Test_DeleteApplication() {
|
func (s *TokenSuite) Test_DeleteApplication() {
|
||||||
s.ctx.Set("user", &model.User{ID: 5})
|
s.db.User(5).App(1)
|
||||||
|
|
||||||
|
test.WithUser(s.ctx, 5)
|
||||||
s.ctx.Request = httptest.NewRequest("DELETE", "/token/"+firstApplicationToken, nil)
|
s.ctx.Request = httptest.NewRequest("DELETE", "/token/"+firstApplicationToken, nil)
|
||||||
s.ctx.Params = gin.Params{{Key: "id", Value: "1"}}
|
s.ctx.Params = gin.Params{{Key: "id", Value: "1"}}
|
||||||
|
|
||||||
s.db.On("DeleteApplicationByID", uint(1)).Return(nil)
|
|
||||||
s.db.On("GetApplicationByID", uint(1)).Return(&model.Application{Token: firstApplicationToken, Name: "custom_name", UserID: 5})
|
|
||||||
|
|
||||||
s.a.DeleteApplication(s.ctx)
|
s.a.DeleteApplication(s.ctx)
|
||||||
|
|
||||||
assert.Equal(s.T(), 200, s.recorder.Code)
|
assert.Equal(s.T(), 200, s.recorder.Code)
|
||||||
|
s.db.AssertAppNotExist(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// test client api
|
// test client api
|
||||||
|
|
||||||
|
func (s *TokenSuite) Test_ensureClientHasCorrectJsonRepresentation() {
|
||||||
|
actual := &model.Client{ID: 1, UserID: 2, Token: "Casdasfgeeg", Name: "myclient"}
|
||||||
|
test.JSONEquals(s.T(), actual, `{"id":1,"token":"Casdasfgeeg","name":"myclient"}`)
|
||||||
|
}
|
||||||
|
|
||||||
func (s *TokenSuite) Test_CreateClient_mapAllParameters() {
|
func (s *TokenSuite) Test_CreateClient_mapAllParameters() {
|
||||||
expected := &model.Client{Token: firstClientToken, UserID: 5, Name: "custom_name"}
|
s.db.User(5)
|
||||||
|
|
||||||
s.ctx.Set("user", &model.User{ID: 5})
|
test.WithUser(s.ctx, 5)
|
||||||
s.withFormData("name=custom_name&description=description_text")
|
s.withFormData("name=custom_name&description=description_text")
|
||||||
|
|
||||||
s.db.On("GetClientByToken", firstClientToken).Return(nil)
|
|
||||||
s.db.On("CreateClient", expected).Return(nil)
|
|
||||||
|
|
||||||
s.a.CreateClient(s.ctx)
|
s.a.CreateClient(s.ctx)
|
||||||
|
|
||||||
s.db.AssertCalled(s.T(), "CreateClient", expected)
|
expected := &model.Client{ID: 1, Token: firstClientToken, UserID: 5, Name: "custom_name"}
|
||||||
assert.Equal(s.T(), 200, s.recorder.Code)
|
assert.Equal(s.T(), 200, s.recorder.Code)
|
||||||
|
assert.Contains(s.T(), s.db.GetClientsByUser(5), expected)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *TokenSuite) Test_CreateClient_expectBadRequestOnEmptyName() {
|
func (s *TokenSuite) Test_CreateClient_expectBadRequestOnEmptyName() {
|
||||||
s.ctx.Set("user", &model.User{ID: 5})
|
s.db.User(5)
|
||||||
|
|
||||||
|
test.WithUser(s.ctx, 5)
|
||||||
s.withFormData("name=&description=description_text")
|
s.withFormData("name=&description=description_text")
|
||||||
|
|
||||||
s.a.CreateClient(s.ctx)
|
s.a.CreateClient(s.ctx)
|
||||||
|
|
||||||
s.db.AssertNotCalled(s.T(), "CreateClient", mock.Anything)
|
|
||||||
assert.Equal(s.T(), 400, s.recorder.Code)
|
assert.Equal(s.T(), 400, s.recorder.Code)
|
||||||
|
assert.Empty(s.T(), s.db.GetClientsByUser(5))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *TokenSuite) Test_DeleteClient_expectNotFoundOnCurrentUserIsNotOwner() {
|
func (s *TokenSuite) Test_DeleteClient_expectNotFoundOnCurrentUserIsNotOwner() {
|
||||||
s.ctx.Set("user", &model.User{ID: 2})
|
s.db.User(5).Client(7)
|
||||||
|
s.db.User(2)
|
||||||
|
|
||||||
|
test.WithUser(s.ctx, 2)
|
||||||
s.ctx.Request = httptest.NewRequest("DELETE", "/token/7", nil)
|
s.ctx.Request = httptest.NewRequest("DELETE", "/token/7", nil)
|
||||||
s.ctx.Params = gin.Params{{Key: "id", Value: "7"}}
|
s.ctx.Params = gin.Params{{Key: "id", Value: "7"}}
|
||||||
|
|
||||||
s.db.On("GetClientByID", uint(7)).Return(&model.Client{Token: firstClientToken, UserID: 5})
|
|
||||||
|
|
||||||
s.a.DeleteClient(s.ctx)
|
s.a.DeleteClient(s.ctx)
|
||||||
|
|
||||||
s.db.AssertNotCalled(s.T(), "DeleteClientByID", mock.Anything)
|
|
||||||
assert.Equal(s.T(), 404, s.recorder.Code)
|
assert.Equal(s.T(), 404, s.recorder.Code)
|
||||||
|
s.db.AssertClientExist(7)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *TokenSuite) Test_CreateClient_returnsClientWithID() {
|
func (s *TokenSuite) Test_CreateClient_returnsClientWithID() {
|
||||||
expected := &model.Client{Token: firstClientToken, Name: "custom_name", UserID: 5}
|
s.db.User(5)
|
||||||
|
|
||||||
s.ctx.Set("user", &model.User{ID: 5})
|
test.WithUser(s.ctx, 5)
|
||||||
s.withFormData("name=custom_name")
|
s.withFormData("name=custom_name")
|
||||||
|
|
||||||
s.db.On("GetClientByToken", firstClientToken).Return(nil)
|
|
||||||
s.db.On("CreateClient", expected).Return(nil)
|
|
||||||
|
|
||||||
s.a.CreateClient(s.ctx)
|
s.a.CreateClient(s.ctx)
|
||||||
|
|
||||||
|
expected := &model.Client{ID: 1, Token: firstClientToken, Name: "custom_name", UserID: 5}
|
||||||
assert.Equal(s.T(), 200, s.recorder.Code)
|
assert.Equal(s.T(), 200, s.recorder.Code)
|
||||||
bytes, _ := ioutil.ReadAll(s.recorder.Body)
|
test.BodyEquals(s.T(), expected, s.recorder)
|
||||||
|
|
||||||
assert.Equal(s.T(), `{"id":0,"token":"CPorrUa5b1IIK3y","name":"custom_name"}`, string(bytes))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *TokenSuite) Test_CreateClient_withExistingToken() {
|
func (s *TokenSuite) Test_CreateClient_withExistingToken() {
|
||||||
expected := &model.Client{Token: secondClientToken, Name: "custom_name", UserID: 5}
|
s.db.User(5).ClientWithToken(1, firstClientToken)
|
||||||
|
|
||||||
s.ctx.Set("user", &model.User{ID: 5})
|
test.WithUser(s.ctx, 5)
|
||||||
s.withFormData("name=custom_name")
|
s.withFormData("name=custom_name")
|
||||||
|
|
||||||
s.db.On("GetClientByToken", firstClientToken).Return(&model.Client{Token: firstClientToken})
|
|
||||||
s.db.On("GetClientByToken", secondClientToken).Return(nil)
|
|
||||||
s.db.On("CreateClient", expected).Return(nil)
|
|
||||||
|
|
||||||
s.a.CreateClient(s.ctx)
|
s.a.CreateClient(s.ctx)
|
||||||
|
|
||||||
s.db.AssertCalled(s.T(), "CreateClient", expected)
|
expected := &model.Client{ID: 2, Token: secondClientToken, Name: "custom_name", UserID: 5}
|
||||||
assert.Equal(s.T(), 200, s.recorder.Code)
|
assert.Equal(s.T(), 200, s.recorder.Code)
|
||||||
|
test.BodyEquals(s.T(), expected, s.recorder)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *TokenSuite) Test_GetClients() {
|
func (s *TokenSuite) Test_GetClients() {
|
||||||
s.ctx.Set("user", &model.User{ID: 5})
|
userBuilder := s.db.User(5)
|
||||||
|
first := userBuilder.NewClientWithToken(1, "perfper")
|
||||||
|
second := userBuilder.NewClientWithToken(2, "asdasd")
|
||||||
|
|
||||||
|
test.WithUser(s.ctx, 5)
|
||||||
s.ctx.Request = httptest.NewRequest("GET", "/tokens", nil)
|
s.ctx.Request = httptest.NewRequest("GET", "/tokens", nil)
|
||||||
|
|
||||||
s.db.On("GetClientsByUser", uint(5)).Return([]*model.Client{
|
|
||||||
{Token: "perfper", Name: "first"},
|
|
||||||
{Token: "asdasd", Name: "second"},
|
|
||||||
})
|
|
||||||
s.a.GetClients(s.ctx)
|
s.a.GetClients(s.ctx)
|
||||||
|
|
||||||
assert.Equal(s.T(), 200, s.recorder.Code)
|
assert.Equal(s.T(), 200, s.recorder.Code)
|
||||||
bytes, _ := ioutil.ReadAll(s.recorder.Body)
|
test.BodyEquals(s.T(), []*model.Client{first, second}, s.recorder)
|
||||||
|
|
||||||
assert.Equal(s.T(), `[{"id":0,"token":"perfper","name":"first"},{"id":0,"token":"asdasd","name":"second"}]`, string(bytes))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *TokenSuite) Test_DeleteClient_expectNotFound() {
|
func (s *TokenSuite) Test_DeleteClient_expectNotFound() {
|
||||||
s.ctx.Set("user", &model.User{ID: 5})
|
s.db.User(5)
|
||||||
|
|
||||||
|
test.WithUser(s.ctx, 5)
|
||||||
s.ctx.Request = httptest.NewRequest("DELETE", "/token/"+firstClientToken, nil)
|
s.ctx.Request = httptest.NewRequest("DELETE", "/token/"+firstClientToken, nil)
|
||||||
s.ctx.Params = gin.Params{{Key: "id", Value: "8"}}
|
s.ctx.Params = gin.Params{{Key: "id", Value: "8"}}
|
||||||
|
|
||||||
s.db.On("DeleteClientByID", uint(8)).Return(errors.New("what? that does not exist"))
|
|
||||||
s.db.On("GetClientByID", uint(8)).Return(nil)
|
|
||||||
|
|
||||||
s.a.DeleteClient(s.ctx)
|
s.a.DeleteClient(s.ctx)
|
||||||
|
|
||||||
assert.Equal(s.T(), 404, s.recorder.Code)
|
assert.Equal(s.T(), 404, s.recorder.Code)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//
|
||||||
func (s *TokenSuite) Test_DeleteClient() {
|
func (s *TokenSuite) Test_DeleteClient() {
|
||||||
s.ctx.Set("user", &model.User{ID: 5})
|
s.db.User(5).Client(8)
|
||||||
|
|
||||||
|
test.WithUser(s.ctx, 5)
|
||||||
s.ctx.Request = httptest.NewRequest("DELETE", "/token/"+firstClientToken, nil)
|
s.ctx.Request = httptest.NewRequest("DELETE", "/token/"+firstClientToken, nil)
|
||||||
s.ctx.Params = gin.Params{{Key: "id", Value: "8"}}
|
s.ctx.Params = gin.Params{{Key: "id", Value: "8"}}
|
||||||
|
|
||||||
s.db.On("DeleteClientByID", uint(8)).Return(nil)
|
|
||||||
s.db.On("GetClientByID", uint(8)).Return(&model.Client{Token: firstClientToken, Name: "custom_name", UserID: 5})
|
|
||||||
|
|
||||||
s.a.DeleteClient(s.ctx)
|
s.a.DeleteClient(s.ctx)
|
||||||
|
|
||||||
assert.Equal(s.T(), 200, s.recorder.Code)
|
assert.Equal(s.T(), 200, s.recorder.Code)
|
||||||
|
s.db.AssertClientNotExist(8)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *TokenSuite) withFormData(formData string) {
|
func (s *TokenSuite) withFormData(formData string) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue