-
-
Notifications
You must be signed in to change notification settings - Fork 658
/
fake-api-token-store.ts
81 lines (68 loc) · 2.12 KB
/
fake-api-token-store.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import type { IApiTokenStore } from '../../lib/types/stores/api-token-store';
import type {
ApiTokenType,
IApiToken,
IApiTokenCreate,
} from '../../lib/types/models/api-token';
import EventEmitter from 'events';
export default class FakeApiTokenStore
extends EventEmitter
implements IApiTokenStore
{
countByType(): Promise<Map<ApiTokenType, number>> {
return Promise.resolve(new Map());
}
tokens: IApiToken[] = [];
async delete(key: string): Promise<void> {
this.tokens.splice(
this.tokens.findIndex((t) => t.secret === key),
1,
);
}
async count(): Promise<number> {
return this.tokens.length;
}
async deleteAll(): Promise<void> {
this.tokens = [];
}
destroy(): void {}
async exists(key: string): Promise<boolean> {
return this.tokens.some((token) => token.secret === key);
}
async get(key: string): Promise<IApiToken> {
// get can return undefined. See api-token-store.e2e.test.ts
return this.tokens.find((t) => t.secret === key);
}
async getAll(): Promise<IApiToken[]> {
return this.tokens;
}
async getAllActive(): Promise<IApiToken[]> {
return this.tokens.filter(
(token) => token.expiresAt === null || token.expiresAt > new Date(),
);
}
async insert(newToken: IApiTokenCreate): Promise<IApiToken> {
const apiToken = {
createdAt: new Date(),
project: newToken.projects?.join(',') || '*',
alias: null,
...newToken,
};
this.tokens.push(apiToken);
this.emit('insert');
return apiToken;
}
async markSeenAt(secrets: string[]): Promise<void> {
this.tokens
.filter((t) => secrets.includes(t.secret))
.forEach((t) => {
// eslint-disable-next-line no-param-reassign
t.seenAt = new Date();
});
}
async setExpiry(secret: string, expiresAt: Date): Promise<IApiToken> {
const t = await this.get(secret);
t.expiresAt = expiresAt;
return t;
}
}