forked from viruschidai/validate-vat-ts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.spec.ts
193 lines (175 loc) · 6.08 KB
/
index.spec.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import validateVat, {
CountryCodes,
ViesValidationResponse,
ViesServerError,
ViesClientError,
VAT_TEST_SERVICE_URL
} from "./index";
describe("validateVat()", () => {
describe("Using VIES prod service", () => {
test("should return true if it is a valid VAT number", async () => {
const result = (await validateVat(
CountryCodes.Netherlands,
"853746333B01"
)) as ViesValidationResponse;
expect(result.valid).toBeTruthy();
});
test("should throw INVALID_INPUT when VAT number is empty", async done => {
try {
await validateVat(CountryCodes.Germany, "");
} catch (e) {
expect(e).toBeInstanceOf(ViesServerError);
expect((e as ViesServerError).message).toBe("INVALID_INPUT");
done();
}
});
test("should throw INVALID_INPUT when VAT number is invalid", async () => {
const result = (await validateVat(CountryCodes.Germany, "853746333B")) as ViesValidationResponse;
expect(result.valid).toBeFalsy();
});
test("should throw Error when failed to call service", async done => {
try {
await validateVat(
CountryCodes.Germany,
"test",
"http://www.vattest123.com"
);
} catch (e) {
expect(e).toBeInstanceOf(Error);
done();
}
});
});
describe("Using VIES testing service", () => {
test("should return true if VAT number is 100", async () => {
const result = (await validateVat(
CountryCodes.Finland,
"100",
VAT_TEST_SERVICE_URL
)) as ViesValidationResponse;
expect(result.valid).toBeTruthy();
});
test("should return false if VAT number is 200", async () => {
const result = (await validateVat(
CountryCodes.Germany,
"200",
VAT_TEST_SERVICE_URL
)) as ViesValidationResponse;
expect(result.valid).toBeFalsy();
});
test("should throw INVALID_INPUT when VAT number is 201", async done => {
try {
await validateVat(CountryCodes.Germany, "201", VAT_TEST_SERVICE_URL);
} catch (e) {
expect(e).toBeInstanceOf(ViesServerError);
expect((e as ViesServerError).message).toBe("INVALID_INPUT");
done();
}
});
test("should throw INVALID_REQUESTER_INFO when VAT number is 202", async done => {
try {
await validateVat(CountryCodes.Germany, "202", VAT_TEST_SERVICE_URL);
} catch (e) {
expect(e).toBeInstanceOf(ViesServerError);
expect((e as ViesServerError).message).toBe("INVALID_REQUESTER_INFO");
done();
}
});
test("should throw SERVICE_UNAVAILABLE when VAT number is 300", async done => {
try {
await validateVat(CountryCodes.Germany, "300", VAT_TEST_SERVICE_URL);
} catch (e) {
expect(e).toBeInstanceOf(ViesServerError);
expect((e as ViesServerError).message).toBe("SERVICE_UNAVAILABLE");
done();
}
});
test("should throw MS_UNAVAILABLE when VAT number is 301", async done => {
try {
await validateVat(CountryCodes.Germany, "301", VAT_TEST_SERVICE_URL);
} catch (e) {
expect(e).toBeInstanceOf(ViesServerError);
expect((e as ViesServerError).message).toBe("MS_UNAVAILABLE");
done();
}
});
test("should throw TIMEOUT when VAT number is 302", async done => {
try {
await validateVat(CountryCodes.Germany, "302", VAT_TEST_SERVICE_URL);
} catch (e) {
expect(e).toBeInstanceOf(ViesServerError);
expect((e as ViesServerError).message).toBe("TIMEOUT");
done();
}
});
test("should throw VAT_BLOCKED when VAT number is 400", async done => {
try {
await validateVat(CountryCodes.Germany, "400", VAT_TEST_SERVICE_URL);
} catch (e) {
expect(e).toBeInstanceOf(ViesServerError);
expect((e as ViesServerError).message).toBe("VAT_BLOCKED");
done();
}
});
test("should throw IP_BLOCKED when VAT number is 401", async done => {
try {
await validateVat(CountryCodes.Germany, "401", VAT_TEST_SERVICE_URL);
} catch (e) {
expect(e).toBeInstanceOf(ViesServerError);
expect((e as ViesServerError).message).toBe("IP_BLOCKED");
done();
}
});
test("should throw GLOBAL_MAX_CONCURRENT_REQ when VAT number is 500", async done => {
try {
await validateVat(CountryCodes.Germany, "500", VAT_TEST_SERVICE_URL);
} catch (e) {
expect(e).toBeInstanceOf(ViesServerError);
expect((e as ViesServerError).message).toBe(
"GLOBAL_MAX_CONCURRENT_REQ"
);
done();
}
});
test("should throw GLOBAL_MAX_CONCURRENT_REQ_TIME when VAT number is 501", async done => {
try {
await validateVat(CountryCodes.Germany, "501", VAT_TEST_SERVICE_URL);
} catch (e) {
expect(e).toBeInstanceOf(ViesServerError);
expect((e as ViesServerError).message).toBe(
"GLOBAL_MAX_CONCURRENT_REQ_TIME"
);
done();
}
});
test("should throw MS_MAX_CONCURRENT_REQ when VAT number is 600", async done => {
try {
await validateVat(CountryCodes.Germany, "600", VAT_TEST_SERVICE_URL);
} catch (e) {
expect(e).toBeInstanceOf(ViesServerError);
expect((e as ViesServerError).message).toBe("MS_MAX_CONCURRENT_REQ");
done();
}
});
test("should throw MS_MAX_CONCURRENT_REQ_TIME when VAT number is 601", async done => {
try {
await validateVat(CountryCodes.Germany, "601", VAT_TEST_SERVICE_URL);
} catch (e) {
expect(e).toBeInstanceOf(ViesServerError);
expect((e as ViesServerError).message).toBe(
"MS_MAX_CONCURRENT_REQ_TIME"
);
done();
}
});
test("should throw SERVICE_UNAVAILABLE for all other cases", async done => {
try {
await validateVat(CountryCodes.Germany, "700", VAT_TEST_SERVICE_URL);
} catch (e) {
expect(e).toBeInstanceOf(ViesServerError);
expect((e as ViesServerError).message).toBe("SERVICE_UNAVAILABLE");
done();
}
});
});
});