Files
gerrit/polygerrit-ui/app/elements/settings/gr-registration-dialog/gr-registration-dialog_test.html
Wyatt Allen 9a1e03f932 Only permit registering username when allowed
The gr-account-details form used in settings houses logic for when to
display an editable username. In particular, the username is only
editable when the server config lists "USER_NAME" as an "editable
account field" and a username hasn't already been set.

However, this logic was not used in the gr-registration-dialog form.
Rather, an input for username was always shown, whether or not it was
editable. As a result, if a user tried to fill out a form when the
username could not be saved, it would always result in an error.

Use the same logic as gr-account-details in gr-registration-dialog for
deciding whether the username should be editable.

Bug: Issue 8167
Change-Id: Ia614b5996f0bcfe7fdf785efcebbbde27919e7d3
2018-05-31 10:59:21 -07:00

184 lines
5.2 KiB
HTML

<!DOCTYPE html>
<!--
@license
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-registration-dialog</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="../../../test/common-test-setup.html"/>
<link rel="import" href="gr-registration-dialog.html">
<script>void(0);</script>
<test-fixture id="basic">
<template>
<gr-registration-dialog></gr-registration-dialog>
</template>
</test-fixture>
<test-fixture id="blank">
<template>
<div></div>
</template>
</test-fixture>
<script>
suite('gr-registration-dialog tests', () => {
let element;
let account;
let sandbox;
let _listeners;
setup(() => {
sandbox = sinon.sandbox.create();
_listeners = {};
account = {
name: 'name',
username: null,
email: 'email',
secondary_emails: [
'email2',
'email3',
],
};
stub('gr-rest-api-interface', {
getAccount() {
return Promise.resolve(account);
},
setAccountName(name) {
account.name = name;
return Promise.resolve();
},
setAccountUsername(username) {
account.username = username;
return Promise.resolve();
},
setPreferredAccountEmail(email) {
account.email = email;
return Promise.resolve();
},
getConfig() {
return Promise.resolve(
{auth: {editable_account_fields: ['USER_NAME']}});
},
});
element = fixture('basic');
return element.loadData();
});
teardown(() => {
sandbox.restore();
for (const eventType in _listeners) {
if (_listeners.hasOwnProperty(eventType)) {
element.removeEventListener(eventType, _listeners[eventType]);
}
}
});
function listen(eventType) {
return new Promise(resolve => {
_listeners[eventType] = function() { resolve(); };
element.addEventListener(eventType, _listeners[eventType]);
});
}
function save(opt_action) {
const promise = listen('account-detail-update');
if (opt_action) {
opt_action();
} else {
MockInteractions.tap(element.$.saveButton);
}
return promise;
}
function close(opt_action) {
const promise = listen('close');
if (opt_action) {
opt_action();
} else {
MockInteractions.tap(element.$.closeButton);
}
return promise;
}
test('fires the close event on close', done => {
close().then(done);
});
test('fires the close event on save', done => {
close(() => {
MockInteractions.tap(element.$.saveButton);
}).then(done);
});
test('saves account details', done => {
flush(() => {
element.$.name.value = 'new name';
element.$.username.value = 'new username';
element.$.email.value = 'email3';
// Nothing should be committed yet.
assert.equal(account.name, 'name');
assert.isNotOk(account.username);
assert.equal(account.email, 'email');
// Save and verify new values are committed.
save().then(() => {
assert.equal(account.name, 'new name');
assert.equal(account.username, 'new username');
assert.equal(account.email, 'email3');
}).then(done);
});
});
test('email select properly populated', done => {
element._account = {email: 'foo', secondary_emails: ['bar', 'baz']};
flush(() => {
assert.equal(element.$.email.value, 'foo');
done();
});
});
test('save btn disabled', () => {
const compute = element._computeSaveDisabled;
assert.isTrue(compute('', '', false));
assert.isTrue(compute('', 'test', false));
assert.isTrue(compute('test', '', false));
assert.isTrue(compute('test', 'test', true));
assert.isFalse(compute('test', 'test', false));
});
test('_computeUsernameMutable', () => {
assert.isTrue(element._computeUsernameMutable(
{auth: {editable_account_fields: ['USER_NAME']}}, null));
assert.isFalse(element._computeUsernameMutable(
{auth: {editable_account_fields: ['USER_NAME']}}, 'abc'));
assert.isFalse(element._computeUsernameMutable(
{auth: {editable_account_fields: []}}, null));
assert.isFalse(element._computeUsernameMutable(
{auth: {editable_account_fields: []}}, 'abc'));
});
});
</script>