-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.test.ts
167 lines (157 loc) · 5.94 KB
/
index.test.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
/* tslint:disable:no-any */
/* tslint:disable:no-object-mutation */
process.env = {
APPINSIGHTS_INSTRUMENTATIONKEY: "foo",
QueueStorageConnection: "foobar",
SPID_BLOB_CONTAINER_NAME: "spidblob",
SPID_BLOB_STORAGE_CONNECTION_STRING: "foobar",
SPID_LOGS_PUBLIC_KEY: `-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDhiXpvLD8UMMUy1T2JCzo/Sj5E
l09Fs0z2U4aA37BrXlSo1DwQ2O9i2XFxXGJmE83siSWEfRlMWlabMu7Yj6dkZvmj
dGIO4gotO33TgiAQcwRo+4pwjoCN7Td47yssCcj9C727zBt+Br+XK7B1bRcqjc0J
YdF4yiVtD7G4RDXmRQIDAQAB
-----END PUBLIC KEY-----`
};
import { format } from "date-fns";
import { toPlainText } from "italia-ts-commons/lib/encrypt";
import { IPString } from "italia-ts-commons/lib/strings";
import { aFiscalCode } from "../../__mocks__/mocks";
import { index, IOutputBinding, SpidMsgItem } from "../index";
const today = format(new Date(), "YYYY-MM-DD");
const aDate = new Date();
const aRSAPrivateKey = `-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQDhiXpvLD8UMMUy1T2JCzo/Sj5El09Fs0z2U4aA37BrXlSo1DwQ
2O9i2XFxXGJmE83siSWEfRlMWlabMu7Yj6dkZvmjdGIO4gotO33TgiAQcwRo+4pw
joCN7Td47yssCcj9C727zBt+Br+XK7B1bRcqjc0JYdF4yiVtD7G4RDXmRQIDAQAB
AoGAJlqjyI4kuAFHN8rNqSWQpTyx9CYrI/ZG60jvAbGIpemnygI1qMPLierigN2u
Gh/aEBSOncZMbBCc083IkmlzlKy3gJH0shgBQrfqGFbqh3i7f/lHkL+lZtXW+fF4
bXo4vdaArHhQW1oKQOHA9BO8uuqCOEaA7OtVLWiZxqe9u80CQQDyuNWZLqlDZT8c
yB6mnLh7KVGY1RYphY0HputmC3Z0qr+bAFT8plNB2SkJwsnD2YSpOj4jzzePZShP
aDNS+LQzAkEA7d/6rtzYVqX4XEvmrdQwXKvq937MgRec7Q6jzmSHSHxBLcsvJ40n
xiBoe1TJWGn866Ug/tBauF8Ws5SgCPVDpwJBAOZc9pzD5HHKjfPLGwwWgiCiPodG
9hnCXu98RL488tgXlnKOBhsj4LEGYiSZctUmhPn4BTIHYTv/ThrPUqbU1HECQDAg
/UucC3mcox+pi8boA9D8R9JDqYUFDg84wxPjayvTWCy3y5apDL8dl4Y8pXBqIW5c
PszPw0tCkglLrQWi+kkCQDzR5FI2eGvXYdkJdAqofbEFDdP+N0ZMWdJITVntrhZO
zsAyYUBrD/FpfHSA5UY9UsldiilvJeCzYbM6Rm1fpmc=
-----END RSA PRIVATE KEY-----`;
const aSpidMsgItem: SpidMsgItem = {
createdAt: aDate,
createdAtDay: today,
fiscalCode: aFiscalCode,
ip: "192.168.1.6" as IPString,
requestPayload:
"<?xml version='1.0' encoding='UTF-8'?><note ID='AAAA_BBBB'><to>Azure</to><from>Azure</from><heading>Reminder</heading><body>New append from local dev - REQUEST</body></note>",
responsePayload:
"<?xml version='1.0' encoding='UTF-8'?><note ID='AAAA_BBBB'><to>Azure</to><from>Azure</from><heading>Reminder</heading><body>New append from local dev - RESPONSE</body></note>",
spidRequestId: "AAAA_BBBB"
};
const anotherSpidMsgItem: SpidMsgItem = {
createdAt: aDate,
createdAtDay: today,
fiscalCode: aFiscalCode,
ip: "192.168.1.7" as IPString,
requestPayload:
"<?xml version='1.0' encoding='UTF-8'?><note ID='CCCC_DDDD'><to>Azure</to><from>Azure</from><heading>Reminder</heading><body>New append from local dev - REQUEST</body></note>",
responsePayload:
"<?xml version='1.0' encoding='UTF-8'?><note ID='CCCC_DDDD'><to>Azure</to><from>Azure</from><heading>Reminder</heading><body>New append from local dev - RESPONSE</body></note>",
spidRequestId: "CCCC_DDDD"
};
describe("StoreSpidLogs", () => {
it("should fail on invalid payload published into the queue", async () => {
const mockedContext = {
bindings: {
spidMsgItem: aSpidMsgItem
},
done: jest.fn(),
log: {
error: jest.fn()
}
};
const blobItem = await index(mockedContext as any, {
...aSpidMsgItem,
ip: "XX" as IPString
});
expect(blobItem).toBeUndefined();
});
it("should store both SPID request/response published into the queue", async () => {
const mockedContext = {
bindings: {
spidMsgItem: aSpidMsgItem
},
done: jest.fn(),
log: {
error: jest.fn()
}
};
const blobItem = await index(mockedContext as any, aSpidMsgItem);
const blob = blobItem as IOutputBinding;
const encryptedSpidBlobItem = blob.spidRequestResponse;
const decryptedRequestPayload = toPlainText(
aRSAPrivateKey,
encryptedSpidBlobItem.encryptedRequestPayload
);
const decryptedResponsePayload = toPlainText(
aRSAPrivateKey,
encryptedSpidBlobItem.encryptedResponsePayload
);
expect(decryptedRequestPayload.value).toEqual(aSpidMsgItem.requestPayload);
expect(decryptedResponsePayload.value).toEqual(
aSpidMsgItem.responsePayload
);
});
it("should encrypt two different messages with the same Cipher instance and decrypt with another one", async () => {
const mockedContext = {
bindings: {
spidMsgItem: aSpidMsgItem
},
done: jest.fn(),
log: {
error: jest.fn()
}
};
const blobItem = await index(mockedContext as any, aSpidMsgItem);
const blob = blobItem as IOutputBinding;
const encryptedSpidBlobItem = blob.spidRequestResponse;
const decryptedRequestPayload = toPlainText(
aRSAPrivateKey,
encryptedSpidBlobItem.encryptedRequestPayload
);
const decryptedResponsePayload = toPlainText(
aRSAPrivateKey,
encryptedSpidBlobItem.encryptedResponsePayload
);
expect(decryptedRequestPayload.value).toEqual(aSpidMsgItem.requestPayload);
expect(decryptedResponsePayload.value).toEqual(
aSpidMsgItem.responsePayload
);
const anotherMockedContext = {
bindings: {
spidMsgItem: anotherSpidMsgItem
},
done: jest.fn(),
log: {
error: jest.fn()
}
};
const secondBlobItem = await index(
anotherMockedContext as any,
anotherSpidMsgItem
);
const secondBlob = secondBlobItem as IOutputBinding;
const secondEncryptedSpidBlobItem = secondBlob.spidRequestResponse;
const secondDecryptedRequestPayload = toPlainText(
aRSAPrivateKey,
secondEncryptedSpidBlobItem.encryptedRequestPayload
);
const secondDecryptedResponsePayload = toPlainText(
aRSAPrivateKey,
secondEncryptedSpidBlobItem.encryptedResponsePayload
);
expect(secondDecryptedRequestPayload.value).toEqual(
anotherSpidMsgItem.requestPayload
);
expect(secondDecryptedResponsePayload.value).toEqual(
anotherSpidMsgItem.responsePayload
);
});
});