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.

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