2017-05-18 13:48:37 +03:00
|
|
|
import ctrlOrCmd from 'ghost-admin/utils/ctrl-or-cmd';
|
2022-03-08 14:32:01 +03:00
|
|
|
import {Response} from 'miragejs';
|
2019-01-02 12:58:55 +03:00
|
|
|
import {authenticateSession, invalidateSession} from 'ember-simple-auth/test-support';
|
|
|
|
import {beforeEach, describe, it} from 'mocha';
|
|
|
|
import {blur, click, currentURL, fillIn, find, findAll, triggerEvent} from '@ember/test-helpers';
|
2017-05-29 21:50:03 +03:00
|
|
|
import {expect} from 'chai';
|
2019-01-02 12:58:55 +03:00
|
|
|
import {setupApplicationTest} from 'ember-mocha';
|
2019-05-27 11:37:05 +03:00
|
|
|
import {setupMirage} from 'ember-cli-mirage/test-support';
|
2019-01-02 12:58:55 +03:00
|
|
|
import {visit} from '../../helpers/visit';
|
2016-03-29 11:40:44 +03:00
|
|
|
|
2018-10-04 13:02:36 +03:00
|
|
|
describe('Acceptance: Settings - Integrations - Slack', function () {
|
2019-01-02 12:58:55 +03:00
|
|
|
let hooks = setupApplicationTest();
|
|
|
|
setupMirage(hooks);
|
2016-03-29 11:40:44 +03:00
|
|
|
|
2017-04-24 15:29:48 +03:00
|
|
|
it('redirects to signin when not authenticated', async function () {
|
2019-01-02 12:58:55 +03:00
|
|
|
await invalidateSession();
|
2021-11-05 15:42:38 +03:00
|
|
|
await visit('/settings/integrations/slack');
|
2016-03-29 11:40:44 +03:00
|
|
|
|
2017-04-24 15:29:48 +03:00
|
|
|
expect(currentURL(), 'currentURL').to.equal('/signin');
|
2016-03-29 11:40:44 +03:00
|
|
|
});
|
|
|
|
|
2022-01-17 13:05:27 +03:00
|
|
|
it('redirects to home page when authenticated as contributor', async function () {
|
2019-01-02 12:58:55 +03:00
|
|
|
let role = this.server.create('role', {name: 'Contributor'});
|
|
|
|
this.server.create('user', {roles: [role], slug: 'test-user'});
|
2018-02-07 12:42:46 +03:00
|
|
|
|
2019-01-02 12:58:55 +03:00
|
|
|
await authenticateSession();
|
2021-11-05 15:42:38 +03:00
|
|
|
await visit('/settings/integrations/slack');
|
2018-02-07 12:42:46 +03:00
|
|
|
|
2022-02-02 12:57:34 +03:00
|
|
|
expect(currentURL(), 'currentURL').to.equal('/posts');
|
2018-02-07 12:42:46 +03:00
|
|
|
});
|
|
|
|
|
2022-01-17 13:05:27 +03:00
|
|
|
it('redirects to home page when authenticated as author', async function () {
|
2019-01-02 12:58:55 +03:00
|
|
|
let role = this.server.create('role', {name: 'Author'});
|
|
|
|
this.server.create('user', {roles: [role], slug: 'test-user'});
|
2016-03-29 11:40:44 +03:00
|
|
|
|
2019-01-02 12:58:55 +03:00
|
|
|
await authenticateSession();
|
2021-11-05 15:42:38 +03:00
|
|
|
await visit('/settings/integrations/slack');
|
2016-03-29 11:40:44 +03:00
|
|
|
|
2022-01-17 13:05:27 +03:00
|
|
|
expect(currentURL(), 'currentURL').to.equal('/site');
|
2016-03-29 11:40:44 +03:00
|
|
|
});
|
|
|
|
|
2022-01-17 13:05:27 +03:00
|
|
|
it('redirects to home page when authenticated as editor', async function () {
|
2019-01-02 12:58:55 +03:00
|
|
|
let role = this.server.create('role', {name: 'Editor'});
|
|
|
|
this.server.create('user', {roles: [role], slug: 'test-user'});
|
2016-03-29 11:40:44 +03:00
|
|
|
|
2019-01-02 12:58:55 +03:00
|
|
|
await authenticateSession();
|
2021-11-05 15:42:38 +03:00
|
|
|
await visit('/settings/integrations/slack');
|
2016-03-29 11:40:44 +03:00
|
|
|
|
2022-01-17 13:05:27 +03:00
|
|
|
expect(currentURL(), 'currentURL').to.equal('/site');
|
2016-03-29 11:40:44 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('when logged in', function () {
|
2019-01-02 12:58:55 +03:00
|
|
|
beforeEach(async function () {
|
|
|
|
let role = this.server.create('role', {name: 'Administrator'});
|
|
|
|
this.server.create('user', {roles: [role]});
|
2016-03-29 11:40:44 +03:00
|
|
|
|
2019-01-02 12:58:55 +03:00
|
|
|
return await authenticateSession();
|
2016-03-29 11:40:44 +03:00
|
|
|
});
|
|
|
|
|
2019-01-21 15:26:21 +03:00
|
|
|
it('it validates and saves slack settings properly', async function () {
|
2021-11-05 15:42:38 +03:00
|
|
|
await visit('/settings/integrations/slack');
|
2016-03-29 11:40:44 +03:00
|
|
|
|
2017-04-24 15:29:48 +03:00
|
|
|
// has correct url
|
2021-11-05 15:42:38 +03:00
|
|
|
expect(currentURL(), 'currentURL').to.equal('/settings/integrations/slack');
|
2016-03-29 11:40:44 +03:00
|
|
|
|
2017-10-31 18:27:25 +03:00
|
|
|
await fillIn('[data-test-slack-url-input]', 'notacorrecturl');
|
2017-08-11 18:28:05 +03:00
|
|
|
await click('[data-test-save-button]');
|
2016-03-29 11:40:44 +03:00
|
|
|
|
2019-01-22 16:53:34 +03:00
|
|
|
expect(find('[data-test-error="slack-url"]').textContent.trim(), 'inline validation response')
|
2017-04-24 15:29:48 +03:00
|
|
|
.to.equal('The URL must be in a format like https://hooks.slack.com/services/<your personal key>');
|
2016-03-29 11:40:44 +03:00
|
|
|
|
2017-05-18 13:48:37 +03:00
|
|
|
// CMD-S shortcut works
|
2017-08-11 18:28:05 +03:00
|
|
|
await fillIn('[data-test-slack-url-input]', 'https://hooks.slack.com/services/1275958430');
|
2019-01-21 15:26:21 +03:00
|
|
|
await fillIn('[data-test-slack-username-input]', 'SlackBot');
|
2017-05-18 13:48:37 +03:00
|
|
|
await triggerEvent('.gh-app', 'keydown', {
|
|
|
|
keyCode: 83, // s
|
|
|
|
metaKey: ctrlOrCmd === 'command',
|
|
|
|
ctrlKey: ctrlOrCmd === 'ctrl'
|
|
|
|
});
|
|
|
|
|
2019-01-02 12:58:55 +03:00
|
|
|
let [newRequest] = this.server.pretender.handledRequests.slice(-1);
|
2017-05-18 13:48:37 +03:00
|
|
|
let params = JSON.parse(newRequest.requestBody);
|
|
|
|
|
2022-05-16 13:38:32 +03:00
|
|
|
let urlResult = params.settings.findBy('key', 'slack_url').value;
|
|
|
|
let usernameResult = params.settings.findBy('key', 'slack_username').value;
|
|
|
|
|
|
|
|
expect(urlResult).to.equal('https://hooks.slack.com/services/1275958430');
|
|
|
|
expect(usernameResult).to.equal('SlackBot');
|
2019-01-22 16:53:34 +03:00
|
|
|
expect(find('[data-test-error="slack-url"]'), 'inline validation response')
|
2019-01-02 12:58:55 +03:00
|
|
|
.to.not.exist;
|
2017-05-18 13:48:37 +03:00
|
|
|
|
2017-10-31 18:27:25 +03:00
|
|
|
await fillIn('[data-test-slack-url-input]', 'https://hooks.slack.com/services/1275958430');
|
2017-08-11 18:28:05 +03:00
|
|
|
await click('[data-test-send-notification-button]');
|
2016-03-29 11:40:44 +03:00
|
|
|
|
2019-01-02 12:58:55 +03:00
|
|
|
expect(findAll('.gh-notification').length, 'number of notifications').to.equal(1);
|
2019-01-22 16:53:34 +03:00
|
|
|
expect(find('[data-test-error="slack-url"]'), 'inline validation response')
|
2019-01-02 12:58:55 +03:00
|
|
|
.to.not.exist;
|
2016-03-29 11:40:44 +03:00
|
|
|
|
2019-01-02 12:58:55 +03:00
|
|
|
this.server.put('/settings/', function () {
|
2022-03-08 14:32:01 +03:00
|
|
|
return new Response(422, {}, {
|
2017-04-24 15:29:48 +03:00
|
|
|
errors: [
|
|
|
|
{
|
2019-03-25 14:29:14 +03:00
|
|
|
type: 'ValidationError',
|
2017-04-24 15:29:48 +03:00
|
|
|
message: 'Test error'
|
|
|
|
}
|
|
|
|
]
|
2016-03-29 11:40:44 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-07-31 12:25:09 +03:00
|
|
|
await click('.gh-notification .gh-notification-close');
|
2017-08-11 18:28:05 +03:00
|
|
|
await click('[data-test-send-notification-button]');
|
2016-03-29 11:40:44 +03:00
|
|
|
|
2016-06-30 17:45:02 +03:00
|
|
|
// we shouldn't try to send the test request if the save fails
|
2019-01-02 12:58:55 +03:00
|
|
|
let [lastRequest] = this.server.pretender.handledRequests.slice(-1);
|
2017-04-24 15:29:48 +03:00
|
|
|
expect(lastRequest.url).to.not.match(/\/slack\/test/);
|
2019-01-02 12:58:55 +03:00
|
|
|
expect(findAll('.gh-notification').length, 'check slack notification after api validation error').to.equal(0);
|
2016-03-29 11:40:44 +03:00
|
|
|
});
|
2017-10-31 18:27:25 +03:00
|
|
|
|
|
|
|
it('warns when leaving without saving', async function () {
|
2021-11-05 15:42:38 +03:00
|
|
|
await visit('/settings/integrations/slack');
|
2017-10-31 18:27:25 +03:00
|
|
|
|
|
|
|
// has correct url
|
2021-11-05 15:42:38 +03:00
|
|
|
expect(currentURL(), 'currentURL').to.equal('/settings/integrations/slack');
|
2017-10-31 18:27:25 +03:00
|
|
|
|
|
|
|
await fillIn('[data-test-slack-url-input]', 'https://hooks.slack.com/services/1275958430');
|
2019-01-02 12:58:55 +03:00
|
|
|
await blur('[data-test-slack-url-input]');
|
2017-10-31 18:27:25 +03:00
|
|
|
|
2021-10-04 18:46:37 +03:00
|
|
|
await visit('/settings');
|
2017-10-31 18:27:25 +03:00
|
|
|
|
2022-09-09 19:09:58 +03:00
|
|
|
expect(findAll('[data-test-modal="unsaved-settings"]').length, 'modal exists').to.equal(1);
|
2017-10-31 18:27:25 +03:00
|
|
|
|
|
|
|
// Leave without saving
|
2022-09-09 19:09:58 +03:00
|
|
|
await click('[data-test-modal="unsaved-settings"] [data-test-leave-button]');
|
2017-10-31 18:27:25 +03:00
|
|
|
|
2021-10-04 18:46:37 +03:00
|
|
|
expect(currentURL(), 'currentURL').to.equal('/settings');
|
2017-10-31 18:27:25 +03:00
|
|
|
|
2021-11-05 15:42:38 +03:00
|
|
|
await visit('/settings/integrations/slack');
|
2017-10-31 18:27:25 +03:00
|
|
|
|
2021-11-05 15:42:38 +03:00
|
|
|
expect(currentURL(), 'currentURL').to.equal('/settings/integrations/slack');
|
2017-10-31 18:27:25 +03:00
|
|
|
|
|
|
|
// settings were not saved
|
|
|
|
expect(
|
2019-01-02 12:58:55 +03:00
|
|
|
find('[data-test-slack-url-input]').textContent.trim(),
|
2017-10-31 18:27:25 +03:00
|
|
|
'Slack Webhook URL'
|
|
|
|
).to.equal('');
|
|
|
|
});
|
2016-03-29 11:40:44 +03:00
|
|
|
});
|
|
|
|
});
|