1 | const assert = require('assert');
|
2 | const fs = require('fs');
|
3 | const get = require('./lib/get');
|
4 |
|
5 | describe('Apostrophe Sitemap: test rewrite URL', function() {
|
6 |
|
7 | let apos;
|
8 |
|
9 | this.timeout(5000);
|
10 |
|
11 | before(function(done) {
|
12 | apos = require('apostrophe')({
|
13 | testModule: true,
|
14 | baseUrl: 'http://localhost:7780',
|
15 | modules: {
|
16 | 'apostrophe-express': {
|
17 | port: 7780
|
18 | },
|
19 | 'apostrophe-site-map': {
|
20 | construct(self, options) {
|
21 | self.rewriteUrl = url => {
|
22 | return url.replace('http://localhost:7780', 'https://public-site.com');
|
23 | }
|
24 | }
|
25 | },
|
26 | 'apostrophe-pages': {
|
27 | park: [
|
28 | {
|
29 | title: 'Tab One',
|
30 | type: 'default',
|
31 | slug: '/tab-one',
|
32 | _children: [
|
33 | {
|
34 | title: 'Tab One Child One',
|
35 | type: 'default',
|
36 | slug: '/tab-one/child-one'
|
37 | },
|
38 | {
|
39 | title: 'Tab One Child Two',
|
40 | type: 'default',
|
41 | slug: '/tab-one/child-two'
|
42 | },
|
43 | ]
|
44 | },
|
45 | {
|
46 | title: 'Tab Two',
|
47 | type: 'default',
|
48 | slug: '/tab-two',
|
49 | _children: [
|
50 | {
|
51 | title: 'Tab Two Child One',
|
52 | type: 'default',
|
53 | slug: '/tab-two/child-one'
|
54 | },
|
55 | {
|
56 | title: 'Tab Two Child Two',
|
57 | type: 'default',
|
58 | slug: '/tab-two/child-two'
|
59 | },
|
60 | ]
|
61 | },
|
62 | {
|
63 | title: 'Products',
|
64 | type: 'products-page',
|
65 | slug: '/products'
|
66 | }
|
67 | ],
|
68 | types: [
|
69 | {
|
70 | name: 'home',
|
71 | label: 'Home'
|
72 | },
|
73 | {
|
74 | name: 'default',
|
75 | label: 'Default'
|
76 | },
|
77 | {
|
78 | name: 'products',
|
79 | label: 'Products'
|
80 | }
|
81 | ]
|
82 | },
|
83 | 'products': {
|
84 | extend: 'apostrophe-pieces',
|
85 | name: 'product'
|
86 | },
|
87 | 'products-pages': {
|
88 | extend: 'apostrophe-pieces-pages'
|
89 | },
|
90 | },
|
91 | afterInit: async function(callback) {
|
92 | assert(apos.modules['apostrophe-site-map']);
|
93 | try {
|
94 | const product = Object.assign(apos.modules.products.newInstance(), {
|
95 | title: 'Cheese',
|
96 | slug: 'cheese'
|
97 | });
|
98 | await apos.modules.products.insert(apos.tasks.getReq(), product);
|
99 | const product2 = Object.assign(apos.modules.products.newInstance(), {
|
100 | title: 'Rocks',
|
101 | slug: 'rocks',
|
102 | published: false
|
103 | });
|
104 | await apos.modules.products.insert(apos.tasks.getReq(), product2);
|
105 | await apos.docs.db.update({}, {
|
106 | $set: {
|
107 | trash: false,
|
108 | published: true
|
109 | }
|
110 | }, {
|
111 | multi: true
|
112 | });
|
113 | return callback(null);
|
114 | } catch (e) {
|
115 | return callback(e);
|
116 | }
|
117 | },
|
118 | afterListen: function(err) {
|
119 | done();
|
120 | }
|
121 | });
|
122 | });
|
123 |
|
124 | after(function(done) {
|
125 | try {
|
126 | require('apostrophe/test-lib/util').destroy(apos, done);
|
127 | } catch (e) {
|
128 | console.warn('Old version of apostrophe does not export test-lib/util library, just dropping old test db');
|
129 | apos.db.dropDatabase();
|
130 | setTimeout(done, 1000);
|
131 | }
|
132 | });
|
133 |
|
134 | it('should generate a suitable sitemap', async function() {
|
135 | this.timeout(10000);
|
136 | const xml = await get('http://localhost:7780/sitemap.xml');
|
137 | assert(xml);
|
138 | assert(xml.includes('<loc>https://public-site.com/</loc>'));
|
139 | assert(xml.includes('<loc>https://public-site.com/tab-one</loc>'));
|
140 | assert(xml.includes('<loc>https://public-site.com/tab-two</loc>'));
|
141 | assert(xml.includes('<loc>https://public-site.com/tab-one/child-one</loc>'));
|
142 | assert(xml.includes('<loc>https://public-site.com/products/cheese</loc>'));
|
143 | assert(xml.includes('<loc>https://public-site.com/products/rocks</loc>'));
|
144 | });
|
145 |
|
146 | });
|