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.

637 lines
19 KiB

API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
4 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
4 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
4 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
4 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
4 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
4 years ago
  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(&SearchTeamOptions{}))
  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. user := AssertExistsAndLoadBean(t, &User{ID: 5}).(*User)
  232. assert.Error(t, DeleteOrganization(user))
  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, &SearchOrganizationsOptions{All: 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, &SearchOrganizationsOptions{All: 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, &SearchOrganizationsOptions{All: 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(&FindOrgMembersOpts{
  337. ListOptions: ListOptions{},
  338. OrgID: 3,
  339. PublicOnly: false,
  340. })
  341. assert.NoError(t, err)
  342. if assert.Len(t, orgUsers, 3) {
  343. assert.Equal(t, OrgUser{
  344. ID: orgUsers[0].ID,
  345. OrgID: 3,
  346. UID: 2,
  347. IsPublic: true}, *orgUsers[0])
  348. assert.Equal(t, OrgUser{
  349. ID: orgUsers[1].ID,
  350. OrgID: 3,
  351. UID: 4,
  352. IsPublic: false}, *orgUsers[1])
  353. }
  354. orgUsers, err = GetOrgUsersByOrgID(&FindOrgMembersOpts{
  355. ListOptions: ListOptions{},
  356. OrgID: NonexistentID,
  357. PublicOnly: false,
  358. })
  359. assert.NoError(t, err)
  360. assert.Len(t, orgUsers, 0)
  361. }
  362. func TestChangeOrgUserStatus(t *testing.T) {
  363. assert.NoError(t, PrepareTestDatabase())
  364. testSuccess := func(orgID, userID int64, public bool) {
  365. assert.NoError(t, ChangeOrgUserStatus(orgID, userID, public))
  366. orgUser := AssertExistsAndLoadBean(t, &OrgUser{OrgID: orgID, UID: userID}).(*OrgUser)
  367. assert.Equal(t, public, orgUser.IsPublic)
  368. }
  369. testSuccess(3, 2, false)
  370. testSuccess(3, 2, false)
  371. testSuccess(3, 4, true)
  372. assert.NoError(t, ChangeOrgUserStatus(NonexistentID, NonexistentID, true))
  373. }
  374. func TestAddOrgUser(t *testing.T) {
  375. assert.NoError(t, PrepareTestDatabase())
  376. testSuccess := func(orgID, userID int64, isPublic bool) {
  377. org := AssertExistsAndLoadBean(t, &User{ID: orgID}).(*User)
  378. expectedNumMembers := org.NumMembers
  379. if !BeanExists(t, &OrgUser{OrgID: orgID, UID: userID}) {
  380. expectedNumMembers++
  381. }
  382. assert.NoError(t, AddOrgUser(orgID, userID))
  383. ou := &OrgUser{OrgID: orgID, UID: userID}
  384. AssertExistsAndLoadBean(t, ou)
  385. assert.Equal(t, ou.IsPublic, isPublic)
  386. org = AssertExistsAndLoadBean(t, &User{ID: orgID}).(*User)
  387. assert.EqualValues(t, expectedNumMembers, org.NumMembers)
  388. }
  389. setting.Service.DefaultOrgMemberVisible = false
  390. testSuccess(3, 5, false)
  391. testSuccess(3, 5, false)
  392. testSuccess(6, 2, false)
  393. setting.Service.DefaultOrgMemberVisible = true
  394. testSuccess(6, 3, true)
  395. CheckConsistencyFor(t, &User{}, &Team{})
  396. }
  397. func TestRemoveOrgUser(t *testing.T) {
  398. assert.NoError(t, PrepareTestDatabase())
  399. testSuccess := func(orgID, userID int64) {
  400. org := AssertExistsAndLoadBean(t, &User{ID: orgID}).(*User)
  401. expectedNumMembers := org.NumMembers
  402. if BeanExists(t, &OrgUser{OrgID: orgID, UID: userID}) {
  403. expectedNumMembers--
  404. }
  405. assert.NoError(t, RemoveOrgUser(orgID, userID))
  406. AssertNotExistsBean(t, &OrgUser{OrgID: orgID, UID: userID})
  407. org = AssertExistsAndLoadBean(t, &User{ID: orgID}).(*User)
  408. assert.EqualValues(t, expectedNumMembers, org.NumMembers)
  409. }
  410. testSuccess(3, 4)
  411. testSuccess(3, 4)
  412. err := RemoveOrgUser(7, 5)
  413. assert.Error(t, err)
  414. assert.True(t, IsErrLastOrgOwner(err))
  415. AssertExistsAndLoadBean(t, &OrgUser{OrgID: 7, UID: 5})
  416. CheckConsistencyFor(t, &User{}, &Team{})
  417. }
  418. func TestUser_GetUserTeamIDs(t *testing.T) {
  419. assert.NoError(t, PrepareTestDatabase())
  420. org := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  421. testSuccess := func(userID int64, expected []int64) {
  422. teamIDs, err := org.GetUserTeamIDs(userID)
  423. assert.NoError(t, err)
  424. assert.Equal(t, expected, teamIDs)
  425. }
  426. testSuccess(2, []int64{1, 2})
  427. testSuccess(4, []int64{2})
  428. testSuccess(NonexistentID, []int64{})
  429. }
  430. func TestAccessibleReposEnv_CountRepos(t *testing.T) {
  431. assert.NoError(t, PrepareTestDatabase())
  432. org := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  433. testSuccess := func(userID, expectedCount int64) {
  434. env, err := org.AccessibleReposEnv(userID)
  435. assert.NoError(t, err)
  436. count, err := env.CountRepos()
  437. assert.NoError(t, err)
  438. assert.EqualValues(t, expectedCount, count)
  439. }
  440. testSuccess(2, 3)
  441. testSuccess(4, 2)
  442. }
  443. func TestAccessibleReposEnv_RepoIDs(t *testing.T) {
  444. assert.NoError(t, PrepareTestDatabase())
  445. org := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  446. testSuccess := func(userID, _, pageSize int64, expectedRepoIDs []int64) {
  447. env, err := org.AccessibleReposEnv(userID)
  448. assert.NoError(t, err)
  449. repoIDs, err := env.RepoIDs(1, 100)
  450. assert.NoError(t, err)
  451. assert.Equal(t, expectedRepoIDs, repoIDs)
  452. }
  453. testSuccess(2, 1, 100, []int64{3, 5, 32})
  454. testSuccess(4, 0, 100, []int64{3, 32})
  455. }
  456. func TestAccessibleReposEnv_Repos(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.Repos(1, 100)
  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{3, 5, 32})
  472. testSuccess(4, []int64{3, 32})
  473. }
  474. func TestAccessibleReposEnv_MirrorRepos(t *testing.T) {
  475. assert.NoError(t, PrepareTestDatabase())
  476. org := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  477. testSuccess := func(userID int64, expectedRepoIDs []int64) {
  478. env, err := org.AccessibleReposEnv(userID)
  479. assert.NoError(t, err)
  480. repos, err := env.MirrorRepos()
  481. assert.NoError(t, err)
  482. expectedRepos := make([]*Repository, len(expectedRepoIDs))
  483. for i, repoID := range expectedRepoIDs {
  484. expectedRepos[i] = AssertExistsAndLoadBean(t,
  485. &Repository{ID: repoID}).(*Repository)
  486. }
  487. assert.Equal(t, expectedRepos, repos)
  488. }
  489. testSuccess(2, []int64{5})
  490. testSuccess(4, []int64{})
  491. }
  492. func TestHasOrgVisibleTypePublic(t *testing.T) {
  493. assert.NoError(t, PrepareTestDatabase())
  494. owner := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  495. user3 := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  496. const newOrgName = "test-org-public"
  497. org := &User{
  498. Name: newOrgName,
  499. Visibility: structs.VisibleTypePublic,
  500. }
  501. AssertNotExistsBean(t, &User{Name: org.Name, Type: UserTypeOrganization})
  502. assert.NoError(t, CreateOrganization(org, owner))
  503. org = AssertExistsAndLoadBean(t,
  504. &User{Name: org.Name, Type: UserTypeOrganization}).(*User)
  505. test1 := HasOrgVisible(org, owner)
  506. test2 := HasOrgVisible(org, user3)
  507. test3 := HasOrgVisible(org, nil)
  508. assert.Equal(t, test1, true) // owner of org
  509. assert.Equal(t, test2, true) // user not a part of org
  510. assert.Equal(t, test3, true) // logged out user
  511. }
  512. func TestHasOrgVisibleTypeLimited(t *testing.T) {
  513. assert.NoError(t, PrepareTestDatabase())
  514. owner := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  515. user3 := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  516. const newOrgName = "test-org-limited"
  517. org := &User{
  518. Name: newOrgName,
  519. Visibility: structs.VisibleTypeLimited,
  520. }
  521. AssertNotExistsBean(t, &User{Name: org.Name, Type: UserTypeOrganization})
  522. assert.NoError(t, CreateOrganization(org, owner))
  523. org = AssertExistsAndLoadBean(t,
  524. &User{Name: org.Name, Type: UserTypeOrganization}).(*User)
  525. test1 := HasOrgVisible(org, owner)
  526. test2 := HasOrgVisible(org, user3)
  527. test3 := HasOrgVisible(org, nil)
  528. assert.Equal(t, test1, true) // owner of org
  529. assert.Equal(t, test2, true) // user not a part of org
  530. assert.Equal(t, test3, false) // logged out user
  531. }
  532. func TestHasOrgVisibleTypePrivate(t *testing.T) {
  533. assert.NoError(t, PrepareTestDatabase())
  534. owner := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  535. user3 := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  536. const newOrgName = "test-org-private"
  537. org := &User{
  538. Name: newOrgName,
  539. Visibility: structs.VisibleTypePrivate,
  540. }
  541. AssertNotExistsBean(t, &User{Name: org.Name, Type: UserTypeOrganization})
  542. assert.NoError(t, CreateOrganization(org, owner))
  543. org = AssertExistsAndLoadBean(t,
  544. &User{Name: org.Name, Type: UserTypeOrganization}).(*User)
  545. test1 := HasOrgVisible(org, owner)
  546. test2 := HasOrgVisible(org, user3)
  547. test3 := HasOrgVisible(org, nil)
  548. assert.Equal(t, test1, true) // owner of org
  549. assert.Equal(t, test2, false) // user not a part of org
  550. assert.Equal(t, test3, false) // logged out user
  551. }