code.gitea.io/gitea@v1.21.7/tests/integration/api_user_star_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 TestAPIStar(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 tokenWithUserScope := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteUser, auth_model.AccessTokenScopeWriteRepository) 27 28 t.Run("Star", func(t *testing.T) { 29 defer tests.PrintCurrentTest(t)() 30 31 req := NewRequest(t, "PUT", fmt.Sprintf("/api/v1/user/starred/%s?token=%s", repo, tokenWithUserScope)) 32 MakeRequest(t, req, http.StatusNoContent) 33 }) 34 35 t.Run("GetStarredRepos", func(t *testing.T) { 36 defer tests.PrintCurrentTest(t)() 37 38 req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/users/%s/starred?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("GetMyStarredRepos", func(t *testing.T) { 50 defer tests.PrintCurrentTest(t)() 51 52 req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/user/starred?token=%s", tokenWithUserScope)) 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("IsStarring", func(t *testing.T) { 64 defer tests.PrintCurrentTest(t)() 65 66 req := NewRequest(t, "GET", fmt.Sprintf("/api/v1/user/starred/%s?token=%s", repo, tokenWithUserScope)) 67 MakeRequest(t, req, http.StatusNoContent) 68 69 req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/user/starred/%s?token=%s", repo+"notexisting", tokenWithUserScope)) 70 MakeRequest(t, req, http.StatusNotFound) 71 }) 72 73 t.Run("Unstar", func(t *testing.T) { 74 defer tests.PrintCurrentTest(t)() 75 76 req := NewRequest(t, "DELETE", fmt.Sprintf("/api/v1/user/starred/%s?token=%s", repo, tokenWithUserScope)) 77 MakeRequest(t, req, http.StatusNoContent) 78 }) 79 }