mocks.go 3.8 KB

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