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

Add password recovery functions to client #70

Merged
merged 3 commits into from
Feb 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions packages/client/src/AccountsClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,38 @@ export class AccountsClient {
throw new AccountsError(err.message);
}
}

async verifyEmail(token: string): Promise<void> {
try {
await this.transport.verifyEmail(token);
} catch (err) {
throw new AccountsError(err.message);
}
}

async resetPassword(token: string, newPassword: string): Promise<void> {
try {
await this.transport.resetPassword(token, newPassword);
} catch (err) {
throw new AccountsError(err.message);
}
}

async requestPasswordReset(email?: string): Promise<void> {
try {
await this.transport.sendResetPasswordEmail(email);
} catch (err) {
throw new AccountsError(err.message);
}
}

async requestVerificationEmail(email?: string): Promise<void> {
try {
await this.transport.sendVerificationEmail(email);
} catch (err) {
throw new AccountsError(err.message);
}
}
}

const Accounts = {
Expand Down Expand Up @@ -269,6 +301,18 @@ const Accounts = {
refreshSession(): Promise<void> {
return this.instance.refreshSession();
},
verifyEmail(token: string): Promise<void> {
return this.instance.verifyEmail(token);
},
resetPassword(token: string, newPassword: string): Promise<void> {
return this.instance.resetPassword(token, newPassword);
},
requestPasswordReset(email?: string): Promise<void> {
return this.instance.requestPasswordReset(email);
},
requestVerificationEmail(email?: string): Promise<void> {
return this.instance.requestVerificationEmail(email);
},
};

export default Accounts;
Expand Down
85 changes: 85 additions & 0 deletions packages/client/src/AccountsClient.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
});