Skip to content

Commit

Permalink
feat: CMS-2648 own account relation me
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-scherzinger committed Feb 13, 2017
1 parent fb404b0 commit a294cf3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
33 changes: 22 additions & 11 deletions src/Accounts.js
Expand Up @@ -89,11 +89,11 @@ export default class Accounts extends Core {
* @returns {Promise<AccountResource>} resolves to the Account which should be loaded.
*/
get(accountID) {
if (!accountID) {
throw new Error('accountID must be defined');
}
return Promise.resolve()
.then(() => {
if (!accountID) {
throw new Error('accountID must be defined');
}
const request = this.newRequest()
.follow('ec:accounts/options')
.withTemplateParameters({ accountid: accountID });
Expand All @@ -102,6 +102,17 @@ export default class Accounts extends Core {
.then(([res, traversal]) => new AccountResource(res, this.environment, traversal));
}

me() {
return Promise.resolve()
.then(() => {
const request = this.newRequest()
.follow('ec:account')
.withTemplateParameters();
return get(this.environment, request);
})
.then(([res, traversal]) => new AccountResource(res, this.environment, traversal));
}

/**
* Creates a new API token with 100 years validity.
*
Expand Down Expand Up @@ -273,17 +284,17 @@ export default class Accounts extends Core {
changeEmail(email) {
return Promise.resolve()
.then(() => {
if (!email) {
throw new Error('email must be defined');
}
if (!email) {
throw new Error('email must be defined');
}

if (!this.tokenStore.has()) {
throw new Error('not logged in.');
}
if (!this.tokenStore.has()) {
throw new Error('not logged in.');
}

const request = this.newRequest().follow('ec:auth/change-email');
const request = this.newRequest().follow('ec:auth/change-email');

return postEmpty(this.environment, request, { email });
return postEmpty(this.environment, request, { email });
});
}

Expand Down
20 changes: 17 additions & 3 deletions test/Account.test.js
Expand Up @@ -79,9 +79,23 @@ describe('Accounts class', () => {
throw err;
});
});
it('should throw on get in undefiend id', () => {
const throws = () => new Accounts().get();
throws.should.throw(Error);
it('should be rejected on get in undefiend id', () => {
return new Accounts().get().should.be.rejectedWith(Error);
});
it('should return resource on me', () => {
const accounts = new Accounts('live');
const stub = sinon.stub(helper, 'get');
stub.returns(resolver('account-list.json'));

return accounts.me()
.then((resource) => {
resource.should.be.instanceof(AccountResource);
stub.restore();
})
.catch((err) => {
stub.restore();
throw err;
});
});
it('should create API token', () => {
const accounts = new Accounts('live');
Expand Down

0 comments on commit a294cf3

Please sign in to comment.