Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor PDFMetadata tests #3373

Merged
merged 4 commits into from
May 11, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
206 changes: 97 additions & 109 deletions src/annotator/integrations/test/pdf-metadata-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ describe('PDFMetadata', function () {
// The `initializedPromise` param simulates different versions of PDF.js with
// and without the `PDFViewerApplication.initializedPromise` API.
[true, false].forEach(initializedPromise => {
it('does not wait for the PDF to load if it has already loaded', function () {
it('does not wait for the PDF to load if it has already loaded', async () => {
const fakePDFViewerApplication = new FakePDFViewerApplication('', {
initializedPromise,
});
Expand All @@ -202,135 +202,123 @@ describe('PDFMetadata', function () {
fingerprint: 'fakeFingerprint',
});
const pdfMetadata = new PDFMetadata(fakePDFViewerApplication);
return pdfMetadata.getUri().then(function (uri) {
assert.equal(uri, 'http://fake.com/');
});
const uri = await pdfMetadata.getUri();
assert.equal(uri, 'http://fake.com/');
});
});

describe('metadata sources', function () {
let pdfMetadata;
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({
fingerprint: 'fakeFingerprint',
title: 'fakeTitle',
metadata: {
'dc:title': 'dcFakeTitle',
},
url: 'http://fake.com/',
fakePDFViewerApplication.finishLoading(metadata);
return {
fakePDFViewerApplication,
pdfMetadata: new PDFMetadata(fakePDFViewerApplication),
};
}

describe('#getUri', () => {
['http://fake.com/', 'https://example.com/test.pdf'].forEach(pdfURL => {
it('returns the PDF URL if it is an HTTP(S) URL', async () => {
const { pdfMetadata } = createPDFMetadata({ url: pdfURL });
const uri = await pdfMetadata.getUri();
assert.equal(uri, pdfURL);
});
});

beforeEach(function () {
pdfMetadata = new PDFMetadata(fakePDFViewerApplication);
it('returns the fingerprint as a URN when the PDF URL is a file:// URL', async () => {
const { pdfMetadata } = createPDFMetadata({
url: 'file:///test.pdf',
fingerprint: 'fakeFingerprint',
});
const uri = await pdfMetadata.getUri();
assert.equal(uri, 'urn:x-pdf:fakeFingerprint');
});

describe('#getUri', function () {
it('returns the non-file URI', function () {
return pdfMetadata.getUri().then(function (uri) {
assert.equal(uri, 'http://fake.com/');
});
it('resolves relative URLs', async () => {
const { fakePDFViewerApplication, pdfMetadata } = createPDFMetadata({
url: 'index.php?action=download&file_id=wibble',
fingerprint: 'fakeFingerprint',
});

it('returns the fingerprint as a URN when the PDF URL is a local file', function () {
const fakePDFViewerApplication = new FakePDFViewerApplication();
fakePDFViewerApplication.completeInit();
fakePDFViewerApplication.finishLoading({
url: 'file:///test.pdf',
fingerprint: 'fakeFingerprint',
});
const pdfMetadata = new PDFMetadata(fakePDFViewerApplication);
const uri = await pdfMetadata.getUri();

return pdfMetadata.getUri().then(function (uri) {
assert.equal(uri, 'urn:x-pdf:fakeFingerprint');
});
});
const expected = new URL(
fakePDFViewerApplication.url,
document.location.href
).toString();
assert.equal(uri, expected);
});
});

it('resolves relative URLs', () => {
const fakePDFViewerApplication = new FakePDFViewerApplication();
fakePDFViewerApplication.completeInit();
fakePDFViewerApplication.finishLoading({
url: 'index.php?action=download&file_id=wibble',
fingerprint: 'fakeFingerprint',
});
const pdfMetadata = new PDFMetadata(fakePDFViewerApplication);

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 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 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 () {
let pdfMetadata;
const fakePDFViewerApplication = new FakePDFViewerApplication();
fakePDFViewerApplication.completeInit();
fakePDFViewerApplication.finishLoading({
fingerprint: 'fakeFingerprint',
url: 'file://fake.pdf',
});
const expectedMetadata = {
link: [
{
href:
'urn:x-pdf:' + fakePDFViewerApplication.pdfDocument.fingerprint,
},
],
};

pdfMetadata = new PDFMetadata(fakePDFViewerApplication);

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);
});

// In order, the title is obtained from:
// 1. The `dc:title` field
// 2. The `documentInfo.Title` field
// 3. The `title` property of the HTML `document` (which PDF.js in turn
// initializes based on the filename from the `Content-Disposition` header
// or URL if that is not available)

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 () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be helpful to detail what context causes the title to come from documentInfo.title vs. dc:title?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a comment above these tests to spell out the intended behavior.

const { pdfMetadata } = createPDFMetadata({
title: 'Some title',
url: 'http://fake.com/',
});

const metadata = await pdfMetadata.getMetadata();

assert.equal(metadata.title, 'Some title');
});
});
});