github.com/google/go-github/v69@v69.2.0/github/users_emails_test.go (about) 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 t.Parallel() 20 client, mux, _ := setup(t) 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": "user@example.com", 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: Ptr("user@example.com"), Verified: Ptr(false), Primary: Ptr(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 t.Parallel() 56 client, mux, _ := setup(t) 57 58 input := []string{"new@example.com"} 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":"old@example.com"}, {"email":"new@example.com"}]`) 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: Ptr("old@example.com")}, 80 {Email: Ptr("new@example.com")}, 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 t.Parallel() 98 client, mux, _ := setup(t) 99 100 input := []string{"user@example.com"} 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 t.Parallel() 126 testJSONMarshal(t, &UserEmail{}, "{}") 127 128 u := &UserEmail{ 129 Email: Ptr("qwe@qwe.qwe"), 130 Primary: Ptr(false), 131 Verified: Ptr(true), 132 Visibility: Ptr("yes"), 133 } 134 135 want := `{ 136 "email": "qwe@qwe.qwe", 137 "primary": false, 138 "verified": true, 139 "visibility": "yes" 140 }` 141 142 testJSONMarshal(t, u, want) 143 } 144 145 func TestUsersService_SetEmailVisibility(t *testing.T) { 146 t.Parallel() 147 client, mux, _ := setup(t) 148 149 input := &UserEmail{Visibility: Ptr("private")} 150 151 mux.HandleFunc("/user/email/visibility", func(w http.ResponseWriter, r *http.Request) { 152 v := new(UserEmail) 153 assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) 154 155 testMethod(t, r, "PATCH") 156 if !cmp.Equal(v, input) { 157 t.Errorf("Request body = %+v, want %+v", v, input) 158 } 159 160 fmt.Fprint(w, `[{ 161 "email": "user@example.com", 162 "verified": false, 163 "primary": true, 164 "visibility": "private" 165 }]`) 166 }) 167 168 ctx := context.Background() 169 emails, _, err := client.Users.SetEmailVisibility(ctx, "private") 170 if err != nil { 171 t.Errorf("Users.SetEmailVisibility returned error: %v", err) 172 } 173 174 want := []*UserEmail{{Email: Ptr("user@example.com"), Verified: Ptr(false), Primary: Ptr(true), Visibility: Ptr("private")}} 175 if !cmp.Equal(emails, want) { 176 t.Errorf("Users.SetEmailVisibility returned %+v, want %+v", emails, want) 177 } 178 179 const methodName = "SetEmailVisibility" 180 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 181 got, resp, err := client.Users.SetEmailVisibility(ctx, "private") 182 if got != nil { 183 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 184 } 185 return resp, err 186 }) 187 }