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

377 lines
11KB

  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. "strings"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestTeam_IsOwnerTeam(t *testing.T) {
  11. assert.NoError(t, PrepareTestDatabase())
  12. team := AssertExistsAndLoadBean(t, &Team{ID: 1}).(*Team)
  13. assert.True(t, team.IsOwnerTeam())
  14. team = AssertExistsAndLoadBean(t, &Team{ID: 2}).(*Team)
  15. assert.False(t, team.IsOwnerTeam())
  16. }
  17. func TestTeam_IsMember(t *testing.T) {
  18. assert.NoError(t, PrepareTestDatabase())
  19. team := AssertExistsAndLoadBean(t, &Team{ID: 1}).(*Team)
  20. assert.True(t, team.IsMember(2))
  21. assert.False(t, team.IsMember(4))
  22. assert.False(t, team.IsMember(NonexistentID))
  23. team = AssertExistsAndLoadBean(t, &Team{ID: 2}).(*Team)
  24. assert.True(t, team.IsMember(2))
  25. assert.True(t, team.IsMember(4))
  26. assert.False(t, team.IsMember(NonexistentID))
  27. }
  28. func TestTeam_GetRepositories(t *testing.T) {
  29. assert.NoError(t, PrepareTestDatabase())
  30. test := func(teamID int64) {
  31. team := AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team)
  32. assert.NoError(t, team.GetRepositories())
  33. assert.Len(t, team.Repos, team.NumRepos)
  34. for _, repo := range team.Repos {
  35. AssertExistsAndLoadBean(t, &TeamRepo{TeamID: teamID, RepoID: repo.ID})
  36. }
  37. }
  38. test(1)
  39. test(3)
  40. }
  41. func TestTeam_GetMembers(t *testing.T) {
  42. assert.NoError(t, PrepareTestDatabase())
  43. test := func(teamID int64) {
  44. team := AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team)
  45. assert.NoError(t, team.GetMembers())
  46. assert.Len(t, team.Members, team.NumMembers)
  47. for _, member := range team.Members {
  48. AssertExistsAndLoadBean(t, &TeamUser{UID: member.ID, TeamID: teamID})
  49. }
  50. }
  51. test(1)
  52. test(3)
  53. }
  54. func TestTeam_AddMember(t *testing.T) {
  55. assert.NoError(t, PrepareTestDatabase())
  56. test := func(teamID, userID int64) {
  57. team := AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team)
  58. assert.NoError(t, team.AddMember(userID))
  59. AssertExistsAndLoadBean(t, &TeamUser{UID: userID, TeamID: teamID})
  60. CheckConsistencyFor(t, &Team{ID: teamID}, &User{ID: team.OrgID})
  61. }
  62. test(1, 2)
  63. test(1, 4)
  64. test(3, 2)
  65. }
  66. func TestTeam_RemoveMember(t *testing.T) {
  67. assert.NoError(t, PrepareTestDatabase())
  68. testSuccess := func(teamID, userID int64) {
  69. team := AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team)
  70. assert.NoError(t, team.RemoveMember(userID))
  71. AssertNotExistsBean(t, &TeamUser{UID: userID, TeamID: teamID})
  72. CheckConsistencyFor(t, &Team{ID: teamID})
  73. }
  74. testSuccess(1, 4)
  75. testSuccess(2, 2)
  76. testSuccess(3, 2)
  77. testSuccess(3, NonexistentID)
  78. team := AssertExistsAndLoadBean(t, &Team{ID: 1}).(*Team)
  79. err := team.RemoveMember(2)
  80. assert.True(t, IsErrLastOrgOwner(err))
  81. }
  82. func TestTeam_HasRepository(t *testing.T) {
  83. assert.NoError(t, PrepareTestDatabase())
  84. test := func(teamID, repoID int64, expected bool) {
  85. team := AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team)
  86. assert.Equal(t, expected, team.HasRepository(repoID))
  87. }
  88. test(1, 1, false)
  89. test(1, 3, true)
  90. test(1, 5, true)
  91. test(1, NonexistentID, false)
  92. test(2, 3, true)
  93. test(2, 5, false)
  94. }
  95. func TestTeam_AddRepository(t *testing.T) {
  96. assert.NoError(t, PrepareTestDatabase())
  97. testSuccess := func(teamID, repoID int64) {
  98. team := AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team)
  99. repo := AssertExistsAndLoadBean(t, &Repository{ID: repoID}).(*Repository)
  100. assert.NoError(t, team.AddRepository(repo))
  101. AssertExistsAndLoadBean(t, &TeamRepo{TeamID: teamID, RepoID: repoID})
  102. CheckConsistencyFor(t, &Team{ID: teamID}, &Repository{ID: repoID})
  103. }
  104. testSuccess(2, 3)
  105. testSuccess(2, 5)
  106. team := AssertExistsAndLoadBean(t, &Team{ID: 1}).(*Team)
  107. repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  108. assert.Error(t, team.AddRepository(repo))
  109. CheckConsistencyFor(t, &Team{ID: 1}, &Repository{ID: 1})
  110. }
  111. func TestTeam_RemoveRepository(t *testing.T) {
  112. assert.NoError(t, PrepareTestDatabase())
  113. testSuccess := func(teamID, repoID int64) {
  114. team := AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team)
  115. assert.NoError(t, team.RemoveRepository(repoID))
  116. AssertNotExistsBean(t, &TeamRepo{TeamID: teamID, RepoID: repoID})
  117. CheckConsistencyFor(t, &Team{ID: teamID}, &Repository{ID: repoID})
  118. }
  119. testSuccess(2, 3)
  120. testSuccess(2, 5)
  121. testSuccess(1, NonexistentID)
  122. }
  123. func TestIsUsableTeamName(t *testing.T) {
  124. assert.NoError(t, IsUsableTeamName("usable"))
  125. assert.True(t, IsErrNameReserved(IsUsableTeamName("new")))
  126. }
  127. func TestNewTeam(t *testing.T) {
  128. assert.NoError(t, PrepareTestDatabase())
  129. const teamName = "newTeamName"
  130. team := &Team{Name: teamName, OrgID: 3}
  131. assert.NoError(t, NewTeam(team))
  132. AssertExistsAndLoadBean(t, &Team{Name: teamName})
  133. CheckConsistencyFor(t, &Team{}, &User{ID: team.OrgID})
  134. }
  135. func TestGetTeam(t *testing.T) {
  136. assert.NoError(t, PrepareTestDatabase())
  137. testSuccess := func(orgID int64, name string) {
  138. team, err := GetTeam(orgID, name)
  139. assert.NoError(t, err)
  140. assert.EqualValues(t, orgID, team.OrgID)
  141. assert.Equal(t, name, team.Name)
  142. }
  143. testSuccess(3, "Owners")
  144. testSuccess(3, "team1")
  145. _, err := GetTeam(3, "nonexistent")
  146. assert.Error(t, err)
  147. _, err = GetTeam(NonexistentID, "Owners")
  148. assert.Error(t, err)
  149. }
  150. func TestGetTeamByID(t *testing.T) {
  151. assert.NoError(t, PrepareTestDatabase())
  152. testSuccess := func(teamID int64) {
  153. team, err := GetTeamByID(teamID)
  154. assert.NoError(t, err)
  155. assert.EqualValues(t, teamID, team.ID)
  156. }
  157. testSuccess(1)
  158. testSuccess(2)
  159. testSuccess(3)
  160. testSuccess(4)
  161. _, err := GetTeamByID(NonexistentID)
  162. assert.Error(t, err)
  163. }
  164. func TestUpdateTeam(t *testing.T) {
  165. // successful update
  166. assert.NoError(t, PrepareTestDatabase())
  167. team := AssertExistsAndLoadBean(t, &Team{ID: 2}).(*Team)
  168. team.LowerName = "newname"
  169. team.Name = "newName"
  170. team.Description = strings.Repeat("A long description!", 100)
  171. team.Authorize = AccessModeAdmin
  172. assert.NoError(t, UpdateTeam(team, true))
  173. team = AssertExistsAndLoadBean(t, &Team{Name: "newName"}).(*Team)
  174. assert.True(t, strings.HasPrefix(team.Description, "A long description!"))
  175. access := AssertExistsAndLoadBean(t, &Access{UserID: 4, RepoID: 3}).(*Access)
  176. assert.EqualValues(t, AccessModeAdmin, access.Mode)
  177. CheckConsistencyFor(t, &Team{ID: team.ID})
  178. }
  179. func TestUpdateTeam2(t *testing.T) {
  180. // update to already-existing team
  181. assert.NoError(t, PrepareTestDatabase())
  182. team := AssertExistsAndLoadBean(t, &Team{ID: 2}).(*Team)
  183. team.LowerName = "owners"
  184. team.Name = "Owners"
  185. team.Description = strings.Repeat("A long description!", 100)
  186. err := UpdateTeam(team, true)
  187. assert.True(t, IsErrTeamAlreadyExist(err))
  188. CheckConsistencyFor(t, &Team{ID: team.ID})
  189. }
  190. func TestDeleteTeam(t *testing.T) {
  191. assert.NoError(t, PrepareTestDatabase())
  192. team := AssertExistsAndLoadBean(t, &Team{ID: 2}).(*Team)
  193. assert.NoError(t, DeleteTeam(team))
  194. AssertNotExistsBean(t, &Team{ID: team.ID})
  195. AssertNotExistsBean(t, &TeamRepo{TeamID: team.ID})
  196. AssertNotExistsBean(t, &TeamUser{TeamID: team.ID})
  197. // check that team members don't have "leftover" access to repos
  198. user := AssertExistsAndLoadBean(t, &User{ID: 4}).(*User)
  199. repo := AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository)
  200. accessMode, err := AccessLevel(user, repo)
  201. assert.NoError(t, err)
  202. assert.True(t, accessMode < AccessModeWrite)
  203. }
  204. func TestIsTeamMember(t *testing.T) {
  205. assert.NoError(t, PrepareTestDatabase())
  206. test := func(orgID, teamID, userID int64, expected bool) {
  207. isMember, err := IsTeamMember(orgID, teamID, userID)
  208. assert.NoError(t, err)
  209. assert.Equal(t, expected, isMember)
  210. }
  211. test(3, 1, 2, true)
  212. test(3, 1, 4, false)
  213. test(3, 1, NonexistentID, false)
  214. test(3, 2, 2, true)
  215. test(3, 2, 4, true)
  216. test(3, NonexistentID, NonexistentID, false)
  217. test(NonexistentID, NonexistentID, NonexistentID, false)
  218. }
  219. func TestGetTeamMembers(t *testing.T) {
  220. assert.NoError(t, PrepareTestDatabase())
  221. test := func(teamID int64) {
  222. team := AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team)
  223. members, err := GetTeamMembers(teamID)
  224. assert.NoError(t, err)
  225. assert.Len(t, members, team.NumMembers)
  226. for _, member := range members {
  227. AssertExistsAndLoadBean(t, &TeamUser{UID: member.ID, TeamID: teamID})
  228. }
  229. }
  230. test(1)
  231. test(3)
  232. }
  233. func TestGetUserTeams(t *testing.T) {
  234. assert.NoError(t, PrepareTestDatabase())
  235. test := func(userID int64) {
  236. teams, err := GetUserTeams(userID)
  237. assert.NoError(t, err)
  238. for _, team := range teams {
  239. AssertExistsAndLoadBean(t, &TeamUser{TeamID: team.ID, UID: userID})
  240. }
  241. }
  242. test(2)
  243. test(5)
  244. test(NonexistentID)
  245. }
  246. func TestGetUserOrgTeams(t *testing.T) {
  247. assert.NoError(t, PrepareTestDatabase())
  248. test := func(orgID, userID int64) {
  249. teams, err := GetUserOrgTeams(orgID, userID)
  250. assert.NoError(t, err)
  251. for _, team := range teams {
  252. assert.EqualValues(t, orgID, team.OrgID)
  253. AssertExistsAndLoadBean(t, &TeamUser{TeamID: team.ID, UID: userID})
  254. }
  255. }
  256. test(3, 2)
  257. test(3, 4)
  258. test(3, NonexistentID)
  259. }
  260. func TestAddTeamMember(t *testing.T) {
  261. assert.NoError(t, PrepareTestDatabase())
  262. test := func(teamID, userID int64) {
  263. team := AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team)
  264. assert.NoError(t, AddTeamMember(team, userID))
  265. AssertExistsAndLoadBean(t, &TeamUser{UID: userID, TeamID: teamID})
  266. CheckConsistencyFor(t, &Team{ID: teamID}, &User{ID: team.OrgID})
  267. }
  268. test(1, 2)
  269. test(1, 4)
  270. test(3, 2)
  271. }
  272. func TestRemoveTeamMember(t *testing.T) {
  273. assert.NoError(t, PrepareTestDatabase())
  274. testSuccess := func(teamID, userID int64) {
  275. team := AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team)
  276. assert.NoError(t, RemoveTeamMember(team, userID))
  277. AssertNotExistsBean(t, &TeamUser{UID: userID, TeamID: teamID})
  278. CheckConsistencyFor(t, &Team{ID: teamID})
  279. }
  280. testSuccess(1, 4)
  281. testSuccess(2, 2)
  282. testSuccess(3, 2)
  283. testSuccess(3, NonexistentID)
  284. team := AssertExistsAndLoadBean(t, &Team{ID: 1}).(*Team)
  285. err := RemoveTeamMember(team, 2)
  286. assert.True(t, IsErrLastOrgOwner(err))
  287. }
  288. func TestHasTeamRepo(t *testing.T) {
  289. assert.NoError(t, PrepareTestDatabase())
  290. test := func(teamID, repoID int64, expected bool) {
  291. team := AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team)
  292. assert.Equal(t, expected, HasTeamRepo(team.OrgID, teamID, repoID))
  293. }
  294. test(1, 1, false)
  295. test(1, 3, true)
  296. test(1, 5, true)
  297. test(1, NonexistentID, false)
  298. test(2, 3, true)
  299. test(2, 5, false)
  300. }
  301. func TestUsersInTeamsCount(t *testing.T) {
  302. assert.NoError(t, PrepareTestDatabase())
  303. test := func(teamIDs []int64, userIDs []int64, expected int64) {
  304. count, err := UsersInTeamsCount(teamIDs, userIDs)
  305. assert.NoError(t, err)
  306. assert.Equal(t, expected, count)
  307. }
  308. test([]int64{2}, []int64{1, 2, 3, 4}, 1) // only userid 2
  309. test([]int64{1, 2, 3, 4, 5}, []int64{2, 5}, 2) // userid 2,4
  310. test([]int64{1, 2, 3, 4, 5}, []int64{2, 3, 5}, 3) // userid 2,4,5
  311. }
上海开阖软件有限公司 沪ICP备12045867号-1