本站源代码
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

381 line
7.7KB

  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 repo
  5. import (
  6. "net/http"
  7. "testing"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/auth"
  10. "code.gitea.io/gitea/modules/context"
  11. "code.gitea.io/gitea/modules/test"
  12. "github.com/stretchr/testify/assert"
  13. )
  14. func TestAddReadOnlyDeployKey(t *testing.T) {
  15. models.PrepareTestEnv(t)
  16. ctx := test.MockContext(t, "user2/repo1/settings/keys")
  17. test.LoadUser(t, ctx, 2)
  18. test.LoadRepo(t, ctx, 2)
  19. addKeyForm := auth.AddKeyForm{
  20. Title: "read-only",
  21. Content: "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDAu7tvIvX6ZHrRXuZNfkR3XLHSsuCK9Zn3X58lxBcQzuo5xZgB6vRwwm/QtJuF+zZPtY5hsQILBLmF+BZ5WpKZp1jBeSjH2G7lxet9kbcH+kIVj0tPFEoyKI9wvWqIwC4prx/WVk2wLTJjzBAhyNxfEq7C9CeiX9pQEbEqJfkKCQ== nocomment\n",
  22. }
  23. DeployKeysPost(ctx, addKeyForm)
  24. assert.EqualValues(t, http.StatusFound, ctx.Resp.Status())
  25. models.AssertExistsAndLoadBean(t, &models.DeployKey{
  26. Name: addKeyForm.Title,
  27. Content: addKeyForm.Content,
  28. Mode: models.AccessModeRead,
  29. })
  30. }
  31. func TestAddReadWriteOnlyDeployKey(t *testing.T) {
  32. models.PrepareTestEnv(t)
  33. ctx := test.MockContext(t, "user2/repo1/settings/keys")
  34. test.LoadUser(t, ctx, 2)
  35. test.LoadRepo(t, ctx, 2)
  36. addKeyForm := auth.AddKeyForm{
  37. Title: "read-write",
  38. Content: "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDAu7tvIvX6ZHrRXuZNfkR3XLHSsuCK9Zn3X58lxBcQzuo5xZgB6vRwwm/QtJuF+zZPtY5hsQILBLmF+BZ5WpKZp1jBeSjH2G7lxet9kbcH+kIVj0tPFEoyKI9wvWqIwC4prx/WVk2wLTJjzBAhyNxfEq7C9CeiX9pQEbEqJfkKCQ== nocomment\n",
  39. IsWritable: true,
  40. }
  41. DeployKeysPost(ctx, addKeyForm)
  42. assert.EqualValues(t, http.StatusFound, ctx.Resp.Status())
  43. models.AssertExistsAndLoadBean(t, &models.DeployKey{
  44. Name: addKeyForm.Title,
  45. Content: addKeyForm.Content,
  46. Mode: models.AccessModeWrite,
  47. })
  48. }
  49. func TestCollaborationPost(t *testing.T) {
  50. models.PrepareTestEnv(t)
  51. ctx := test.MockContext(t, "user2/repo1/issues/labels")
  52. test.LoadUser(t, ctx, 2)
  53. test.LoadUser(t, ctx, 4)
  54. test.LoadRepo(t, ctx, 1)
  55. ctx.Req.Form.Set("collaborator", "user4")
  56. u := &models.User{
  57. LowerName: "user2",
  58. Type: models.UserTypeIndividual,
  59. }
  60. re := &models.Repository{
  61. ID: 2,
  62. Owner: u,
  63. }
  64. repo := &context.Repository{
  65. Owner: u,
  66. Repository: re,
  67. }
  68. ctx.Repo = repo
  69. CollaborationPost(ctx)
  70. assert.EqualValues(t, http.StatusFound, ctx.Resp.Status())
  71. exists, err := re.IsCollaborator(4)
  72. assert.NoError(t, err)
  73. assert.True(t, exists)
  74. }
  75. func TestCollaborationPost_InactiveUser(t *testing.T) {
  76. models.PrepareTestEnv(t)
  77. ctx := test.MockContext(t, "user2/repo1/issues/labels")
  78. test.LoadUser(t, ctx, 2)
  79. test.LoadUser(t, ctx, 9)
  80. test.LoadRepo(t, ctx, 1)
  81. ctx.Req.Form.Set("collaborator", "user9")
  82. repo := &context.Repository{
  83. Owner: &models.User{
  84. LowerName: "user2",
  85. },
  86. }
  87. ctx.Repo = repo
  88. CollaborationPost(ctx)
  89. assert.EqualValues(t, http.StatusFound, ctx.Resp.Status())
  90. assert.NotEmpty(t, ctx.Flash.ErrorMsg)
  91. }
  92. func TestCollaborationPost_AddCollaboratorTwice(t *testing.T) {
  93. models.PrepareTestEnv(t)
  94. ctx := test.MockContext(t, "user2/repo1/issues/labels")
  95. test.LoadUser(t, ctx, 2)
  96. test.LoadUser(t, ctx, 4)
  97. test.LoadRepo(t, ctx, 1)
  98. ctx.Req.Form.Set("collaborator", "user4")
  99. u := &models.User{
  100. LowerName: "user2",
  101. Type: models.UserTypeIndividual,
  102. }
  103. re := &models.Repository{
  104. ID: 2,
  105. Owner: u,
  106. }
  107. repo := &context.Repository{
  108. Owner: u,
  109. Repository: re,
  110. }
  111. ctx.Repo = repo
  112. CollaborationPost(ctx)
  113. assert.EqualValues(t, http.StatusFound, ctx.Resp.Status())
  114. exists, err := re.IsCollaborator(4)
  115. assert.NoError(t, err)
  116. assert.True(t, exists)
  117. // Try adding the same collaborator again
  118. CollaborationPost(ctx)
  119. assert.EqualValues(t, http.StatusFound, ctx.Resp.Status())
  120. assert.NotEmpty(t, ctx.Flash.ErrorMsg)
  121. }
  122. func TestCollaborationPost_NonExistentUser(t *testing.T) {
  123. models.PrepareTestEnv(t)
  124. ctx := test.MockContext(t, "user2/repo1/issues/labels")
  125. test.LoadUser(t, ctx, 2)
  126. test.LoadRepo(t, ctx, 1)
  127. ctx.Req.Form.Set("collaborator", "user34")
  128. repo := &context.Repository{
  129. Owner: &models.User{
  130. LowerName: "user2",
  131. },
  132. }
  133. ctx.Repo = repo
  134. CollaborationPost(ctx)
  135. assert.EqualValues(t, http.StatusFound, ctx.Resp.Status())
  136. assert.NotEmpty(t, ctx.Flash.ErrorMsg)
  137. }
  138. func TestAddTeamPost(t *testing.T) {
  139. models.PrepareTestEnv(t)
  140. ctx := test.MockContext(t, "org26/repo43")
  141. ctx.Req.Form.Set("team", "team11")
  142. org := &models.User{
  143. LowerName: "org26",
  144. Type: models.UserTypeOrganization,
  145. }
  146. team := &models.Team{
  147. ID: 11,
  148. OrgID: 26,
  149. }
  150. re := &models.Repository{
  151. ID: 43,
  152. Owner: org,
  153. OwnerID: 26,
  154. }
  155. repo := &context.Repository{
  156. Owner: &models.User{
  157. ID: 26,
  158. LowerName: "org26",
  159. RepoAdminChangeTeamAccess: true,
  160. },
  161. Repository: re,
  162. }
  163. ctx.Repo = repo
  164. AddTeamPost(ctx)
  165. assert.True(t, team.HasRepository(re.ID))
  166. assert.EqualValues(t, http.StatusFound, ctx.Resp.Status())
  167. assert.Empty(t, ctx.Flash.ErrorMsg)
  168. }
  169. func TestAddTeamPost_NotAllowed(t *testing.T) {
  170. models.PrepareTestEnv(t)
  171. ctx := test.MockContext(t, "org26/repo43")
  172. ctx.Req.Form.Set("team", "team11")
  173. org := &models.User{
  174. LowerName: "org26",
  175. Type: models.UserTypeOrganization,
  176. }
  177. team := &models.Team{
  178. ID: 11,
  179. OrgID: 26,
  180. }
  181. re := &models.Repository{
  182. ID: 43,
  183. Owner: org,
  184. OwnerID: 26,
  185. }
  186. repo := &context.Repository{
  187. Owner: &models.User{
  188. ID: 26,
  189. LowerName: "org26",
  190. RepoAdminChangeTeamAccess: false,
  191. },
  192. Repository: re,
  193. }
  194. ctx.Repo = repo
  195. AddTeamPost(ctx)
  196. assert.False(t, team.HasRepository(re.ID))
  197. assert.EqualValues(t, http.StatusFound, ctx.Resp.Status())
  198. assert.NotEmpty(t, ctx.Flash.ErrorMsg)
  199. }
  200. func TestAddTeamPost_AddTeamTwice(t *testing.T) {
  201. models.PrepareTestEnv(t)
  202. ctx := test.MockContext(t, "org26/repo43")
  203. ctx.Req.Form.Set("team", "team11")
  204. org := &models.User{
  205. LowerName: "org26",
  206. Type: models.UserTypeOrganization,
  207. }
  208. team := &models.Team{
  209. ID: 11,
  210. OrgID: 26,
  211. }
  212. re := &models.Repository{
  213. ID: 43,
  214. Owner: org,
  215. OwnerID: 26,
  216. }
  217. repo := &context.Repository{
  218. Owner: &models.User{
  219. ID: 26,
  220. LowerName: "org26",
  221. RepoAdminChangeTeamAccess: true,
  222. },
  223. Repository: re,
  224. }
  225. ctx.Repo = repo
  226. AddTeamPost(ctx)
  227. AddTeamPost(ctx)
  228. assert.True(t, team.HasRepository(re.ID))
  229. assert.EqualValues(t, http.StatusFound, ctx.Resp.Status())
  230. assert.NotEmpty(t, ctx.Flash.ErrorMsg)
  231. }
  232. func TestAddTeamPost_NonExistentTeam(t *testing.T) {
  233. models.PrepareTestEnv(t)
  234. ctx := test.MockContext(t, "org26/repo43")
  235. ctx.Req.Form.Set("team", "team-non-existent")
  236. org := &models.User{
  237. LowerName: "org26",
  238. Type: models.UserTypeOrganization,
  239. }
  240. re := &models.Repository{
  241. ID: 43,
  242. Owner: org,
  243. OwnerID: 26,
  244. }
  245. repo := &context.Repository{
  246. Owner: &models.User{
  247. ID: 26,
  248. LowerName: "org26",
  249. RepoAdminChangeTeamAccess: true,
  250. },
  251. Repository: re,
  252. }
  253. ctx.Repo = repo
  254. AddTeamPost(ctx)
  255. assert.EqualValues(t, http.StatusFound, ctx.Resp.Status())
  256. assert.NotEmpty(t, ctx.Flash.ErrorMsg)
  257. }
  258. func TestDeleteTeam(t *testing.T) {
  259. models.PrepareTestEnv(t)
  260. ctx := test.MockContext(t, "org3/team1/repo3")
  261. ctx.Req.Form.Set("id", "2")
  262. org := &models.User{
  263. LowerName: "org3",
  264. Type: models.UserTypeOrganization,
  265. }
  266. team := &models.Team{
  267. ID: 2,
  268. OrgID: 3,
  269. }
  270. re := &models.Repository{
  271. ID: 3,
  272. Owner: org,
  273. OwnerID: 3,
  274. }
  275. repo := &context.Repository{
  276. Owner: &models.User{
  277. ID: 3,
  278. LowerName: "org3",
  279. RepoAdminChangeTeamAccess: true,
  280. },
  281. Repository: re,
  282. }
  283. ctx.Repo = repo
  284. DeleteTeam(ctx)
  285. assert.False(t, team.HasRepository(re.ID))
  286. }
上海开阖软件有限公司 沪ICP备12045867号-1