Use *ByToken methods in auth
This commit is contained in:
parent
6aa10ad917
commit
433a83823b
|
|
@ -13,8 +13,8 @@ const (
|
|||
|
||||
// The Database interface for encapsulating database access.
|
||||
type Database interface {
|
||||
GetApplicationByID(id string) *model.Application
|
||||
GetClientByID(id string) *model.Client
|
||||
GetApplicationByToken(token string) *model.Application
|
||||
GetClientByToken(token string) *model.Client
|
||||
GetUserByName(name string) *model.User
|
||||
GetUserByID(id uint) *model.User
|
||||
}
|
||||
|
|
@ -33,7 +33,7 @@ func (a *Auth) RequireAdmin() gin.HandlerFunc {
|
|||
if user != nil {
|
||||
return true, user.Admin, user.ID
|
||||
}
|
||||
if token := a.DB.GetClientByID(tokenID); token != nil {
|
||||
if token := a.DB.GetClientByToken(tokenID); token != nil {
|
||||
return true, a.DB.GetUserByID(token.UserID).Admin, token.UserID
|
||||
}
|
||||
return false, false, 0
|
||||
|
|
@ -47,7 +47,7 @@ func (a *Auth) RequireClient() gin.HandlerFunc {
|
|||
if user != nil {
|
||||
return true, true, user.ID
|
||||
}
|
||||
if token := a.DB.GetClientByID(tokenID); token != nil {
|
||||
if token := a.DB.GetClientByToken(tokenID); token != nil {
|
||||
return true, true, token.UserID
|
||||
}
|
||||
return false, false, 0
|
||||
|
|
@ -60,7 +60,7 @@ func (a *Auth) RequireApplicationToken() gin.HandlerFunc {
|
|||
if user != nil {
|
||||
return true, false, 0
|
||||
}
|
||||
if token := a.DB.GetApplicationByID(tokenID); token != nil {
|
||||
if token := a.DB.GetApplicationByToken(tokenID); token != nil {
|
||||
return true, true, token.UserID
|
||||
}
|
||||
return false, false, 0
|
||||
|
|
|
|||
|
|
@ -29,12 +29,12 @@ func (s *AuthenticationSuite) SetupSuite() {
|
|||
gin.SetMode(gin.TestMode)
|
||||
s.DB = &authmock.MockDatabase{}
|
||||
s.auth = &Auth{s.DB}
|
||||
s.DB.On("GetClientByID", "clienttoken").Return(&model.Client{ID: "clienttoken", UserID: 1, Name: "android phone"})
|
||||
s.DB.On("GetClientByID", "clienttoken_admin").Return(&model.Client{ID: "clienttoken", UserID: 2, Name: "android phone2"})
|
||||
s.DB.On("GetClientByID", mock.Anything).Return(nil)
|
||||
s.DB.On("GetApplicationByID", "apptoken").Return(&model.Application{ID: "apptoken", UserID: 1, Name: "backup server", Description: "irrelevant"})
|
||||
s.DB.On("GetApplicationByID", "apptoken_admin").Return(&model.Application{ID: "apptoken", UserID: 2, Name: "backup server", Description: "irrelevant"})
|
||||
s.DB.On("GetApplicationByID", mock.Anything).Return(nil)
|
||||
s.DB.On("GetClientByToken", "clienttoken").Return(&model.Client{ID: 1, Token: "clienttoken", UserID: 1, Name: "android phone"})
|
||||
s.DB.On("GetClientByToken", "clienttoken_admin").Return(&model.Client{ID: 2, Token: "clienttoken_admin", UserID: 2, Name: "android phone2"})
|
||||
s.DB.On("GetClientByToken", mock.Anything).Return(nil)
|
||||
s.DB.On("GetApplicationByToken", "apptoken").Return(&model.Application{ID: 3, Token: "apptoken", UserID: 1, Name: "backup server", Description: "irrelevant"})
|
||||
s.DB.On("GetApplicationByToken", "apptoken_admin").Return(&model.Application{ID: 4, Token: "apptoken_admin", UserID: 2, Name: "backup server", Description: "irrelevant"})
|
||||
s.DB.On("GetApplicationByToken", mock.Anything).Return(nil)
|
||||
|
||||
s.DB.On("GetUserByID", uint(1)).Return(&model.User{ID: 1, Name: "irrelevant", Admin: false})
|
||||
s.DB.On("GetUserByID", uint(2)).Return(&model.User{ID: 2, Name: "irrelevant", Admin: true})
|
||||
|
|
|
|||
|
|
@ -1,21 +1,21 @@
|
|||
// Code generated by mockery v1.0.0
|
||||
package mock
|
||||
|
||||
import mock "github.com/stretchr/testify/mock"
|
||||
import model "github.com/gotify/server/model"
|
||||
import "github.com/stretchr/testify/mock"
|
||||
import "github.com/gotify/server/model"
|
||||
|
||||
// MockDatabase is an autogenerated mock type for the Database type
|
||||
type MockDatabase struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
// GetApplicationById provides a mock function with given fields: id
|
||||
func (_m *MockDatabase) GetApplicationByID(id string) *model.Application {
|
||||
ret := _m.Called(id)
|
||||
// GetApplicationByToken provides a mock function with given fields: token
|
||||
func (_m *MockDatabase) 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(id)
|
||||
r0 = rf(token)
|
||||
} else {
|
||||
if ret.Get(0) != nil {
|
||||
r0 = ret.Get(0).(*model.Application)
|
||||
|
|
@ -25,13 +25,13 @@ func (_m *MockDatabase) GetApplicationByID(id string) *model.Application {
|
|||
return r0
|
||||
}
|
||||
|
||||
// GetClientById provides a mock function with given fields: id
|
||||
func (_m *MockDatabase) GetClientByID(id string) *model.Client {
|
||||
ret := _m.Called(id)
|
||||
// GetClientByToken provides a mock function with given fields: token
|
||||
func (_m *MockDatabase) 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(id)
|
||||
r0 = rf(token)
|
||||
} else {
|
||||
if ret.Get(0) != nil {
|
||||
r0 = ret.Get(0).(*model.Client)
|
||||
|
|
@ -41,7 +41,7 @@ func (_m *MockDatabase) GetClientByID(id string) *model.Client {
|
|||
return r0
|
||||
}
|
||||
|
||||
// GetUserById provides a mock function with given fields: id
|
||||
// GetUserByID provides a mock function with given fields: id
|
||||
func (_m *MockDatabase) GetUserByID(id uint) *model.User {
|
||||
ret := _m.Called(id)
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue