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.

187 lines
4.2 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 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. }