Skip to content

Commit

Permalink
fix: reauthenticate an expired session after successful account fetch (
Browse files Browse the repository at this point in the history
  • Loading branch information
mbad0la authored and gr2m committed Apr 11, 2017
1 parent 264be5c commit e339829
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ function accountGet (state, path, options) {
cachedProperties = result
}

// reauthenticate an expired session
if (cachedProperties.session.invalid) {
delete cachedProperties.session.invalid
state.emitter.emit('reauthenticate')
}

return state.cache.set(cachedProperties)

.then(function () {
Expand Down
6 changes: 6 additions & 0 deletions lib/profile-get.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ function profileGet (state, path, options) {
cachedProperties.profile = result
}

// reauthenticate an expired session
if (cachedProperties.session.invalid) {
delete cachedProperties.session.invalid
state.emitter.emit('reauthenticate')
}

return state.cache.set(cachedProperties)

.then(function () {
Expand Down
47 changes: 47 additions & 0 deletions test/unit/get-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,53 @@ test('get(["id", "session.id"]) with session', function (t) {
.catch(t.error)
})

test('get() and reauthenticate on invalid session', function (t) {
simple.mock(internals, 'fetchProperties').resolveWith({
foo: 'bar'
})
var state = {
setup: Promise.resolve(),
cache: {
get: function () {
return Promise.resolve({
session: {
id: 'session123',
invalid: true
}
})
},
set: simple.stub().resolveWith()
},
emitter: {
emit: simple.stub()
}
}

get(state)

.then(function (result) {
t.deepEqual(result, {
foo: 'bar',
session: {
id: 'session123'
}
})
t.deepEqual(state.cache.set.lastCall.arg, {
foo: 'bar',
session: {
id: 'session123'
}
})
t.is(state.emitter.emit.callCount, 1)
t.deepEqual(state.emitter.emit.lastCall.arg, 'reauthenticate')

simple.restore()
t.end()
})

.catch(t.error)
})

test('get() with session and server error', function (t) {
simple.mock(internals, 'fetchProperties').rejectWith(new Error('oops'))
var state = {
Expand Down
46 changes: 46 additions & 0 deletions test/unit/profile-get-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,52 @@ test('profileGet() with session', function (t) {
.catch(t.error)
})

test('profileGet() and reauthenticate on invalid session', function (t) {
simple.mock(internals, 'fetchProperties').resolveWith({
foo: 'bar'
})
var state = {
setup: Promise.resolve(),
cache: {
get: function () {
return Promise.resolve({
session: {
id: 'session123',
invalid: true
}
})
},
set: simple.stub().resolveWith()
},
emitter: {
emit: simple.stub()
}
}

profileGet(state)

.then(function (result) {
t.deepEqual(result, {
foo: 'bar'
})
t.deepEqual(state.cache.set.lastCall.arg, {
profile: {
foo: 'bar'
},
session: {
id: 'session123'
}
})
t.is(state.emitter.emit.callCount, 1)
t.deepEqual(state.emitter.emit.lastCall.arg, 'reauthenticate')

simple.restore()
t.end()
})

.catch(t.error)
})

test('profileGet() with session and server error', function (t) {
simple.mock(internals, 'fetchProperties').rejectWith(new Error('oops'))
var state = {
Expand Down

0 comments on commit e339829

Please sign in to comment.