repositories_test.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. "context"
  7. "testing"
  8. "time"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/stretchr/testify/require"
  11. "gorm.io/gorm"
  12. "gogs.io/gogs/internal/dbtest"
  13. "gogs.io/gogs/internal/errutil"
  14. )
  15. func TestRepository_BeforeCreate(t *testing.T) {
  16. now := time.Now()
  17. db := &gorm.DB{
  18. Config: &gorm.Config{
  19. SkipDefaultTransaction: true,
  20. NowFunc: func() time.Time {
  21. return now
  22. },
  23. },
  24. }
  25. t.Run("CreatedUnix has been set", func(t *testing.T) {
  26. repo := &Repository{
  27. CreatedUnix: 1,
  28. }
  29. _ = repo.BeforeCreate(db)
  30. assert.Equal(t, int64(1), repo.CreatedUnix)
  31. })
  32. t.Run("CreatedUnix has not been set", func(t *testing.T) {
  33. repo := &Repository{}
  34. _ = repo.BeforeCreate(db)
  35. assert.Equal(t, db.NowFunc().Unix(), repo.CreatedUnix)
  36. })
  37. }
  38. func TestRepository_BeforeUpdate(t *testing.T) {
  39. now := time.Now()
  40. db := &gorm.DB{
  41. Config: &gorm.Config{
  42. SkipDefaultTransaction: true,
  43. NowFunc: func() time.Time {
  44. return now
  45. },
  46. },
  47. }
  48. repo := &Repository{}
  49. _ = repo.BeforeUpdate(db)
  50. assert.Equal(t, db.NowFunc().Unix(), repo.UpdatedUnix)
  51. }
  52. func TestRepository_AfterFind(t *testing.T) {
  53. now := time.Now()
  54. db := &gorm.DB{
  55. Config: &gorm.Config{
  56. SkipDefaultTransaction: true,
  57. NowFunc: func() time.Time {
  58. return now
  59. },
  60. },
  61. }
  62. repo := &Repository{
  63. CreatedUnix: now.Unix(),
  64. UpdatedUnix: now.Unix(),
  65. }
  66. _ = repo.AfterFind(db)
  67. assert.Equal(t, repo.CreatedUnix, repo.Created.Unix())
  68. assert.Equal(t, repo.UpdatedUnix, repo.Updated.Unix())
  69. }
  70. func TestRepos(t *testing.T) {
  71. if testing.Short() {
  72. t.Skip()
  73. }
  74. t.Parallel()
  75. ctx := context.Background()
  76. tables := []any{new(Repository), new(Access), new(Watch), new(User), new(EmailAddress), new(Star)}
  77. db := &repositories{
  78. DB: dbtest.NewDB(t, "repos", tables...),
  79. }
  80. for _, tc := range []struct {
  81. name string
  82. test func(t *testing.T, ctx context.Context, db *repositories)
  83. }{
  84. {"Create", reposCreate},
  85. {"GetByCollaboratorID", reposGetByCollaboratorID},
  86. {"GetByCollaboratorIDWithAccessMode", reposGetByCollaboratorIDWithAccessMode},
  87. {"GetByID", reposGetByID},
  88. {"GetByName", reposGetByName},
  89. {"Star", reposStar},
  90. {"Touch", reposTouch},
  91. {"ListByRepo", reposListWatches},
  92. {"Watch", reposWatch},
  93. {"HasForkedBy", reposHasForkedBy},
  94. } {
  95. t.Run(tc.name, func(t *testing.T) {
  96. t.Cleanup(func() {
  97. err := clearTables(t, db.DB, tables...)
  98. require.NoError(t, err)
  99. })
  100. tc.test(t, ctx, db)
  101. })
  102. if t.Failed() {
  103. break
  104. }
  105. }
  106. }
  107. func reposCreate(t *testing.T, ctx context.Context, db *repositories) {
  108. t.Run("name not allowed", func(t *testing.T) {
  109. _, err := db.Create(ctx,
  110. 1,
  111. CreateRepoOptions{
  112. Name: "my.git",
  113. },
  114. )
  115. wantErr := ErrNameNotAllowed{args: errutil.Args{"reason": "reserved", "pattern": "*.git"}}
  116. assert.Equal(t, wantErr, err)
  117. })
  118. t.Run("already exists", func(t *testing.T) {
  119. _, err := db.Create(ctx, 2,
  120. CreateRepoOptions{
  121. Name: "repo1",
  122. },
  123. )
  124. require.NoError(t, err)
  125. _, err = db.Create(ctx, 2,
  126. CreateRepoOptions{
  127. Name: "repo1",
  128. },
  129. )
  130. wantErr := ErrRepositoryAlreadyExist{args: errutil.Args{"ownerID": int64(2), "name": "repo1"}}
  131. assert.Equal(t, wantErr, err)
  132. })
  133. repo, err := db.Create(ctx, 3,
  134. CreateRepoOptions{
  135. Name: "repo2",
  136. },
  137. )
  138. require.NoError(t, err)
  139. repo, err = db.GetByName(ctx, repo.OwnerID, repo.Name)
  140. require.NoError(t, err)
  141. assert.Equal(t, db.NowFunc().Format(time.RFC3339), repo.Created.UTC().Format(time.RFC3339))
  142. assert.Equal(t, 1, repo.NumWatches) // The owner is watching the repo by default.
  143. }
  144. func reposGetByCollaboratorID(t *testing.T, ctx context.Context, db *repositories) {
  145. repo1, err := db.Create(ctx, 1, CreateRepoOptions{Name: "repo1"})
  146. require.NoError(t, err)
  147. repo2, err := db.Create(ctx, 2, CreateRepoOptions{Name: "repo2"})
  148. require.NoError(t, err)
  149. permsStore := NewPermsStore(db.DB)
  150. err = permsStore.SetRepoPerms(ctx, repo1.ID, map[int64]AccessMode{3: AccessModeRead})
  151. require.NoError(t, err)
  152. err = permsStore.SetRepoPerms(ctx, repo2.ID, map[int64]AccessMode{4: AccessModeAdmin})
  153. require.NoError(t, err)
  154. t.Run("user 3 is a collaborator of repo1", func(t *testing.T) {
  155. got, err := db.GetByCollaboratorID(ctx, 3, 10, "")
  156. require.NoError(t, err)
  157. require.Len(t, got, 1)
  158. assert.Equal(t, repo1.ID, got[0].ID)
  159. })
  160. t.Run("do not return directly owned repository", func(t *testing.T) {
  161. got, err := db.GetByCollaboratorID(ctx, 1, 10, "")
  162. require.NoError(t, err)
  163. require.Len(t, got, 0)
  164. })
  165. }
  166. func reposGetByCollaboratorIDWithAccessMode(t *testing.T, ctx context.Context, db *repositories) {
  167. repo1, err := db.Create(ctx, 1, CreateRepoOptions{Name: "repo1"})
  168. require.NoError(t, err)
  169. repo2, err := db.Create(ctx, 2, CreateRepoOptions{Name: "repo2"})
  170. require.NoError(t, err)
  171. repo3, err := db.Create(ctx, 2, CreateRepoOptions{Name: "repo3"})
  172. require.NoError(t, err)
  173. permsStore := NewPermsStore(db.DB)
  174. err = permsStore.SetRepoPerms(ctx, repo1.ID, map[int64]AccessMode{3: AccessModeRead})
  175. require.NoError(t, err)
  176. err = permsStore.SetRepoPerms(ctx, repo2.ID, map[int64]AccessMode{3: AccessModeAdmin, 4: AccessModeWrite})
  177. require.NoError(t, err)
  178. err = permsStore.SetRepoPerms(ctx, repo3.ID, map[int64]AccessMode{4: AccessModeWrite})
  179. require.NoError(t, err)
  180. got, err := db.GetByCollaboratorIDWithAccessMode(ctx, 3)
  181. require.NoError(t, err)
  182. require.Len(t, got, 2)
  183. accessModes := make(map[int64]AccessMode)
  184. for repo, mode := range got {
  185. accessModes[repo.ID] = mode
  186. }
  187. assert.Equal(t, AccessModeRead, accessModes[repo1.ID])
  188. assert.Equal(t, AccessModeAdmin, accessModes[repo2.ID])
  189. }
  190. func reposGetByID(t *testing.T, ctx context.Context, db *repositories) {
  191. repo1, err := db.Create(ctx, 1, CreateRepoOptions{Name: "repo1"})
  192. require.NoError(t, err)
  193. got, err := db.GetByID(ctx, repo1.ID)
  194. require.NoError(t, err)
  195. assert.Equal(t, repo1.Name, got.Name)
  196. _, err = db.GetByID(ctx, 404)
  197. wantErr := ErrRepoNotExist{args: errutil.Args{"repoID": int64(404)}}
  198. assert.Equal(t, wantErr, err)
  199. }
  200. func reposGetByName(t *testing.T, ctx context.Context, db *repositories) {
  201. repo, err := db.Create(ctx, 1,
  202. CreateRepoOptions{
  203. Name: "repo1",
  204. },
  205. )
  206. require.NoError(t, err)
  207. _, err = db.GetByName(ctx, repo.OwnerID, repo.Name)
  208. require.NoError(t, err)
  209. _, err = db.GetByName(ctx, 1, "bad_name")
  210. wantErr := ErrRepoNotExist{args: errutil.Args{"ownerID": int64(1), "name": "bad_name"}}
  211. assert.Equal(t, wantErr, err)
  212. }
  213. func reposStar(t *testing.T, ctx context.Context, db *repositories) {
  214. repo1, err := db.Create(ctx, 1, CreateRepoOptions{Name: "repo1"})
  215. require.NoError(t, err)
  216. usersStore := NewUsersStore(db.DB)
  217. alice, err := usersStore.Create(ctx, "alice", "alice@example.com", CreateUserOptions{})
  218. require.NoError(t, err)
  219. err = db.Star(ctx, alice.ID, repo1.ID)
  220. require.NoError(t, err)
  221. repo1, err = db.GetByID(ctx, repo1.ID)
  222. require.NoError(t, err)
  223. assert.Equal(t, 1, repo1.NumStars)
  224. alice, err = usersStore.GetByID(ctx, alice.ID)
  225. require.NoError(t, err)
  226. assert.Equal(t, 1, alice.NumStars)
  227. }
  228. func reposTouch(t *testing.T, ctx context.Context, db *repositories) {
  229. repo, err := db.Create(ctx, 1,
  230. CreateRepoOptions{
  231. Name: "repo1",
  232. },
  233. )
  234. require.NoError(t, err)
  235. err = db.WithContext(ctx).Model(new(Repository)).Where("id = ?", repo.ID).Update("is_bare", true).Error
  236. require.NoError(t, err)
  237. // Make sure it is bare
  238. got, err := db.GetByName(ctx, repo.OwnerID, repo.Name)
  239. require.NoError(t, err)
  240. assert.True(t, got.IsBare)
  241. // Touch it
  242. err = db.Touch(ctx, repo.ID)
  243. require.NoError(t, err)
  244. // It should not be bare anymore
  245. got, err = db.GetByName(ctx, repo.OwnerID, repo.Name)
  246. require.NoError(t, err)
  247. assert.False(t, got.IsBare)
  248. }
  249. func reposListWatches(t *testing.T, ctx context.Context, db *repositories) {
  250. repo1, err := db.Create(ctx, 1, CreateRepoOptions{Name: "repo1"})
  251. require.NoError(t, err)
  252. _, err = db.Create(ctx, 2, CreateRepoOptions{Name: "repo2"})
  253. require.NoError(t, err)
  254. err = db.Watch(
  255. ctx,
  256. WatchRepositoryOptions{
  257. UserID: 2,
  258. RepoID: repo1.ID,
  259. RepoOwnerID: repo1.OwnerID,
  260. RepoIsPrivate: repo1.IsPrivate,
  261. },
  262. )
  263. require.NoError(t, err)
  264. got, err := db.ListWatches(ctx, 1)
  265. require.NoError(t, err)
  266. for _, w := range got {
  267. w.ID = 0
  268. }
  269. want := []*Watch{
  270. {UserID: 1, RepoID: 1},
  271. {UserID: 2, RepoID: 1},
  272. }
  273. assert.Equal(t, want, got)
  274. }
  275. func reposWatch(t *testing.T, ctx context.Context, db *repositories) {
  276. t.Run("user does not have access to the repository", func(t *testing.T) {
  277. repo1, err := db.Create(ctx, 1, CreateRepoOptions{Name: "repo1", Private: true})
  278. require.NoError(t, err)
  279. err = db.Watch(
  280. ctx,
  281. WatchRepositoryOptions{
  282. UserID: 2,
  283. RepoID: repo1.ID,
  284. RepoOwnerID: repo1.OwnerID,
  285. RepoIsPrivate: repo1.IsPrivate,
  286. },
  287. )
  288. require.Error(t, err)
  289. })
  290. t.Run("user has access to the repository", func(t *testing.T) {
  291. repo2, err := db.Create(ctx, 1, CreateRepoOptions{Name: "repo2"})
  292. require.NoError(t, err)
  293. err = db.Watch(
  294. ctx,
  295. WatchRepositoryOptions{
  296. UserID: 2,
  297. RepoID: repo2.ID,
  298. RepoOwnerID: repo2.OwnerID,
  299. RepoIsPrivate: repo2.IsPrivate,
  300. },
  301. )
  302. require.NoError(t, err)
  303. // It is OK to watch multiple times and just be noop.
  304. err = db.Watch(
  305. ctx,
  306. WatchRepositoryOptions{
  307. UserID: 2,
  308. RepoID: repo2.ID,
  309. RepoOwnerID: repo2.OwnerID,
  310. RepoIsPrivate: repo2.IsPrivate,
  311. },
  312. )
  313. require.NoError(t, err)
  314. repo2, err = db.GetByID(ctx, repo2.ID)
  315. require.NoError(t, err)
  316. assert.Equal(t, 2, repo2.NumWatches) // The owner is watching the repo by default.
  317. })
  318. }
  319. func reposHasForkedBy(t *testing.T, ctx context.Context, db *repositories) {
  320. has := db.HasForkedBy(ctx, 1, 2)
  321. assert.False(t, has)
  322. _, err := NewRepositoriesStore(db.DB).Create(
  323. ctx,
  324. 2,
  325. CreateRepoOptions{
  326. Name: "repo1",
  327. ForkID: 1,
  328. },
  329. )
  330. require.NoError(t, err)
  331. has = db.HasForkedBy(ctx, 1, 2)
  332. assert.True(t, has)
  333. }