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.

92 lines
3.3 KiB

  1. // Copyright 2017 The Gogs 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 integrations
  5. import (
  6. "fmt"
  7. "net/http"
  8. "testing"
  9. "code.gitea.io/gitea/models"
  10. api "code.gitea.io/sdk/gitea"
  11. )
  12. func TestViewDeployKeysNoLogin(t *testing.T) {
  13. prepareTestEnv(t)
  14. req := NewRequest(t, "GET", "/api/v1/repos/user2/repo1/keys")
  15. MakeRequest(t, req, http.StatusUnauthorized)
  16. }
  17. func TestCreateDeployKeyNoLogin(t *testing.T) {
  18. prepareTestEnv(t)
  19. req := NewRequestWithJSON(t, "POST", "/api/v1/repos/user2/repo1/keys", api.CreateKeyOption{
  20. Title: "title",
  21. Key: "key",
  22. })
  23. MakeRequest(t, req, http.StatusUnauthorized)
  24. }
  25. func TestGetDeployKeyNoLogin(t *testing.T) {
  26. prepareTestEnv(t)
  27. req := NewRequest(t, "GET", "/api/v1/repos/user2/repo1/keys/1")
  28. MakeRequest(t, req, http.StatusUnauthorized)
  29. }
  30. func TestDeleteDeployKeyNoLogin(t *testing.T) {
  31. prepareTestEnv(t)
  32. req := NewRequest(t, "DELETE", "/api/v1/repos/user2/repo1/keys/1")
  33. MakeRequest(t, req, http.StatusUnauthorized)
  34. }
  35. func TestCreateReadOnlyDeployKey(t *testing.T) {
  36. prepareTestEnv(t)
  37. repo := models.AssertExistsAndLoadBean(t, &models.Repository{Name: "repo1"}).(*models.Repository)
  38. repoOwner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
  39. session := loginUser(t, repoOwner.Name)
  40. keysURL := fmt.Sprintf("/api/v1/repos/%s/%s/keys", repoOwner.Name, repo.Name)
  41. rawKeyBody := api.CreateKeyOption{
  42. Title: "read-only",
  43. Key: "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDAu7tvIvX6ZHrRXuZNfkR3XLHSsuCK9Zn3X58lxBcQzuo5xZgB6vRwwm/QtJuF+zZPtY5hsQILBLmF+BZ5WpKZp1jBeSjH2G7lxet9kbcH+kIVj0tPFEoyKI9wvWqIwC4prx/WVk2wLTJjzBAhyNxfEq7C9CeiX9pQEbEqJfkKCQ== nocomment\n",
  44. ReadOnly: true,
  45. }
  46. req := NewRequestWithJSON(t, "POST", keysURL, rawKeyBody)
  47. resp := session.MakeRequest(t, req, http.StatusCreated)
  48. var newDeployKey api.DeployKey
  49. DecodeJSON(t, resp, &newDeployKey)
  50. models.AssertExistsAndLoadBean(t, &models.DeployKey{
  51. ID: newDeployKey.ID,
  52. Name: rawKeyBody.Title,
  53. Content: rawKeyBody.Key,
  54. Mode: models.AccessModeRead,
  55. })
  56. }
  57. func TestCreateReadWriteDeployKey(t *testing.T) {
  58. prepareTestEnv(t)
  59. repo := models.AssertExistsAndLoadBean(t, &models.Repository{Name: "repo1"}).(*models.Repository)
  60. repoOwner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
  61. session := loginUser(t, repoOwner.Name)
  62. keysURL := fmt.Sprintf("/api/v1/repos/%s/%s/keys", repoOwner.Name, repo.Name)
  63. rawKeyBody := api.CreateKeyOption{
  64. Title: "read-write",
  65. Key: "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDsufOCrDDlT8DLkodnnJtbq7uGflcPae7euTfM+Laq4So+v4WeSV362Rg0O/+Sje1UthrhN6lQkfRkdWIlCRQEXg+LMqr6RhvDfZquE2Xwqv/itlz7LjbdAUdYoO1iH7rMSmYvQh4WEnC/DAacKGbhdGIM/ZBz0z6tHm7bPgbI9ykEKekTmPwQFP1Qebvf5NYOFMWqQ2sCEAI9dBMVLoojsIpV+KADf+BotiIi8yNfTG2rzmzpxBpW9fYjd1Sy1yd4NSUpoPbEJJYJ1TrjiSWlYOVq9Ar8xW1O87i6gBjL/3zN7ANeoYhaAXupdOS6YL22YOK/yC0tJtXwwdh/eSrh",
  66. }
  67. req := NewRequestWithJSON(t, "POST", keysURL, rawKeyBody)
  68. resp := session.MakeRequest(t, req, http.StatusCreated)
  69. var newDeployKey api.DeployKey
  70. DecodeJSON(t, resp, &newDeployKey)
  71. models.AssertExistsAndLoadBean(t, &models.DeployKey{
  72. ID: newDeployKey.ID,
  73. Name: rawKeyBody.Title,
  74. Content: rawKeyBody.Key,
  75. Mode: models.AccessModeWrite,
  76. })
  77. }