-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(preferences): Migrate SSH key deletion to API v3 MAASENG-4449 (#…
…5626) - Migrated SSH key delete form to API v3 - Added missing tests for ssh key query hooks - Delete form now fetches IDs from side panel context instead of URL parameters - Created mock resolver for SSH key deletion - Added generic type props to ModelActionForm to support v3 API errors Resolves [MAASENG-4449](https://warthogs.atlassian.net/browse/MAASENG-4449)
- Loading branch information
Showing
9 changed files
with
164 additions
and
126 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,7 +1,7 @@ | ||
import type { KeySource, SSHKey } from "@/app/store/sshkey/types"; | ||
import type { SshKeyResponse } from "@/app/apiclient"; | ||
|
||
export type SSHKeyFormValues = { | ||
protocol: KeySource["protocol"]; | ||
auth_id: KeySource["auth_id"]; | ||
key: SSHKey["key"]; | ||
protocol: SshKeyResponse["protocol"] | "upload" | ""; | ||
auth_id: SshKeyResponse["auth_id"]; | ||
key: SshKeyResponse["key"]; | ||
}; |
This file contains 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
130 changes: 50 additions & 80 deletions
130
src/app/preferences/views/SSHKeys/DeleteSSHKey/DeleteSSHKey.test.tsx
This file contains 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,95 +1,65 @@ | ||
import configureStore from "redux-mock-store"; | ||
|
||
import DeleteSSHKey from "./DeleteSSHKey"; | ||
|
||
import * as sidePanelHooks from "@/app/base/side-panel-context"; | ||
import { PreferenceSidePanelViews } from "@/app/preferences/constants"; | ||
import type { RootState } from "@/app/store/root/types"; | ||
import * as factory from "@/testing/factories"; | ||
import { renderWithBrowserRouter, screen, userEvent } from "@/testing/utils"; | ||
import { sshKeyResolvers } from "@/testing/resolvers/sshKeys"; | ||
import { | ||
renderWithBrowserRouter, | ||
screen, | ||
setupMockServer, | ||
userEvent, | ||
waitFor, | ||
} from "@/testing/utils"; | ||
|
||
let state: RootState; | ||
const mockStore = configureStore<RootState>(); | ||
const mockServer = setupMockServer(sshKeyResolvers.deleteSshKey.handler()); | ||
|
||
beforeEach(() => { | ||
const keys = [ | ||
factory.sshKey({ | ||
id: 1, | ||
key: "ssh-rsa aabb", | ||
keysource: { protocol: "lp", auth_id: "koalaparty" }, | ||
}), | ||
factory.sshKey({ | ||
id: 2, | ||
key: "ssh-rsa ccdd", | ||
keysource: { protocol: "gh", auth_id: "koalaparty" }, | ||
}), | ||
factory.sshKey({ | ||
id: 3, | ||
key: "ssh-rsa eeff", | ||
keysource: { protocol: "lp", auth_id: "maaate" }, | ||
}), | ||
factory.sshKey({ | ||
id: 4, | ||
key: "ssh-rsa gghh", | ||
keysource: { protocol: "gh", auth_id: "koalaparty" }, | ||
}), | ||
factory.sshKey({ id: 5, key: "ssh-rsa gghh" }), | ||
]; | ||
vi.spyOn(sidePanelHooks, "useSidePanel").mockReturnValue({ | ||
setSidePanelContent: vi.fn(), | ||
sidePanelContent: { | ||
view: PreferenceSidePanelViews.DELETE_SSH_KEYS, | ||
extras: { group: { keys } }, | ||
}, | ||
setSidePanelSize: vi.fn(), | ||
sidePanelSize: "regular", | ||
}); | ||
state = factory.rootState({ | ||
sshkey: factory.sshKeyState({ | ||
loading: false, | ||
loaded: true, | ||
items: keys, | ||
}), | ||
describe("DeleteSSHKey", () => { | ||
beforeEach(() => { | ||
vi.spyOn(sidePanelHooks, "useSidePanel").mockReturnValue({ | ||
setSidePanelContent: vi.fn(), | ||
sidePanelContent: { | ||
view: PreferenceSidePanelViews.DELETE_SSH_KEYS, | ||
}, | ||
setSidePanelSize: vi.fn(), | ||
sidePanelSize: "regular", | ||
}); | ||
}); | ||
}); | ||
|
||
it("renders", () => { | ||
renderWithBrowserRouter(<DeleteSSHKey />, { | ||
state, | ||
route: "/account/prefs/ssh-keys/delete?ids=2,3", | ||
it("renders", () => { | ||
renderWithBrowserRouter(<DeleteSSHKey />, { | ||
route: "/account/prefs/ssh-keys/delete?ids=2,3", | ||
}); | ||
expect( | ||
screen.getByRole("form", { name: "Delete SSH key confirmation" }) | ||
).toBeInTheDocument(); | ||
expect( | ||
screen.getByText("Are you sure you want to delete these SSH keys?") | ||
).toBeInTheDocument(); | ||
}); | ||
expect( | ||
screen.getByRole("form", { name: "Delete SSH key confirmation" }) | ||
).toBeInTheDocument(); | ||
expect( | ||
screen.getByText("Are you sure you want to delete these SSH keys?") | ||
).toBeInTheDocument(); | ||
}); | ||
|
||
it("can delete a group of SSH keys", async () => { | ||
const store = mockStore(state); | ||
renderWithBrowserRouter(<DeleteSSHKey />, { | ||
route: "/account/prefs/ssh-keys/delete?ids=2,3", | ||
store, | ||
it("can delete a group of SSH keys", async () => { | ||
renderWithBrowserRouter(<DeleteSSHKey />, { | ||
route: "/account/prefs/ssh-keys/delete?ids=2,3", | ||
}); | ||
await userEvent.click(screen.getByRole("button", { name: /delete/i })); | ||
|
||
await waitFor(() => { | ||
expect(sshKeyResolvers.deleteSshKey.resolved).toBeTruthy(); | ||
}); | ||
}); | ||
await userEvent.click(screen.getByRole("button", { name: /delete/i })); | ||
|
||
expect( | ||
store.getActions().some((action) => action.type === "sshkey/delete") | ||
).toBe(true); | ||
}); | ||
it("can show errors encountered when deleting SSH keys", async () => { | ||
mockServer.use( | ||
sshKeyResolvers.deleteSshKey.error({ message: "Uh oh!", code: 404 }) | ||
); | ||
renderWithBrowserRouter(<DeleteSSHKey />, { | ||
route: "/account/prefs/ssh-keys/delete?ids=2,3", | ||
}); | ||
|
||
it("can add a message when a SSH key is deleted", async () => { | ||
state.sshkey.saved = true; | ||
const store = mockStore(state); | ||
renderWithBrowserRouter(<DeleteSSHKey />, { | ||
route: "/account/prefs/ssh-keys/delete?ids=2,3", | ||
store, | ||
}); | ||
// Click on the delete button: | ||
await userEvent.click(screen.getByRole("button", { name: /delete/i })); | ||
await userEvent.click(screen.getByRole("button", { name: /delete/i })); | ||
|
||
const actions = store.getActions(); | ||
expect(actions.some((action) => action.type === "sshkey/cleanup")).toBe(true); | ||
expect(actions.some((action) => action.type === "message/add")).toBe(true); | ||
await waitFor(() => { | ||
expect(screen.getByText("Uh oh!")).toBeInTheDocument(); | ||
}); | ||
}); | ||
}); |
This file contains 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
Oops, something went wrong.