Ghost/ghost/admin/tests/acceptance/settings/unsplash-test.js
Kevin Ansfield 350e3d1481 Unsplash integration
closes https://github.com/TryGhost/Ghost/issues/8859, requires https://github.com/TryGhost/Ghost/pull/8895
- adds Unsplash app to app settings
  - enable/disable toggle
  - validation and testing of Unsplash App ID
  - Unsplash App ID field hidden if provided via Ghost config
  - adds `fetchPrivate` method to `config` service to pull config that requires authentication and updates authentication routines to fetch private config
- adds Unsplash buttons to editor toolbar and `{{gh-image-uploader}}`
  - only present when Unsplash app is enabled
  - opens Unsplash image selector when clicked
  - `{{gh-image-uploader}}` has a new `allowUnsplash` attribute to control display of the unsplash button on a per-uploader basis
- adds Unsplash image selector (`{{gh-unsplash}}`)
  - uses new `unsplash` service to handle API requests and maintain state
  - search
  - infinite scroll
  - zoom image
  - insert image
  - download image
- adds `{{gh-scroll-trigger}}` that will fire an event when the component is rendered into or enters the visible screen area via scrolling
- updates `ui` service
  - adds `isFullscreen` property and updates `gh-editor` so that it gets set/unset when toggling editor fullscreen mode
  - adds `hasSideNav` and `isSideNavHidden` properties
- updates `media-queries` service so that it fires an event each time a breakpoint is entered/exited
  - removes the need for observers in certain circumstances
2017-08-15 16:01:12 +01:00

181 lines
7.3 KiB
JavaScript

/* jshint expr:true */
import Mirage from 'ember-cli-mirage';
import ctrlOrCmd from 'ghost-admin/utils/ctrl-or-cmd';
import destroyApp from '../../helpers/destroy-app';
import startApp from '../../helpers/start-app';
import {afterEach, beforeEach, describe, it} from 'mocha';
import {authenticateSession, invalidateSession} from 'ghost-admin/tests/helpers/ember-simple-auth';
import {expect} from 'chai';
describe('Acceptance: Settings - Apps - Unsplash', function () {
let application;
beforeEach(function () {
application = startApp();
});
afterEach(function () {
destroyApp(application);
});
it('redirects to signin when not authenticated', async function () {
invalidateSession(application);
await visit('/settings/apps/unsplash');
expect(currentURL(), 'currentURL').to.equal('/signin');
});
it('redirects to team page when authenticated as author', async function () {
let role = server.create('role', {name: 'Author'});
server.create('user', {roles: [role], slug: 'test-user'});
authenticateSession(application);
await visit('/settings/apps/unsplash');
expect(currentURL(), 'currentURL').to.equal('/team/test-user');
});
it('redirects to team page when authenticated as editor', async function () {
let role = server.create('role', {name: 'Editor'});
server.create('user', {roles: [role], slug: 'test-user'});
authenticateSession(application);
await visit('/settings/apps/unsplash');
expect(currentURL(), 'currentURL').to.equal('/team');
});
describe('when logged in', function () {
beforeEach(function () {
let role = server.create('role', {name: 'Administrator'});
server.create('user', {roles: [role]});
return authenticateSession(application);
});
it('it validates and saves an application id properly', async function () {
server.get('/configuration/private', function () {
return new Mirage.Response(200, {}, {
configuration: {
unsplashAPI: ''
}
});
});
await visit('/settings/apps/unsplash');
// has correct url
expect(currentURL(), 'currentURL').to.equal('/settings/apps/unsplash');
// without application id provided via config
expect(find('[data-test-input="unsplash"]').text().trim(), 'default application id value')
.to.equal('');
expect(find('[data-test-checkbox="unsplash"]').prop('checked'), 'isActive checkbox').to.be.false;
await click(find('[data-test-checkbox="unsplash"]'), 'enable without application id');
expect(find('[data-test-checkbox="unsplash"]').prop('checked'), 'isActive checkbox').to.be.true;
expect(find('#unsplash-toggle .error .response').text().trim(), 'inline validation checkbox')
.to.equal('You need to enter an Application ID before enabling it');
await fillIn('[data-test-input="unsplash"]', '345 456 567 ');
expect(find('#unsplash-toggle .error .response').text().trim(), 'inline validation checkbox')
.to.equal('');
await click('[data-test-save-button]');
// application id validation
expect(find('#unsplash-settings .error .response').text().trim(), 'inline validation response')
.to.equal('Please enter a valid Application Id for Unsplash');
// doesn't save when errors
expect(find('[data-test-save-button]').text().trim(), 'task button saved response')
.to.equal('Retry');
// CMD-S shortcut works
await fillIn('[data-test-input="unsplash"]', '123456789012345678901234567890');
await triggerEvent('.gh-app', 'keydown', {
keyCode: 83, // s
metaKey: ctrlOrCmd === 'command',
ctrlKey: ctrlOrCmd === 'ctrl'
});
let [firstRequest] = server.pretender.handledRequests.slice(-1);
let params = JSON.parse(firstRequest.requestBody);
let result = JSON.parse(params.settings.findBy('key', 'unsplash').value);
expect(result.applicationId).to.equal('123456789012345678901234567890');
expect(find('#unsplash-settings .error .response').text().trim(), 'inline validation response')
.to.equal('');
server.get('https://api.unsplash.com/photos/random', function () {
return new Mirage.Response(401, {}, {
errors: ['OAuth error: The access token is invalid']
});
});
// Test invalid application id
await fillIn('[data-test-input="unsplash"]', '098765432109876543210987654321');
await click('[data-test-button="send-request"]');
expect(find('[data-test-button="send-request"]').text().trim(), 'test request button validation response')
.to.equal('Invalid Application Id');
expect(find('.gh-alert-red .gh-alert-content').text().trim(), 'server response')
.to.equal('OAuth error: The access token is invalid');
let [secondRequest] = server.pretender.handledRequests.slice(-1);
// Result shouldn't be saved
expect(secondRequest.requestBody).to.equal(null);
expect(secondRequest.url).to.equal('https://api.unsplash.com/photos/random');
server.get('https://api.unsplash.com/photos/random', function () {
return new Mirage.Response(200);
});
// Test valid application id
await fillIn('[data-test-input="unsplash"]', '098765432109876543210987654321');
await click('[data-test-button="send-request"]');
expect(find('[data-test-button="send-request"]').text().trim(), 'test request button validation response')
.to.equal('Valid Application ID');
// saves settings when valid application id
expect(find('[data-test-save-button]').text().trim(), 'task button saved response')
.to.equal('Saved');
let [thirdRequest] = server.pretender.handledRequests.slice(-1);
params = JSON.parse(thirdRequest.requestBody);
result = JSON.parse(params.settings.findBy('key', 'unsplash').value);
// Result should be saved
expect(result.applicationId).to.equal('098765432109876543210987654321');
});
it('does not render application id input when config provides it', async function () {
server.get('/configuration/private', function () {
return new Mirage.Response(200, {}, {
configuration: [{
unsplashAPI: {
applicationId: '12345678923456789'
}
}]
});
});
await visit('/settings/apps/unsplash');
// has correct url
expect(currentURL(), 'currentURL').to.equal('/settings/apps/unsplash');
// without application id provided via config
expect(find('[data-test-input="unsplash"]').length, 'no application id input')
.to.equal(0);
});
});
});