Skip to content

Commit

Permalink
feat(account): destroy when not signed in (#119)
Browse files Browse the repository at this point in the history
closes #110
  • Loading branch information
thomasjinlo authored and gr2m committed Oct 8, 2016
1 parent a6a26be commit 3cd20bc
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 8 deletions.
23 changes: 15 additions & 8 deletions lib/destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,26 @@ var internals = module.exports.internals = {}
internals.request = require('../utils/request')
internals.clearSession = require('../utils/clear-session')
internals.get = require('./get')
internals.isSignedIn = require('./is-signed-in')

function destroy (state) {
var accountProperties = internals.get(state)

return internals.request({
method: 'DELETE',
url: state.url + '/session/account',
headers: {
authorization: 'Session ' + state.account.session.id
}
})
var promise = Promise.resolve()

if (internals.isSignedIn(state)) {
promise = promise.then(function () {
internals.request({
method: 'DELETE',
url: state.url + '/session/account',
headers: {
authorization: 'Session ' + state.account.session.id
}
})
})
}

.then(function () {
return promise.then(function () {
internals.clearSession({
cacheKey: state.cacheKey
})
Expand Down
40 changes: 40 additions & 0 deletions test/integration/destroy-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var nock = require('nock')
var clone = require('lodash/clone')
var signInResponse = clone(require('../fixtures/signin.json'))
var simple = require('simple-mock')
var lolex = require('lolex')

var baseURL = 'http://localhost:3000'
var options = {
Expand Down Expand Up @@ -60,3 +61,42 @@ test('destroy account', function (t) {

.catch(t.error)
})

test('destroy account even when session is invalid', function (t) {
store.clear()
t.plan(5)

// mock the Date object to always return 1970-01-01T00:00:00.000Z
var clock = lolex.install(0)
var account = new Account({
url: baseURL,
id: 'abc4567'
})

var signOutHandler = simple.stub()
var destroyHandler = simple.stub()
account.on('signout', signOutHandler)
account.on('destroy', destroyHandler)

account.destroy(options)

.then(function () {
clock.uninstall()
t.pass('destroys account')

t.deepEqual(signOutHandler.lastCall.arg, {
createdAt: '1970-01-01T00:00:00.000Z',
id: 'abc4567'
}, '"signout" event emitted with account object')

t.deepEqual(destroyHandler.lastCall.arg, {
createdAt: '1970-01-01T00:00:00.000Z',
id: 'abc4567'
}, '"destroy" event emitted with account object')

t.is(signOutHandler.callCount, 1, '"signout" event emitted once')
t.is(destroyHandler.callCount, 1, '"destroy" event emitted once')
})

.catch(t.error)
})

0 comments on commit 3cd20bc

Please sign in to comment.