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