Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: admin.signOut() throws error when signed out #113

Merged
merged 3 commits into from Aug 8, 2016
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions admin/README.md
Expand Up @@ -205,6 +205,10 @@ but without the session id:
Rejects with:

<table>
<tr>
<th align="left"><code>UnauthenticatedError</code></th>
<td>Not signed in</td>
</tr>
<tr>
<th align="left"><code>Error</code></th>
<td><em>A custom error thrown in a <code>before:signout</code> hook</em></td>
Expand Down
5 changes: 5 additions & 0 deletions lib/sign-out.js
Expand Up @@ -7,8 +7,13 @@ 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 signOut (state) {
if (!internals.isSignedIn(state)) {
return Promise.reject(new Error('UnauthenticatedError: Not signed in'))
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good to me 👍


var accountProperties = internals.get(state)

var preHooks = []
Expand Down
28 changes: 28 additions & 0 deletions test/integration/sign-out-not-signed-in-test.js
@@ -0,0 +1,28 @@
var store = require('humble-localstorage')
var test = require('tape')

var Account = require('../../index')

var baseURL = 'http://localhost:3000'

test('sign out without being signed in', function (t) {
t.plan(2)

// simulate user who has not yet signed in
store.clear()
store.setObject('account', {})
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When simulating a user who is not signed in, Is it sufficient to set the account object to an empty object literal, or should there be more items in the object? From the code, it seems that having a session object inside of the account object implies that the user is logged in, so I wouldn't want to add a session object, but I didn't know if any other information is necessary in the account object to simulate a user who is not signed in.

Copy link
Member

@gr2m gr2m Aug 8, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the store.clear() should be sufficient here. The account.signOut() call should not require account to be stored as an empty {} in the store. Compare for example to the .signIn() integration test


var account = new Account({
url: baseURL,
id: 'abc4567'
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This question ties in with my last one. Should I remove the id from account, since (I think) the id represents a session ID that would be assigned to a logged-in user?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

id represents the account id here. It’s an optional argument, you can see it described in the README: https://github.com/hoodiehq/hoodie-account-client#constructor

})

account.signOut().catch(function (error) {
t.is(typeof error, 'object', 'rejects with error object')
t.equal(
error.message,
'UnauthenticatedError: Not signed in',
'error not an UnauthenticatedError'
)
})
})
22 changes: 22 additions & 0 deletions test/unit/sign-out-test.js
Expand Up @@ -68,3 +68,25 @@ test('signOut() with request error', function (t) {
t.pass('rejects with error')
})
})

test('signOut() without being signed in', function (t) {
t.plan(2)

signOut({
account: {},
emitter: {
emit: simple.stub()
}
})

.then(t.fail.bind(t, 'must reject'))

.catch(function (error) {
t.is(typeof error, 'object', 'rejects with error object')
t.equal(
error.message,
'UnauthenticatedError: Not signed in',
'error not an UnauthenticatedError'
)
})
})