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

org_test.go 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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 TestOrganizations(t *testing.T) {
  198. assert.NoError(t, PrepareTestDatabase())
  199. testSuccess := func(opts *SearchUserOptions, expectedOrgIDs []int64) {
  200. orgs, err := Organizations(opts)
  201. assert.NoError(t, err)
  202. if assert.Len(t, orgs, len(expectedOrgIDs)) {
  203. for i, expectedOrgID := range expectedOrgIDs {
  204. assert.EqualValues(t, expectedOrgID, orgs[i].ID)
  205. }
  206. }
  207. }
  208. testSuccess(&SearchUserOptions{OrderBy: "id ASC", Page: 1, PageSize: 2},
  209. []int64{3, 6})
  210. testSuccess(&SearchUserOptions{OrderBy: "id ASC", Page: 2, PageSize: 2},
  211. []int64{7})
  212. testSuccess(&SearchUserOptions{Page: 3, PageSize: 2},
  213. []int64{})
  214. }
  215. func TestDeleteOrganization(t *testing.T) {
  216. assert.NoError(t, PrepareTestDatabase())
  217. org := AssertExistsAndLoadBean(t, &User{ID: 6}).(*User)
  218. assert.NoError(t, DeleteOrganization(org))
  219. AssertNotExistsBean(t, &User{ID: 6})
  220. AssertNotExistsBean(t, &OrgUser{OrgID: 6})
  221. AssertNotExistsBean(t, &Team{OrgID: 6})
  222. org = AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  223. err := DeleteOrganization(org)
  224. assert.Error(t, err)
  225. assert.True(t, IsErrUserOwnRepos(err))
  226. nonOrg := AssertExistsAndLoadBean(t, &User{ID: 5}).(*User)
  227. assert.Error(t, DeleteOrganization(nonOrg))
  228. CheckConsistencyFor(t, &User{}, &Team{})
  229. }
  230. func TestIsOrganizationOwner(t *testing.T) {
  231. assert.NoError(t, PrepareTestDatabase())
  232. assert.True(t, IsOrganizationOwner(3, 2))
  233. assert.False(t, IsOrganizationOwner(3, 3))
  234. assert.True(t, IsOrganizationOwner(6, 5))
  235. assert.False(t, IsOrganizationOwner(6, 4))
  236. assert.False(t, IsOrganizationOwner(NonexistentID, NonexistentID))
  237. }
  238. func TestIsOrganizationMember(t *testing.T) {
  239. assert.NoError(t, PrepareTestDatabase())
  240. assert.True(t, IsOrganizationMember(3, 2))
  241. assert.False(t, IsOrganizationMember(3, 3))
  242. assert.True(t, IsOrganizationMember(3, 4))
  243. assert.True(t, IsOrganizationMember(6, 5))
  244. assert.False(t, IsOrganizationMember(6, 4))
  245. assert.False(t, IsOrganizationMember(NonexistentID, NonexistentID))
  246. }
  247. func TestIsPublicMembership(t *testing.T) {
  248. assert.NoError(t, PrepareTestDatabase())
  249. assert.True(t, IsPublicMembership(3, 2))
  250. assert.False(t, IsPublicMembership(3, 3))
  251. assert.False(t, IsPublicMembership(3, 4))
  252. assert.True(t, IsPublicMembership(6, 5))
  253. assert.False(t, IsPublicMembership(6, 4))
  254. assert.False(t, IsPublicMembership(NonexistentID, NonexistentID))
  255. }
  256. func TestGetOrgsByUserID(t *testing.T) {
  257. assert.NoError(t, PrepareTestDatabase())
  258. orgs, err := GetOrgsByUserID(4, true)
  259. assert.NoError(t, err)
  260. if assert.Len(t, orgs, 1) {
  261. assert.EqualValues(t, 3, orgs[0].ID)
  262. }
  263. orgs, err = GetOrgsByUserID(4, false)
  264. assert.NoError(t, err)
  265. assert.Len(t, orgs, 0)
  266. }
  267. func TestGetOwnedOrgsByUserID(t *testing.T) {
  268. assert.NoError(t, PrepareTestDatabase())
  269. orgs, err := GetOwnedOrgsByUserID(2)
  270. assert.NoError(t, err)
  271. if assert.Len(t, orgs, 1) {
  272. assert.EqualValues(t, 3, orgs[0].ID)
  273. }
  274. orgs, err = GetOwnedOrgsByUserID(4)
  275. assert.NoError(t, err)
  276. assert.Len(t, orgs, 0)
  277. }
  278. func TestGetOwnedOrgsByUserIDDesc(t *testing.T) {
  279. assert.NoError(t, PrepareTestDatabase())
  280. orgs, err := GetOwnedOrgsByUserIDDesc(5, "id")
  281. assert.NoError(t, err)
  282. if assert.Len(t, orgs, 2) {
  283. assert.EqualValues(t, 7, orgs[0].ID)
  284. assert.EqualValues(t, 6, orgs[1].ID)
  285. }
  286. orgs, err = GetOwnedOrgsByUserIDDesc(4, "id")
  287. assert.NoError(t, err)
  288. assert.Len(t, orgs, 0)
  289. }
  290. func TestGetOrgUsersByUserID(t *testing.T) {
  291. assert.NoError(t, PrepareTestDatabase())
  292. orgUsers, err := GetOrgUsersByUserID(5, true)
  293. assert.NoError(t, err)
  294. if assert.Len(t, orgUsers, 2) {
  295. assert.Equal(t, OrgUser{
  296. ID: orgUsers[0].ID,
  297. OrgID: 6,
  298. UID: 5,
  299. IsOwner: true,
  300. IsPublic: true,
  301. NumTeams: 1}, *orgUsers[0])
  302. assert.Equal(t, OrgUser{
  303. ID: orgUsers[1].ID,
  304. OrgID: 7,
  305. UID: 5,
  306. IsOwner: true,
  307. IsPublic: false,
  308. NumTeams: 1}, *orgUsers[1])
  309. }
  310. publicOrgUsers, err := GetOrgUsersByUserID(5, false)
  311. assert.NoError(t, err)
  312. assert.Len(t, publicOrgUsers, 1)
  313. assert.Equal(t, *orgUsers[0], *publicOrgUsers[0])
  314. orgUsers, err = GetOrgUsersByUserID(1, true)
  315. assert.NoError(t, err)
  316. assert.Len(t, orgUsers, 0)
  317. }
  318. func TestGetOrgUsersByOrgID(t *testing.T) {
  319. assert.NoError(t, PrepareTestDatabase())
  320. orgUsers, err := GetOrgUsersByOrgID(3)
  321. assert.NoError(t, err)
  322. if assert.Len(t, orgUsers, 2) {
  323. assert.Equal(t, OrgUser{
  324. ID: orgUsers[0].ID,
  325. OrgID: 3,
  326. UID: 2,
  327. IsOwner: true,
  328. IsPublic: true,
  329. NumTeams: 1}, *orgUsers[0])
  330. assert.Equal(t, OrgUser{
  331. ID: orgUsers[1].ID,
  332. OrgID: 3,
  333. UID: 4,
  334. IsOwner: false,
  335. IsPublic: false,
  336. NumTeams: 0}, *orgUsers[1])
  337. }
  338. orgUsers, err = GetOrgUsersByOrgID(NonexistentID)
  339. assert.NoError(t, err)
  340. assert.Len(t, orgUsers, 0)
  341. }
  342. func TestChangeOrgUserStatus(t *testing.T) {
  343. assert.NoError(t, PrepareTestDatabase())
  344. testSuccess := func(orgID, userID int64, public bool) {
  345. assert.NoError(t, ChangeOrgUserStatus(orgID, userID, public))
  346. orgUser := AssertExistsAndLoadBean(t, &OrgUser{OrgID: orgID, UID: userID}).(*OrgUser)
  347. assert.Equal(t, public, orgUser.IsPublic)
  348. }
  349. testSuccess(3, 2, false)
  350. testSuccess(3, 2, false)
  351. testSuccess(3, 4, true)
  352. assert.NoError(t, ChangeOrgUserStatus(NonexistentID, NonexistentID, true))
  353. }
  354. func TestAddOrgUser(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, AddOrgUser(orgID, userID))
  363. AssertExistsAndLoadBean(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, 5)
  368. testSuccess(3, 5)
  369. testSuccess(6, 2)
  370. CheckConsistencyFor(t, &User{}, &Team{})
  371. }
  372. func TestRemoveOrgUser(t *testing.T) {
  373. assert.NoError(t, PrepareTestDatabase())
  374. testSuccess := func(orgID, userID int64) {
  375. org := AssertExistsAndLoadBean(t, &User{ID: orgID}).(*User)
  376. expectedNumMembers := org.NumMembers
  377. if BeanExists(t, &OrgUser{OrgID: orgID, UID: userID}) {
  378. expectedNumMembers--
  379. }
  380. assert.NoError(t, RemoveOrgUser(orgID, userID))
  381. AssertNotExistsBean(t, &OrgUser{OrgID: orgID, UID: userID})
  382. org = AssertExistsAndLoadBean(t, &User{ID: orgID}).(*User)
  383. assert.EqualValues(t, expectedNumMembers, org.NumMembers)
  384. }
  385. testSuccess(3, 4)
  386. testSuccess(3, 4)
  387. err := RemoveOrgUser(7, 5)
  388. assert.Error(t, err)
  389. assert.True(t, IsErrLastOrgOwner(err))
  390. AssertExistsAndLoadBean(t, &OrgUser{OrgID: 7, UID: 5})
  391. CheckConsistencyFor(t, &User{}, &Team{})
  392. }
  393. func TestUser_GetUserTeamIDs(t *testing.T) {
  394. assert.NoError(t, PrepareTestDatabase())
  395. org := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  396. testSuccess := func(userID int64, expected []int64) {
  397. teamIDs, err := org.GetUserTeamIDs(userID)
  398. assert.NoError(t, err)
  399. assert.Equal(t, expected, teamIDs)
  400. }
  401. testSuccess(2, []int64{1, 2})
  402. testSuccess(4, []int64{2})
  403. testSuccess(NonexistentID, []int64{})
  404. }
  405. func TestAccessibleReposEnv_CountRepos(t *testing.T) {
  406. assert.NoError(t, PrepareTestDatabase())
  407. org := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  408. testSuccess := func(userID, expectedCount int64) {
  409. env, err := org.AccessibleReposEnv(userID)
  410. assert.NoError(t, err)
  411. count, err := env.CountRepos()
  412. assert.NoError(t, err)
  413. assert.EqualValues(t, expectedCount, count)
  414. }
  415. testSuccess(2, 2)
  416. testSuccess(4, 1)
  417. }
  418. func TestAccessibleReposEnv_RepoIDs(t *testing.T) {
  419. assert.NoError(t, PrepareTestDatabase())
  420. org := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  421. testSuccess := func(userID, page, pageSize int64, expectedRepoIDs []int64) {
  422. env, err := org.AccessibleReposEnv(userID)
  423. assert.NoError(t, err)
  424. repoIDs, err := env.RepoIDs(1, 100)
  425. assert.NoError(t, err)
  426. assert.Equal(t, expectedRepoIDs, repoIDs)
  427. }
  428. testSuccess(2, 1, 100, []int64{3, 5})
  429. testSuccess(4, 0, 100, []int64{3})
  430. }
  431. func TestAccessibleReposEnv_Repos(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.Repos(1, 100)
  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{3, 5})
  447. testSuccess(4, []int64{3})
  448. }
  449. func TestAccessibleReposEnv_MirrorRepos(t *testing.T) {
  450. assert.NoError(t, PrepareTestDatabase())
  451. org := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  452. testSuccess := func(userID int64, expectedRepoIDs []int64) {
  453. env, err := org.AccessibleReposEnv(userID)
  454. assert.NoError(t, err)
  455. repos, err := env.MirrorRepos()
  456. assert.NoError(t, err)
  457. expectedRepos := make([]*Repository, len(expectedRepoIDs))
  458. for i, repoID := range expectedRepoIDs {
  459. expectedRepos[i] = AssertExistsAndLoadBean(t,
  460. &Repository{ID: repoID}).(*Repository)
  461. }
  462. assert.Equal(t, expectedRepos, repos)
  463. }
  464. testSuccess(2, []int64{5})
  465. testSuccess(4, []int64{})
  466. }