forked from amazon-connect/amazon-connect-streams
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.spec.js
209 lines (201 loc) · 9.32 KB
/
log.spec.js
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
require("../unit/test-setup.js");
const { expect } = require("chai");
describe('Logger', function() {
describe('LogEntry.withException()', function() {
it('extracts type, message, and stack when called with object inheriting from Error', function() {
var message = "my syntax error";
var err = SyntaxError(message);
var loggedException = connect.getLog().error("Error encountered").withException(err).exception;
assert.equal(loggedException.type, "SyntaxError");
assert.equal(loggedException.message, message);
assert.deepEqual(loggedException.stack, err.stack.split('\n'));
});
it('extracts type, message, and stack when called with object error returned by AWS client', function() {
var err = { code: "TestError", message: "error message" };
var loggedException = connect.getLog().error("Error encountered").withException(err).exception;
assert.equal(loggedException.type, err.code);
assert.equal(loggedException.message, err.message);
assert.deepEqual(loggedException.stack, []);
});
it('determines type using Object.prototype.toString() if called with unrecognized object', function() {
var err = "error message";
var loggedException = connect.getLog().error("Error encountered").withException(err).exception;
assert.equal(loggedException.type, "[object String]");
assert.isUndefined(err.message);
assert.deepEqual(loggedException.stack, []);
});
});
describe('Exceptions in logger class should not be thrown', function() {
it('Set wrong level should not throw error to out side', function() {
var logger = connect.getLog();
logger.setLogLevel("invalid log level");
assert.equal(logger._logLevel, 30);
})
});
describe('Logger.withObject()', function(){
it('Log should not contain websocket auth token', function(){
var obj = {
"webSocketTransport": {
"url": "wss://15isv8flsl.execute-api.us-west-2.amazonaws.com/gamma/?AuthToken=QVFJREFIa==",
"transportLifeTimeInSeconds": 3869,
"expiry": "2021-03-09T20:03:34.625Z"
}
};
var expectedObj = [{
"webSocketTransport": {
"url": "wss://15isv8flsl.execute-api.us-west-2.amazonaws.com/gamma/?[redacted]",
"transportLifeTimeInSeconds": 3869,
"expiry": "2021-03-09T20:03:34.625Z"
}
}];
var loggedObject = connect.getLog().trace("AWSClient: <-- Operation '%s' succeeded.").withObject(obj);
assert.deepEqual(loggedObject.objects, expectedObj);
});
it('Log should have VoiceID customerId, CustomerId, SpeakerId, or CustomerSpeakerId values as MD5 digest', function(){
var obj = {
"customerId":"69b39df2-a06a-4f66-a9a9-ad699004d245",
"CustomerId":"52d67abc-9aa8-4276-9de0-8dbe48220a1c",
"SpeakerId":"1c58b4c5-6dff-4182-9eab-479aa2e78a3c",
"CustomerSpeakerId":"89376e97-9370-481f-ba55-afdca7854c09",
"DomainId":"XKkbTGuCuCnYgOD1iPHzPv"
};
var expectedObj = [{
"customerId":"[obfuscated value] 86afb5d2f86f0ddbe298a8dd895bf5aa",
"CustomerId":"[obfuscated value] a80f842e82ba7031ae6e22d939b179cf",
"SpeakerId":"[obfuscated value] 7e7d858bbf580fdaba583bfb0bf4a118",
"CustomerSpeakerId":"[obfuscated value] 82d1d42ff66faf8403e8eda577bb9e10",
"DomainId":"XKkbTGuCuCnYgOD1iPHzPv"
}];
var loggedObject = connect.getLog().trace("AWSClient: <-- Operation '%s' succeeded.").withObject(obj);
assert.deepEqual(loggedObject.objects, expectedObj);
});
it('Log should not break if the value of url or text is not a string', function(){
var obj = {
"webSocketTransport": {
"url": {
"text": "wss://15isv8flsl.execute-api.us-west-2.amazonaws.com/gamma/?AuthToken=QVFJREFIa==",
"url":[]
},
"text":545,
"transportLifeTimeInSeconds": 3869,
"expiry": "2021-03-09T20:03:34.625Z"
}
};
var expectedObj = [{
"webSocketTransport": {
"url": {
"text":"wss://15isv8flsl.execute-api.us-west-2.amazonaws.com/gamma/?[redacted]",
"url":[]
},
"text":545,
"transportLifeTimeInSeconds": 3869,
"expiry": "2021-03-09T20:03:34.625Z"
}
}];
var loggedObject = connect.getLog().trace("AWSClient: <-- Operation '%s' succeeded.").withObject(obj);
assert.deepEqual(loggedObject.objects, expectedObj);
})
});
describe('Logger.withCrossOriginEventObject()', function(){
it('Log should be empty as none of these are the right fields.', function(){
var obj = {
"webSocketTransport": {
"url": "wss://15isv8flsl.execute-api.us-west-2.amazonaws.com/gamma/?AuthToken=QVFJREFIa==",
"transportLifeTimeInSeconds": 3869,
"expiry": "2021-03-09T20:03:34.625Z"
}
};
var expectedObj = [{
"webSocketTransport": {
"url": "wss://15isv8flsl.execute-api.us-west-2.amazonaws.com/gamma/?[redacted]",
"transportLifeTimeInSeconds": 3869,
"expiry": "2021-03-09T20:03:34.625Z"
}
}];
var loggedObject = connect.getLog().trace("AWSClient: <-- Operation '%s' succeeded.").withCrossOriginEventObject(obj);
assert.notDeepEqual(loggedObject.objects, expectedObj);
assert.deepEqual([{}], loggedObject.objects);
});
it('Log should not break if the value of url or text is not a string', function(){
var obj = {
"bubbles": true
};
var expectedObj = [{
"bubbles": true
}];
var loggedObject = connect.getLog().trace("AWSClient: <-- Operation '%s' succeeded.").withObject(obj);
assert.deepEqual(loggedObject.objects, expectedObj);
});
});
describe('LogEntry fields', () => {
var sandbox = sinon.createSandbox();
let agentResourceId = "id";
before(() => {
connect.agent.initialized = true;
});
after(() => {
connect.agent.initialized = false;
});
it("provides the correct agentResourceId", () => {
let log = connect.getLog().info("hi");
assert.equal(log.getAgentResourceId(), agentResourceId);
});
it("should have the tabId property", () => {
let log = connect.getLog().info("hi");
expect(log.tabId).not.to.be.undefined;
});
it("should have the contextLayer property", () => {
let log = connect.getLog().info("hi");
expect(log.contextLayer).not.to.be.undefined;
});
it("LogEntry should take contextLayer input in fromObject invocation", () => {
let log = connect.LogEntry.fromObject({
contextLayer: 'contextLayer',
level: null,
text: "text",
loggerId: "loggerId",
exception: null,
objects: {}
})
expect(log.contextLayer).to.be.string('contextLayer');
})
it("LogEntry tabId should contain 'tabId' when passed in fromObject invocation", () => {
let log = connect.LogEntry.fromObject({
tabId: 'tabId',
level: null,
text: "text",
loggerId: "loggerId",
exception: null,
objects: {}
})
expect(log.tabId).to.be.string('tabId');
})
it("LogEntry tabId should contain null when passed in fromObject invocation", () => {
let log = connect.LogEntry.fromObject({
tabId: null,
level: null,
text: "text",
loggerId: "loggerId",
exception: null,
objects: {}
})
expect(log.tabId).to.be.null;
})
it("LogEntry tabId should be null when not passed in fromObject invocation", () => {
let log = connect.LogEntry.fromObject({
level: null,
text: "text",
loggerId: "loggerId",
exception: null,
objects: {}
})
expect(log.tabId).to.be.null;
})
it("includes the agentResourceId when printed, and the correct log string", () => {
let spy = sandbox.spy(connect, "sprintf");
let log = connect.getLog().info("hello");
sandbox.assert.calledWithMatch(spy, sinon.match.string, sinon.match.string, sinon.match.string, agentResourceId, "hello");
sandbox.restore();
});
});
});