Adjust token api to use new app/client id
This commit is contained in:
parent
6dda2606d4
commit
ec1eb50d0e
|
|
@ -1,8 +1,8 @@
|
||||||
// Code generated by mockery v1.0.0
|
// Code generated by mockery v1.0.0
|
||||||
package mock
|
package mock
|
||||||
|
|
||||||
import mock "github.com/stretchr/testify/mock"
|
import "github.com/stretchr/testify/mock"
|
||||||
import model "github.com/gotify/server/model"
|
import "github.com/gotify/server/model"
|
||||||
|
|
||||||
// MockTokenDatabase is an autogenerated mock type for the TokenDatabase type
|
// MockTokenDatabase is an autogenerated mock type for the TokenDatabase type
|
||||||
type MockTokenDatabase struct {
|
type MockTokenDatabase struct {
|
||||||
|
|
@ -38,11 +38,11 @@ func (_m *MockTokenDatabase) CreateClient(client *model.Client) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteApplicationByID provides a mock function with given fields: id
|
// DeleteApplicationByID provides a mock function with given fields: id
|
||||||
func (_m *MockTokenDatabase) DeleteApplicationByID(id string) error {
|
func (_m *MockTokenDatabase) DeleteApplicationByID(id uint) error {
|
||||||
ret := _m.Called(id)
|
ret := _m.Called(id)
|
||||||
|
|
||||||
var r0 error
|
var r0 error
|
||||||
if rf, ok := ret.Get(0).(func(string) error); ok {
|
if rf, ok := ret.Get(0).(func(uint) error); ok {
|
||||||
r0 = rf(id)
|
r0 = rf(id)
|
||||||
} else {
|
} else {
|
||||||
r0 = ret.Error(0)
|
r0 = ret.Error(0)
|
||||||
|
|
@ -52,11 +52,11 @@ func (_m *MockTokenDatabase) DeleteApplicationByID(id string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteClientByID provides a mock function with given fields: id
|
// DeleteClientByID provides a mock function with given fields: id
|
||||||
func (_m *MockTokenDatabase) DeleteClientByID(id string) error {
|
func (_m *MockTokenDatabase) DeleteClientByID(id uint) error {
|
||||||
ret := _m.Called(id)
|
ret := _m.Called(id)
|
||||||
|
|
||||||
var r0 error
|
var r0 error
|
||||||
if rf, ok := ret.Get(0).(func(string) error); ok {
|
if rf, ok := ret.Get(0).(func(uint) error); ok {
|
||||||
r0 = rf(id)
|
r0 = rf(id)
|
||||||
} else {
|
} else {
|
||||||
r0 = ret.Error(0)
|
r0 = ret.Error(0)
|
||||||
|
|
@ -66,11 +66,11 @@ func (_m *MockTokenDatabase) DeleteClientByID(id string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetApplicationByID provides a mock function with given fields: id
|
// GetApplicationByID provides a mock function with given fields: id
|
||||||
func (_m *MockTokenDatabase) GetApplicationByID(id string) *model.Application {
|
func (_m *MockTokenDatabase) GetApplicationByID(id uint) *model.Application {
|
||||||
ret := _m.Called(id)
|
ret := _m.Called(id)
|
||||||
|
|
||||||
var r0 *model.Application
|
var r0 *model.Application
|
||||||
if rf, ok := ret.Get(0).(func(string) *model.Application); ok {
|
if rf, ok := ret.Get(0).(func(uint) *model.Application); ok {
|
||||||
r0 = rf(id)
|
r0 = rf(id)
|
||||||
} else {
|
} else {
|
||||||
if ret.Get(0) != nil {
|
if ret.Get(0) != nil {
|
||||||
|
|
@ -81,6 +81,22 @@ func (_m *MockTokenDatabase) GetApplicationByID(id string) *model.Application {
|
||||||
return r0
|
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
|
// GetApplicationsByUser provides a mock function with given fields: userID
|
||||||
func (_m *MockTokenDatabase) GetApplicationsByUser(userID uint) []*model.Application {
|
func (_m *MockTokenDatabase) GetApplicationsByUser(userID uint) []*model.Application {
|
||||||
ret := _m.Called(userID)
|
ret := _m.Called(userID)
|
||||||
|
|
@ -98,11 +114,11 @@ func (_m *MockTokenDatabase) GetApplicationsByUser(userID uint) []*model.Applica
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetClientByID provides a mock function with given fields: id
|
// GetClientByID provides a mock function with given fields: id
|
||||||
func (_m *MockTokenDatabase) GetClientByID(id string) *model.Client {
|
func (_m *MockTokenDatabase) GetClientByID(id uint) *model.Client {
|
||||||
ret := _m.Called(id)
|
ret := _m.Called(id)
|
||||||
|
|
||||||
var r0 *model.Client
|
var r0 *model.Client
|
||||||
if rf, ok := ret.Get(0).(func(string) *model.Client); ok {
|
if rf, ok := ret.Get(0).(func(uint) *model.Client); ok {
|
||||||
r0 = rf(id)
|
r0 = rf(id)
|
||||||
} else {
|
} else {
|
||||||
if ret.Get(0) != nil {
|
if ret.Get(0) != nil {
|
||||||
|
|
@ -113,6 +129,22 @@ func (_m *MockTokenDatabase) GetClientByID(id string) *model.Client {
|
||||||
return r0
|
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
|
// GetClientsByUser provides a mock function with given fields: userID
|
||||||
func (_m *MockTokenDatabase) GetClientsByUser(userID uint) []*model.Client {
|
func (_m *MockTokenDatabase) GetClientsByUser(userID uint) []*model.Client {
|
||||||
ret := _m.Called(userID)
|
ret := _m.Called(userID)
|
||||||
|
|
|
||||||
40
api/token.go
40
api/token.go
|
|
@ -11,14 +11,16 @@ import (
|
||||||
// The TokenDatabase interface for encapsulating database access.
|
// The TokenDatabase interface for encapsulating database access.
|
||||||
type TokenDatabase interface {
|
type TokenDatabase interface {
|
||||||
CreateApplication(application *model.Application) error
|
CreateApplication(application *model.Application) error
|
||||||
GetApplicationByID(id string) *model.Application
|
GetApplicationByToken(token string) *model.Application
|
||||||
|
GetApplicationByID(id uint) *model.Application
|
||||||
GetApplicationsByUser(userID uint) []*model.Application
|
GetApplicationsByUser(userID uint) []*model.Application
|
||||||
DeleteApplicationByID(id string) error
|
DeleteApplicationByID(id uint) error
|
||||||
|
|
||||||
CreateClient(client *model.Client) error
|
CreateClient(client *model.Client) error
|
||||||
GetClientByID(id string) *model.Client
|
GetClientByToken(token string) *model.Client
|
||||||
|
GetClientByID(id uint) *model.Client
|
||||||
GetClientsByUser(userID uint) []*model.Client
|
GetClientsByUser(userID uint) []*model.Client
|
||||||
DeleteClientByID(id string) error
|
DeleteClientByID(id uint) error
|
||||||
}
|
}
|
||||||
|
|
||||||
// The TokenAPI provides handlers for managing clients and applications.
|
// The TokenAPI provides handlers for managing clients and applications.
|
||||||
|
|
@ -30,7 +32,7 @@ type TokenAPI struct {
|
||||||
func (a *TokenAPI) CreateApplication(ctx *gin.Context) {
|
func (a *TokenAPI) CreateApplication(ctx *gin.Context) {
|
||||||
app := model.Application{}
|
app := model.Application{}
|
||||||
if err := ctx.Bind(&app); err == nil {
|
if err := ctx.Bind(&app); err == nil {
|
||||||
app.ID = generateNotExistingToken(auth.GenerateApplicationToken, a.applicationExists)
|
app.Token = generateNotExistingToken(auth.GenerateApplicationToken, a.applicationExists)
|
||||||
app.UserID = auth.GetUserID(ctx)
|
app.UserID = auth.GetUserID(ctx)
|
||||||
a.DB.CreateApplication(&app)
|
a.DB.CreateApplication(&app)
|
||||||
ctx.JSON(200, app)
|
ctx.JSON(200, app)
|
||||||
|
|
@ -41,7 +43,7 @@ func (a *TokenAPI) CreateApplication(ctx *gin.Context) {
|
||||||
func (a *TokenAPI) CreateClient(ctx *gin.Context) {
|
func (a *TokenAPI) CreateClient(ctx *gin.Context) {
|
||||||
client := model.Client{}
|
client := model.Client{}
|
||||||
if err := ctx.Bind(&client); err == nil {
|
if err := ctx.Bind(&client); err == nil {
|
||||||
client.ID = generateNotExistingToken(auth.GenerateClientToken, a.clientExists)
|
client.Token = generateNotExistingToken(auth.GenerateClientToken, a.clientExists)
|
||||||
client.UserID = auth.GetUserID(ctx)
|
client.UserID = auth.GetUserID(ctx)
|
||||||
a.DB.CreateClient(&client)
|
a.DB.CreateClient(&client)
|
||||||
ctx.JSON(200, client)
|
ctx.JSON(200, client)
|
||||||
|
|
@ -64,30 +66,32 @@ func (a *TokenAPI) GetClients(ctx *gin.Context) {
|
||||||
|
|
||||||
// DeleteApplication deletes an application by its id.
|
// DeleteApplication deletes an application by its id.
|
||||||
func (a *TokenAPI) DeleteApplication(ctx *gin.Context) {
|
func (a *TokenAPI) DeleteApplication(ctx *gin.Context) {
|
||||||
appID := ctx.Param("id")
|
withID(ctx, "id", func(id uint) {
|
||||||
if app := a.DB.GetApplicationByID(appID); app != nil && app.UserID == auth.GetUserID(ctx) {
|
if app := a.DB.GetApplicationByID(id); app != nil && app.UserID == auth.GetUserID(ctx) {
|
||||||
a.DB.DeleteApplicationByID(appID)
|
a.DB.DeleteApplicationByID(id)
|
||||||
} else {
|
} else {
|
||||||
ctx.AbortWithError(404, fmt.Errorf("app with id %s doesn't exists", appID))
|
ctx.AbortWithError(404, fmt.Errorf("app with id %d doesn't exists", id))
|
||||||
}
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteClient deletes a client by its id.
|
// DeleteClient deletes a client by its id.
|
||||||
func (a *TokenAPI) DeleteClient(ctx *gin.Context) {
|
func (a *TokenAPI) DeleteClient(ctx *gin.Context) {
|
||||||
clientID := ctx.Param("id")
|
withID(ctx, "id", func(id uint) {
|
||||||
if client := a.DB.GetClientByID(clientID); client != nil && client.UserID == auth.GetUserID(ctx) {
|
if client := a.DB.GetClientByID(id); client != nil && client.UserID == auth.GetUserID(ctx) {
|
||||||
a.DB.DeleteClientByID(clientID)
|
a.DB.DeleteClientByID(id)
|
||||||
} else {
|
} else {
|
||||||
ctx.AbortWithError(404, fmt.Errorf("client with id %s doesn't exists", clientID))
|
ctx.AbortWithError(404, fmt.Errorf("client with id %d doesn't exists", id))
|
||||||
}
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *TokenAPI) applicationExists(appID string) bool {
|
func (a *TokenAPI) applicationExists(token string) bool {
|
||||||
return a.DB.GetApplicationByID(appID) != nil
|
return a.DB.GetApplicationByToken(token) != nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *TokenAPI) clientExists(clientID string) bool {
|
func (a *TokenAPI) clientExists(token string) bool {
|
||||||
return a.DB.GetClientByID(clientID) != nil
|
return a.DB.GetClientByToken(token) != nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateNotExistingToken(generateToken func() string, tokenExists func(token string) bool) string {
|
func generateNotExistingToken(generateToken func() string, tokenExists func(token string) bool) string {
|
||||||
|
|
|
||||||
|
|
@ -47,12 +47,12 @@ func (s *TokenSuite) BeforeTest(suiteName, testName string) {
|
||||||
// test application api
|
// test application api
|
||||||
|
|
||||||
func (s *TokenSuite) Test_CreateApplication_mapAllParameters() {
|
func (s *TokenSuite) Test_CreateApplication_mapAllParameters() {
|
||||||
expected := &model.Application{ID: firstApplicationToken, UserID: 5, Name: "custom_name", Description: "description_text"}
|
expected := &model.Application{Token: firstApplicationToken, UserID: 5, Name: "custom_name", Description: "description_text"}
|
||||||
|
|
||||||
s.ctx.Set("user", &model.User{ID: 5})
|
s.ctx.Set("user", &model.User{ID: 5})
|
||||||
s.withFormData("name=custom_name&description=description_text")
|
s.withFormData("name=custom_name&description=description_text")
|
||||||
|
|
||||||
s.db.On("GetApplicationByID", firstApplicationToken).Return(nil)
|
s.db.On("GetApplicationByToken", firstApplicationToken).Return(nil)
|
||||||
s.db.On("CreateApplication", expected).Return(nil)
|
s.db.On("CreateApplication", expected).Return(nil)
|
||||||
|
|
||||||
s.a.CreateApplication(s.ctx)
|
s.a.CreateApplication(s.ctx)
|
||||||
|
|
@ -73,10 +73,10 @@ func (s *TokenSuite) Test_CreateApplication_expectBadRequestOnEmptyName() {
|
||||||
|
|
||||||
func (s *TokenSuite) Test_DeleteApplication_expectNotFoundOnCurrentUserIsNotOwner() {
|
func (s *TokenSuite) Test_DeleteApplication_expectNotFoundOnCurrentUserIsNotOwner() {
|
||||||
s.ctx.Set("user", &model.User{ID: 2})
|
s.ctx.Set("user", &model.User{ID: 2})
|
||||||
s.ctx.Request = httptest.NewRequest("DELETE", "/token/"+firstApplicationToken, nil)
|
s.ctx.Request = httptest.NewRequest("DELETE", "/token/5", nil)
|
||||||
s.ctx.Params = gin.Params{{Key: "id", Value: firstApplicationToken}}
|
s.ctx.Params = gin.Params{{Key: "id", Value: "5"}}
|
||||||
|
|
||||||
s.db.On("GetApplicationByID", firstApplicationToken).Return(&model.Application{ID: firstApplicationToken, UserID: 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)
|
||||||
|
|
||||||
|
|
@ -85,12 +85,12 @@ func (s *TokenSuite) Test_DeleteApplication_expectNotFoundOnCurrentUserIsNotOwne
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *TokenSuite) Test_CreateApplication_onlyRequiredParameters() {
|
func (s *TokenSuite) Test_CreateApplication_onlyRequiredParameters() {
|
||||||
expected := &model.Application{ID: firstApplicationToken, Name: "custom_name", UserID: 5}
|
expected := &model.Application{Token: firstApplicationToken, Name: "custom_name", UserID: 5}
|
||||||
|
|
||||||
s.ctx.Set("user", &model.User{ID: 5})
|
s.ctx.Set("user", &model.User{ID: 5})
|
||||||
s.withFormData("name=custom_name")
|
s.withFormData("name=custom_name")
|
||||||
|
|
||||||
s.db.On("GetApplicationByID", firstApplicationToken).Return(nil)
|
s.db.On("GetApplicationByToken", firstApplicationToken).Return(nil)
|
||||||
s.db.On("CreateApplication", expected).Return(nil)
|
s.db.On("CreateApplication", expected).Return(nil)
|
||||||
|
|
||||||
s.a.CreateApplication(s.ctx)
|
s.a.CreateApplication(s.ctx)
|
||||||
|
|
@ -100,12 +100,12 @@ func (s *TokenSuite) Test_CreateApplication_onlyRequiredParameters() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *TokenSuite) Test_CreateApplication_returnsApplicationWithID() {
|
func (s *TokenSuite) Test_CreateApplication_returnsApplicationWithID() {
|
||||||
expected := &model.Application{ID: firstApplicationToken, Name: "custom_name", UserID: 5}
|
expected := &model.Application{Token: firstApplicationToken, Name: "custom_name", UserID: 5}
|
||||||
|
|
||||||
s.ctx.Set("user", &model.User{ID: 5})
|
s.ctx.Set("user", &model.User{ID: 5})
|
||||||
s.withFormData("name=custom_name")
|
s.withFormData("name=custom_name")
|
||||||
|
|
||||||
s.db.On("GetApplicationByID", firstApplicationToken).Return(nil)
|
s.db.On("GetApplicationByToken", firstApplicationToken).Return(nil)
|
||||||
s.db.On("CreateApplication", expected).Return(nil)
|
s.db.On("CreateApplication", expected).Return(nil)
|
||||||
|
|
||||||
s.a.CreateApplication(s.ctx)
|
s.a.CreateApplication(s.ctx)
|
||||||
|
|
@ -113,17 +113,17 @@ func (s *TokenSuite) Test_CreateApplication_returnsApplicationWithID() {
|
||||||
assert.Equal(s.T(), 200, s.recorder.Code)
|
assert.Equal(s.T(), 200, s.recorder.Code)
|
||||||
bytes, _ := ioutil.ReadAll(s.recorder.Body)
|
bytes, _ := ioutil.ReadAll(s.recorder.Body)
|
||||||
|
|
||||||
assert.Equal(s.T(), `{"id":"APorrUa5b1IIK3y","name":"custom_name","description":""}`, string(bytes))
|
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{ID: secondApplicationToken, Name: "custom_name", UserID: 5}
|
expected := &model.Application{Token: secondApplicationToken, Name: "custom_name", UserID: 5}
|
||||||
|
|
||||||
s.ctx.Set("user", &model.User{ID: 5})
|
s.ctx.Set("user", &model.User{ID: 5})
|
||||||
s.withFormData("name=custom_name")
|
s.withFormData("name=custom_name")
|
||||||
|
|
||||||
s.db.On("GetApplicationByID", firstApplicationToken).Return(&model.Application{ID: firstApplicationToken})
|
s.db.On("GetApplicationByToken", firstApplicationToken).Return(&model.Application{Token: firstApplicationToken})
|
||||||
s.db.On("GetApplicationByID", secondApplicationToken).Return(nil)
|
s.db.On("GetApplicationByToken", secondApplicationToken).Return(nil)
|
||||||
s.db.On("CreateApplication", expected).Return(nil)
|
s.db.On("CreateApplication", expected).Return(nil)
|
||||||
|
|
||||||
s.a.CreateApplication(s.ctx)
|
s.a.CreateApplication(s.ctx)
|
||||||
|
|
@ -137,24 +137,24 @@ func (s *TokenSuite) Test_GetApplications() {
|
||||||
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{
|
s.db.On("GetApplicationsByUser", uint(5)).Return([]*model.Application{
|
||||||
{ID: "perfper", Name: "first", Description: "desc"},
|
{Token: "perfper", Name: "first", Description: "desc"},
|
||||||
{ID: "asdasd", Name: "second", Description: "desc2"},
|
{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)
|
bytes, _ := ioutil.ReadAll(s.recorder.Body)
|
||||||
|
|
||||||
assert.Equal(s.T(), `[{"id":"perfper","name":"first","description":"desc"},{"id":"asdasd","name":"second","description":"desc2"}]`, string(bytes))
|
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.ctx.Set("user", &model.User{ID: 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: firstApplicationToken}}
|
s.ctx.Params = gin.Params{{Key: "id", Value: "4"}}
|
||||||
|
|
||||||
s.db.On("DeleteApplicationByID", firstApplicationToken).Return(errors.New("what? that does not exist"))
|
s.db.On("DeleteApplicationByID", uint(4)).Return(errors.New("what? that does not exist"))
|
||||||
s.db.On("GetApplicationByID", firstApplicationToken).Return(nil)
|
s.db.On("GetApplicationByID", uint(4)).Return(nil)
|
||||||
|
|
||||||
s.a.DeleteApplication(s.ctx)
|
s.a.DeleteApplication(s.ctx)
|
||||||
|
|
||||||
|
|
@ -164,10 +164,10 @@ func (s *TokenSuite) Test_DeleteApplication_expectNotFound() {
|
||||||
func (s *TokenSuite) Test_DeleteApplication() {
|
func (s *TokenSuite) Test_DeleteApplication() {
|
||||||
s.ctx.Set("user", &model.User{ID: 5})
|
s.ctx.Set("user", &model.User{ID: 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: firstApplicationToken}}
|
s.ctx.Params = gin.Params{{Key: "id", Value: "1"}}
|
||||||
|
|
||||||
s.db.On("DeleteApplicationByID", firstApplicationToken).Return(nil)
|
s.db.On("DeleteApplicationByID", uint(1)).Return(nil)
|
||||||
s.db.On("GetApplicationByID", firstApplicationToken).Return(&model.Application{ID: firstApplicationToken, Name: "custom_name", UserID: 5})
|
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)
|
||||||
|
|
||||||
|
|
@ -177,12 +177,12 @@ func (s *TokenSuite) Test_DeleteApplication() {
|
||||||
// test client api
|
// test client api
|
||||||
|
|
||||||
func (s *TokenSuite) Test_CreateClient_mapAllParameters() {
|
func (s *TokenSuite) Test_CreateClient_mapAllParameters() {
|
||||||
expected := &model.Client{ID: firstClientToken, UserID: 5, Name: "custom_name"}
|
expected := &model.Client{Token: firstClientToken, UserID: 5, Name: "custom_name"}
|
||||||
|
|
||||||
s.ctx.Set("user", &model.User{ID: 5})
|
s.ctx.Set("user", &model.User{ID: 5})
|
||||||
s.withFormData("name=custom_name&description=description_text")
|
s.withFormData("name=custom_name&description=description_text")
|
||||||
|
|
||||||
s.db.On("GetClientByID", firstClientToken).Return(nil)
|
s.db.On("GetClientByToken", firstClientToken).Return(nil)
|
||||||
s.db.On("CreateClient", expected).Return(nil)
|
s.db.On("CreateClient", expected).Return(nil)
|
||||||
|
|
||||||
s.a.CreateClient(s.ctx)
|
s.a.CreateClient(s.ctx)
|
||||||
|
|
@ -203,10 +203,10 @@ func (s *TokenSuite) Test_CreateClient_expectBadRequestOnEmptyName() {
|
||||||
|
|
||||||
func (s *TokenSuite) Test_DeleteClient_expectNotFoundOnCurrentUserIsNotOwner() {
|
func (s *TokenSuite) Test_DeleteClient_expectNotFoundOnCurrentUserIsNotOwner() {
|
||||||
s.ctx.Set("user", &model.User{ID: 2})
|
s.ctx.Set("user", &model.User{ID: 2})
|
||||||
s.ctx.Request = httptest.NewRequest("DELETE", "/token/"+firstClientToken, nil)
|
s.ctx.Request = httptest.NewRequest("DELETE", "/token/7", nil)
|
||||||
s.ctx.Params = gin.Params{{Key: "id", Value: firstClientToken}}
|
s.ctx.Params = gin.Params{{Key: "id", Value: "7"}}
|
||||||
|
|
||||||
s.db.On("GetClientByID", firstClientToken).Return(&model.Client{ID: firstClientToken, UserID: 5})
|
s.db.On("GetClientByID", uint(7)).Return(&model.Client{Token: firstClientToken, UserID: 5})
|
||||||
|
|
||||||
s.a.DeleteClient(s.ctx)
|
s.a.DeleteClient(s.ctx)
|
||||||
|
|
||||||
|
|
@ -215,12 +215,12 @@ func (s *TokenSuite) Test_DeleteClient_expectNotFoundOnCurrentUserIsNotOwner() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *TokenSuite) Test_CreateClient_returnsClientWithID() {
|
func (s *TokenSuite) Test_CreateClient_returnsClientWithID() {
|
||||||
expected := &model.Client{ID: firstClientToken, Name: "custom_name", UserID: 5}
|
expected := &model.Client{Token: firstClientToken, Name: "custom_name", UserID: 5}
|
||||||
|
|
||||||
s.ctx.Set("user", &model.User{ID: 5})
|
s.ctx.Set("user", &model.User{ID: 5})
|
||||||
s.withFormData("name=custom_name")
|
s.withFormData("name=custom_name")
|
||||||
|
|
||||||
s.db.On("GetClientByID", firstClientToken).Return(nil)
|
s.db.On("GetClientByToken", firstClientToken).Return(nil)
|
||||||
s.db.On("CreateClient", expected).Return(nil)
|
s.db.On("CreateClient", expected).Return(nil)
|
||||||
|
|
||||||
s.a.CreateClient(s.ctx)
|
s.a.CreateClient(s.ctx)
|
||||||
|
|
@ -228,17 +228,17 @@ func (s *TokenSuite) Test_CreateClient_returnsClientWithID() {
|
||||||
assert.Equal(s.T(), 200, s.recorder.Code)
|
assert.Equal(s.T(), 200, s.recorder.Code)
|
||||||
bytes, _ := ioutil.ReadAll(s.recorder.Body)
|
bytes, _ := ioutil.ReadAll(s.recorder.Body)
|
||||||
|
|
||||||
assert.Equal(s.T(), `{"id":"CPorrUa5b1IIK3y","name":"custom_name"}`, string(bytes))
|
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{ID: secondClientToken, Name: "custom_name", UserID: 5}
|
expected := &model.Client{Token: secondClientToken, Name: "custom_name", UserID: 5}
|
||||||
|
|
||||||
s.ctx.Set("user", &model.User{ID: 5})
|
s.ctx.Set("user", &model.User{ID: 5})
|
||||||
s.withFormData("name=custom_name")
|
s.withFormData("name=custom_name")
|
||||||
|
|
||||||
s.db.On("GetClientByID", firstClientToken).Return(&model.Client{ID: firstClientToken})
|
s.db.On("GetClientByToken", firstClientToken).Return(&model.Client{Token: firstClientToken})
|
||||||
s.db.On("GetClientByID", secondClientToken).Return(nil)
|
s.db.On("GetClientByToken", secondClientToken).Return(nil)
|
||||||
s.db.On("CreateClient", expected).Return(nil)
|
s.db.On("CreateClient", expected).Return(nil)
|
||||||
|
|
||||||
s.a.CreateClient(s.ctx)
|
s.a.CreateClient(s.ctx)
|
||||||
|
|
@ -252,24 +252,24 @@ func (s *TokenSuite) Test_GetClients() {
|
||||||
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{
|
s.db.On("GetClientsByUser", uint(5)).Return([]*model.Client{
|
||||||
{ID: "perfper", Name: "first"},
|
{Token: "perfper", Name: "first"},
|
||||||
{ID: "asdasd", Name: "second"},
|
{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)
|
bytes, _ := ioutil.ReadAll(s.recorder.Body)
|
||||||
|
|
||||||
assert.Equal(s.T(), `[{"id":"perfper","name":"first"},{"id":"asdasd","name":"second"}]`, string(bytes))
|
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.ctx.Set("user", &model.User{ID: 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: firstClientToken}}
|
s.ctx.Params = gin.Params{{Key: "id", Value: "8"}}
|
||||||
|
|
||||||
s.db.On("DeleteClientByID", firstClientToken).Return(errors.New("what? that does not exist"))
|
s.db.On("DeleteClientByID", uint(8)).Return(errors.New("what? that does not exist"))
|
||||||
s.db.On("GetClientByID", firstClientToken).Return(nil)
|
s.db.On("GetClientByID", uint(8)).Return(nil)
|
||||||
|
|
||||||
s.a.DeleteClient(s.ctx)
|
s.a.DeleteClient(s.ctx)
|
||||||
|
|
||||||
|
|
@ -279,10 +279,10 @@ func (s *TokenSuite) Test_DeleteClient_expectNotFound() {
|
||||||
func (s *TokenSuite) Test_DeleteClient() {
|
func (s *TokenSuite) Test_DeleteClient() {
|
||||||
s.ctx.Set("user", &model.User{ID: 5})
|
s.ctx.Set("user", &model.User{ID: 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: firstClientToken}}
|
s.ctx.Params = gin.Params{{Key: "id", Value: "8"}}
|
||||||
|
|
||||||
s.db.On("DeleteClientByID", firstClientToken).Return(nil)
|
s.db.On("DeleteClientByID", uint(8)).Return(nil)
|
||||||
s.db.On("GetClientByID", firstClientToken).Return(&model.Client{ID: firstClientToken, Name: "custom_name", UserID: 5})
|
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)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue