From fadf989158c1610111d897828af11a03f8758de1 Mon Sep 17 00:00:00 2001 From: Jannis Mattheis Date: Sun, 28 Jan 2018 14:52:37 +0100 Subject: [PATCH] Rename auth.Database methods --- auth/authentication.go | 22 +++++++++++----------- auth/authentication_test.go | 16 ++++++++-------- auth/mock/mock_database.go | 6 +++--- auth/util_test.go | 2 +- 4 files changed, 23 insertions(+), 23 deletions(-) diff --git a/auth/authentication.go b/auth/authentication.go index 7c80c65..dbdc314 100644 --- a/auth/authentication.go +++ b/auth/authentication.go @@ -14,10 +14,10 @@ const ( // The Database interface for encapsulating database access. type Database interface { - GetApplicationById(id string) *model.Application - GetClientById(id string) *model.Client + GetApplicationByID(id string) *model.Application + GetClientByID(id string) *model.Client GetUserByName(name string) *model.User - GetUserById(id uint) *model.User + GetUserByID(id uint) *model.User } // Auth is the provider for authentication middleware @@ -25,17 +25,17 @@ type Auth struct { DB Database } -type authenticate func(tokenId string, user *model.User) (success bool, userId uint) +type authenticate func(tokenID string, user *model.User) (success bool, userId uint) // RequireAdmin returns a gin middleware which requires a client token or basic authentication header to be supplied // with the request. Also the authenticated user must be an administrator. func (a *Auth) RequireAdmin() gin.HandlerFunc { - return a.requireToken(func(tokenId string, user *model.User) (bool, uint) { + return a.requireToken(func(tokenID string, user *model.User) (bool, uint) { if user != nil { return user.Admin, user.ID } - if token := a.DB.GetClientById(tokenId); token != nil { - return a.DB.GetUserById(token.UserID).Admin, token.UserID + if token := a.DB.GetClientByID(tokenID); token != nil { + return a.DB.GetUserByID(token.UserID).Admin, token.UserID } return false, 0 }) @@ -44,11 +44,11 @@ func (a *Auth) RequireAdmin() gin.HandlerFunc { // RequireClient returns a gin middleware which requires a client token or basic authentication header to be supplied // with the request. func (a *Auth) RequireClient() gin.HandlerFunc { - return a.requireToken(func(tokenId string, user *model.User) (bool, uint) { + return a.requireToken(func(tokenID string, user *model.User) (bool, uint) { if user != nil { return true, user.ID } - if token := a.DB.GetClientById(tokenId); token != nil { + if token := a.DB.GetClientByID(tokenID); token != nil { return true, token.UserID } return false, 0 @@ -57,11 +57,11 @@ func (a *Auth) RequireClient() gin.HandlerFunc { // RequireApplicationToken returns a gin middleware which requires an application token to be supplied with the request. func (a *Auth) RequireApplicationToken() gin.HandlerFunc { - return a.requireToken(func(tokenId string, user *model.User) (bool, uint) { + return a.requireToken(func(tokenID string, user *model.User) (bool, uint) { if user != nil { return false, 0 } - if token := a.DB.GetApplicationById(tokenId); token != nil { + if token := a.DB.GetApplicationByID(tokenID); token != nil { return true, token.UserID } return false, 0 diff --git a/auth/authentication_test.go b/auth/authentication_test.go index 2907abf..c273c1c 100644 --- a/auth/authentication_test.go +++ b/auth/authentication_test.go @@ -26,15 +26,15 @@ 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("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("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(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("GetUserByName", "existing").Return(&model.User{Name: "existing", Pass: CreatePassword("pw")}) s.DB.On("GetUserByName", "admin").Return(&model.User{Name: "admin", Pass: CreatePassword("pw"), Admin: true}) diff --git a/auth/mock/mock_database.go b/auth/mock/mock_database.go index 9e4bd11..dc35315 100644 --- a/auth/mock/mock_database.go +++ b/auth/mock/mock_database.go @@ -10,7 +10,7 @@ type MockDatabase struct { } // GetApplicationById provides a mock function with given fields: id -func (_m *MockDatabase) GetApplicationById(id string) *model.Application { +func (_m *MockDatabase) GetApplicationByID(id string) *model.Application { ret := _m.Called(id) var r0 *model.Application @@ -26,7 +26,7 @@ func (_m *MockDatabase) GetApplicationById(id string) *model.Application { } // GetClientById provides a mock function with given fields: id -func (_m *MockDatabase) GetClientById(id string) *model.Client { +func (_m *MockDatabase) GetClientByID(id string) *model.Client { ret := _m.Called(id) var r0 *model.Client @@ -42,7 +42,7 @@ func (_m *MockDatabase) GetClientById(id string) *model.Client { } // 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) var r0 *model.User diff --git a/auth/util_test.go b/auth/util_test.go index 9cdc6e1..2af626a 100644 --- a/auth/util_test.go +++ b/auth/util_test.go @@ -21,7 +21,7 @@ func (s *UtilSuite) BeforeTest(suiteName, testName string) { gin.SetMode(gin.TestMode) } -func (s *UtilSuite) Test_getId() { +func (s *UtilSuite) Test_getID() { s.expectUserIDWith(&model.User{ID: 2}, 0, 2) s.expectUserIDWith(nil, 5, 5) assert.Panics(s.T(), func() {