mocks.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright 2020 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package db
  5. import (
  6. "testing"
  7. )
  8. // NOTE: Mocks are sorted in alphabetical order.
  9. var _ AccessTokensStore = (*MockAccessTokensStore)(nil)
  10. type MockAccessTokensStore struct {
  11. MockGetBySHA func(sha string) (*AccessToken, error)
  12. MockSave func(t *AccessToken) error
  13. }
  14. func (m *MockAccessTokensStore) GetBySHA(sha string) (*AccessToken, error) {
  15. return m.MockGetBySHA(sha)
  16. }
  17. func (m *MockAccessTokensStore) Save(t *AccessToken) error {
  18. return m.MockSave(t)
  19. }
  20. func SetMockAccessTokensStore(t *testing.T, mock AccessTokensStore) {
  21. before := AccessTokens
  22. AccessTokens = mock
  23. t.Cleanup(func() {
  24. AccessTokens = before
  25. })
  26. }
  27. var _ PermsStore = (*MockPermsStore)(nil)
  28. type MockPermsStore struct {
  29. MockAccessMode func(userID int64, repo *Repository) AccessMode
  30. MockAuthorize func(userID int64, repo *Repository, desired AccessMode) bool
  31. }
  32. func (m *MockPermsStore) AccessMode(userID int64, repo *Repository) AccessMode {
  33. return m.MockAccessMode(userID, repo)
  34. }
  35. func (m *MockPermsStore) Authorize(userID int64, repo *Repository, desired AccessMode) bool {
  36. return m.MockAuthorize(userID, repo, desired)
  37. }
  38. func SetMockPermsStore(t *testing.T, mock PermsStore) {
  39. before := Perms
  40. Perms = mock
  41. t.Cleanup(func() {
  42. Perms = before
  43. })
  44. }
  45. var _ ReposStore = (*MockReposStore)(nil)
  46. type MockReposStore struct {
  47. MockGetByName func(ownerID int64, name string) (*Repository, error)
  48. }
  49. func (m *MockReposStore) GetByName(ownerID int64, name string) (*Repository, error) {
  50. return m.MockGetByName(ownerID, name)
  51. }
  52. func SetMockReposStore(t *testing.T, mock ReposStore) {
  53. before := Repos
  54. Repos = mock
  55. t.Cleanup(func() {
  56. Repos = before
  57. })
  58. }
  59. var _ TwoFactorsStore = (*MockTwoFactorsStore)(nil)
  60. type MockTwoFactorsStore struct {
  61. MockIsUserEnabled func(userID int64) bool
  62. }
  63. func (m *MockTwoFactorsStore) IsUserEnabled(userID int64) bool {
  64. return m.MockIsUserEnabled(userID)
  65. }
  66. func SetMockTwoFactorsStore(t *testing.T, mock TwoFactorsStore) {
  67. before := TwoFactors
  68. TwoFactors = mock
  69. t.Cleanup(func() {
  70. TwoFactors = before
  71. })
  72. }
  73. var _ UsersStore = (*MockUsersStore)(nil)
  74. type MockUsersStore struct {
  75. MockAuthenticate func(username, password string, loginSourceID int64) (*User, error)
  76. MockGetByID func(id int64) (*User, error)
  77. MockGetByUsername func(username string) (*User, error)
  78. }
  79. func (m *MockUsersStore) Authenticate(username, password string, loginSourceID int64) (*User, error) {
  80. return m.MockAuthenticate(username, password, loginSourceID)
  81. }
  82. func (m *MockUsersStore) GetByID(id int64) (*User, error) {
  83. return m.MockGetByID(id)
  84. }
  85. func (m *MockUsersStore) GetByUsername(username string) (*User, error) {
  86. return m.MockGetByUsername(username)
  87. }
  88. func SetMockUsersStore(t *testing.T, mock UsersStore) {
  89. before := Users
  90. Users = mock
  91. t.Cleanup(func() {
  92. Users = before
  93. })
  94. }