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.

73 lines
2.6 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 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 TestAPIAdminCreateAndDeleteSSHKey(t *testing.T) {
  13. prepareTestEnv(t)
  14. // user1 is an admin user
  15. session := loginUser(t, "user1")
  16. keyOwner := models.AssertExistsAndLoadBean(t, &models.User{Name: "user2"}).(*models.User)
  17. urlStr := fmt.Sprintf("/api/v1/admin/users/%s/keys", keyOwner.Name)
  18. req := NewRequestWithValues(t, "POST", urlStr, map[string]string{
  19. "key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDAu7tvIvX6ZHrRXuZNfkR3XLHSsuCK9Zn3X58lxBcQzuo5xZgB6vRwwm/QtJuF+zZPtY5hsQILBLmF+BZ5WpKZp1jBeSjH2G7lxet9kbcH+kIVj0tPFEoyKI9wvWqIwC4prx/WVk2wLTJjzBAhyNxfEq7C9CeiX9pQEbEqJfkKCQ== nocomment\n",
  20. "title": "test-key",
  21. })
  22. resp := session.MakeRequest(t, req, http.StatusCreated)
  23. var newPublicKey api.PublicKey
  24. DecodeJSON(t, resp, &newPublicKey)
  25. models.AssertExistsAndLoadBean(t, &models.PublicKey{
  26. ID: newPublicKey.ID,
  27. Name: newPublicKey.Title,
  28. Content: newPublicKey.Key,
  29. Fingerprint: newPublicKey.Fingerprint,
  30. OwnerID: keyOwner.ID,
  31. })
  32. req = NewRequestf(t, "DELETE", "/api/v1/admin/users/%s/keys/%d",
  33. keyOwner.Name, newPublicKey.ID)
  34. session.MakeRequest(t, req, http.StatusNoContent)
  35. models.AssertNotExistsBean(t, &models.PublicKey{ID: newPublicKey.ID})
  36. }
  37. func TestAPIAdminDeleteMissingSSHKey(t *testing.T) {
  38. prepareTestEnv(t)
  39. // user1 is an admin user
  40. session := loginUser(t, "user1")
  41. req := NewRequestf(t, "DELETE", "/api/v1/admin/users/user1/keys/%d", models.NonexistentID)
  42. session.MakeRequest(t, req, http.StatusNotFound)
  43. }
  44. func TestAPIAdminDeleteUnauthorizedKey(t *testing.T) {
  45. prepareTestEnv(t)
  46. adminUsername := "user1"
  47. normalUsername := "user2"
  48. session := loginUser(t, adminUsername)
  49. urlStr := fmt.Sprintf("/api/v1/admin/users/%s/keys", adminUsername)
  50. req := NewRequestWithValues(t, "POST", urlStr, map[string]string{
  51. "key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDAu7tvIvX6ZHrRXuZNfkR3XLHSsuCK9Zn3X58lxBcQzuo5xZgB6vRwwm/QtJuF+zZPtY5hsQILBLmF+BZ5WpKZp1jBeSjH2G7lxet9kbcH+kIVj0tPFEoyKI9wvWqIwC4prx/WVk2wLTJjzBAhyNxfEq7C9CeiX9pQEbEqJfkKCQ== nocomment\n",
  52. "title": "test-key",
  53. })
  54. resp := session.MakeRequest(t, req, http.StatusCreated)
  55. var newPublicKey api.PublicKey
  56. DecodeJSON(t, resp, &newPublicKey)
  57. session = loginUser(t, normalUsername)
  58. req = NewRequestf(t, "DELETE", "/api/v1/admin/users/%s/keys/%d",
  59. adminUsername, newPublicKey.ID)
  60. session.MakeRequest(t, req, http.StatusForbidden)
  61. }