Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions github/users_social_accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ type SocialAccount struct {
URL *string `json:"url,omitempty"`
}

// socialAccountsRequest represents the request body for adding or deleting social accounts.
type socialAccountsRequest struct {
AccountURLs []string `json:"account_urls"`
}

// ListSocialAccounts lists all social accounts for the authenticated user.
//
// GitHub API docs: https://docs.github.com/rest/users/social-accounts#list-social-accounts-for-the-authenticated-user
Expand Down Expand Up @@ -49,7 +54,7 @@ func (s *UsersService) ListSocialAccounts(ctx context.Context, opts *ListOptions
//meta:operation POST /user/social_accounts
func (s *UsersService) AddSocialAccounts(ctx context.Context, accountURLs []string) ([]*SocialAccount, *Response, error) {
u := "user/social_accounts"
req, err := s.client.NewRequest("POST", u, accountURLs)
req, err := s.client.NewRequest("POST", u, &socialAccountsRequest{AccountURLs: accountURLs})
if err != nil {
return nil, nil, err
}
Expand All @@ -70,7 +75,7 @@ func (s *UsersService) AddSocialAccounts(ctx context.Context, accountURLs []stri
//meta:operation DELETE /user/social_accounts
func (s *UsersService) DeleteSocialAccounts(ctx context.Context, accountURLs []string) (*Response, error) {
u := "user/social_accounts"
req, err := s.client.NewRequest("DELETE", u, accountURLs)
req, err := s.client.NewRequest("DELETE", u, &socialAccountsRequest{AccountURLs: accountURLs})
if err != nil {
return nil, err
}
Expand Down
40 changes: 14 additions & 26 deletions github/users_social_accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package github

import (
"encoding/json"
"fmt"
"net/http"
"testing"
Expand All @@ -23,8 +22,8 @@ func TestUsersService_ListSocialAccounts(t *testing.T) {
testMethod(t, r, "GET")
testFormValues(t, r, values{"page": "2"})
fmt.Fprint(w, `[{
"provider": "twitter",
"url": "https://twitter.com/github"
"provider": "example",
"url": "https://example.com"
}]`)
})

Expand All @@ -35,7 +34,7 @@ func TestUsersService_ListSocialAccounts(t *testing.T) {
t.Errorf("Users.ListSocialAccounts returned error: %v", err)
}

want := []*SocialAccount{{Provider: Ptr("twitter"), URL: Ptr("https://twitter.com/github")}}
want := []*SocialAccount{{Provider: Ptr("example"), URL: Ptr("https://example.com")}}
if !cmp.Equal(accounts, want) {
t.Errorf("Users.ListSocialAccounts returned %#v, want %#v", accounts, want)
}
Expand All @@ -55,18 +54,12 @@ func TestUsersService_AddSocialAccounts(t *testing.T) {

client, mux, _ := setup(t)

input := []string{"https://twitter.com/github"}
input := []string{"https://example.com"}

mux.HandleFunc("/user/social_accounts", func(w http.ResponseWriter, r *http.Request) {
var v []string
assertNilError(t, json.NewDecoder(r.Body).Decode(&v))

testMethod(t, r, "POST")
if !cmp.Equal(v, input) {
t.Errorf("Request body = %+v, want %+v", v, input)
}

fmt.Fprint(w, `[{"provider":"twitter","url":"https://twitter.com/github"},{"provider":"facebook","url":"https://facebook.com/github"}]`)
testBody(t, r, `{"account_urls":["https://example.com"]}`+"\n")
fmt.Fprint(w, `[{"provider":"example","url":"https://example.com"}]`)
})

ctx := t.Context()
Expand All @@ -76,8 +69,7 @@ func TestUsersService_AddSocialAccounts(t *testing.T) {
}

want := []*SocialAccount{
{Provider: Ptr("twitter"), URL: Ptr("https://twitter.com/github")},
{Provider: Ptr("facebook"), URL: Ptr("https://facebook.com/github")},
{Provider: Ptr("example"), URL: Ptr("https://example.com")},
}
if !cmp.Equal(accounts, want) {
t.Errorf("Users.AddSocialAccounts returned %#v, want %#v", accounts, want)
Expand All @@ -98,16 +90,12 @@ func TestUsersService_DeleteSocialAccounts(t *testing.T) {

client, mux, _ := setup(t)

input := []string{"https://twitter.com/github"}

mux.HandleFunc("/user/social_accounts", func(_ http.ResponseWriter, r *http.Request) {
var v []string
assertNilError(t, json.NewDecoder(r.Body).Decode(&v))
input := []string{"https://example.com"}

mux.HandleFunc("/user/social_accounts", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "DELETE")
if !cmp.Equal(v, input) {
t.Errorf("Request body = %+v, want %+v", v, input)
}
testBody(t, r, `{"account_urls":["https://example.com"]}`+"\n")
w.WriteHeader(http.StatusNoContent)
})

ctx := t.Context()
Expand All @@ -131,8 +119,8 @@ func TestUsersService_ListUserSocialAccounts(t *testing.T) {
testMethod(t, r, "GET")
testFormValues(t, r, values{"page": "2"})
fmt.Fprint(w, `[{
"provider": "twitter",
"url": "https://twitter.com/github"
"provider": "example",
"url": "https://example.com"
}]`)
})

Expand All @@ -143,7 +131,7 @@ func TestUsersService_ListUserSocialAccounts(t *testing.T) {
t.Errorf("Users.ListUserSocialAccounts returned error: %v", err)
}

want := []*SocialAccount{{Provider: Ptr("twitter"), URL: Ptr("https://twitter.com/github")}}
want := []*SocialAccount{{Provider: Ptr("example"), URL: Ptr("https://example.com")}}
if !cmp.Equal(accounts, want) {
t.Errorf("Users.ListUserSocialAccounts returned %#v, want %#v", accounts, want)
}
Expand Down
Loading