Skip to content

Commit ad70ebd

Browse files
committed
add blockList option
1 parent cb318a3 commit ad70ebd

File tree

9 files changed

+147
-8
lines changed

9 files changed

+147
-8
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { it, expect } from '@jest/globals';
2+
3+
import { EmailJSResponseStatus } from '../../models/EmailJSResponseStatus';
4+
import { blockedEmailError } from './blockedEmailError';
5+
6+
it('should return EmailJSResponseStatus', () => {
7+
expect(blockedEmailError()).toBeInstanceOf(EmailJSResponseStatus);
8+
});
9+
10+
it('should return status 403', () => {
11+
expect(blockedEmailError()).toEqual({
12+
status: 403,
13+
text: 'Forbidden',
14+
});
15+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { EmailJSResponseStatus } from '../../models/EmailJSResponseStatus';
2+
3+
export const blockedEmailError = () => {
4+
return new EmailJSResponseStatus(403, 'Forbidden');
5+
};

src/methods/init/init.spec.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { store } from '../../store/store';
66
beforeEach(() => {
77
store.origin = 'https://api.emailjs.com';
88
store.limitRate = 0;
9-
store.blockList = [];
109
store.publicKey = undefined;
1110
});
1211

@@ -18,7 +17,6 @@ describe('sdk v3', () => {
1817
origin: 'https://api.emailjs.com',
1918
publicKey: 'C2JWGTestKeySomething',
2019
limitRate: 0,
21-
blockList: [],
2220
});
2321
});
2422
});
@@ -30,21 +28,24 @@ describe('sdk v4', () => {
3028
expect(store).toEqual({
3129
origin: 'https://api.emailjs.com',
3230
limitRate: 0,
33-
blockList: [],
3431
});
3532
});
3633

3734
it('should call the init method with custom options', () => {
3835
init({
3936
publicKey: 'C2JWGTestKeySomething',
40-
blockList: ['block@email.com'],
37+
blockList: {
38+
list: ['block@email.com'],
39+
},
4140
limitRate: 10000,
4241
});
4342

4443
expect(store).toEqual({
4544
origin: 'https://api.emailjs.com',
4645
publicKey: 'C2JWGTestKeySomething',
47-
blockList: ['block@email.com'],
46+
blockList: {
47+
list: ['block@email.com'],
48+
},
4849
limitRate: 10000,
4950
});
5051
});

src/methods/init/init.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export const init = (
1818

1919
store.publicKey = opts.publicKey;
2020
store.blockHeadless = opts.blockHeadless;
21+
store.blockList = opts.blockList;
2122
store.limitRate = opts.limitRate || store.limitRate;
22-
store.blockList = opts.blockList || store.blockList;
2323
store.origin = opts.origin || origin;
2424
};

src/store/store.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,4 @@ export const store: Options = {
44
origin: 'https://api.emailjs.com',
55
blockHeadless: false,
66
limitRate: 0,
7-
blockList: [],
87
};

src/types/BlockList.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface BlockList {
2+
list?: string[];
3+
watchVariable?: string;
4+
}

src/types/Options.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import type { BlockList } from './BlockList';
2+
13
export interface Options {
24
origin?: string;
35
publicKey?: string;
46
blockHeadless?: boolean;
7+
blockList?: BlockList;
58
limitRate?: number;
6-
blockList?: string[];
79
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import { it, describe, expect } from '@jest/globals';
2+
import { isBlockedValueInParams } from './isBlockedValue';
3+
import type { BlockList } from '../../types/BlockList';
4+
5+
describe('should be disabled', () => {
6+
it('empty block list options', () => {
7+
const blockList: BlockList = {};
8+
9+
expect(isBlockedValueInParams(blockList, {})).toBeFalsy();
10+
});
11+
12+
it('without list', () => {
13+
const blockList: BlockList = {
14+
watchVariable: 'email',
15+
};
16+
17+
expect(isBlockedValueInParams(blockList, {})).toBeFalsy();
18+
});
19+
20+
it('without watchVariable', () => {
21+
const blockList: BlockList = {
22+
list: ['test@emailjs.com'],
23+
};
24+
25+
expect(isBlockedValueInParams(blockList, {})).toBeFalsy();
26+
});
27+
28+
it('without data', () => {
29+
const blockList: BlockList = {
30+
watchVariable: 'email',
31+
list: ['test@emailjs.com'],
32+
};
33+
34+
expect(isBlockedValueInParams(blockList, {})).toBeFalsy();
35+
});
36+
37+
it('wrong type', () => {
38+
const blockList: BlockList = {
39+
watchVariable: 'email',
40+
list: ['test@emailjs.com'],
41+
};
42+
43+
expect(
44+
isBlockedValueInParams(blockList, {
45+
email: ['item', 'item'],
46+
}),
47+
).toBeFalsy();
48+
});
49+
50+
it('not found in the list', () => {
51+
const blockList: BlockList = {
52+
watchVariable: 'email',
53+
list: ['test@emailjs.com', 'bar@emailjs.com'],
54+
};
55+
56+
expect(
57+
isBlockedValueInParams(blockList, {
58+
email: 'foo@emailjs.com',
59+
}),
60+
).toBeFalsy();
61+
});
62+
});
63+
64+
describe('should be enabled', () => {
65+
it('template params', () => {
66+
const blockList: BlockList = {
67+
watchVariable: 'email',
68+
list: ['test@emailjs.com', 'foo@emailjs.com', 'bar@emailjs.com'],
69+
};
70+
71+
expect(
72+
isBlockedValueInParams(blockList, {
73+
email: 'test@emailjs.com',
74+
other: 'other data',
75+
}),
76+
).toBeTruthy();
77+
});
78+
79+
it('form data', () => {
80+
const blockList: BlockList = {
81+
watchVariable: 'email',
82+
list: ['test@emailjs.com', 'foo@emailjs.com', 'bar@emailjs.com'],
83+
};
84+
85+
const formData = new FormData();
86+
formData.append('email', 'test@emailjs.com');
87+
formData.append('other', 'other data');
88+
89+
expect(isBlockedValueInParams(blockList, formData)).toBeTruthy();
90+
});
91+
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import type { BlockList } from '../../types/BlockList';
2+
3+
const isBlockListDisabled = (options: BlockList): boolean => {
4+
return !options.list?.length || !options.watchVariable;
5+
};
6+
7+
const getValue = (data: Record<string, unknown> | FormData, name: string): unknown => {
8+
return data instanceof FormData ? data.get(name) : data[name];
9+
};
10+
11+
export const isBlockedValueInParams = (
12+
options: BlockList,
13+
params: Record<string, unknown> | FormData,
14+
): boolean => {
15+
if (isBlockListDisabled(options)) return false;
16+
17+
const value = getValue(params, options.watchVariable!);
18+
19+
if (typeof value !== 'string') return false;
20+
21+
return options.list!.includes(value);
22+
};

0 commit comments

Comments
 (0)