This repository has been archived by the owner on Nov 28, 2022. It is now read-only.
/
general-test.js
705 lines (583 loc) · 30.5 KB
/
general-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
/* jshint expr:true */
import {
describe,
it,
beforeEach,
afterEach
} from 'mocha';
import {expect} from 'chai';
import $ from 'jquery';
import startApp from '../../helpers/start-app';
import destroyApp from '../../helpers/destroy-app';
import {invalidateSession, authenticateSession} from 'ghost-admin/tests/helpers/ember-simple-auth';
import Mirage from 'ember-cli-mirage';
import mockThemes from 'ghost-admin/mirage/config/themes';
describe('Acceptance: Settings - General', function () {
let application;
beforeEach(function() {
application = startApp();
});
afterEach(function() {
destroyApp(application);
});
it('redirects to signin when not authenticated', function () {
invalidateSession(application);
visit('/settings/general');
andThen(function() {
expect(currentURL(), 'currentURL').to.equal('/signin');
});
});
it('redirects to team page when authenticated as author', function () {
let role = server.create('role', {name: 'Author'});
server.create('user', {roles: [role], slug: 'test-user'});
authenticateSession(application);
visit('/settings/general');
andThen(() => {
expect(currentURL(), 'currentURL').to.equal('/team/test-user');
});
});
it('redirects to team page when authenticated as editor', function () {
let role = server.create('role', {name: 'Editor'});
server.create('user', {roles: [role], slug: 'test-user'});
authenticateSession(application);
visit('/settings/general');
andThen(() => {
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 renders, shows image uploader modals', function () {
visit('/settings/general');
andThen(() => {
// has correct url
expect(currentURL(), 'currentURL').to.equal('/settings/general');
// has correct page title
expect(document.title, 'page title').to.equal('Settings - General - Test Blog');
// highlights nav menu
expect($('.gh-nav-settings-general').hasClass('active'), 'highlights nav menu item')
.to.be.true;
expect(find('.view-header .view-actions .btn-blue').text().trim(), 'save button text').to.equal('Save');
// initial postsPerPage should be 5
expect(find('input#postsPerPage').val(), 'post per page value').to.equal('5');
expect(find('input#permalinks').prop('checked'), 'date permalinks checkbox').to.be.false;
});
fillIn('#settings-general input[name="general[title]"]', 'New Blog Title');
click('.view-header .btn.btn-blue');
andThen(() => {
expect(document.title, 'page title').to.equal('Settings - General - New Blog Title');
});
click('.blog-logo');
andThen(() => {
expect(find('.fullscreen-modal .modal-content .gh-image-uploader').length, 'modal selector').to.equal(1);
});
click('.fullscreen-modal .modal-content .gh-image-uploader .image-cancel');
andThen(() => {
expect(find('.fullscreen-modal .modal-content .gh-image-uploader .description').text()).to.equal('Upload an image');
});
// click cancel button
click('.fullscreen-modal .modal-footer .btn.btn-minor');
andThen(() => {
expect(find('.fullscreen-modal').length).to.equal(0);
});
click('.blog-cover');
andThen(() => {
expect(find('.fullscreen-modal .modal-content .gh-image-uploader').length, 'modal selector').to.equal(1);
});
click('.fullscreen-modal .modal-footer .js-button-accept');
andThen(() => {
expect(find('.fullscreen-modal').length).to.equal(0);
});
});
it('renders timezone selector correctly', function () {
visit('/settings/general');
andThen(() => {
expect(currentURL(), 'currentURL').to.equal('/settings/general');
expect(find('#activeTimezone option').length, 'available timezones').to.equal(66);
expect(find('#activeTimezone option:selected').text().trim()).to.equal('(GMT) UTC');
find('#activeTimezone option[value="Africa/Cairo"]').prop('selected', true);
});
triggerEvent('#activeTimezone', 'change');
click('.view-header .btn.btn-blue');
andThen(() => {
expect(find('#activeTimezone option:selected').text().trim()).to.equal('(GMT +2:00) Cairo, Egypt');
});
});
it('handles private blog settings correctly', function () {
visit('/settings/general');
// handles private blog settings correctly
andThen(() => {
expect(find('input#isPrivate').prop('checked'), 'isPrivate checkbox').to.be.false;
});
click('input#isPrivate');
andThen(() => {
expect(find('input#isPrivate').prop('checked'), 'isPrivate checkbox').to.be.true;
expect(find('#settings-general input[name="general[password]"]').length, 'password input').to.equal(1);
expect(find('#settings-general input[name="general[password]"]').val(), 'password default value').to.not.equal('');
});
fillIn('#settings-general input[name="general[password]"]', '');
triggerEvent('#settings-general input[name="general[password]"]', 'blur');
andThen(() => {
expect(find('#settings-general .error .response').text().trim(), 'inline validation response')
.to.equal('Password must be supplied');
});
fillIn('#settings-general input[name="general[password]"]', 'asdfg');
triggerEvent('#settings-general input[name="general[password]"]', 'blur');
andThen(() => {
expect(find('#settings-general .error .response').text().trim(), 'inline validation response')
.to.equal('');
});
// validates a facebook url correctly
andThen(() => {
// loads fixtures and performs transform
expect(find('input[name="general[facebook]"]').val(), 'initial facebook value')
.to.equal('https://www.facebook.com/test');
});
triggerEvent('#settings-general input[name="general[facebook]"]', 'focus');
triggerEvent('#settings-general input[name="general[facebook]"]', 'blur');
andThen(() => {
// regression test: we still have a value after the input is
// focused and then blurred without any changes
expect(find('input[name="general[facebook]"]').val(), 'facebook value after blur with no change')
.to.equal('https://www.facebook.com/test');
});
fillIn('#settings-general input[name="general[facebook]"]', 'facebook.com/username');
triggerEvent('#settings-general input[name="general[facebook]"]', 'blur');
andThen(() => {
expect(find('#settings-general input[name="general[facebook]"]').val()).to.be.equal('https://www.facebook.com/username');
expect(find('#settings-general .error .response').text().trim(), 'inline validation response')
.to.equal('');
});
fillIn('#settings-general input[name="general[facebook]"]', 'facebook.com/pages/some-facebook-page/857469375913?ref=ts');
triggerEvent('#settings-general input[name="general[facebook]"]', 'blur');
andThen(() => {
expect(find('#settings-general input[name="general[facebook]"]').val()).to.be.equal('https://www.facebook.com/pages/some-facebook-page/857469375913?ref=ts');
expect(find('#settings-general .error .response').text().trim(), 'inline validation response')
.to.equal('');
});
fillIn('#settings-general input[name="general[facebook]"]', '*(&*(%%))');
triggerEvent('#settings-general input[name="general[facebook]"]', 'blur');
andThen(() => {
expect(find('#settings-general .error .response').text().trim(), 'inline validation response')
.to.equal('The URL must be in a format like https://www.facebook.com/yourPage');
});
fillIn('#settings-general input[name="general[facebook]"]', 'http://github.com/username');
triggerEvent('#settings-general input[name="general[facebook]"]', 'blur');
andThen(() => {
expect(find('#settings-general input[name="general[facebook]"]').val()).to.be.equal('https://www.facebook.com/username');
expect(find('#settings-general .error .response').text().trim(), 'inline validation response')
.to.equal('');
});
fillIn('#settings-general input[name="general[facebook]"]', 'http://github.com/pages/username');
triggerEvent('#settings-general input[name="general[facebook]"]', 'blur');
andThen(() => {
expect(find('#settings-general input[name="general[facebook]"]').val()).to.be.equal('https://www.facebook.com/pages/username');
expect(find('#settings-general .error .response').text().trim(), 'inline validation response')
.to.equal('');
});
fillIn('#settings-general input[name="general[facebook]"]', 'testuser');
triggerEvent('#settings-general input[name="general[facebook]"]', 'blur');
andThen(() => {
expect(find('#settings-general input[name="general[facebook]"]').val()).to.be.equal('https://www.facebook.com/testuser');
expect(find('#settings-general .error .response').text().trim(), 'inline validation response')
.to.equal('');
});
fillIn('#settings-general input[name="general[facebook]"]', 'ab99');
triggerEvent('#settings-general input[name="general[facebook]"]', 'blur');
andThen(() => {
expect(find('#settings-general .error .response').text().trim(), 'inline validation response')
.to.equal('Your Page name is not a valid Facebook Page name');
});
fillIn('#settings-general input[name="general[facebook]"]', 'page/ab99');
triggerEvent('#settings-general input[name="general[facebook]"]', 'blur');
andThen(() => {
expect(find('#settings-general input[name="general[facebook]"]').val()).to.be.equal('https://www.facebook.com/page/ab99');
expect(find('#settings-general .error .response').text().trim(), 'inline validation response')
.to.equal('');
});
fillIn('#settings-general input[name="general[facebook]"]', 'page/*(&*(%%))');
triggerEvent('#settings-general input[name="general[facebook]"]', 'blur');
andThen(() => {
expect(find('#settings-general input[name="general[facebook]"]').val()).to.be.equal('https://www.facebook.com/page/*(&*(%%))');
expect(find('#settings-general .error .response').text().trim(), 'inline validation response')
.to.equal('');
});
// validates a twitter url correctly
andThen(() => {
// loads fixtures and performs transform
expect(find('input[name="general[twitter]"]').val(), 'initial twitter value')
.to.equal('https://twitter.com/test');
});
triggerEvent('#settings-general input[name="general[twitter]"]', 'focus');
triggerEvent('#settings-general input[name="general[twitter]"]', 'blur');
andThen(() => {
// regression test: we still have a value after the input is
// focused and then blurred without any changes
expect(find('input[name="general[twitter]"]').val(), 'twitter value after blur with no change')
.to.equal('https://twitter.com/test');
});
fillIn('#settings-general input[name="general[twitter]"]', 'twitter.com/username');
triggerEvent('#settings-general input[name="general[twitter]"]', 'blur');
andThen(() => {
expect(find('#settings-general input[name="general[twitter]"]').val()).to.be.equal('https://twitter.com/username');
expect(find('#settings-general .error .response').text().trim(), 'inline validation response')
.to.equal('');
});
fillIn('#settings-general input[name="general[twitter]"]', '*(&*(%%))');
triggerEvent('#settings-general input[name="general[twitter]"]', 'blur');
andThen(() => {
expect(find('#settings-general .error .response').text().trim(), 'inline validation response')
.to.equal('The URL must be in a format like https://twitter.com/yourUsername');
});
fillIn('#settings-general input[name="general[twitter]"]', 'http://github.com/username');
triggerEvent('#settings-general input[name="general[twitter]"]', 'blur');
andThen(() => {
expect(find('#settings-general input[name="general[twitter]"]').val()).to.be.equal('https://twitter.com/username');
expect(find('#settings-general .error .response').text().trim(), 'inline validation response')
.to.equal('');
});
fillIn('#settings-general input[name="general[twitter]"]', 'thisusernamehasmorethan15characters');
triggerEvent('#settings-general input[name="general[twitter]"]', 'blur');
andThen(() => {
expect(find('#settings-general .error .response').text().trim(), 'inline validation response')
.to.equal('Your Username is not a valid Twitter Username');
});
fillIn('#settings-general input[name="general[twitter]"]', 'testuser');
triggerEvent('#settings-general input[name="general[twitter]"]', 'blur');
andThen(() => {
expect(find('#settings-general input[name="general[twitter]"]').val()).to.be.equal('https://twitter.com/testuser');
expect(find('#settings-general .error .response').text().trim(), 'inline validation response')
.to.equal('');
});
});
it('allows management of themes', function () {
// lists available themes + active theme is highlighted
// theme upload
// - displays modal
// - validates mime type
// - validates casper.zip
// - handles validation errors
// - handles upload and close
// - handles upload and activate
// - displays overwrite warning if theme already exists
// theme activation
// - switches theme
// theme deletion
// - displays modal
// - deletes theme and refreshes list
visit('/settings/general');
// lists available themes (themes are specified in mirage/fixtures/settings)
andThen(() => {
expect(
find('.theme-list-item').length,
'shows correct number of themes'
).to.equal(3);
expect(
find('.theme-list-item:contains("Blog")').hasClass('theme-list-item--active'),
'Blog theme marked as active'
);
});
// theme upload displays modal
click('a:contains("Upload a theme")');
andThen(() => {
expect(
find('.fullscreen-modal .modal-content:contains("Upload a theme")').length,
'theme upload modal displayed after button click'
).to.equal(1);
});
// cancelling theme upload closes modal
click('.fullscreen-modal button:contains("Cancel")');
andThen(() => {
expect(
find('.fullscreen-modal').length === 0,
'upload theme modal is closed when cancelling'
).to.be.true;
});
// theme upload validates mime type
click('a:contains("Upload a theme")');
fileUpload('.fullscreen-modal input[type="file"]', ['test'], {type: 'text/csv'});
andThen(() => {
expect(
find('.fullscreen-modal .failed').text(),
'validation error is shown for invalid mime type'
).to.match(/is not supported/);
});
// theme upload validates casper.zip
click('button:contains("Try Again")');
fileUpload('.fullscreen-modal input[type="file"]', ['test'], {name: 'casper.zip', type: 'application/zip'});
andThen(() => {
expect(
find('.fullscreen-modal .failed').text(),
'validation error is shown when uploading casper.zip'
).to.match(/default Casper theme cannot be overwritten/);
});
// theme upload handles upload errors
andThen(() => {
server.post('/themes/upload/', function () {
return new Mirage.Response(422, {}, {
errors: [{
message: 'Invalid theme'
}]
});
});
});
click('button:contains("Try Again")');
fileUpload('.fullscreen-modal input[type="file"]', ['test'], {name: 'error.zip', type: 'application/zip'});
andThen(() => {
expect(
find('.fullscreen-modal .failed').text().trim(),
'validation error is passed through from server'
).to.equal('Invalid theme');
// reset to default mirage handlers
mockThemes(server);
});
// theme upload handles validation errors
andThen(() => {
server.post('/themes/upload/', function () {
return new Mirage.Response(422, {}, {
errors: [
{
message: 'Theme is not compatible or contains errors.',
errorType: 'ThemeValidationError',
errorDetails: [
{
level: 'error',
rule: 'Templates must contain valid Handlebars.',
failures: [
{
ref: 'index.hbs',
message: 'The partial index_meta could not be found'
},
{
ref: 'tag.hbs',
message: 'The partial index_meta could not be found'
}
]
},
{
level: 'error',
rule: 'Assets such as CSS & JS must use the <code>{{asset}}</code> helper',
details: '<p>The listed files should be included using the <code>{{asset}}</code> helper.</p>',
failures: [
{
ref: '/assets/javascripts/ui.js'
}
]
}
]
}
]
});
});
});
click('button:contains("Try Again")');
fileUpload('.fullscreen-modal input[type="file"]', ['test'], {name: 'bad-theme.zip', type: 'application/zip'});
andThen(() => {
expect(
find('.fullscreen-modal h1').text().trim(),
'modal title after uploading invalid theme'
).to.equal('Invalid theme');
expect(
find('.theme-validation-errors').text(),
'top-level errors are displayed'
).to.match(/Templates must contain valid Handlebars/);
expect(
find('.theme-validation-errors').text(),
'top-level errors do not escape HTML'
).to.match(/The listed files should be included using the {{asset}} helper/);
expect(
find('.theme-validation-errors').text(),
'individual failures are displayed'
).to.match(/index\.hbs: The partial index_meta could not be found/);
// reset to default mirage handlers
mockThemes(server);
});
click('button:contains("Try Again")');
andThen(() => {
expect(
find('.theme-validation-errors').length,
'"Try Again" resets form after theme validation error'
).to.equal(0);
expect(
find('.gh-image-uploader').length,
'"Try Again" resets form after theme validation error'
).to.equal(1);
expect(
find('.fullscreen-modal h1').text().trim(),
'"Try Again" resets form after theme validation error'
).to.equal('Upload a theme');
});
// theme upload handles validation warnings
andThen(() => {
server.post('/themes/upload/', function () {
return new Mirage.Response(200, {}, {
themes: [
{
name: 'blackpalm',
package: {
name: 'BlackPalm',
version: '1.0.0'
},
warnings: [
{
level: 'warning',
rule: 'Assets such as CSS & JS must use the <code>{{asset}}</code> helper',
details: '<p>The listed files should be included using the <code>{{asset}}</code> helper. For more information, please see the <a href="http://themes.ghost.org/docs/asset">asset helper documentation</a>.</p>',
failures: [
{
ref: '/assets/dist/img/apple-touch-icon.png'
},
{
ref: '/assets/dist/img/favicon.ico'
},
{
ref: '/assets/dist/css/blackpalm.min.css'
},
{
ref: '/assets/dist/js/blackpalm.min.js'
}
],
code: 'GS030-ASSET-REQ'
}
]
}
]
});
});
});
fileUpload('.fullscreen-modal input[type="file"]', ['test'], {name: 'warning-theme.zip', type: 'application/zip'});
andThen(() => {
expect(
find('.fullscreen-modal h1').text().trim(),
'modal title after uploading theme with warnings'
).to.equal('Uploaded with warnings');
expect(
find('.theme-validation-errors').text(),
'top-level warnings are displayed'
).to.match(/The listed files should be included using the {{asset}} helper/);
expect(
find('.theme-validation-errors').text(),
'individual warning failures are displayed'
).to.match(/\/assets\/dist\/img\/apple-touch-icon\.png/);
// reset to default mirage handlers
mockThemes(server);
});
click('button:contains("Close")');
// theme upload handles success then close
click('a:contains("Upload a theme")');
fileUpload('.fullscreen-modal input[type="file"]', ['test'], {name: 'theme-1.zip', type: 'application/zip'});
andThen(() => {
expect(
find('.fullscreen-modal h1').text().trim(),
'modal header after successful upload'
).to.equal('Upload successful!');
expect(
find('.modal-body').text(),
'modal displays theme name after successful upload'
).to.match(/"Test 1 - 0\.1" uploaded successfully/);
expect(
find('.theme-list-item').length,
'number of themes in list grows after upload'
).to.equal(4);
expect(
find('.theme-list-item:contains("Test 1 - 0.1")').hasClass('theme-list-item--active'),
'newly uploaded theme is active'
).to.be.false;
});
click('.fullscreen-modal button:contains("Close")');
// theme upload handles success then activate
click('a:contains("Upload a theme")');
fileUpload('.fullscreen-modal input[type="file"]', ['test'], {name: 'theme-2.zip', type: 'application/zip'});
click('button:contains("Activate Now")');
andThen(() => {
expect(
find('.theme-list-item').length,
'number of themes in list grows after upload and activate'
).to.equal(5);
expect(
find('.theme-list-item:contains("Test 2 - 0.1")').hasClass('theme-list-item--active'),
'newly uploaded+activated theme is active'
).to.be.true;
});
// theme activation switches active theme
click('.theme-list-item:contains("Blog") a:contains("Activate")');
andThen(() => {
expect(
find('.theme-list-item:contains("Test 2 - 0.1")').hasClass('theme-list-item--active'),
'previously active theme is not active'
).to.be.false;
expect(
find('.theme-list-item:contains("Blog")').hasClass('theme-list-item--active'),
'activated theme is active'
).to.be.true;
});
// theme deletion displays modal
click('.theme-list-item:contains("Test 1") a:contains("Delete")');
andThen(() => {
expect(
find('.fullscreen-modal .modal-content:contains("delete this theme")').length,
'theme deletion modal displayed after button click'
).to.equal(1);
});
// cancelling theme deletion closes modal
click('.fullscreen-modal button:contains("Cancel")');
andThen(() => {
expect(
find('.fullscreen-modal').length === 0,
'delete theme modal is closed when cancelling'
).to.be.true;
});
// confirming theme deletion closes modal and refreshes list
click('.theme-list-item:contains("Test 1") a:contains("Delete")');
click('.fullscreen-modal button:contains("Delete")');
andThen(() => {
expect(
find('.fullscreen-modal').length === 0,
'delete theme modal closes after deletion'
).to.be.true;
});
andThen(() => {
expect(
find('.theme-list-item').length,
'number of themes in list shrinks after delete'
).to.equal(4);
expect(
find('.theme-list-item .name').text(),
'correct theme is removed from theme list after deletion'
).to.not.match(/Test 1/);
});
// validation errors are handled when deleting a theme
andThen(() => {
server.del('/themes/:theme/', function () {
return new Mirage.Response(422, {}, {
errors: [{
message: 'Can\'t delete theme'
}]
});
});
});
click('.theme-list-item:contains("Test 2") a:contains("Delete")');
click('.fullscreen-modal button:contains("Delete")');
andThen(() => {
expect(
find('.fullscreen-modal').length === 0,
'delete theme modal closes after failed deletion'
).to.be.true;
expect(
find('.gh-alert').length,
'alert is shown when deletion fails'
).to.equal(1);
expect(
find('.gh-alert').text(),
'failed deletion alert has correct text'
).to.match(/Can't delete theme/);
// restore default mirage handlers
mockThemes(server);
});
});
});
});