From 96f20276818527186ac01cd162174cae4a3da092 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pradel=20Le=CC=81o?= Date: Fri, 24 Feb 2017 19:03:11 +0100 Subject: [PATCH 1/3] Add password recovery functions --- packages/client/src/AccountsClient.js | 44 +++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/packages/client/src/AccountsClient.js b/packages/client/src/AccountsClient.js index aa83ae084..8f70c8999 100644 --- a/packages/client/src/AccountsClient.js +++ b/packages/client/src/AccountsClient.js @@ -226,6 +226,38 @@ export class AccountsClient { throw new AccountsError(err.message); } } + + async verifyEmail(token: string): Promise { + try { + await this.transport.verifyEmail(token); + } catch (err) { + throw new AccountsError(err.message); + } + } + + async resetPassword(token: string, newPassword: string): Promise { + try { + await this.transport.resetPassword(token, newPassword); + } catch (err) { + throw new AccountsError(err.message); + } + } + + async requestPasswordReset(userId: string, email?: string): Promise { + try { + await this.transport.sendResetPasswordEmail(userId, email); + } catch (err) { + throw new AccountsError(err.message); + } + } + + async requestVerificationEmail(userId: string, email?: string): Promise { + try { + await this.transport.sendVerificationEmail(userId, email); + } catch (err) { + throw new AccountsError(err.message); + } + } } const Accounts = { @@ -269,6 +301,18 @@ const Accounts = { refreshSession(): Promise { return this.instance.refreshSession(); }, + verifyEmail(token: string): Promise { + return this.instance.verifyEmail(token); + }, + resetPassword(token: string, newPassword: string): Promise { + return this.instance.resetPassword(token, newPassword); + }, + requestPasswordReset(userId: string, email?: string): Promise { + return this.instance.requestPasswordReset(userId, email); + }, + requestVerificationEmail(userId: string, email?: string): Promise { + return this.instance.requestVerificationEmail(userId, email); + }, }; export default Accounts; From 342c7572d76e08b9b2fd43bf54bdc5d63ff76be6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pradel=20Le=CC=81o?= Date: Fri, 24 Feb 2017 20:44:41 +0100 Subject: [PATCH 2/3] remove userId --- packages/client/src/AccountsClient.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/client/src/AccountsClient.js b/packages/client/src/AccountsClient.js index 8f70c8999..4edca30d5 100644 --- a/packages/client/src/AccountsClient.js +++ b/packages/client/src/AccountsClient.js @@ -243,17 +243,17 @@ export class AccountsClient { } } - async requestPasswordReset(userId: string, email?: string): Promise { + async requestPasswordReset(email?: string): Promise { try { - await this.transport.sendResetPasswordEmail(userId, email); + await this.transport.sendResetPasswordEmail(email); } catch (err) { throw new AccountsError(err.message); } } - async requestVerificationEmail(userId: string, email?: string): Promise { + async requestVerificationEmail(email?: string): Promise { try { - await this.transport.sendVerificationEmail(userId, email); + await this.transport.sendVerificationEmail(email); } catch (err) { throw new AccountsError(err.message); } @@ -307,11 +307,11 @@ const Accounts = { resetPassword(token: string, newPassword: string): Promise { return this.instance.resetPassword(token, newPassword); }, - requestPasswordReset(userId: string, email?: string): Promise { - return this.instance.requestPasswordReset(userId, email); + requestPasswordReset(email?: string): Promise { + return this.instance.requestPasswordReset(email); }, - requestVerificationEmail(userId: string, email?: string): Promise { - return this.instance.requestVerificationEmail(userId, email); + requestVerificationEmail(email?: string): Promise { + return this.instance.requestVerificationEmail(email); }, }; From 8d7718cefa41db2ab7f4850b31be397f592b0394 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pradel=20Le=CC=81o?= Date: Tue, 28 Feb 2017 10:44:47 +0100 Subject: [PATCH 3/3] Add some tests --- packages/client/src/AccountsClient.spec.js | 85 ++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/packages/client/src/AccountsClient.spec.js b/packages/client/src/AccountsClient.spec.js index de8c751f9..794663103 100644 --- a/packages/client/src/AccountsClient.spec.js +++ b/packages/client/src/AccountsClient.spec.js @@ -354,4 +354,89 @@ describe('Accounts', () => { // }); // }); }); + + describe('verifyEmail', () => { + it('should return an AccountsError', async () => { + const error = 'something bad'; + Accounts.config({}, { verifyEmail: () => Promise.reject({ message: error }) }); + try { + await Accounts.verifyEmail(); + throw new Error(); + } catch (err) { + expect(err.message).toEqual(error); + } + }); + + it('should call transport.verifyEmail', async () => { + const mock = jest.fn(() => Promise.resolve()); + Accounts.config({}, { verifyEmail: mock }); + await Accounts.verifyEmail('token'); + expect(mock.mock.calls.length).toEqual(1); + expect(mock.mock.calls[0][0]).toEqual('token'); + }); + }); + + describe('resetPassword', () => { + it('should return an AccountsError', async () => { + const error = 'something bad'; + Accounts.config({}, { resetPassword: () => Promise.reject({ message: error }) }); + try { + await Accounts.resetPassword(); + throw new Error(); + } catch (err) { + expect(err.message).toEqual(error); + } + }); + + it('should call transport.resetPassword', async () => { + const mock = jest.fn(() => Promise.resolve()); + Accounts.config({}, { resetPassword: mock }); + await Accounts.resetPassword('token', 'newPassword'); + expect(mock.mock.calls.length).toEqual(1); + expect(mock.mock.calls[0][0]).toEqual('token'); + expect(mock.mock.calls[0][1]).toEqual('newPassword'); + }); + }); + + describe('requestPasswordReset', () => { + it('should return an AccountsError', async () => { + const error = 'something bad'; + Accounts.config({}, { sendResetPasswordEmail: () => Promise.reject({ message: error }) }); + try { + await Accounts.requestPasswordReset(); + throw new Error(); + } catch (err) { + expect(err.message).toEqual(error); + } + }); + + it('should call transport.sendResetPasswordEmail', async () => { + const mock = jest.fn(() => Promise.resolve()); + Accounts.config({}, { sendResetPasswordEmail: mock }); + await Accounts.requestPasswordReset('email'); + expect(mock.mock.calls.length).toEqual(1); + expect(mock.mock.calls[0][0]).toEqual('email'); + }); + }); + + describe('requestVerificationEmail', () => { + it('should return an AccountsError', async () => { + const error = 'something bad'; + Accounts.config({}, { sendVerificationEmail: () => Promise.reject({ message: error }) }); + try { + await Accounts.requestVerificationEmail(); + throw new Error(); + } catch (err) { + expect(err.message).toEqual(error); + } + }); + + it('should call transport.sendVerificationEmail', async () => { + const mock = jest.fn(() => Promise.resolve()); + Accounts.config({}, { sendVerificationEmail: mock }); + await Accounts.requestVerificationEmail('email'); + expect(mock.mock.calls.length).toEqual(1); + expect(mock.mock.calls[0][0]).toEqual('email'); + }); + }); });