Skip to content

Commit

Permalink
test: add reference creation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Magnus-Kuhn committed Sep 24, 2024
1 parent 81a46ef commit 8298577
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
22 changes: 22 additions & 0 deletions packages/transport/test/modules/files/FileReference.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Serializable } from "@js-soft/ts-serval";
import { CoreId } from "@nmshd/core-types";
import { CoreBuffer, CryptoEncryption, CryptoSecretKey } from "@nmshd/crypto";
import { BackboneIds, FileReference } from "../../../src";
import { TestUtil } from "../../testHelpers/TestUtil";

describe("FileReference", function () {
test("should serialize and deserialize correctly (verbose)", async function () {
Expand Down Expand Up @@ -200,4 +201,25 @@ describe("FileReference", function () {
});
}).rejects.toThrow("FileReference.forIdentityTruncated");
});

test("should correctly create a reference to a file", async function () {
const connection = await TestUtil.createDatabaseConnection();
const transport = TestUtil.createTransport(connection);
await transport.init();
const account = (await TestUtil.provideAccounts(transport, 1))[0];

const content = CoreBuffer.fromUtf8("Test");
const file = await TestUtil.uploadFile(account, content);

const reference = file.toFileReference();
expect(reference).toBeInstanceOf(Serializable);
expect(reference).toBeInstanceOf(FileReference);
expect(reference.key).toBeInstanceOf(CryptoSecretKey);
expect(reference.id).toBeInstanceOf(CoreId);
expect(reference.id.equals(file.id)).toBe(true);
expect(reference.backboneBaseUrl).toBe("localhost");

await account.close();
await connection.close();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Serializable } from "@js-soft/ts-serval";
import { CoreId } from "@nmshd/core-types";
import { CoreBuffer, CryptoEncryption, CryptoSecretKey } from "@nmshd/crypto";
import { BackboneIds, RelationshipTemplateReference } from "../../../src";
import { TestUtil } from "../../testHelpers/TestUtil";

describe("RelationshipTemplateReference", function () {
test("should serialize and deserialize correctly (verbose)", async function () {
Expand Down Expand Up @@ -200,4 +201,24 @@ describe("RelationshipTemplateReference", function () {
});
}).rejects.toThrow("RelationshipTemplateReference.forIdentityTruncated");
});

test("should correctly create a reference to a template", async function () {
const connection = await TestUtil.createDatabaseConnection();
const transport = TestUtil.createTransport(connection);
await transport.init();
const account = (await TestUtil.provideAccounts(transport, 1))[0];

const sentRelationshipTemplate = await TestUtil.sendRelationshipTemplate(account);

const reference = sentRelationshipTemplate.toRelationshipTemplateReference();
expect(reference).toBeInstanceOf(Serializable);
expect(reference).toBeInstanceOf(RelationshipTemplateReference);
expect(reference.key).toBeInstanceOf(CryptoSecretKey);
expect(reference.id).toBeInstanceOf(CoreId);
expect(reference.id.equals(sentRelationshipTemplate.id)).toBe(true);
expect(reference.backboneBaseUrl).toBe("localhost");

await account.close();
await connection.close();
});
});
29 changes: 28 additions & 1 deletion packages/transport/test/modules/tokens/TokenReference.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Serializable } from "@js-soft/ts-serval";
import { CoreId } from "@nmshd/core-types";
import { CoreDate, CoreId } from "@nmshd/core-types";
import { CoreBuffer, CryptoEncryption, CryptoSecretKey } from "@nmshd/crypto";
import { BackboneIds, TokenReference } from "../../../src";
import { TestUtil } from "../../testHelpers/TestUtil";

describe("TokenReference", function () {
test("should serialize and deserialize correctly (verbose)", async function () {
Expand Down Expand Up @@ -200,4 +201,30 @@ describe("TokenReference", function () {
});
}).rejects.toThrow("TokenReference.forIdentityTruncated");
});

test("should correctly create a reference to a token", async function () {
const connection = await TestUtil.createDatabaseConnection();
const transport = TestUtil.createTransport(connection);
await transport.init();
const account = (await TestUtil.provideAccounts(transport, 1))[0];

const content = Serializable.fromAny({ content: "TestToken" });
const expiresAt = CoreDate.utc().add({ minutes: 5 });
const sentToken = await account.tokens.sendToken({
content,
expiresAt,
ephemeral: false
});

const reference = sentToken.toTokenReference();
expect(reference).toBeInstanceOf(Serializable);
expect(reference).toBeInstanceOf(TokenReference);
expect(reference.key).toBeInstanceOf(CryptoSecretKey);
expect(reference.id).toBeInstanceOf(CoreId);
expect(reference.id.equals(sentToken.id)).toBe(true);
expect(reference.backboneBaseUrl).toBe("localhost");

await account.close();
await connection.close();
});
});

0 comments on commit 8298577

Please sign in to comment.