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.

63 lines
2.1 KiB

  1. // Copyright 2018 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. "net/http"
  7. "testing"
  8. "code.gitea.io/gitea/models"
  9. api "code.gitea.io/gitea/modules/structs"
  10. )
  11. // TestAPICreateAndDeleteToken tests that token that was just created can be deleted
  12. func TestAPICreateAndDeleteToken(t *testing.T) {
  13. defer prepareTestEnv(t)()
  14. user := models.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User)
  15. req := NewRequestWithJSON(t, "POST", "/api/v1/users/user1/tokens", map[string]string{
  16. "name": "test-key-1",
  17. })
  18. req = AddBasicAuthHeader(req, user.Name)
  19. resp := MakeRequest(t, req, http.StatusCreated)
  20. var newAccessToken api.AccessToken
  21. DecodeJSON(t, resp, &newAccessToken)
  22. models.AssertExistsAndLoadBean(t, &models.AccessToken{
  23. ID: newAccessToken.ID,
  24. Name: newAccessToken.Name,
  25. Token: newAccessToken.Token,
  26. UID: user.ID,
  27. })
  28. req = NewRequestf(t, "DELETE", "/api/v1/users/user1/tokens/%d", newAccessToken.ID)
  29. req = AddBasicAuthHeader(req, user.Name)
  30. MakeRequest(t, req, http.StatusNoContent)
  31. models.AssertNotExistsBean(t, &models.AccessToken{ID: newAccessToken.ID})
  32. req = NewRequestWithJSON(t, "POST", "/api/v1/users/user1/tokens", map[string]string{
  33. "name": "test-key-2",
  34. })
  35. req = AddBasicAuthHeader(req, user.Name)
  36. resp = MakeRequest(t, req, http.StatusCreated)
  37. DecodeJSON(t, resp, &newAccessToken)
  38. req = NewRequestf(t, "DELETE", "/api/v1/users/user1/tokens/%s", newAccessToken.Name)
  39. req = AddBasicAuthHeader(req, user.Name)
  40. MakeRequest(t, req, http.StatusNoContent)
  41. models.AssertNotExistsBean(t, &models.AccessToken{ID: newAccessToken.ID})
  42. }
  43. // TestAPIDeleteMissingToken ensures that error is thrown when token not found
  44. func TestAPIDeleteMissingToken(t *testing.T) {
  45. defer prepareTestEnv(t)()
  46. user := models.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User)
  47. req := NewRequestf(t, "DELETE", "/api/v1/users/user1/tokens/%d", models.NonexistentID)
  48. req = AddBasicAuthHeader(req, user.Name)
  49. MakeRequest(t, req, http.StatusNotFound)
  50. }