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.

520 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. 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. 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. func TestUser_GetMembers(t *testing.T) {
  63. assert.NoError(t, PrepareTestDatabase())
  64. org := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  65. assert.NoError(t, org.GetMembers())
  66. assert.Len(t, org.Members, 2)
  67. assert.Equal(t, int64(2), org.Members[0].ID)
  68. assert.Equal(t, int64(4), org.Members[1].ID)
  69. }
  70. func TestUser_AddMember(t *testing.T) {
  71. assert.NoError(t, PrepareTestDatabase())
  72. org := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  73. // add a user that is not a member
  74. AssertNotExistsBean(t, &OrgUser{UID: 5, OrgID: 3})
  75. prevNumMembers := org.NumMembers
  76. assert.NoError(t, org.AddMember(5))
  77. AssertExistsAndLoadBean(t, &OrgUser{UID: 5, OrgID: 3})
  78. org = AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  79. assert.Equal(t, prevNumMembers+1, org.NumMembers)
  80. // add a user that is already a member
  81. AssertExistsAndLoadBean(t, &OrgUser{UID: 4, OrgID: 3})
  82. prevNumMembers = org.NumMembers
  83. assert.NoError(t, org.AddMember(4))
  84. AssertExistsAndLoadBean(t, &OrgUser{UID: 4, OrgID: 3})
  85. org = AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  86. assert.Equal(t, prevNumMembers, org.NumMembers)
  87. }
  88. func TestUser_RemoveMember(t *testing.T) {
  89. assert.NoError(t, PrepareTestDatabase())
  90. org := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  91. // remove a user that is a member
  92. AssertExistsAndLoadBean(t, &OrgUser{UID: 4, OrgID: 3})
  93. prevNumMembers := org.NumMembers
  94. assert.NoError(t, org.RemoveMember(4))
  95. AssertNotExistsBean(t, &OrgUser{UID: 4, OrgID: 3})
  96. org = AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  97. assert.Equal(t, prevNumMembers-1, org.NumMembers)
  98. // remove a user that is not a member
  99. AssertNotExistsBean(t, &OrgUser{UID: 5, OrgID: 3})
  100. prevNumMembers = org.NumMembers
  101. assert.NoError(t, org.RemoveMember(5))
  102. AssertNotExistsBean(t, &OrgUser{UID: 5, OrgID: 3})
  103. org = AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  104. assert.Equal(t, prevNumMembers, org.NumMembers)
  105. }
  106. func TestUser_RemoveOrgRepo(t *testing.T) {
  107. assert.NoError(t, PrepareTestDatabase())
  108. org := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  109. repo := AssertExistsAndLoadBean(t, &Repository{OwnerID: org.ID}).(*Repository)
  110. // remove a repo that does belong to org
  111. AssertExistsAndLoadBean(t, &TeamRepo{RepoID: repo.ID, OrgID: org.ID})
  112. assert.NoError(t, org.RemoveOrgRepo(repo.ID))
  113. AssertNotExistsBean(t, &TeamRepo{RepoID: repo.ID, OrgID: org.ID})
  114. AssertExistsAndLoadBean(t, &Repository{ID: repo.ID}) // repo should still exist
  115. // remove a repo that does not belong to org
  116. assert.NoError(t, org.RemoveOrgRepo(repo.ID))
  117. AssertNotExistsBean(t, &TeamRepo{RepoID: repo.ID, OrgID: org.ID})
  118. assert.NoError(t, org.RemoveOrgRepo(NonexistentID))
  119. }
  120. func TestCreateOrganization(t *testing.T) {
  121. // successful creation of org
  122. assert.NoError(t, PrepareTestDatabase())
  123. owner := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  124. const newOrgName = "neworg"
  125. org := &User{
  126. Name: newOrgName,
  127. }
  128. AssertNotExistsBean(t, &User{Name: newOrgName, Type: UserTypeOrganization})
  129. assert.NoError(t, CreateOrganization(org, owner))
  130. org = AssertExistsAndLoadBean(t,
  131. &User{Name: newOrgName, Type: UserTypeOrganization}).(*User)
  132. ownerTeam := AssertExistsAndLoadBean(t,
  133. &Team{Name: ownerTeamName, OrgID: org.ID}).(*Team)
  134. AssertExistsAndLoadBean(t, &TeamUser{UID: owner.ID, TeamID: ownerTeam.ID})
  135. }
  136. func TestCreateOrganization2(t *testing.T) {
  137. // unauthorized creation of org
  138. assert.NoError(t, PrepareTestDatabase())
  139. owner := AssertExistsAndLoadBean(t, &User{ID: 5}).(*User)
  140. const newOrgName = "neworg"
  141. org := &User{
  142. Name: newOrgName,
  143. }
  144. AssertNotExistsBean(t, &User{Name: newOrgName, Type: UserTypeOrganization})
  145. err := CreateOrganization(org, owner)
  146. assert.Error(t, err)
  147. assert.True(t, IsErrUserNotAllowedCreateOrg(err))
  148. AssertNotExistsBean(t, &User{Name: newOrgName, Type: UserTypeOrganization})
  149. }
  150. func TestCreateOrganization3(t *testing.T) {
  151. // create org with same name as existent org
  152. assert.NoError(t, PrepareTestDatabase())
  153. owner := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  154. org := &User{Name: "user3"} // should already exist
  155. AssertExistsAndLoadBean(t, &User{Name: org.Name}) // sanity check
  156. err := CreateOrganization(org, owner)
  157. assert.Error(t, err)
  158. assert.True(t, IsErrUserAlreadyExist(err))
  159. }
  160. func TestCreateOrganization4(t *testing.T) {
  161. // create org with unusable name
  162. assert.NoError(t, PrepareTestDatabase())
  163. owner := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  164. err := CreateOrganization(&User{Name: "assets"}, owner)
  165. assert.Error(t, err)
  166. assert.True(t, IsErrNameReserved(err))
  167. }
  168. func TestGetOrgByName(t *testing.T) {
  169. assert.NoError(t, PrepareTestDatabase())
  170. org, err := GetOrgByName("user3")
  171. assert.NoError(t, err)
  172. assert.EqualValues(t, 3, org.ID)
  173. assert.Equal(t, "user3", org.Name)
  174. org, err = GetOrgByName("user2") // user2 is an individual
  175. assert.Equal(t, ErrOrgNotExist, err)
  176. org, err = GetOrgByName("") // corner case
  177. assert.Equal(t, ErrOrgNotExist, err)
  178. }
  179. func TestCountOrganizations(t *testing.T) {
  180. assert.NoError(t, PrepareTestDatabase())
  181. expected, err := x.Where("type=?", UserTypeOrganization).Count(&User{})
  182. assert.NoError(t, err)
  183. assert.Equal(t, expected, CountOrganizations())
  184. }
  185. func TestOrganizations(t *testing.T) {
  186. assert.NoError(t, PrepareTestDatabase())
  187. testSuccess := func(opts *SearchUserOptions, expectedOrgIDs []int64) {
  188. orgs, err := Organizations(opts)
  189. assert.NoError(t, err)
  190. assert.Len(t, orgs, len(expectedOrgIDs))
  191. for i, expectedOrgID := range expectedOrgIDs {
  192. assert.EqualValues(t, expectedOrgID, orgs[i].ID)
  193. }
  194. }
  195. testSuccess(&SearchUserOptions{OrderBy: "id ASC", Page: 1, PageSize: 2},
  196. []int64{3, 6})
  197. testSuccess(&SearchUserOptions{OrderBy: "id ASC", Page: 2, PageSize: 2},
  198. []int64{7})
  199. testSuccess(&SearchUserOptions{Page: 3, PageSize: 2},
  200. []int64{})
  201. }
  202. func TestDeleteOrganization(t *testing.T) {
  203. assert.NoError(t, PrepareTestDatabase())
  204. org := AssertExistsAndLoadBean(t, &User{ID: 6}).(*User)
  205. assert.NoError(t, DeleteOrganization(org))
  206. AssertNotExistsBean(t, &User{ID: 6})
  207. AssertNotExistsBean(t, &OrgUser{OrgID: 6})
  208. AssertNotExistsBean(t, &Team{OrgID: 6})
  209. org = AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  210. err := DeleteOrganization(org)
  211. assert.Error(t, err)
  212. assert.True(t, IsErrUserOwnRepos(err))
  213. nonOrg := AssertExistsAndLoadBean(t, &User{ID: 5}).(*User)
  214. assert.Error(t, DeleteOrganization(nonOrg))
  215. }
  216. func TestIsOrganizationOwner(t *testing.T) {
  217. assert.NoError(t, PrepareTestDatabase())
  218. assert.True(t, IsOrganizationOwner(3, 2))
  219. assert.False(t, IsOrganizationOwner(3, 3))
  220. assert.True(t, IsOrganizationOwner(6, 5))
  221. assert.False(t, IsOrganizationOwner(6, 4))
  222. assert.False(t, IsOrganizationOwner(NonexistentID, NonexistentID))
  223. }
  224. func TestIsOrganizationMember(t *testing.T) {
  225. assert.NoError(t, PrepareTestDatabase())
  226. assert.True(t, IsOrganizationMember(3, 2))
  227. assert.False(t, IsOrganizationMember(3, 3))
  228. assert.True(t, IsOrganizationMember(3, 4))
  229. assert.True(t, IsOrganizationMember(6, 5))
  230. assert.False(t, IsOrganizationMember(6, 4))
  231. assert.False(t, IsOrganizationMember(NonexistentID, NonexistentID))
  232. }
  233. func TestIsPublicMembership(t *testing.T) {
  234. assert.NoError(t, PrepareTestDatabase())
  235. assert.True(t, IsPublicMembership(3, 2))
  236. assert.False(t, IsPublicMembership(3, 3))
  237. assert.False(t, IsPublicMembership(3, 4))
  238. assert.True(t, IsPublicMembership(6, 5))
  239. assert.False(t, IsPublicMembership(6, 4))
  240. assert.False(t, IsPublicMembership(NonexistentID, NonexistentID))
  241. }
  242. func TestGetOrgsByUserID(t *testing.T) {
  243. assert.NoError(t, PrepareTestDatabase())
  244. orgs, err := GetOrgsByUserID(4, true)
  245. assert.NoError(t, err)
  246. assert.Len(t, orgs, 1)
  247. assert.EqualValues(t, 3, orgs[0].ID)
  248. orgs, err = GetOrgsByUserID(4, false)
  249. assert.NoError(t, err)
  250. assert.Len(t, orgs, 0)
  251. }
  252. func TestGetOwnedOrgsByUserID(t *testing.T) {
  253. assert.NoError(t, PrepareTestDatabase())
  254. orgs, err := GetOwnedOrgsByUserID(2)
  255. assert.NoError(t, err)
  256. assert.Len(t, orgs, 1)
  257. assert.EqualValues(t, 3, orgs[0].ID)
  258. orgs, err = GetOwnedOrgsByUserID(4)
  259. assert.NoError(t, err)
  260. assert.Len(t, orgs, 0)
  261. }
  262. func TestGetOwnedOrgsByUserIDDesc(t *testing.T) {
  263. assert.NoError(t, PrepareTestDatabase())
  264. orgs, err := GetOwnedOrgsByUserIDDesc(5, "id")
  265. assert.NoError(t, err)
  266. assert.Len(t, orgs, 2)
  267. assert.EqualValues(t, 7, orgs[0].ID)
  268. assert.EqualValues(t, 6, orgs[1].ID)
  269. orgs, err = GetOwnedOrgsByUserIDDesc(4, "id")
  270. assert.NoError(t, err)
  271. assert.Len(t, orgs, 0)
  272. }
  273. func TestGetOrgUsersByUserID(t *testing.T) {
  274. assert.NoError(t, PrepareTestDatabase())
  275. orgUsers, err := GetOrgUsersByUserID(5, true)
  276. assert.NoError(t, err)
  277. assert.Len(t, orgUsers, 2)
  278. assert.Equal(t, OrgUser{
  279. ID: orgUsers[0].ID,
  280. OrgID: 6,
  281. UID: 5,
  282. IsOwner: true,
  283. IsPublic: true,
  284. NumTeams: 1}, *orgUsers[0])
  285. assert.Equal(t, OrgUser{
  286. ID: orgUsers[1].ID,
  287. OrgID: 7,
  288. UID: 5,
  289. IsOwner: true,
  290. IsPublic: false,
  291. NumTeams: 1}, *orgUsers[1])
  292. publicOrgUsers, err := GetOrgUsersByUserID(5, false)
  293. assert.NoError(t, err)
  294. assert.Len(t, publicOrgUsers, 1)
  295. assert.Equal(t, *orgUsers[0], *publicOrgUsers[0])
  296. orgUsers, err = GetOrgUsersByUserID(1, true)
  297. assert.NoError(t, err)
  298. assert.Len(t, orgUsers, 0)
  299. }
  300. func TestGetOrgUsersByOrgID(t *testing.T) {
  301. assert.NoError(t, PrepareTestDatabase())
  302. orgUsers, err := GetOrgUsersByOrgID(3)
  303. assert.NoError(t, err)
  304. assert.Len(t, orgUsers, 2)
  305. assert.Equal(t, OrgUser{
  306. ID: orgUsers[0].ID,
  307. OrgID: 3,
  308. UID: 2,
  309. IsOwner: true,
  310. IsPublic: true,
  311. NumTeams: 1}, *orgUsers[0])
  312. assert.Equal(t, OrgUser{
  313. ID: orgUsers[1].ID,
  314. OrgID: 3,
  315. UID: 4,
  316. IsOwner: false,
  317. IsPublic: false,
  318. NumTeams: 0}, *orgUsers[1])
  319. orgUsers, err = GetOrgUsersByOrgID(NonexistentID)
  320. assert.NoError(t, err)
  321. assert.Len(t, orgUsers, 0)
  322. }
  323. func TestChangeOrgUserStatus(t *testing.T) {
  324. assert.NoError(t, PrepareTestDatabase())
  325. testSuccess := func(orgID, userID int64, public bool) {
  326. assert.NoError(t, ChangeOrgUserStatus(orgID, userID, public))
  327. orgUser := AssertExistsAndLoadBean(t, &OrgUser{OrgID: orgID, UID: userID}).(*OrgUser)
  328. assert.Equal(t, public, orgUser.IsPublic)
  329. }
  330. testSuccess(3, 2, false)
  331. testSuccess(3, 2, false)
  332. testSuccess(3, 4, true)
  333. assert.NoError(t, ChangeOrgUserStatus(NonexistentID, NonexistentID, true))
  334. }
  335. func TestAddOrgUser(t *testing.T) {
  336. assert.NoError(t, PrepareTestDatabase())
  337. testSuccess := func(orgID, userID int64) {
  338. org := AssertExistsAndLoadBean(t, &User{ID: orgID}).(*User)
  339. expectedNumMembers := org.NumMembers
  340. if !BeanExists(t, &OrgUser{OrgID: orgID, UID: userID}) {
  341. expectedNumMembers++
  342. }
  343. assert.NoError(t, AddOrgUser(orgID, userID))
  344. AssertExistsAndLoadBean(t, &OrgUser{OrgID: orgID, UID: userID})
  345. org = AssertExistsAndLoadBean(t, &User{ID: orgID}).(*User)
  346. assert.EqualValues(t, expectedNumMembers, org.NumMembers)
  347. }
  348. testSuccess(3, 5)
  349. testSuccess(3, 5)
  350. testSuccess(6, 2)
  351. }
  352. func TestRemoveOrgUser(t *testing.T) {
  353. assert.NoError(t, PrepareTestDatabase())
  354. testSuccess := func(orgID, userID int64) {
  355. org := AssertExistsAndLoadBean(t, &User{ID: orgID}).(*User)
  356. expectedNumMembers := org.NumMembers
  357. if BeanExists(t, &OrgUser{OrgID: orgID, UID: userID}) {
  358. expectedNumMembers--
  359. }
  360. assert.NoError(t, RemoveOrgUser(orgID, userID))
  361. AssertNotExistsBean(t, &OrgUser{OrgID: orgID, UID: userID})
  362. org = AssertExistsAndLoadBean(t, &User{ID: orgID}).(*User)
  363. assert.EqualValues(t, expectedNumMembers, org.NumMembers)
  364. }
  365. testSuccess(3, 4)
  366. testSuccess(3, 4)
  367. err := RemoveOrgUser(7, 5)
  368. assert.Error(t, err)
  369. assert.True(t, IsErrLastOrgOwner(err))
  370. AssertExistsAndLoadBean(t, &OrgUser{OrgID: 7, UID: 5})
  371. }
  372. func TestUser_GetUserTeamIDs(t *testing.T) {
  373. assert.NoError(t, PrepareTestDatabase())
  374. org := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  375. testSuccess := func(userID int64, expected []int64) {
  376. teamIDs, err := org.GetUserTeamIDs(userID)
  377. assert.NoError(t, err)
  378. assert.Equal(t, expected, teamIDs)
  379. }
  380. testSuccess(2, []int64{1, 2})
  381. testSuccess(4, []int64{2})
  382. testSuccess(NonexistentID, []int64{})
  383. }
  384. func TestAccessibleReposEnv_CountRepos(t *testing.T) {
  385. assert.NoError(t, PrepareTestDatabase())
  386. org := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  387. testSuccess := func(userID, expectedCount int64) {
  388. env, err := org.AccessibleReposEnv(userID)
  389. assert.NoError(t, err)
  390. count, err := env.CountRepos()
  391. assert.NoError(t, err)
  392. assert.EqualValues(t, expectedCount, count)
  393. }
  394. testSuccess(2, 2)
  395. testSuccess(4, 1)
  396. }
  397. func TestAccessibleReposEnv_RepoIDs(t *testing.T) {
  398. assert.NoError(t, PrepareTestDatabase())
  399. org := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  400. testSuccess := func(userID, page, pageSize int64, expectedRepoIDs []int64) {
  401. env, err := org.AccessibleReposEnv(userID)
  402. assert.NoError(t, err)
  403. repoIDs, err := env.RepoIDs(1, 100)
  404. assert.NoError(t, err)
  405. assert.Equal(t, expectedRepoIDs, repoIDs)
  406. }
  407. testSuccess(2, 1, 100, []int64{3, 5})
  408. testSuccess(4, 0, 100, []int64{3})
  409. }
  410. func TestAccessibleReposEnv_Repos(t *testing.T) {
  411. assert.NoError(t, PrepareTestDatabase())
  412. org := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  413. testSuccess := func(userID int64, expectedRepoIDs []int64) {
  414. env, err := org.AccessibleReposEnv(userID)
  415. assert.NoError(t, err)
  416. repos, err := env.Repos(1, 100)
  417. assert.NoError(t, err)
  418. expectedRepos := make([]*Repository, len(expectedRepoIDs))
  419. for i, repoID := range expectedRepoIDs {
  420. expectedRepos[i] = AssertExistsAndLoadBean(t,
  421. &Repository{ID: repoID}).(*Repository)
  422. }
  423. assert.Equal(t, expectedRepos, repos)
  424. }
  425. testSuccess(2, []int64{3, 5})
  426. testSuccess(4, []int64{3})
  427. }
  428. func TestAccessibleReposEnv_MirrorRepos(t *testing.T) {
  429. assert.NoError(t, PrepareTestDatabase())
  430. org := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  431. testSuccess := func(userID int64, expectedRepoIDs []int64) {
  432. env, err := org.AccessibleReposEnv(userID)
  433. assert.NoError(t, err)
  434. repos, err := env.MirrorRepos()
  435. assert.NoError(t, err)
  436. expectedRepos := make([]*Repository, len(expectedRepoIDs))
  437. for i, repoID := range expectedRepoIDs {
  438. expectedRepos[i] = AssertExistsAndLoadBean(t,
  439. &Repository{ID: repoID}).(*Repository)
  440. }
  441. assert.Equal(t, expectedRepos, repos)
  442. }
  443. testSuccess(2, []int64{5})
  444. testSuccess(4, []int64{})
  445. }