diff options
author | ᴜɴᴋɴᴡᴏɴ <u@gogs.io> | 2020-04-06 18:35:10 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-06 18:35:10 +0800 |
commit | ee0ea2c5fc9f470512acf3f15834f14f8df9d737 (patch) | |
tree | 077bbea20818621e98d3a46ab01c0be235c2d25c /internal/db | |
parent | 4d7db6e1c108cee234422b34e3252ab217c461cc (diff) |
lfs: add unit test for middleware (#6070)
* Add unit test for `authenticate` middleware
* Add more cases
* Add tests for verifyOID and internalServerError
* Add tests for verifyHeader
* Add tests for authroize
Diffstat (limited to 'internal/db')
-rw-r--r-- | internal/db/mocks.go | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/internal/db/mocks.go b/internal/db/mocks.go new file mode 100644 index 00000000..0a52042b --- /dev/null +++ b/internal/db/mocks.go @@ -0,0 +1,121 @@ +// Copyright 2020 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package db + +import ( + "testing" +) + +// NOTE: Mocks are sorted in alphabetical order. + +var _ AccessTokensStore = (*MockAccessTokensStore)(nil) + +type MockAccessTokensStore struct { + MockGetBySHA func(sha string) (*AccessToken, error) + MockSave func(t *AccessToken) error +} + +func (m *MockAccessTokensStore) GetBySHA(sha string) (*AccessToken, error) { + return m.MockGetBySHA(sha) +} + +func (m *MockAccessTokensStore) Save(t *AccessToken) error { + return m.MockSave(t) +} + +func SetMockAccessTokensStore(t *testing.T, mock AccessTokensStore) { + before := AccessTokens + AccessTokens = mock + t.Cleanup(func() { + AccessTokens = before + }) +} + +var _ PermsStore = (*MockPermsStore)(nil) + +type MockPermsStore struct { + MockAccessMode func(userID int64, repo *Repository) AccessMode + MockAuthorize func(userID int64, repo *Repository, desired AccessMode) bool +} + +func (m *MockPermsStore) AccessMode(userID int64, repo *Repository) AccessMode { + return m.MockAccessMode(userID, repo) +} + +func (m *MockPermsStore) Authorize(userID int64, repo *Repository, desired AccessMode) bool { + return m.MockAuthorize(userID, repo, desired) +} + +func SetMockPermsStore(t *testing.T, mock PermsStore) { + before := Perms + Perms = mock + t.Cleanup(func() { + Perms = before + }) +} + +var _ ReposStore = (*MockReposStore)(nil) + +type MockReposStore struct { + MockGetByName func(ownerID int64, name string) (*Repository, error) +} + +func (m *MockReposStore) GetByName(ownerID int64, name string) (*Repository, error) { + return m.MockGetByName(ownerID, name) +} + +func SetMockReposStore(t *testing.T, mock ReposStore) { + before := Repos + Repos = mock + t.Cleanup(func() { + Repos = before + }) +} + +var _ TwoFactorsStore = (*MockTwoFactorsStore)(nil) + +type MockTwoFactorsStore struct { + MockIsUserEnabled func(userID int64) bool +} + +func (m *MockTwoFactorsStore) IsUserEnabled(userID int64) bool { + return m.MockIsUserEnabled(userID) +} + +func SetMockTwoFactorsStore(t *testing.T, mock TwoFactorsStore) { + before := TwoFactors + TwoFactors = mock + t.Cleanup(func() { + TwoFactors = before + }) +} + +var _ UsersStore = (*MockUsersStore)(nil) + +type MockUsersStore struct { + MockAuthenticate func(username, password string, loginSourceID int64) (*User, error) + MockGetByID func(id int64) (*User, error) + MockGetByUsername func(username string) (*User, error) +} + +func (m *MockUsersStore) Authenticate(username, password string, loginSourceID int64) (*User, error) { + return m.MockAuthenticate(username, password, loginSourceID) +} + +func (m *MockUsersStore) GetByID(id int64) (*User, error) { + return m.MockGetByID(id) +} + +func (m *MockUsersStore) GetByUsername(username string) (*User, error) { + return m.MockGetByUsername(username) +} + +func SetMockUsersStore(t *testing.T, mock UsersStore) { + before := Users + Users = mock + t.Cleanup(func() { + Users = before + }) +} |