mocks.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 int64, repo *Repository) AccessMode
  106. MockAuthorize func(userID int64, repo *Repository, desired AccessMode) bool
  107. MockSetRepoPerms func(repoID int64, accessMap map[int64]AccessMode) error
  108. }
  109. func (m *MockPermsStore) AccessMode(userID int64, repo *Repository) AccessMode {
  110. return m.MockAccessMode(userID, repo)
  111. }
  112. func (m *MockPermsStore) Authorize(userID int64, repo *Repository, desired AccessMode) bool {
  113. return m.MockAuthorize(userID, repo, desired)
  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. MockIsUserEnabled func(userID int64) bool
  142. }
  143. func (m *MockTwoFactorsStore) IsUserEnabled(userID int64) bool {
  144. return m.MockIsUserEnabled(userID)
  145. }
  146. func SetMockTwoFactorsStore(t *testing.T, mock TwoFactorsStore) {
  147. before := TwoFactors
  148. TwoFactors = mock
  149. t.Cleanup(func() {
  150. TwoFactors = before
  151. })
  152. }
  153. var _ UsersStore = (*MockUsersStore)(nil)
  154. type MockUsersStore struct {
  155. MockAuthenticate func(username, password string, loginSourceID int64) (*User, error)
  156. MockGetByID func(id int64) (*User, error)
  157. MockGetByUsername func(username string) (*User, error)
  158. }
  159. func (m *MockUsersStore) Authenticate(username, password string, loginSourceID int64) (*User, error) {
  160. return m.MockAuthenticate(username, password, loginSourceID)
  161. }
  162. func (m *MockUsersStore) GetByID(id int64) (*User, error) {
  163. return m.MockGetByID(id)
  164. }
  165. func (m *MockUsersStore) GetByUsername(username string) (*User, error) {
  166. return m.MockGetByUsername(username)
  167. }
  168. func SetMockUsersStore(t *testing.T, mock UsersStore) {
  169. before := Users
  170. Users = mock
  171. t.Cleanup(func() {
  172. Users = before
  173. })
  174. }