Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

org_team_test.go 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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.ID, 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. assert.True(t, IsTeamMember(3, 1, 2))
  207. assert.False(t, IsTeamMember(3, 1, 4))
  208. assert.False(t, IsTeamMember(3, 1, NonexistentID))
  209. assert.True(t, IsTeamMember(3, 2, 2))
  210. assert.True(t, IsTeamMember(3, 2, 4))
  211. assert.False(t, IsTeamMember(3, NonexistentID, NonexistentID))
  212. assert.False(t, IsTeamMember(NonexistentID, NonexistentID, NonexistentID))
  213. }
  214. func TestGetTeamMembers(t *testing.T) {
  215. assert.NoError(t, PrepareTestDatabase())
  216. test := func(teamID int64) {
  217. team := AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team)
  218. members, err := GetTeamMembers(teamID)
  219. assert.NoError(t, err)
  220. assert.Len(t, members, team.NumMembers)
  221. for _, member := range members {
  222. AssertExistsAndLoadBean(t, &TeamUser{UID: member.ID, TeamID: teamID})
  223. }
  224. }
  225. test(1)
  226. test(3)
  227. }
  228. func TestGetUserTeams(t *testing.T) {
  229. assert.NoError(t, PrepareTestDatabase())
  230. test := func(orgID, userID int64) {
  231. teams, err := GetUserTeams(orgID, userID)
  232. assert.NoError(t, err)
  233. for _, team := range teams {
  234. assert.EqualValues(t, orgID, team.OrgID)
  235. AssertExistsAndLoadBean(t, &TeamUser{TeamID: team.ID, UID: userID})
  236. }
  237. }
  238. test(3, 2)
  239. test(3, 4)
  240. test(3, NonexistentID)
  241. }
  242. func TestAddTeamMember(t *testing.T) {
  243. assert.NoError(t, PrepareTestDatabase())
  244. test := func(teamID, userID int64) {
  245. team := AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team)
  246. assert.NoError(t, AddTeamMember(team, userID))
  247. AssertExistsAndLoadBean(t, &TeamUser{UID: userID, TeamID: teamID})
  248. CheckConsistencyFor(t, &Team{ID: teamID}, &User{ID: team.OrgID})
  249. }
  250. test(1, 2)
  251. test(1, 4)
  252. test(3, 2)
  253. }
  254. func TestRemoveTeamMember(t *testing.T) {
  255. assert.NoError(t, PrepareTestDatabase())
  256. testSuccess := func(teamID, userID int64) {
  257. team := AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team)
  258. assert.NoError(t, RemoveTeamMember(team, userID))
  259. AssertNotExistsBean(t, &TeamUser{UID: userID, TeamID: teamID})
  260. CheckConsistencyFor(t, &Team{ID: teamID})
  261. }
  262. testSuccess(1, 4)
  263. testSuccess(2, 2)
  264. testSuccess(3, 2)
  265. testSuccess(3, NonexistentID)
  266. team := AssertExistsAndLoadBean(t, &Team{ID: 1}).(*Team)
  267. err := RemoveTeamMember(team, 2)
  268. assert.True(t, IsErrLastOrgOwner(err))
  269. }
  270. func TestHasTeamRepo(t *testing.T) {
  271. assert.NoError(t, PrepareTestDatabase())
  272. test := func(teamID, repoID int64, expected bool) {
  273. team := AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team)
  274. assert.Equal(t, expected, HasTeamRepo(team.OrgID, teamID, repoID))
  275. }
  276. test(1, 1, false)
  277. test(1, 3, true)
  278. test(1, 5, true)
  279. test(1, NonexistentID, false)
  280. test(2, 3, true)
  281. test(2, 5, false)
  282. }