ekyc

Electronic KYC process with uploading ID document using OAuth 2.1 (experimental)
Log | Files | Refs | README | LICENSE

auth_repository.test.ts (4068B)


      1 import {
      2   AuthDto,
      3   mapFromAuth,
      4   mapToAuth,
      5 } from "../infrastructure/memory/mapper/auth.ts";
      6 import {
      7   EntityLocked,
      8   EntityNotFound,
      9   Repository,
     10 } from "../core/application/repository_error.ts";
     11 import { Auth } from "#core/domain/auth.ts";
     12 import { MemoryAuthRepositoryAdapter } from "#infrastructure/memory/auth.ts";
     13 import "$dotenv";
     14 import { Pool } from "$postgres";
     15 import { assertEquals } from "$std/assert/assert_equals.ts";
     16 import { assertRejects } from "$std/assert/assert_rejects.ts";
     17 import { PostgresAuthRepositoryAdapter } from "../infrastructure/postgres/auth.ts";
     18 
     19 const uuid = "9272d511-a47f-4c91-8e41-d056ca797b42";
     20 const email = "doydy1@bfh.ch";
     21 // hash("password")
     22 const passwordHash =
     23   "$argon2id$v=19$m=65536,t=2,p=1$JqSklInU0x0uFDs/tj+dDQ$Z6vJ+4MlZqSwPocHobYwbeUt+I18a4T5k5m90wB/dpg";
     24 
     25 const implementations = [
     26   function memoryAuthRepository() {
     27     return new MemoryAuthRepositoryAdapter(new Map());
     28   },
     29   async function postgresAuthRepository() {
     30     const pool = new Pool(undefined, 4);
     31     const connection = await pool.connect();
     32     await connection.queryArray`delete from auth;`;
     33     connection.release();
     34     return new PostgresAuthRepositoryAdapter(pool);
     35   },
     36 ] as ReadonlyArray<() => Promise<Repository<Auth>>>;
     37 
     38 for (const factory of implementations) {
     39   Deno.test({
     40     name: `integration test of ${factory.name}`,
     41     async fn(t) {
     42       const authRepo = await factory();
     43 
     44       await t.step("should be not found invalid uuid", async () => {
     45         const act = async () => await authRepo.find("invalid uuid");
     46         await assertRejects(act, EntityNotFound);
     47       });
     48 
     49       await t.step("should be not found in empty", async () => {
     50         const act = async () => await authRepo.find(uuid);
     51         await assertRejects(act, EntityNotFound);
     52       });
     53 
     54       const sample1: AuthDto = {
     55         uuid,
     56         email,
     57         emailVerified: true,
     58         emailCode: null,
     59         emailCodeExpire: new Date(0),
     60         emailChallengeRequest: 0,
     61         emailChallengeRequestExpire: new Date(0),
     62         emailChallengeAttempt: 0,
     63         emailChallengeAttemptExpire: new Date(0),
     64         passwordHash,
     65         passwordAttempt: 0,
     66         passwordAttemptExpire: new Date(0),
     67         sessionToken: null,
     68         sessionExpire: new Date(0),
     69         version: 0,
     70       };
     71 
     72       await t.step("should be store sample 1", async () => {
     73         const act = await authRepo.store(mapToAuth(sample1));
     74         assertEquals(act, sample1.version + 1);
     75       });
     76 
     77       await t.step("should be find stored sample 1", async () => {
     78         const act = mapFromAuth(await authRepo.find(uuid));
     79         assertEquals(
     80           act,
     81           Object.assign({}, sample1, { version: sample1.version + 1 }),
     82         );
     83       });
     84 
     85       const sample2: AuthDto = {
     86         uuid,
     87         email,
     88         emailVerified: true,
     89         emailCode: "769262",
     90         emailCodeExpire: new Date("2024-07-10T10:00:00Z"),
     91         emailChallengeRequest: 1,
     92         emailChallengeRequestExpire: new Date("2024-07-10T10:00:00Z"),
     93         emailChallengeAttempt: 1,
     94         emailChallengeAttemptExpire: new Date("2024-07-10T10:00:25Z"),
     95         passwordHash,
     96         passwordAttempt: 0,
     97         passwordAttemptExpire: new Date(0),
     98         sessionToken: null,
     99         sessionExpire: new Date(0),
    100         version: 1,
    101       };
    102 
    103       await t.step("should be store sample 2", async () => {
    104         const act = await authRepo.store(mapToAuth(sample2));
    105         assertEquals(act, sample2.version + 1);
    106       });
    107 
    108       await t.step("should be find stored sample 2", async () => {
    109         const act = mapFromAuth(await authRepo.find(uuid));
    110         assertEquals(
    111           act,
    112           Object.assign({}, sample2, { version: sample2.version + 1 }),
    113         );
    114       });
    115 
    116       await t.step(
    117         "should be optimistic lock on store invalid version",
    118         async () => {
    119           const act = async () => await authRepo.store(mapToAuth(sample2));
    120           await assertRejects(act, EntityLocked);
    121         },
    122       );
    123     },
    124     sanitizeResources: false,
    125   });
    126 }