github.com/google/go-github/v49@v49.1.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 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": "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: String("user@example.com"), 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{"new@example.com"} 59 60 mux.HandleFunc("/user/emails", func(w http.ResponseWriter, r *http.Request) { 61 var v []string 62 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: String("old@example.com")}, 80 {Email: String("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 client, mux, _, teardown := setup() 98 defer teardown() 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 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("qwe@qwe.qwe"), 129 Primary: Bool(false), 130 Verified: Bool(true), 131 Visibility: String("yes"), 132 } 133 134 want := `{ 135 "email": "qwe@qwe.qwe", 136 "primary": false, 137 "verified": true, 138 "visibility": "yes" 139 }` 140 141 testJSONMarshal(t, u, want) 142 }