本站源代码
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.

628 lines
19KB

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