| 1 |
// Copyright 2013 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_ListEmails(t *testing.T) { |
| 19 |
client, mux, _, teardown := setup() |
| 20 |
defer teardown() |
| 21 |
|
| 22 |
mux.HandleFunc("/user/emails", func(w http.ResponseWriter, r *http.Request) { |
| 23 |
testMethod(t, r, "GET") |
| 24 |
testFormValues(t, r, values{"page": "2"}) |
| 25 |
fmt.Fprint(w, `[{ |
| 26 |
"email": "[email protected]", |
| 27 |
"verified": false, |
| 28 |
"primary": true |
| 29 |
}]`) |
| 30 |
}) |
| 31 |
|
| 32 |
opt := &ListOptions{Page: 2} |
| 33 |
ctx := context.Background() |
| 34 |
emails, _, err := client.Users.ListEmails(ctx, opt) |
| 35 |
if err != nil { |
| 36 |
t.Errorf("Users.ListEmails returned error: %v", err) |
| 37 |
} |
| 38 |
|
| 39 |
want := []*UserEmail{{Email: String("[email protected]"), Verified: Bool(false), Primary: Bool(true)}} |
| 40 |
if !cmp.Equal(emails, want) { |
| 41 |
t.Errorf("Users.ListEmails returned %+v, want %+v", emails, want) |
| 42 |
} |
| 43 |
|
| 44 |
const methodName = "ListEmails" |
| 45 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 46 |
got, resp, err := client.Users.ListEmails(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_AddEmails(t *testing.T) { |
| 55 |
client, mux, _, teardown := setup() |
| 56 |
defer teardown() |
| 57 |
|
| 58 |
input := []string{"[email protected]"} |
| 59 |
|
| 60 |
mux.HandleFunc("/user/emails", func(w http.ResponseWriter, r *http.Request) { |
| 61 |
var v []string |
| 62 |
assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) |
| 63 |
|
| 64 |
testMethod(t, r, "POST") |
| 65 |
if !cmp.Equal(v, input) { |
| 66 |
t.Errorf("Request body = %+v, want %+v", v, input) |
| 67 |
} |
| 68 |
|
| 69 |
fmt.Fprint(w, `[{"email":"[email protected]"}, {"email":"[email protected]"}]`) |
| 70 |
}) |
| 71 |
|
| 72 |
ctx := context.Background() |
| 73 |
emails, _, err := client.Users.AddEmails(ctx, input) |
| 74 |
if err != nil { |
| 75 |
t.Errorf("Users.AddEmails returned error: %v", err) |
| 76 |
} |
| 77 |
|
| 78 |
want := []*UserEmail{ |
| 79 |
{Email: String("[email protected]")}, |
| 80 |
{Email: String("[email protected]")}, |
| 81 |
} |
| 82 |
if !cmp.Equal(emails, want) { |
| 83 |
t.Errorf("Users.AddEmails returned %+v, want %+v", emails, want) |
| 84 |
} |
| 85 |
|
| 86 |
const methodName = "AddEmails" |
| 87 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 88 |
got, resp, err := client.Users.AddEmails(ctx, input) |
| 89 |
if got != nil { |
| 90 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 91 |
} |
| 92 |
return resp, err |
| 93 |
}) |
| 94 |
} |
| 95 |
|
| 96 |
func TestUsersService_DeleteEmails(t *testing.T) { |
| 97 |
client, mux, _, teardown := setup() |
| 98 |
defer teardown() |
| 99 |
|
| 100 |
input := []string{"[email protected]"} |
| 101 |
|
| 102 |
mux.HandleFunc("/user/emails", func(w http.ResponseWriter, r *http.Request) { |
| 103 |
var v []string |
| 104 |
assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) |
| 105 |
|
| 106 |
testMethod(t, r, "DELETE") |
| 107 |
if !cmp.Equal(v, input) { |
| 108 |
t.Errorf("Request body = %+v, want %+v", v, input) |
| 109 |
} |
| 110 |
}) |
| 111 |
|
| 112 |
ctx := context.Background() |
| 113 |
_, err := client.Users.DeleteEmails(ctx, input) |
| 114 |
if err != nil { |
| 115 |
t.Errorf("Users.DeleteEmails returned error: %v", err) |
| 116 |
} |
| 117 |
|
| 118 |
const methodName = "DeleteEmails" |
| 119 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 120 |
return client.Users.DeleteEmails(ctx, input) |
| 121 |
}) |
| 122 |
} |
| 123 |
|
| 124 |
func TestUserEmail_Marshal(t *testing.T) { |
| 125 |
testJSONMarshal(t, &UserEmail{}, "{}") |
| 126 |
|
| 127 |
u := &UserEmail{ |
| 128 |
Email: String("[email protected]"), |
| 129 |
Primary: Bool(false), |
| 130 |
Verified: Bool(true), |
| 131 |
Visibility: String("yes"), |
| 132 |
} |
| 133 |
|
| 134 |
want := `{ |
| 135 |
"email": "[email protected]", |
| 136 |
"primary": false, |
| 137 |
"verified": true, |
| 138 |
"visibility": "yes" |
| 139 |
}` |
| 140 |
|
| 141 |
testJSONMarshal(t, u, want) |
| 142 |
} |
| 143 |
|
| 144 |
func TestUsersService_SetEmailVisibility(t *testing.T) { |
| 145 |
client, mux, _, teardown := setup() |
| 146 |
defer teardown() |
| 147 |
|
| 148 |
input := &UserEmail{Visibility: String("private")} |
| 149 |
|
| 150 |
mux.HandleFunc("/user/email/visibility", func(w http.ResponseWriter, r *http.Request) { |
| 151 |
v := new(UserEmail) |
| 152 |
assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) |
| 153 |
|
| 154 |
testMethod(t, r, "PATCH") |
| 155 |
if !cmp.Equal(v, input) { |
| 156 |
t.Errorf("Request body = %+v, want %+v", v, input) |
| 157 |
} |
| 158 |
|
| 159 |
fmt.Fprint(w, `[{ |
| 160 |
"email": "[email protected]", |
| 161 |
"verified": false, |
| 162 |
"primary": true, |
| 163 |
"visibility": "private" |
| 164 |
}]`) |
| 165 |
}) |
| 166 |
|
| 167 |
ctx := context.Background() |
| 168 |
emails, _, err := client.Users.SetEmailVisibility(ctx, "private") |
| 169 |
if err != nil { |
| 170 |
t.Errorf("Users.SetEmailVisibility returned error: %v", err) |
| 171 |
} |
| 172 |
|
| 173 |
want := []*UserEmail{{Email: String("[email protected]"), Verified: Bool(false), Primary: Bool(true), Visibility: String("private")}} |
| 174 |
if !cmp.Equal(emails, want) { |
| 175 |
t.Errorf("Users.SetEmailVisibility returned %+v, want %+v", emails, want) |
| 176 |
} |
| 177 |
|
| 178 |
const methodName = "SetEmailVisibility" |
| 179 |
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 180 |
got, resp, err := client.Users.SetEmailVisibility(ctx, "private") |
| 181 |
if got != nil { |
| 182 |
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 183 |
} |
| 184 |
return resp, err |
| 185 |
}) |
| 186 |
} |