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