|
| 1 | +// Copyright 2025 The go-github AUTHORS. All rights reserved. |
| 2 | +// |
| 3 | +// Use of this source code is governed by a BSD-style |
| 4 | +// license that can be found in the LICENSE file. |
| 5 | + |
| 6 | +package github |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + "encoding/json" |
| 11 | + "fmt" |
| 12 | + "net/http" |
| 13 | + "testing" |
| 14 | + |
| 15 | + "github.com/google/go-cmp/cmp" |
| 16 | +) |
| 17 | + |
| 18 | +func TestUsersService_ListSocialAccounts(t *testing.T) { |
| 19 | + t.Parallel() |
| 20 | + |
| 21 | + client, mux, _ := setup(t) |
| 22 | + |
| 23 | + mux.HandleFunc("/user/social_accounts", func(w http.ResponseWriter, r *http.Request) { |
| 24 | + testMethod(t, r, "GET") |
| 25 | + testFormValues(t, r, values{"page": "2"}) |
| 26 | + fmt.Fprint(w, `[{ |
| 27 | + "provider": "twitter", |
| 28 | + "url": "https://twitter.com/github" |
| 29 | + }]`) |
| 30 | + }) |
| 31 | + |
| 32 | + opt := &ListOptions{Page: 2} |
| 33 | + ctx := context.Background() |
| 34 | + accounts, _, err := client.Users.ListSocialAccounts(ctx, opt) |
| 35 | + if err != nil { |
| 36 | + t.Errorf("Users.ListSocialAccounts returned error: %v", err) |
| 37 | + } |
| 38 | + |
| 39 | + want := []*SocialAccount{{Provider: Ptr("twitter"), URL: Ptr("https://twitter.com/github")}} |
| 40 | + if !cmp.Equal(accounts, want) { |
| 41 | + t.Errorf("Users.ListSocialAccounts returned %#v, want %#v", accounts, want) |
| 42 | + } |
| 43 | + |
| 44 | + const methodName = "ListSocialAccounts" |
| 45 | + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 46 | + got, resp, err := client.Users.ListSocialAccounts(ctx, opt) |
| 47 | + if got != nil { |
| 48 | + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 49 | + } |
| 50 | + return resp, err |
| 51 | + }) |
| 52 | +} |
| 53 | + |
| 54 | +func TestUsersService_AddSocialAccounts(t *testing.T) { |
| 55 | + t.Parallel() |
| 56 | + |
| 57 | + client, mux, _ := setup(t) |
| 58 | + |
| 59 | + input := []string{"https://twitter.com/github"} |
| 60 | + |
| 61 | + mux.HandleFunc("/user/social_accounts", func(w http.ResponseWriter, r *http.Request) { |
| 62 | + var v []string |
| 63 | + assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) |
| 64 | + |
| 65 | + testMethod(t, r, "POST") |
| 66 | + if !cmp.Equal(v, input) { |
| 67 | + t.Errorf("Request body = %+v, want %+v", v, input) |
| 68 | + } |
| 69 | + |
| 70 | + fmt.Fprint(w, `[{"provider":"twitter","url":"https://twitter.com/github"},{"provider":"facebook","url":"https://facebook.com/github"}]`) |
| 71 | + }) |
| 72 | + |
| 73 | + ctx := context.Background() |
| 74 | + accounts, _, err := client.Users.AddSocialAccounts(ctx, input) |
| 75 | + if err != nil { |
| 76 | + t.Errorf("Users.AddSocialAccounts returned error: %v", err) |
| 77 | + } |
| 78 | + |
| 79 | + want := []*SocialAccount{ |
| 80 | + {Provider: Ptr("twitter"), URL: Ptr("https://twitter.com/github")}, |
| 81 | + {Provider: Ptr("facebook"), URL: Ptr("https://facebook.com/github")}, |
| 82 | + } |
| 83 | + if !cmp.Equal(accounts, want) { |
| 84 | + t.Errorf("Users.AddSocialAccounts returned %#v, want %#v", accounts, want) |
| 85 | + } |
| 86 | + |
| 87 | + const methodName = "AddSocialAccounts" |
| 88 | + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 89 | + got, resp, err := client.Users.AddSocialAccounts(ctx, input) |
| 90 | + if got != nil { |
| 91 | + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 92 | + } |
| 93 | + return resp, err |
| 94 | + }) |
| 95 | +} |
| 96 | + |
| 97 | +func TestUsersService_DeleteSocialAccounts(t *testing.T) { |
| 98 | + t.Parallel() |
| 99 | + |
| 100 | + client, mux, _ := setup(t) |
| 101 | + |
| 102 | + input := []string{"https://twitter.com/github"} |
| 103 | + |
| 104 | + mux.HandleFunc("/user/social_accounts", func(_ http.ResponseWriter, r *http.Request) { |
| 105 | + var v []string |
| 106 | + assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) |
| 107 | + |
| 108 | + testMethod(t, r, "DELETE") |
| 109 | + if !cmp.Equal(v, input) { |
| 110 | + t.Errorf("Request body = %+v, want %+v", v, input) |
| 111 | + } |
| 112 | + }) |
| 113 | + |
| 114 | + ctx := context.Background() |
| 115 | + _, err := client.Users.DeleteSocialAccounts(ctx, input) |
| 116 | + if err != nil { |
| 117 | + t.Errorf("Users.DeleteSocialAccounts returned error: %v", err) |
| 118 | + } |
| 119 | + |
| 120 | + const methodName = "DeleteSocialAccounts" |
| 121 | + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 122 | + return client.Users.DeleteSocialAccounts(ctx, input) |
| 123 | + }) |
| 124 | +} |
| 125 | + |
| 126 | +func TestUsersService_ListUserSocialAccounts(t *testing.T) { |
| 127 | + t.Parallel() |
| 128 | + |
| 129 | + client, mux, _ := setup(t) |
| 130 | + |
| 131 | + mux.HandleFunc("/users/u/social_accounts", func(w http.ResponseWriter, r *http.Request) { |
| 132 | + testMethod(t, r, "GET") |
| 133 | + testFormValues(t, r, values{"page": "2"}) |
| 134 | + fmt.Fprint(w, `[{ |
| 135 | + "provider": "twitter", |
| 136 | + "url": "https://twitter.com/github" |
| 137 | + }]`) |
| 138 | + }) |
| 139 | + |
| 140 | + opt := &ListOptions{Page: 2} |
| 141 | + ctx := context.Background() |
| 142 | + accounts, _, err := client.Users.ListUserSocialAccounts(ctx, "u", opt) |
| 143 | + if err != nil { |
| 144 | + t.Errorf("Users.ListUserSocialAccounts returned error: %v", err) |
| 145 | + } |
| 146 | + |
| 147 | + want := []*SocialAccount{{Provider: Ptr("twitter"), URL: Ptr("https://twitter.com/github")}} |
| 148 | + if !cmp.Equal(accounts, want) { |
| 149 | + t.Errorf("Users.ListUserSocialAccounts returned %#v, want %#v", accounts, want) |
| 150 | + } |
| 151 | + |
| 152 | + const methodName = "ListUserSocialAccounts" |
| 153 | + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 154 | + got, resp, err := client.Users.ListUserSocialAccounts(ctx, "u", opt) |
| 155 | + if got != nil { |
| 156 | + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 157 | + } |
| 158 | + return resp, err |
| 159 | + }) |
| 160 | +} |
0 commit comments