github.com/google/go-github/v33@v33.0.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  	"reflect"
    14  	"testing"
    15  )
    16  
    17  func TestUsersService_ListEmails(t *testing.T) {
    18  	client, mux, _, teardown := setup()
    19  	defer teardown()
    20  
    21  	mux.HandleFunc("/user/emails", func(w http.ResponseWriter, r *http.Request) {
    22  		testMethod(t, r, "GET")
    23  		testFormValues(t, r, values{"page": "2"})
    24  		fmt.Fprint(w, `[{
    25  			"email": "user@example.com",
    26  			"verified": false,
    27  			"primary": true
    28  		}]`)
    29  	})
    30  
    31  	opt := &ListOptions{Page: 2}
    32  	emails, _, err := client.Users.ListEmails(context.Background(), opt)
    33  	if err != nil {
    34  		t.Errorf("Users.ListEmails returned error: %v", err)
    35  	}
    36  
    37  	want := []*UserEmail{{Email: String("user@example.com"), Verified: Bool(false), Primary: Bool(true)}}
    38  	if !reflect.DeepEqual(emails, want) {
    39  		t.Errorf("Users.ListEmails returned %+v, want %+v", emails, want)
    40  	}
    41  }
    42  
    43  func TestUsersService_AddEmails(t *testing.T) {
    44  	client, mux, _, teardown := setup()
    45  	defer teardown()
    46  
    47  	input := []string{"new@example.com"}
    48  
    49  	mux.HandleFunc("/user/emails", func(w http.ResponseWriter, r *http.Request) {
    50  		var v []string
    51  		json.NewDecoder(r.Body).Decode(&v)
    52  
    53  		testMethod(t, r, "POST")
    54  		if !reflect.DeepEqual(v, input) {
    55  			t.Errorf("Request body = %+v, want %+v", v, input)
    56  		}
    57  
    58  		fmt.Fprint(w, `[{"email":"old@example.com"}, {"email":"new@example.com"}]`)
    59  	})
    60  
    61  	emails, _, err := client.Users.AddEmails(context.Background(), input)
    62  	if err != nil {
    63  		t.Errorf("Users.AddEmails returned error: %v", err)
    64  	}
    65  
    66  	want := []*UserEmail{
    67  		{Email: String("old@example.com")},
    68  		{Email: String("new@example.com")},
    69  	}
    70  	if !reflect.DeepEqual(emails, want) {
    71  		t.Errorf("Users.AddEmails returned %+v, want %+v", emails, want)
    72  	}
    73  }
    74  
    75  func TestUsersService_DeleteEmails(t *testing.T) {
    76  	client, mux, _, teardown := setup()
    77  	defer teardown()
    78  
    79  	input := []string{"user@example.com"}
    80  
    81  	mux.HandleFunc("/user/emails", func(w http.ResponseWriter, r *http.Request) {
    82  		var v []string
    83  		json.NewDecoder(r.Body).Decode(&v)
    84  
    85  		testMethod(t, r, "DELETE")
    86  		if !reflect.DeepEqual(v, input) {
    87  			t.Errorf("Request body = %+v, want %+v", v, input)
    88  		}
    89  	})
    90  
    91  	_, err := client.Users.DeleteEmails(context.Background(), input)
    92  	if err != nil {
    93  		t.Errorf("Users.DeleteEmails returned error: %v", err)
    94  	}
    95  }