Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

org_test.go 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. // Copyright 2017 The Gitea 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 models
  5. import (
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestUser_IsOwnedBy(t *testing.T) {
  10. assert.NoError(t, PrepareTestDatabase())
  11. org := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  12. assert.True(t, org.IsOwnedBy(2))
  13. assert.False(t, org.IsOwnedBy(1))
  14. assert.False(t, org.IsOwnedBy(3))
  15. assert.False(t, org.IsOwnedBy(4))
  16. nonOrg := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  17. assert.False(t, nonOrg.IsOwnedBy(2))
  18. assert.False(t, nonOrg.IsOwnedBy(3))
  19. }
  20. func TestUser_IsOrgMember(t *testing.T) {
  21. assert.NoError(t, PrepareTestDatabase())
  22. org := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  23. assert.True(t, org.IsOrgMember(2))
  24. assert.True(t, org.IsOrgMember(4))
  25. assert.False(t, org.IsOrgMember(1))
  26. assert.False(t, org.IsOrgMember(3))
  27. nonOrg := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  28. assert.False(t, nonOrg.IsOrgMember(2))
  29. assert.False(t, nonOrg.IsOrgMember(3))
  30. }
  31. func TestUser_GetTeam(t *testing.T) {
  32. assert.NoError(t, PrepareTestDatabase())
  33. org := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  34. team, err := org.GetTeam("team1")
  35. assert.NoError(t, err)
  36. assert.Equal(t, org.ID, team.OrgID)
  37. assert.Equal(t, "team1", team.LowerName)
  38. _, err = org.GetTeam("does not exist")
  39. assert.Equal(t, ErrTeamNotExist, err)
  40. nonOrg := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  41. _, err = nonOrg.GetTeam("team")
  42. assert.Equal(t, ErrTeamNotExist, err)
  43. }
  44. func TestUser_GetOwnerTeam(t *testing.T) {
  45. assert.NoError(t, PrepareTestDatabase())
  46. org := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  47. team, err := org.GetOwnerTeam()
  48. assert.NoError(t, err)
  49. assert.Equal(t, org.ID, team.OrgID)
  50. nonOrg := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  51. _, err = nonOrg.GetOwnerTeam()
  52. assert.Equal(t, ErrTeamNotExist, err)
  53. }
  54. func TestUser_GetTeams(t *testing.T) {
  55. assert.NoError(t, PrepareTestDatabase())
  56. org := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  57. assert.NoError(t, org.GetTeams())
  58. if assert.Len(t, org.Teams, 2) {
  59. assert.Equal(t, int64(1), org.Teams[0].ID)
  60. assert.Equal(t, int64(2), org.Teams[1].ID)
  61. }
  62. }
  63. func TestUser_GetMembers(t *testing.T) {
  64. assert.NoError(t, PrepareTestDatabase())
  65. org := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  66. assert.NoError(t, org.GetMembers())
  67. if assert.Len(t, org.Members, 2) {
  68. assert.Equal(t, int64(2), org.Members[0].ID)
  69. assert.Equal(t, int64(4), org.Members[1].ID)
  70. }
  71. }
  72. func TestUser_AddMember(t *testing.T) {
  73. assert.NoError(t, PrepareTestDatabase())
  74. org := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  75. // add a user that is not a member
  76. AssertNotExistsBean(t, &OrgUser{UID: 5, OrgID: 3})
  77. prevNumMembers := org.NumMembers
  78. assert.NoError(t, org.AddMember(5))
  79. AssertExistsAndLoadBean(t, &OrgUser{UID: 5, OrgID: 3})
  80. org = AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  81. assert.Equal(t, prevNumMembers+1, org.NumMembers)
  82. // add a user that is already a member
  83. AssertExistsAndLoadBean(t, &OrgUser{UID: 4, OrgID: 3})
  84. prevNumMembers = org.NumMembers
  85. assert.NoError(t, org.AddMember(4))
  86. AssertExistsAndLoadBean(t, &OrgUser{UID: 4, OrgID: 3})
  87. org = AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  88. assert.Equal(t, prevNumMembers, org.NumMembers)
  89. CheckConsistencyFor(t, &User{})
  90. }
  91. func TestUser_RemoveMember(t *testing.T) {
  92. assert.NoError(t, PrepareTestDatabase())
  93. org := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  94. // remove a user that is a member
  95. AssertExistsAndLoadBean(t, &OrgUser{UID: 4, OrgID: 3})
  96. prevNumMembers := org.NumMembers
  97. assert.NoError(t, org.RemoveMember(4))
  98. AssertNotExistsBean(t, &OrgUser{UID: 4, OrgID: 3})
  99. org = AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  100. assert.Equal(t, prevNumMembers-1, org.NumMembers)
  101. // remove a user that is not a member
  102. AssertNotExistsBean(t, &OrgUser{UID: 5, OrgID: 3})
  103. prevNumMembers = org.NumMembers
  104. assert.NoError(t, org.RemoveMember(5))
  105. AssertNotExistsBean(t, &OrgUser{UID: 5, OrgID: 3})
  106. org = AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  107. assert.Equal(t, prevNumMembers, org.NumMembers)
  108. CheckConsistencyFor(t, &User{}, &Team{})
  109. }
  110. func TestUser_RemoveOrgRepo(t *testing.T) {
  111. assert.NoError(t, PrepareTestDatabase())
  112. org := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  113. repo := AssertExistsAndLoadBean(t, &Repository{OwnerID: org.ID}).(*Repository)
  114. // remove a repo that does belong to org
  115. AssertExistsAndLoadBean(t, &TeamRepo{RepoID: repo.ID, OrgID: org.ID})
  116. assert.NoError(t, org.RemoveOrgRepo(repo.ID))
  117. AssertNotExistsBean(t, &TeamRepo{RepoID: repo.ID, OrgID: org.ID})
  118. AssertExistsAndLoadBean(t, &Repository{ID: repo.ID}) // repo should still exist
  119. // remove a repo that does not belong to org
  120. assert.NoError(t, org.RemoveOrgRepo(repo.ID))
  121. AssertNotExistsBean(t, &TeamRepo{RepoID: repo.ID, OrgID: org.ID})
  122. assert.NoError(t, org.RemoveOrgRepo(NonexistentID))
  123. CheckConsistencyFor(t,
  124. &User{ID: org.ID},
  125. &Team{OrgID: org.ID},
  126. &Repository{ID: repo.ID})
  127. }
  128. func TestCreateOrganization(t *testing.T) {
  129. // successful creation of org
  130. assert.NoError(t, PrepareTestDatabase())
  131. owner := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  132. const newOrgName = "neworg"
  133. org := &User{
  134. Name: newOrgName,
  135. }
  136. AssertNotExistsBean(t, &User{Name: newOrgName, Type: UserTypeOrganization})
  137. assert.NoError(t, CreateOrganization(org, owner))
  138. org = AssertExistsAndLoadBean(t,
  139. &User{Name: newOrgName, Type: UserTypeOrganization}).(*User)
  140. ownerTeam := AssertExistsAndLoadBean(t,
  141. &Team{Name: ownerTeamName, OrgID: org.ID}).(*Team)
  142. AssertExistsAndLoadBean(t, &TeamUser{UID: owner.ID, TeamID: ownerTeam.ID})
  143. CheckConsistencyFor(t, &User{}, &Team{})
  144. }
  145. func TestCreateOrganization2(t *testing.T) {
  146. // unauthorized creation of org
  147. assert.NoError(t, PrepareTestDatabase())
  148. owner := AssertExistsAndLoadBean(t, &User{ID: 5}).(*User)
  149. const newOrgName = "neworg"
  150. org := &User{
  151. Name: newOrgName,
  152. }
  153. AssertNotExistsBean(t, &User{Name: newOrgName, Type: UserTypeOrganization})
  154. err := CreateOrganization(org, owner)
  155. assert.Error(t, err)
  156. assert.True(t, IsErrUserNotAllowedCreateOrg(err))
  157. AssertNotExistsBean(t, &User{Name: newOrgName, Type: UserTypeOrganization})
  158. CheckConsistencyFor(t, &User{}, &Team{})
  159. }
  160. func TestCreateOrganization3(t *testing.T) {
  161. // create org with same name as existent org
  162. assert.NoError(t, PrepareTestDatabase())
  163. owner := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  164. org := &User{Name: "user3"} // should already exist
  165. AssertExistsAndLoadBean(t, &User{Name: org.Name}) // sanity check
  166. err := CreateOrganization(org, owner)
  167. assert.Error(t, err)
  168. assert.True(t, IsErrUserAlreadyExist(err))
  169. CheckConsistencyFor(t, &User{}, &Team{})
  170. }
  171. func TestCreateOrganization4(t *testing.T) {
  172. // create org with unusable name
  173. assert.NoError(t, PrepareTestDatabase())
  174. owner := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  175. err := CreateOrganization(&User{Name: "assets"}, owner)
  176. assert.Error(t, err)
  177. assert.True(t, IsErrNameReserved(err))
  178. CheckConsistencyFor(t, &User{}, &Team{})
  179. }
  180. func TestGetOrgByName(t *testing.T) {
  181. assert.NoError(t, PrepareTestDatabase())
  182. org, err := GetOrgByName("user3")
  183. assert.NoError(t, err)
  184. assert.EqualValues(t, 3, org.ID)
  185. assert.Equal(t, "user3", org.Name)
  186. org, err = GetOrgByName("user2") // user2 is an individual
  187. assert.True(t, IsErrOrgNotExist(err))
  188. org, err = GetOrgByName("") // corner case
  189. assert.True(t, IsErrOrgNotExist(err))
  190. }
  191. func TestCountOrganizations(t *testing.T) {
  192. assert.NoError(t, PrepareTestDatabase())
  193. expected, err := x.Where("type=?", UserTypeOrganization).Count(&User{})
  194. assert.NoError(t, err)
  195. assert.Equal(t, expected, CountOrganizations())
  196. }
  197. func TestDeleteOrganization(t *testing.T) {
  198. assert.NoError(t, PrepareTestDatabase())
  199. org := AssertExistsAndLoadBean(t, &User{ID: 6}).(*User)
  200. assert.NoError(t, DeleteOrganization(org))
  201. AssertNotExistsBean(t, &User{ID: 6})
  202. AssertNotExistsBean(t, &OrgUser{OrgID: 6})
  203. AssertNotExistsBean(t, &Team{OrgID: 6})
  204. org = AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  205. err := DeleteOrganization(org)
  206. assert.Error(t, err)
  207. assert.True(t, IsErrUserOwnRepos(err))
  208. nonOrg := AssertExistsAndLoadBean(t, &User{ID: 5}).(*User)
  209. assert.Error(t, DeleteOrganization(nonOrg))
  210. CheckConsistencyFor(t, &User{}, &Team{})
  211. }
  212. func TestIsOrganizationOwner(t *testing.T) {
  213. assert.NoError(t, PrepareTestDatabase())
  214. assert.True(t, IsOrganizationOwner(3, 2))
  215. assert.False(t, IsOrganizationOwner(3, 3))
  216. assert.True(t, IsOrganizationOwner(6, 5))
  217. assert.False(t, IsOrganizationOwner(6, 4))
  218. assert.False(t, IsOrganizationOwner(NonexistentID, NonexistentID))
  219. }
  220. func TestIsOrganizationMember(t *testing.T) {
  221. assert.NoError(t, PrepareTestDatabase())
  222. assert.True(t, IsOrganizationMember(3, 2))
  223. assert.False(t, IsOrganizationMember(3, 3))
  224. assert.True(t, IsOrganizationMember(3, 4))
  225. assert.True(t, IsOrganizationMember(6, 5))
  226. assert.False(t, IsOrganizationMember(6, 4))
  227. assert.False(t, IsOrganizationMember(NonexistentID, NonexistentID))
  228. }
  229. func TestIsPublicMembership(t *testing.T) {
  230. assert.NoError(t, PrepareTestDatabase())
  231. assert.True(t, IsPublicMembership(3, 2))
  232. assert.False(t, IsPublicMembership(3, 3))
  233. assert.False(t, IsPublicMembership(3, 4))
  234. assert.True(t, IsPublicMembership(6, 5))
  235. assert.False(t, IsPublicMembership(6, 4))
  236. assert.False(t, IsPublicMembership(NonexistentID, NonexistentID))
  237. }
  238. func TestGetOrgsByUserID(t *testing.T) {
  239. assert.NoError(t, PrepareTestDatabase())
  240. orgs, err := GetOrgsByUserID(4, true)
  241. assert.NoError(t, err)
  242. if assert.Len(t, orgs, 1) {
  243. assert.EqualValues(t, 3, orgs[0].ID)
  244. }
  245. orgs, err = GetOrgsByUserID(4, false)
  246. assert.NoError(t, err)
  247. assert.Len(t, orgs, 0)
  248. }
  249. func TestGetOwnedOrgsByUserID(t *testing.T) {
  250. assert.NoError(t, PrepareTestDatabase())
  251. orgs, err := GetOwnedOrgsByUserID(2)
  252. assert.NoError(t, err)
  253. if assert.Len(t, orgs, 1) {
  254. assert.EqualValues(t, 3, orgs[0].ID)
  255. }
  256. orgs, err = GetOwnedOrgsByUserID(4)
  257. assert.NoError(t, err)
  258. assert.Len(t, orgs, 0)
  259. }
  260. func TestGetOwnedOrgsByUserIDDesc(t *testing.T) {
  261. assert.NoError(t, PrepareTestDatabase())
  262. orgs, err := GetOwnedOrgsByUserIDDesc(5, "id")
  263. assert.NoError(t, err)
  264. if assert.Len(t, orgs, 2) {
  265. assert.EqualValues(t, 7, orgs[0].ID)
  266. assert.EqualValues(t, 6, orgs[1].ID)
  267. }
  268. orgs, err = GetOwnedOrgsByUserIDDesc(4, "id")
  269. assert.NoError(t, err)
  270. assert.Len(t, orgs, 0)
  271. }
  272. func TestGetOrgUsersByUserID(t *testing.T) {
  273. assert.NoError(t, PrepareTestDatabase())
  274. orgUsers, err := GetOrgUsersByUserID(5, true)
  275. assert.NoError(t, err)
  276. if assert.Len(t, orgUsers, 2) {
  277. assert.Equal(t, OrgUser{
  278. ID: orgUsers[0].ID,
  279. OrgID: 6,
  280. UID: 5,
  281. IsOwner: true,
  282. IsPublic: true,
  283. NumTeams: 1}, *orgUsers[0])
  284. assert.Equal(t, OrgUser{
  285. ID: orgUsers[1].ID,
  286. OrgID: 7,
  287. UID: 5,
  288. IsOwner: true,
  289. IsPublic: false,
  290. NumTeams: 1}, *orgUsers[1])
  291. }
  292. publicOrgUsers, err := GetOrgUsersByUserID(5, false)
  293. assert.NoError(t, err)
  294. assert.Len(t, publicOrgUsers, 1)
  295. assert.Equal(t, *orgUsers[0], *publicOrgUsers[0])
  296. orgUsers, err = GetOrgUsersByUserID(1, true)
  297. assert.NoError(t, err)
  298. assert.Len(t, orgUsers, 0)
  299. }
  300. func TestGetOrgUsersByOrgID(t *testing.T) {
  301. assert.NoError(t, PrepareTestDatabase())
  302. orgUsers, err := GetOrgUsersByOrgID(3)
  303. assert.NoError(t, err)
  304. if assert.Len(t, orgUsers, 2) {
  305. assert.Equal(t, OrgUser{
  306. ID: orgUsers[0].ID,
  307. OrgID: 3,
  308. UID: 2,
  309. IsOwner: true,
  310. IsPublic: true,
  311. NumTeams: 1}, *orgUsers[0])
  312. assert.Equal(t, OrgUser{
  313. ID: orgUsers[1].ID,
  314. OrgID: 3,
  315. UID: 4,
  316. IsOwner: false,
  317. IsPublic: false,
  318. NumTeams: 0}, *orgUsers[1])
  319. }
  320. orgUsers, err = GetOrgUsersByOrgID(NonexistentID)
  321. assert.NoError(t, err)
  322. assert.Len(t, orgUsers, 0)
  323. }
  324. func TestChangeOrgUserStatus(t *testing.T) {
  325. assert.NoError(t, PrepareTestDatabase())
  326. testSuccess := func(orgID, userID int64, public bool) {
  327. assert.NoError(t, ChangeOrgUserStatus(orgID, userID, public))
  328. orgUser := AssertExistsAndLoadBean(t, &OrgUser{OrgID: orgID, UID: userID}).(*OrgUser)
  329. assert.Equal(t, public, orgUser.IsPublic)
  330. }
  331. testSuccess(3, 2, false)
  332. testSuccess(3, 2, false)
  333. testSuccess(3, 4, true)
  334. assert.NoError(t, ChangeOrgUserStatus(NonexistentID, NonexistentID, true))
  335. }
  336. func TestAddOrgUser(t *testing.T) {
  337. assert.NoError(t, PrepareTestDatabase())
  338. testSuccess := func(orgID, userID int64) {
  339. org := AssertExistsAndLoadBean(t, &User{ID: orgID}).(*User)
  340. expectedNumMembers := org.NumMembers
  341. if !BeanExists(t, &OrgUser{OrgID: orgID, UID: userID}) {
  342. expectedNumMembers++
  343. }
  344. assert.NoError(t, AddOrgUser(orgID, userID))
  345. AssertExistsAndLoadBean(t, &OrgUser{OrgID: orgID, UID: userID})
  346. org = AssertExistsAndLoadBean(t, &User{ID: orgID}).(*User)
  347. assert.EqualValues(t, expectedNumMembers, org.NumMembers)
  348. }
  349. testSuccess(3, 5)
  350. testSuccess(3, 5)
  351. testSuccess(6, 2)
  352. CheckConsistencyFor(t, &User{}, &Team{})
  353. }
  354. func TestRemoveOrgUser(t *testing.T) {
  355. assert.NoError(t, PrepareTestDatabase())
  356. testSuccess := func(orgID, userID int64) {
  357. org := AssertExistsAndLoadBean(t, &User{ID: orgID}).(*User)
  358. expectedNumMembers := org.NumMembers
  359. if BeanExists(t, &OrgUser{OrgID: orgID, UID: userID}) {
  360. expectedNumMembers--
  361. }
  362. assert.NoError(t, RemoveOrgUser(orgID, userID))
  363. AssertNotExistsBean(t, &OrgUser{OrgID: orgID, UID: userID})
  364. org = AssertExistsAndLoadBean(t, &User{ID: orgID}).(*User)
  365. assert.EqualValues(t, expectedNumMembers, org.NumMembers)
  366. }
  367. testSuccess(3, 4)
  368. testSuccess(3, 4)
  369. err := RemoveOrgUser(7, 5)
  370. assert.Error(t, err)
  371. assert.True(t, IsErrLastOrgOwner(err))
  372. AssertExistsAndLoadBean(t, &OrgUser{OrgID: 7, UID: 5})
  373. CheckConsistencyFor(t, &User{}, &Team{})
  374. }
  375. func TestUser_GetUserTeamIDs(t *testing.T) {
  376. assert.NoError(t, PrepareTestDatabase())
  377. org := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  378. testSuccess := func(userID int64, expected []int64) {
  379. teamIDs, err := org.GetUserTeamIDs(userID)
  380. assert.NoError(t, err)
  381. assert.Equal(t, expected, teamIDs)
  382. }
  383. testSuccess(2, []int64{1, 2})
  384. testSuccess(4, []int64{2})
  385. testSuccess(NonexistentID, []int64{})
  386. }
  387. func TestAccessibleReposEnv_CountRepos(t *testing.T) {
  388. assert.NoError(t, PrepareTestDatabase())
  389. org := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  390. testSuccess := func(userID, expectedCount int64) {
  391. env, err := org.AccessibleReposEnv(userID)
  392. assert.NoError(t, err)
  393. count, err := env.CountRepos()
  394. assert.NoError(t, err)
  395. assert.EqualValues(t, expectedCount, count)
  396. }
  397. testSuccess(2, 2)
  398. testSuccess(4, 1)
  399. }
  400. func TestAccessibleReposEnv_RepoIDs(t *testing.T) {
  401. assert.NoError(t, PrepareTestDatabase())
  402. org := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  403. testSuccess := func(userID, page, pageSize int64, expectedRepoIDs []int64) {
  404. env, err := org.AccessibleReposEnv(userID)
  405. assert.NoError(t, err)
  406. repoIDs, err := env.RepoIDs(1, 100)
  407. assert.NoError(t, err)
  408. assert.Equal(t, expectedRepoIDs, repoIDs)
  409. }
  410. testSuccess(2, 1, 100, []int64{3, 5})
  411. testSuccess(4, 0, 100, []int64{3})
  412. }
  413. func TestAccessibleReposEnv_Repos(t *testing.T) {
  414. assert.NoError(t, PrepareTestDatabase())
  415. org := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  416. testSuccess := func(userID int64, expectedRepoIDs []int64) {
  417. env, err := org.AccessibleReposEnv(userID)
  418. assert.NoError(t, err)
  419. repos, err := env.Repos(1, 100)
  420. assert.NoError(t, err)
  421. expectedRepos := make([]*Repository, len(expectedRepoIDs))
  422. for i, repoID := range expectedRepoIDs {
  423. expectedRepos[i] = AssertExistsAndLoadBean(t,
  424. &Repository{ID: repoID}).(*Repository)
  425. }
  426. assert.Equal(t, expectedRepos, repos)
  427. }
  428. testSuccess(2, []int64{3, 5})
  429. testSuccess(4, []int64{3})
  430. }
  431. func TestAccessibleReposEnv_MirrorRepos(t *testing.T) {
  432. assert.NoError(t, PrepareTestDatabase())
  433. org := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  434. testSuccess := func(userID int64, expectedRepoIDs []int64) {
  435. env, err := org.AccessibleReposEnv(userID)
  436. assert.NoError(t, err)
  437. repos, err := env.MirrorRepos()
  438. assert.NoError(t, err)
  439. expectedRepos := make([]*Repository, len(expectedRepoIDs))
  440. for i, repoID := range expectedRepoIDs {
  441. expectedRepos[i] = AssertExistsAndLoadBean(t,
  442. &Repository{ID: repoID}).(*Repository)
  443. }
  444. assert.Equal(t, expectedRepos, repos)
  445. }
  446. testSuccess(2, []int64{5})
  447. testSuccess(4, []int64{})
  448. }