|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+'use strict'
|
|
|
2
|
+
|
|
|
3
|
+/*
|
|
|
4
|
+ * NOTE: This test ACTUALLY will send an email
|
|
|
5
|
+ * (see email variable below)
|
|
|
6
|
+ * USE WITH CAUTION
|
|
|
7
|
+ */
|
|
|
8
|
+
|
|
|
9
|
+const test = require('ava')
|
|
|
10
|
+const { stub } = require('sinon')
|
|
|
11
|
+const Hapi = require('@hapi/hapi')
|
|
|
12
|
+const UserService = require('../lib/services/user.js')
|
|
|
13
|
+const plugin = require('../lib/plugins/user.js')
|
|
|
14
|
+
|
|
|
15
|
+// Necessary Dependencies/Configurations for Brevo Transac Email
|
|
|
16
|
+require('dotenv').config()
|
|
|
17
|
+const crypto = require('crypto')
|
|
|
18
|
+const SibApiV3Sdk = require('sib-api-v3-sdk')
|
|
|
19
|
+const defaultClient = SibApiV3Sdk.ApiClient.instance
|
|
|
20
|
+const apiKey = defaultClient.authentications['api-key']
|
|
|
21
|
+apiKey.apiKey = process.env.BREVO_KEY
|
|
|
22
|
+const apiInstance = new SibApiV3Sdk.TransactionalEmailsApi()
|
|
|
23
|
+
|
|
|
24
|
+// Change email here to your actual email
|
|
|
25
|
+const email = 'myalias@myactualemail.com'
|
|
|
26
|
+
|
|
|
27
|
+// Existing activeSession to test against (should not match)
|
|
|
28
|
+const activeSessions = {
|
|
|
29
|
+ 'a;lsdkfja;ldfjka;ldfja;lskjdfa;dfjk': {
|
|
|
30
|
+ email: 'test@testemail.com',
|
|
|
31
|
+ name: 'john_doe',
|
|
|
32
|
+ seeking: 'position',
|
|
|
33
|
+ sessionToken: 'efasdf;laksdfja;lkdjfa;lkdjf',
|
|
|
34
|
+ expiration: Date.now() + 600000,
|
|
|
35
|
+ emailWasRespondedTo: false,
|
|
|
36
|
+ accessToken: null,
|
|
|
37
|
+ },
|
|
|
38
|
+}
|
|
|
39
|
+
|
|
|
40
|
+let hashedSessionToken = ''
|
|
|
41
|
+
|
|
|
42
|
+const userCredentials = {
|
|
|
43
|
+ email: email,
|
|
|
44
|
+ name: 'fk',
|
|
|
45
|
+ seeking: 'seeker',
|
|
|
46
|
+ sessionToken: 'a;slkdjfa;lskdf;asjkdfl;asdf;klj',
|
|
|
47
|
+}
|
|
|
48
|
+
|
|
|
49
|
+const payload = {
|
|
|
50
|
+ email: email,
|
|
|
51
|
+ name: 'fk',
|
|
|
52
|
+ seeking: 'seeker',
|
|
|
53
|
+ sessionToken: 'a;slkdjfa;lskdf;asjkdfl;asdf;klj',
|
|
|
54
|
+}
|
|
|
55
|
+
|
|
|
56
|
+const pathToTest = {
|
|
|
57
|
+ method: 'POST',
|
|
|
58
|
+ url: '/send-email/',
|
|
|
59
|
+ payload: JSON.stringify(payload),
|
|
|
60
|
+}
|
|
|
61
|
+
|
|
|
62
|
+test('path /send-email should send test transac email', async t => {
|
|
|
63
|
+ /**
|
|
|
64
|
+ * Create a new server and register services,
|
|
|
65
|
+ * models and routes for testing
|
|
|
66
|
+ */
|
|
|
67
|
+ const server = Hapi.server()
|
|
|
68
|
+ server.registrations = {
|
|
|
69
|
+ 'main-app-plugin': {
|
|
|
70
|
+ options: {},
|
|
|
71
|
+ },
|
|
|
72
|
+ }
|
|
|
73
|
+
|
|
|
74
|
+ /**
|
|
|
75
|
+ * Register Services
|
|
|
76
|
+ */
|
|
|
77
|
+ await plugin.register(server)
|
|
|
78
|
+ server.services()['userService'] = new UserService(server)
|
|
|
79
|
+ server.services()['userService']['activeSessions'] = activeSessions
|
|
|
80
|
+
|
|
|
81
|
+ const hashToken = token => {
|
|
|
82
|
+ const salt = process.env.APP_SESSION_SALT
|
|
|
83
|
+ try {
|
|
|
84
|
+ return crypto.createHmac('sha256', salt).update(token).digest('hex')
|
|
|
85
|
+ } catch (err) {
|
|
|
86
|
+ throw new Error(err.message)
|
|
|
87
|
+ }
|
|
|
88
|
+ }
|
|
|
89
|
+
|
|
|
90
|
+ /**
|
|
|
91
|
+ * Sends a Transactional Email via Brevo
|
|
|
92
|
+ * @ returns {Object}
|
|
|
93
|
+ */
|
|
|
94
|
+ const emailSent = async userCredentials => {
|
|
|
95
|
+ hashedSessionToken = hashToken(userCredentials.sessionToken)
|
|
|
96
|
+ if (Object.keys(activeSessions).includes(hashedSessionToken)) {
|
|
|
97
|
+ return new Error('session already in cache!!')
|
|
|
98
|
+ }
|
|
|
99
|
+ // Set expiration time for ten minutes from now
|
|
|
100
|
+ const duration = 600000
|
|
|
101
|
+
|
|
|
102
|
+ activeSessions[hashedSessionToken] = {
|
|
|
103
|
+ email: userCredentials.email,
|
|
|
104
|
+ name: userCredentials.name,
|
|
|
105
|
+ seeking: userCredentials.seeking,
|
|
|
106
|
+ sessionToken: userCredentials.sessionToken,
|
|
|
107
|
+ expiration: Date.now() + duration,
|
|
|
108
|
+ emailWasRespondedTo: false,
|
|
|
109
|
+ accessToken: null,
|
|
|
110
|
+ }
|
|
|
111
|
+ const sendSmtpEmail = {
|
|
|
112
|
+ to: [
|
|
|
113
|
+ {
|
|
|
114
|
+ email: userCredentials.email,
|
|
|
115
|
+ },
|
|
|
116
|
+ ],
|
|
|
117
|
+ templateId: 1,
|
|
|
118
|
+ params: {
|
|
|
119
|
+ // TODO: Change this in production...
|
|
|
120
|
+ link: `localhost:3000/verify/${hashedSessionToken}`,
|
|
|
121
|
+ },
|
|
|
122
|
+ }
|
|
|
123
|
+ return await apiInstance.sendTransacEmail(sendSmtpEmail).then(
|
|
|
124
|
+ data => {
|
|
|
125
|
+ return { wasSuccessfull: true, data: data }
|
|
|
126
|
+ },
|
|
|
127
|
+ error => {
|
|
|
128
|
+ return { wasSuccessfull: false, error: error }
|
|
|
129
|
+ },
|
|
|
130
|
+ )
|
|
|
131
|
+ }
|
|
|
132
|
+
|
|
|
133
|
+ hashedSessionToken = Object.keys(activeSessions).find(hashedToken => {
|
|
|
134
|
+ return activeSessions[`${hashedToken}`].email === userCredentials.email
|
|
|
135
|
+ })
|
|
|
136
|
+
|
|
|
137
|
+ stub(server.services()['userService'], 'emailSent').returns(
|
|
|
138
|
+ await emailSent(userCredentials),
|
|
|
139
|
+ )
|
|
|
140
|
+
|
|
|
141
|
+ const mockReturn = {
|
|
|
142
|
+ emailSentSuccessfully: true,
|
|
|
143
|
+ hashedSessionToken,
|
|
|
144
|
+ }
|
|
|
145
|
+
|
|
|
146
|
+ /**
|
|
|
147
|
+ * Test the server with registered models and services
|
|
|
148
|
+ */
|
|
|
149
|
+ const { payload } = await server.inject(pathToTest)
|
|
|
150
|
+ const res = JSON.parse(payload)
|
|
|
151
|
+ t.deepEqual(res.ok, true)
|
|
|
152
|
+ t.deepEqual(res.data, mockReturn)
|
|
|
153
|
+ server.stop()
|
|
|
154
|
+})
|