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.

50 lines
1.6 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. }
  33. // TestAPIDeleteMissingToken ensures that error is thrown when token not found
  34. func TestAPIDeleteMissingToken(t *testing.T) {
  35. defer prepareTestEnv(t)()
  36. user := models.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User)
  37. req := NewRequestf(t, "DELETE", "/api/v1/users/user1/tokens/%d", models.NonexistentID)
  38. req = AddBasicAuthHeader(req, user.Name)
  39. MakeRequest(t, req, http.StatusNotFound)
  40. }