1 | var assert = require('assert');
|
2 | var mongo = require('mongodb');
|
3 | var _ = require('lodash');
|
4 | var apos = require('apostrophe')();
|
5 |
|
6 | var db;
|
7 |
|
8 | var pages;
|
9 |
|
10 | var home;
|
11 |
|
12 | var page;
|
13 |
|
14 | var children;
|
15 |
|
16 | var about;
|
17 |
|
18 | var contact;
|
19 |
|
20 | var req = apos.getTaskReq();
|
21 |
|
22 |
|
23 |
|
24 | describe('apostrophe-pages', function() {
|
25 | describe('initialize resources', function() {
|
26 | it('initialize mongodb', function(done) {
|
27 | db = new mongo.Db(
|
28 | 'apostest',
|
29 | new mongo.Server('127.0.0.1', 27017, {}),
|
30 |
|
31 |
|
32 | { safe: true }
|
33 | );
|
34 | assert(!!db);
|
35 | db.open(function(err) {
|
36 | assert(!err);
|
37 | return done();
|
38 | });
|
39 | });
|
40 | it('initialize apostrophe', function(done) {
|
41 | return apos.init({
|
42 | db: db,
|
43 | app: {
|
44 | request: {},
|
45 | locals: {},
|
46 | get: function() {},
|
47 | post: function() {},
|
48 | all: function() {}
|
49 | }
|
50 | }, function(err) {
|
51 | assert(!err);
|
52 | return done();
|
53 | });
|
54 | });
|
55 | it('initialize apostrophe-pages', function(done) {
|
56 | pages = require('../index.js')({
|
57 | apos: apos,
|
58 | ui: false
|
59 | }, function(err) {
|
60 | assert(!err);
|
61 | assert(!!pages);
|
62 | return done();
|
63 | });
|
64 | });
|
65 | });
|
66 | describe('remove test data', function() {
|
67 | it('removed', function(done) {
|
68 | apos.pages.remove({}, function(err) {
|
69 | assert(!err);
|
70 | done();
|
71 | });
|
72 | });
|
73 | });
|
74 | describe('insert test data', function() {
|
75 | apos.pages = apos.pages;
|
76 | it('inserted', function(done) {
|
77 | apos.pages.insert(
|
78 | [
|
79 | {
|
80 | _id: 'home',
|
81 | path: 'home',
|
82 | title: 'Home',
|
83 | sortTitle: 'home',
|
84 | level: 0,
|
85 | rank: 0,
|
86 | slug: '/',
|
87 | body: {
|
88 | items: [
|
89 | {
|
90 | type: 'richText',
|
91 | content: '<p>Body content</p>'
|
92 | }
|
93 | ],
|
94 | type: 'area'
|
95 | },
|
96 | sidebar: {
|
97 | items: [
|
98 | {
|
99 | type: 'richText',
|
100 | content: '<p>Sidebar content</p>'
|
101 | }
|
102 | ],
|
103 | type: 'area'
|
104 | },
|
105 | published: true
|
106 | },
|
107 |
|
108 | {
|
109 | _id: 'contact',
|
110 | path: 'home/contact',
|
111 | title: 'Contact',
|
112 | sortTitle: 'contact',
|
113 | level: 1,
|
114 | rank: 2,
|
115 | slug: '/contact',
|
116 | body: {
|
117 | items: [
|
118 | {
|
119 | type: 'richText',
|
120 | content: '<p>Body content</p>'
|
121 | }
|
122 | ],
|
123 | type: 'area'
|
124 | },
|
125 | sidebar: {
|
126 | items: [
|
127 | {
|
128 | type: 'richText',
|
129 | content: '<p>Sidebar content</p>'
|
130 | }
|
131 | ],
|
132 | type: 'area'
|
133 | },
|
134 | tags: [ 'red', 'green' ],
|
135 | published: true
|
136 | },
|
137 | {
|
138 | _id: 'about',
|
139 | path: 'home/about',
|
140 | title: 'About',
|
141 | sortTitle: 'about',
|
142 | level: 1,
|
143 | rank: 0,
|
144 | slug: '/about',
|
145 | body: {
|
146 | items: [
|
147 | {
|
148 | type: 'richText',
|
149 | content: '<p>Body content</p>'
|
150 | }
|
151 | ],
|
152 | type: 'area'
|
153 | },
|
154 | sidebar: {
|
155 | items: [
|
156 | {
|
157 | type: 'richText',
|
158 | content: '<p>Sidebar content</p>'
|
159 | }
|
160 | ],
|
161 | type: 'area'
|
162 | },
|
163 | tags: [ 'green', 'blue' ],
|
164 | published: true
|
165 | },
|
166 | {
|
167 | _id: 'location',
|
168 | path: 'home/about/location',
|
169 | title: 'Location',
|
170 | sortTitle: 'location',
|
171 | level: 2,
|
172 | rank: 1,
|
173 | slug: '/about/location',
|
174 | body: {
|
175 | items: [
|
176 | {
|
177 | type: 'richText',
|
178 | content: '<p>Body content</p>'
|
179 | }
|
180 | ],
|
181 | type: 'area'
|
182 | },
|
183 | sidebar: {
|
184 | items: [
|
185 | {
|
186 | type: 'richText',
|
187 | content: '<p>Sidebar content</p>'
|
188 | }
|
189 | ],
|
190 | type: 'area'
|
191 | },
|
192 | published: true
|
193 | },
|
194 | {
|
195 | _id: 'people',
|
196 | path: 'home/about/people',
|
197 | title: 'People',
|
198 | sortTitle: 'people',
|
199 | level: 2,
|
200 | rank: 0,
|
201 | slug: '/about/people',
|
202 | body: {
|
203 | items: [
|
204 | {
|
205 | type: 'richText',
|
206 | content: '<p>Body content</p>'
|
207 | }
|
208 | ],
|
209 | type: 'area'
|
210 | },
|
211 | sidebar: {
|
212 | items: [
|
213 | {
|
214 | type: 'richText',
|
215 | content: '<p>Sidebar content</p>'
|
216 | }
|
217 | ],
|
218 | type: 'area'
|
219 | },
|
220 | tags: [ 'green' ],
|
221 | published: true
|
222 | },
|
223 | {
|
224 | _id: 'friends',
|
225 |
|
226 |
|
227 |
|
228 | orphan: true,
|
229 | path: 'home/about/friends',
|
230 | title: 'Friends',
|
231 | sortTitle: 'friends',
|
232 | level: 2,
|
233 | rank: 2,
|
234 | slug: '/about/friends',
|
235 | body: {
|
236 | items: [
|
237 | {
|
238 | type: 'richText',
|
239 | content: '<p>Body content</p>'
|
240 | }
|
241 | ],
|
242 | type: 'area'
|
243 | },
|
244 | sidebar: {
|
245 | items: [
|
246 | {
|
247 | type: 'richText',
|
248 | content: '<p>Sidebar content</p>'
|
249 | }
|
250 | ],
|
251 | type: 'area'
|
252 | },
|
253 | tags: [ 'friends' ],
|
254 | published: true
|
255 | },
|
256 | {
|
257 | _id: 'products',
|
258 | path: 'home/products',
|
259 | title: 'Products',
|
260 | sortTitle: 'products',
|
261 | level: 1,
|
262 | rank: 1,
|
263 | slug: '/products',
|
264 | body: {
|
265 | items: [
|
266 | {
|
267 | type: 'richText',
|
268 | content: '<p>Body content</p>'
|
269 | }
|
270 | ],
|
271 | type: 'area'
|
272 | },
|
273 | sidebar: {
|
274 | items: [
|
275 | {
|
276 | type: 'richText',
|
277 | content: '<p>Sidebar content</p>'
|
278 | }
|
279 | ],
|
280 | type: 'area'
|
281 | },
|
282 | published: true
|
283 | }
|
284 | ], function(err) {
|
285 | assert(!err);
|
286 | done();
|
287 | }
|
288 | );
|
289 | });
|
290 | });
|
291 | describe('fetch home page', function() {
|
292 | it('fetched', function(done) {
|
293 | apos.pages.findOne({ _id: 'home' }, function(err, doc) {
|
294 | assert(!!doc);
|
295 | home = doc;
|
296 | page = doc;
|
297 | done();
|
298 | });
|
299 | });
|
300 | });
|
301 | describe('fetch ancestors of home page (should be empty)', function() {
|
302 | it('fetched', function(done) {
|
303 | pages.getAncestors(req, page, function(err, ancestors) {
|
304 | assert(ancestors.length === 0);
|
305 | done();
|
306 | });
|
307 | });
|
308 | });
|
309 |
|
310 | describe('fetch descendants of home page', function() {
|
311 | it('fetched', function(done) {
|
312 | pages.getDescendants(req, page, { depth: 2 }, function(err, childrenArg) {
|
313 | children = childrenArg;
|
314 | assert(!err);
|
315 | assert(children.length === 3);
|
316 | done();
|
317 | });
|
318 | });
|
319 | it('in order', function() {
|
320 | assert(children[0]._id === 'about');
|
321 | assert(children[1]._id === 'products');
|
322 | assert(children[2]._id === 'contact');
|
323 | });
|
324 | it('did not return areas', function() {
|
325 | assert(!children[0].body);
|
326 | assert(!children[1].body);
|
327 | assert(!children[2].body);
|
328 | });
|
329 | it('have grandkids', function() {
|
330 | assert(children[0].children.length === 3);
|
331 | });
|
332 | it('grandkids in order', function() {
|
333 | assert(children[0].children[0]._id === 'people');
|
334 | assert(children[0].children[1]._id === 'location');
|
335 | assert(children[0].children[2]._id === 'friends');
|
336 | });
|
337 | it('fetch again with orphans turned off', function(done) {
|
338 | pages.getDescendants(req, page, { depth: 2, orphan: false }, function(err, childrenArg) {
|
339 | children = childrenArg;
|
340 | assert(!err);
|
341 | assert(children.length === 3);
|
342 | done();
|
343 | });
|
344 | });
|
345 | it('in order', function() {
|
346 | assert(children[0]._id === 'about');
|
347 | assert(children[1]._id === 'products');
|
348 | assert(children[2]._id === 'contact');
|
349 | });
|
350 | it('did not return areas', function() {
|
351 | assert(!children[0].body);
|
352 | assert(!children[1].body);
|
353 | assert(!children[2].body);
|
354 | });
|
355 | it('have correct number of grandkids', function() {
|
356 | assert(children[0].children.length === 2);
|
357 | });
|
358 | it('grandkids in order', function() {
|
359 | assert(children[0].children[0]._id === 'people');
|
360 | assert(children[0].children[1]._id === 'location');
|
361 | });
|
362 | it('fetch again with areas turned on', function(done) {
|
363 | pages.getDescendants(req, page, { depth: 2, areas: true }, function(err, childrenArg) {
|
364 | children = childrenArg;
|
365 | assert(!err);
|
366 | assert(children.length === 3);
|
367 | done();
|
368 | });
|
369 | });
|
370 | it('did return areas', function() {
|
371 | assert(children[0].sidebar);
|
372 | assert(children[1].sidebar);
|
373 | assert(children[2].sidebar);
|
374 | assert(children[0].body);
|
375 | assert(children[1].body);
|
376 | assert(children[2].body);
|
377 | });
|
378 | it('fetch again with specific area', function(done) {
|
379 | pages.getDescendants(req, page, { depth: 2, areas: [ 'body' ] }, function(err, childrenArg) {
|
380 | children = childrenArg;
|
381 | assert(!err);
|
382 | assert(children.length === 3);
|
383 | done();
|
384 | });
|
385 | });
|
386 | it('returned body area', function() {
|
387 | assert(children[0].body);
|
388 | assert(children[1].body);
|
389 | assert(children[2].body);
|
390 | });
|
391 | it('did not return sidebar area', function() {
|
392 | assert(!children[0].sidebar);
|
393 | assert(!children[1].sidebar);
|
394 | assert(!children[2].sidebar);
|
395 | });
|
396 | });
|
397 |
|
398 | var ancestors;
|
399 |
|
400 | describe('fetch ancestors of home/about/people', function() {
|
401 | it('fetched', function(done) {
|
402 | var people = children[0].children[0];
|
403 | pages.getAncestors(req, people, function(err, ancestorsArg) {
|
404 | assert(!err);
|
405 | assert(ancestorsArg);
|
406 | ancestors = ancestorsArg;
|
407 | done();
|
408 | });
|
409 | });
|
410 | it('correct count', function() {
|
411 | assert(ancestors.length === 2);
|
412 | });
|
413 | it('correct paths in order', function() {
|
414 | assert(ancestors[0]._id === 'home');
|
415 | assert(ancestors[1]._id === 'about');
|
416 | });
|
417 | it('did not return areas', function() {
|
418 | assert(!ancestors[0].body);
|
419 | assert(!ancestors[1].body);
|
420 | assert(!ancestors[0].sidebar);
|
421 | assert(!ancestors[1].sidebar);
|
422 | });
|
423 | it('fetch again with areas turned on', function(done) {
|
424 | var people = children[0].children[0];
|
425 | pages.getAncestors(req, people, { areas: true }, function(err, ancestorsArg) {
|
426 | assert(!err);
|
427 | assert(ancestorsArg);
|
428 | ancestors = ancestorsArg;
|
429 | done();
|
430 | });
|
431 | });
|
432 | it('did return all areas', function() {
|
433 | assert(ancestors[0].body);
|
434 | assert(ancestors[1].body);
|
435 | assert(ancestors[0].sidebar);
|
436 | assert(ancestors[1].sidebar);
|
437 | });
|
438 | it('fetch again with specific area', function(done) {
|
439 | var people = children[0].children[0];
|
440 | pages.getAncestors(req, people, { areas: [ 'body' ] }, function(err, ancestorsArg) {
|
441 | assert(!err);
|
442 | assert(ancestorsArg);
|
443 | ancestors = ancestorsArg;
|
444 | done();
|
445 | });
|
446 | });
|
447 | it('returned body area', function() {
|
448 | assert(ancestors[0].body);
|
449 | assert(ancestors[1].body);
|
450 | });
|
451 | it('did not return sidebar area', function() {
|
452 | assert(!ancestors[0].sidebar);
|
453 | assert(!ancestors[1].sidebar);
|
454 | });
|
455 | it('did not return children', function() {
|
456 | assert(!ancestors[0].children);
|
457 | assert(!ancestors[1].children);
|
458 | });
|
459 | it('fetch again with children', function(done) {
|
460 | var people = children[0].children[0];
|
461 | pages.getAncestors(req, people, { children: true }, function(err, ancestorsArg) {
|
462 | assert(!err);
|
463 | assert(ancestorsArg);
|
464 | ancestors = ancestorsArg;
|
465 | done();
|
466 | });
|
467 | });
|
468 | it('did return children arrays', function() {
|
469 | assert(Array.isArray(ancestors[0].children));
|
470 | assert(Array.isArray(ancestors[1].children));
|
471 | });
|
472 | it('children arrays are correct', function() {
|
473 | assert(ancestors[0].children.length === 3);
|
474 | });
|
475 | });
|
476 |
|
477 | describe('getParent returns home/about for home/about/people', function() {
|
478 | it('returned', function(done) {
|
479 | var people = children[0].children[0];
|
480 | pages.getParent(req, people, function(err, parent) {
|
481 | assert(!err);
|
482 | assert(parent);
|
483 | assert(parent._id === 'about');
|
484 | about = parent;
|
485 | return done();
|
486 | });
|
487 | });
|
488 | });
|
489 |
|
490 | describe('move home/about/people after home/contact', function() {
|
491 | var people;
|
492 | it('people exists', function(done) {
|
493 | people = children[0].children[0];
|
494 | assert(people._id === 'people');
|
495 | done();
|
496 | });
|
497 | it('moved without error', function(done) {
|
498 | pages.move(req, people, '/contact', 'after', function(err) {
|
499 | if (err) {
|
500 | console.log(err);
|
501 | }
|
502 | assert(!err);
|
503 | return done();
|
504 | });
|
505 | });
|
506 | it('home has 4 descendants', function(done) {
|
507 | pages.getDescendants(req, home, { depth: 1 }, function(err, childrenArg) {
|
508 | children = childrenArg;
|
509 | assert(children.length === 4);
|
510 | done();
|
511 | });
|
512 | });
|
513 | it('people is now the final child of home', function(done) {
|
514 | assert(children[3]._id === 'people');
|
515 | return done();
|
516 | });
|
517 | it('slug of people is now /people', function(done) {
|
518 | assert(children[3].slug === '/people');
|
519 | return done();
|
520 | });
|
521 | });
|
522 |
|
523 | describe('move home/people back under home/about as first child', function() {
|
524 | var people;
|
525 | it('people exists', function(done) {
|
526 | people = children[3];
|
527 | assert(people._id === 'people');
|
528 | done();
|
529 | });
|
530 | it('moved without error', function(done) {
|
531 | pages.move(req, people, '/about', 'inside', function(err) {
|
532 | if (err) {
|
533 | console.log(err);
|
534 | }
|
535 | assert(!err);
|
536 | return done();
|
537 | });
|
538 | });
|
539 | it('home/about has 3 descendants', function(done) {
|
540 | pages.getDescendants(req, about, { depth: 1 }, function(err, childrenArg) {
|
541 | children = childrenArg;
|
542 | assert(children.length === 3);
|
543 | done();
|
544 | });
|
545 | });
|
546 | it('first child of home/about is now people', function(done) {
|
547 | assert(children[0]._id === 'people');
|
548 | return done();
|
549 | });
|
550 | it('people is at /about/people', function(done) {
|
551 | assert(children[0].slug === '/about/people');
|
552 | return done();
|
553 | });
|
554 | });
|
555 |
|
556 | describe('move home/about under home/contact, by slug', function() {
|
557 | var location;
|
558 | it('moved without error', function(done) {
|
559 | pages.move(req, '/about', '/contact', 'inside', function(err) {
|
560 | if (err) {
|
561 | console.log(err);
|
562 | }
|
563 | assert(!err);
|
564 | return done();
|
565 | });
|
566 | });
|
567 | it('got contact', function(done) {
|
568 | apos.pages.findOne({ slug: '/contact' }, function(err, page) {
|
569 | contact = page;
|
570 | assert(page);
|
571 | return done();
|
572 | });
|
573 | });
|
574 | it('home/contact has 1 child', function(done) {
|
575 | pages.getDescendants(req, contact, { depth: 2 }, function(err, childrenArg) {
|
576 | children = childrenArg;
|
577 | assert(children.length === 1);
|
578 | done();
|
579 | });
|
580 | });
|
581 | it('home/contact/about/location exists at the right path', function(done) {
|
582 | apos.pages.findOne({ _id: 'location', path: 'home/contact/about/location' }, function(err, page) {
|
583 | location = page;
|
584 | assert(location);
|
585 | return done();
|
586 | });
|
587 | });
|
588 | it('home/contact/about/location has level 3', function(done) {
|
589 | assert(location.level === 3);
|
590 | return done();
|
591 | });
|
592 | it('home/contact/about/location has slug /contact/about/location', function(done) {
|
593 | assert(location.slug === '/contact/about/location');
|
594 | return done();
|
595 | });
|
596 | });
|
597 |
|
598 | describe('fetch pages by tag', function() {
|
599 | var fetched;
|
600 | it('fetched without error', function(done) {
|
601 | pages.getByTag(req, 'green', function(err, fetchedArg) {
|
602 | if (err) {
|
603 | console.log(err);
|
604 | }
|
605 | assert(!err);
|
606 | fetched = fetchedArg.pages;
|
607 | return done();
|
608 | });
|
609 | });
|
610 | it('fetched three pages', function(done) {
|
611 | assert(fetched.length === 3);
|
612 | return done();
|
613 | });
|
614 | it('first one must be about due to title order', function(done) {
|
615 | assert(fetched[0]._id === 'about');
|
616 | return done();
|
617 | });
|
618 | it('filterByTag returns only contact for "red"', function(done) {
|
619 | var filtered = pages.filterByTag(fetched, 'red');
|
620 | assert(filtered.length === 1);
|
621 | assert(filtered[0]._id === 'contact');
|
622 | return done();
|
623 | });
|
624 | });
|
625 |
|
626 | describe('add page', function() {
|
627 | it('adds a new page beneath /contact called /contact/new-kid', function(done) {
|
628 | var req = apos.getTaskReq();
|
629 | req.body = {
|
630 | parent: '/contact',
|
631 | title: 'New Kid',
|
632 | published: true,
|
633 | tags: [ 'one', 'two' ],
|
634 | type: 'default',
|
635 | body: {
|
636 | type: 'area',
|
637 | items: [
|
638 | {
|
639 | type: 'richText',
|
640 | content: 'This is a test'
|
641 | }
|
642 | ]
|
643 | }
|
644 | };
|
645 | var res = {
|
646 | send: function(data) {
|
647 | assert((!res.statusCode) || (res.statusCode === 200));
|
648 | var page = JSON.parse(data);
|
649 | assert(page.body.items.length > 0);
|
650 | assert(typeof(page) === 'object');
|
651 | assert(page.slug === '/contact/new-kid');
|
652 | return apos.getPage(req, '/contact', function(err, page) {
|
653 | assert(!err);
|
654 | assert(page);
|
655 | assert(page.slug === '/contact');
|
656 | return pages.getDescendants(apos.getTaskReq(), page, { depth: 2 }, function(err, children) {
|
657 | assert(children.length === 2);
|
658 | assert(children[0].slug === '/contact/about');
|
659 | assert(children[1].slug === '/contact/new-kid');
|
660 | assert(children[1].path === 'home/contact/new-kid');
|
661 | return done();
|
662 | });
|
663 | });
|
664 | }
|
665 | };
|
666 | return pages._newRoute(req, res);
|
667 | });
|
668 | });
|
669 |
|
670 | describe('edit page settings', function() {
|
671 | it('propagates slug changes to children properly', function(done) {
|
672 | var req = apos.getTaskReq();
|
673 | req.body = {
|
674 | originalSlug: '/contact/about',
|
675 | slug: '/contact/about2',
|
676 | title: 'About2',
|
677 | published: true,
|
678 | tags: [ 'one', 'two' ],
|
679 | type: 'default'
|
680 | };
|
681 | var res = {
|
682 | send: function(data) {
|
683 | assert((!res.statusCode) || (res.statusCode === 200));
|
684 | var page = JSON.parse(data);
|
685 | assert(typeof(page) === 'object');
|
686 | assert(page.slug === '/contact/about2');
|
687 | return pages.getDescendants(apos.getTaskReq(), page, { depth: 2 }, function(err, childrenArg) {
|
688 | children = childrenArg;
|
689 | assert(!err);
|
690 | assert(children.length === 3);
|
691 | assert(children[0]._id === 'people');
|
692 | assert(children[0].slug === '/contact/about2/people');
|
693 | assert(children[1].slug === '/contact/about2/location');
|
694 | assert(children[2].slug === '/contact/about2/friends');
|
695 | return done();
|
696 | });
|
697 | }
|
698 | };
|
699 | return pages._editRoute(req, res);
|
700 | });
|
701 | it('retains children when avoiding a duplicate slug error', function(done) {
|
702 | var req = apos.getTaskReq();
|
703 | req.body = {
|
704 | originalSlug: '/contact/about2',
|
705 | slug: '/contact/new-kid',
|
706 | title: 'About2',
|
707 | published: true,
|
708 | tags: [ 'one', 'two' ],
|
709 | type: 'default'
|
710 | };
|
711 | var res = {
|
712 | send: function(data) {
|
713 | assert((!res.statusCode) || (res.statusCode === 200));
|
714 | var page = JSON.parse(data);
|
715 | assert(typeof(page) === 'object');
|
716 | assert(page.slug.match(/^\/contact\/new\-kid\d$/));
|
717 | var baseSlug = page.slug;
|
718 | return pages.getDescendants(apos.getTaskReq(), page, { depth: 2 }, function(err, childrenArg) {
|
719 | children = childrenArg;
|
720 | assert(!err);
|
721 | assert(children.length === 3);
|
722 | assert(children[0]._id === 'people');
|
723 | assert(children[0].slug === baseSlug + '/people');
|
724 | assert(children[1].slug === baseSlug + '/location');
|
725 | assert(children[2].slug === baseSlug + '/friends');
|
726 | return done();
|
727 | });
|
728 | }
|
729 | };
|
730 | return pages._editRoute(req, res);
|
731 | }); });
|
732 | });
|
733 |
|