-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathassignVar-getVar.js
More file actions
215 lines (195 loc) · 8.75 KB
/
Copy pathassignVar-getVar.js
File metadata and controls
215 lines (195 loc) · 8.75 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
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
210
211
212
213
214
215
const Lab = require('lab'),
lab = exports.lab = Lab.script(),
describe = lab.experiment,
it = lab.it,
specHelpers = require('../spec-helpers'),
testRunner = specHelpers.testRunner,
renderString = specHelpers.renderString;
const AssignVarHelper = require('../../helpers/assignVar')[0];
describe('assignVar and getVar helpers', function() {
const generateOverflowString = (length, char) => {
return `${[...Array(length).keys()].map(_=>char).join("")}`;
}
const context = {
value1: "Big",
value2: "Commerce",
value3: 12,
value4: -12.34,
valueUnderStringBuffer: generateOverflowString(AssignVarHelper.max_length-1, "U"),
// Bug in testing suite, strings in context that are over or equal to the max length are not being sent to the
// helpers. instead an `undefined` is being sent. To handle this we can use a template string in the tests
// directly to pretend that the string is being sent to the helpers.
valueFillStringBuffer: generateOverflowString(AssignVarHelper.max_length, "F"),
valueOverflowStringBufferBy1: generateOverflowString(AssignVarHelper.max_length+1, "O"),
valueEmptyString: "",
valueNull: null,
valueEncodedSafeString: "<script>alert('XSS')</script>",
};
const runTestCases = testRunner({context});
it('should throw an exception if the assignVar key is not a string', function (done) {
renderString('{{assignVar 1 2}}').catch(_ => {
done();
});
});
it('should throw an exception if the getVar key is not a string', function (done) {
renderString('{{getVar 2}}').catch(_ => {
done();
});
});
it('should throw an exception if the assignVar value is not a string or number', function (done) {
renderString('{{assignVar "key" true}}').catch(_ => {
done();
});
});
it('should assign and get variables', function(done) {
runTestCases([
{
// New test case: Assigning a variable with an empty string value
input: "{{assignVar 'data1' valueEmptyString}}{{getVar 'data1'}}",
output: '',
},
{
// New test case: Assigning a variable with a string value, then delete it by assigning null
input: "{{assignVar 'data1' value1}}{{getVar 'data1'}} {{assignVar 'data1' null}}{{getVar 'data1'}} {{assignVar 'data1' value1}}{{getVar 'data1'}} {{assignVar 'data1' valueNull}}{{getVar 'data1'}}",
output: 'Big Big ',
},
{
// New test case: Assigning a variable with a string value, then delete it by assigning undefined
input: "{{assignVar 'data1' value1}}{{getVar 'data1'}} {{assignVar 'data1' undefined}}{{getVar 'data1'}}",
output: 'Big ',
},
{
input: "{{assignVar 'data1' value1}}{{assignVar 'data2' 12}}{{getVar 'data1'}} {{getVar 'data2'}}",
output: 'Big 12',
},
{
input: "{{assignVar 'data1' value1}}{{assignVar 'data2' value4}}{{getVar 'data1'}} {{getVar 'data2'}}",
output: 'Big -12.34',
},
{
input: "{{assignVar 'data1' value1}}{{assignVar 'data2' value2}}{{getVar 'data1'}}{{getVar 'data2'}}",
output: 'BigCommerce',
},
{
input: "{{assignVar 'data1' null}}{{assignVar 'data2' undefined}}{{getVar 'data1'}}{{getVar 'data2'}}",
output: '',
},
], done);
});
// Check to see if the assignVar still works when the situation is that no data has been stored yet, and a delete instruction is given.
it('should accept null and undefined as input before any variables are stored', function(done) {
runTestCases([
{
input: "{{assignVar 'data1' null}}{{assignVar 'data2' undefined}}{{getVar 'data1'}}{{getVar 'data2'}}",
output: '',
},
], done);
});
it('should return empty string if variable is not defined', function(done) {
runTestCases([
{
input: "{{getVar 'data3'}}",
output: '',
},
{
input: "{{assignVar 'data1' value1}}{{getVar 'data3'}}",
output: '',
},
], done);
});
it('should return empty string if variable is not *yet* defined', function(done) {
runTestCases([
{
input: "{{getVar 'data1'}}{{assignVar 'data1' value1}}",
output: '',
},
], done);
});
it('should return integers with correct type', function(done) {
runTestCases([
{
input: "{{assignVar 'data1' value3}}{{assignVar 'data2' 10}}{{getVar 'data1'}} + {{getVar 'data2'}} = {{add (getVar 'data1') (getVar 'data2')}}",
output: '12 + 10 = 22',
},
], done);
});
it('should accept a "SafeString" object as a valid alternative to a string (results should be a string when stored)', function(done) {
runTestCases([
{
input: "{{assignVar 'data1' (encodeHtmlEntities valueEncodedSafeString)}}{{getVar 'data1'}}",
output: '&#x3C;script&#x3E;alert(&#x27;XSS&#x27;)&#x3C;/script&#x3E;', // SAY NO TO XSS
},
], done);
});
it(`should accept a string up to the maximum length of the buffer (Max length: ${AssignVarHelper.max_length})`, function(done) {
runTestCases([
{
input: "{{assignVar 'data1' valueUnderStringBuffer}}{{getVar 'data1'}}",
output: `${context.valueUnderStringBuffer}`,
},
], done);
});
it(`should throw an error if buffer is filled (Max length: ${AssignVarHelper.max_length})`,
function(done) {
renderString(`{{assignVar "fill" "${context.valueFillStringBuffer}"}}`).catch(_ => {
// If inline injection of string is not done, the test will fail when it should pass via the catch.
// See context above for more information.
done();
});
}
);
it(`should throw an error if buffer is overflowed (Max length: ${AssignVarHelper.max_length})`,
function(done) {
renderString(`{{assignVar 'overflow' "${context.valueOverflowStringBufferBy1}"}}`).catch(_ => {
// If inline injection of string is not done, the test will fail when it should pass via the catch.
// See context above for more information.
done();
});
}
);
it(`should allow for the writing of up to and including maximum variables (Maximum: ${AssignVarHelper.max_keys})`, function(done) {
runTestCases([
{
input: `{{#for 1 ${AssignVarHelper.max_keys}}}{{assignVar (multiConcat 'data' this.$index) this.$index}}{{getVar (multiConcat 'data' this.$index)}}{{/for}}`,
output: [...Array(50).keys()]
.map(n=>n+1) // Start at 1
.join(""),
},
], done);
});
it(`should fail when creating more than the maximum variables (Overflow at: ${AssignVarHelper.max_keys+1})`,
function(done) {
renderString(`{{#for 1 ${AssignVarHelper.max_keys+1}}}{{assignVar (multiConcat 'data' this.$index) this.$index}}{{getVar (multiConcat 'data' this.$index)}}{{/for}}`,
).catch(_ => {
done();
});
}
);
it("should allow for more variable assignments when some are deleted", function(done) {
runTestCases([
{
input: `
{{~#for 1 ${AssignVarHelper.max_keys}}}{{assignVar (multiConcat 'data' this.$index) this.$index}}{{getVar (multiConcat 'data' this.$index)}}{{/for}}
{{~assignVar 'data1' null}}
{{~assignVar 'data2' null}}
{{~#for ${AssignVarHelper.max_keys+1} ${AssignVarHelper.max_keys+2}}}{{assignVar (multiConcat 'data' this.$index) this.$index}}{{getVar (multiConcat 'data' this.$index)}}{{/for}}
`.trim(),
output: [...Array(50).keys()]
.map(n=>n+1) // Start at 1
.join("") + "51" + "52",
},
], done);
});
it('should return undefined accessing proto/constructor', function(done) {
runTestCases([
{
input: "{{getVar '__proto__'}}",
output: '',
},
{
input: "{{getVar 'constructor'}}",
output: '',
},
], done);
});
});