Users can add any website as a dashboard quick link from Settings → Site Links. The link is stored in the integrations namespace as a `custom` type so it flows through the existing sites/integrations/settings pipeline, and appears in a new "Links" group. Its icon is discovered with the existing favicon pipeline; an uploaded icon overrides it and stays pinned until the user chooses "Use site's icon" again. resolveIcons and refreshIcon skip pinned sites (and re-check the live list before writing back) so an in-flight discovery can never clobber a freshly uploaded icon. URLs are normalized to http(s) only, with embedded credentials stripped, so a value can never reach an anchor's href as javascript:/data:. Uploads are validated, size-capped and downscaled to 192px (SVGs kept as-is), and only ever rendered through an <img>. Adds app/utils/site-url.js and app/utils/image-file.js, a custom-link form, a `custom` registry entry/group and adapter, and tests.
519 lines
17 KiB
JavaScript
519 lines
17 KiB
JavaScript
import { module, test } from 'qunit';
|
|
import { setupTest } from 'dashtab/tests/helpers';
|
|
import { DISCOURSE_ICON_URL } from 'dashtab/config/integrations';
|
|
|
|
module('Unit | Service | sites', function (hooks) {
|
|
setupTest(hooks);
|
|
|
|
hooks.beforeEach(async function () {
|
|
const storage = this.owner.lookup('service:storage');
|
|
await storage.clear('integrations');
|
|
|
|
const siteIcons = this.owner.lookup('service:site-icons');
|
|
siteIcons.iconFor = async () => undefined;
|
|
});
|
|
|
|
test('it exists', function (assert) {
|
|
let service = this.owner.lookup('service:sites');
|
|
assert.ok(service);
|
|
});
|
|
|
|
test('load groups active supported integrations', async function (assert) {
|
|
const storage = this.owner.lookup('service:storage');
|
|
await storage.set('integrations', 'discourse-abc', {
|
|
type: 'discourse',
|
|
status: 'active',
|
|
url: 'https://forum.example.com',
|
|
apiKey: 'key',
|
|
clientId: 'abc',
|
|
});
|
|
await storage.set('integrations', 'bitcoin', {
|
|
type: 'bitcoin',
|
|
status: 'active',
|
|
});
|
|
await storage.set('integrations', 'discourse-pending', {
|
|
type: 'discourse',
|
|
status: 'pending',
|
|
url: 'https://pending.example.com',
|
|
});
|
|
|
|
const service = this.owner.lookup('service:sites');
|
|
await service.ready;
|
|
|
|
assert.strictEqual(service.all.length, 1);
|
|
|
|
const groups = service.groups;
|
|
assert.strictEqual(groups.length, 1);
|
|
assert.strictEqual(groups[0].id, 'discourse');
|
|
assert.strictEqual(groups[0].label, 'Discourse');
|
|
assert.strictEqual(groups[0].items.length, 1);
|
|
|
|
const [item] = groups[0].items;
|
|
assert.strictEqual(item.label, 'forum.example.com');
|
|
assert.strictEqual(item.href, 'https://forum.example.com');
|
|
assert.strictEqual(item.groupId, 'discourse');
|
|
});
|
|
|
|
test('a Discourse site falls back to the bundled official icon', async function (assert) {
|
|
const storage = this.owner.lookup('service:storage');
|
|
await storage.set('integrations', 'discourse-abc', {
|
|
type: 'discourse',
|
|
status: 'active',
|
|
url: 'https://forum.example.com',
|
|
apiKey: 'key',
|
|
});
|
|
|
|
const service = this.owner.lookup('service:sites');
|
|
await service.ready;
|
|
|
|
const [item] = service.all;
|
|
assert.strictEqual(item.icon.dataUrl, DISCOURSE_ICON_URL);
|
|
assert.true(item.icon.maskable);
|
|
});
|
|
|
|
test('load builds the Feedbin site with a bundled maskable icon', async function (assert) {
|
|
const storage = this.owner.lookup('service:storage');
|
|
await storage.set('integrations', 'feedbin', {
|
|
type: 'feedbin',
|
|
status: 'active',
|
|
email: 'me@example.com',
|
|
token: 'dG9rZW4=',
|
|
});
|
|
|
|
const service = this.owner.lookup('service:sites');
|
|
await service.ready;
|
|
|
|
assert.strictEqual(service.all.length, 1);
|
|
|
|
const [item] = service.all;
|
|
assert.strictEqual(item.label, 'Feedbin');
|
|
assert.strictEqual(item.href, 'https://feedbin.com');
|
|
assert.strictEqual(item.groupId, 'feedbin');
|
|
assert.notOk(item.url);
|
|
assert.deepEqual(item.icon, {
|
|
dataUrl: '/img/icons/feedbin.png',
|
|
maskable: true,
|
|
});
|
|
|
|
const groups = service.groups;
|
|
assert.strictEqual(groups.length, 1);
|
|
assert.strictEqual(groups[0].id, 'feedbin');
|
|
assert.strictEqual(groups[0].label, 'Feedbin');
|
|
});
|
|
|
|
test('the Feedbin site links to a self-hosted URL when configured', async function (assert) {
|
|
const storage = this.owner.lookup('service:storage');
|
|
await storage.set('integrations', 'feedbin', {
|
|
type: 'feedbin',
|
|
status: 'active',
|
|
url: 'https://feedbin.example.com',
|
|
email: 'me@example.com',
|
|
token: 'dG9rZW4=',
|
|
});
|
|
|
|
const service = this.owner.lookup('service:sites');
|
|
await service.ready;
|
|
|
|
assert.strictEqual(service.all[0].href, 'https://feedbin.example.com');
|
|
});
|
|
|
|
test('load builds the GitHub site with the bundled maskable icon', async function (assert) {
|
|
const storage = this.owner.lookup('service:storage');
|
|
await storage.set('integrations', 'github', {
|
|
type: 'github',
|
|
status: 'active',
|
|
url: 'https://github.com',
|
|
token: 'ghp_token',
|
|
});
|
|
|
|
const service = this.owner.lookup('service:sites');
|
|
await service.ready;
|
|
|
|
assert.strictEqual(service.all.length, 1);
|
|
|
|
const [item] = service.all;
|
|
assert.strictEqual(item.label, 'GitHub');
|
|
assert.strictEqual(item.href, 'https://github.com/notifications');
|
|
assert.strictEqual(item.groupId, 'code-forge');
|
|
assert.notOk(item.url);
|
|
assert.deepEqual(item.icon, {
|
|
dataUrl: '/img/icons/github-inverted.png',
|
|
maskable: true,
|
|
themeColor: '#1f2937',
|
|
});
|
|
});
|
|
|
|
test('the GitHub site uses an Enterprise host and links to its notifications', async function (assert) {
|
|
const storage = this.owner.lookup('service:storage');
|
|
await storage.set('integrations', 'github', {
|
|
type: 'github',
|
|
status: 'active',
|
|
url: 'https://git.example.com',
|
|
token: 'ghp_token',
|
|
});
|
|
|
|
const service = this.owner.lookup('service:sites');
|
|
await service.ready;
|
|
|
|
const [item] = service.all;
|
|
assert.strictEqual(item.label, 'git.example.com');
|
|
assert.strictEqual(item.href, 'https://git.example.com/notifications');
|
|
assert.strictEqual(item.url, 'https://git.example.com');
|
|
assert.notOk(item.icon);
|
|
});
|
|
|
|
test('load builds a Gitea site keyed by host with a notifications link', async function (assert) {
|
|
const storage = this.owner.lookup('service:storage');
|
|
await storage.set('integrations', 'gitea-git.example.com', {
|
|
type: 'gitea',
|
|
status: 'active',
|
|
url: 'https://git.example.com',
|
|
token: 'gta_token',
|
|
});
|
|
|
|
const service = this.owner.lookup('service:sites');
|
|
await service.ready;
|
|
|
|
assert.strictEqual(service.all.length, 1);
|
|
|
|
const [item] = service.all;
|
|
assert.strictEqual(item.id, 'gitea-git.example.com');
|
|
assert.strictEqual(item.label, 'git.example.com');
|
|
assert.strictEqual(item.groupId, 'code-forge');
|
|
assert.strictEqual(item.href, 'https://git.example.com');
|
|
assert.strictEqual(
|
|
item.notificationsHref,
|
|
'https://git.example.com/notifications'
|
|
);
|
|
assert.notOk(item.url);
|
|
assert.deepEqual(item.icon, {
|
|
dataUrl: '/img/icons/gitea.png',
|
|
maskable: true,
|
|
themeColor: '#ffffff',
|
|
});
|
|
});
|
|
|
|
test('load supports multiple Gitea instances', async function (assert) {
|
|
const storage = this.owner.lookup('service:storage');
|
|
await storage.set('integrations', 'gitea-a.example.com', {
|
|
type: 'gitea',
|
|
status: 'active',
|
|
url: 'https://a.example.com',
|
|
token: 'a',
|
|
});
|
|
await storage.set('integrations', 'gitea-b.example.com', {
|
|
type: 'gitea',
|
|
status: 'active',
|
|
url: 'https://b.example.com',
|
|
token: 'b',
|
|
});
|
|
|
|
const service = this.owner.lookup('service:sites');
|
|
await service.ready;
|
|
|
|
assert.strictEqual(service.all.length, 2);
|
|
assert.deepEqual(
|
|
service.all.map((site) => site.id),
|
|
['gitea-a.example.com', 'gitea-b.example.com']
|
|
);
|
|
});
|
|
|
|
test('load builds a Home Assistant site keyed by host and links to the root', async function (assert) {
|
|
const storage = this.owner.lookup('service:storage');
|
|
await storage.set('integrations', 'homeassistant-ha.example.com:8123', {
|
|
type: 'homeassistant',
|
|
status: 'active',
|
|
url: 'https://ha.example.com:8123',
|
|
token: 'llat_token',
|
|
});
|
|
|
|
const service = this.owner.lookup('service:sites');
|
|
await service.ready;
|
|
|
|
assert.strictEqual(service.all.length, 1);
|
|
|
|
const [item] = service.all;
|
|
assert.strictEqual(item.id, 'homeassistant-ha.example.com:8123');
|
|
assert.strictEqual(item.label, 'ha.example.com');
|
|
assert.strictEqual(item.groupId, 'home-assistant');
|
|
assert.strictEqual(item.href, 'https://ha.example.com:8123/');
|
|
assert.notOk(item.url);
|
|
assert.deepEqual(item.icon, {
|
|
dataUrl: '/img/icons/home-assistant.png',
|
|
maskable: true,
|
|
themeColor: '#18bcf2',
|
|
});
|
|
|
|
const groups = service.groups;
|
|
assert.strictEqual(groups.length, 1);
|
|
assert.strictEqual(groups[0].id, 'home-assistant');
|
|
assert.strictEqual(groups[0].label, 'Home Assistant');
|
|
});
|
|
|
|
test('a Home Assistant custom name overrides the host label', async function (assert) {
|
|
const storage = this.owner.lookup('service:storage');
|
|
await storage.set('integrations', 'homeassistant-ha.example.com:8123', {
|
|
type: 'homeassistant',
|
|
status: 'active',
|
|
url: 'https://ha.example.com:8123',
|
|
token: 'llat_token',
|
|
name: ' Home ',
|
|
});
|
|
|
|
const service = this.owner.lookup('service:sites');
|
|
await service.ready;
|
|
|
|
const [item] = service.all;
|
|
assert.strictEqual(item.label, 'Home');
|
|
assert.strictEqual(item.href, 'https://ha.example.com:8123/');
|
|
});
|
|
|
|
test('integrationFor exposes the raw record for a site', async function (assert) {
|
|
const storage = this.owner.lookup('service:storage');
|
|
await storage.set('integrations', 'discourse-abc', {
|
|
type: 'discourse',
|
|
status: 'active',
|
|
url: 'https://forum.example.com',
|
|
apiKey: 'key',
|
|
});
|
|
|
|
const service = this.owner.lookup('service:sites');
|
|
await service.ready;
|
|
|
|
const integration = service.integrationFor('discourse-abc');
|
|
assert.strictEqual(integration.apiKey, 'key');
|
|
assert.strictEqual(service.integrationFor('missing'), undefined);
|
|
});
|
|
|
|
test('resolveIcons attaches a fetched icon to the site', async function (assert) {
|
|
const storage = this.owner.lookup('service:storage');
|
|
await storage.set('integrations', 'discourse-abc', {
|
|
type: 'discourse',
|
|
status: 'active',
|
|
url: 'https://forum.example.com',
|
|
apiKey: 'key',
|
|
clientId: 'abc',
|
|
});
|
|
|
|
const icon = { dataUrl: 'data:image/png;base64,x', maskable: true };
|
|
const siteIcons = this.owner.lookup('service:site-icons');
|
|
siteIcons.iconFor = async () => icon;
|
|
|
|
const service = this.owner.lookup('service:sites');
|
|
await service.ready;
|
|
await service.resolveIcons();
|
|
|
|
assert.deepEqual(service.all[0].icon, icon);
|
|
});
|
|
|
|
test('load builds a custom link with a hostname label and a discovery URL', async function (assert) {
|
|
const storage = this.owner.lookup('service:storage');
|
|
await storage.set('integrations', 'custom-1', {
|
|
type: 'custom',
|
|
status: 'active',
|
|
url: 'https://news.example.com/top',
|
|
});
|
|
|
|
const service = this.owner.lookup('service:sites');
|
|
await service.ready;
|
|
|
|
assert.strictEqual(service.all.length, 1);
|
|
|
|
const [item] = service.all;
|
|
assert.strictEqual(item.type, 'custom');
|
|
assert.strictEqual(item.groupId, 'links');
|
|
assert.strictEqual(item.label, 'news.example.com');
|
|
assert.strictEqual(item.href, 'https://news.example.com/top');
|
|
assert.strictEqual(item.url, 'https://news.example.com/top');
|
|
assert.notOk(item.icon);
|
|
assert.notOk(item.customIcon);
|
|
|
|
const groups = service.groups;
|
|
assert.strictEqual(groups.length, 1);
|
|
assert.strictEqual(groups[0].id, 'links');
|
|
assert.strictEqual(groups[0].label, 'Links');
|
|
});
|
|
|
|
test('a custom link name overrides the hostname label', async function (assert) {
|
|
const storage = this.owner.lookup('service:storage');
|
|
await storage.set('integrations', 'custom-1', {
|
|
type: 'custom',
|
|
status: 'active',
|
|
url: 'https://news.example.com',
|
|
name: ' Hacker News ',
|
|
});
|
|
|
|
const service = this.owner.lookup('service:sites');
|
|
await service.ready;
|
|
|
|
assert.strictEqual(service.all[0].label, 'Hacker News');
|
|
});
|
|
|
|
test('resolveIcons discovers an icon for a custom link without a pinned icon', async function (assert) {
|
|
const storage = this.owner.lookup('service:storage');
|
|
await storage.set('integrations', 'custom-1', {
|
|
type: 'custom',
|
|
status: 'active',
|
|
url: 'https://news.example.com',
|
|
});
|
|
|
|
const fetched = {
|
|
dataUrl: 'data:image/png;base64,fetched',
|
|
maskable: true,
|
|
};
|
|
const siteIcons = this.owner.lookup('service:site-icons');
|
|
siteIcons.iconFor = async () => fetched;
|
|
|
|
const service = this.owner.lookup('service:sites');
|
|
await service.ready;
|
|
await service.resolveIcons();
|
|
|
|
assert.deepEqual(service.all[0].icon, fetched);
|
|
});
|
|
|
|
test('resolveIcons leaves a pinned custom icon alone', async function (assert) {
|
|
const storage = this.owner.lookup('service:storage');
|
|
const customIcon = { dataUrl: 'data:image/png;base64,up', maskable: false };
|
|
await storage.set('integrations', 'custom-1', {
|
|
type: 'custom',
|
|
status: 'active',
|
|
url: 'https://news.example.com',
|
|
customIcon,
|
|
});
|
|
|
|
const siteIcons = this.owner.lookup('service:site-icons');
|
|
let calls = 0;
|
|
siteIcons.iconFor = async () => {
|
|
calls++;
|
|
return { dataUrl: 'data:image/png;base64,fetched', maskable: true };
|
|
};
|
|
|
|
const service = this.owner.lookup('service:sites');
|
|
await service.ready;
|
|
await service.resolveIcons();
|
|
|
|
assert.strictEqual(calls, 0);
|
|
assert.deepEqual(service.all[0].icon, customIcon);
|
|
assert.deepEqual(service.all[0].customIcon, customIcon);
|
|
});
|
|
|
|
test('resolveIcons does not clobber a custom icon pinned while discovery was in flight', async function (assert) {
|
|
const storage = this.owner.lookup('service:storage');
|
|
await storage.set('integrations', 'custom-1', {
|
|
type: 'custom',
|
|
status: 'active',
|
|
url: 'https://news.example.com',
|
|
});
|
|
|
|
let resolveDiscovery;
|
|
const siteIcons = this.owner.lookup('service:site-icons');
|
|
siteIcons.iconFor = () =>
|
|
new Promise((resolve) => {
|
|
resolveDiscovery = resolve;
|
|
});
|
|
|
|
const service = this.owner.lookup('service:sites');
|
|
await service.ready; // discovery is now pending on the controlled promise
|
|
|
|
const pinned = { dataUrl: 'data:image/png;base64,pinned', maskable: false };
|
|
service.updateSite('custom-1', { icon: pinned, customIcon: pinned });
|
|
|
|
resolveDiscovery({
|
|
dataUrl: 'data:image/png;base64,fetched',
|
|
maskable: true,
|
|
});
|
|
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
|
|
assert.deepEqual(service.all[0].icon, pinned);
|
|
});
|
|
|
|
test('reload picks up newly added integrations', async function (assert) {
|
|
const storage = this.owner.lookup('service:storage');
|
|
const service = this.owner.lookup('service:sites');
|
|
await service.ready;
|
|
assert.strictEqual(service.all.length, 0);
|
|
|
|
await storage.set('integrations', 'discourse-new', {
|
|
type: 'discourse',
|
|
status: 'active',
|
|
url: 'https://new.example.com',
|
|
apiKey: 'key',
|
|
});
|
|
await service.reload();
|
|
|
|
assert.strictEqual(service.all.length, 1);
|
|
assert.strictEqual(service.all[0].id, 'discourse-new');
|
|
});
|
|
|
|
test('refreshIcon replaces the site icon with a freshly resolved record', async function (assert) {
|
|
const storage = this.owner.lookup('service:storage');
|
|
await storage.set('integrations', 'discourse-abc', {
|
|
type: 'discourse',
|
|
status: 'active',
|
|
url: 'https://forum.example.com',
|
|
apiKey: 'key',
|
|
});
|
|
|
|
const fresh = { dataUrl: 'data:image/png;base64,new', maskable: true };
|
|
const siteIcons = this.owner.lookup('service:site-icons');
|
|
let requested;
|
|
siteIcons.refresh = async (url) => {
|
|
requested = url;
|
|
return fresh;
|
|
};
|
|
|
|
const service = this.owner.lookup('service:sites');
|
|
await service.ready;
|
|
|
|
await service.refreshIcon('https://forum.example.com');
|
|
|
|
assert.strictEqual(requested, 'https://forum.example.com');
|
|
assert.deepEqual(service.all[0].icon, fresh);
|
|
});
|
|
|
|
test('refreshIcon ignores a URL with no matching site', async function (assert) {
|
|
const storage = this.owner.lookup('service:storage');
|
|
await storage.set('integrations', 'discourse-abc', {
|
|
type: 'discourse',
|
|
status: 'active',
|
|
url: 'https://forum.example.com',
|
|
});
|
|
|
|
const siteIcons = this.owner.lookup('service:site-icons');
|
|
siteIcons.refresh = async () => ({
|
|
dataUrl: 'data:image/png;base64,new',
|
|
maskable: true,
|
|
});
|
|
|
|
const service = this.owner.lookup('service:sites');
|
|
await service.ready;
|
|
|
|
await service.refreshIcon('https://other.example.com');
|
|
|
|
// The bundled fallback is left untouched when nothing matches.
|
|
assert.strictEqual(service.all[0].icon.dataUrl, DISCOURSE_ICON_URL);
|
|
});
|
|
|
|
test('removeSource deletes the integration and reloads', async function (assert) {
|
|
const storage = this.owner.lookup('service:storage');
|
|
await storage.set('integrations', 'discourse-abc', {
|
|
type: 'discourse',
|
|
status: 'active',
|
|
url: 'https://forum.example.com',
|
|
apiKey: 'key',
|
|
});
|
|
|
|
const service = this.owner.lookup('service:sites');
|
|
await service.ready;
|
|
assert.strictEqual(service.all.length, 1);
|
|
|
|
await service.removeSource('discourse-abc');
|
|
|
|
assert.strictEqual(service.all.length, 0);
|
|
assert.strictEqual(
|
|
await storage.get('integrations', 'discourse-abc'),
|
|
null
|
|
);
|
|
});
|
|
});
|