Files

269 lines
8.0 KiB
Plaintext

import { module, test } from 'qunit';
import { setupRenderingTest } from 'dashtab/tests/helpers';
import { render, fillIn, triggerEvent } from '@ember/test-helpers';
import Github from 'dashtab/components/settings/integrations/github-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/github-form',
function (hooks) {
setupRenderingTest(hooks);
test('it renders the token form', async function (assert) {
registerServices(this.owner);
const noop = () => {};
await render(<template><Github @close={{noop}} /></template>);
assert.dom().containsText('Add GitHub Integration');
assert.dom('#github-url').exists().hasValue('https://github.com');
assert.dom('#github-token').exists();
assert.dom().containsText('classic personal access token');
assert
.dom('a[href="https://github.com/settings/tokens/new"]')
.exists()
.hasAttribute('target', '_blank');
assert.dom('form').exists();
});
test('submitting validates and stores the credentials', 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><Github @close={{closeDialog}} /></template>);
await fillIn('#github-token', 'ghp_secret');
await triggerEvent('form', 'submit');
assert.strictEqual(requests.length, 1);
assert.strictEqual(
requests[0].url,
'https://api.github.com/notifications?per_page=50'
);
assert.strictEqual(
requests[0].options.headers['Authorization'],
'Bearer ghp_secret'
);
assert.strictEqual(
requests[0].options.headers['Accept'],
'application/vnd.github+json'
);
assert.strictEqual(saved.length, 1);
assert.strictEqual(saved[0].namespace, 'integrations');
assert.strictEqual(saved[0].key, 'github');
assert.strictEqual(saved[0].value.type, 'github');
assert.strictEqual(saved[0].value.status, 'active');
assert.strictEqual(saved[0].value.url, 'https://github.com');
assert.strictEqual(saved[0].value.token, 'ghp_secret');
assert.true(reloaded);
assert.true(polling);
assert.true(closed);
});
test('submitting an Enterprise URL derives the /api/v3 base', 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(value),
},
});
const noop = () => {};
await render(<template><Github @close={{noop}} /></template>);
await fillIn('#github-url', 'git.example.com');
await fillIn('#github-token', 'ghp_secret');
await triggerEvent('form', 'submit');
assert.strictEqual(
requests[0].url,
'https://git.example.com/api/v3/notifications?per_page=50'
);
assert.strictEqual(saved[0].url, 'https://git.example.com');
});
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><Github @close={{noop}} /></template>);
await fillIn('#github-url', 'not a valid url');
await fillIn('#github-token', 'ghp_secret');
await triggerEvent('form', 'submit');
assert.dom('#github-url').hasValue('not a valid url');
assert.dom().containsText('Please enter a valid GitHub 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><Github @close={{closeDialog}} /></template>);
await fillIn('#github-token', 'bad');
await triggerEvent('form', 'submit');
assert.dom().containsText('Invalid personal access token');
assert.strictEqual(saved.length, 0);
assert.false(closed);
});
test('it reports a token missing the notifications scope', async function (assert) {
registerServices(this.owner, {
http: {
request: async () => {
throw { status: 403 };
},
},
});
const noop = () => {};
await render(<template><Github @close={{noop}} /></template>);
await fillIn('#github-token', 'no-scope');
await triggerEvent('form', 'submit');
assert
.dom()
.containsText('Use a classic token with the notifications scope');
});
test('it requires a token', async function (assert) {
registerServices(this.owner);
const noop = () => {};
await render(<template><Github @close={{noop}} /></template>);
await triggerEvent('form', 'submit');
assert.dom().containsText('Please enter a personal access token');
});
test('edit mode prefills the host and keeps the stored token when blank', 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: {
url: 'https://git.example.com',
token: 'OLDT0KEN',
colorClass: 'bg-gray-900',
},
};
const noop = () => {};
await render(
<template><Github @props={{this.props}} @close={{noop}} /></template>
);
assert.dom().containsText('Edit GitHub');
assert.dom('#github-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/v3/notifications?per_page=50'
);
assert.strictEqual(
requests[0].options.headers['Authorization'],
'Bearer OLDT0KEN'
);
assert.strictEqual(saved.length, 1);
assert.strictEqual(saved[0].value.url, 'https://git.example.com');
assert.strictEqual(saved[0].value.token, 'OLDT0KEN');
assert.strictEqual(saved[0].value.colorClass, undefined);
});
}
);