github.com/google/go-github/v70@v70.0.0/github/enterprise_manage_ghes_ssh_test.go (about) 1 // Copyright 2025 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 TestEnterpriseService_GetSSHKey(t *testing.T) { 19 t.Parallel() 20 client, mux, _ := setup(t) 21 22 mux.HandleFunc("/manage/v1/access/ssh", func(w http.ResponseWriter, r *http.Request) { 23 testMethod(t, r, "GET") 24 fmt.Fprint(w, `[{ 25 "key": "ssh-rsa 1234", 26 "fingerprint": "bd" 27 }]`) 28 }) 29 30 ctx := context.Background() 31 accessSSH, _, err := client.Enterprise.GetSSHKey(ctx) 32 if err != nil { 33 t.Errorf("Enterprise.GetSSHKey returned error: %v", err) 34 } 35 36 want := []*ClusterSSHKey{{ 37 Key: Ptr("ssh-rsa 1234"), 38 Fingerprint: Ptr("bd"), 39 }} 40 if !cmp.Equal(accessSSH, want) { 41 t.Errorf("Enterprise.GetSSHKey returned %+v, want %+v", accessSSH, want) 42 } 43 44 const methodName = "GetSSHKey" 45 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 46 got, resp, err := client.Enterprise.GetSSHKey(ctx) 47 if got != nil { 48 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 49 } 50 return resp, err 51 }) 52 } 53 54 func TestEnterpriseService_DeleteSSHKey(t *testing.T) { 55 t.Parallel() 56 client, mux, _ := setup(t) 57 58 input := &SSHKeyOptions{ 59 Key: "ssh-rsa 1234", 60 } 61 62 mux.HandleFunc("/manage/v1/access/ssh", func(w http.ResponseWriter, r *http.Request) { 63 v := new(SSHKeyOptions) 64 assertNilError(t, json.NewDecoder(r.Body).Decode(v)) 65 66 testMethod(t, r, "DELETE") 67 if !cmp.Equal(v, input) { 68 t.Errorf("Request body = %+v, want %+v", v, input) 69 } 70 71 fmt.Fprint(w, `[ { "hostname": "primary", "uuid": "1b6cf518-f97c-11ed-8544-061d81f7eedb", "message": "SSH key removed successfully" } ]`) 72 }) 73 74 ctx := context.Background() 75 sshStatus, _, err := client.Enterprise.DeleteSSHKey(ctx, "ssh-rsa 1234") 76 if err != nil { 77 t.Errorf("Enterprise.DeleteSSHKey returned error: %v", err) 78 } 79 80 want := []*SSHKeyStatus{{Hostname: Ptr("primary"), UUID: Ptr("1b6cf518-f97c-11ed-8544-061d81f7eedb"), Message: Ptr("SSH key removed successfully")}} 81 if diff := cmp.Diff(want, sshStatus); diff != "" { 82 t.Errorf("diff mismatch (-want +got):\n%v", diff) 83 } 84 85 const methodName = "DeleteSSHKey" 86 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 87 got, resp, err := client.Enterprise.DeleteSSHKey(ctx, "ssh-rsa 1234") 88 if got != nil { 89 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 90 } 91 return resp, err 92 }) 93 } 94 95 func TestEnterpriseService_CreateSSHKey(t *testing.T) { 96 t.Parallel() 97 client, mux, _ := setup(t) 98 99 input := &SSHKeyOptions{ 100 Key: "ssh-rsa 1234", 101 } 102 103 mux.HandleFunc("/manage/v1/access/ssh", func(w http.ResponseWriter, r *http.Request) { 104 v := new(SSHKeyOptions) 105 assertNilError(t, json.NewDecoder(r.Body).Decode(v)) 106 107 testMethod(t, r, "POST") 108 if !cmp.Equal(v, input) { 109 t.Errorf("Request body = %+v, want %+v", v, input) 110 } 111 112 fmt.Fprint(w, `[ { "hostname": "primary", "uuid": "1b6cf518-f97c-11ed-8544-061d81f7eedb", "message": "SSH key added successfully", "modified": true } ]`) 113 }) 114 115 ctx := context.Background() 116 sshStatus, _, err := client.Enterprise.CreateSSHKey(ctx, "ssh-rsa 1234") 117 if err != nil { 118 t.Errorf("Enterprise.CreateSSHKey returned error: %v", err) 119 } 120 121 want := []*SSHKeyStatus{{Hostname: Ptr("primary"), UUID: Ptr("1b6cf518-f97c-11ed-8544-061d81f7eedb"), Message: Ptr("SSH key added successfully"), Modified: Ptr(true)}} 122 if diff := cmp.Diff(want, sshStatus); diff != "" { 123 t.Errorf("diff mismatch (-want +got):\n%v", diff) 124 } 125 126 const methodName = "CreateSSHKey" 127 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 128 got, resp, err := client.Enterprise.CreateSSHKey(ctx, "ssh-rsa 1234") 129 if got != nil { 130 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 131 } 132 return resp, err 133 }) 134 }