263 lines
8.5 KiB
Plaintext
263 lines
8.5 KiB
Plaintext
import { module, test } from 'qunit';
|
|
import { setupRenderingTest } from 'dashtab/tests/helpers';
|
|
import { render, fillIn, triggerEvent } from '@ember/test-helpers';
|
|
import HomeAssistant from 'dashtab/components/settings/integrations/home-assistant-form';
|
|
import { HomeAssistantAuthError } from 'dashtab/services/home-assistant';
|
|
|
|
function registerServices(owner, overrides = {}) {
|
|
const homeAssistant = overrides.homeAssistant ?? {
|
|
fetchSummary: async () => ({}),
|
|
};
|
|
const storage = overrides.storage ?? { set: async () => {} };
|
|
const sites = overrides.sites ?? { reload: async () => {} };
|
|
const notifications = overrides.notifications ?? { startPolling: () => {} };
|
|
|
|
owner.register('service:home-assistant', homeAssistant, {
|
|
instantiate: false,
|
|
});
|
|
owner.register('service:storage', storage, { instantiate: false });
|
|
owner.register('service:sites', sites, { instantiate: false });
|
|
owner.register('service:notifications', notifications, {
|
|
instantiate: false,
|
|
});
|
|
}
|
|
|
|
module(
|
|
'Integration | Component | settings/integrations/home-assistant-form',
|
|
function (hooks) {
|
|
setupRenderingTest(hooks);
|
|
|
|
test('it renders the token form without a default URL', async function (assert) {
|
|
registerServices(this.owner);
|
|
const noop = () => {};
|
|
|
|
await render(<template><HomeAssistant @close={{noop}} /></template>);
|
|
|
|
assert.dom().containsText('Add Home Assistant');
|
|
assert.dom('#homeassistant-url').exists().hasValue('');
|
|
assert.dom('#homeassistant-token').exists();
|
|
assert.dom('#homeassistant-name').exists().hasValue('');
|
|
assert.dom('form').exists();
|
|
});
|
|
|
|
test('it links to the profile settings once a URL is entered', async function (assert) {
|
|
registerServices(this.owner);
|
|
const noop = () => {};
|
|
|
|
await render(<template><HomeAssistant @close={{noop}} /></template>);
|
|
|
|
assert
|
|
.dom('a[href="https://ha.example.com:8123/profile/security"]')
|
|
.doesNotExist();
|
|
|
|
await fillIn('#homeassistant-url', 'ha.example.com:8123');
|
|
|
|
assert
|
|
.dom('a[href="https://ha.example.com:8123/profile/security"]')
|
|
.exists()
|
|
.hasAttribute('target', '_blank');
|
|
});
|
|
|
|
test('submitting validates and stores the credentials under a host key', async function (assert) {
|
|
const verified = [];
|
|
const saved = [];
|
|
let reloaded = false;
|
|
let polling = false;
|
|
let closed = false;
|
|
|
|
registerServices(this.owner, {
|
|
homeAssistant: {
|
|
fetchSummary: async (integration) => {
|
|
verified.push(integration);
|
|
return { unreadCount: 0 };
|
|
},
|
|
},
|
|
storage: {
|
|
set: async (namespace, key, value) =>
|
|
saved.push({ namespace, key, value }),
|
|
},
|
|
sites: {
|
|
reload: async () => {
|
|
reloaded = true;
|
|
},
|
|
},
|
|
notifications: {
|
|
startPolling: () => {
|
|
polling = true;
|
|
},
|
|
},
|
|
});
|
|
|
|
const closeDialog = () => {
|
|
closed = true;
|
|
};
|
|
|
|
await render(
|
|
<template><HomeAssistant @close={{closeDialog}} /></template>
|
|
);
|
|
await fillIn('#homeassistant-url', 'ha.example.com:8123');
|
|
await fillIn('#homeassistant-token', 'llat_secret');
|
|
await fillIn('#homeassistant-name', ' Home ');
|
|
await triggerEvent('form', 'submit');
|
|
|
|
assert.strictEqual(verified.length, 1);
|
|
assert.strictEqual(verified[0].url, 'https://ha.example.com:8123');
|
|
assert.strictEqual(verified[0].token, 'llat_secret');
|
|
|
|
assert.strictEqual(saved.length, 1);
|
|
assert.strictEqual(saved[0].namespace, 'integrations');
|
|
assert.strictEqual(saved[0].key, 'homeassistant-ha.example.com:8123');
|
|
assert.strictEqual(saved[0].value.type, 'homeassistant');
|
|
assert.strictEqual(saved[0].value.status, 'active');
|
|
assert.strictEqual(saved[0].value.url, 'https://ha.example.com:8123');
|
|
assert.strictEqual(saved[0].value.token, 'llat_secret');
|
|
assert.strictEqual(saved[0].value.name, 'Home');
|
|
|
|
assert.true(reloaded);
|
|
assert.true(polling);
|
|
assert.true(closed);
|
|
});
|
|
|
|
test('it omits the name when left blank', async function (assert) {
|
|
const saved = [];
|
|
|
|
registerServices(this.owner, {
|
|
homeAssistant: {
|
|
fetchSummary: async () => ({ unreadCount: 0 }),
|
|
},
|
|
storage: {
|
|
set: async (namespace, key, value) =>
|
|
saved.push({ namespace, key, value }),
|
|
},
|
|
});
|
|
|
|
const noop = () => {};
|
|
|
|
await render(<template><HomeAssistant @close={{noop}} /></template>);
|
|
await fillIn('#homeassistant-url', 'ha.example.com:8123');
|
|
await fillIn('#homeassistant-token', 'llat_secret');
|
|
await triggerEvent('form', 'submit');
|
|
|
|
assert.strictEqual(saved.length, 1);
|
|
assert.strictEqual(saved[0].value.name, undefined);
|
|
});
|
|
|
|
test('it rejects an invalid server URL', async function (assert) {
|
|
const verified = [];
|
|
|
|
registerServices(this.owner, {
|
|
homeAssistant: {
|
|
fetchSummary: async (...args) => verified.push(args),
|
|
},
|
|
});
|
|
|
|
const noop = () => {};
|
|
|
|
await render(<template><HomeAssistant @close={{noop}} /></template>);
|
|
await fillIn('#homeassistant-url', 'not a valid url');
|
|
await fillIn('#homeassistant-token', 'llat_secret');
|
|
await triggerEvent('form', 'submit');
|
|
|
|
assert.dom('#homeassistant-url').hasValue('not a valid url');
|
|
assert.dom().containsText('Please enter a valid Home Assistant URL');
|
|
assert.strictEqual(verified.length, 0);
|
|
});
|
|
|
|
test('it reports an invalid token without storing it', async function (assert) {
|
|
const saved = [];
|
|
let closed = false;
|
|
|
|
registerServices(this.owner, {
|
|
homeAssistant: {
|
|
fetchSummary: async () => {
|
|
throw new HomeAssistantAuthError();
|
|
},
|
|
},
|
|
storage: {
|
|
set: async (...args) => saved.push(args),
|
|
},
|
|
});
|
|
|
|
const closeDialog = () => {
|
|
closed = true;
|
|
};
|
|
|
|
await render(
|
|
<template><HomeAssistant @close={{closeDialog}} /></template>
|
|
);
|
|
await fillIn('#homeassistant-url', 'ha.example.com:8123');
|
|
await fillIn('#homeassistant-token', 'bad');
|
|
await triggerEvent('form', 'submit');
|
|
|
|
assert.dom().containsText('Invalid access token');
|
|
assert.strictEqual(saved.length, 0);
|
|
assert.false(closed);
|
|
});
|
|
|
|
test('it requires a server URL and token', async function (assert) {
|
|
registerServices(this.owner);
|
|
const noop = () => {};
|
|
|
|
await render(<template><HomeAssistant @close={{noop}} /></template>);
|
|
await triggerEvent('form', 'submit');
|
|
assert.dom().containsText('Please enter a valid Home Assistant URL');
|
|
|
|
await fillIn('#homeassistant-url', 'ha.example.com:8123');
|
|
await triggerEvent('form', 'submit');
|
|
assert.dom().containsText('Please enter a long-lived access token');
|
|
});
|
|
|
|
test('edit mode prefills the host and keeps the stored token and key', async function (assert) {
|
|
const verified = [];
|
|
const saved = [];
|
|
|
|
registerServices(this.owner, {
|
|
homeAssistant: {
|
|
fetchSummary: async (integration) => {
|
|
verified.push(integration);
|
|
return { unreadCount: 0 };
|
|
},
|
|
},
|
|
storage: {
|
|
set: async (namespace, key, value) =>
|
|
saved.push({ namespace, key, value }),
|
|
},
|
|
});
|
|
|
|
this.props = {
|
|
mode: 'edit',
|
|
integration: {
|
|
id: 'homeassistant-ha.example.com:8123',
|
|
url: 'https://ha.example.com:8123',
|
|
token: 'OLDT0KEN',
|
|
name: 'Home',
|
|
colorClass: 'bg-[#18BCF2]',
|
|
},
|
|
};
|
|
|
|
const noop = () => {};
|
|
|
|
await render(
|
|
<template>
|
|
<HomeAssistant @props={{this.props}} @close={{noop}} />
|
|
</template>
|
|
);
|
|
|
|
assert.dom().containsText('Edit Home Assistant');
|
|
assert.dom('#homeassistant-url').hasValue('https://ha.example.com:8123');
|
|
assert.dom('#homeassistant-name').hasValue('Home');
|
|
|
|
await triggerEvent('form', 'submit');
|
|
|
|
assert.strictEqual(verified.length, 1);
|
|
assert.strictEqual(verified[0].token, 'OLDT0KEN');
|
|
assert.strictEqual(saved.length, 1);
|
|
assert.strictEqual(saved[0].key, 'homeassistant-ha.example.com:8123');
|
|
assert.strictEqual(saved[0].value.url, 'https://ha.example.com:8123');
|
|
assert.strictEqual(saved[0].value.token, 'OLDT0KEN');
|
|
assert.strictEqual(saved[0].value.name, 'Home');
|
|
assert.strictEqual(saved[0].value.colorClass, undefined);
|
|
});
|
|
}
|
|
);
|