forked from solidcouch/simple-email-notifications
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotification.spec.ts
163 lines (147 loc) · 4.8 KB
/
notification.spec.ts
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
import { expect } from 'chai'
import fetch from 'cross-fetch'
import { describe } from 'mocha'
import Mail from 'nodemailer/lib/mailer'
import { SinonSandbox, SinonSpy, createSandbox } from 'sinon'
import { baseUrl } from '../config'
import type { GoodBody } from '../controllers/notification'
import * as mailerService from '../services/mailerService'
import { verifyEmail } from './helpers'
import {
authenticatedFetch,
authenticatedFetch3,
otherAuthenticatedFetch,
otherPerson,
person,
person3,
} from './testSetup.spec'
const email = 'email@example.com'
/**
* Generate body for POST /notification
*/
const getBody = ({
from,
to,
message,
}: {
from: string
to: string
message: string
}): GoodBody => ({
'@context': 'https://www.w3.org/ns/activitystreams',
type: 'Create',
actor: { type: 'Person', id: from },
object: { type: 'Note', id: 'https://example', content: message },
target: { type: 'Person', id: to },
})
describe('send notification via /notification', () => {
let sendMailSpy: SinonSpy<[options: Mail.Options], Promise<void>>
let sandbox: SinonSandbox
beforeEach(async () => {
// setup email for receiver
await verifyEmail({
email,
person: person3,
authenticatedFetch: authenticatedFetch3,
})
})
beforeEach(() => {
sandbox = createSandbox()
sendMailSpy = sandbox.spy(mailerService, 'sendMail')
})
afterEach(() => {
sandbox.restore()
})
it('[everything ok] should send email to email address when requested', async () => {
const response = await authenticatedFetch(`${baseUrl}/notification`, {
method: 'post',
headers: { 'content-type': 'application/ld+json' },
body: JSON.stringify(
getBody({ from: person.webId, to: person3.webId, message: 'Hello!' }),
),
})
expect(response.status).to.equal(202)
// let's wait a bit
// TODO this should be improved. waiting without knowing how long isn't great
// await promisify(setTimeout)(2000)
expect(sendMailSpy.callCount).to.equal(1)
const emailNotification = sendMailSpy.firstCall.firstArg
expect(emailNotification).to.exist
expect(emailNotification.to).to.haveOwnProperty('address', email)
// TODO
})
it('[invalid body] should fail with 400', async () => {
const response = await authenticatedFetch(`${baseUrl}/notification`, {
method: 'post',
headers: { 'content-type': 'application/ld+json' },
body: JSON.stringify({ invalid: 'body' }),
})
expect(response.status).to.equal(400)
})
it('[not authenticated] should fail with 401', async () => {
const response = await fetch(`${baseUrl}/notification`, {
method: 'post',
headers: { 'content-type': 'application/ld+json' },
body: JSON.stringify({}),
})
expect(response.status).to.equal(401)
})
it('[authenticated person not from group] should fail with 403', async () => {
const response = await otherAuthenticatedFetch(`${baseUrl}/notification`, {
method: 'post',
headers: { 'content-type': 'application/ld+json' },
body: JSON.stringify({}),
})
expect(response.status).to.equal(403)
})
it('[receiver person not from group] should fail with 400', async () => {
const response = await authenticatedFetch(`${baseUrl}/notification`, {
method: 'post',
headers: { 'content-type': 'application/ld+json' },
body: JSON.stringify(
getBody({
from: person.webId,
to: otherPerson.webId,
message: 'Hello there!',
}),
),
})
expect(response.status).to.equal(400)
const body = await response.text()
expect(body).to.equal('Person is not a member of any allowed group')
})
it('[actor is not authenticated person] should fail with 403', async () => {
const response = await authenticatedFetch3(`${baseUrl}/notification`, {
method: 'post',
headers: { 'content-type': 'application/ld+json' },
body: JSON.stringify(
getBody({
from: person.webId,
to: person3.webId,
message: 'Hello there!',
}),
),
})
expect(response.status).to.equal(403)
const body = await response.text()
expect(body).to.equal("You can't send notification as somebody else")
})
it("[receiver person doesn't have verified email address] should fail with 404", async () => {
const response = await authenticatedFetch3(`${baseUrl}/notification`, {
method: 'post',
headers: { 'content-type': 'application/ld+json' },
body: JSON.stringify(
getBody({
from: person3.webId,
to: person.webId,
message: 'Hello there!',
}),
),
})
expect(response.status).to.equal(404)
const body = await response.text()
expect(body).to.equal(
"Receiving person doesn't have available email address",
)
})
})