This repository was archived by the owner on Nov 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathtest.email-event-edit-notification.js
229 lines (214 loc) · 8.85 KB
/
test.email-event-edit-notification.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
var server = require('../lib/unhangout-server'),
expect = require('expect.js'),
Promise = require("bluebird"),
_ = require('underscore')._,
request = require('superagent'),
conf = require('../lib/options'),
utils = require("../lib/utils"),
models = require("../lib/server-models"),
common = require('./common');
var origDelay;
describe("EMAIL EVENT EDIT NOTIFICATION", function() {
beforeEach(function(done) {
common.standardSetup(function() {
origDelay = common.server.options.EVENT_EDIT_NOTIFICATION_DELAY;
common.server.options.EVENT_EDIT_NOTIFICATION_DELAY = 100;
common.startEmailServer(done);
});
});
afterEach(function(done) {
common.standardShutdown(function() {
common.server.options.EVENT_EDIT_NOTIFICATION_DELAY = origDelay;
common.stopEmailServer(done);
});
});
function postEventEdit(user, params) {
return new Promise(function(resolve, reject) {
request.post(common.URL + "/admin/event/" + (params.id || "new"))
.set("x-mock-user", user)
.send(params)
.redirects(0)
.end(function(res) {
if (res.status != 302) {
return reject(new Error("Expected 302, got " + res.status));
}
// next tick.
setTimeout(function() { resolve(res); }, 0);
});
});
};
it("Sends no email on text-only edit", function(done) {
var event = common.server.db.events.get(1);
expect(common.outbox.length).to.be(0);
var warnings = utils.getEventSanitizationWarnings(event);
expect(_.size(warnings)).to.be(0);
postEventEdit("admin1", event.toJSON()).then(function(res) {
expect(common.outbox.length).to.be(0);
setTimeout(function() {
expect(common.outbox.length).to.be(0);
done();
}, 200); // 200ms ought to be long enough for email to process..
}).catch(function(err) { done(err); });
});
it("Sends email on edit containing worrisome HTML", function(done) {
var event = common.server.db.events.get(1);
event.set("description",
"<style>body { color: pink; }</style>" + event.get("description"));
var warnings = utils.getEventSanitizationWarnings(event);
expect(_.size(warnings)).to.be(1);
expect(common.outbox.length).to.be(0);
postEventEdit("admin1", event.toJSON()).then(function(res) {
return common.await(function() { return common.outbox.length == 1; })
}).then(function() {
expect(common.outbox.length).to.be(1);
var msg = common.outbox.shift();
expect(msg.to).to.eql(_.map(conf.UNHANGOUT_MANAGERS, common.recipientify));
expect(msg.subject).to.eql("Unhangout: Event 1 edited");
_.each(["Risky Tags", "style", "color: pink"], function(txt) {
expect(msg.html.indexOf(txt)).to.not.eql(-1);
});
done();
}).catch(function(err) {
done(err);
});
});
it("Throttles multiple edits", function(done) {
var event = common.server.db.events.get(1);
event.set("description",
"<style>body{color:pink}</style>" + event.get("description"));
var origTitle = event.get("title");
expect(common.outbox.length).to.be(0);
Promise.map([1, 2, 3, 4, 5], function(val) {
var json = event.toJSON();
// append the number..
json.title = json.title.substring(0, json.title.length - 1) + val;
return postEventEdit("admin1", json);
}).then(function() {
expect(common.outbox.length).to.be(0);
return common.await(function() { return common.outbox.length > 0; });
}).then(function() {
expect(common.outbox.length).to.be(1);
var msg = common.outbox.shift();
expect(msg.to).to.eql(_.map(conf.UNHANGOUT_MANAGERS, common.recipientify));
expect(msg.subject).to.eql("Unhangout: Event 1 edited");
var expectedTitle = origTitle.substring(0, origTitle.length - 1) + 5;
expect(msg.html.indexOf(_.escape(expectedTitle))).to.not.eql(-1);
done();
}).catch(function(err) {
done(err);
});
});
it("Notifies after event creation with new ID", function(done) {
postEventEdit("superuser1", {
title: "New Event",
description: "Description <style>body{color: worrisome}</style>",
shortName: "shawty"
}).then(function() {
return common.await(function() { return common.outbox.length == 1; });
}).then(function() {
expect(common.outbox.length).to.be(1);
var msg = common.outbox.shift();
var match = /Unhangout: Event (\d+) edited/.exec(msg.subject);
expect(match).to.not.be(null);
var event = common.server.db.events.get(parseInt(match[1]));
expect(event.get("title")).to.be("New Event");
done();
}).catch(function(err) {
done(err);
});
});
it("Issues warnings for all relevant fields", function() {
function checkWarnings(props, expected) {
expect(
utils.getEventSanitizationWarnings(new models.ServerEvent(props))
).to.eql(
expected
);
};
var fields = ["organizer", "description", "overflowMessage"];
checkWarnings({
organizer: "test",
description: "test",
overflowMessage: "test"
}, {});
// mixed content warnings
fields.forEach(function(field) {
var props = {};
props[field] = "test <img src='http://i.imgur.com/E4p6tht.jpg' />";
var errors = {"mixed content": {}};
errors["mixed content"][field] = [{
tagName: "img",
attribName: "src",
change: "removed",
oldValue: "http://i.imgur.com/E4p6tht.jpg",
newValue: null
}];
checkWarnings(props, errors);
});
// Unsafe tag warnings
fields.forEach(function(field) {
var props = {};
props[field] = "test <style>body{color:pink}</style>";
var errors = {"risky tag": {}};
errors["risky tag"][field] = [{
tagName: "style",
change: "removed",
innerHTML: "body{color:pink}"
}];
checkWarnings(props, errors);
});
// Unsafe attribute warnings
fields.forEach(function(field) {
var props = {};
props[field] = "test <span style='color: pink;'>oh no</style>";
var errors = {"risky attribute": {}};
errors["risky attribute"][field] = [{
tagName: "span",
attribName: "style",
change: "removed",
oldValue: "color: pink;",
newValue: null
}];
checkWarnings(props, errors);
});
// Shadowable attribute warnings
fields.forEach(function(field) {
var props = {};
props[field] = "test <span class='login'>oh my</span>";
var errors = {"shadowable attribute": {}};
errors["shadowable attribute"][field] = [{
change: "changed",
tagName: "span",
attribName: "class",
oldValue: "login",
newValue: "userhtml-login"
}];
checkWarnings(props, errors);
});
// Multiple warnings
var props = {
"organizer": "<style>body{color:pink}</style>",
"description": "<span class='login'>oh my</span><img src='http://i.imgur.com/E4p6tht.jpg'>",
"overflowMessage": "<span style='font-family: ugly;'>oy</span>"
};
var errors = {
"risky tag": {organizer: [{
change: "removed", tagName: "style",
innerHTML: "body{color:pink}"
}]},
"shadowable attribute": {description: [{
change: "changed", tagName: "span", attribName: "class",
oldValue: "login", newValue: "userhtml-login"
}]},
"mixed content": {description: [{
change: "removed", tagName: "img", attribName: "src",
oldValue: "http://i.imgur.com/E4p6tht.jpg", newValue: null
}]},
"risky attribute": {overflowMessage: [{
change: "removed", tagName: "span", attribName: "style",
oldValue: "font-family: ugly;", newValue: null
}]}
};
checkWarnings(props, errors);
});
});