|
| 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