Browse Source

fix AddEmails return type and finish email tests

The GitHub docs don't make it completely clear that AddEmails is also
switching to the new v3 response format, but new integration tests
verify that it is.
Will Norris 12 years ago
parent
commit
8664943d1f
3 changed files with 66 additions and 5 deletions
  1. +2
    -2
      github/users_emails.go
  2. +5
    -2
      github/users_emails_test.go
  3. +59
    -1
      tests/integration/users_test.go

+ 2
- 2
github/users_emails.go View File

@ -34,14 +34,14 @@ func (s *UsersService) ListEmails() ([]UserEmail, *Response, error) {
// AddEmails adds email addresses of the authenticated user. // AddEmails adds email addresses of the authenticated user.
// //
// GitHub API docs: http://developer.github.com/v3/users/emails/#add-email-addresses // GitHub API docs: http://developer.github.com/v3/users/emails/#add-email-addresses
func (s *UsersService) AddEmails(emails []string) ([]string, *Response, error) {
func (s *UsersService) AddEmails(emails []string) ([]UserEmail, *Response, error) {
u := "user/emails" u := "user/emails"
req, err := s.client.NewRequest("POST", u, emails) req, err := s.client.NewRequest("POST", u, emails)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
e := new([]string)
e := new([]UserEmail)
resp, err := s.client.Do(req, e) resp, err := s.client.Do(req, e)
if err != nil { if err != nil {
return nil, resp, err return nil, resp, err


+ 5
- 2
github/users_emails_test.go View File

@ -52,7 +52,7 @@ func TestUsersService_AddEmails(t *testing.T) {
t.Errorf("Request body = %+v, want %+v", *v, input) t.Errorf("Request body = %+v, want %+v", *v, input)
} }
fmt.Fprint(w, `["old@example.com", "new@example.com"]`)
fmt.Fprint(w, `[{"email":"old@example.com"}, {"email":"new@example.com"}]`)
}) })
emails, _, err := client.Users.AddEmails(input) emails, _, err := client.Users.AddEmails(input)
@ -60,7 +60,10 @@ func TestUsersService_AddEmails(t *testing.T) {
t.Errorf("Users.AddEmails returned error: %v", err) t.Errorf("Users.AddEmails returned error: %v", err)
} }
want := []string{"old@example.com", "new@example.com"}
want := []UserEmail{
{Email: String("old@example.com")},
{Email: String("new@example.com")},
}
if !reflect.DeepEqual(emails, want) { if !reflect.DeepEqual(emails, want) {
t.Errorf("Users.AddEmails returned %+v, want %+v", emails, want) t.Errorf("Users.AddEmails returned %+v, want %+v", emails, want)
} }


+ 59
- 1
tests/integration/users_test.go View File

@ -5,7 +5,11 @@
package tests package tests
import "testing"
import (
"fmt"
"math/rand"
"testing"
)
func TestUsers_List(t *testing.T) { func TestUsers_List(t *testing.T) {
u, _, err := client.Users.ListAll(nil) u, _, err := client.Users.ListAll(nil)
@ -57,4 +61,58 @@ func TestUsers_Emails(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Users.ListEmails() returned error: %v", err) t.Fatalf("Users.ListEmails() returned error: %v", err)
} }
// create random address not currently in user's emails
var email string
for {
email = fmt.Sprintf("test-%d@example.com", rand.Int())
for _, e := range emails {
if e.Email != nil && *e.Email == email {
continue
}
}
break
}
// Add new address
_, _, err = client.Users.AddEmails([]string{email})
if err != nil {
t.Fatalf("Users.AddEmails() returned error: %v", err)
}
// List emails again and verify new email is present
emails, _, err = client.Users.ListEmails()
if err != nil {
t.Fatalf("Users.ListEmails() returned error: %v", err)
}
var found bool
for _, e := range emails {
if e.Email != nil && *e.Email == email {
found = true
break
}
}
if !found {
t.Fatalf("Users.ListEmails() does not contain new addres: %v", email)
}
// Remove new address
_, err = client.Users.DeleteEmails([]string{email})
if err != nil {
t.Fatalf("Users.DeleteEmails() returned error: %v", err)
}
// List emails again and verify new email was removed
emails, _, err = client.Users.ListEmails()
if err != nil {
t.Fatalf("Users.ListEmails() returned error: %v", err)
}
for _, e := range emails {
if e.Email != nil && *e.Email == email {
t.Fatalf("Users.ListEmails() still contains address %v after removing it", email)
}
}
} }

Loading…
Cancel
Save