blob: 7bb5528a33df6c5c11fbaae34254c1d675ad6429 [file] [log] [blame]
<!DOCTYPE html>
<!--
Copyright (C) 2016 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">
<title>gr-ssh-editor</title>
<script src="../../../bower_components/webcomponentsjs/webcomponents-lite.min.js"></script>
<script src="../../../bower_components/web-component-tester/browser.js"></script>
<link rel="import" href="../../../bower_components/iron-test-helpers/iron-test-helpers.html">
<link rel="import" href="gr-ssh-editor.html">
<script>void(0);</script>
<test-fixture id="basic">
<template>
<gr-ssh-editor></gr-ssh-editor>
</template>
</test-fixture>
<script>
suite('gr-ssh-editor tests', function() {
var element;
var keys;
setup(function(done) {
keys = [{
seq: 1,
ssh_public_key: 'ssh-rsa <key 1> comment-one@machine-one',
encoded_key: '<key 1>',
algorithm: 'ssh-rsa',
comment: 'comment-one@machine-one',
valid: true,
}, {
seq: 2,
ssh_public_key: 'ssh-rsa <key 2> comment-two@machine-two',
encoded_key: '<key 2>',
algorithm: 'ssh-rsa',
comment: 'comment-two@machine-two',
valid: true,
}];
stub('gr-rest-api-interface', {
getAccountSSHKeys: function() { return Promise.resolve(keys); },
});
element = fixture('basic');
element.loadData().then(function() { flush(done); });
});
test('renders', function() {
var rows = Polymer.dom(element.root).querySelectorAll('tbody tr');
assert.equal(rows.length, 2);
var cells = rows[0].querySelectorAll('td');
assert.equal(cells[0].textContent, keys[0].comment);
cells = rows[1].querySelectorAll('td');
assert.equal(cells[0].textContent, keys[1].comment);
});
test('remove key', function(done) {
var lastKey = keys[1];
var saveStub = sinon.stub(element.$.restAPI, 'deleteAccountSSHKey',
function() { return Promise.resolve(); });
assert.equal(element._keysToRemove.length, 0);
assert.isFalse(element.hasUnsavedChanges);
// Get the delete button for the last row.
var button = Polymer.dom(element.root).querySelector(
'tbody tr:last-of-type td:nth-child(4) gr-button');
MockInteractions.tap(button);
assert.equal(element._keys.length, 1);
assert.equal(element._keysToRemove.length, 1);
assert.equal(element._keysToRemove[0], lastKey);
assert.isTrue(element.hasUnsavedChanges);
assert.isFalse(saveStub.called);
element.save().then(function() {
assert.isTrue(saveStub.called);
assert.equal(saveStub.lastCall.args[0], lastKey.seq);
assert.equal(element._keysToRemove.length, 0);
assert.isFalse(element.hasUnsavedChanges);
done();
});
});
test('show key', function() {
var openSpy = sinon.spy(element.$.viewKeyOverlay, 'open');
// Get the show button for the last row.
var button = Polymer.dom(element.root).querySelector(
'tbody tr:last-of-type td:nth-child(3) gr-button');
MockInteractions.tap(button);
assert.equal(element._keyToView, keys[1]);
assert.isTrue(openSpy.called);
});
test('add key', function(done) {
var newKeyString = 'ssh-rsa <key 3> comment-three@machine-three';
var newKeyObject = {
seq: 3,
ssh_public_key: newKeyString,
encoded_key: '<key 3>',
algorithm: 'ssh-rsa',
comment: 'comment-three@machine-three',
valid: true,
};
var addStub = sinon.stub(element.$.restAPI, 'addAccountSSHKey',
function() { return Promise.resolve(newKeyObject); });
element._newKey = newKeyString;
assert.isFalse(element.$.addButton.disabled);
assert.isFalse(element.$.newKey.disabled);
element._handleAddKey().then(function() {
assert.isTrue(element.$.addButton.disabled);
assert.isFalse(element.$.newKey.disabled);
assert.equal(element._keys.length, 3);
done();
});
assert.isTrue(element.$.addButton.disabled);
assert.isTrue(element.$.newKey.disabled);
assert.isTrue(addStub.called);
assert.equal(addStub.lastCall.args[0], newKeyString);
});
test('add invalid key', function(done) {
var newKeyString = 'not even close to valid';
var addStub = sinon.stub(element.$.restAPI, 'addAccountSSHKey',
function() { return Promise.reject(); });
element._newKey = newKeyString;
assert.isFalse(element.$.addButton.disabled);
assert.isFalse(element.$.newKey.disabled);
element._handleAddKey().then(function() {
assert.isFalse(element.$.addButton.disabled);
assert.isFalse(element.$.newKey.disabled);
assert.equal(element._keys.length, 2);
done();
});
assert.isTrue(element.$.addButton.disabled);
assert.isTrue(element.$.newKey.disabled);
assert.isTrue(addStub.called);
assert.equal(addStub.lastCall.args[0], newKeyString);
});
});
</script>