...

Source file src/github.com/google/go-github/v45/test/integration/users_test.go

Documentation: github.com/google/go-github/v45/test/integration

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

View as plain text