Skip to content

feat: Add SSH Keys page on /preferences/ssh-keys #1478

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add missing tests
  • Loading branch information
BrunoQuaresma committed May 16, 2022
commit 69458968d5495c3997c8020624575f53e104b2a3
87 changes: 87 additions & 0 deletions site/src/pages/PreferencesPages/SSHKeysPage/SSHKeysPage.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { fireEvent, screen, within } from "@testing-library/react"
import React from "react"
import * as API from "../../../api/api"
import { GlobalSnackbar } from "../../../components/GlobalSnackbar/GlobalSnackbar"
import { MockGitSSHKey, renderWithAuth } from "../../../testHelpers/renderHelpers"
import { Language as authXServiceLanguage } from "../../../xServices/auth/authXService"
import { Language as SSHKeysPageLanguage, SSHKeysPage } from "./SSHKeysPage"

describe("SSH Keys Page", () => {
it("shows the SSH key", async () => {
renderWithAuth(<SSHKeysPage />)
await screen.findByText(MockGitSSHKey.public_key)
})

describe("regenerate SSH key", () => {
describe("when it is success", () => {
it("shows a success message and updates the ssh key on the page", async () => {
renderWithAuth(
<>
<SSHKeysPage />
<GlobalSnackbar />
</>,
)

// Wait to the ssh be rendered on the screen
await screen.findByText(MockGitSSHKey.public_key)

// Click on the "Regenerate" button to display the confirm dialog
const regenerateButton = screen.getByRole("button", { name: SSHKeysPageLanguage.regenerateLabel })
fireEvent.click(regenerateButton)
const confirmDialog = screen.getByRole("dialog")
expect(confirmDialog).toHaveTextContent(SSHKeysPageLanguage.regenerateDialogMessage)

const newUserSSHKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDSC/ouD/LqiT1Rd99vDv/MwUmqzJuinLTMTpk5kVy66"
jest.spyOn(API, "regenerateUserSSHKey").mockResolvedValueOnce({
...MockGitSSHKey,
public_key: newUserSSHKey,
})

// Click on the "Confirm" button
const confirmButton = within(confirmDialog).getByRole("button", { name: SSHKeysPageLanguage.confirmLabel })
fireEvent.click(confirmButton)

// Check if the success message is displayed
await screen.findByText(authXServiceLanguage.successRegenerateSSHKey)

// Check if the API was called correctly
expect(API.regenerateUserSSHKey).toBeCalledTimes(1)

// Check if the SSH key is updated
await screen.findByText(newUserSSHKey)
})
})

describe("when it fails", () => {
it("shows an error message", async () => {
renderWithAuth(
<>
<SSHKeysPage />
<GlobalSnackbar />
</>,
)

// Wait to the ssh be rendered on the screen
await screen.findByText(MockGitSSHKey.public_key)

jest.spyOn(API, "regenerateUserSSHKey").mockRejectedValueOnce({})

// Click on the "Regenerate" button to display the confirm dialog
const regenerateButton = screen.getByRole("button", { name: SSHKeysPageLanguage.regenerateLabel })
fireEvent.click(regenerateButton)
const confirmDialog = screen.getByRole("dialog")
expect(confirmDialog).toHaveTextContent(SSHKeysPageLanguage.regenerateDialogMessage)

// Click on the "Confirm" button
const confirmButton = within(confirmDialog).getByRole("button", { name: SSHKeysPageLanguage.confirmLabel })
fireEvent.click(confirmButton)

// Check if the error message is displayed
await screen.findByText(authXServiceLanguage.errorRegenerateSSHKey)

// Check if the API was called correctly
expect(API.regenerateUserSSHKey).toBeCalledTimes(1)
})
})
})
})
4 changes: 2 additions & 2 deletions site/src/pages/PreferencesPages/SSHKeysPage/SSHKeysPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Section } from "../../../components/Section/Section"
import { Stack } from "../../../components/Stack/Stack"
import { XServiceContext } from "../../../xServices/StateContext"

const Language = {
export const Language = {
title: "SSH Keys",
description:
"Coder automatically inserts a private key into every workspace; you can add the corresponding public key to any services (such as Git) that you need access to from your workspace.",
Expand Down Expand Up @@ -41,7 +41,7 @@ export const SSHKeysPage: React.FC = () => {

{sshKey && (
<Stack>
<CodeBlock lines={[sshKey.public_key]} />
<CodeBlock lines={[sshKey.public_key.trim()]} />
<div>
<Button
color="primary"
Expand Down
7 changes: 7 additions & 0 deletions site/src/testHelpers/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,10 @@ export const MockAuthMethods: TypesGen.AuthMethods = {
password: true,
github: false,
}

export const MockGitSSHKey: TypesGen.GitSSHKey = {
user_id: "1fa0200f-7331-4524-a364-35770666caa7",
created_at: "2022-05-16T14:30:34.148205897Z",
updated_at: "2022-05-16T15:29:10.302441433Z",
public_key: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFJOQRIM7kE30rOzrfy+/+R+nQGCk7S9pioihy+2ARbq",
}
3 changes: 3 additions & 0 deletions site/src/testHelpers/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ export const handlers = [

return res(ctx.status(200), ctx.json(response))
}),
rest.get("/api/v2/users/:userId/gitsshkey", async (req, res, ctx) => {
return res(ctx.status(200), ctx.json(M.MockGitSSHKey))
}),

// workspaces
rest.get("/api/v2/organizations/:organizationId/workspaces/:userName/:workspaceName", (req, res, ctx) => {
Expand Down