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.4 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. token := getTokenForLoggedInUser(t, session)
  41. keysURL := fmt.Sprintf("/api/v1/repos/%s/%s/keys?token=%s", repoOwner.Name, repo.Name, token)
  42. rawKeyBody := api.CreateKeyOption{
  43. Title: "read-only",
  44. Key: "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDAu7tvIvX6ZHrRXuZNfkR3XLHSsuCK9Zn3X58lxBcQzuo5xZgB6vRwwm/QtJuF+zZPtY5hsQILBLmF+BZ5WpKZp1jBeSjH2G7lxet9kbcH+kIVj0tPFEoyKI9wvWqIwC4prx/WVk2wLTJjzBAhyNxfEq7C9CeiX9pQEbEqJfkKCQ== nocomment\n",
  45. ReadOnly: true,
  46. }
  47. req := NewRequestWithJSON(t, "POST", keysURL, rawKeyBody)
  48. resp := session.MakeRequest(t, req, http.StatusCreated)
  49. var newDeployKey api.DeployKey
  50. DecodeJSON(t, resp, &newDeployKey)
  51. models.AssertExistsAndLoadBean(t, &models.DeployKey{
  52. ID: newDeployKey.ID,
  53. Name: rawKeyBody.Title,
  54. Content: rawKeyBody.Key,
  55. Mode: models.AccessModeRead,
  56. })
  57. }
  58. func TestCreateReadWriteDeployKey(t *testing.T) {
  59. prepareTestEnv(t)
  60. repo := models.AssertExistsAndLoadBean(t, &models.Repository{Name: "repo1"}).(*models.Repository)
  61. repoOwner := models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
  62. session := loginUser(t, repoOwner.Name)
  63. token := getTokenForLoggedInUser(t, session)
  64. keysURL := fmt.Sprintf("/api/v1/repos/%s/%s/keys?token=%s", repoOwner.Name, repo.Name, token)
  65. rawKeyBody := api.CreateKeyOption{
  66. Title: "read-write",
  67. Key: "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDsufOCrDDlT8DLkodnnJtbq7uGflcPae7euTfM+Laq4So+v4WeSV362Rg0O/+Sje1UthrhN6lQkfRkdWIlCRQEXg+LMqr6RhvDfZquE2Xwqv/itlz7LjbdAUdYoO1iH7rMSmYvQh4WEnC/DAacKGbhdGIM/ZBz0z6tHm7bPgbI9ykEKekTmPwQFP1Qebvf5NYOFMWqQ2sCEAI9dBMVLoojsIpV+KADf+BotiIi8yNfTG2rzmzpxBpW9fYjd1Sy1yd4NSUpoPbEJJYJ1TrjiSWlYOVq9Ar8xW1O87i6gBjL/3zN7ANeoYhaAXupdOS6YL22YOK/yC0tJtXwwdh/eSrh",
  68. }
  69. req := NewRequestWithJSON(t, "POST", keysURL, rawKeyBody)
  70. resp := session.MakeRequest(t, req, http.StatusCreated)
  71. var newDeployKey api.DeployKey
  72. DecodeJSON(t, resp, &newDeployKey)
  73. models.AssertExistsAndLoadBean(t, &models.DeployKey{
  74. ID: newDeployKey.ID,
  75. Name: rawKeyBody.Title,
  76. Content: rawKeyBody.Key,
  77. Mode: models.AccessModeWrite,
  78. })
  79. }