mirror of
https://github.com/bitwarden/browser
synced 2025-12-16 08:13:42 +00:00
[PM-27506] CLI allows creating SSH key items with null fields (#17063)
* Added checks to on the sshkey view to prevent null fields * Give default values to the template * Give default values to the template * change function signature to match ts-strct styles * Added unit tests for the ssh key to view and replaced deafults to empty strings
This commit is contained in:
@@ -3,6 +3,8 @@ import { SecureNoteExport } from "@bitwarden/common/models/export/secure-note.ex
|
||||
import { CipherType } from "@bitwarden/common/vault/enums";
|
||||
import { CipherView } from "@bitwarden/common/vault/models/view/cipher.view";
|
||||
|
||||
import { SshKeyExport } from "./ssh-key.export";
|
||||
|
||||
describe("Cipher Export", () => {
|
||||
describe("toView", () => {
|
||||
it.each([[null], [undefined]])(
|
||||
@@ -41,4 +43,36 @@ describe("Cipher Export", () => {
|
||||
expect(resultView.deletedDate).toEqual(request.deletedDate);
|
||||
});
|
||||
});
|
||||
|
||||
describe("SshKeyExport.toView", () => {
|
||||
const validSshKey = {
|
||||
privateKey: "PRIVATE_KEY",
|
||||
publicKey: "PUBLIC_KEY",
|
||||
keyFingerprint: "FINGERPRINT",
|
||||
};
|
||||
|
||||
it.each([null, undefined, "", " "])("should throw when privateKey is %p", (value) => {
|
||||
const sshKey = { ...validSshKey, privateKey: value } as any;
|
||||
expect(() => SshKeyExport.toView(sshKey)).toThrow("SSH key private key is required.");
|
||||
});
|
||||
|
||||
it.each([null, undefined, "", " "])("should throw when publicKey is %p", (value) => {
|
||||
const sshKey = { ...validSshKey, publicKey: value } as any;
|
||||
expect(() => SshKeyExport.toView(sshKey)).toThrow("SSH key public key is required.");
|
||||
});
|
||||
|
||||
it.each([null, undefined, "", " "])("should throw when keyFingerprint is %p", (value) => {
|
||||
const sshKey = { ...validSshKey, keyFingerprint: value } as any;
|
||||
expect(() => SshKeyExport.toView(sshKey)).toThrow("SSH key fingerprint is required.");
|
||||
});
|
||||
|
||||
it("should succeed with valid inputs", () => {
|
||||
const sshKey = { ...validSshKey };
|
||||
const result = SshKeyExport.toView(sshKey);
|
||||
expect(result).toBeDefined();
|
||||
expect(result?.privateKey).toBe(validSshKey.privateKey);
|
||||
expect(result?.publicKey).toBe(validSshKey.publicKey);
|
||||
expect(result?.keyFingerprint).toBe(validSshKey.keyFingerprint);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -16,7 +16,22 @@ export class SshKeyExport {
|
||||
return req;
|
||||
}
|
||||
|
||||
static toView(req: SshKeyExport, view = new SshKeyView()) {
|
||||
static toView(req?: SshKeyExport, view = new SshKeyView()): SshKeyView | undefined {
|
||||
if (req == null) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// Validate required fields
|
||||
if (!req.privateKey || req.privateKey.trim() === "") {
|
||||
throw new Error("SSH key private key is required.");
|
||||
}
|
||||
if (!req.publicKey || req.publicKey.trim() === "") {
|
||||
throw new Error("SSH key public key is required.");
|
||||
}
|
||||
if (!req.keyFingerprint || req.keyFingerprint.trim() === "") {
|
||||
throw new Error("SSH key fingerprint is required.");
|
||||
}
|
||||
|
||||
view.privateKey = req.privateKey;
|
||||
view.publicKey = req.publicKey;
|
||||
view.keyFingerprint = req.keyFingerprint;
|
||||
|
||||
Reference in New Issue
Block a user