Files
2026-10-05 13:17:03 +02:00

258 lines
7.9 KiB
Plaintext

import { module, test } from 'qunit';
import { setupRenderingTest } from 'dashtab/tests/helpers';
import { render, fillIn, triggerEvent } from '@ember/test-helpers';
import Gitea from 'dashtab/components/settings/integrations/gitea-form';
function registerServices(owner, overrides = {}) {
const http = overrides.http ?? { request: async () => ({}) };
const storage = overrides.storage ?? { set: async () => {} };
const sites = overrides.sites ?? { reload: async () => {} };
const notifications = overrides.notifications ?? { startPolling: () => {} };
owner.register('service:http', http, { 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/gitea-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><Gitea @close={{noop}} /></template>);
assert.dom().containsText('Add Gitea Integration');
assert.dom('#gitea-url').exists().hasValue('');
assert.dom('#gitea-token').exists();
assert.dom('form').exists();
});
test('it links to the applications settings once a URL is entered', async function (assert) {
registerServices(this.owner);
const noop = () => {};
await render(<template><Gitea @close={{noop}} /></template>);
assert
.dom('a[href="https://git.example.com/user/settings/applications"]')
.doesNotExist();
await fillIn('#gitea-url', 'git.example.com');
assert
.dom('a[href="https://git.example.com/user/settings/applications"]')
.exists()
.hasAttribute('target', '_blank');
});
test('submitting validates and stores the credentials under a host key', async function (assert) {
const requests = [];
const saved = [];
let reloaded = false;
let polling = false;
let closed = false;
registerServices(this.owner, {
http: {
request: async (url, options) => {
requests.push({ url, options });
return {};
},
},
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><Gitea @close={{closeDialog}} /></template>);
await fillIn('#gitea-url', 'git.example.com');
await fillIn('#gitea-token', 'gta_secret');
await triggerEvent('form', 'submit');
assert.strictEqual(requests.length, 1);
assert.strictEqual(
requests[0].url,
'https://git.example.com/api/v1/notifications/new'
);
assert.strictEqual(
requests[0].options.headers['Authorization'],
'token gta_secret'
);
assert.strictEqual(
requests[0].options.headers['Accept'],
'application/json'
);
assert.strictEqual(saved.length, 1);
assert.strictEqual(saved[0].namespace, 'integrations');
assert.strictEqual(saved[0].key, 'gitea-git.example.com');
assert.strictEqual(saved[0].value.type, 'gitea');
assert.strictEqual(saved[0].value.status, 'active');
assert.strictEqual(saved[0].value.url, 'https://git.example.com');
assert.strictEqual(saved[0].value.token, 'gta_secret');
assert.true(reloaded);
assert.true(polling);
assert.true(closed);
});
test('it rejects an invalid server URL', async function (assert) {
const requests = [];
registerServices(this.owner, {
http: {
request: async (...args) => requests.push(args),
},
});
const noop = () => {};
await render(<template><Gitea @close={{noop}} /></template>);
await fillIn('#gitea-url', 'not a valid url');
await fillIn('#gitea-token', 'gta_secret');
await triggerEvent('form', 'submit');
assert.dom('#gitea-url').hasValue('not a valid url');
assert.dom().containsText('Please enter a valid Gitea URL');
assert.strictEqual(requests.length, 0);
});
test('it reports an invalid token without storing it', async function (assert) {
const saved = [];
let closed = false;
registerServices(this.owner, {
http: {
request: async () => {
throw { status: 401 };
},
},
storage: {
set: async (...args) => saved.push(args),
},
});
const closeDialog = () => {
closed = true;
};
await render(<template><Gitea @close={{closeDialog}} /></template>);
await fillIn('#gitea-url', 'git.example.com');
await fillIn('#gitea-token', 'bad');
await triggerEvent('form', 'submit');
assert.dom().containsText('Invalid access token');
assert.strictEqual(saved.length, 0);
assert.false(closed);
});
test('it reports a token missing the notification scope', async function (assert) {
registerServices(this.owner, {
http: {
request: async () => {
throw { status: 403 };
},
},
});
const noop = () => {};
await render(<template><Gitea @close={{noop}} /></template>);
await fillIn('#gitea-url', 'git.example.com');
await fillIn('#gitea-token', 'no-scope');
await triggerEvent('form', 'submit');
assert.dom().containsText('Token is missing the read:notification scope');
});
test('it requires a server URL and token', async function (assert) {
registerServices(this.owner);
const noop = () => {};
await render(<template><Gitea @close={{noop}} /></template>);
await triggerEvent('form', 'submit');
assert.dom().containsText('Please enter a valid Gitea URL');
await fillIn('#gitea-url', 'git.example.com');
await triggerEvent('form', 'submit');
assert.dom().containsText('Please enter an access token');
});
test('edit mode prefills the host and keeps the stored token and key', async function (assert) {
const requests = [];
const saved = [];
registerServices(this.owner, {
http: {
request: async (url, options) => {
requests.push({ url, options });
return {};
},
},
storage: {
set: async (namespace, key, value) =>
saved.push({ namespace, key, value }),
},
});
this.props = {
mode: 'edit',
integration: {
id: 'gitea-git.example.com',
url: 'https://git.example.com',
token: 'OLDT0KEN',
colorClass: 'bg-[#609926]',
},
};
const noop = () => {};
await render(
<template><Gitea @props={{this.props}} @close={{noop}} /></template>
);
assert.dom().containsText('Edit Gitea');
assert.dom('#gitea-url').hasValue('https://git.example.com');
await triggerEvent('form', 'submit');
assert.strictEqual(requests.length, 1);
assert.strictEqual(
requests[0].url,
'https://git.example.com/api/v1/notifications/new'
);
assert.strictEqual(
requests[0].options.headers['Authorization'],
'token OLDT0KEN'
);
assert.strictEqual(saved.length, 1);
assert.strictEqual(saved[0].key, 'gitea-git.example.com');
assert.strictEqual(saved[0].value.url, 'https://git.example.com');
assert.strictEqual(saved[0].value.token, 'OLDT0KEN');
assert.strictEqual(saved[0].value.colorClass, undefined);
});
}
);