Skip to content

Commit 8b0a210

Browse files
committed
Fix: remove GLOBAL.DATABASE_URL
related to Thinkful-Ed/curric-node-001-v5/issues/473
1 parent 74c59f9 commit 8b0a210

File tree

5 files changed

+62
-63
lines changed

5 files changed

+62
-63
lines changed

config.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
'use strict';
2-
exports.DATABASE_URL =
3-
process.env.DATABASE_URL ||
4-
global.DATABASE_URL ||
5-
'mongodb://localhost/jwt-auth-demo';
2+
exports.DATABASE_URL = process.env.DATABASE_URL || 'mongodb://localhost/jwt-auth-demo';
3+
exports.TEST_DATABASE_URL = process.env.TEST_DATABASE_URL || 'mongodb://localhost/jwt-auth-demo';
64
exports.PORT = process.env.PORT || 8080;
75
exports.JWT_SECRET = process.env.JWT_SECRET;
8-
exports.JWT_EXPIRY = process.env.JWT_EXPIRY || '7d';
6+
exports.JWT_EXPIRY = process.env.JWT_EXPIRY || '7d';

server.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,17 @@ app.use('*', (req, res) => {
5858
// assumes runServer has run and set `server` to a server object
5959
let server;
6060

61-
function runServer() {
61+
function runServer(databaseUrl, port = PORT) {
62+
6263
return new Promise((resolve, reject) => {
63-
mongoose.connect(DATABASE_URL, { useMongoClient: true }, err => {
64+
mongoose.connect(databaseUrl, { useMongoClient: true }, err => {
6465
if (err) {
6566
return reject(err);
6667
}
67-
server = app
68-
.listen(PORT, () => {
69-
console.log(`Your app is listening on port ${PORT}`);
70-
resolve();
71-
})
68+
server = app.listen(port, () => {
69+
console.log(`Your app is listening on port ${port}`);
70+
resolve();
71+
})
7272
.on('error', err => {
7373
mongoose.disconnect();
7474
reject(err);
@@ -92,7 +92,7 @@ function closeServer() {
9292
}
9393

9494
if (require.main === module) {
95-
runServer().catch(err => console.error(err));
95+
runServer(DATABASE_URL).catch(err => console.error(err));
9696
}
9797

9898
module.exports = { app, runServer, closeServer };

test/test-auth.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
'use strict';
2-
global.DATABASE_URL = 'mongodb://localhost/jwt-auth-demo-test';
2+
33
const chai = require('chai');
44
const chaiHttp = require('chai-http');
55
const jwt = require('jsonwebtoken');
66

77
const { app, runServer, closeServer } = require('../server');
88
const { User } = require('../users');
9-
const { JWT_SECRET } = require('../config');
9+
const { JWT_SECRET, TEST_DATABASE_URL } = require('../config');
1010

1111
const expect = chai.expect;
1212

@@ -22,14 +22,14 @@ describe('Auth endpoints', function () {
2222
const lastName = 'User';
2323

2424
before(function () {
25-
return runServer();
25+
return runServer(TEST_DATABASE_URL);
2626
});
2727

2828
after(function () {
2929
return closeServer();
3030
});
3131

32-
beforeEach(function() {
32+
beforeEach(function () {
3333
return User.hashPassword(password).then(password =>
3434
User.create({
3535
username,
@@ -65,7 +65,7 @@ describe('Auth endpoints', function () {
6565
return chai
6666
.request(app)
6767
.post('/api/auth/login')
68-
.send({ username: 'wrongUsername', password })
68+
.send({ username: 'wrongUsername', password })
6969
.then(() =>
7070
expect.fail(null, null, 'Request should not succeed')
7171
)
@@ -172,12 +172,12 @@ describe('Auth endpoints', function () {
172172
firstName,
173173
lastName
174174
},
175-
exp: Math.floor(Date.now() / 1000) - 10 // Expired ten seconds ago
176175
},
177176
JWT_SECRET,
178177
{
179178
algorithm: 'HS256',
180-
subject: username
179+
subject: username,
180+
expiresIn: Math.floor(Date.now() / 1000) - 10 // Expired ten seconds ago
181181
}
182182
);
183183

test/test-protected.js

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
'use strict';
2-
global.DATABASE_URL = 'mongodb://localhost/jwt-auth-demo-test';
2+
33
const chai = require('chai');
44
const chaiHttp = require('chai-http');
55
const jwt = require('jsonwebtoken');
66

7-
const {app, runServer, closeServer} = require('../server');
8-
const {User} = require('../users');
9-
const {JWT_SECRET} = require('../config');
7+
const { app, runServer, closeServer } = require('../server');
8+
const { User } = require('../users');
9+
const { JWT_SECRET, TEST_DATABASE_URL } = require('../config');
1010

1111
const expect = chai.expect;
1212

@@ -15,21 +15,21 @@ const expect = chai.expect;
1515
// see: https://github.com/chaijs/chai-http
1616
chai.use(chaiHttp);
1717

18-
describe('Protected endpoint', function() {
18+
describe('Protected endpoint', function () {
1919
const username = 'exampleUser';
2020
const password = 'examplePass';
2121
const firstName = 'Example';
2222
const lastName = 'User';
2323

24-
before(function() {
25-
return runServer();
24+
before(function () {
25+
return runServer(TEST_DATABASE_URL);
2626
});
2727

28-
after(function() {
28+
after(function () {
2929
return closeServer();
3030
});
3131

32-
beforeEach(function() {
32+
beforeEach(function () {
3333
return User.hashPassword(password).then(password =>
3434
User.create({
3535
username,
@@ -40,12 +40,12 @@ describe('Protected endpoint', function() {
4040
);
4141
});
4242

43-
afterEach(function() {
43+
afterEach(function () {
4444
return User.remove({});
4545
});
4646

47-
describe('/api/protected', function() {
48-
it('Should reject requests with no credentials', function() {
47+
describe('/api/protected', function () {
48+
it('Should reject requests with no credentials', function () {
4949
return chai
5050
.request(app)
5151
.get('/api/protected')
@@ -62,7 +62,7 @@ describe('Protected endpoint', function() {
6262
});
6363
});
6464

65-
it('Should reject requests with an invalid token', function() {
65+
it('Should reject requests with an invalid token', function () {
6666
const token = jwt.sign(
6767
{
6868
username,
@@ -92,7 +92,7 @@ describe('Protected endpoint', function() {
9292
expect(res).to.have.status(401);
9393
});
9494
});
95-
it('Should reject requests with an expired token', function() {
95+
it('Should reject requests with an expired token', function () {
9696
const token = jwt.sign(
9797
{
9898
user: {
@@ -125,7 +125,7 @@ describe('Protected endpoint', function() {
125125
expect(res).to.have.status(401);
126126
});
127127
});
128-
it('Should send protected data', function() {
128+
it('Should send protected data', function () {
129129
const token = jwt.sign(
130130
{
131131
user: {

test/test-users.js

+29-28
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
'use strict';
2-
global.DATABASE_URL = 'mongodb://localhost/jwt-auth-demo-test';
2+
33
const chai = require('chai');
44
const chaiHttp = require('chai-http');
55

6-
const {app, runServer, closeServer} = require('../server');
7-
const {User} = require('../users');
6+
const { app, runServer, closeServer } = require('../server');
7+
const { User } = require('../users');
8+
const { TEST_DATABASE_URL } = require('../config');
89

910
const expect = chai.expect;
1011

@@ -13,7 +14,7 @@ const expect = chai.expect;
1314
// see: https://github.com/chaijs/chai-http
1415
chai.use(chaiHttp);
1516

16-
describe('/api/user', function() {
17+
describe('/api/user', function () {
1718
const username = 'exampleUser';
1819
const password = 'examplePass';
1920
const firstName = 'Example';
@@ -23,23 +24,23 @@ describe('/api/user', function() {
2324
const firstNameB = 'ExampleB';
2425
const lastNameB = 'UserB';
2526

26-
before(function() {
27-
return runServer();
27+
before(function () {
28+
return runServer(TEST_DATABASE_URL);
2829
});
2930

30-
after(function() {
31+
after(function () {
3132
return closeServer();
3233
});
3334

34-
beforeEach(function() {});
35+
beforeEach(function () { });
3536

36-
afterEach(function() {
37+
afterEach(function () {
3738
return User.remove({});
3839
});
3940

40-
describe('/api/users', function() {
41-
describe('POST', function() {
42-
it('Should reject users with missing username', function() {
41+
describe('/api/users', function () {
42+
describe('POST', function () {
43+
it('Should reject users with missing username', function () {
4344
return chai
4445
.request(app)
4546
.post('/api/users')
@@ -63,7 +64,7 @@ describe('/api/user', function() {
6364
expect(res.body.location).to.equal('username');
6465
});
6566
});
66-
it('Should reject users with missing password', function() {
67+
it('Should reject users with missing password', function () {
6768
return chai
6869
.request(app)
6970
.post('/api/users')
@@ -87,7 +88,7 @@ describe('/api/user', function() {
8788
expect(res.body.location).to.equal('password');
8889
});
8990
});
90-
it('Should reject users with non-string username', function() {
91+
it('Should reject users with non-string username', function () {
9192
return chai
9293
.request(app)
9394
.post('/api/users')
@@ -114,7 +115,7 @@ describe('/api/user', function() {
114115
expect(res.body.location).to.equal('username');
115116
});
116117
});
117-
it('Should reject users with non-string password', function() {
118+
it('Should reject users with non-string password', function () {
118119
return chai
119120
.request(app)
120121
.post('/api/users')
@@ -141,7 +142,7 @@ describe('/api/user', function() {
141142
expect(res.body.location).to.equal('password');
142143
});
143144
});
144-
it('Should reject users with non-string first name', function() {
145+
it('Should reject users with non-string first name', function () {
145146
return chai
146147
.request(app)
147148
.post('/api/users')
@@ -168,7 +169,7 @@ describe('/api/user', function() {
168169
expect(res.body.location).to.equal('firstName');
169170
});
170171
});
171-
it('Should reject users with non-string last name', function() {
172+
it('Should reject users with non-string last name', function () {
172173
return chai
173174
.request(app)
174175
.post('/api/users')
@@ -195,7 +196,7 @@ describe('/api/user', function() {
195196
expect(res.body.location).to.equal('lastName');
196197
});
197198
});
198-
it('Should reject users with non-trimmed username', function() {
199+
it('Should reject users with non-trimmed username', function () {
199200
return chai
200201
.request(app)
201202
.post('/api/users')
@@ -222,7 +223,7 @@ describe('/api/user', function() {
222223
expect(res.body.location).to.equal('username');
223224
});
224225
});
225-
it('Should reject users with non-trimmed password', function() {
226+
it('Should reject users with non-trimmed password', function () {
226227
return chai
227228
.request(app)
228229
.post('/api/users')
@@ -249,7 +250,7 @@ describe('/api/user', function() {
249250
expect(res.body.location).to.equal('password');
250251
});
251252
});
252-
it('Should reject users with empty username', function() {
253+
it('Should reject users with empty username', function () {
253254
return chai
254255
.request(app)
255256
.post('/api/users')
@@ -276,7 +277,7 @@ describe('/api/user', function() {
276277
expect(res.body.location).to.equal('username');
277278
});
278279
});
279-
it('Should reject users with password less than ten characters', function() {
280+
it('Should reject users with password less than ten characters', function () {
280281
return chai
281282
.request(app)
282283
.post('/api/users')
@@ -303,7 +304,7 @@ describe('/api/user', function() {
303304
expect(res.body.location).to.equal('password');
304305
});
305306
});
306-
it('Should reject users with password greater than 72 characters', function() {
307+
it('Should reject users with password greater than 72 characters', function () {
307308
return chai
308309
.request(app)
309310
.post('/api/users')
@@ -330,7 +331,7 @@ describe('/api/user', function() {
330331
expect(res.body.location).to.equal('password');
331332
});
332333
});
333-
it('Should reject users with duplicate username', function() {
334+
it('Should reject users with duplicate username', function () {
334335
// Create an initial user
335336
return User.create({
336337
username,
@@ -364,7 +365,7 @@ describe('/api/user', function() {
364365
expect(res.body.location).to.equal('username');
365366
});
366367
});
367-
it('Should create a new user', function() {
368+
it('Should create a new user', function () {
368369
return chai
369370
.request(app)
370371
.post('/api/users')
@@ -399,7 +400,7 @@ describe('/api/user', function() {
399400
expect(passwordIsCorrect).to.be.true;
400401
});
401402
});
402-
it('Should trim firstName and lastName', function() {
403+
it('Should trim firstName and lastName', function () {
403404
return chai
404405
.request(app)
405406
.post('/api/users')
@@ -432,15 +433,15 @@ describe('/api/user', function() {
432433
});
433434
});
434435

435-
describe('GET', function() {
436-
it('Should return an empty array initially', function() {
436+
describe('GET', function () {
437+
it('Should return an empty array initially', function () {
437438
return chai.request(app).get('/api/users').then(res => {
438439
expect(res).to.have.status(200);
439440
expect(res.body).to.be.an('array');
440441
expect(res.body).to.have.length(0);
441442
});
442443
});
443-
it('Should return an array of users', function() {
444+
it('Should return an array of users', function () {
444445
return User.create(
445446
{
446447
username,

0 commit comments

Comments
 (0)