Skip to content

Commit

Permalink
tests working, .env files removed comments
Browse files Browse the repository at this point in the history
  • Loading branch information
bloombar committed Nov 26, 2024
1 parent a0aef7a commit 1b5001f
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-back.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:
- name: Build and push
uses: docker/build-push-action@v3
with:
context: "{{defaultContext}}:back-end"
context: '{{defaultContext}}:back-end'
platforms: linux/amd64,linux/arm64,linux/arm/v7
push: true
tags: bloombar/data-storage-example-app-back-end:latest
4 changes: 3 additions & 1 deletion back-end/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const path = require('path')
const cookieParser = require('cookie-parser') // middleware useful for parsing cookies in requests
require('dotenv').config({ silent: true }) // load environmental variables from a hidden file named .env

console.log(`Front-end assumed to be at ${process.env.FRONT_END_DOMAIN}`)

// the following are used for authentication with JSON Web Tokens
const jwt = require('jsonwebtoken')
const passport = require('passport')
Expand All @@ -23,7 +25,7 @@ const mongoose = require('mongoose')
const User = require('./models/User.js')

// connect to the database
// console.log(`Conneting to MongoDB at ${process.env.MONGODB_URI}`)
// console.log(`Connecting to MongoDB at ${process.env.MONGODB_URI}`)
try {
mongoose.connect(process.env.MONGODB_URI)
console.log(`Connected to MongoDB.`)
Expand Down
2 changes: 1 addition & 1 deletion back-end/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Example of a few data storage techniques on the back-end web server",
"main": "server.js",
"scripts": {
"test": "mocha --timeout 2000",
"test": "mocha --timeout 2000 --exit",
"coverage": "nyc --reporter=html --reporter=text --reporter=lcov --reporter=clover mocha --timeout 10000 --exit",
"coveralls": "nyc report --reporter=text-lcov | coveralls"
},
Expand Down
7 changes: 5 additions & 2 deletions back-end/server.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
#!/usr/bin/env node

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

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

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

// a function to stop listening to the port
const close = () => {
listener.close()
// mongoose.disonnect()
// process.exit(0)
}

// export the close function
module.exports = {
app: app,
close: close,
}
11 changes: 8 additions & 3 deletions back-end/test/get-cookie.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,23 @@ const expect = chai.expect // the assertion library in the style using the word
const should = chai.should() // the same assertion library in the style using the word 'should'

// import the server
const server = require('../app')
const server = require('../server')

// a group of tests related to the /logout route
describe('Get cookie', () => {
// clean up by destroying the server when done
after(() => {
server.close()
})

/**
* test the GET /get-cookie route
*/
const cookieData = 'foo=bar' // mock cookie data
describe('GET /cookie/get with Cookies in request', () => {
it('it should return a 200 HTTP response code', done => {
chai
.request(server)
.request(server.app)
.get('/cookie/get')
.set('Cookie', cookieData) // set a cookie header with a valid cookie key/value pair our server is expecting
.end((err, res) => {
Expand All @@ -32,7 +37,7 @@ describe('Get cookie', () => {

it('it should return an object with specific properties', done => {
chai
.request(server)
.request(server.app)
.get('/cookie/get')
.set('Cookie', cookieData) // set a cookie header with a valid cookie key/value pair our server is expecting
.end((err, res) => {
Expand Down
11 changes: 8 additions & 3 deletions back-end/test/login.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,24 @@ const expect = chai.expect // the assertion library in the style using the word
const should = chai.should() // the same assertion library in the style using the word 'should'

// import the server
const server = require('../app')
const server = require('../server')
const User = require('../models/User')

// a group of tests related to the /protected route
describe('Login', () => {
// clean up by destroying the server when done
after(() => {
server.close()
})

/**
* test the POST /login route
*/
const formData = { username: 'bla', password: 'wrong' } // mock form data with incorrect credentials
describe('POST /auth/login with incorrect username/password', () => {
it('it should return a 401 HTTP response code', done => {
chai
.request(server)
.request(server.app)
.post('/auth/login')
.type('form')
.send(formData)
Expand Down Expand Up @@ -56,7 +61,7 @@ describe('Login', () => {

it('it should return a 200 HTTP response code', done => {
chai
.request(server)
.request(server.app)
.post('/auth/login')
.type('form')
.send(formData)
Expand Down
11 changes: 8 additions & 3 deletions back-end/test/logout.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,22 @@ const expect = chai.expect // the assertion library in the style using the word
const should = chai.should() // the same assertion library in the style using the word 'should'

// import the server
const server = require('../app')
const server = require('../server')

// a group of tests related to the /logout route
describe('Logout', () => {
// clean up by destroying the server when done
after(() => {
server.close()
})

/**
* test the GET /logout route
*/
describe('GET /auth/logout', () => {
it('it should return a 200 HTTP response code', done => {
chai
.request(server)
.request(server.app)
.get('/auth/logout')
.end((err, res) => {
res.should.have.status(200) // use 'should' to make BDD-style assertions
Expand All @@ -32,7 +37,7 @@ describe('Logout', () => {
// logging out is really not a server-side issue when using JWT authentication
// nevertheless, including this for example
chai
.request(server)
.request(server.app)
.get('/auth/logout')
.end((err, res) => {
res.body.should.be.a('object') // our route sends back an object
Expand Down
13 changes: 9 additions & 4 deletions back-end/test/protected.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,23 @@ const expect = chai.expect // the assertion library in the style using the word
const should = chai.should() // the same assertion library in the style using the word 'should'

// import the server
const server = require('../app')
const server = require('../server')

// a group of tests related to the /protected route
describe('Protected', () => {
// clean up by destroying the server when done
after(() => {
server.close()
})

/**
* test the GET /protected route
*/
describe('GET /protected when not logged in', () => {
// test a protected route when not logged in... passport auth should send back a 401 HTTP error
it('it should return a 401 HTTP response code', done => {
chai
.request(server)
.request(server.app)
.get('/protected')
.end((err, res) => {
res.should.have.status(401) // use 'should' to make BDD-style assertions
Expand Down Expand Up @@ -65,7 +70,7 @@ describe('Protected', () => {
it('it should return a 200 HTTP response code', done => {
console.log(`DEBUG: running test`)
chai
.request(server)
.request(server.app)
.get('/protected')
.set('Authorization', `JWT ${token}`) // set JWT authentication headers to simulate a logged-in user, using the token we created at top
.end((err, res) => {
Expand All @@ -76,7 +81,7 @@ describe('Protected', () => {

it('it should return an object with specific properties', done => {
chai
.request(server)
.request(server.app)
.get('/protected')
.set('Authorization', `JWT ${token}`) // set JWT authentication headers to simulate a logged-in user, using the token we created at top
.end((err, res) => {
Expand Down
13 changes: 9 additions & 4 deletions back-end/test/set-cookie.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,23 @@ const expect = chai.expect // the assertion library in the style using the word
const should = chai.should() // the same assertion library in the style using the word 'should'

// import the server
const server = require('../app')
const server = require('../server')

// a group of tests related to the /logout route
describe('Set cookie', () => {
// clean up by destroying the server when done
after(() => {
server.close()
})

/**
* test the GET /set-cookie route
*/
const cookieData = 'foo=bar' // mock cookie data
describe('GET /cookie/set', () => {
it('it should return a 200 HTTP response code', done => {
chai
.request(server)
.request(server.app)
.get('/cookie/set')
.end((err, res) => {
res.should.have.status(200) // use 'should' to make BDD-style assertions
Expand All @@ -33,7 +38,7 @@ describe('Set cookie', () => {
// logging out is really not a server-side issue when using JWT authentication
// nevertheless, including this for example
chai
.request(server)
.request(server.app)
.get('/cookie/set')
.end((err, res) => {
res.body.should.be.a('object') // our route sends back an object
Expand All @@ -46,7 +51,7 @@ describe('Set cookie', () => {
// logging out is really not a server-side issue when using JWT authentication
// nevertheless, including this for example
chai
.request(server)
.request(server.app)
.get('/cookie/set')
.end((err, res) => {
const [expectedKey, expectedValue] = cookieData.split('=') // get the expected cookie key/value pair
Expand Down
2 changes: 1 addition & 1 deletion front-end/.env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# 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
PORT=4000 # the port at which the react app will run
PORT=4000
REACT_APP_BACKEND=http://localhost:3000
2 changes: 2 additions & 0 deletions front-end/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import Login from './Login'
import Logout from './Logout'
import Protected from './Protected'

console.log(`Back-end assumed to be at ${process.env.REACT_APP_BACKEND}`)

const App = props => {
return (
<div className="App">
Expand Down

0 comments on commit 1b5001f

Please sign in to comment.