Commit bfe5d42b authored by Robert Knight's avatar Robert Knight

Refactor `getMetadata` tests

 - Convert promise chains to async/await

 - Add missing tests to check that the PDF URL and fingerprint URL
   appear in the object returned by `getMetadata`. These were previously
   tested indirectly in other tests.

 - Change tests to only check the specific properties of the returned
   object that are of interest. This reduces the changes needed when
   tests change unrelated parts of the output.
parent 1b539b87
......@@ -208,123 +208,111 @@ describe('PDFMetadata', function () {
});
});
describe('metadata sources', function () {
const testMetadata = {
fingerprint: 'fakeFingerprint',
title: 'fakeTitle',
metadata: {
'dc:title': 'dcFakeTitle',
},
url: 'http://fake.com/',
const testMetadata = {
fingerprint: 'fakeFingerprint',
title: 'fakeTitle',
metadata: {
'dc:title': 'dcFakeTitle',
},
url: 'http://fake.com/',
};
function createPDFMetadata(metadata = testMetadata) {
const fakePDFViewerApplication = new FakePDFViewerApplication();
fakePDFViewerApplication.completeInit();
fakePDFViewerApplication.finishLoading(metadata);
return {
fakePDFViewerApplication,
pdfMetadata: new PDFMetadata(fakePDFViewerApplication),
};
}
function createPDFMetadata(metadata = testMetadata) {
const fakePDFViewerApplication = new FakePDFViewerApplication();
fakePDFViewerApplication.completeInit();
fakePDFViewerApplication.finishLoading(metadata);
return {
fakePDFViewerApplication,
pdfMetadata: new PDFMetadata(fakePDFViewerApplication),
};
}
describe('#getUri', function () {
it('returns the non-file URI', function () {
const { pdfMetadata } = createPDFMetadata();
return pdfMetadata.getUri().then(function (uri) {
assert.equal(uri, 'http://fake.com/');
});
});
describe('#getUri', function () {
it('returns the non-file URI', function () {
const { pdfMetadata } = createPDFMetadata();
return pdfMetadata.getUri().then(function (uri) {
assert.equal(uri, 'http://fake.com/');
});
it('returns the fingerprint as a URN when the PDF URL is a local file', function () {
const { pdfMetadata } = createPDFMetadata({
url: 'file:///test.pdf',
fingerprint: 'fakeFingerprint',
});
return pdfMetadata.getUri().then(function (uri) {
assert.equal(uri, 'urn:x-pdf:fakeFingerprint');
});
});
it('returns the fingerprint as a URN when the PDF URL is a local file', function () {
const { pdfMetadata } = createPDFMetadata({
url: 'file:///test.pdf',
fingerprint: 'fakeFingerprint',
});
return pdfMetadata.getUri().then(function (uri) {
assert.equal(uri, 'urn:x-pdf:fakeFingerprint');
});
it('resolves relative URLs', () => {
const { fakePDFViewerApplication, pdfMetadata } = createPDFMetadata({
url: 'index.php?action=download&file_id=wibble',
fingerprint: 'fakeFingerprint',
});
it('resolves relative URLs', () => {
const { fakePDFViewerApplication, pdfMetadata } = createPDFMetadata({
url: 'index.php?action=download&file_id=wibble',
fingerprint: 'fakeFingerprint',
});
return pdfMetadata.getUri().then(uri => {
const expected = new URL(
fakePDFViewerApplication.url,
document.location.href
).toString();
assert.equal(uri, expected);
});
});
});
return pdfMetadata.getUri().then(uri => {
const expected = new URL(
fakePDFViewerApplication.url,
document.location.href
).toString();
assert.equal(uri, expected);
});
describe('#getMetadata', () => {
it('returns the document fingerprint in the `documentFingerprint` property', async () => {
const { pdfMetadata } = createPDFMetadata();
const metadata = await pdfMetadata.getMetadata();
assert.equal(metadata.documentFingerprint, testMetadata.fingerprint);
});
it('returns the PDF URL in the `links` array', async () => {
const { pdfMetadata } = createPDFMetadata();
const metadata = await pdfMetadata.getMetadata();
assert.deepInclude(metadata.link, {
href: testMetadata.url,
});
});
describe('#getMetadata', function () {
it('gets the title from the dc:title field', function () {
const { fakePDFViewerApplication, pdfMetadata } = createPDFMetadata();
const expectedMetadata = {
title: 'dcFakeTitle',
link: [
{
href:
'urn:x-pdf:' + fakePDFViewerApplication.pdfDocument.fingerprint,
},
{ href: fakePDFViewerApplication.url },
],
documentFingerprint: fakePDFViewerApplication.pdfDocument.fingerprint,
};
return pdfMetadata.getMetadata().then(function (actualMetadata) {
assert.deepEqual(actualMetadata, expectedMetadata);
});
it('returns the document fingerprint in the `links` array', async () => {
const { pdfMetadata } = createPDFMetadata();
const metadata = await pdfMetadata.getMetadata();
assert.deepInclude(metadata.link, {
href: `urn:x-pdf:${testMetadata.fingerprint}`,
});
});
it('gets the title from the documentInfo.Title field', function () {
const { fakePDFViewerApplication, pdfMetadata } = createPDFMetadata();
const expectedMetadata = {
title: fakePDFViewerApplication.documentInfo.Title,
link: [
{
href:
'urn:x-pdf:' + fakePDFViewerApplication.pdfDocument.fingerprint,
},
{ href: fakePDFViewerApplication.url },
],
documentFingerprint: fakePDFViewerApplication.pdfDocument.fingerprint,
};
fakePDFViewerApplication.metadata.has = sinon.stub().returns(false);
return pdfMetadata.getMetadata().then(function (actualMetadata) {
assert.deepEqual(actualMetadata, expectedMetadata);
});
it('does not return file:// URLs in `links` array', async () => {
const { pdfMetadata } = createPDFMetadata({
fingerprint: 'fakeFingerprint',
url: 'file://fake.pdf',
});
it('does not save file:// URLs in document metadata', function () {
const { fakePDFViewerApplication, pdfMetadata } = createPDFMetadata({
fingerprint: 'fakeFingerprint',
url: 'file://fake.pdf',
});
const expectedMetadata = {
link: [
{
href:
'urn:x-pdf:' + fakePDFViewerApplication.pdfDocument.fingerprint,
},
],
};
return pdfMetadata.getMetadata().then(function (actualMetadata) {
assert.equal(actualMetadata.link.length, 1);
assert.equal(
actualMetadata.link[0].href,
expectedMetadata.link[0].href
);
});
const metadata = await pdfMetadata.getMetadata();
const fileLink = metadata.link.find(link =>
link.href.includes('file://')
);
assert.isUndefined(fileLink);
});
it('gets the title from the dc:title field', async () => {
const { pdfMetadata } = createPDFMetadata();
const metadata = await pdfMetadata.getMetadata();
assert.equal(metadata.title, testMetadata.metadata['dc:title']);
});
it('gets the title from the documentInfo.Title field', async () => {
const { pdfMetadata } = createPDFMetadata({
title: 'Some title',
url: 'http://fake.com/',
});
const metadata = await pdfMetadata.getMetadata();
assert.equal(metadata.title, 'Some title');
});
});
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment