-
Notifications
You must be signed in to change notification settings - Fork 7
/
mocks.ts
67 lines (62 loc) · 2.5 KB
/
mocks.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { IRoleDefinition } from "@energyweb/iam-contracts";
import { JSONCodec } from "../../node_modules/nats.ws/lib/src/mod.js";
import { Claim } from "../../src/cacheServerClient/cacheServerClient.types";
import { IAM } from "../../src/iam";
let _natsConnection;
export const mockNats = () => {
({ _natsConnection } = Reflect.get(IAM, "prototype"));
const mockedNatsConnection = {
publish: jest.fn().mockImplementation(),
};
Reflect.set(IAM.prototype, "_natsConnection", mockedNatsConnection);
return mockedNatsConnection;
};
export const restoreNats = () => {
Reflect.set(IAM.prototype, "_natsConnection", _natsConnection);
};
let _cacheClient;
export const mockCacheClient = () => {
({ _cacheClient } = Reflect.get(IAM, "prototype"));
const cachedRoleDefinitions: Record<string, IRoleDefinition> = {};
const cachedClaims: Record<string, Claim[]> = {};
const cacheRoleDefinitions = (defs: Record<string, IRoleDefinition>) => {
Object.assign(cachedRoleDefinitions, defs);
};
const cacheClaim = (did: string, claim: Claim) => {
const claims = cachedClaims[did] || [];
cachedClaims[did] = [...claims, claim];
};
const mockedCachedClient = {
getRoleDefinition: jest.fn(({ namespace }: { namespace: string }) => cachedRoleDefinitions[namespace]),
getClaimsBySubject: jest.fn(({ did, isAccepted }: { did: string; isAccepted?: boolean }) => {
const claims = cachedClaims[did];
return claims ? claims.filter((c) => c.isAccepted === isAccepted) : [];
}),
getAssetById: jest.fn().mockImplementation(() => {
return {};
}),
};
Reflect.set(IAM.prototype, "_cacheClient", mockedCachedClient);
return { cacheRoleDefinitions, cacheClaim };
};
export const restoreCacheClient = () => {
Reflect.set(IAM.prototype, "_cacheClient", _cacheClient);
};
let _jsonCodec;
export const mockJsonCodec = <T = any>() => {
({ _jsonCodec } = Reflect.get(IAM, "prototype"));
const jsonCodec = JSONCodec<T>();
Reflect.set(IAM.prototype, "_jsonCodec", jsonCodec);
return jsonCodec;
};
export const restoreJsonCodec = () => {
Reflect.set(IAM.prototype, "_jsonCodec", _jsonCodec);
};
let _ipfsStore;
export const mockIpfs = () => {
({ _ipfsStore } = Reflect.get(IAM, "prototype"));
Reflect.set(IAM.prototype, "_ipfsStore", { save: (token: string) => Promise.resolve(token) });
};
export const restoreIpfs = () => {
Reflect.set(IAM.prototype, "_ipfsStore", _ipfsStore);
};