mocks.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. "gogs.io/gogs/internal/lfsutil"
  8. )
  9. // NOTE: Mocks are sorted in alphabetical order.
  10. var _ AccessTokensStore = (*MockAccessTokensStore)(nil)
  11. type MockAccessTokensStore struct {
  12. MockCreate func(userID int64, name string) (*AccessToken, error)
  13. MockDeleteByID func(userID, id int64) error
  14. MockGetBySHA func(sha string) (*AccessToken, error)
  15. MockList func(userID int64) ([]*AccessToken, error)
  16. MockSave func(t *AccessToken) error
  17. }
  18. func (m *MockAccessTokensStore) Create(userID int64, name string) (*AccessToken, error) {
  19. return m.MockCreate(userID, name)
  20. }
  21. func (m *MockAccessTokensStore) DeleteByID(userID, id int64) error {
  22. return m.MockDeleteByID(userID, id)
  23. }
  24. func (m *MockAccessTokensStore) GetBySHA(sha string) (*AccessToken, error) {
  25. return m.MockGetBySHA(sha)
  26. }
  27. func (m *MockAccessTokensStore) List(userID int64) ([]*AccessToken, error) {
  28. return m.MockList(userID)
  29. }
  30. func (m *MockAccessTokensStore) Save(t *AccessToken) error {
  31. return m.MockSave(t)
  32. }
  33. func SetMockAccessTokensStore(t *testing.T, mock AccessTokensStore) {
  34. before := AccessTokens
  35. AccessTokens = mock
  36. t.Cleanup(func() {
  37. AccessTokens = before
  38. })
  39. }
  40. var _ LFSStore = (*MockLFSStore)(nil)
  41. type MockLFSStore struct {
  42. MockCreateObject func(repoID int64, oid lfsutil.OID, size int64, storage lfsutil.Storage) error
  43. MockGetObjectByOID func(repoID int64, oid lfsutil.OID) (*LFSObject, error)
  44. MockGetObjectsByOIDs func(repoID int64, oids ...lfsutil.OID) ([]*LFSObject, error)
  45. }
  46. func (m *MockLFSStore) CreateObject(repoID int64, oid lfsutil.OID, size int64, storage lfsutil.Storage) error {
  47. return m.MockCreateObject(repoID, oid, size, storage)
  48. }
  49. func (m *MockLFSStore) GetObjectByOID(repoID int64, oid lfsutil.OID) (*LFSObject, error) {
  50. return m.MockGetObjectByOID(repoID, oid)
  51. }
  52. func (m *MockLFSStore) GetObjectsByOIDs(repoID int64, oids ...lfsutil.OID) ([]*LFSObject, error) {
  53. return m.MockGetObjectsByOIDs(repoID, oids...)
  54. }
  55. func SetMockLFSStore(t *testing.T, mock LFSStore) {
  56. before := LFS
  57. LFS = mock
  58. t.Cleanup(func() {
  59. LFS = before
  60. })
  61. }
  62. var _ loginSourceFilesStore = (*mockLoginSourceFilesStore)(nil)
  63. type mockLoginSourceFilesStore struct {
  64. MockGetByID func(id int64) (*LoginSource, error)
  65. MockLen func() int
  66. MockList func(opts ListLoginSourceOpts) []*LoginSource
  67. MockUpdate func(source *LoginSource)
  68. }
  69. func (m *mockLoginSourceFilesStore) GetByID(id int64) (*LoginSource, error) {
  70. return m.MockGetByID(id)
  71. }
  72. func (m *mockLoginSourceFilesStore) Len() int {
  73. return m.MockLen()
  74. }
  75. func (m *mockLoginSourceFilesStore) List(opts ListLoginSourceOpts) []*LoginSource {
  76. return m.MockList(opts)
  77. }
  78. func (m *mockLoginSourceFilesStore) Update(source *LoginSource) {
  79. m.MockUpdate(source)
  80. }
  81. func setMockLoginSourceFilesStore(t *testing.T, db *loginSources, mock loginSourceFilesStore) {
  82. before := db.files
  83. db.files = mock
  84. t.Cleanup(func() {
  85. db.files = before
  86. })
  87. }
  88. var _ loginSourceFileStore = (*mockLoginSourceFileStore)(nil)
  89. type mockLoginSourceFileStore struct {
  90. MockSetGeneral func(name, value string)
  91. MockSetConfig func(cfg interface{}) error
  92. MockSave func() error
  93. }
  94. func (m *mockLoginSourceFileStore) SetGeneral(name, value string) {
  95. m.MockSetGeneral(name, value)
  96. }
  97. func (m *mockLoginSourceFileStore) SetConfig(cfg interface{}) error {
  98. return m.MockSetConfig(cfg)
  99. }
  100. func (m *mockLoginSourceFileStore) Save() error {
  101. return m.MockSave()
  102. }
  103. var _ PermsStore = (*MockPermsStore)(nil)
  104. type MockPermsStore struct {
  105. MockAccessMode func(userID, repoID int64, opts AccessModeOptions) AccessMode
  106. MockAuthorize func(userID, repoID int64, desired AccessMode, opts AccessModeOptions) bool
  107. MockSetRepoPerms func(repoID int64, accessMap map[int64]AccessMode) error
  108. }
  109. func (m *MockPermsStore) AccessMode(userID, repoID int64, opts AccessModeOptions) AccessMode {
  110. return m.MockAccessMode(userID, repoID, opts)
  111. }
  112. func (m *MockPermsStore) Authorize(userID, repoID int64, desired AccessMode, opts AccessModeOptions) bool {
  113. return m.MockAuthorize(userID, repoID, desired, opts)
  114. }
  115. func (m *MockPermsStore) SetRepoPerms(repoID int64, accessMap map[int64]AccessMode) error {
  116. return m.MockSetRepoPerms(repoID, accessMap)
  117. }
  118. func SetMockPermsStore(t *testing.T, mock PermsStore) {
  119. before := Perms
  120. Perms = mock
  121. t.Cleanup(func() {
  122. Perms = before
  123. })
  124. }
  125. var _ ReposStore = (*MockReposStore)(nil)
  126. type MockReposStore struct {
  127. MockGetByName func(ownerID int64, name string) (*Repository, error)
  128. }
  129. func (m *MockReposStore) GetByName(ownerID int64, name string) (*Repository, error) {
  130. return m.MockGetByName(ownerID, name)
  131. }
  132. func SetMockReposStore(t *testing.T, mock ReposStore) {
  133. before := Repos
  134. Repos = mock
  135. t.Cleanup(func() {
  136. Repos = before
  137. })
  138. }
  139. var _ TwoFactorsStore = (*MockTwoFactorsStore)(nil)
  140. type MockTwoFactorsStore struct {
  141. MockCreate func(userID int64, key, secret string) error
  142. MockGetByUserID func(userID int64) (*TwoFactor, error)
  143. MockIsUserEnabled func(userID int64) bool
  144. }
  145. func (m *MockTwoFactorsStore) Create(userID int64, key, secret string) error {
  146. return m.MockCreate(userID, key, secret)
  147. }
  148. func (m *MockTwoFactorsStore) GetByUserID(userID int64) (*TwoFactor, error) {
  149. return m.MockGetByUserID(userID)
  150. }
  151. func (m *MockTwoFactorsStore) IsUserEnabled(userID int64) bool {
  152. return m.MockIsUserEnabled(userID)
  153. }
  154. func SetMockTwoFactorsStore(t *testing.T, mock TwoFactorsStore) {
  155. before := TwoFactors
  156. TwoFactors = mock
  157. t.Cleanup(func() {
  158. TwoFactors = before
  159. })
  160. }
  161. var _ UsersStore = (*MockUsersStore)(nil)
  162. type MockUsersStore struct {
  163. MockAuthenticate func(username, password string, loginSourceID int64) (*User, error)
  164. MockCreate func(username, email string, opts CreateUserOpts) (*User, error)
  165. MockGetByEmail func(email string) (*User, error)
  166. MockGetByID func(id int64) (*User, error)
  167. MockGetByUsername func(username string) (*User, error)
  168. }
  169. func (m *MockUsersStore) Authenticate(username, password string, loginSourceID int64) (*User, error) {
  170. return m.MockAuthenticate(username, password, loginSourceID)
  171. }
  172. func (m *MockUsersStore) Create(username, email string, opts CreateUserOpts) (*User, error) {
  173. return m.MockCreate(username, email, opts)
  174. }
  175. func (m *MockUsersStore) GetByEmail(email string) (*User, error) {
  176. return m.MockGetByEmail(email)
  177. }
  178. func (m *MockUsersStore) GetByID(id int64) (*User, error) {
  179. return m.MockGetByID(id)
  180. }
  181. func (m *MockUsersStore) GetByUsername(username string) (*User, error) {
  182. return m.MockGetByUsername(username)
  183. }
  184. func SetMockUsersStore(t *testing.T, mock UsersStore) {
  185. before := Users
  186. Users = mock
  187. t.Cleanup(func() {
  188. Users = before
  189. })
  190. }