# test.build/core/adapters/security.adapter.test.ts

> 33 lines of code and 0 definitions.

Tree: Codebase Testing
Language: typescript
Canonical: https://banes-lab.com/anatomy/testing#file-testing-test-build-core-adapters-security-adapter-test-ts
Source text: https://banes-lab.com/source/testing/test.build/core/adapters/security.adapter.test.ts.txt

Listed in [test.build/core/adapters](https://banes-lab.com/api/source/testing/test.build/core/adapters.md), after [test.build/core/adapters/document.adapter.test.ts](https://banes-lab.com/source/testing/test.build/core/adapters/document.adapter.test.ts.md) and before [test.build/core/adapters/security.fixture.ts](https://banes-lab.com/source/testing/test.build/core/adapters/security.fixture.ts.md).

## Contained in

- [test.build/core/adapters](https://banes-lab.com/anatomy/testing/folder-testing-test-build-core-adapters.md)

## Source

```typescript
import { type TestKey, testKey } from "./security.fixture.ts";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { isSignedBy, signText } from "@banes-lab/build-scripts/core/adapters/security.adapter.ts";
import { signingFailed } from "@banes-lab/build-scripts/configuration/strings/site.strings.ts";

const TEXT = "Contact: mailto:a@example.test\n";

let key: TestKey | null = null;

const encoded = function encoded(): string {
    if (key === null) {
        throw new Error("security adapter test: no key");
    }
    return key.encoded;
};

beforeAll(() => {
    key = testKey();
});

afterAll(() => {
    key?.dispose();
});

describe("signText and isSignedBy", () => {
    it("signs the text, exports the public key, and that key verifies the signed text", () => {
        const security = signText(encoded(), TEXT);
        expect(security.signed).toContain(TEXT.trim());
        expect(isSignedBy(security.publicKey, security.signed)).toBe(true);
    });

    it("refuses a changed text and a text with no signature", () => {
        const security = signText(encoded(), TEXT);
        expect(isSignedBy(security.publicKey, security.signed.replace("a@example.test", "b@example.test"))).toBe(false);
        expect(isSignedBy(security.publicKey, TEXT)).toBe(false);
    });

    it("fails loudly on a key that does not import", () => {
        expect(() => signText("bm90IGEga2V5", TEXT)).toThrow(signingFailed("import"));
    });
});
```
