Skip to content

Commit

Permalink
tests: encryption predicate function
Browse files Browse the repository at this point in the history
  • Loading branch information
basz committed Apr 17, 2024
1 parent 4d27298 commit 359b0ca
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
46 changes: 46 additions & 0 deletions test/encryption-predicate.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { expect } from "chai";
import { encrypt, decrypt } from "../src/entity";
import { getConnection } from "./utils";
import ColumnOptionsEntity4 from "./entities/ColumnOptionsEntity4";

describe("Column Options - Encryption Predicate", function () {
this.timeout(10000);

before(async function () {
await getConnection();
});

it("should encrypt", function () {
let result = new ColumnOptionsEntity4();
result.enablePredicate = true;
result.secret = "test";
encrypt(result);
expect(result.secret).to.equal(
"/1rBkZBCSx2I+UGe+UmuVhKzmHsDDv0EvRtMBFiaE3A="
);
});

it("should not encrypt", function () {
let result = new ColumnOptionsEntity4();
result.enablePredicate = false;
result.secret = "test";
encrypt(result);
expect(result.secret).to.equal("test");
});

it("should decrypt", function () {
let result = new ColumnOptionsEntity4();
result.enablePredicate = true;
result.secret = "/1rBkZBCSx2I+UGe+UmuVhKzmHsDDv0EvRtMBFiaE3A=";
decrypt(result);
expect(result.secret).to.equal("test");
});

it("should not decrypt", function () {
let result = new ColumnOptionsEntity4();
result.enablePredicate = false;
result.secret = "test";
decrypt(result);
expect(result.secret).to.equal("test");
});
});
25 changes: 25 additions & 0 deletions test/entities/ColumnOptionsEntity4.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { BaseEntity, Entity, PrimaryGeneratedColumn, Column } from "typeorm";
import { ExtendedColumnOptions } from "../../src/options";

@Entity()
export default class ColumnOptionsEntity4 extends BaseEntity {
@PrimaryGeneratedColumn()
id: number;

@Column({ type: "boolean" })
enablePredicate: boolean;

@Column(<ExtendedColumnOptions>{
type: "varchar",
nullable: false,
encrypt: {
key: "e41c966f21f9e1577802463f8924e6a3fe3e9751f201304213b2f845d8841d61",
algorithm: "aes-256-cbc",
ivLength: 16,
iv: "ff5ac19190424b1d88f9419ef949ae56",
encryptionPredicate: (entity: ColumnOptionsEntity4) =>
entity.enablePredicate,
},
})
secret: string;
}

0 comments on commit 359b0ca

Please sign in to comment.