Skip to content

Commit

Permalink
ditch more barrel file imports and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marcolarosa committed Nov 17, 2023
1 parent 8a8a719 commit 9307832
Show file tree
Hide file tree
Showing 23 changed files with 222 additions and 111 deletions.
5 changes: 3 additions & 2 deletions api/src/common/aws-ses.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ describe("AWS SES tests", () => {
let templates = await ses.listTemplates();
templates.TemplatesMetadata = templates.TemplatesMetadata.filter(
(t) => !t.Name.match(/production-/)
);
).filter((t) => !t.Name.match(/development-/));
expect(templates.TemplatesMetadata).toEqual([]);

await ses.loadTemplates();
templates = await ses.listTemplates();
templates.TemplatesMetadata = templates.TemplatesMetadata.filter(
(t) => !t.Name.match(/production-/)
);
).filter((t) => !t.Name.match(/development-/));
expect(templates.TemplatesMetadata).toMatchObject([{ Name: "testing-application-login" }]);
});
it("should be able to send a templated email", async () => {
Expand Down
23 changes: 12 additions & 11 deletions api/src/common/elastic-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,27 @@ export async function indexItem({ location = "workspace", configuration, item, c

// index the item metadata and content
const indexIdentifier = `/${item.type}/${item.identifier}`;
let store = await getStoreHandle({ location, id: item.identifier, type: "item" });
let teiText;
try {
let store = await getStoreHandle({ location, id: item.identifier, type: "item" });
const teiFileContent = await store.get({
target: `${item.identifier}-tei-complete.xml`,
});
let teiText = await extractText({
teiText = await extractText({
configuration,
content: teiFileContent,
});
const document = assembleIndexRecord({ crate });
document.text = teiText;
await client.index({
index: "manuscripts",
id: indexIdentifier,
document,
});
// console.log(document);
} catch (error) {
console.log(error);
teiText = "";
}
const document = assembleIndexRecord({ crate });
document.text = teiText;
await client.index({
index: "manuscripts",
id: indexIdentifier,
document,
});
// console.log(document);
// setup the entities index
try {
// await client.indices.delete({ index: "entities" });
Expand Down
4 changes: 2 additions & 2 deletions api/src/common/jwt.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require("regenerator-runtime");
import { generateToken, verifyToken } from "./jwt";
import { loadConfiguration } from "../common";
import { loadConfiguration } from "../common/configuration.js";
const chance = require("chance").Chance();
import MockDate from "mockdate";
import { TestSetup } from "../common";
import { TestSetup } from "../common/test-utils.js";

describe("JWT tests", () => {
let configuration, users, userEmail, adminEmail, bucket;
Expand Down
2 changes: 1 addition & 1 deletion api/src/common/logger.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import "regenerator-runtime";
import { logEvent } from "./logger";
import { logEvent } from "./logger.js";
import models from "../models";
describe("Logger tests", () => {
it("should be able to log an event to the logs table", async () => {
Expand Down
99 changes: 99 additions & 0 deletions api/src/common/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,101 @@ import { getS3Handle } from "./getS3Handle.js";

const bucketName = "testing";
export const host = `http://localhost:8080`;
export function teiDocument(identifier) {
return `
<?xml version="1.0" encoding="UTF-8"?>
<TEI xmlns="http://www.tei-c.org/ns/1.0" xmlns:c="http://www.w3.org/ns/xproc-step" xmlns:cx="http://xmlcalabash.com/ns/extensions" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:nyingarn="https://nyingarn.net/ns/functions" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:z="https://github.com/Conal-Tuohy/XProc-Z" xml:id="MarcoTest">
<teiHeader>
<fileDesc><titleStmt><title>My Research Object Crate</title></titleStmt><publicationStmt><publisher><ref target="https://nyingarn.net/">Nyingarn</ref></publisher></publicationStmt><sourceDesc><p>some text</p></sourceDesc></fileDesc>
<xenoData type="application/ld+json">{
"@graph": [
{
"identifier": "ro-crate-metadata.json",
"@type": "CreativeWork",
"about": { "@id": ".\/" },
"@id": "ro-crate-metadata.json",
"conformsTo": { "@id": "https:\/\/w3id.org\/ro\/crate\/1.1" }
},
{
"rightsHolder": { "@id": "https:\/\/orcid.org\/0000-0003-1783-627X" },
"identifier": "MarcoTest",
"licence": { "@id": "LICENCE.md" },
"hasPart": [
{ "@id": "MarcoTest-357.jpg" },
{ "@id": "MarcoTest-357.tei.xml" },
{ "@id": "MarcoTest-357.textract_ocr-ADMIN.json" },
{ "@id": "MarcoTest-357.thumbnail_h300.jpg" },
{ "@id": "MarcoTest-357.webp" },
{ "@id": "MarcoTest-4003.jpg" },
{ "@id": "MarcoTest-4003.tei.xml" },
{ "@id": "MarcoTest-4003.textract_ocr-ADMIN.json" },
{ "@id": "MarcoTest-4003.thumbnail_h300.jpg" },
{ "@id": "MarcoTest-4003.webp" },
{ "@id": "MarcoTest-language-authority-permission.pdf" },
{ "@id": "MarcoTest-rights-holder-permission.pdf" },
{ "@id": ".item-status.json" },
{ "@id": "MarcoTest-tei-complete.xml" },
{ "@id": "LICENCE.md" },
{ "@id": "MarcoTest-tei-complete.v2023-07-10T03:50:13.975Z.xml" }
],
"@type": [ "Dataset" ],
"author": { "@id": "https:\/\/orcid.org\/0000-0003-1783-627X" },
"description": "some text",
"languageName": "more text",
"subjectLanguage": { "@id": "https:\/\/collection.aiatsis.gov.au\/austlang\/language\/D2" },
"copyrightStatus": "Yes = in copyright",
"name": "My Research Object Crate",
"contentLanguage": [
{ "@id": "https:\/\/collection.aiatsis.gov.au\/austlang\/language\/W40" },
{ "@id": "https:\/\/collection.aiatsis.gov.au\/austlang\/language\/A48" },
{ "@id": "#e27" }
],
"depositor": { "@id": "https:\/\/orcid.org\/0000-0001-5383-6993" },
"@id": ".\/"
},
]
}</xenodata>
</teiHeader><text><body><pb xml:id="${identifier}-357"/>
<ab>A<lb/>
English and <unclear>Aboriginal</unclear><lb/>
A<lb/>
<unclear>Allow</unclear><lb/>
Anoy<lb/>
Along<lb/>
<unclear>Kash</unclear> too bur-roin<lb/>
answer<lb/>
<unclear>Foom</unclear> boon<lb/>
-<lb/>
Also<lb/>
<unclear>tey</unclear><lb/>
<unclear>ant</unclear> <unclear>small kind</unclear><lb/>
<unclear>Beal-</unclear> <unclear>belp</unclear><lb/>
<unclear>Ber</unclear><lb/>
<unclear>ser-gun</unclear><lb/>
do bull<lb/>
<unclear>nur</unclear> <unclear>murn</unclear><lb/>
<unclear>Amidel</unclear><lb/>
<unclear>Bar-go-rut</unclear><lb/>
Ankle<lb/>
Beur nar <unclear>fer</unclear><lb/>
Amiss<lb/>
<unclear>nil</unclear> lam<lb/>
apartment<lb/>
<unclear>Wil-mut</unclear><lb/>
and<lb/>
Bar<lb/>
apple<lb/>
Bool <unclear>le-vrum</unclear><lb/>
<unclear>anget</unclear><lb/>
Pork <unclear>bork/bar-ren</unclear><lb/>
<unclear>War-ra</unclear> <unclear>three</unclear><lb/>
<unclear>Lam-bar-more</unclear><lb/>
another<lb/>
<unclear>th</unclear> ung<lb/>
arch on Gap<lb/>
Pear - <unclear>bor</unclear> ring</ab>
</body></text></TEI>`;
}

export function headers(session) {
return {
Expand Down Expand Up @@ -107,6 +202,10 @@ export async function setupTestItem({ identifier, store, user }) {
content: "text",
target: `${identifier}-02.txt`,
},
{
content: teiDocument(identifier),
target: `${identifier}-tei-complete.xml`,
},
],
});
return { item };
Expand Down
31 changes: 21 additions & 10 deletions api/src/lib/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,17 +427,28 @@ export async function setRepositoryItemMetadata({ configuration, item, store })

try {
let licence = crate.getEntity("LICENCE.md");
if (!licence) throw new Error(`no licence found - setting to closed`);
let openAccess = false;
let accessControlList = null;
let accessNarrative = null;
let reviewDate = null;
if (licence?.access?.[0]?.["@id"]?.match(/OpenAccess/)) {
openAccess = true;
let openAccess;
let accessControlList;
let accessNarrative;
let reviewDate;
if (!licence) {
console.log(`no licence found - setting to closed`);
openAccess = false;
accessControlList = null;
accessNarrative = null;
reviewDate = null;
} else {
accessNarrative = licence.description.join("\n");
reviewDate = licence?.reviewDate?.[0];
accessControlList = await store.getJSON({ target: authorisedUsersFile });
openAccess = false;
accessControlList = null;
accessNarrative = null;
reviewDate = null;
if (licence?.access?.[0]?.["@id"]?.match(/OpenAccess/)) {
openAccess = true;
} else {
accessNarrative = licence.description.join("\n");
reviewDate = licence?.reviewDate?.[0];
accessControlList = await store.getJSON({ target: authorisedUsersFile });
}
}

item.openAccess = openAccess;
Expand Down
37 changes: 16 additions & 21 deletions api/src/lib/admin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@ require("regenerator-runtime");
import { deleteItem } from "../lib/item.js";
import Chance from "chance";
const chance = Chance();
import {
loadConfiguration,
getStoreHandle,
TestSetup,
setupTestItem,
setupTestCollection,
getS3Handle,
} from "../common/index.js";
import { TestSetup, setupTestItem, setupTestCollection } from "../common/test-utils.js";
import { getS3Handle, getStoreHandle } from "../common/getS3Handle.js";
import { loadConfiguration } from "../common/configuration.js";
import models from "../models/index.js";
import {
getAdminItems,
Expand Down Expand Up @@ -427,25 +422,24 @@ describe("Admin management tests", () => {
await item.reload();

// deposit into the repo
await depositObjectIntoRepository({ type: "item", identifier });
await depositObjectIntoRepository({ type: "item", identifier, configuration });

let resources = await objectRepository.listResources();
resources = resources.map((r) => r.Key);
expect(resources.length).toEqual(9);
expect(resources.length).toEqual(10);

let objectExistsInWorkspace = await objectWorkspace.exists();
expect(objectExistsInWorkspace).toBeFalse;
// let objectExistsInWorkspace = await objectWorkspace.exists();
// expect(objectExistsInWorkspace).toBeFalse;

const client = new Client({
node: configuration.api.services.elastic.host,
});
let document = await client.get({
index: "metadata",
index: "manuscripts",
id: `/item/${identifier}`,
});
expect(document._source).toMatchObject({
"@id": "./",
"@type": ["Dataset"],
name: "My Research Object Crate",
});

await objectWorkspace.removeObject();
Expand Down Expand Up @@ -480,16 +474,16 @@ describe("Admin management tests", () => {
await item.reload();

// deposit into the repo
await depositObjectIntoRepository({ type: "item", identifier });
await depositObjectIntoRepository({ type: "item", identifier, configuration });

// restore the object from the repo back into the workspace
await restoreObjectIntoWorkspace({ type: "item", identifier });
// await restoreObjectIntoWorkspace({ type: "item", identifier });

let objectExistsInWorkspace = await objectWorkspace.exists();
expect(objectExistsInWorkspace).toBeTrue;
let resources = await objectWorkspace.listResources();
resources = resources.map((r) => r.Key);
expect(resources.length).toEqual(9);
expect(resources.length).toEqual(10);

// change the metadata file so it versions
let crateFile = await objectWorkspace.getJSON({ target: "ro-crate-metadata.json" });
Expand All @@ -498,18 +492,19 @@ describe("Admin management tests", () => {

resources = await objectWorkspace.listResources();
resources = resources.map((r) => r.Key);
expect(resources.length).toEqual(9);
expect(resources.length).toEqual(10);

// deposit into the repo a second time and check metadata versioned
await depositObjectIntoRepository({
type: "item",
identifier,
version: { metadata: true },
configuration,
});

resources = await objectRepository.listResources();
resources = resources.map((r) => r.Key);
expect(resources.length).toEqual(10);
expect(resources.length).toEqual(11);

let crateFileVersions = await objectRepository.listFileVersions({
target: "ro-crate-metadata.json ",
Expand Down Expand Up @@ -548,7 +543,7 @@ describe("Admin management tests", () => {
await item.reload();

// deposit into the repo
await depositObjectIntoRepository({ type: "item", identifier });
await depositObjectIntoRepository({ type: "item", identifier, configuration });

// delete the item from the repository
await deleteItemFromRepository({ type: "item", identifier, configuration });
Expand Down
3 changes: 2 additions & 1 deletion api/src/lib/collection.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
toggleCollectionVisibility,
} from "./collection";
const chance = require("chance").Chance();
import { getStoreHandle, TestSetup } from "../common";
import { TestSetup } from "../common/test-utils.js";
import { getStoreHandle } from "../common/getS3Handle";

describe("Collection management tests", () => {
let configuration, users, userEmail, adminEmail, bucket;
Expand Down
5 changes: 3 additions & 2 deletions api/src/lib/crate-tools.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
require("regenerator-runtime");
import { registerAllFiles } from "./crate-tools.js";
const chance = require("chance").Chance();
import { getStoreHandle, TestSetup, setupTestItem } from "../common";
import { TestSetup, setupTestItem } from "../common/test-utils.js";
import { getStoreHandle } from "../common/getS3Handle.js";
import { ROCrate } from "ro-crate";
import models from "../models";

Expand Down Expand Up @@ -35,7 +36,7 @@ describe("Crate tools tests", () => {
let resources = await store.listResources();

await registerAllFiles({ crate, resources });
expect(crate.rootDataset.hasPart.length).toBe(4);
expect(crate.rootDataset.hasPart.length).toBe(5);

await models.item.destroy({ where: { identifier } });
await store.removeObject();
Expand Down
4 changes: 3 additions & 1 deletion api/src/lib/item.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ import {
saveItemTranscription,
} from "./item";
const chance = require("chance").Chance();
import { getStoreHandle, TestSetup, setupTestItem, resourceStatusFile } from "../common";
import { resourceStatusFile } from "../common/index.js";
import { TestSetup, setupTestItem } from "../common/test-utils.js";
import { getStoreHandle } from "../common/getS3Handle";
import models from "../models";

describe("Item management tests", () => {
Expand Down
2 changes: 1 addition & 1 deletion api/src/lib/logs.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require("regenerator-runtime");
import models from "../models";
const { getLogs } = require("./logs");
import { generateLogs } from "../common";
import { generateLogs } from "../common/test-utils.js";

describe("Log management tests", () => {
beforeAll(async () => {
Expand Down
Loading

0 comments on commit 9307832

Please # to comment.