mocks.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. MockGetBySHA func(sha string) (*AccessToken, error)
  13. MockSave func(t *AccessToken) error
  14. }
  15. func (m *MockAccessTokensStore) GetBySHA(sha string) (*AccessToken, error) {
  16. return m.MockGetBySHA(sha)
  17. }
  18. func (m *MockAccessTokensStore) Save(t *AccessToken) error {
  19. return m.MockSave(t)
  20. }
  21. func SetMockAccessTokensStore(t *testing.T, mock AccessTokensStore) {
  22. before := AccessTokens
  23. AccessTokens = mock
  24. t.Cleanup(func() {
  25. AccessTokens = before
  26. })
  27. }
  28. var _ LFSStore = (*MockLFSStore)(nil)
  29. type MockLFSStore struct {
  30. MockCreateObject func(repoID int64, oid lfsutil.OID, size int64, storage lfsutil.Storage) error
  31. MockGetObjectByOID func(repoID int64, oid lfsutil.OID) (*LFSObject, error)
  32. MockGetObjectsByOIDs func(repoID int64, oids ...lfsutil.OID) ([]*LFSObject, error)
  33. }
  34. func (m *MockLFSStore) CreateObject(repoID int64, oid lfsutil.OID, size int64, storage lfsutil.Storage) error {
  35. return m.MockCreateObject(repoID, oid, size, storage)
  36. }
  37. func (m *MockLFSStore) GetObjectByOID(repoID int64, oid lfsutil.OID) (*LFSObject, error) {
  38. return m.MockGetObjectByOID(repoID, oid)
  39. }
  40. func (m *MockLFSStore) GetObjectsByOIDs(repoID int64, oids ...lfsutil.OID) ([]*LFSObject, error) {
  41. return m.MockGetObjectsByOIDs(repoID, oids...)
  42. }
  43. func SetMockLFSStore(t *testing.T, mock LFSStore) {
  44. before := LFS
  45. LFS = mock
  46. t.Cleanup(func() {
  47. LFS = before
  48. })
  49. }
  50. var _ PermsStore = (*MockPermsStore)(nil)
  51. type MockPermsStore struct {
  52. MockAccessMode func(userID int64, repo *Repository) AccessMode
  53. MockAuthorize func(userID int64, repo *Repository, desired AccessMode) bool
  54. }
  55. func (m *MockPermsStore) AccessMode(userID int64, repo *Repository) AccessMode {
  56. return m.MockAccessMode(userID, repo)
  57. }
  58. func (m *MockPermsStore) Authorize(userID int64, repo *Repository, desired AccessMode) bool {
  59. return m.MockAuthorize(userID, repo, desired)
  60. }
  61. func SetMockPermsStore(t *testing.T, mock PermsStore) {
  62. before := Perms
  63. Perms = mock
  64. t.Cleanup(func() {
  65. Perms = before
  66. })
  67. }
  68. var _ ReposStore = (*MockReposStore)(nil)
  69. type MockReposStore struct {
  70. MockGetByName func(ownerID int64, name string) (*Repository, error)
  71. }
  72. func (m *MockReposStore) GetByName(ownerID int64, name string) (*Repository, error) {
  73. return m.MockGetByName(ownerID, name)
  74. }
  75. func SetMockReposStore(t *testing.T, mock ReposStore) {
  76. before := Repos
  77. Repos = mock
  78. t.Cleanup(func() {
  79. Repos = before
  80. })
  81. }
  82. var _ TwoFactorsStore = (*MockTwoFactorsStore)(nil)
  83. type MockTwoFactorsStore struct {
  84. MockIsUserEnabled func(userID int64) bool
  85. }
  86. func (m *MockTwoFactorsStore) IsUserEnabled(userID int64) bool {
  87. return m.MockIsUserEnabled(userID)
  88. }
  89. func SetMockTwoFactorsStore(t *testing.T, mock TwoFactorsStore) {
  90. before := TwoFactors
  91. TwoFactors = mock
  92. t.Cleanup(func() {
  93. TwoFactors = before
  94. })
  95. }
  96. var _ UsersStore = (*MockUsersStore)(nil)
  97. type MockUsersStore struct {
  98. MockAuthenticate func(username, password string, loginSourceID int64) (*User, error)
  99. MockGetByID func(id int64) (*User, error)
  100. MockGetByUsername func(username string) (*User, error)
  101. }
  102. func (m *MockUsersStore) Authenticate(username, password string, loginSourceID int64) (*User, error) {
  103. return m.MockAuthenticate(username, password, loginSourceID)
  104. }
  105. func (m *MockUsersStore) GetByID(id int64) (*User, error) {
  106. return m.MockGetByID(id)
  107. }
  108. func (m *MockUsersStore) GetByUsername(username string) (*User, error) {
  109. return m.MockGetByUsername(username)
  110. }
  111. func SetMockUsersStore(t *testing.T, mock UsersStore) {
  112. before := Users
  113. Users = mock
  114. t.Cleanup(func() {
  115. Users = before
  116. })
  117. }