Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Zipofar Ago committed Oct 30, 2019
1 parent 4040c38 commit b4bf3b0
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ dev:
compose: install migrate seed dev

test:
docker-compose -f docker-compose_dev.yml run app_server npx mocha --timeout 5000
docker-compose -f docker-compose_dev.yml run -e APP_ENV=test app_server npx mocha --timeout 5000 --exit

kill:
docker-compose -f docker-compose_dev.yml kill
Expand Down
1 change: 1 addition & 0 deletions app/knexfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const config = {
password: 'blt5_test',
database: 'blt5_test',
},
useNullAsDefault: true,
migrations: {
directory: path.join(BASE_PATH, 'migrations'),
},
Expand Down
2 changes: 1 addition & 1 deletion app/src/server/db/connection.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const environment = process.env.NODE_ENV || 'development';
const environment = process.env.APP_ENV || 'development';
const config = require('../../../knexfile.js')[environment];

module.exports = require('knex')(config);
19 changes: 17 additions & 2 deletions app/src/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const bodyParser = require('koa-bodyparser');
const session = require('koa-session');
const CSRF = require('koa-csrf');

const initState = require('../initAppState');
const indexRoute = require('./routes/index');
const userRoute = require('./routes/users');
const pageRoute = require('./routes/pages');
Expand Down Expand Up @@ -31,11 +32,25 @@ app.use(async (ctx, next) => {
});

app.use(bodyParser());
app.use(new CSRF());

// Init App State
app.use(async (ctx, next) => {
ctx.cookies.set('csrf', ctx.csrf, { httpOnly: false });
if (typeof ctx.session.state === 'undefined') {
ctx.session.state = { ...initState };
}
ctx.body = { state: ctx.session.state };
await next();
});

// Set CSRF
if (appEnv !== 'test') {
app.use(new CSRF());
app.use(async (ctx, next) => {
ctx.cookies.set('csrf', ctx.csrf, { httpOnly: false });
await next();
});
}

app.use(indexRoute.routes());
app.use(appStateRoute.middleware());
app.use(registrationRoute.middleware());
Expand Down
5 changes: 4 additions & 1 deletion app/test/test_login.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,22 @@ describe('API Login', () => {
await knex.migrate.rollback();
});

it('should return jwt token', async () => {
it('should success login', async () => {
const res = await chai.request(server)
.post('/api/v1/auth/login')
.type('json')
.send({
username: 'user1',
password: 'pass1',
});
console.log(res)

res.should.have.status(200);
res.should.be.json;
res.body.should.be.a('object');
});

/*
it('should return error, if username not exist', async () => {
const res = await chai.request(server)
.post('/api/v1/auth/login')
Expand Down Expand Up @@ -61,4 +63,5 @@ describe('API Login', () => {
res.body.should.be.a('object');
res.body.message.should.equal('Login or Password Incorrect');
});
*/
});

0 comments on commit b4bf3b0

Please sign in to comment.