forked from GoogleCloudPlatform/nodejs-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.test.js
More file actions
170 lines (146 loc) · 4.26 KB
/
Copy pathindex.test.js
File metadata and controls
170 lines (146 loc) · 4.26 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
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
'use strict';
const proxyquire = require('proxyquire').noCallThru();
const sinon = require('sinon');
const assert = require('assert');
describe('tasks/function', () => {
let key;
const getSample = function () {
const sendGridStub = {
setApiKey: sinon.stub(),
send: sinon.stub().resolves([{statusCode: 200}]),
};
return {
program: proxyquire('../', {
'@sendgrid/mail': sendGridStub,
}),
mocks: {
sendGridStub: sendGridStub,
},
};
};
const getMocks = function () {
const req = {
body: {},
};
const res = {
send: sinon.stub().returnsThis(),
status: function (statusCode) {
this.statusCode = statusCode;
return this;
},
};
sinon.spy(res, 'status');
return {req, res};
};
before(async () => {
key = 'SG.dummy_key_for_testing';
process.env.SENDGRID_API_KEY = key;
});
beforeEach(() => {
sinon.spy(console, 'error');
sinon.spy(console, 'log');
});
afterEach(() => {
console.error.restore();
console.log.restore();
});
it('send fails without API key', async () => {
process.env = {
SENDGRID_API_KEY: undefined,
};
const mocks = getMocks();
const sample = getSample();
const error = new Error(
'SENDGRID_API_KEY was not provided as environment variable.'
);
error.code = 401;
try {
await sample.program.sendEmail(mocks.req, mocks.res);
} catch (err) {
assert.deepStrictEqual(err, error);
}
});
it('send fails without sender name', async () => {
process.env = {
SENDGRID_API_KEY: key,
};
const mocks = getMocks();
mocks.req.body.to_email = 'to@gmail.com';
mocks.req.body.to_name = 'testA';
const sample = getSample();
const error = new Error('Sender name not provided.');
error.code = 400;
try {
await sample.program.sendEmail(mocks.req, mocks.res);
} catch (err) {
assert.deepStrictEqual(err, error);
}
});
it('send fails without recipient email', async () => {
process.env = {
SENDGRID_API_KEY: key,
};
const mocks = getMocks();
mocks.req.body.to_name = 'testA';
mocks.req.body.from_name = 'testB';
const sample = getSample();
const error = new Error('Email address not provided.');
error.code = 400;
try {
await sample.program.sendEmail(mocks.req, mocks.res);
} catch (err) {
assert.deepStrictEqual(err, error);
}
});
it('send fails without recipient name', async () => {
process.env = {
SENDGRID_API_KEY: key,
};
const mocks = getMocks();
mocks.req.body.to_email = 'to@gmail.com';
mocks.req.body.from_name = 'testB';
const sample = getSample({
SENDGRID_API_KEY: key,
});
const error = new Error('Recipient name not provided.');
error.code = 400;
try {
await sample.program.sendEmail(mocks.req, mocks.res);
} catch (err) {
assert.deepStrictEqual(err, error);
}
});
it('send succeeds', async () => {
process.env = {
SENDGRID_API_KEY: key,
};
const mocks = getMocks();
mocks.req.body.to_email = 'to@gmail.com';
mocks.req.body.to_name = 'testA';
mocks.req.body.from_name = 'testB';
const sample = getSample();
await sample.program.sendEmail(mocks.req, mocks.res);
assert.strictEqual(mocks.res.status.callCount, 1);
assert.deepStrictEqual(mocks.res.status.firstCall.args, [200]);
assert.strictEqual(mocks.res.send.callCount, 1);
sinon.assert.calledOnceWithExactly(sample.mocks.sendGridStub.send, {
to: 'to@gmail.com',
from: 'postcard@example.com',
subject: 'A Postcard Just for You!',
html: sinon.match.string,
});
});
});