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.

api_user_watch_test.go 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package integration
  4. import (
  5. "fmt"
  6. "net/http"
  7. "testing"
  8. auth_model "code.gitea.io/gitea/models/auth"
  9. api "code.gitea.io/gitea/modules/structs"
  10. "code.gitea.io/gitea/tests"
  11. "github.com/stretchr/testify/assert"
  12. )
  13. func TestAPIWatch(t *testing.T) {
  14. defer tests.PrepareTestEnv(t)()
  15. user := "user1"
  16. repo := "user2/repo1"
  17. session := loginUser(t, user)
  18. token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadUser)
  19. tokenWithRepoScope := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository, auth_model.AccessTokenScopeReadUser)
  20. t.Run("Watch", func(t *testing.T) {
  21. defer tests.PrintCurrentTest(t)()
  22. req := NewRequest(t, "PUT", fmt.Sprintf("/api/v1/repos/%s/subscription?token=%s", repo, tokenWithRepoScope))
  23. MakeRequest(t, req, http.StatusOK)
  24. })
  25. t.Run("GetWatchedRepos", func(t *testing.T) {
  26. defer tests.PrintCurrentTest(t)()
  27. req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/users/%s/subscriptions?token=%s", user, token))
  28. resp := MakeRequest(t, req, http.StatusOK)
  29. assert.Equal(t, "1", resp.Header().Get("X-Total-Count"))
  30. var repos []api.Repository
  31. DecodeJSON(t, resp, &repos)
  32. assert.Len(t, repos, 1)
  33. assert.Equal(t, repo, repos[0].FullName)
  34. })
  35. t.Run("GetMyWatchedRepos", func(t *testing.T) {
  36. defer tests.PrintCurrentTest(t)()
  37. req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/user/subscriptions?token=%s", tokenWithRepoScope))
  38. resp := MakeRequest(t, req, http.StatusOK)
  39. assert.Equal(t, "1", resp.Header().Get("X-Total-Count"))
  40. var repos []api.Repository
  41. DecodeJSON(t, resp, &repos)
  42. assert.Len(t, repos, 1)
  43. assert.Equal(t, repo, repos[0].FullName)
  44. })
  45. t.Run("IsWatching", func(t *testing.T) {
  46. defer tests.PrintCurrentTest(t)()
  47. req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/%s/subscription", repo))
  48. MakeRequest(t, req, http.StatusUnauthorized)
  49. req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/%s/subscription?token=%s", repo, tokenWithRepoScope))
  50. MakeRequest(t, req, http.StatusOK)
  51. req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/repos/%s/subscription?token=%s", repo+"notexisting", tokenWithRepoScope))
  52. MakeRequest(t, req, http.StatusNotFound)
  53. })
  54. t.Run("Unwatch", func(t *testing.T) {
  55. defer tests.PrintCurrentTest(t)()
  56. req := NewRequest(t, "DELETE", fmt.Sprintf("/api/v1/repos/%s/subscription?token=%s", repo, tokenWithRepoScope))
  57. MakeRequest(t, req, http.StatusNoContent)
  58. })
  59. }