Wyatt Allen | be9869c | 2016-06-20 15:50:37 -0700 | [diff] [blame] | 1 | <!DOCTYPE html> |
| 2 | <!-- |
Dave Borowitz | 8cdc76b | 2018-03-26 10:04:27 -0400 | [diff] [blame] | 3 | @license |
Wyatt Allen | be9869c | 2016-06-20 15:50:37 -0700 | [diff] [blame] | 4 | Copyright (C) 2016 The Android Open Source Project |
| 5 | |
| 6 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | you may not use this file except in compliance with the License. |
| 8 | You may obtain a copy of the License at |
| 9 | |
| 10 | http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | |
| 12 | Unless required by applicable law or agreed to in writing, software |
| 13 | distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | See the License for the specific language governing permissions and |
| 16 | limitations 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 Rehmsen | 6290935 | 2019-05-16 16:10:33 +0200 | [diff] [blame] | 21 | <script src="/test/common-test-setup.js"></script> |
| 22 | <script src="/bower_components/webcomponentsjs/custom-elements-es5-adapter.js"></script> |
Wyatt Allen | be9869c | 2016-06-20 15:50:37 -0700 | [diff] [blame] | 23 | |
Ole Rehmsen | ecf0b78 | 2019-05-16 11:29:39 +0200 | [diff] [blame] | 24 | <script src="/bower_components/webcomponentsjs/webcomponents-lite.js"></script> |
Ole Rehmsen | 3164074 | 2019-05-16 11:24:47 +0200 | [diff] [blame] | 25 | <script src="/bower_components/web-component-tester/browser.js"></script> |
Mike Samuel | e07c4b2 | 2017-06-02 13:08:19 -0400 | [diff] [blame] | 26 | <link rel="import" href="../../../test/common-test-setup.html"/> |
Wyatt Allen | be9869c | 2016-06-20 15:50:37 -0700 | [diff] [blame] | 27 | <link rel="import" href="gr-ssh-editor.html"> |
| 28 | |
Viktar Donich | 29e1ce5 | 2017-03-28 17:02:44 -0700 | [diff] [blame] | 29 | <script>void(0);</script> |
| 30 | |
Wyatt Allen | be9869c | 2016-06-20 15:50:37 -0700 | [diff] [blame] | 31 | <test-fixture id="basic"> |
| 32 | <template> |
| 33 | <gr-ssh-editor></gr-ssh-editor> |
| 34 | </template> |
| 35 | </test-fixture> |
| 36 | |
| 37 | <script> |
Kasper Nilsson | c15f556 | 2017-05-15 17:45:47 -0700 | [diff] [blame] | 38 | suite('gr-ssh-editor tests', () => { |
| 39 | let element; |
| 40 | let keys; |
Wyatt Allen | be9869c | 2016-06-20 15:50:37 -0700 | [diff] [blame] | 41 | |
Kasper Nilsson | c15f556 | 2017-05-15 17:45:47 -0700 | [diff] [blame] | 42 | setup(done => { |
Wyatt Allen | be9869c | 2016-06-20 15:50:37 -0700 | [diff] [blame] | 43 | 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 Nilsson | c15f556 | 2017-05-15 17:45:47 -0700 | [diff] [blame] | 60 | getAccountSSHKeys() { return Promise.resolve(keys); }, |
Wyatt Allen | be9869c | 2016-06-20 15:50:37 -0700 | [diff] [blame] | 61 | }); |
| 62 | |
| 63 | element = fixture('basic'); |
| 64 | |
Kasper Nilsson | c15f556 | 2017-05-15 17:45:47 -0700 | [diff] [blame] | 65 | element.loadData().then(() => { flush(done); }); |
Wyatt Allen | be9869c | 2016-06-20 15:50:37 -0700 | [diff] [blame] | 66 | }); |
| 67 | |
Kasper Nilsson | c15f556 | 2017-05-15 17:45:47 -0700 | [diff] [blame] | 68 | test('renders', () => { |
| 69 | const rows = Polymer.dom(element.root).querySelectorAll('tbody tr'); |
Wyatt Allen | be9869c | 2016-06-20 15:50:37 -0700 | [diff] [blame] | 70 | |
| 71 | assert.equal(rows.length, 2); |
| 72 | |
Kasper Nilsson | c15f556 | 2017-05-15 17:45:47 -0700 | [diff] [blame] | 73 | let cells = rows[0].querySelectorAll('td'); |
Wyatt Allen | be9869c | 2016-06-20 15:50:37 -0700 | [diff] [blame] | 74 | 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 Nilsson | c15f556 | 2017-05-15 17:45:47 -0700 | [diff] [blame] | 80 | test('remove key', done => { |
| 81 | const lastKey = keys[1]; |
Wyatt Allen | be9869c | 2016-06-20 15:50:37 -0700 | [diff] [blame] | 82 | |
Kasper Nilsson | c15f556 | 2017-05-15 17:45:47 -0700 | [diff] [blame] | 83 | const saveStub = sinon.stub(element.$.restAPI, 'deleteAccountSSHKey', |
| 84 | () => { return Promise.resolve(); }); |
Wyatt Allen | be9869c | 2016-06-20 15:50:37 -0700 | [diff] [blame] | 85 | |
| 86 | assert.equal(element._keysToRemove.length, 0); |
| 87 | assert.isFalse(element.hasUnsavedChanges); |
| 88 | |
| 89 | // Get the delete button for the last row. |
Kasper Nilsson | c15f556 | 2017-05-15 17:45:47 -0700 | [diff] [blame] | 90 | const button = Polymer.dom(element.root).querySelector( |
Paladox none | e121d24 | 2019-10-06 23:03:04 +0000 | [diff] [blame] | 91 | 'tbody tr:last-of-type td:nth-child(5) gr-button'); |
Wyatt Allen | be9869c | 2016-06-20 15:50:37 -0700 | [diff] [blame] | 92 | |
| 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 Nilsson | c15f556 | 2017-05-15 17:45:47 -0700 | [diff] [blame] | 101 | element.save().then(() => { |
Wyatt Allen | be9869c | 2016-06-20 15:50:37 -0700 | [diff] [blame] | 102 | 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 Nilsson | c15f556 | 2017-05-15 17:45:47 -0700 | [diff] [blame] | 110 | test('show key', () => { |
| 111 | const openSpy = sinon.spy(element.$.viewKeyOverlay, 'open'); |
Wyatt Allen | be9869c | 2016-06-20 15:50:37 -0700 | [diff] [blame] | 112 | |
| 113 | // Get the show button for the last row. |
Kasper Nilsson | c15f556 | 2017-05-15 17:45:47 -0700 | [diff] [blame] | 114 | const button = Polymer.dom(element.root).querySelector( |
Wyatt Allen | be9869c | 2016-06-20 15:50:37 -0700 | [diff] [blame] | 115 | '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 Nilsson | c15f556 | 2017-05-15 17:45:47 -0700 | [diff] [blame] | 123 | test('add key', done => { |
| 124 | const newKeyString = 'ssh-rsa <key 3> comment-three@machine-three'; |
| 125 | const newKeyObject = { |
Wyatt Allen | be9869c | 2016-06-20 15:50:37 -0700 | [diff] [blame] | 126 | 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 Nilsson | c15f556 | 2017-05-15 17:45:47 -0700 | [diff] [blame] | 134 | const addStub = sinon.stub(element.$.restAPI, 'addAccountSSHKey', |
| 135 | () => { return Promise.resolve(newKeyObject); }); |
Wyatt Allen | be9869c | 2016-06-20 15:50:37 -0700 | [diff] [blame] | 136 | |
| 137 | element._newKey = newKeyString; |
| 138 | |
| 139 | assert.isFalse(element.$.addButton.disabled); |
| 140 | assert.isFalse(element.$.newKey.disabled); |
| 141 | |
Kasper Nilsson | c15f556 | 2017-05-15 17:45:47 -0700 | [diff] [blame] | 142 | element._handleAddKey().then(() => { |
Wyatt Allen | be9869c | 2016-06-20 15:50:37 -0700 | [diff] [blame] | 143 | 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 Nilsson | c15f556 | 2017-05-15 17:45:47 -0700 | [diff] [blame] | 156 | test('add invalid key', done => { |
| 157 | const newKeyString = 'not even close to valid'; |
Wyatt Allen | be9869c | 2016-06-20 15:50:37 -0700 | [diff] [blame] | 158 | |
Kasper Nilsson | c15f556 | 2017-05-15 17:45:47 -0700 | [diff] [blame] | 159 | const addStub = sinon.stub(element.$.restAPI, 'addAccountSSHKey', |
Thomas Shafer | 2a1a93b6 | 2019-02-07 14:40:02 -0800 | [diff] [blame] | 160 | () => { return Promise.reject(new Error('error')); }); |
Wyatt Allen | be9869c | 2016-06-20 15:50:37 -0700 | [diff] [blame] | 161 | |
| 162 | element._newKey = newKeyString; |
| 163 | |
| 164 | assert.isFalse(element.$.addButton.disabled); |
| 165 | assert.isFalse(element.$.newKey.disabled); |
| 166 | |
Kasper Nilsson | c15f556 | 2017-05-15 17:45:47 -0700 | [diff] [blame] | 167 | element._handleAddKey().then(() => { |
Wyatt Allen | be9869c | 2016-06-20 15:50:37 -0700 | [diff] [blame] | 168 | 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> |