Skip to content

Commit 2e27388

Browse files
committed
Added AddSSHKey function and unit tests
1 parent 31687dc commit 2e27388

File tree

2 files changed

+130
-1
lines changed

2 files changed

+130
-1
lines changed

accounts.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,27 @@ func (s *AccountsService) GetSSHKey(ctx context.Context, accountID, sshKeyID str
453453
return v, resp, err
454454
}
455455

456+
// AddSSHKey adds an SSH key to a user's account.
457+
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-accounts.html#add-ssh-key
458+
func (s *AccountsService) AddSSHKey(ctx context.Context, accountID string, SSHKey string) (*SSHKeyInfo, *Response, error) {
459+
u := fmt.Sprintf("accounts/%s/sshkeys", accountID)
460+
461+
req, err := s.client.NewRequest(ctx, "POST", u, SSHKey)
462+
if err != nil {
463+
return nil, nil, err
464+
}
465+
466+
req.Header.Set("Content-Type", "text/plain")
467+
468+
var keyInfo SSHKeyInfo
469+
resp, err := s.client.Do(req, &keyInfo)
470+
if err != nil {
471+
return nil, resp, err
472+
}
473+
474+
return &keyInfo, resp, nil
475+
}
476+
456477
// ListGPGKeys returns the GPG keys of an account.
457478
//
458479
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-accounts.html#list-gpg-keys
@@ -957,6 +978,5 @@ func (s *AccountsService) UnstarChange(ctx context.Context, accountID, changeID
957978

958979
/*
959980
Missing Account Endpoints:
960-
Add SSH Key
961981
Get Avatar
962982
*/

accounts_test.go

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package gerrit
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"io"
7+
"net/http"
8+
"net/http/httptest"
9+
"strings"
10+
"testing"
11+
12+
"github.com/andygrunwald/go-gerrit"
13+
)
14+
15+
const (
16+
// testGerritInstanceURL is a test instance url that won`t be called
17+
testGerritInstanceURL = "https://go-review.googlesource.com/"
18+
)
19+
20+
var (
21+
// testMux is the HTTP request multiplexer used with the test server.
22+
testMux *http.ServeMux
23+
24+
// testClient is the gerrit client being tested.
25+
testClient *gerrit.Client
26+
27+
// testServer is a test HTTP server used to provide mock API responses.
28+
testServer *httptest.Server
29+
)
30+
31+
type testValues map[string]string
32+
33+
// setup sets up a test HTTP server along with a gerrit.Client that is configured to talk to that test server.
34+
// Tests should register handlers on mux which provide mock responses for the API method being tested.
35+
func setup() {
36+
// Test server
37+
testMux = http.NewServeMux()
38+
testServer = httptest.NewServer(testMux)
39+
40+
// gerrit client configured to use test server
41+
testClient, _ = gerrit.NewClient(context.Background(), testServer.URL, nil)
42+
}
43+
44+
// teardown closes the test HTTP server.
45+
func teardown() {
46+
testServer.Close()
47+
}
48+
49+
// TestAddSSHKey tests the addition of an SSH key to an account.
50+
func TestAddSSHKey(t *testing.T) {
51+
setup()
52+
defer teardown()
53+
54+
testMux.HandleFunc("/accounts/self/sshkeys", func(w http.ResponseWriter, r *http.Request) {
55+
// Ensure the request method is POST
56+
if r.Method != http.MethodPost {
57+
t.Errorf("Expected POST request, got %s", r.Method)
58+
}
59+
60+
// Ensure Content-Type is text/plain
61+
if r.Header.Get("Content-Type") != "text/plain" {
62+
t.Errorf("Expected Content-Type 'text/plain', got %s", r.Header.Get("Content-Type"))
63+
}
64+
65+
// Read body and validate SSH key
66+
expectedSSHKey := "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA0T...YImydZAw== john.doe@example.com"
67+
body, _ := io.ReadAll(r.Body)
68+
receivedSSHKey := strings.TrimSpace(string(body))
69+
receivedSSHKey = strings.Trim(receivedSSHKey, `"`)
70+
71+
if receivedSSHKey != expectedSSHKey {
72+
t.Errorf("Expected SSH key '%s', but received '%s'", expectedSSHKey, receivedSSHKey)
73+
}
74+
75+
// Mock successful JSON response
76+
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
77+
w.WriteHeader(http.StatusOK)
78+
fmt.Fprint(w, `{
79+
"seq": 2,
80+
"ssh_public_key": "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA0T...YImydZAw== john.doe@example.com",
81+
"encoded_key": "AAAAB3NzaC1yc2EAAAABIwAAAQEA0T...YImydZAw==",
82+
"algorithm": "ssh-rsa",
83+
"comment": "john.doe@example.com",
84+
"valid": true
85+
}`)
86+
})
87+
88+
ctx := context.Background()
89+
sshKey := "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA0T...YImydZAw== john.doe@example.com"
90+
91+
// Use testClient.Accounts instead of undefined Accounts variable
92+
keyInfo, _, err := testClient.Accounts.AddSSHKey(ctx, "self", sshKey)
93+
if err != nil {
94+
t.Fatalf("AddSSHKey returned error: %v", err)
95+
}
96+
97+
// Verify SSH key information in the response
98+
if keyInfo.SSHPublicKey != sshKey {
99+
t.Errorf("Expected SSH key '%s', got '%s'", sshKey, keyInfo.SSHPublicKey)
100+
}
101+
102+
if keyInfo.Valid != true {
103+
t.Errorf("Expected key validity to be true, got false")
104+
}
105+
106+
if keyInfo.Comment != "john.doe@example.com" {
107+
t.Errorf("Expected comment 'john.doe@example.com', got '%s'", keyInfo.Comment)
108+
}
109+
}

0 commit comments

Comments
 (0)