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.

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