...
 
Commits (7)
......@@ -19,4 +19,4 @@ services:
- ./package.json:/usr/package.json
- ./package-lock.json:/usr/package-lock.json
- ./webpack.config.js:/usr/webpack.config.js
command: npm run dev:start
command: bash -c "npm install && npm run dev:start"
const express = require('express')
const router = express.Router()
const jwt = require('jsonwebtoken')
const passport = require('./passport')
router.post('/login', (req, res) => {
passport.authenticate('local', { session: false }, (err, user) => {
if (err || !user) {
return res
.status(400)
.json({ message: 'Something is not right', user: user })
}
req.login(user, { session: false }, err => {
if (err) {
res.send(err)
}
const token = jwt.sign(user, 'please-let-me-pass-psit4')
return res.json({ token })
})
})(req, res)
})
module.exports = router
const passport = require('passport')
const LocalStrategy = require('passport-local').Strategy
const passportJWT = require('passport-jwt')
const JWTStrategy = passportJWT.Strategy
const ExtractJWT = passportJWT.ExtractJwt
const adminUser = { username: 'admin', password: 'psit4' }
passport.use(
new LocalStrategy((username, password, done) => {
if (username === adminUser.username && password === adminUser.password) {
done(null, adminUser)
} else {
done(null, false)
}
})
)
passport.use(
new JWTStrategy(
{
jwtFromRequest: ExtractJWT.fromAuthHeaderAsBearerToken(),
secretOrKey: 'please-let-me-pass-psit4'
},
(jwtPayload, cb) => {
return cb(null, jwtPayload)
}
)
)
module.exports = passport
const request = require('supertest')
const jwt = require('jsonwebtoken')
const server = require('../server')
const { postgressConnect, mainSpecs, testSpecs } = require('../db/pg')
const { writeData } = require('../db/dbUtils')
......@@ -6,7 +7,7 @@ const { initGraphql } = require('./graphql')
const { dbInitForTests, dropDb } = require('../db/init')
let pgTest
const authToken = jwt.sign({}, 'please-let-me-pass-psit4')
const startTestApi = async () => {}
beforeAll(async () => {
......@@ -27,6 +28,7 @@ describe('Test if the api is available', () => {
test('It should respond to the GET method', async () => {
const response = await request(server)
.get('/graphql')
.set('Authorization', 'bearer ' + authToken)
.send(`query={typeList{id}}`)
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
......@@ -38,6 +40,7 @@ describe('Test if the api is available', () => {
test('It should respond to the POST method', async () => {
const response = await request(server)
.post('/graphql')
.set('Authorization', 'bearer ' + authToken)
.send({ query: `{typeList{id}}` })
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
......
......@@ -6,17 +6,14 @@ const server = require('./server')
const { postgressConnect, prodSpecs } = require('./db/pg')
const { dbinit } = require('./db/init')
const { refreshRoutes } = require('./refreshRoutes')
const auth = require('./auth/authRoutes')
//const { refreshCustomRoutes } = require('./customRoutes')
const cors = require('cors')
const port = 3000
Sentry.init({
dsn: 'https://d06473e562d74397a1433f63a52430e0@sentry.io/1439248'
})
server.use(cors())
dbinit().then(async () => {
await refreshRoutes({
server,
......
const request = require('supertest')
const jwt = require('jsonwebtoken')
const server = require('../server')
const { postgressConnect, mainSpecs, testSpecs } = require('../db/pg')
const { writeData } = require('../db/dbUtils')
......@@ -7,7 +8,7 @@ const { dbInitForTests, dropDb } = require('../db/init')
const { mountRestRoutes } = require('./routesRest.js')
let pgTest
const authToken = jwt.sign({}, 'please-let-me-pass-psit4')
const startTestApi = async () => {}
beforeAll(async () => {
......@@ -25,17 +26,31 @@ afterAll(async () => {
mainDb.pool.end()
})
describe('Test if the REST "/rest/job" delivers', () => {
test('It should respond to the GET method with correct data', async () => {
const response = await request(server)
.get('/rest/job')
.set('Authorization', 'bearer ' + authToken)
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(200)
expect(JSON.parse(response.text)).toEqual([
{id : 1, Title : "Programmier-Spezi", Company : 4, Posted : "2019-03-30", Deadline : "2019-05-30", Requirements : [5,7]},
{id : 2, Title : "Web Bastler", Company : 3, Posted : "2019-03-30",Deadline : "2019-05-30", Requirements : [6]}
{
id: 1,
Title: 'Programmier-Spezi',
Company: 4,
Posted: '2019-03-30',
Deadline: '2019-05-30',
Requirements: [5, 7]
},
{
id: 2,
Title: 'Web Bastler',
Company: 3,
Posted: '2019-03-30',
Deadline: '2019-05-30',
Requirements: [6]
}
])
})
})
......@@ -44,28 +59,33 @@ describe('Test if the REST "/rest/job/1" delivers', () => {
test('It should respond to the GET method', async () => {
const response = await request(server)
.get('/rest/job/1')
.set('Authorization', 'bearer ' + authToken)
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(200)
expect(JSON.parse(response.text)).toEqual(
{id : 1, Title : "Programmier-Spezi", Company : 4, Posted : "2019-03-30", Deadline : "2019-05-30", Requirements : [5,7]}
)
expect(JSON.parse(response.text)).toEqual({
id: 1,
Title: 'Programmier-Spezi',
Company: 4,
Posted: '2019-03-30',
Deadline: '2019-05-30',
Requirements: [5, 7]
})
})
})
describe('Test if the REST "/rest/requirement" delivers', () => {
test('It should respond to the GET method with correct data', async () => {
const response = await request(server)
.get('/rest/requirement')
.set('Authorization', 'bearer ' + authToken)
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(200)
expect(JSON.parse(response.text)).toEqual([
{id : 5, Requirement : "C#"},
{id : 6, Requirement : "Javascript"},
{id : 7, Requirement : "Java"}
{ id: 5, Requirement: 'C#' },
{ id: 6, Requirement: 'Javascript' },
{ id: 7, Requirement: 'Java' }
])
})
})
......@@ -74,13 +94,13 @@ describe('Test if the REST "/rest/requirement/6" delivers', () => {
test('It should respond to the GET method', async () => {
const response = await request(server)
.get('/rest/requirement/6')
.set('Authorization', 'bearer ' + authToken)
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(200)
expect(JSON.parse(response.text)).toEqual(
{id : 6, Requirement : "Javascript"}
)
expect(JSON.parse(response.text)).toEqual({
id: 6,
Requirement: 'Javascript'
})
})
})
const request = require('supertest')
const jwt = require('jsonwebtoken')
const server = require('../server')
const { postgressConnect, mainSpecs, testSpecs } = require('../db/pg')
const { writeData } = require('../db/dbUtils')
......@@ -7,7 +8,7 @@ const { dbInitForTests, dropDb } = require('../db/init')
const { mountCustomRoutes } = require('./routesCustom.js')
let pgTest
const authToken = jwt.sign({}, 'please-let-me-pass-psit4')
const startTestApi = async () => {}
beforeAll(async () => {
......@@ -25,31 +26,42 @@ afterAll(async () => {
mainDb.pool.end()
})
describe('Test if the the custom route "/routes/allJobsComplete" delivers', () => {
test('It should respond to the GET method with correct data', async () => {
const response = await request(server)
.get('/routes/allJobsComplete')
.set('Authorization', 'bearer ' + authToken)
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(200)
expect(JSON.parse(response.text)).toEqual({
JobList : [
{id : 1,Title : "Programmier-Spezi", Posted : "2019-03-30", Deadline : "2019-05-30", Company : {
id : 4,
Name : "Microsoft"
}, Requirements : [
{id : 5, Requirement : "C#"},
{id : 7, Requirement : "Java"}
JobList: [
{
id: 1,
Title: 'Programmier-Spezi',
Posted: '2019-03-30',
Deadline: '2019-05-30',
Company: {
id: 4,
Name: 'Microsoft'
},
Requirements: [
{ id: 5, Requirement: 'C#' },
{ id: 7, Requirement: 'Java' }
]
},
{id : 2,Title : "Web Bastler", Posted : "2019-03-30", Deadline : "2019-05-30", Company : {
id : 3,
Name : "Apple"
}, Requirements:[
{id : 6, Requirement : "Javascript"}
]}
{
id: 2,
Title: 'Web Bastler',
Posted: '2019-03-30',
Deadline: '2019-05-30',
Company: {
id: 3,
Name: 'Apple'
},
Requirements: [{ id: 6, Requirement: 'Javascript' }]
}
]
})
})
})
\ No newline at end of file
})
const express = require('express')
const cors = require('cors')
const bodyParser = require('body-parser')
const app = express()
const passport = require('./auth/passport')
const authRoutes = require('./auth/authRoutes')
app.use(cors())
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(passport.initialize())
app.use('/auth', authRoutes)
app.all('*', passport.authenticate('jwt', { session: false }))
app.get('/', (req, res) => res.send('Hello World!'))
module.exports = app
const request = require('supertest')
const jwt = require('jsonwebtoken')
const server = require('./server')
const authToken = jwt.sign({}, 'please-let-me-pass-psit4')
describe('Test the root path', () => {
test('It should response the GET method', async () => {
test('It should require authentication', async () => {
const response = await request(server).get('/')
expect(response.statusCode).toBe(401)
})
test('It should pass authentication', async () => {
const response = await request(server)
.get('/')
.set('Authorization', 'bearer ' + authToken)
expect(response.statusCode).toBe(200)
})
})
This diff is collapsed.