Skip to content
This repository was archived by the owner on Nov 25, 2020. It is now read-only.

Commit

Permalink
feat(authService): add profileMethod config option
Browse files Browse the repository at this point in the history
Supports a new config property 'profileMethod' that controls
the HTTP method used for AuthService.updateMe().
  • Loading branch information
pfurini committed May 28, 2016
1 parent 877fa50 commit 218fffc
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/authService.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ export class AuthService {
if (typeof criteria === 'string' || typeof criteria === 'number') {
criteria = { id: criteria };
}
return this.client.update(this.config.withBase(this.config.profileUrl), criteria, body);
if (this.config.profileMethod === 'put') {
return this.client.update(this.config.withBase(this.config.profileUrl), criteria, body);
}
return this.client.patch(this.config.withBase(this.config.profileUrl), criteria, body);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/baseConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ export class BaseConfig {
signupUrl = '/auth/signup';
// The API endpoint used in profile requests (inc. `find/get` and `update`)
profileUrl = '/auth/me';
// The method used to update the profile ('put' or 'patch')
profileMethod = 'put';
// The API endpoint used with oAuth to unlink authentication
unlinkUrl = '/auth/unlink/';
// The HTTP method used for 'unlink' requests (Options: 'get' or 'post')
Expand Down
62 changes: 60 additions & 2 deletions test/authService.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,14 @@ describe('AuthService', () => {
});


describe('.updateMe()', () => {
describe('.updateMe() with PUT', () => {
const container = getContainer();
const authService = container.get(AuthService);


beforeEach(() => {
authService.config.profileMethod = 'put';
});

it('without criteria', done => {
authService.updateMe({data: 'some'})
.then(result => {
Expand Down Expand Up @@ -134,6 +138,60 @@ describe('AuthService', () => {
});


describe('.updateMe() with PATCH', () => {
const container = getContainer();
const authService = container.get(AuthService);

beforeEach(() => {
authService.config.profileMethod = 'patch';
});

it('without criteria', done => {
authService.updateMe({data: 'some'})
.then(result => {
expect(result.method).toBe('PATCH');
expect(result.path).toBe('/auth/me');
expect(JSON.stringify(result.query)).toBe('{}');
expect(result.body.data).toBe('some');
done();
});
});

it('with criteria a number', done => {
authService.updateMe({data: 'some'}, 5)
.then(result => {
expect(result.method).toBe('PATCH');
expect(result.path).toBe('/auth/me');
expect(result.query.id).toBe('5');
expect(result.body.data).toBe('some');
done();
});
});

it('with criteria a string', done => {
authService.updateMe({data: 'some'}, 'five')
.then(result => {
expect(result.method).toBe('PATCH');
expect(result.path).toBe('/auth/me');
expect(result.query.id).toBe('five');
expect(result.body.data).toBe('some');
done();
});
});

it('with criteria an object', done => {
authService.updateMe({data: 'some'}, {foo: 'bar'})
.then(result => {
expect(result.method).toBe('PATCH');
expect(result.path).toBe('/auth/me');
expect(result.query.foo).toBe('bar');
expect(result.body.data).toBe('some');
done();
});
});
});


describe('.getAccessToken()', () => {
const container = getContainer();
const authService = container.get(AuthService);
Expand Down

0 comments on commit 218fffc

Please sign in to comment.