mocks.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 _ PermsStore = (*MockPermsStore)(nil)
  63. type MockPermsStore struct {
  64. MockAccessMode func(userID int64, repo *Repository) AccessMode
  65. MockAuthorize func(userID int64, repo *Repository, desired AccessMode) bool
  66. }
  67. func (m *MockPermsStore) AccessMode(userID int64, repo *Repository) AccessMode {
  68. return m.MockAccessMode(userID, repo)
  69. }
  70. func (m *MockPermsStore) Authorize(userID int64, repo *Repository, desired AccessMode) bool {
  71. return m.MockAuthorize(userID, repo, desired)
  72. }
  73. func SetMockPermsStore(t *testing.T, mock PermsStore) {
  74. before := Perms
  75. Perms = mock
  76. t.Cleanup(func() {
  77. Perms = before
  78. })
  79. }
  80. var _ ReposStore = (*MockReposStore)(nil)
  81. type MockReposStore struct {
  82. MockGetByName func(ownerID int64, name string) (*Repository, error)
  83. }
  84. func (m *MockReposStore) GetByName(ownerID int64, name string) (*Repository, error) {
  85. return m.MockGetByName(ownerID, name)
  86. }
  87. func SetMockReposStore(t *testing.T, mock ReposStore) {
  88. before := Repos
  89. Repos = mock
  90. t.Cleanup(func() {
  91. Repos = before
  92. })
  93. }
  94. var _ TwoFactorsStore = (*MockTwoFactorsStore)(nil)
  95. type MockTwoFactorsStore struct {
  96. MockIsUserEnabled func(userID int64) bool
  97. }
  98. func (m *MockTwoFactorsStore) IsUserEnabled(userID int64) bool {
  99. return m.MockIsUserEnabled(userID)
  100. }
  101. func SetMockTwoFactorsStore(t *testing.T, mock TwoFactorsStore) {
  102. before := TwoFactors
  103. TwoFactors = mock
  104. t.Cleanup(func() {
  105. TwoFactors = before
  106. })
  107. }
  108. var _ UsersStore = (*MockUsersStore)(nil)
  109. type MockUsersStore struct {
  110. MockAuthenticate func(username, password string, loginSourceID int64) (*User, error)
  111. MockGetByID func(id int64) (*User, error)
  112. MockGetByUsername func(username string) (*User, error)
  113. }
  114. func (m *MockUsersStore) Authenticate(username, password string, loginSourceID int64) (*User, error) {
  115. return m.MockAuthenticate(username, password, loginSourceID)
  116. }
  117. func (m *MockUsersStore) GetByID(id int64) (*User, error) {
  118. return m.MockGetByID(id)
  119. }
  120. func (m *MockUsersStore) GetByUsername(username string) (*User, error) {
  121. return m.MockGetByUsername(username)
  122. }
  123. func SetMockUsersStore(t *testing.T, mock UsersStore) {
  124. before := Users
  125. Users = mock
  126. t.Cleanup(func() {
  127. Users = before
  128. })
  129. }