blob: d313f5a50b3821a5b95763dd78675556e5891603 [file] [log] [blame]
Wyatt Allenbe9869c2016-06-20 15:50:37 -07001<!DOCTYPE html>
2<!--
Dave Borowitz8cdc76b2018-03-26 10:04:27 -04003@license
Wyatt Allenbe9869c2016-06-20 15:50:37 -07004Copyright (C) 2016 The Android Open Source Project
5
6Licensed under the Apache License, Version 2.0 (the "License");
7you may not use this file except in compliance with the License.
8You may obtain a copy of the License at
9
10http://www.apache.org/licenses/LICENSE-2.0
11
12Unless required by applicable law or agreed to in writing, software
13distributed under the License is distributed on an "AS IS" BASIS,
14WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15See the License for the specific language governing permissions and
16limitations under the License.
17-->
18
19<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">
20<title>gr-ssh-editor</title>
Ole Rehmsen62909352019-05-16 16:10:33 +020021<script src="/test/common-test-setup.js"></script>
22<script src="/bower_components/webcomponentsjs/custom-elements-es5-adapter.js"></script>
Wyatt Allenbe9869c2016-06-20 15:50:37 -070023
Ole Rehmsenecf0b782019-05-16 11:29:39 +020024<script src="/bower_components/webcomponentsjs/webcomponents-lite.js"></script>
Ole Rehmsen31640742019-05-16 11:24:47 +020025<script src="/bower_components/web-component-tester/browser.js"></script>
Mike Samuele07c4b22017-06-02 13:08:19 -040026<link rel="import" href="../../../test/common-test-setup.html"/>
Wyatt Allenbe9869c2016-06-20 15:50:37 -070027<link rel="import" href="gr-ssh-editor.html">
28
Viktar Donich29e1ce52017-03-28 17:02:44 -070029<script>void(0);</script>
30
Wyatt Allenbe9869c2016-06-20 15:50:37 -070031<test-fixture id="basic">
32 <template>
33 <gr-ssh-editor></gr-ssh-editor>
34 </template>
35</test-fixture>
36
37<script>
Kasper Nilssonc15f5562017-05-15 17:45:47 -070038 suite('gr-ssh-editor tests', () => {
39 let element;
40 let keys;
Wyatt Allenbe9869c2016-06-20 15:50:37 -070041
Kasper Nilssonc15f5562017-05-15 17:45:47 -070042 setup(done => {
Wyatt Allenbe9869c2016-06-20 15:50:37 -070043 keys = [{
44 seq: 1,
45 ssh_public_key: 'ssh-rsa <key 1> comment-one@machine-one',
46 encoded_key: '<key 1>',
47 algorithm: 'ssh-rsa',
48 comment: 'comment-one@machine-one',
49 valid: true,
50 }, {
51 seq: 2,
52 ssh_public_key: 'ssh-rsa <key 2> comment-two@machine-two',
53 encoded_key: '<key 2>',
54 algorithm: 'ssh-rsa',
55 comment: 'comment-two@machine-two',
56 valid: true,
57 }];
58
59 stub('gr-rest-api-interface', {
Kasper Nilssonc15f5562017-05-15 17:45:47 -070060 getAccountSSHKeys() { return Promise.resolve(keys); },
Wyatt Allenbe9869c2016-06-20 15:50:37 -070061 });
62
63 element = fixture('basic');
64
Kasper Nilssonc15f5562017-05-15 17:45:47 -070065 element.loadData().then(() => { flush(done); });
Wyatt Allenbe9869c2016-06-20 15:50:37 -070066 });
67
Kasper Nilssonc15f5562017-05-15 17:45:47 -070068 test('renders', () => {
69 const rows = Polymer.dom(element.root).querySelectorAll('tbody tr');
Wyatt Allenbe9869c2016-06-20 15:50:37 -070070
71 assert.equal(rows.length, 2);
72
Kasper Nilssonc15f5562017-05-15 17:45:47 -070073 let cells = rows[0].querySelectorAll('td');
Wyatt Allenbe9869c2016-06-20 15:50:37 -070074 assert.equal(cells[0].textContent, keys[0].comment);
75
76 cells = rows[1].querySelectorAll('td');
77 assert.equal(cells[0].textContent, keys[1].comment);
78 });
79
Kasper Nilssonc15f5562017-05-15 17:45:47 -070080 test('remove key', done => {
81 const lastKey = keys[1];
Wyatt Allenbe9869c2016-06-20 15:50:37 -070082
Kasper Nilssonc15f5562017-05-15 17:45:47 -070083 const saveStub = sinon.stub(element.$.restAPI, 'deleteAccountSSHKey',
84 () => { return Promise.resolve(); });
Wyatt Allenbe9869c2016-06-20 15:50:37 -070085
86 assert.equal(element._keysToRemove.length, 0);
87 assert.isFalse(element.hasUnsavedChanges);
88
89 // Get the delete button for the last row.
Kasper Nilssonc15f5562017-05-15 17:45:47 -070090 const button = Polymer.dom(element.root).querySelector(
Paladox nonee121d242019-10-06 23:03:04 +000091 'tbody tr:last-of-type td:nth-child(5) gr-button');
Wyatt Allenbe9869c2016-06-20 15:50:37 -070092
93 MockInteractions.tap(button);
94
95 assert.equal(element._keys.length, 1);
96 assert.equal(element._keysToRemove.length, 1);
97 assert.equal(element._keysToRemove[0], lastKey);
98 assert.isTrue(element.hasUnsavedChanges);
99 assert.isFalse(saveStub.called);
100
Kasper Nilssonc15f5562017-05-15 17:45:47 -0700101 element.save().then(() => {
Wyatt Allenbe9869c2016-06-20 15:50:37 -0700102 assert.isTrue(saveStub.called);
103 assert.equal(saveStub.lastCall.args[0], lastKey.seq);
104 assert.equal(element._keysToRemove.length, 0);
105 assert.isFalse(element.hasUnsavedChanges);
106 done();
107 });
108 });
109
Kasper Nilssonc15f5562017-05-15 17:45:47 -0700110 test('show key', () => {
111 const openSpy = sinon.spy(element.$.viewKeyOverlay, 'open');
Wyatt Allenbe9869c2016-06-20 15:50:37 -0700112
113 // Get the show button for the last row.
Kasper Nilssonc15f5562017-05-15 17:45:47 -0700114 const button = Polymer.dom(element.root).querySelector(
Wyatt Allenbe9869c2016-06-20 15:50:37 -0700115 'tbody tr:last-of-type td:nth-child(3) gr-button');
116
117 MockInteractions.tap(button);
118
119 assert.equal(element._keyToView, keys[1]);
120 assert.isTrue(openSpy.called);
121 });
122
Kasper Nilssonc15f5562017-05-15 17:45:47 -0700123 test('add key', done => {
124 const newKeyString = 'ssh-rsa <key 3> comment-three@machine-three';
125 const newKeyObject = {
Wyatt Allenbe9869c2016-06-20 15:50:37 -0700126 seq: 3,
127 ssh_public_key: newKeyString,
128 encoded_key: '<key 3>',
129 algorithm: 'ssh-rsa',
130 comment: 'comment-three@machine-three',
131 valid: true,
132 };
133
Kasper Nilssonc15f5562017-05-15 17:45:47 -0700134 const addStub = sinon.stub(element.$.restAPI, 'addAccountSSHKey',
135 () => { return Promise.resolve(newKeyObject); });
Wyatt Allenbe9869c2016-06-20 15:50:37 -0700136
137 element._newKey = newKeyString;
138
139 assert.isFalse(element.$.addButton.disabled);
140 assert.isFalse(element.$.newKey.disabled);
141
Kasper Nilssonc15f5562017-05-15 17:45:47 -0700142 element._handleAddKey().then(() => {
Wyatt Allenbe9869c2016-06-20 15:50:37 -0700143 assert.isTrue(element.$.addButton.disabled);
144 assert.isFalse(element.$.newKey.disabled);
145 assert.equal(element._keys.length, 3);
146 done();
147 });
148
149 assert.isTrue(element.$.addButton.disabled);
150 assert.isTrue(element.$.newKey.disabled);
151
152 assert.isTrue(addStub.called);
153 assert.equal(addStub.lastCall.args[0], newKeyString);
154 });
155
Kasper Nilssonc15f5562017-05-15 17:45:47 -0700156 test('add invalid key', done => {
157 const newKeyString = 'not even close to valid';
Wyatt Allenbe9869c2016-06-20 15:50:37 -0700158
Kasper Nilssonc15f5562017-05-15 17:45:47 -0700159 const addStub = sinon.stub(element.$.restAPI, 'addAccountSSHKey',
Thomas Shafer2a1a93b62019-02-07 14:40:02 -0800160 () => { return Promise.reject(new Error('error')); });
Wyatt Allenbe9869c2016-06-20 15:50:37 -0700161
162 element._newKey = newKeyString;
163
164 assert.isFalse(element.$.addButton.disabled);
165 assert.isFalse(element.$.newKey.disabled);
166
Kasper Nilssonc15f5562017-05-15 17:45:47 -0700167 element._handleAddKey().then(() => {
Wyatt Allenbe9869c2016-06-20 15:50:37 -0700168 assert.isFalse(element.$.addButton.disabled);
169 assert.isFalse(element.$.newKey.disabled);
170 assert.equal(element._keys.length, 2);
171 done();
172 });
173
174 assert.isTrue(element.$.addButton.disabled);
175 assert.isTrue(element.$.newKey.disabled);
176
177 assert.isTrue(addStub.called);
178 assert.equal(addStub.lastCall.args[0], newKeyString);
179 });
180 });
181</script>