Skip to content

Commit

Permalink
feat(user-roles): Adds user roles (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
emiliosnic authored May 21, 2017
1 parent 9ea7c14 commit e2b8e92
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 28 deletions.
19 changes: 0 additions & 19 deletions CHANGELOG.md

This file was deleted.

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ authClient.user.create(email, password, firstName, lastName)
authClient.user.email;
authClient.user.bearer;
authClient.user.publisherId;
authClient.user.roles;

/**
* Update User
Expand Down
22 changes: 21 additions & 1 deletion dist/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -3057,6 +3057,7 @@ var User = function () {
assert(consumer$$1 instanceof consumer, '`consumer` should be instance of Consumer');
this._store = store$$1;
this._consumer = consumer$$1;
this._roles = [];
this._bearer = undefined;
this._id = undefined;
this._publisherId = undefined;
Expand Down Expand Up @@ -3100,6 +3101,7 @@ var User = function () {
_this._email = data.email;
_this._firstName = data.first_name;
_this._lastName = data.last_name;
_this._roles = data.roles;
_this._bearer = bearer;
_this._isDirty = false;
return Promise.resolve({
Expand Down Expand Up @@ -3171,6 +3173,7 @@ var User = function () {
_this3._publisherId = data.publisher_id;
_this3._firstName = data.first_name;
_this3._lastName = data.last_name;
_this3._roles = data.roles;
_this3._email = data.email;
_this3._isDirty = false;
return Promise.resolve({
Expand Down Expand Up @@ -3216,6 +3219,7 @@ var User = function () {
_this4._publisherId = data.publisher_id;
_this4._email = data.email;
_this4._firstName = data.first_name;
_this4._roles = data.roles;
_this4._lastName = data.last_name;
_this4._isDirty = false;
return Promise.resolve({
Expand Down Expand Up @@ -3268,6 +3272,7 @@ var User = function () {
_this5._email = data.email;
_this5._firstName = data.first_name;
_this5._lastName = data.last_name;
_this5._roles = data.roles;
_this5._isDirty = false;
return Promise.resolve({
data: data,
Expand All @@ -3292,6 +3297,7 @@ var User = function () {
this._firstName = undefined;
this._lastName = undefined;
this._email = undefined;
this._roles = [];
this._isDirty = false;
return this._store.remove('access_token', 'refresh_token').then(function () {
return Promise.resolve({
Expand Down Expand Up @@ -3344,6 +3350,19 @@ var User = function () {
return this._bearer;
}

/**
* Returns User roles
*
* @return {Array} [read-only] roles
*
*/

}, {
key: 'roles',
get: function get$$1() {
return this._roles;
}

/**
* Returns User first Name
*
Expand Down Expand Up @@ -4075,7 +4094,8 @@ var users = [{
"first_name": "John",
"last_name": "Doe",
"email": "john.doe@mail.com",
"password": "qwerty123"
"password": "qwerty123",
"roles": ["developer"]
}];

var users$1 = Object.freeze({
Expand Down
3 changes: 2 additions & 1 deletion fixtures/users.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"first_name": "John",
"last_name": "Doe",
"email": "john.doe@mail.com",
"password": "qwerty123"
"password": "qwerty123",
"roles": ["developer"]
}
]
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "authentication-client",
"version": "2.8.0",
"version": "2.9.0",
"description": "A thin Authentication API consumer",
"homepage": "https://github.com/Avocarrot/authentication-client",
"main": "dist/bundle.js",
Expand Down
16 changes: 16 additions & 0 deletions src/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class User {
assert(consumer instanceof Consumer, '`consumer` should be instance of Consumer');
this._store = store;
this._consumer = consumer;
this._roles = [];
this._bearer = undefined;
this._id = undefined;
this._publisherId = undefined;
Expand Down Expand Up @@ -71,6 +72,16 @@ class User {
return this._bearer;
}

/**
* Returns User roles
*
* @return {Array} [read-only] roles
*
*/
get roles() {
return this._roles;
}

/**
* Returns User first Name
*
Expand Down Expand Up @@ -123,6 +134,7 @@ class User {
this._email = data.email;
this._firstName = data.first_name;
this._lastName = data.last_name;
this._roles = data.roles;
this._bearer = bearer;
this._isDirty = false;
return Promise.resolve({
Expand Down Expand Up @@ -180,6 +192,7 @@ class User {
this._publisherId = data.publisher_id;
this._firstName = data.first_name;
this._lastName = data.last_name;
this._roles = data.roles;
this._email = data.email;
this._isDirty = false;
return Promise.resolve({
Expand Down Expand Up @@ -216,6 +229,7 @@ class User {
this._publisherId = data.publisher_id;
this._email = data.email;
this._firstName = data.first_name;
this._roles = data.roles;
this._lastName = data.last_name;
this._isDirty = false;
return Promise.resolve({
Expand Down Expand Up @@ -263,6 +277,7 @@ class User {
this._email = data.email;
this._firstName = data.first_name;
this._lastName = data.last_name;
this._roles = data.roles;
this._isDirty = false;
return Promise.resolve({
data,
Expand All @@ -284,6 +299,7 @@ class User {
this._firstName = undefined;
this._lastName = undefined;
this._email = undefined;
this._roles = [];
this._isDirty = false;
return this._store.remove('access_token', 'refresh_token').then(() => Promise.resolve({
message: 'Flushed User data',
Expand Down
2 changes: 2 additions & 0 deletions tests/mocks/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const UserMocks = {
first_name: 'John',
last_name: 'Doe',
email: 'john.doe@mail.com',
roles: ['developer'],
_links: {
self: {
href: 'https://auth.mock.com/users/44d2c8e0-762b-4fa5-8571-097c81c3130d',
Expand All @@ -27,6 +28,7 @@ const UserMocks = {
first_name: options.first_name,
last_name: options.last_name,
email: options.email,
roles: options.roles,
_links: {
self: {
href: 'http://auth.mock.com/users/44d2c8e0-762b-4fa5-8571-097c81c3130d',
Expand Down
41 changes: 35 additions & 6 deletions tests/src/models/user.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,18 @@ test('User.publisherId should be read-only', (assert) => {
assert.equals(instances.user.publisherId, undefined);
});

/**
* User.roles
*/

test('User.roles should be read-only', (assert) => {
assert.plan(2);
const instances = getUserInstances();
assert.deepEquals(instances.user.roles, []);
instances.user.roles = ['developer'];
assert.deepEquals(instances.user.roles, []);
});

/**
* User.email
*/
Expand Down Expand Up @@ -169,7 +181,7 @@ test('User.authenticate(username, password) should throw an error for', (t) => {
});

test('User.authenticate(username, password) should store user and token on success', (assert) => {
assert.plan(9);
assert.plan(10);
const instances = getUserInstances();
const storeSetStub = sandbox.stub();
const retrieveUserStub = sandbox.stub();
Expand All @@ -181,6 +193,7 @@ test('User.authenticate(username, password) should store user and token on succe
first_name: 'John',
last_name: 'Doe',
email: 'john.doe@mail.com',
roles: ['developer'],
});
retrieveTokenStub.returns(Promise.resolve({
refresh_token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9',
Expand All @@ -197,13 +210,15 @@ test('User.authenticate(username, password) should store user and token on succe
assert.equals(instances.user.email, 'john.doe@mail.com');
assert.equals(instances.user.firstName, 'John');
assert.equals(instances.user.lastName, 'Doe');
assert.deepEquals(instances.user.roles, ['developer']);
assert.equals(res.message, 'Authenticated User');
assert.deepEquals(res.data, {
email: 'john.doe@mail.com',
first_name: 'John',
id: '44d2c8e0-762b-4fa5-8571-097c81c3130d',
last_name: 'Doe',
publisher_id: '55f5c8e0-762b-4fa5-8571-197c8183130a',
roles: ['developer'],
});
});
sandbox.restore();
Expand All @@ -215,7 +230,7 @@ test('User.authenticate(username, password) should store user and token on succe
*/

test('User.syncWithStore() should synrchronize data with Store', (assert) => {
assert.plan(8);
assert.plan(9);
const instances = getUserInstances();
const storeGetStub = sandbox.stub();
const retrieveUserStub = sandbox.stub();
Expand All @@ -227,6 +242,7 @@ test('User.syncWithStore() should synrchronize data with Store', (assert) => {
first_name: 'John',
last_name: 'Doe',
email: 'john.doe@mail.com',
roles: ['developer'],
}));
instances.store.get = storeGetStub;
instances.consumer.retrieveUser = retrieveUserStub;
Expand All @@ -239,6 +255,7 @@ test('User.syncWithStore() should synrchronize data with Store', (assert) => {
assert.equals(instances.user.firstName, 'John');
assert.equals(instances.user.lastName, 'Doe');
assert.equals(instances.user._isDirty, false);
assert.deepEquals(instances.user.roles, ['developer']);
});
sandbox.restore();
});
Expand All @@ -260,7 +277,7 @@ test('User.authenticateWithToken(accessToken, refreshToken) should', (t) => {
});

t.test('store user and token on success', (assert) => {
assert.plan(8);
assert.plan(9);
const instances = getUserInstances();
const storeSetStub = sandbox.stub();
const storeRemoveStub = sandbox.stub();
Expand All @@ -273,6 +290,7 @@ test('User.authenticateWithToken(accessToken, refreshToken) should', (t) => {
first_name: 'John',
last_name: 'Doe',
email: 'john.doe@mail.com',
roles: ['developer'],
}));
instances.store.set = storeSetStub;
instances.store.remove = storeRemoveStub;
Expand All @@ -284,20 +302,22 @@ test('User.authenticateWithToken(accessToken, refreshToken) should', (t) => {
assert.equals(instances.user.email, 'john.doe@mail.com');
assert.equals(instances.user.firstName, 'John');
assert.equals(instances.user.lastName, 'Doe');
assert.deepEquals(instances.user.roles, ['developer']);
assert.equals(res.message, 'Authenticated User');
assert.deepEquals(res.data, {
email: 'john.doe@mail.com',
first_name: 'John',
id: '44d2c8e0-762b-4fa5-8571-097c81c3130d',
last_name: 'Doe',
publisher_id: '55f5c8e0-762b-4fa5-8571-197c8183130a',
roles: ['developer'],
});
});
sandbox.restore();
});

t.test('successfully refresh tokens on `invalid_grant` failure', (assert) => {
assert.plan(14);
assert.plan(15);
const instances = getUserInstances();
const storeSetStub = sandbox.stub();
const storeRemoveStub = sandbox.stub();
Expand All @@ -316,6 +336,7 @@ test('User.authenticateWithToken(accessToken, refreshToken) should', (t) => {
first_name: 'John',
last_name: 'Doe',
email: 'john.doe@mail.com',
roles: ['developer'],
}));
instances.store.set = storeSetStub;
instances.store.remove = storeRemoveStub;
Expand All @@ -334,13 +355,15 @@ test('User.authenticateWithToken(accessToken, refreshToken) should', (t) => {
assert.equals(instances.user.email, 'john.doe@mail.com');
assert.equals(instances.user.firstName, 'John');
assert.equals(instances.user.lastName, 'Doe');
assert.deepEquals(instances.user.roles, ['developer']);
assert.equals(res.message, 'Authenticated User');
assert.deepEquals(res.data, {
email: 'john.doe@mail.com',
first_name: 'John',
id: '44d2c8e0-762b-4fa5-8571-097c81c3130d',
last_name: 'Doe',
publisher_id: '55f5c8e0-762b-4fa5-8571-197c8183130a',
roles: ['developer'],
});
});
sandbox.restore();
Expand Down Expand Up @@ -412,7 +435,7 @@ test('User.create(email, password, firstName, lastName) should reject invalid pa
});

test('User.create(email, password, firstName, lastName) should set User data on success', (assert) => {
assert.plan(11);
assert.plan(12);
const response = Object.assign(UserMocks.User, {});
const instances = getUserInstances();
sandbox.stub(instances.consumer, 'createUser', () => Promise.resolve(response));
Expand All @@ -426,6 +449,7 @@ test('User.create(email, password, firstName, lastName) should set User data on
assert.equals(res.data.first_name, response.first_name);
assert.equals(res.data.last_name, response.last_name);
assert.equals(res.data.email, response.email);
assert.equals(res.data.roles, response.roles);
assert.ok(res.data.id);
assert.ok(res.data.publisher_id);
});
Expand Down Expand Up @@ -462,6 +486,9 @@ test('User.save() should update User with new data', (assert) => {
// Update data
instances.user.firstName = 'Foo';
instances.user.lastName = 'Bar';
// Include role update (should be declined)
instances.user.roles = ['admin'];
// Save model
instances.user.save().then((res) => {
assert.equals(instances.user._isDirty, false);
assert.equals(updateUserStub.callCount, 1);
Expand All @@ -480,6 +507,7 @@ test('User.save() should skip update of synced User data', (assert) => {
instances.user._firstName = userData.first_name;
instances.user._lastName = userData.last_name;
instances.user._email = userData.email;
instances.user._roles = userData.roles;
instances.user._bearer = TokenMocks.PaswordGrant.access_token;
// Stub consumer
const updateUserStub = sandbox.stub(instances.consumer, 'updateUser', () => Promise.resolve());
Expand All @@ -501,7 +529,7 @@ test('User.save() should skip update of synced User data', (assert) => {
*/

test('User.flush() removes user data', (assert) => {
assert.plan(10);
assert.plan(11);
const instances = getUserInstances();
const storeRemoveStub = sandbox.stub(instances.store, 'remove', () => Promise.resolve({}));
instances.user.flush().then((res) => {
Expand All @@ -513,6 +541,7 @@ test('User.flush() removes user data', (assert) => {
assert.equals(instances.user.publisherId, undefined);
assert.equals(instances.user.firstName, undefined);
assert.equals(instances.user.lastName, undefined);
assert.deepEquals(instances.user.roles, []);
assert.equals(instances.user.email, undefined);
assert.equals(instances.user._isDirty, false);
});
Expand Down
Loading

0 comments on commit e2b8e92

Please sign in to comment.