From 70f2a5f404216d4c2ba2d9982e0159b7b562f38e Mon Sep 17 00:00:00 2001 From: Will Norris Date: Wed, 9 Apr 2014 16:27:42 -0700 Subject: [PATCH] update Users.ListEmails to use v3 media type This is a breaking change, but it appears that this method has been broken for some time now and no one has complained yet, so I'm going to assume no one is using it yet. Notably, this is also the first (of many, I'm sure) bug I've discovered while working on the new integration tests (#98). Yay for better tests! --- github/users_emails.go | 18 +++++++++++------- github/users_emails_test.go | 18 +++++++++++------- tests/integration/users_test.go | 16 ++++++++++++---- 3 files changed, 34 insertions(+), 18 deletions(-) diff --git a/github/users_emails.go b/github/users_emails.go index 891c32f..6695550 100644 --- a/github/users_emails.go +++ b/github/users_emails.go @@ -6,9 +6,13 @@ package github // UserEmail represents user's email address -type UserEmail string +type UserEmail struct { + Email *string `json:"email,omitempty"` + Primary *bool `json:"primary,omitempty"` + Verified *bool `json:"verified,omitempty"` +} -// ListEmails lists all authenticated user email addresses +// ListEmails lists all email addresses for the authenticated user. // // GitHub API docs: http://developer.github.com/v3/users/emails/#list-email-addresses-for-a-user func (s *UsersService) ListEmails() ([]UserEmail, *Response, error) { @@ -27,17 +31,17 @@ func (s *UsersService) ListEmails() ([]UserEmail, *Response, error) { return *emails, resp, err } -// AddEmails adds email addresses of authenticated user +// AddEmails adds email addresses of the authenticated user. // // GitHub API docs: http://developer.github.com/v3/users/emails/#add-email-addresses -func (s *UsersService) AddEmails(emails []UserEmail) ([]UserEmail, *Response, error) { +func (s *UsersService) AddEmails(emails []string) ([]string, *Response, error) { u := "user/emails" req, err := s.client.NewRequest("POST", u, emails) if err != nil { return nil, nil, err } - e := new([]UserEmail) + e := new([]string) resp, err := s.client.Do(req, e) if err != nil { return nil, resp, err @@ -46,10 +50,10 @@ func (s *UsersService) AddEmails(emails []UserEmail) ([]UserEmail, *Response, er return *e, resp, err } -// DeleteEmails deletes email addresses from authenticated user +// DeleteEmails deletes email addresses from authenticated user. // // GitHub API docs: http://developer.github.com/v3/users/emails/#delete-email-addresses -func (s *UsersService) DeleteEmails(emails []UserEmail) (*Response, error) { +func (s *UsersService) DeleteEmails(emails []string) (*Response, error) { u := "user/emails" req, err := s.client.NewRequest("DELETE", u, emails) if err != nil { diff --git a/github/users_emails_test.go b/github/users_emails_test.go index 7a5d021..e2ca373 100644 --- a/github/users_emails_test.go +++ b/github/users_emails_test.go @@ -19,7 +19,11 @@ func TestUsersService_ListEmails(t *testing.T) { mux.HandleFunc("/user/emails", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "GET") - fmt.Fprint(w, `["user@example.com"]`) + fmt.Fprint(w, `[{ + "email": "user@example.com", + "verified": false, + "primary": true + }]`) }) emails, _, err := client.Users.ListEmails() @@ -27,7 +31,7 @@ func TestUsersService_ListEmails(t *testing.T) { t.Errorf("Users.ListEmails returned error: %v", err) } - want := []UserEmail{"user@example.com"} + want := []UserEmail{{Email: String("user@example.com"), Verified: Bool(false), Primary: Bool(true)}} if !reflect.DeepEqual(emails, want) { t.Errorf("Users.ListEmails returned %+v, want %+v", emails, want) } @@ -37,10 +41,10 @@ func TestUsersService_AddEmails(t *testing.T) { setup() defer teardown() - input := []UserEmail{"new@example.com"} + input := []string{"new@example.com"} mux.HandleFunc("/user/emails", func(w http.ResponseWriter, r *http.Request) { - v := new([]UserEmail) + v := new([]string) json.NewDecoder(r.Body).Decode(v) testMethod(t, r, "POST") @@ -56,7 +60,7 @@ func TestUsersService_AddEmails(t *testing.T) { t.Errorf("Users.AddEmails returned error: %v", err) } - want := []UserEmail{"old@example.com", "new@example.com"} + want := []string{"old@example.com", "new@example.com"} if !reflect.DeepEqual(emails, want) { t.Errorf("Users.AddEmails returned %+v, want %+v", emails, want) } @@ -66,10 +70,10 @@ func TestUsersService_DeleteEmails(t *testing.T) { setup() defer teardown() - input := []UserEmail{"user@example.com"} + input := []string{"user@example.com"} mux.HandleFunc("/user/emails", func(w http.ResponseWriter, r *http.Request) { - v := new([]UserEmail) + v := new([]string) json.NewDecoder(r.Body).Decode(v) testMethod(t, r, "DELETE") diff --git a/tests/integration/users_test.go b/tests/integration/users_test.go index 947f708..e315ac5 100644 --- a/tests/integration/users_test.go +++ b/tests/integration/users_test.go @@ -5,10 +5,7 @@ package tests -import ( - "fmt" - "testing" -) +import "testing" func TestUsers_List(t *testing.T) { u, _, err := client.Users.ListAll(nil) @@ -50,3 +47,14 @@ func TestUsers_Get(t *testing.T) { } } } + +func TestUsers_Emails(t *testing.T) { + if !checkAuth("TestUsers_Emails") { + return + } + + emails, _, err := client.Users.ListEmails() + if err != nil { + t.Fatalf("Users.ListEmails() returned error: %v", err) + } +}