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