...

Source file src/github.com/google/go-github/v47/github/users_gpg_keys_test.go

Documentation: github.com/google/go-github/v47/github

     1  // Copyright 2016 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  	"testing"
    14  	"time"
    15  
    16  	"github.com/google/go-cmp/cmp"
    17  )
    18  
    19  func TestUsersService_ListGPGKeys_authenticatedUser(t *testing.T) {
    20  	client, mux, _, teardown := setup()
    21  	defer teardown()
    22  
    23  	mux.HandleFunc("/user/gpg_keys", func(w http.ResponseWriter, r *http.Request) {
    24  		testMethod(t, r, "GET")
    25  		testFormValues(t, r, values{"page": "2"})
    26  		fmt.Fprint(w, `[{"id":1,"primary_key_id":2}]`)
    27  	})
    28  
    29  	opt := &ListOptions{Page: 2}
    30  	ctx := context.Background()
    31  	keys, _, err := client.Users.ListGPGKeys(ctx, "", opt)
    32  	if err != nil {
    33  		t.Errorf("Users.ListGPGKeys returned error: %v", err)
    34  	}
    35  
    36  	want := []*GPGKey{{ID: Int64(1), PrimaryKeyID: Int64(2)}}
    37  	if !cmp.Equal(keys, want) {
    38  		t.Errorf("Users.ListGPGKeys = %+v, want %+v", keys, want)
    39  	}
    40  
    41  	const methodName = "ListGPGKeys"
    42  	testBadOptions(t, methodName, func() (err error) {
    43  		_, _, err = client.Users.ListGPGKeys(ctx, "\n", opt)
    44  		return err
    45  	})
    46  
    47  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
    48  		got, resp, err := client.Users.ListGPGKeys(ctx, "", opt)
    49  		if got != nil {
    50  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
    51  		}
    52  		return resp, err
    53  	})
    54  }
    55  
    56  func TestUsersService_ListGPGKeys_specifiedUser(t *testing.T) {
    57  	client, mux, _, teardown := setup()
    58  	defer teardown()
    59  
    60  	mux.HandleFunc("/users/u/gpg_keys", func(w http.ResponseWriter, r *http.Request) {
    61  		testMethod(t, r, "GET")
    62  		fmt.Fprint(w, `[{"id":1,"primary_key_id":2}]`)
    63  	})
    64  
    65  	ctx := context.Background()
    66  	keys, _, err := client.Users.ListGPGKeys(ctx, "u", nil)
    67  	if err != nil {
    68  		t.Errorf("Users.ListGPGKeys returned error: %v", err)
    69  	}
    70  
    71  	want := []*GPGKey{{ID: Int64(1), PrimaryKeyID: Int64(2)}}
    72  	if !cmp.Equal(keys, want) {
    73  		t.Errorf("Users.ListGPGKeys = %+v, want %+v", keys, want)
    74  	}
    75  }
    76  
    77  func TestUsersService_ListGPGKeys_invalidUser(t *testing.T) {
    78  	client, _, _, teardown := setup()
    79  	defer teardown()
    80  
    81  	ctx := context.Background()
    82  	_, _, err := client.Users.ListGPGKeys(ctx, "%", nil)
    83  	testURLParseError(t, err)
    84  }
    85  
    86  func TestUsersService_GetGPGKey(t *testing.T) {
    87  	client, mux, _, teardown := setup()
    88  	defer teardown()
    89  
    90  	mux.HandleFunc("/user/gpg_keys/1", func(w http.ResponseWriter, r *http.Request) {
    91  		testMethod(t, r, "GET")
    92  		fmt.Fprint(w, `{"id":1}`)
    93  	})
    94  
    95  	ctx := context.Background()
    96  	key, _, err := client.Users.GetGPGKey(ctx, 1)
    97  	if err != nil {
    98  		t.Errorf("Users.GetGPGKey returned error: %v", err)
    99  	}
   100  
   101  	want := &GPGKey{ID: Int64(1)}
   102  	if !cmp.Equal(key, want) {
   103  		t.Errorf("Users.GetGPGKey = %+v, want %+v", key, want)
   104  	}
   105  
   106  	const methodName = "GetGPGKey"
   107  	testBadOptions(t, methodName, func() (err error) {
   108  		_, _, err = client.Users.GetGPGKey(ctx, -1)
   109  		return err
   110  	})
   111  
   112  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   113  		got, resp, err := client.Users.GetGPGKey(ctx, 1)
   114  		if got != nil {
   115  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   116  		}
   117  		return resp, err
   118  	})
   119  }
   120  
   121  func TestUsersService_CreateGPGKey(t *testing.T) {
   122  	client, mux, _, teardown := setup()
   123  	defer teardown()
   124  
   125  	input := `
   126  -----BEGIN PGP PUBLIC KEY BLOCK-----
   127  Comment: GPGTools - https://gpgtools.org
   128  
   129  mQINBFcEd9kBEACo54TDbGhKlXKWMvJgecEUKPPcv7XdnpKdGb3LRw5MvFwT0V0f
   130  ...
   131  =tqfb
   132  -----END PGP PUBLIC KEY BLOCK-----`
   133  
   134  	mux.HandleFunc("/user/gpg_keys", func(w http.ResponseWriter, r *http.Request) {
   135  		var gpgKey struct {
   136  			ArmoredPublicKey *string `json:"armored_public_key,omitempty"`
   137  		}
   138  		json.NewDecoder(r.Body).Decode(&gpgKey)
   139  
   140  		testMethod(t, r, "POST")
   141  		if gpgKey.ArmoredPublicKey == nil || *gpgKey.ArmoredPublicKey != input {
   142  			t.Errorf("gpgKey = %+v, want %q", gpgKey, input)
   143  		}
   144  
   145  		fmt.Fprint(w, `{"id":1}`)
   146  	})
   147  
   148  	ctx := context.Background()
   149  	gpgKey, _, err := client.Users.CreateGPGKey(ctx, input)
   150  	if err != nil {
   151  		t.Errorf("Users.GetGPGKey returned error: %v", err)
   152  	}
   153  
   154  	want := &GPGKey{ID: Int64(1)}
   155  	if !cmp.Equal(gpgKey, want) {
   156  		t.Errorf("Users.GetGPGKey = %+v, want %+v", gpgKey, want)
   157  	}
   158  
   159  	const methodName = "CreateGPGKey"
   160  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   161  		got, resp, err := client.Users.CreateGPGKey(ctx, input)
   162  		if got != nil {
   163  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   164  		}
   165  		return resp, err
   166  	})
   167  }
   168  
   169  func TestUsersService_DeleteGPGKey(t *testing.T) {
   170  	client, mux, _, teardown := setup()
   171  	defer teardown()
   172  
   173  	mux.HandleFunc("/user/gpg_keys/1", func(w http.ResponseWriter, r *http.Request) {
   174  		testMethod(t, r, "DELETE")
   175  	})
   176  
   177  	ctx := context.Background()
   178  	_, err := client.Users.DeleteGPGKey(ctx, 1)
   179  	if err != nil {
   180  		t.Errorf("Users.DeleteGPGKey returned error: %v", err)
   181  	}
   182  
   183  	const methodName = "DeleteGPGKey"
   184  	testBadOptions(t, methodName, func() (err error) {
   185  		_, err = client.Users.DeleteGPGKey(ctx, -1)
   186  		return err
   187  	})
   188  
   189  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   190  		return client.Users.DeleteGPGKey(ctx, 1)
   191  	})
   192  }
   193  
   194  func TestGPGEmail_Marshal(t *testing.T) {
   195  	testJSONMarshal(t, &GPGEmail{}, "{}")
   196  
   197  	u := &GPGEmail{
   198  		Email:    String("email@abc.com"),
   199  		Verified: Bool(false),
   200  	}
   201  
   202  	want := `{
   203  		"email" : "email@abc.com",
   204  		"verified" : false
   205  	}`
   206  
   207  	testJSONMarshal(t, u, want)
   208  }
   209  
   210  func TestGPGKey_Marshal(t *testing.T) {
   211  	testJSONMarshal(t, &GPGKey{}, "{}")
   212  
   213  	ti := &time.Time{}
   214  
   215  	g := &GPGKey{
   216  		ID:           Int64(1),
   217  		PrimaryKeyID: Int64(1),
   218  		KeyID:        String("someKeyID"),
   219  		RawKey:       String("someRawKeyID"),
   220  		PublicKey:    String("somePublicKey"),
   221  		Emails: []*GPGEmail{
   222  			{
   223  				Email:    String("someEmail"),
   224  				Verified: Bool(true),
   225  			},
   226  		},
   227  		Subkeys: []*GPGKey{
   228  			{},
   229  		},
   230  		CanSign:           Bool(true),
   231  		CanEncryptComms:   Bool(true),
   232  		CanEncryptStorage: Bool(true),
   233  		CanCertify:        Bool(true),
   234  		CreatedAt:         ti,
   235  		ExpiresAt:         ti,
   236  	}
   237  
   238  	want := `{
   239  			"id":1,
   240  			"primary_key_id":1,
   241  			"key_id":"someKeyID",
   242  			"raw_key":"someRawKeyID",
   243  			"public_key":"somePublicKey",
   244  			"emails":[
   245  				{
   246  					"email":"someEmail",
   247  					"verified":true
   248  				}
   249  			],
   250  			"subkeys":[
   251  				{}
   252  			],
   253  			"can_sign":true,
   254  			"can_encrypt_comms":true,
   255  			"can_encrypt_storage":true,
   256  			"can_certify":true,
   257  			"created_at":"0001-01-01T00:00:00Z",
   258  			"expires_at":"0001-01-01T00:00:00Z"
   259  		}`
   260  
   261  	testJSONMarshal(t, g, want)
   262  }
   263  

View as plain text