From 433a83823b38bb58665df1da4766d2a6deeb95e9 Mon Sep 17 00:00:00 2001 From: Jannis Mattheis Date: Sat, 10 Mar 2018 21:19:50 +0100 Subject: [PATCH] Use *ByToken methods in auth --- auth/authentication.go | 10 +++++----- auth/authentication_test.go | 12 ++++++------ auth/mock/mock_database.go | 22 +++++++++++----------- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/auth/authentication.go b/auth/authentication.go index d1e1460..e0cc0f5 100644 --- a/auth/authentication.go +++ b/auth/authentication.go @@ -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 diff --git a/auth/authentication_test.go b/auth/authentication_test.go index 4b63baa..7ee0878 100644 --- a/auth/authentication_test.go +++ b/auth/authentication_test.go @@ -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}) diff --git a/auth/mock/mock_database.go b/auth/mock/mock_database.go index 28c43cf..5bc6583 100644 --- a/auth/mock/mock_database.go +++ b/auth/mock/mock_database.go @@ -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)