summaryrefslogtreecommitdiff
path: root/web-ui/test/spec/mail_view/ui/mail_view.spec.js
blob: a99eba59112818cce4e175921853723cb01ef26d (plain)
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
describeComponent('mail_view/ui/mail_view', function () {
  'use strict';

  var mail;

  var testData;

  beforeEach(function () {
    mail = {ident: 1, header: { date: '12/12/12T12:12' }, tags: ['inbox']};
    testData = {mail: Pixelated.testData().parsedMail.simpleTextPlain};
    Pixelated.mockBloodhound();
    this.setupComponent('<div></div>', {mail: mail});
  });

  it('triggers mail:want on ui:openMail', function () {
    var spyEvent = spyOnEvent(document, Pixelated.events.mail.want);

    this.setupComponent('<div></div>', {ident: mail.ident });

    expect(spyEvent).toHaveBeenTriggeredOn(document);
    expect(spyEvent.mostRecentCall.data.mail).toEqual(1);
  });

  it('triggers mail.highlightMailContent when receiving mail.here', function () {
    var hightlightEvent = spyOnEvent(document,Pixelated.events.mail.highlightMailContent);
    this.component.trigger(this.component, Pixelated.events.mail.here);
    expect(hightlightEvent).toHaveBeenTriggeredOn(document);
  });

  it('triggers dispatchers.rightPane.openNoMessageSelected when getting mail.notFound', function () {
    var openNoMessageSelectedEvent = spyOnEvent(document, Pixelated.events.dispatchers.rightPane.openNoMessageSelected);

    this.component.trigger(this.component, Pixelated.events.mail.notFound);

    expect(openNoMessageSelectedEvent).toHaveBeenTriggeredOn(document);
  });

  it('removes the tag from the mail when the tag label is clicked', function() {
    var updateSpy = spyOnEvent(document, Pixelated.events.mail.tags.update);

    testData.mail.tags = ['inbox', 'other'];
    this.component.displayMail({}, testData);
    this.component.removeTag('inbox');

    expect(updateSpy).toHaveBeenTriggeredOn(document);
    expect(updateSpy.mostRecentCall.data.tags).toEqual(['other']);
  });

  it('removes the tag from email even if tag is highlighted', function () {
    var updateSpy = spyOnEvent(document, Pixelated.events.mail.tags.update);

    testData.mail.tags = ['tag', 'other'];
    this.component.displayMail({}, testData);

    var inboxTag = this.component.$node.find('.mail-read-view__header-tags-tag[data-tag="tag"]');
    inboxTag.html('<em class="search-highlight">' + inboxTag.text() + '</em>');
    this.component.$node.find('.search-highlight').click();

    expect(updateSpy).toHaveBeenTriggeredOn(document);
    expect(updateSpy.mostRecentCall.data.tags).toEqual(['other']);
  });

  it('removes numeric tag from the mail when its label is clicked', function() {
    var updateSpy = spyOnEvent(document, Pixelated.events.mail.tags.update);

    testData.mail.tags = ['inbox', '12345'];
    this.component.displayMail({}, testData);
    this.component.removeTag(12345);

    expect(updateSpy).toHaveBeenTriggeredOn(document);
    expect(updateSpy.mostRecentCall.data.tags).toEqual(['inbox']);
  });

  it('remove tag triggers refreshTagList event', function(){
    var refreshTagListEvent = spyOnEvent(document, Pixelated.events.dispatchers.tags.refreshTagList);
    this.component.displayMail({}, testData);
    this.component.removeTag('inbox');
    expect(refreshTagListEvent).toHaveBeenTriggeredOn(document);
  });

  it('verifies if new tag input is hidden when rendering mail view', function() {
    this.component.displayMail({}, testData);

    var newTagInputComponent = this.component.select('newTagInput');
    expect(newTagInputComponent.attr('style').trim()).toEqual('display: none;');
  });

  it('verifies if new tag input is shown when clicking on new tag button', function() {
    this.component.displayMail({}, testData);

    var newTagInputComponent = this.component.select('newTagInput');

    this.component.select('newTagButton').click();

    expect(newTagInputComponent.attr('style').trim()).not.toEqual('display: none;');
  });

  it('hides new tag input when pressing esc key', function(){
    this.component.displayMail({}, testData);
    this.component.select('newTagButton').click();

    var e = creatingEvent('keydown', 27);
    var newTagInputComponent = this.component.select('newTagInput');

    newTagInputComponent.trigger(e);

    expect(newTagInputComponent.attr('style').trim()).toEqual('display: none;');
  });

  it('assumes that the mail is encrypted and valid if at least one of the locks are valid', function() {
    var email = testData;
    email.security_casing = {locks: [{state: 'valid'}, {state: 'failure'}]};
    var checkEncrypted = this.component.checkEncrypted(email);
    expect(checkEncrypted.cssClass).toEqual('security-status__label--encrypted');
    expect(checkEncrypted.tooltipText).toEqual('encrypted-label-tooltip');
  });

  it('assumes that the mail is encrypted and failure if all the locks are failed', function() {
    var email = testData;
    email.security_casing = {locks: [{state: 'failure'}, {state: 'failure'}]};
    var checkEncrypted = this.component.checkEncrypted(email);
    expect(checkEncrypted.cssClass).toEqual('security-status__label--encrypted--with-error');
    expect(checkEncrypted.tooltipText).toEqual('encryption-error-label-tooltip');
  });

  it('assumes that the mail is not encrypted if it doesn\'t have any locks', function() {
    var email = testData;
    email.security_casing = {locks: []};
    var checkEncrypted = this.component.checkEncrypted(email);
    expect(checkEncrypted.cssClass).toEqual('security-status__label--not-encrypted');
    expect(checkEncrypted.tooltipText).toEqual('not-encrypted-label-tooltip');
  });

  it('assumes that the mail is signed only if all imprints are valid', function() {
    var email = testData;
    email.security_casing = {imprints: [{state: 'valid', seal: {trust: 'marginal', validity: 'marginal'}}, {state: 'valid', seal: {trust: 'marginal', validity: 'marginal'}}]};
    var checkSigned = this.component.checkSigned(email);
    expect(checkSigned.cssClass).toEqual('security-status__label--signed');
    expect(checkSigned.tooltipText).toEqual('signed-label-tooltip');
  });

  it('assumes that the mail is signed with failures if there is a revoke or expire', function() {
    var email = testData;
    email.security_casing = {imprints: [{state: 'valid', seal: {trust: 'marginal', validity: 'marginal'}}, {state: 'from_revoked', seal: {trust: 'marginal', validity: 'marginal'}}]};
    var checkSigned = this.component.checkSigned(email);
    expect(checkSigned.cssClass).toEqual('security-status__label--signed--revoked');
    expect(checkSigned.tooltipText).toEqual('not-signed-label-tooltip');
  });

  it('assumes that mail is not trusted if its signature contains no_trust from the user', function() {
    var email = testData;
    email.security_casing = {imprints: [{seal: {trust: 'no_trust', validity: 'ultimate'}}]};
    var checkSigned = this.component.checkSigned(email);
    expect(checkSigned.cssClass).toEqual('security-status__label--signed--not-trusted');
    expect(checkSigned.tooltipText).toEqual('not-signed-label-tooltip');
  });

  it('uses validity when trust is not present', function() {
    var email = testData;
    email.security_casing = {imprints: [{seal: { validity: 'no_trust'}}]};
    var checkSigned = this.component.checkSigned(email);
    expect(checkSigned.cssClass).toEqual('security-status__label--signed--not-trusted');
    expect(checkSigned.tooltipText).toEqual('not-signed-label-tooltip');
  });

  it('assumes not trusted when the seal signature is not found', function(){
    var email = testData;
    email.security_casing = {imprints: [{seal: null}]};
    var checkSigned = this.component.checkSigned(email);
    expect(checkSigned.cssClass).toEqual('security-status__label--signed--not-trusted');
    expect(checkSigned.tooltipText).toEqual('not-signed-label-tooltip');
  });

  it('assumes that the mail is not signed if there are no imprints', function() {
    var email = testData;
    email.security_casing = {imprints: []};
    var checkSigned = this.component.checkSigned(email);
    expect(checkSigned.cssClass).toEqual('security-status__label--not-signed');
    expect(checkSigned.tooltipText).toEqual('not-signed-label-tooltip');
  });

  it('assumes that there is no signature info to show', function() {
    var email = testData;
    email.security_casing = {imprints: [{state: 'no_signature_information'}]};
    var checkSigned = this.component.checkSigned(email);
    expect(checkSigned.cssClass).toEqual('security-status__label--not-signed');
    expect(checkSigned.tooltipText).toEqual('not-signed-label-tooltip');
  });

  it('shows that mail is encrypted if it is', function() {
    spyOn(this.component, 'checkEncrypted').and.returnValue({cssClass: 'security-status__label--encrypted'});
    this.component.displayMail({}, testData);
    expect(this.component.$node.find('.security-status__label--encrypted')).toExist();
  });

  it('shows that mail is signed if it is', function() {
    spyOn(this.component, 'checkSigned').and.returnValue({cssClass: 'security-status__label--signed'});
    this.component.displayMail({}, testData);
    expect(this.component.$node.find('.security-status__label--signed')).toExist();
  });

  it('shows that mail is not encrypted if it isn\'t', function() {
    spyOn(this.component, 'checkEncrypted').and.returnValue({cssClass: 'security-status__label--not-encrypted'});
    this.component.displayMail({}, testData);
    expect(this.component.$node.find('.security-status__label--not-encrypted')).toExist();
  });

  it('shows that mail is not signed if it isn\'t', function() {
    spyOn(this.component, 'checkEncrypted').and.returnValue({cssClass: 'securty-status__label--not-signed'});
    this.component.displayMail({}, testData);
    expect(this.component.$node.find('.security-status__label--not-signed')).toExist();
  });

  it('creates new tag when pressing Enter key on new tag input', function(){
    var tagsUpdateEvent = spyOnEvent(document, Pixelated.events.mail.tags.update);

    this.component.displayMail({}, testData);
    this.component.select('newTagButton').click();

    var newTagInputComponent = this.component.select('newTagInput');
    newTagInputComponent.val('Test');

    var e = creatingEvent('keydown', 13); //ENTER KEY EVENT
    newTagInputComponent.trigger(e);

    var tags = testData.mail.tags.slice();
    tags.push('Test');
    expect(tagsUpdateEvent).toHaveBeenTriggeredOnAndWith(document, { ident: testData.mail.ident, tags: tags});
  });

  it('creates new tag when pressing Enter key on new tag input', function(){
    var tagsUpdateEvent = spyOnEvent(document, Pixelated.events.mail.tags.update);

    this.component.displayMail({}, testData);
    this.component.select('newTagButton').click();

    var newTagInputComponent = this.component.select('newTagInput');
    newTagInputComponent.val('    ');

    var e = creatingEvent('keydown', 13); //ENTER KEY EVENT
    newTagInputComponent.trigger(e);

    expect(tagsUpdateEvent).not.toHaveBeenTriggeredOnAndWith(document);
  });

  it('trigger mail delete event when moving email to trash', function(){
    var mailDeleteEvent = spyOnEvent(document, Pixelated.events.ui.mail.delete);

    Foundation.global.namespace = '';
    $(document).foundation();

    this.component.displayMail({}, testData);
    this.component.moveToTrash();

    expect(mailDeleteEvent).toHaveBeenTriggeredOnAndWith(document, { mail: this.component.attr.mail });
  });

  it('shows no message selected pane when deleting the email being composed', function() {
    var openNoMessageSelectedPaneEvent = spyOnEvent(document, Pixelated.events.dispatchers.rightPane.openNoMessageSelected);
    var mails = [{ident: 123}];
    this.component.attr.mail = mails[0];

    this.component.trigger(document, Pixelated.events.mail.deleted, {mails: mails});

    expect(openNoMessageSelectedPaneEvent).toHaveBeenTriggeredOn(document);
  });

  it('does not show no message selected pane when deleting a different set of emails', function() {
    var openNoMessageSelectedPaneEvent = spyOnEvent(document, Pixelated.events.dispatchers.rightPane.openNoMessageSelected);
    var mails = [{ident: 321}];
    this.component.attr.mail = {ident: 123};

    this.component.trigger(document, Pixelated.events.mail.deleted, {mails: mails});

    expect(openNoMessageSelectedPaneEvent).not.toHaveBeenTriggeredOn(document);
  });

  it('opens the no message selected pane when clicking the close button', function() {
    var openNoMessageSelectedEvent = spyOnEvent(document, Pixelated.events.dispatchers.rightPane.openNoMessageSelected);

    this.component.displayMail({}, testData);
    this.component.select('closeMailButton').click();

    expect(openNoMessageSelectedEvent).toHaveBeenTriggeredOn(document);
  });

  it('shows a download link for attachments', function() {
    var withAttachments = {mail: Pixelated.testData().parsedMail.withAttachments};

    this.component.displayMail({}, withAttachments);

    var attachmentLink = $(this.component.$node.find('.mail-read-view__attachments-item').html());
    var expectedLink = '/attachment/912ec803b2ce49e4a541068d495ab570?content_type=text/plain&encoding=base64&filename=filename.txt';
    expect(attachmentLink.attr('href')) .toBe(expectedLink);
  });

  function creatingEvent(event, keyCode) {
    var e = $.Event(event);
    e.which = keyCode;
    return e;
  }
});