github.com/google/go-github/v33@v33.0.0/test/integration/users_test.go (about)

     1  // Copyright 2014 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  // +build integration
     7  
     8  package integration
     9  
    10  import (
    11  	"context"
    12  	"fmt"
    13  	"math/rand"
    14  	"testing"
    15  
    16  	"github.com/google/go-github/v33/github"
    17  )
    18  
    19  func TestUsers_Get(t *testing.T) {
    20  	// list all users
    21  	users, _, err := client.Users.ListAll(context.Background(), nil)
    22  	if err != nil {
    23  		t.Fatalf("Users.ListAll returned error: %v", err)
    24  	}
    25  
    26  	if len(users) == 0 {
    27  		t.Errorf("Users.ListAll returned no users")
    28  	}
    29  
    30  	// mojombo is user #1
    31  	if want := "mojombo"; want != *users[0].Login {
    32  		t.Errorf("user[0].Login was %q, wanted %q", *users[0].Login, want)
    33  	}
    34  
    35  	// get individual user
    36  	u, _, err := client.Users.Get(context.Background(), "octocat")
    37  	if err != nil {
    38  		t.Fatalf("Users.Get('octocat') returned error: %v", err)
    39  	}
    40  
    41  	if want := "octocat"; want != *u.Login {
    42  		t.Errorf("user.Login was %q, wanted %q", *u.Login, want)
    43  	}
    44  	if want := "The Octocat"; want != *u.Name {
    45  		t.Errorf("user.Name was %q, wanted %q", *u.Name, want)
    46  	}
    47  }
    48  
    49  func TestUsers_Update(t *testing.T) {
    50  	if !checkAuth("TestUsers_Get") {
    51  		return
    52  	}
    53  
    54  	u, _, err := client.Users.Get(context.Background(), "")
    55  	if err != nil {
    56  		t.Fatalf("Users.Get('') returned error: %v", err)
    57  	}
    58  
    59  	if *u.Login == "" {
    60  		t.Errorf("wanted non-empty values for user.Login")
    61  	}
    62  
    63  	// save original location
    64  	var location string
    65  	if u.Location != nil {
    66  		location = *u.Location
    67  	}
    68  
    69  	// update location to test value
    70  	testLoc := fmt.Sprintf("test-%d", rand.Int())
    71  	u.Location = &testLoc
    72  
    73  	_, _, err = client.Users.Edit(context.Background(), u)
    74  	if err != nil {
    75  		t.Fatalf("Users.Update returned error: %v", err)
    76  	}
    77  
    78  	// refetch user and check location value
    79  	u, _, err = client.Users.Get(context.Background(), "")
    80  	if err != nil {
    81  		t.Fatalf("Users.Get('') returned error: %v", err)
    82  	}
    83  
    84  	if testLoc != *u.Location {
    85  		t.Errorf("Users.Get('') has location: %v, want: %v", *u.Location, testLoc)
    86  	}
    87  
    88  	// set location back to the original value
    89  	u.Location = &location
    90  	_, _, err = client.Users.Edit(context.Background(), u)
    91  	if err != nil {
    92  		t.Fatalf("Users.Edit returned error: %v", err)
    93  	}
    94  }
    95  
    96  func TestUsers_Emails(t *testing.T) {
    97  	if !checkAuth("TestUsers_Emails") {
    98  		return
    99  	}
   100  
   101  	emails, _, err := client.Users.ListEmails(context.Background(), nil)
   102  	if err != nil {
   103  		t.Fatalf("Users.ListEmails() returned error: %v", err)
   104  	}
   105  
   106  	// create random address not currently in user's emails
   107  	var email string
   108  EmailLoop:
   109  	for {
   110  		email = fmt.Sprintf("test-%d@example.com", rand.Int())
   111  		for _, e := range emails {
   112  			if e.Email != nil && *e.Email == email {
   113  				continue EmailLoop
   114  			}
   115  		}
   116  		break
   117  	}
   118  
   119  	// Add new address
   120  	_, _, err = client.Users.AddEmails(context.Background(), []string{email})
   121  	if err != nil {
   122  		t.Fatalf("Users.AddEmails() returned error: %v", err)
   123  	}
   124  
   125  	// List emails again and verify new email is present
   126  	emails, _, err = client.Users.ListEmails(context.Background(), nil)
   127  	if err != nil {
   128  		t.Fatalf("Users.ListEmails() returned error: %v", err)
   129  	}
   130  
   131  	var found bool
   132  	for _, e := range emails {
   133  		if e.Email != nil && *e.Email == email {
   134  			found = true
   135  			break
   136  		}
   137  	}
   138  
   139  	if !found {
   140  		t.Fatalf("Users.ListEmails() does not contain new address: %v", email)
   141  	}
   142  
   143  	// Remove new address
   144  	_, err = client.Users.DeleteEmails(context.Background(), []string{email})
   145  	if err != nil {
   146  		t.Fatalf("Users.DeleteEmails() returned error: %v", err)
   147  	}
   148  
   149  	// List emails again and verify new email was removed
   150  	emails, _, err = client.Users.ListEmails(context.Background(), nil)
   151  	if err != nil {
   152  		t.Fatalf("Users.ListEmails() returned error: %v", err)
   153  	}
   154  
   155  	for _, e := range emails {
   156  		if e.Email != nil && *e.Email == email {
   157  			t.Fatalf("Users.ListEmails() still contains address %v after removing it", email)
   158  		}
   159  	}
   160  }
   161  
   162  func TestUsers_Keys(t *testing.T) {
   163  	keys, _, err := client.Users.ListKeys(context.Background(), "willnorris", nil)
   164  	if err != nil {
   165  		t.Fatalf("Users.ListKeys('willnorris') returned error: %v", err)
   166  	}
   167  
   168  	if len(keys) == 0 {
   169  		t.Errorf("Users.ListKeys('willnorris') returned no keys")
   170  	}
   171  
   172  	// the rest of the tests requires auth
   173  	if !checkAuth("TestUsers_Keys") {
   174  		return
   175  	}
   176  
   177  	// TODO: make this integration test work for any authenticated user.
   178  	keys, _, err = client.Users.ListKeys(context.Background(), "", nil)
   179  	if err != nil {
   180  		t.Fatalf("Users.ListKeys('') returned error: %v", err)
   181  	}
   182  
   183  	// ssh public key for testing (fingerprint: a7:22:ad:8c:36:9f:68:65:eb:ae:a1:e4:59:73:c1:76)
   184  	key := "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCy/RIqaMFj2wjkOEjx9EAU0ReLAIhodga82/feo5nnT9UUkHLbL9xrIavfdLHx28lD3xYgPfAoSicUMaAeNwuQhmuerr2c2LFGxzrdXP8pVsQ+Ol7y7OdmFPfe0KrzoZaLJs9aSiZ4VKyY4z5Se/k2UgcJTdgQVlLfw/P96aqCx8yUu94BiWqkDqYEvgWKRNHrTiIo1EXeVBCCcfgNZe1suFfNJUJSUU2T3EG2bpwBbSOCjE3FyH8+Lz3K3BOGzm3df8E7Regj9j4YIcD8cWJYO86jLJoGgQ0L5MSOq+ishNaHQXech22Ix03D1lVMjCvDT7S/C94Z1LzhI2lhvyff"
   185  	for _, k := range keys {
   186  		if k.Key != nil && *k.Key == key {
   187  			t.Fatalf("Test key already exists for user. Please manually remove it first.")
   188  		}
   189  	}
   190  
   191  	// Add new key
   192  	_, _, err = client.Users.CreateKey(context.Background(), &github.Key{
   193  		Title: github.String("go-github test key"),
   194  		Key:   github.String(key),
   195  	})
   196  	if err != nil {
   197  		t.Fatalf("Users.CreateKey() returned error: %v", err)
   198  	}
   199  
   200  	// List keys again and verify new key is present
   201  	keys, _, err = client.Users.ListKeys(context.Background(), "", nil)
   202  	if err != nil {
   203  		t.Fatalf("Users.ListKeys('') returned error: %v", err)
   204  	}
   205  
   206  	var id int64
   207  	for _, k := range keys {
   208  		if k.Key != nil && *k.Key == key {
   209  			id = *k.ID
   210  			break
   211  		}
   212  	}
   213  
   214  	if id == 0 {
   215  		t.Fatalf("Users.ListKeys('') does not contain added test key")
   216  	}
   217  
   218  	// Verify that fetching individual key works
   219  	k, _, err := client.Users.GetKey(context.Background(), id)
   220  	if err != nil {
   221  		t.Fatalf("Users.GetKey(%q) returned error: %v", id, err)
   222  	}
   223  	if *k.Key != key {
   224  		t.Fatalf("Users.GetKey(%q) returned key %v, want %v", id, *k.Key, key)
   225  	}
   226  
   227  	// Remove test key
   228  	_, err = client.Users.DeleteKey(context.Background(), id)
   229  	if err != nil {
   230  		t.Fatalf("Users.DeleteKey(%d) returned error: %v", id, err)
   231  	}
   232  
   233  	// List keys again and verify test key was removed
   234  	keys, _, err = client.Users.ListKeys(context.Background(), "", nil)
   235  	if err != nil {
   236  		t.Fatalf("Users.ListKeys('') returned error: %v", err)
   237  	}
   238  
   239  	for _, k := range keys {
   240  		if k.Key != nil && *k.Key == key {
   241  			t.Fatalf("Users.ListKeys('') still contains test key after removing it")
   242  		}
   243  	}
   244  }