Repository navigation
Expand file tree
/
Copy pathuseAssetConnection.test.ts
More file actions
154 lines (144 loc) · 5.06 KB
/
Copy pathuseAssetConnection.test.ts
File metadata and controls
154 lines (144 loc) · 5.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import type { AssetItem } from "~/types";
import type { ConnectionFormInfo } from "./useAssetConnection";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { ref } from "vue";
import { useAssetConnection } from "./useAssetConnection";
const store = {
setConnectionInfoForAsset: vi.fn(),
setConnectionPreferenceForAsset: vi.fn(),
setConnectionPreferenceForProtocol: vi.fn(),
deleteConnectionInfoForAsset: vi.fn()
};
vi.mock("~/store/modules/userInfo", () => ({ useUserInfoStore: () => store }));
const account = {
id: "account",
alias: "account",
name: "root",
username: "root",
has_secret: false,
has_username: true,
secret_type: "password",
actions: [],
date_expired: ""
};
const asset = {
id: "asset",
name: "host",
address: "192.0.2.1",
platform: "Linux",
zone: "Default",
isActive: true,
category: "host",
type: "linux",
permedAccounts: [account],
permedProtocols: [{ name: "ssh", port: 22, public: true }]
} as AssetItem;
const info: ConnectionFormInfo = {
protocol: "ssh",
account: "root",
accountId: "account",
accountMode: "hosted",
manualUsername: "",
manualPassword: "",
dynamicPassword: "",
rememberSecret: false,
connectMethod: "",
personalCredentialId: "credential",
personalCredentialVersion: 3,
personalCredentialSecretType: "ssh_key",
preserveStoredSelection: true
};
describe("hosted personal credential connections", () => {
const connect = vi.fn();
beforeEach(() => {
vi.clearAllMocks();
vi.stubGlobal("useI18n", () => ({ t: (key: string) => key }));
vi.stubGlobal("isDesktopRuntime", () => false);
vi.stubGlobal("useAssetAction", () => ({ handleAssetConnection: connect }));
vi.stubGlobal("useConnectMethods", () => ({ getMethodsForProtocol: async () => [] }));
vi.stubGlobal("useSettingManager", () => ({ appConfig: ref({}) }));
});
afterEach(() => vi.unstubAllGlobals());
it.each([false, true])("does not restore hidden protocols from cached choices (desktop=%s)", async (desktop) => {
vi.stubGlobal("isDesktopRuntime", () => desktop);
await expect(
useAssetConnection().confirmConnection(
{ ...asset, permedProtocols: [{ name: "winrm", port: 5986, public: false }] },
{ ...info, protocol: "winrm", availableProtocols: ["winrm"] }
)
).rejects.toThrow("ConnectError.ProtocolUnavailable");
expect(connect).not.toHaveBeenCalled();
});
it.each([false, true])(
"selects a visible protocol instead of a hidden saved choice (desktop=%s)",
async (desktop) => {
vi.stubGlobal("isDesktopRuntime", () => desktop);
await useAssetConnection().confirmConnection(
{
...asset,
permedAccounts: [{ ...account, has_secret: true }],
permedProtocols: [
{ name: "winrm", port: 5986, public: false },
{ name: "ssh", port: 22, public: true }
]
},
{ ...info, protocol: "winrm" }
);
expect(connect.mock.calls[0]?.[2]).toBe("ssh");
}
);
it.each(["ssh", "sftp"])("preserves a saved credential after %s form normalization", async (protocol) => {
await useAssetConnection().confirmConnection(
{
...asset,
permedProtocols: [{ name: protocol, port: 22, public: true }]
},
{ ...info, protocol }
);
expect(connect).toHaveBeenCalledWith(
"root",
"asset",
protocol,
asset.permedAccounts,
undefined,
expect.objectContaining({
accountMode: "hosted",
accountId: "account",
personalCredentialId: "credential",
personalCredentialVersion: 3
})
);
});
it("requires a replacement secret when updating a saved credential", async () => {
await expect(
useAssetConnection().confirmConnection(asset, { ...info, savePersonalCredential: true })
).rejects.toThrow("ConnectError.SecretRequired");
expect(connect).not.toHaveBeenCalled();
});
it("does not transfer a credential to a fallback account", async () => {
await expect(
useAssetConnection().confirmConnection(
{
...asset,
permedAccounts: [{ ...account, id: "different-account", name: "other" }]
},
info
)
).rejects.toThrow("ConnectError.SecretRequired");
expect(connect).not.toHaveBeenCalled();
});
it("removes personal credential selection when the account gains a managed secret", async () => {
await useAssetConnection().confirmConnection(
{ ...asset, permedAccounts: [{ ...account, has_secret: true }] },
info
);
expect(connect.mock.calls[0]![5].personalCredentialId).toBeUndefined();
});
it("remembers a hosted credential without storing its secret", () => {
useAssetConnection().saveConnectionInfo(asset, { ...info, hostedSecret: "replacement-private-key" });
const saved = store.setConnectionInfoForAsset.mock.calls[0]![1];
expect(saved).toMatchObject({ accountId: "account", accountMode: "hosted", personalCredentialId: "credential" });
expect(saved).not.toHaveProperty("hostedSecret");
expect(saved).not.toHaveProperty("manualPassword");
});
});