-
Notifications
You must be signed in to change notification settings - Fork 887
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 0 additions & 12 deletions
12
site/src/pages/PreferencesPages/AccountPage/SSHKeysPage.tsx
This file was deleted.
Oops, something went wrong.
87 changes: 87 additions & 0 deletions
87
site/src/pages/PreferencesPages/SSHKeysPage/SSHKeysPage.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) | ||
}) | ||
}) |
70 changes: 67 additions & 3 deletions
70
site/src/pages/PreferencesPages/SSHKeysPage/SSHKeysPage.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,76 @@ | ||
import React from "react" | ||
import Box from "@material-ui/core/Box" | ||
import Button from "@material-ui/core/Button" | ||
import CircularProgress from "@material-ui/core/CircularProgress" | ||
import { useActor } from "@xstate/react" | ||
import React, { useContext, useEffect } from "react" | ||
import { CodeBlock } from "../../../components/CodeBlock/CodeBlock" | ||
import { ConfirmDialog } from "../../../components/ConfirmDialog/ConfirmDialog" | ||
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.", | ||
regenerateLabel: "Regenerate", | ||
regenerateDialogTitle: "Regenerate SSH Key?", | ||
regenerateDialogMessage: | ||
"You will need to replace the public SSH key on services you use it with, and you'll need to rebuild existing workspaces.", | ||
confirmLabel: "Confirm", | ||
cancelLabel: "Cancel", | ||
} | ||
|
||
export const SSHKeysPage: React.FC = () => { | ||
return <Section title={Language.title} description={Language.description} /> | ||
const xServices = useContext(XServiceContext) | ||
const [authState, authSend] = useActor(xServices.authXService) | ||
const { sshKey } = authState.context | ||
|
||
useEffect(() => { | ||
authSend({ type: "GET_SSH_KEY" }) | ||
}, [authSend]) | ||
|
||
return ( | ||
<> | ||
<Section title={Language.title} description={Language.description}> | ||
{!sshKey && ( | ||
<Box p={4}> | ||
<CircularProgress size={26} /> | ||
</Box> | ||
)} | ||
|
||
{sshKey && ( | ||
<Stack> | ||
<CodeBlock lines={[sshKey.public_key.trim()]} /> | ||
<div> | ||
<Button | ||
color="primary" | ||
onClick={() => { | ||
authSend({ type: "REGENERATE_SSH_KEY" }) | ||
}} | ||
> | ||
{Language.regenerateLabel} | ||
</Button> | ||
</div> | ||
</Stack> | ||
)} | ||
</Section> | ||
|
||
<ConfirmDialog | ||
type="delete" | ||
hideCancel={false} | ||
open={authState.matches("signedIn.ssh.loaded.confirmSSHKeyRegenerate")} | ||
confirmLoading={authState.matches("signedIn.ssh.loaded.regeneratingSSHKey")} | ||
title={Language.regenerateDialogTitle} | ||
confirmText={Language.confirmLabel} | ||
onConfirm={() => { | ||
authSend({ type: "CONFIRM_REGENERATE_SSH_KEY" }) | ||
}} | ||
onClose={() => { | ||
authSend({ type: "CANCEL_REGENERATE_SSH_KEY" }) | ||
}} | ||
description={<>{Language.regenerateDialogMessage}</>} | ||
/> | ||
</> | ||
) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we rename this
UserEvent
instead? Seems like it's more accurate.