From 6e488e612b9c1ed38a1c0ea64fd163b082ea03dd Mon Sep 17 00:00:00 2001 From: Jannis Mattheis Date: Sat, 24 Mar 2018 16:42:37 +0100 Subject: [PATCH] Remove mock & Use database util in authentication handler --- auth/authentication_test.go | 43 ++++++++++++--------- auth/mock/mock_database.go | 74 ------------------------------------- 2 files changed, 25 insertions(+), 92 deletions(-) delete mode 100644 auth/mock/mock_database.go diff --git a/auth/authentication_test.go b/auth/authentication_test.go index a7bf1fc..282d047 100644 --- a/auth/authentication_test.go +++ b/auth/authentication_test.go @@ -8,12 +8,12 @@ import ( "testing" "github.com/gin-gonic/gin" - authmock "github.com/gotify/server/auth/mock" - "github.com/gotify/server/model" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/mock" - "github.com/stretchr/testify/suite" + "github.com/gotify/server/auth/password" "github.com/gotify/server/mode" + "github.com/gotify/server/model" + "github.com/gotify/server/test" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/suite" ) func TestSuite(t *testing.T) { @@ -23,26 +23,33 @@ func TestSuite(t *testing.T) { type AuthenticationSuite struct { suite.Suite auth *Auth - DB *authmock.MockDatabase + DB *test.Database } func (s *AuthenticationSuite) SetupSuite() { mode.Set(mode.TestDev) - s.DB = &authmock.MockDatabase{} + s.DB = test.NewDB(s.T()) s.auth = &Auth{s.DB} - 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}) + s.DB.CreateUser(&model.User{ + Name: "existing", + Pass: password.CreatePassword("pw", 5), + Admin: false, + Applications: []model.Application{{Token: "apptoken", Name: "backup server1", Description: "irrelevant"}}, + Clients: []model.Client{{Token: "clienttoken", Name: "android phone1"}}, + }) - s.DB.On("GetUserByName", "existing").Return(&model.User{Name: "existing", Pass: CreatePassword("pw", 5)}) - s.DB.On("GetUserByName", "admin").Return(&model.User{Name: "admin", Pass: CreatePassword("pw", 5), Admin: true}) - s.DB.On("GetUserByName", mock.Anything).Return(nil) + s.DB.CreateUser(&model.User{ + Name: "admin", + Pass: password.CreatePassword("pw", 5), + Admin: true, + Applications: []model.Application{{Token: "apptoken_admin", Name: "backup server2", Description: "irrelevant"}}, + Clients: []model.Client{{Token: "clienttoken_admin", Name: "android phone2"}}, + }) +} + +func (s *AuthenticationSuite) TearDownSuite() { + s.DB.Close() } func (s *AuthenticationSuite) TestQueryToken() { diff --git a/auth/mock/mock_database.go b/auth/mock/mock_database.go deleted file mode 100644 index 5bc6583..0000000 --- a/auth/mock/mock_database.go +++ /dev/null @@ -1,74 +0,0 @@ -// Code generated by mockery v1.0.0 -package mock - -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 -} - -// 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(token) - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(*model.Application) - } - } - - return r0 -} - -// 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(token) - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(*model.Client) - } - } - - return r0 -} - -// GetUserByID provides a mock function with given fields: id -func (_m *MockDatabase) GetUserByID(id uint) *model.User { - ret := _m.Called(id) - - var r0 *model.User - if rf, ok := ret.Get(0).(func(uint) *model.User); ok { - r0 = rf(id) - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(*model.User) - } - } - - return r0 -} - -// GetUserByName provides a mock function with given fields: name -func (_m *MockDatabase) GetUserByName(name string) *model.User { - ret := _m.Called(name) - - var r0 *model.User - if rf, ok := ret.Get(0).(func(string) *model.User); ok { - r0 = rf(name) - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(*model.User) - } - } - - return r0 -}