This repository was archived by the owner on Dec 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathhelpers_test.js
250 lines (211 loc) · 6.85 KB
/
helpers_test.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
(function (){
'use strict';
var express = require("express")
, request = require("request")
, bodyParser = require("body-parser")
, http = require("http")
, chai = require("chai")
, chaiHttp = require("chai-http")
, sinon = require("sinon")
, expect = chai.expect
, helpers = require("../helpers")
, app
describe("helpers", function() {
before(function() {
chai.use(chaiHttp);
});
beforeEach(function() {
app = express();
app.use(bodyParser.json());
});
describe("#redirectHandler", function() {
it("should return bad request if invalid redirection", function(done) {
app.use(function(req, res) {
helpers.rewriteSlash(req, res, done);
});
chai.request(app).
get("//category.html/").
end(function(err, res) {
expect(res).to.have.status(400);
done();
});
});
it("should redirect if valid redirection", function(done) {
app.use(function(req, res) {
helpers.rewriteSlash(req, res, done);
});
chai.request(app).
get("/category.html/").
end(function(err, res) {
expect(res).to.have.status(301);
done();
});
});
});
describe("#errorHandler", function() {
var message, code, error, res, resErr;
beforeEach(function(done) {
message = "Something went terribly wrong";
code = 501;
error = new Error(message);
error.status = code;
app.use(function(_req, _res) {
helpers.errorHandler(error, _req, _res);
});
chai.request(app).
get("/").
set("Content-Type", "application/json").
end(function(_err, _res) {
resErr = _err;
res = _res;
done();
});
});
describe("the rendered JSON", function() {
it("includes an error message", function() {
expect(res.body).to.include.keys("message");
expect(res.body.message).to.equal(message);
});
it("includes an error object", function() {
expect(resErr).not.to.be.null;
});
it("returns the right HTTP status code", function() {
expect(res).to.have.status(501);
});
});
describe("given the error has no status defined", function() {
beforeEach(function() {
delete error.status;
});
it("responds with HTTP status code 500", function(done) {
chai.request(app).
get("/").
set("Content-Type", "application/json").
end(function(err, res) {
expect(err).not.to.be.null;
expect(res).to.have.status(500);
done();
});
});
});
});
describe("#respondSuccessBody", function() {
it("renders the given body with status 200 OK", function(done) {
app.use(function(req, res) {
helpers.respondSuccessBody(res, "ayylmao");
});
chai.request(app).
get("/").
end(function(err, res) {
expect(res).to.have.status(200);
expect(res.text).to.equal("ayylmao");
done();
});
});
});
describe("#respondStatusBody", function() {
it("sets the proper status code & body", function(done) {
app.use(function(req, res) {
helpers.respondStatusBody(res, 201, "foo");
});
chai.request(app).
get("/").
end(function(err, res) {
expect(res).to.have.status(201);
expect(res.text).to.equal("foo");
done();
});
});
});
describe("#respondStatus", function() {
it("sets the proper status code", function(done) {
app.use(function(req, res) {
helpers.respondStatus(res, 404);
});
chai.request(app).
get("/").
end(function(err, res) {
expect(err).to.not.be.null;
expect(err.message).to.equal("Not Found");
expect(res).to.have.status(404);
expect(res.text).to.equal("");
done();
});
});
});
describe("#simpleHttpRequest", function() {
var res, resErr;
it("performs a GET request to the given URL", function() {
var url = "http://google.com";
sinon.stub(request, "get", function(requestedUrl, cb) {
expect(requestedUrl).to.equal(url);
});
helpers.simpleHttpRequest(url);
request.get.restore();
});
describe("given the external service responds with success", function() {
beforeEach(function(done) {
sinon.stub(request, "get", function(url, cb) {
var _res = {}
, mockRes = sinon.mock(_res);
cb(null, mockRes, "success");
});
app.use(function(_req, _res) {
helpers.simpleHttpRequest("http://api.example.org/users", _res, done);
});
chai.
request(app).
get("/").
end(function(_err, _res) {
if (_err) return done(_err);
resErr = _err;
res = _res;
done();
});
});
afterEach(function() {
request.get.restore();
});
it("yields the external service response to the response body", function() {
expect(res.text).to.equal("success");
});
it("responds with success", function() {
expect(res).to.have.status(200);
});
});
describe("given the external service fails", function() {
it("invokes the given callback with an error object", function(done) {
var spy = sinon.spy();
sinon.stub(request, "get", function(url, cb) {
cb(new Error("Something went wrong"));
});
app.use(function(req, res) {
helpers.simpleHttpRequest("http://example.org/fail", res, function(err) {
expect(err).not.to.be.null;
expect(err.message).to.equal("Something went wrong");
request.get.restore();
done();
});
});
chai.
request(app).
get("/").
end();
});
});
});
describe("#getCustomerId", function() {
describe("given the environment is development", function() {
it("returns the customer id from the query string");
});
describe("given a customer id set in session", function() {
it("returns the customer id from the session");
});
describe("given no customer id set in the cookies", function() {
describe("given no customer id set session", function() {
it("throws a 'User not logged in' error");
});
});
});
});
}());