|
| 1 | +import { expect } from 'chai'; |
| 2 | +import config from 'config'; |
| 3 | +import supertest from 'supertest'; |
| 4 | + |
| 5 | +import { getCollection } from '../../archivist/collection/index.js'; |
| 6 | +import RepositoryFactory from '../../archivist/recorder/repositories/factory.js'; |
| 7 | +import Version from '../../archivist/recorder/version.js'; |
| 8 | +import { toISODateWithoutMilliseconds } from '../../archivist/utils/date.js'; |
| 9 | +import app from '../server.js'; |
| 10 | + |
| 11 | +const basePath = config.get('@opentermsarchive/engine.collection-api.basePath'); |
| 12 | +const request = supertest(app); |
| 13 | +const storageConfig = config.get('@opentermsarchive/engine.recorder.versions.storage'); |
| 14 | + |
| 15 | +function extractTag(xml, tag) { |
| 16 | + const match = xml.match(new RegExp(`<${tag}>([\\s\\S]*?)</${tag}>`)); |
| 17 | + |
| 18 | + return match ? match[1] : null; |
| 19 | +} |
| 20 | + |
| 21 | +describe('Feed API', () => { |
| 22 | + describe('GET /feed', () => { |
| 23 | + let response; |
| 24 | + let collection; |
| 25 | + |
| 26 | + before(async () => { |
| 27 | + collection = await getCollection(); |
| 28 | + response = await request.get(`${basePath}/v1/feed`); |
| 29 | + }); |
| 30 | + |
| 31 | + it('responds with 200 status code', () => { |
| 32 | + expect(response.status).to.equal(200); |
| 33 | + }); |
| 34 | + |
| 35 | + it('responds with Content-Type application/atom+xml', () => { |
| 36 | + expect(response.headers['content-type']).to.match(/^application\/atom\+xml/); |
| 37 | + }); |
| 38 | + |
| 39 | + it('is a valid Atom feed root', () => { |
| 40 | + expect(response.text).to.match(/^<\?xml version="1\.0"/); |
| 41 | + expect(response.text).to.include('<feed'); |
| 42 | + expect(response.text).to.include('xmlns="http://www.w3.org/2005/Atom"'); |
| 43 | + }); |
| 44 | + |
| 45 | + describe('feed-level metadata', () => { |
| 46 | + it('has a title matching the collection name', () => { |
| 47 | + expect(extractTag(response.text, 'title')).to.equal(collection.metadata.name); |
| 48 | + }); |
| 49 | + |
| 50 | + it('has a subtitle matching the collection tagline', () => { |
| 51 | + expect(extractTag(response.text, 'subtitle')).to.equal(collection.metadata.tagline); |
| 52 | + }); |
| 53 | + |
| 54 | + it('has a tag URI id based on the collection id', () => { |
| 55 | + expect(extractTag(response.text, 'id')).to.equal(`tag:opentermsarchive.org,2026:feed:${collection.metadata.id}`); |
| 56 | + }); |
| 57 | + |
| 58 | + it('has an updated element with a valid ISO 8601 datetime', () => { |
| 59 | + const updated = extractTag(response.text, 'updated'); |
| 60 | + |
| 61 | + expect(updated).to.be.a('string'); |
| 62 | + expect(new Date(updated).toString()).to.not.equal('Invalid Date'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('has a self link pointing to the feed endpoint', () => { |
| 66 | + const selfHrefMatch = response.text.match(/<link[^>]*rel="self"[^>]*href="([^"]+)"/); |
| 67 | + |
| 68 | + expect(selfHrefMatch).to.not.be.null; |
| 69 | + expect(selfHrefMatch[1]).to.match(new RegExp(`${basePath}/v1/feed$`)); |
| 70 | + }); |
| 71 | + |
| 72 | + it('has an author named OTA-Bot', () => { |
| 73 | + expect(response.text).to.match(/<author>[\s\S]*<name>OTA-Bot<\/name>[\s\S]*<\/author>/); |
| 74 | + }); |
| 75 | + |
| 76 | + it('has a logo matching the collection logo', () => { |
| 77 | + expect(extractTag(response.text, 'logo')).to.equal(collection.metadata.logo); |
| 78 | + }); |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + describe('GET /feed — entries', () => { |
| 83 | + const FETCH_DATE_FIRST = new Date('2023-01-01T12:00:00Z'); |
| 84 | + const FETCH_DATE_CHANGE = new Date('2023-06-15T08:30:00Z'); |
| 85 | + const FETCH_DATE_UPGRADE = new Date('2024-02-10T16:45:00Z'); |
| 86 | + |
| 87 | + let response; |
| 88 | + let repository; |
| 89 | + let savedVersions; |
| 90 | + |
| 91 | + before(async function () { |
| 92 | + this.timeout(5000); |
| 93 | + repository = RepositoryFactory.create(storageConfig); |
| 94 | + await repository.initialize(); |
| 95 | + |
| 96 | + const firstRecord = await repository.save(new Version({ |
| 97 | + serviceId: 'service-1', |
| 98 | + termsType: 'Terms of Service', |
| 99 | + content: 'first content', |
| 100 | + fetchDate: FETCH_DATE_FIRST, |
| 101 | + snapshotIds: ['snapshot_1'], |
| 102 | + })); |
| 103 | + |
| 104 | + const changeRecord = await repository.save(new Version({ |
| 105 | + serviceId: 'service-1', |
| 106 | + termsType: 'Terms of Service', |
| 107 | + content: 'changed content', |
| 108 | + fetchDate: FETCH_DATE_CHANGE, |
| 109 | + snapshotIds: ['snapshot_2'], |
| 110 | + })); |
| 111 | + |
| 112 | + const upgradeRecord = await repository.save(new Version({ |
| 113 | + serviceId: 'service-2', |
| 114 | + termsType: 'Privacy Policy', |
| 115 | + content: 'initial privacy', |
| 116 | + fetchDate: new Date('2024-01-01T00:00:00Z'), |
| 117 | + snapshotIds: ['snapshot_3'], |
| 118 | + })); |
| 119 | + |
| 120 | + const technicalUpgradeRecord = await repository.save(new Version({ |
| 121 | + serviceId: 'service-2', |
| 122 | + termsType: 'Privacy Policy', |
| 123 | + content: 'upgraded privacy', |
| 124 | + fetchDate: FETCH_DATE_UPGRADE, |
| 125 | + snapshotIds: ['snapshot_4'], |
| 126 | + isTechnicalUpgrade: true, |
| 127 | + })); |
| 128 | + |
| 129 | + savedVersions = { firstRecord, changeRecord, upgradeRecord, technicalUpgradeRecord }; |
| 130 | + response = await request.get(`${basePath}/v1/feed`); |
| 131 | + }); |
| 132 | + |
| 133 | + after(() => repository.removeAll()); |
| 134 | + |
| 135 | + it('orders entries newest-first', () => { |
| 136 | + const updates = [...response.text.matchAll(/<entry>[\s\S]*?<updated>([^<]+)<\/updated>[\s\S]*?<\/entry>/g)].map(match => match[1]); |
| 137 | + |
| 138 | + expect(updates).to.deep.equal([...updates].sort().reverse()); |
| 139 | + }); |
| 140 | + |
| 141 | + describe('entry metadata', () => { |
| 142 | + let firstEntry; |
| 143 | + |
| 144 | + before(() => { |
| 145 | + firstEntry = response.text.match(/<entry>[\s\S]*?<\/entry>/)[0]; |
| 146 | + }); |
| 147 | + |
| 148 | + it('has an id tag URI including storage type and record id', () => { |
| 149 | + const collectionId = 'test'; |
| 150 | + const expected = `tag:opentermsarchive.org,2026:version:${collectionId}:${storageConfig.type}:${savedVersions.technicalUpgradeRecord.id}`; |
| 151 | + |
| 152 | + expect(firstEntry).to.include(`<id>${expected}</id>`); |
| 153 | + }); |
| 154 | + |
| 155 | + it('has an alternate link to the version API endpoint', () => { |
| 156 | + const href = firstEntry.match(/<link[^>]*rel="alternate"[^>]*href="([^"]+)"/)[1]; |
| 157 | + const expectedPathFragment = `/version/${encodeURIComponent('service-2')}/${encodeURIComponent('Privacy Policy')}/${encodeURIComponent(toISODateWithoutMilliseconds(FETCH_DATE_UPGRADE))}`; |
| 158 | + |
| 159 | + expect(href).to.include(expectedPathFragment); |
| 160 | + }); |
| 161 | + |
| 162 | + it('has a type="text/html" on the alternate link', () => { |
| 163 | + expect(firstEntry).to.match(/<link[^>]*rel="alternate"[^>]*type="text\/html"/); |
| 164 | + }); |
| 165 | + |
| 166 | + it('has a title reconstructed from commit prefix + serviceId + termsType', () => { |
| 167 | + const title = firstEntry.match(/<title[^>]*>([\s\S]*?)<\/title>/)[1]; |
| 168 | + |
| 169 | + expect(title).to.include('Apply technical or declaration upgrade on'); |
| 170 | + expect(title).to.include('service-2'); |
| 171 | + expect(title).to.include('Privacy Policy'); |
| 172 | + }); |
| 173 | + |
| 174 | + it('has an updated element matching the fetch date', () => { |
| 175 | + const updated = firstEntry.match(/<updated>([^<]+)<\/updated>/)[1]; |
| 176 | + |
| 177 | + expect(new Date(updated).toISOString()).to.equal(FETCH_DATE_UPGRADE.toISOString()); |
| 178 | + }); |
| 179 | + |
| 180 | + it('has three categories with the expected schemes', () => { |
| 181 | + const categories = [...firstEntry.matchAll(/<category([^/]*)\/>/g)].map(match => match[1]); |
| 182 | + |
| 183 | + expect(categories).to.have.length(3); |
| 184 | + |
| 185 | + const schemes = categories.map(attrs => attrs.match(/scheme="([^"]+)"/)[1]); |
| 186 | + |
| 187 | + expect(schemes).to.include('tag:opentermsarchive.org,2026:scheme:service'); |
| 188 | + expect(schemes).to.include('tag:opentermsarchive.org,2026:scheme:terms-type'); |
| 189 | + expect(schemes).to.include('tag:opentermsarchive.org,2026:scheme:record-type'); |
| 190 | + }); |
| 191 | + |
| 192 | + it('has category terms for service, terms type and record type', () => { |
| 193 | + const categories = [...firstEntry.matchAll(/<category([^/]*)\/>/g)].map(match => match[1]); |
| 194 | + const terms = categories.map(attrs => attrs.match(/term="([^"]+)"/)[1]); |
| 195 | + |
| 196 | + expect(terms).to.include('service-2'); |
| 197 | + expect(terms).to.include('Privacy Policy'); |
| 198 | + expect(terms).to.include('Technical upgrade'); |
| 199 | + }); |
| 200 | + }); |
| 201 | + |
| 202 | + describe('record-type classification', () => { |
| 203 | + function findEntryById(xml, recordId) { |
| 204 | + const match = [...xml.matchAll(/<entry>[\s\S]*?<\/entry>/g)].find(entry => entry[0].includes(`:${recordId}</id>`)); |
| 205 | + |
| 206 | + return match && match[0]; |
| 207 | + } |
| 208 | + |
| 209 | + it('classifies a first record as "First record"', () => { |
| 210 | + const entry = findEntryById(response.text, savedVersions.upgradeRecord.id); |
| 211 | + |
| 212 | + expect(entry).to.not.be.undefined; |
| 213 | + expect(entry).to.match(/term="First record"/); |
| 214 | + }); |
| 215 | + |
| 216 | + it('classifies a content change as "Change"', () => { |
| 217 | + const entry = findEntryById(response.text, savedVersions.changeRecord.id); |
| 218 | + |
| 219 | + expect(entry).to.not.be.undefined; |
| 220 | + expect(entry).to.match(/term="Change"/); |
| 221 | + }); |
| 222 | + |
| 223 | + it('classifies a technical upgrade as "Technical upgrade"', () => { |
| 224 | + const entry = findEntryById(response.text, savedVersions.technicalUpgradeRecord.id); |
| 225 | + |
| 226 | + expect(entry).to.not.be.undefined; |
| 227 | + expect(entry).to.match(/term="Technical upgrade"/); |
| 228 | + }); |
| 229 | + }); |
| 230 | + }); |
| 231 | +}); |
0 commit comments