Skip to content

Commit

Permalink
Merge branch 'release/1.0.28'
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Oct 16, 2018
2 parents 6ac8e73 + 6a9580b commit 183e916
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 19 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
<a name="1.0.28"></a>
## [1.0.28](https://github.com/adonisjs/adonis-session/compare/v1.0.27...v1.0.28) (2018-10-16)


### Features

* **flash:** use request.original when exists ([961501a](https://github.com/adonisjs/adonis-session/commit/961501a))



<a name="1.0.27"></a>
## [1.0.27](https://github.com/adonisjs/adonis-session/compare/v1.0.26...v1.0.27) (2018-09-27)

Expand Down
3 changes: 3 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ environment:
- nodejs_version: 8.0.0
init: git config --global core.autocrlf true
install:
- nuget install redis-64 -excludeversion
- redis-64\tools\redis-server.exe --service-install
- redis-64\tools\redis-server.exe --service-start
- ps: 'Install-Product node $env:nodejs_version'
- npm install
test_script:
Expand Down
21 changes: 11 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@adonisjs/session",
"version": "1.0.27",
"version": "1.0.28",
"description": "This repo is the official session provider for Adonisjs apps. It supports multiple drivers to store session data.",
"main": "providers/SessionProvider",
"files": [
Expand Down Expand Up @@ -31,20 +31,21 @@
"license": "MIT",
"devDependencies": {
"@adonisjs/fold": "^4.0.9",
"@adonisjs/mrm-preset": "^1.0.13",
"@adonisjs/mrm-preset": "^1.0.14",
"@adonisjs/sink": "^1.0.17",
"commitizen": "^2.10.1",
"commitizen": "^3.0.2",
"coveralls": "^3.0.2",
"cz-conventional-changelog": "^2.1.0",
"ioredis": "^4.0.0",
"japa": "^2.0.3",
"ioredis": "^4.1.0",
"japa": "^2.0.5",
"japa-cli": "^1.0.1",
"macroable": "^1.0.0",
"mrm": "^1.2.1",
"node-cookie": "^2.1.1",
"nyc": "^13.0.1",
"pkg-ok": "^2.2.0",
"semver": "^5.5.1",
"node-req": "^2.1.1",
"nyc": "^13.1.0",
"pkg-ok": "^2.3.1",
"semver": "^5.6.0",
"standard": "^12.0.1",
"supertest": "^3.3.0"
},
Expand All @@ -60,8 +61,8 @@
},
"dependencies": {
"@adonisjs/generic-exceptions": "^2.0.1",
"bson": "^3.0.2",
"debug": "^4.0.1",
"bson": "^4.0.0-rc5",
"debug": "^4.1.0",
"fs-extra": "^7.0.0",
"lodash": "^4.17.11",
"ms": "^2.1.1",
Expand Down
12 changes: 12 additions & 0 deletions src/Session/Store.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,14 @@ const toString = {
'String' (value) {
return value
},
/**
* To be backward compatible
*/
'ObjectID' (value) {
return String(value)
},
'ObjectId' (value) {
return String(value)
}
}

Expand Down Expand Up @@ -64,8 +70,14 @@ const toOriginalType = {
'String' (value) {
return value
},
/**
* To be backward compatible
*/
'ObjectID' (value) {
return new ObjectId(value)
},
'ObjectId' (value) {
return new ObjectId(value)
}
}

Expand Down
28 changes: 21 additions & 7 deletions src/Session/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,20 @@ class Session {
return new Store(sessionValue)
}

/**
* Returns the request body for flashing data inside
* sessions
*
* @method _requestBody
*
* @return {Object}
*
* @private
*/
_requestBody () {
return typeof (this._request.original) === 'function' ? this._request.original() : this._request.all()
}

/**
* Instantiate session object
*
Expand Down Expand Up @@ -285,7 +299,7 @@ class Session {
*/
flashAll () {
this._ensureNotFreezed()
return this.flash(this._request.all())
return this.flash(this._requestBody())
}

/* istanbul ignore next */
Expand All @@ -295,13 +309,13 @@ class Session {
*
* @method flashOnly
*
* @param {...Spread} fields
* @param {Array} fields
*
* @chainable
*/
flashOnly (...fields) {
flashOnly (fields) {
this._ensureNotFreezed()
return this.flash(this._request.only(...fields))
return this.flash(_.pick(this._requestBody(), fields))
}

/* istanbul ignore next */
Expand All @@ -311,13 +325,13 @@ class Session {
*
* @method flashExcept
*
* @param {...Spread} fields
* @param {Array} fields
*
* @chainable
*/
flashExcept (...fields) {
flashExcept (fields) {
this._ensureNotFreezed()
return this.flash(this._request.except(...fields))
return this.flash(_.omit(this._requestBody(), fields))
}

/**
Expand Down
99 changes: 99 additions & 0 deletions test/flash.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,105 @@ test.group('Flash Messages', () => {
const { text } = await supertest(server).get('/').expect(500)
assert.match(text, /E_INVALID_PARAMETER: Flash data should be an object instead received string/)
})

test('flash request original data', async (assert) => {
const server = http.createServer((req, res) => {
const config = new Config()
const cookie = new Cookie(config)
cookie.setRequest(helpers.getRequest(req), helpers.getResponse(res))
const session = new Session(helpers.getRequest(req), helpers.getResponse(res), cookie, config)
session
.instantiate()
.then(() => {
session.flashAll()
return session.commit()
})
.then(() => {
res.end()
})
.catch(({ message, status }) => {
res.writeHead(status || 500)
res.write(message)
res.end()
})
})

const { headers } = await supertest(server).post('/?username=virk').expect(200)
assert.deepEqual(helpers.getValueObject(headers['set-cookie'][1]), {
__flash__: {
t: 'Object',
d: JSON.stringify({
username: 'virk'
})
}
})
})

test('flash request picked fields data', async (assert) => {
const server = http.createServer((req, res) => {
const config = new Config()
const cookie = new Cookie(config)
cookie.setRequest(helpers.getRequest(req), helpers.getResponse(res))
const session = new Session(helpers.getRequest(req), helpers.getResponse(res), cookie, config)
session
.instantiate()
.then(() => {
session.flashOnly(['age'])
return session.commit()
})
.then(() => {
res.end()
})
.catch(({ message, status }) => {
res.writeHead(status || 500)
res.write(message)
res.end()
})
})

const { headers } = await supertest(server).post('/?username=virk&age=22').expect(200)
assert.deepEqual(helpers.getValueObject(headers['set-cookie'][1]), {
__flash__: {
t: 'Object',
d: JSON.stringify({
age: '22'
})
}
})
})

test('flash request except picked fields data', async (assert) => {
const server = http.createServer((req, res) => {
const config = new Config()
const cookie = new Cookie(config)
cookie.setRequest(helpers.getRequest(req), helpers.getResponse(res))
const session = new Session(helpers.getRequest(req), helpers.getResponse(res), cookie, config)
session
.instantiate()
.then(() => {
session.flashExcept(['age'])
return session.commit()
})
.then(() => {
res.end()
})
.catch(({ message, status }) => {
res.writeHead(status || 500)
res.write(message)
res.end()
})
})

const { headers } = await supertest(server).post('/?username=virk&age=22').expect(200)
assert.deepEqual(helpers.getValueObject(headers['set-cookie'][1]), {
__flash__: {
t: 'Object',
d: JSON.stringify({
username: 'virk'
})
}
})
})
})

test.group('Flash View Globals', (group) => {
Expand Down
4 changes: 4 additions & 0 deletions test/helpers/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const nodeCookie = require('node-cookie')
const nodeReq = require('node-req')
const querystring = require('querystring')

module.exports = {
Expand All @@ -16,6 +17,9 @@ module.exports = {
getRequest (req) {
return {
request: req,
original () {
return nodeReq.get(req)
},
cookie: function (key) {
return nodeCookie.get(req, key)
}
Expand Down
4 changes: 2 additions & 2 deletions test/session.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,8 @@ test.group('Session Store', () => {
test('guard objectId', (assert) => {
const store = new Store()
const id = '507f191e810c19729de860ea'
const objId = ObjectId(id)
assert.deepEqual(store._guardValue(objId), { d: id, t: 'ObjectID' })
const objId = new ObjectId(id)
assert.deepEqual(store._guardValue(objId), { d: id, t: 'ObjectId' })
})

test('unguard object', (assert) => {
Expand Down

0 comments on commit 183e916

Please sign in to comment.