You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

629 lines
19 KiB

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