forked from libapps/libapps-mirror
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnassh_google_tests.js
More file actions
137 lines (112 loc) · 3.71 KB
/
nassh_google_tests.js
File metadata and controls
137 lines (112 loc) · 3.71 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
// Copyright 2025 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* @fileoverview Test suite for functions in nassh_google.js.
*/
import {fetchSshPolicy} from './nassh_google.js';
/**
* A mock for the chrome.runtime.sendMessage API.
*/
class MockChromeRuntime {
constructor() {
// We need this assignment so that chrome.runtime wouldn't be undefined.
/** @suppress {constantProperty} */
chrome.runtime = chrome.runtime || {};
this.originalSendMessage_ = chrome.runtime.sendMessage;
this.mockResponseData_ = undefined;
}
/**
* Starts the mock, replacing the original sendMessage with our mock version.
*/
start() {
chrome.runtime.sendMessage = (...args) => {
const callback = args[args.length - 1];
if (callback instanceof Function) {
/** @type {function(*)} */
const fn = callback;
setTimeout(() => fn(this.mockResponseData_), 0);
} else {
return this.mockResponseData_;
}
};
}
/**
* Stops the mock and restores the original sendMessage function.
*/
stop() {
chrome.runtime.sendMessage = this.originalSendMessage_;
}
/**
* Sets the data that the mock sendMessage function will return.
* @param {*} data The data to return.
*/
setResponseData(data) {
this.mockResponseData_ = data;
}
}
describe('fetchSshPolicy', function() {
const sshPolicyResponse = 'get_ssh_policy_response';
beforeEach(function() {
this.mockRuntime = new MockChromeRuntime();
this.mockRuntime.start();
this.createSuccessData = (sshKnownHosts = '', sshConfig = '') => {
this.mockRuntime.setResponseData({
'type': sshPolicyResponse,
'data': {
sshKnownHosts,
sshConfig,
},
});
};
});
afterEach(function() {
this.mockRuntime.stop();
});
it('returns sshPolicy in the correct shape', async function() {
const sshKnownHosts = 'sshKnownHosts';
const sshConfig = 'sshConfig';
this.createSuccessData(sshKnownHosts, sshConfig);
const response = await fetchSshPolicy();
assert.equal(response.getSshKnownHosts(), sshKnownHosts);
assert.equal(response.getSshConfig(), sshConfig);
});
it('returns sshPolicy with empty data if the data returned from SKE is ' +
'empty', async function() {
this.createSuccessData();
const response = await fetchSshPolicy();
assert.equal(response.getSshKnownHosts(), '');
assert.equal(response.getSshConfig(), '');
});
it('returns sshPolicy with empty data if the data returned from SKE does ' +
'not include the required key', async function() {
this.createSuccessData();
const response = await fetchSshPolicy();
assert.equal(response.getSshKnownHosts(), '');
assert.equal(response.getSshConfig(), '');
});
it('returns sshPolicy with empty data if the data returned from SKE ' +
'includes malformed key', async function() {
this.mockRuntime.setResponseData({
'type': sshPolicyResponse,
'data': {
'unknown_key': 'random_key',
},
});
const response = await fetchSshPolicy();
assert.equal(response.getSshKnownHosts(), '');
assert.equal(response.getSshConfig(), '');
});
it('returns sshPolicy with empty data if the SKE returns ' +
'error', async function() {
this.mockRuntime.setResponseData({
'type': 'error_response',
'errorDetail': 'test',
'errorReason': 'other error',
'requestId': 1847507321,
});
const response = await fetchSshPolicy();
assert.equal(response.getSshKnownHosts(), '');
assert.equal(response.getSshConfig(), '');
});
});