feat(users): add authenticated starred listing
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
a0dee7c40a
commit
de60dc08b5
2 changed files with 69 additions and 0 deletions
10
users.go
10
users.go
|
|
@ -168,6 +168,16 @@ func (s *UserService) IterMySubscriptions(ctx context.Context) iter.Seq2[types.R
|
|||
return ListIter[types.Repository](ctx, s.client, "/api/v1/user/subscriptions", nil)
|
||||
}
|
||||
|
||||
// ListMyStarred returns all repositories starred by the authenticated user.
|
||||
func (s *UserService) ListMyStarred(ctx context.Context) ([]types.Repository, error) {
|
||||
return ListAll[types.Repository](ctx, s.client, "/api/v1/user/starred", nil)
|
||||
}
|
||||
|
||||
// IterMyStarred returns an iterator over all repositories starred by the authenticated user.
|
||||
func (s *UserService) IterMyStarred(ctx context.Context) iter.Seq2[types.Repository, error] {
|
||||
return ListIter[types.Repository](ctx, s.client, "/api/v1/user/starred", nil)
|
||||
}
|
||||
|
||||
// ListFollowers returns all followers of a user.
|
||||
func (s *UserService) ListFollowers(ctx context.Context, username string) ([]types.User, error) {
|
||||
path := ResolvePath("/api/v1/users/{username}/followers", pathParams("username", username))
|
||||
|
|
|
|||
|
|
@ -823,6 +823,65 @@ func TestUserService_IterSubscriptions_Good(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestUserService_ListMyStarred_Good(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
t.Errorf("expected GET, got %s", r.Method)
|
||||
}
|
||||
if r.URL.Path != "/api/v1/user/starred" {
|
||||
t.Errorf("wrong path: %s", r.URL.Path)
|
||||
}
|
||||
w.Header().Set("X-Total-Count", "1")
|
||||
json.NewEncoder(w).Encode([]types.Repository{
|
||||
{Name: "go-forge", FullName: "core/go-forge"},
|
||||
})
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
f := NewForge(srv.URL, "tok")
|
||||
repos, err := f.Users.ListMyStarred(context.Background())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(repos) != 1 {
|
||||
t.Fatalf("got %d repositories, want 1", len(repos))
|
||||
}
|
||||
if repos[0].FullName != "core/go-forge" {
|
||||
t.Errorf("got full_name=%q, want %q", repos[0].FullName, "core/go-forge")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUserService_IterMyStarred_Good(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
t.Errorf("expected GET, got %s", r.Method)
|
||||
}
|
||||
if r.URL.Path != "/api/v1/user/starred" {
|
||||
t.Errorf("wrong path: %s", r.URL.Path)
|
||||
}
|
||||
w.Header().Set("X-Total-Count", "1")
|
||||
json.NewEncoder(w).Encode([]types.Repository{
|
||||
{Name: "go-forge", FullName: "core/go-forge"},
|
||||
})
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
f := NewForge(srv.URL, "tok")
|
||||
count := 0
|
||||
for repo, err := range f.Users.IterMyStarred(context.Background()) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
count++
|
||||
if repo.Name != "go-forge" {
|
||||
t.Errorf("got name=%q, want %q", repo.Name, "go-forge")
|
||||
}
|
||||
}
|
||||
if count != 1 {
|
||||
t.Fatalf("got %d repositories, want 1", count)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUserService_CheckStarring_Good(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue