chore: Typescript database models (#2886)

closes #2798
This commit is contained in:
Tom Moor
2022-01-06 18:24:28 -08:00
committed by GitHub
parent d3cbf250e6
commit b20a341f0c
207 changed files with 5624 additions and 5315 deletions

View File

@@ -23,6 +23,7 @@ const app = webService();
const server = new TestServer(app.callback());
beforeEach(() => flushdb());
afterAll(() => server.close());
describe("#documents.info", () => {
it("should return published document", async () => {
const { user, document } = await seed();
@@ -190,7 +191,7 @@ describe("#documents.info", () => {
expect(body.data.document.id).toEqual(childDocument.id);
expect(body.data.document.createdBy).toEqual(undefined);
expect(body.data.document.updatedBy).toEqual(undefined);
expect(body.data.sharedTree).toEqual(collection.documentStructure[0]);
expect(body.data.sharedTree).toEqual(collection.documentStructure?.[0]);
await share.reload();
expect(share.lastAccessedAt).toBeTruthy();
});
@@ -457,6 +458,7 @@ describe("#documents.info", () => {
expect(res.status).toEqual(400);
});
});
describe("#documents.export", () => {
it("should return published document", async () => {
const { user, document } = await seed();
@@ -656,6 +658,7 @@ describe("#documents.export", () => {
expect(res.status).toEqual(400);
});
});
describe("#documents.list", () => {
it("should return documents", async () => {
const { user, document } = await seed();
@@ -875,6 +878,7 @@ describe("#documents.drafts", () => {
expect(body.data.length).toEqual(0);
});
});
describe("#documents.search_titles", () => {
it("should return case insensitive results for partial query", async () => {
const user = await buildUser();
@@ -925,6 +929,7 @@ describe("#documents.search_titles", () => {
expect(res.status).toEqual(401);
});
});
describe("#documents.search", () => {
it("should return results", async () => {
const { user } = await seed();
@@ -1278,8 +1283,7 @@ describe("#documents.search", () => {
expect(body).toMatchSnapshot();
});
// @ts-expect-error ts-migrate(2345) FIXME: Argument of type '(done: DoneCallback) => Promise<... Remove this comment to see the full error message
it("should save search term, hits and source", async (done) => {
it("should save search term, hits and source", async () => {
const { user } = await seed();
await server.post("/api/documents.search", {
body: {
@@ -1287,21 +1291,25 @@ describe("#documents.search", () => {
query: "my term",
},
});
// setTimeout is needed here because SearchQuery is saved asynchronously
// in order to not slow down the response time.
setTimeout(async () => {
const searchQuery = await SearchQuery.findAll({
where: {
query: "my term",
},
});
expect(searchQuery.length).toBe(1);
expect(searchQuery[0].results).toBe(0);
expect(searchQuery[0].source).toBe("app");
done();
}, 100);
return new Promise((resolve) => {
// setTimeout is needed here because SearchQuery is saved asynchronously
// in order to not slow down the response time.
setTimeout(async () => {
const searchQuery = await SearchQuery.findAll({
where: {
query: "my term",
},
});
expect(searchQuery.length).toBe(1);
expect(searchQuery[0].results).toBe(0);
expect(searchQuery[0].source).toBe("app");
resolve(undefined);
}, 100);
});
});
});
describe("#documents.archived", () => {
it("should return archived documents", async () => {
const { user } = await seed();
@@ -1326,7 +1334,7 @@ describe("#documents.archived", () => {
userId: user.id,
teamId: user.teamId,
});
await document.delete();
await document.delete(user.id);
const res = await server.post("/api/documents.archived", {
body: {
token: user.getJwtToken(),
@@ -1362,6 +1370,7 @@ describe("#documents.archived", () => {
expect(res.status).toEqual(401);
});
});
describe("#documents.viewed", () => {
it("should return empty result if no views", async () => {
const { user } = await seed();
@@ -1377,7 +1386,7 @@ describe("#documents.viewed", () => {
it("should return recently viewed documents", async () => {
const { user, document } = await seed();
await View.increment({
await View.incrementOrCreate({
documentId: document.id,
userId: user.id,
});
@@ -1395,7 +1404,7 @@ describe("#documents.viewed", () => {
it("should not return recently viewed but deleted documents", async () => {
const { user, document } = await seed();
await View.increment({
await View.incrementOrCreate({
documentId: document.id,
userId: user.id,
});
@@ -1412,7 +1421,7 @@ describe("#documents.viewed", () => {
it("should not return recently viewed documents in collection not a member of", async () => {
const { user, document, collection } = await seed();
await View.increment({
await View.incrementOrCreate({
documentId: document.id,
userId: user.id,
});
@@ -1435,6 +1444,7 @@ describe("#documents.viewed", () => {
expect(body).toMatchSnapshot();
});
});
describe("#documents.starred", () => {
it("should return empty result if no stars", async () => {
const { user } = await seed();
@@ -1524,10 +1534,11 @@ describe("#documents.move", () => {
expect(res.status).toEqual(403);
});
});
describe("#documents.restore", () => {
it("should allow restore of trashed documents", async () => {
const { user, document } = await seed();
await document.destroy(user.id);
await document.destroy();
const res = await server.post("/api/documents.restore", {
body: {
token: user.getJwtToken(),
@@ -1545,7 +1556,7 @@ describe("#documents.restore", () => {
userId: user.id,
teamId: user.teamId,
});
await document.destroy(user.id);
await document.destroy();
const res = await server.post("/api/documents.restore", {
body: {
token: user.getJwtToken(),
@@ -1561,7 +1572,7 @@ describe("#documents.restore", () => {
it("should not allow restore of documents in deleted collection", async () => {
const { user, document, collection } = await seed();
await document.destroy(user.id);
await document.destroy();
await collection.destroy();
const res = await server.post("/api/documents.restore", {
body: {
@@ -1575,7 +1586,7 @@ describe("#documents.restore", () => {
it("should not allow restore of trashed documents to collection user cannot access", async () => {
const { user, document } = await seed();
const collection = await buildCollection();
await document.destroy(user.id);
await document.destroy();
const res = await server.post("/api/documents.restore", {
body: {
token: user.getJwtToken(),
@@ -1749,6 +1760,7 @@ describe("#documents.star", () => {
expect(res.status).toEqual(403);
});
});
describe("#documents.unstar", () => {
it("should unstar the document", async () => {
const { user, document } = await seed();
@@ -1786,6 +1798,7 @@ describe("#documents.unstar", () => {
expect(res.status).toEqual(403);
});
});
describe("#documents.import", () => {
it("should error if no file is passed", async () => {
const user = await buildUser();
@@ -1807,6 +1820,7 @@ describe("#documents.import", () => {
expect(res.status).toEqual(401);
});
});
describe("#documents.create", () => {
it("should create as a new document", async () => {
const { user, collection } = await seed();
@@ -1822,8 +1836,8 @@ describe("#documents.create", () => {
const body = await res.json();
const newDocument = await Document.findByPk(body.data.id);
expect(res.status).toEqual(200);
expect(newDocument.parentDocumentId).toBe(null);
expect(newDocument.collectionId).toBe(collection.id);
expect(newDocument!.parentDocumentId).toBe(null);
expect(newDocument!.collectionId).toBe(collection.id);
expect(body.policies[0].abilities.update).toEqual(true);
});
@@ -1892,6 +1906,7 @@ describe("#documents.create", () => {
expect(body.policies[0].abilities.update).toEqual(true);
});
});
describe("#documents.update", () => {
it("should update document details in the root", async () => {
const { user, document } = await seed();
@@ -1901,7 +1916,7 @@ describe("#documents.update", () => {
id: document.id,
title: "Updated title",
text: "Updated text",
lastRevision: document.revision,
lastRevision: document.revisionCount,
},
});
const body = await res.json();
@@ -1929,7 +1944,7 @@ describe("#documents.update", () => {
id: template.id,
title: "Updated title",
text: "Updated text",
lastRevision: template.revision,
lastRevision: template.revisionCount,
publish: true,
},
});
@@ -1960,7 +1975,7 @@ describe("#documents.update", () => {
id: document.id,
title: "Updated title",
text: "Updated text",
lastRevision: document.revision,
lastRevision: document.revisionCount,
publish: true,
},
});
@@ -1974,14 +1989,14 @@ describe("#documents.update", () => {
it("should not edit archived document", async () => {
const { user, document } = await seed();
await document.archive();
await document.archive(user.id);
const res = await server.post("/api/documents.update", {
body: {
token: user.getJwtToken(),
id: document.id,
title: "Updated title",
text: "Updated text",
lastRevision: document.revision,
lastRevision: document.revisionCount,
},
});
expect(res.status).toEqual(403);
@@ -2048,7 +2063,7 @@ describe("#documents.update", () => {
token: admin.getJwtToken(),
id: document.id,
text: "Changed text",
lastRevision: document.revision,
lastRevision: document.revisionCount,
},
});
const body = await res.json();
@@ -2072,7 +2087,7 @@ describe("#documents.update", () => {
token: user.getJwtToken(),
id: document.id,
text: "Changed text",
lastRevision: document.revision,
lastRevision: document.revisionCount,
},
});
expect(res.status).toEqual(403);
@@ -2087,7 +2102,7 @@ describe("#documents.update", () => {
token: user.getJwtToken(),
id: document.id,
text: "Changed text",
lastRevision: document.revision,
lastRevision: document.revisionCount,
},
});
expect(res.status).toEqual(403);
@@ -2100,7 +2115,7 @@ describe("#documents.update", () => {
token: user.getJwtToken(),
id: document.id,
text: "Additional text",
lastRevision: document.revision,
lastRevision: document.revisionCount,
append: true,
},
});
@@ -2116,7 +2131,7 @@ describe("#documents.update", () => {
body: {
token: user.getJwtToken(),
id: document.id,
lastRevision: document.revision,
lastRevision: document.revisionCount,
title: "Updated Title",
append: true,
},
@@ -2132,7 +2147,7 @@ describe("#documents.update", () => {
body: {
token: user.getJwtToken(),
id: document.id,
lastRevision: document.revision,
lastRevision: document.revisionCount,
title: "Updated Title",
text: "",
},
@@ -2148,7 +2163,7 @@ describe("#documents.update", () => {
body: {
token: user.getJwtToken(),
id: document.id,
lastRevision: document.revision,
lastRevision: document.revisionCount,
title: document.title,
text: document.text,
},
@@ -2184,6 +2199,7 @@ describe("#documents.update", () => {
expect(res.status).toEqual(403);
});
});
describe("#documents.archive", () => {
it("should allow archiving document", async () => {
const { user, document } = await seed();
@@ -2209,6 +2225,7 @@ describe("#documents.archive", () => {
expect(res.status).toEqual(401);
});
});
describe("#documents.delete", () => {
it("should allow deleting document", async () => {
const { user, document } = await seed();
@@ -2251,6 +2268,7 @@ describe("#documents.delete", () => {
const { user, document, collection } = await seed();
// delete collection without hooks to trigger document deletion
await collection.destroy({
// @ts-expect-error type is incorrect here
hooks: false,
});
const res = await server.post("/api/documents.delete", {
@@ -2276,6 +2294,7 @@ describe("#documents.delete", () => {
expect(body).toMatchSnapshot();
});
});
describe("#documents.unpublish", () => {
it("should unpublish a document", async () => {
const { user, document } = await seed();
@@ -2291,12 +2310,12 @@ describe("#documents.unpublish", () => {
expect(body.data.publishedAt).toBeNull();
const reloaded = await Document.unscoped().findByPk(document.id);
expect(reloaded.userId).toEqual(user.id);
expect(reloaded!.createdById).toEqual(user.id);
});
it("should unpublish another users document", async () => {
const { user, collection } = await seed();
let document = await buildDocument({
const document = await buildDocument({
teamId: user.teamId,
collectionId: collection.id,
});
@@ -2310,8 +2329,9 @@ describe("#documents.unpublish", () => {
expect(res.status).toEqual(200);
expect(body.data.id).toEqual(document.id);
expect(body.data.publishedAt).toBeNull();
document = await Document.unscoped().findByPk(document.id);
expect(document.userId).toEqual(user.id);
const reloaded = await Document.unscoped().findByPk(document.id);
expect(reloaded!.createdById).toEqual(user.id);
});
it("should fail to unpublish a draft document", async () => {
@@ -2329,7 +2349,7 @@ describe("#documents.unpublish", () => {
it("should fail to unpublish a deleted document", async () => {
const { user, document } = await seed();
await document.delete();
await document.delete(user.id);
const res = await server.post("/api/documents.unpublish", {
body: {
token: user.getJwtToken(),
@@ -2341,7 +2361,7 @@ describe("#documents.unpublish", () => {
it("should fail to unpublish an archived document", async () => {
const { user, document } = await seed();
await document.archive();
await document.archive(user.id);
const res = await server.post("/api/documents.unpublish", {
body: {
token: user.getJwtToken(),