Skip to content

Commit 1b5001f

Browse files
committed
tests working, .env files removed comments
1 parent a0aef7a commit 1b5001f

File tree

11 files changed

+55
-23
lines changed

11 files changed

+55
-23
lines changed

.github/workflows/build-back.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ jobs:
4848
- name: Build and push
4949
uses: docker/build-push-action@v3
5050
with:
51-
context: "{{defaultContext}}:back-end"
51+
context: '{{defaultContext}}:back-end'
5252
platforms: linux/amd64,linux/arm64,linux/arm/v7
5353
push: true
5454
tags: bloombar/data-storage-example-app-back-end:latest

back-end/app.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ const path = require('path')
77
const cookieParser = require('cookie-parser') // middleware useful for parsing cookies in requests
88
require('dotenv').config({ silent: true }) // load environmental variables from a hidden file named .env
99

10+
console.log(`Front-end assumed to be at ${process.env.FRONT_END_DOMAIN}`)
11+
1012
// the following are used for authentication with JSON Web Tokens
1113
const jwt = require('jsonwebtoken')
1214
const passport = require('passport')
@@ -23,7 +25,7 @@ const mongoose = require('mongoose')
2325
const User = require('./models/User.js')
2426

2527
// connect to the database
26-
// console.log(`Conneting to MongoDB at ${process.env.MONGODB_URI}`)
28+
// console.log(`Connecting to MongoDB at ${process.env.MONGODB_URI}`)
2729
try {
2830
mongoose.connect(process.env.MONGODB_URI)
2931
console.log(`Connected to MongoDB.`)

back-end/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "Example of a few data storage techniques on the back-end web server",
55
"main": "server.js",
66
"scripts": {
7-
"test": "mocha --timeout 2000",
7+
"test": "mocha --timeout 2000 --exit",
88
"coverage": "nyc --reporter=html --reporter=text --reporter=lcov --reporter=clover mocha --timeout 10000 --exit",
99
"coveralls": "nyc report --reporter=text-lcov | coveralls"
1010
},

back-end/server.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
#!/usr/bin/env node
22

33
// import the express app
4-
const server = require('./app')
4+
const app = require('./app')
55
require('dotenv').config({ silent: true }) // load environmental variables from a hidden file named .env
66

77
// which port to listen for HTTP(S) requests
88
const port = process.env.PORT || 3000
99

1010
// call a function to start listening to the port
11-
const listener = server.listen(port, function () {
11+
const listener = app.listen(port, function () {
1212
console.log(`Server running on port: ${port}`)
1313
})
1414

1515
// a function to stop listening to the port
1616
const close = () => {
1717
listener.close()
18+
// mongoose.disonnect()
19+
// process.exit(0)
1820
}
1921

2022
// export the close function
2123
module.exports = {
24+
app: app,
2225
close: close,
2326
}

back-end/test/get-cookie.test.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,23 @@ const expect = chai.expect // the assertion library in the style using the word
1010
const should = chai.should() // the same assertion library in the style using the word 'should'
1111

1212
// import the server
13-
const server = require('../app')
13+
const server = require('../server')
1414

1515
// a group of tests related to the /logout route
1616
describe('Get cookie', () => {
17+
// clean up by destroying the server when done
18+
after(() => {
19+
server.close()
20+
})
21+
1722
/**
1823
* test the GET /get-cookie route
1924
*/
2025
const cookieData = 'foo=bar' // mock cookie data
2126
describe('GET /cookie/get with Cookies in request', () => {
2227
it('it should return a 200 HTTP response code', done => {
2328
chai
24-
.request(server)
29+
.request(server.app)
2530
.get('/cookie/get')
2631
.set('Cookie', cookieData) // set a cookie header with a valid cookie key/value pair our server is expecting
2732
.end((err, res) => {
@@ -32,7 +37,7 @@ describe('Get cookie', () => {
3237

3338
it('it should return an object with specific properties', done => {
3439
chai
35-
.request(server)
40+
.request(server.app)
3641
.get('/cookie/get')
3742
.set('Cookie', cookieData) // set a cookie header with a valid cookie key/value pair our server is expecting
3843
.end((err, res) => {

back-end/test/login.test.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,24 @@ const expect = chai.expect // the assertion library in the style using the word
1010
const should = chai.should() // the same assertion library in the style using the word 'should'
1111

1212
// import the server
13-
const server = require('../app')
13+
const server = require('../server')
1414
const User = require('../models/User')
1515

1616
// a group of tests related to the /protected route
1717
describe('Login', () => {
18+
// clean up by destroying the server when done
19+
after(() => {
20+
server.close()
21+
})
22+
1823
/**
1924
* test the POST /login route
2025
*/
2126
const formData = { username: 'bla', password: 'wrong' } // mock form data with incorrect credentials
2227
describe('POST /auth/login with incorrect username/password', () => {
2328
it('it should return a 401 HTTP response code', done => {
2429
chai
25-
.request(server)
30+
.request(server.app)
2631
.post('/auth/login')
2732
.type('form')
2833
.send(formData)
@@ -56,7 +61,7 @@ describe('Login', () => {
5661

5762
it('it should return a 200 HTTP response code', done => {
5863
chai
59-
.request(server)
64+
.request(server.app)
6065
.post('/auth/login')
6166
.type('form')
6267
.send(formData)

back-end/test/logout.test.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,22 @@ const expect = chai.expect // the assertion library in the style using the word
1010
const should = chai.should() // the same assertion library in the style using the word 'should'
1111

1212
// import the server
13-
const server = require('../app')
13+
const server = require('../server')
1414

1515
// a group of tests related to the /logout route
1616
describe('Logout', () => {
17+
// clean up by destroying the server when done
18+
after(() => {
19+
server.close()
20+
})
21+
1722
/**
1823
* test the GET /logout route
1924
*/
2025
describe('GET /auth/logout', () => {
2126
it('it should return a 200 HTTP response code', done => {
2227
chai
23-
.request(server)
28+
.request(server.app)
2429
.get('/auth/logout')
2530
.end((err, res) => {
2631
res.should.have.status(200) // use 'should' to make BDD-style assertions
@@ -32,7 +37,7 @@ describe('Logout', () => {
3237
// logging out is really not a server-side issue when using JWT authentication
3338
// nevertheless, including this for example
3439
chai
35-
.request(server)
40+
.request(server.app)
3641
.get('/auth/logout')
3742
.end((err, res) => {
3843
res.body.should.be.a('object') // our route sends back an object

back-end/test/protected.test.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,23 @@ const expect = chai.expect // the assertion library in the style using the word
1010
const should = chai.should() // the same assertion library in the style using the word 'should'
1111

1212
// import the server
13-
const server = require('../app')
13+
const server = require('../server')
1414

1515
// a group of tests related to the /protected route
1616
describe('Protected', () => {
17+
// clean up by destroying the server when done
18+
after(() => {
19+
server.close()
20+
})
21+
1722
/**
1823
* test the GET /protected route
1924
*/
2025
describe('GET /protected when not logged in', () => {
2126
// test a protected route when not logged in... passport auth should send back a 401 HTTP error
2227
it('it should return a 401 HTTP response code', done => {
2328
chai
24-
.request(server)
29+
.request(server.app)
2530
.get('/protected')
2631
.end((err, res) => {
2732
res.should.have.status(401) // use 'should' to make BDD-style assertions
@@ -65,7 +70,7 @@ describe('Protected', () => {
6570
it('it should return a 200 HTTP response code', done => {
6671
console.log(`DEBUG: running test`)
6772
chai
68-
.request(server)
73+
.request(server.app)
6974
.get('/protected')
7075
.set('Authorization', `JWT ${token}`) // set JWT authentication headers to simulate a logged-in user, using the token we created at top
7176
.end((err, res) => {
@@ -76,7 +81,7 @@ describe('Protected', () => {
7681

7782
it('it should return an object with specific properties', done => {
7883
chai
79-
.request(server)
84+
.request(server.app)
8085
.get('/protected')
8186
.set('Authorization', `JWT ${token}`) // set JWT authentication headers to simulate a logged-in user, using the token we created at top
8287
.end((err, res) => {

back-end/test/set-cookie.test.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,23 @@ const expect = chai.expect // the assertion library in the style using the word
1010
const should = chai.should() // the same assertion library in the style using the word 'should'
1111

1212
// import the server
13-
const server = require('../app')
13+
const server = require('../server')
1414

1515
// a group of tests related to the /logout route
1616
describe('Set cookie', () => {
17+
// clean up by destroying the server when done
18+
after(() => {
19+
server.close()
20+
})
21+
1722
/**
1823
* test the GET /set-cookie route
1924
*/
2025
const cookieData = 'foo=bar' // mock cookie data
2126
describe('GET /cookie/set', () => {
2227
it('it should return a 200 HTTP response code', done => {
2328
chai
24-
.request(server)
29+
.request(server.app)
2530
.get('/cookie/set')
2631
.end((err, res) => {
2732
res.should.have.status(200) // use 'should' to make BDD-style assertions
@@ -33,7 +38,7 @@ describe('Set cookie', () => {
3338
// logging out is really not a server-side issue when using JWT authentication
3439
// nevertheless, including this for example
3540
chai
36-
.request(server)
41+
.request(server.app)
3742
.get('/cookie/set')
3843
.end((err, res) => {
3944
res.body.should.be.a('object') // our route sends back an object
@@ -46,7 +51,7 @@ describe('Set cookie', () => {
4651
// logging out is really not a server-side issue when using JWT authentication
4752
// nevertheless, including this for example
4853
chai
49-
.request(server)
54+
.request(server.app)
5055
.get('/cookie/set')
5156
.end((err, res) => {
5257
const [expectedKey, expectedValue] = cookieData.split('=') // get the expected cookie key/value pair

front-end/.env.example

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# an example of .env file... make a copy of this called .env in your repository and make sure the back-end port number is correct
2-
PORT=4000 # the port at which the react app will run
2+
PORT=4000
33
REACT_APP_BACKEND=http://localhost:3000

front-end/src/App.js

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import Login from './Login'
1212
import Logout from './Logout'
1313
import Protected from './Protected'
1414

15+
console.log(`Back-end assumed to be at ${process.env.REACT_APP_BACKEND}`)
16+
1517
const App = props => {
1618
return (
1719
<div className="App">

0 commit comments

Comments
 (0)