-
Notifications
You must be signed in to change notification settings - Fork 288
Expand file tree
/
Copy pathEncryptRequestSpec.js
More file actions
133 lines (129 loc) · 5.91 KB
/
EncryptRequestSpec.js
File metadata and controls
133 lines (129 loc) · 5.91 KB
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
import { FetchRequest } from '../../../src/common/util/FetchRequest';
import { EncryptRequest, getServiceKey } from '../../../src/common/util/EncryptRequest';
describe('EncryptRequest', () => {
const serverUrl = 'http://fake.iserver.com/iserver';
const options = {
url: 'http://fake.iserver.com/iserver/iserver/services/security/svckeys/keyIDNAME.json',
method: 'get'
};
it('request fail', (done) => {
const spyGet = spyOn(FetchRequest, 'get').and.returnValue('');
const spyPost = spyOn(FetchRequest, 'post').and.returnValue({});
const encryptRequest = new EncryptRequest(serverUrl);
encryptRequest.request(options).then((result) => {
expect(result).toBeUndefined();
expect(spyGet.calls.count()).toBe(1);
expect(spyPost.calls.count()).toBe(0);
spyGet.calls.reset();
spyPost.calls.reset();
done();
});
});
it('request succeed', (done) => {
const spyGet = spyOn(FetchRequest, 'get').and.callFake(() => {
const response = {
keyLength: 2048,
publicKey:
'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2BQweGm/+YpzPn/QaRvkOYQQOwO0LI52NSdtJDehARLvFLfKfpaXs2Qks2VISYX8bl1aBRtS5m5/Z8mdo41k2XM9sRUKldF5M0gTUqKWUnDkS6INnNfsW4VsHNizIiZY7YvQA2cwl/4eYm4YF5Qy3KugPGrxt5KxZvh8O7b6si9JwLwIx53Y5oRbFttCHdjWR4mqFTzTR/yD4K1xYo/fizdvqUmWLhfpirHQsWv3WLaUOdEj36nDGBxuqahQ5JbH3VeASdbJQRTp+0QQcfpZ1x0GxXJWstemCrCUETQIQczYtj98qxSqknC8HZQhDz8F31NFV4341vrGKgOzrsq5HQIDAQAB',
keyScheme: 'PKCS#8'
};
return Promise.resolve({ json: () => Promise.resolve(response) });
});
const spyPost = spyOn(FetchRequest, 'post').and.callFake(() => {
const response = {
blockedUrlRegex: {
DELETE: [],
POST: [],
GET: ['.*/services/security/svckeys/[a-zA-Z]+\\.json'],
PUT: []
},
tunnelUrl:
'http://fake.iserver.com/iserver/services/security/tunnel/v1/tunnels/UUV6U25KNkVSNHhnQTYzcFVOTnZlNm9KVEVWZE4yeXVPNHlEVHAzeEZ2UnQ3WmtxS29qZURXQi9HcDBDYjNtM1FaOHFabGQwclduNGNzNjFjck1PbmFyakdOcGxQR25id2dQZ2ljY1NWdU5lMWg3dzV4UUtDQktyQ3doc3MzWnhmTkdNNWU3V01FZ21XNWJsR3pVdEtJenRpQXRTQ2RYQVBkN1oxRGNhNTh1a2pGSG1rUUZBeStjYVZPMXJ3NXFJcUdxN05ack1SNEEzNWRwNEZVNWV6ME96anYwN0tQempHZVl2U2VHa3YxRlc1R3ZXUW9KNmgwb290MjE1cEVZT2xNdzAwdXBidWpNOTQ2ck1iR1FFajFVUEtpeSt0OU9xdzdON0ZpZnhkTjEvRlF2cFpHN3ZBalVPV0ovVkNDdU9nN3RyU081SlhRMlZVQXhsbDlkMHZnPT0=.json'
};
return Promise.resolve({ json: () => Promise.resolve(response) });
});
const spyCommit = spyOn(FetchRequest, 'commit').and.callFake(() => {
const response =
'HAsHE/ok/jROEySWBxWSr2FTLXcnIkeFAYzig+V7NGalR0f/VnBorkAOyOnbCSq9nM3YWrhEUFaWwAouSDeEVqe+BLuIA+7KmCBfD7hh+qyM0lC5cvZ8vOIjsI3eqhHhiPOi+IQLGHQsRbFl8hSkE0XU1GIojqjppSEAxW5jhFC2bH5hdCt/+PKuHPhATElgJqOI6FJHpVpbLWiqoP7WMYVYvZm7wubYCQIG77LUSivbUQ61gjW0mevsKRdoiRl8fafV8Zq5D+QBbCy+Mn4rWXDC+gjwvyyYxEdOixALJgfnjWL48RRHxvITPapzbEsEkcnZiu+INSULcT60BeuduKzxp+hUg6Q8sn2Bu//CNk0NlGMeT5hqTON72iI4GBgfEOnGrcBHjsT/N2jX0NnVz1bgR6B9O6TpQQr3zkjVPidw8ElSO+lM8P5AuRqtNH9ajYt2uDwWBhbG+OfyR4hKIJ9V5aDhAwkIzkUerRP78Colsg==';
return Promise.resolve({ text: () => Promise.resolve(response) });
});
const encryptRequest = new EncryptRequest(serverUrl);
encryptRequest.encryptAESKey = 'SLbsaRbf4Rou8Bju';
encryptRequest.encryptAESIV = 'rzLM7Z4RJGFd';
encryptRequest.request(options).then(res => res.json()).then((result) => {
expect(result).toBe('l3nQtAUM4li87qMfO68exInHVFQ5gS3a6pb8ySIbib8=');
expect(spyGet.calls.count()).toBe(1);
expect(spyPost.calls.count()).toBe(1);
expect(spyCommit.calls.count()).toBe(1);
expect(encryptRequest.tunnelUrl).not.toBeUndefined();
encryptRequest.request(options).then(res => res.json()).then(result => {
expect(result).toBe('l3nQtAUM4li87qMfO68exInHVFQ5gS3a6pb8ySIbib8=');
expect(FetchRequest.get.calls.count()).toBe(1);
expect(FetchRequest.post.calls.count()).toBe(1);
expect(FetchRequest.commit.calls.count()).toBe(2);
spyGet.calls.reset();
spyPost.calls.reset();
spyCommit.calls.reset();
done();
});
});
});
it('getServiceKey', (done) => {
const serviceKey = 'l3nQtAUM4li87qMfO68exInHVFQ5gS3a6pb8ySIbib8=';
const encrptSpec = {
keyLength: 256,
attributes: 'abcd',
version: '1.1',
algorithm: 'AES'
};
const spyEncrypt = spyOn(EncryptRequest.prototype, 'request').and.callFake((options) => {
if (options.url.includes('keyID1')) {
return { json: () => Promise.resolve(serviceKey)};
}
return { json: () => Promise.resolve(null)};
});
const spyGet = spyOn(FetchRequest, 'get').and.callFake((url) => {
if (url.includes('map-China100')) {
return Promise.resolve(
new Response(
JSON.stringify([
{
serviceEncryptInfo: {
encrptSpec,
updateTime: 'Fri Mar 15 08:52:15 CST 2024',
encrptKeyID: 'keyID1'
},
name: 'map-China100/rest'
}
])
)
);
}
return Promise.resolve(
new Response(
JSON.stringify([
{
serviceEncryptInfo: {
encrptSpec,
updateTime: 'Fri Mar 15 08:52:15 CST 2024',
encrptKeyID: 'keyID2'
},
name: 'data-china100/rest'
}
])
)
);
});
getServiceKey('http://localhost:8090/iserver/services/map-China100/rest/maps/China').then(res => {
expect(res).not.toBeUndefined();
expect(res.serviceKey).toBe(serviceKey);
expect(res.algorithm).toBe(encrptSpec.algorithm);
getServiceKey('http://localhost:8090/iserver/services/data-China100/rest/data/datasources/China/datasets/Airport_pt').then(res => {
expect(res).toBeUndefined();
spyGet.calls.reset();
spyEncrypt.calls.reset();
done();
});
});
});
});