Merge pull request #12405 from meteor/release-3.0-tests-accounts-2fa

Making accounts-2fa async
This commit is contained in:
Gabriel Grubba
2022-12-28 15:15:35 -03:00
committed by GitHub
3 changed files with 29 additions and 20 deletions

View File

@@ -7,6 +7,14 @@
* `email`:
- `Email.send` is no longer available. Use `Email.sendAsync` instead.
* `accounts-2fa`:
- Some methods are now async. See below:
- `Accounts._is2faEnabledForUser`
- `(Meteor Method) - generate2faActivationQrCode`
- `(Meteor Method) - enableUser2fa`
- `(Meteor Method) - disableUser2fa`
- `(Meteor Method) - has2faEnabled`
* `accounts-password`:
- `Accounts.sendResetPasswordEmail` is now async
- `Accounts.sendEnrollmentEmail` is now async

View File

@@ -13,8 +13,8 @@ Accounts._check2faEnabled = user => {
);
};
Accounts._is2faEnabledForUser = () => {
const user = Meteor.user();
Accounts._is2faEnabledForUser = async () => {
const user = await Meteor.user();
if (!user) {
throw new Meteor.Error('no-logged-user', 'No user logged in.');
}
@@ -34,9 +34,9 @@ Accounts._isTokenValid = (secret, code) => {
};
Meteor.methods({
generate2faActivationQrCode(appName) {
async generate2faActivationQrCode(appName) {
check(appName, String);
const user = Meteor.user();
const user = await Meteor.user();
if (!user) {
throw new Meteor.Error(
@@ -59,7 +59,7 @@ Meteor.methods({
});
const svg = new QRCode(uri).svg();
Meteor.users.update(
await Meteor.users.update(
{ _id: user._id },
{
$set: {
@@ -72,9 +72,9 @@ Meteor.methods({
return { svg, secret, uri };
},
enableUser2fa(code) {
async enableUser2fa(code) {
check(code, String);
const user = Meteor.user();
const user = await Meteor.user();
if (!user) {
throw new Meteor.Error(400, 'No user logged in.');
@@ -94,7 +94,7 @@ Meteor.methods({
Accounts._handleError('Invalid 2FA code', true, 'invalid-2fa-code');
}
Meteor.users.update(
await Meteor.users.update(
{ _id: user._id },
{
$set: {
@@ -106,14 +106,14 @@ Meteor.methods({
}
);
},
disableUser2fa() {
async disableUser2fa() {
const userId = Meteor.userId();
if (!userId) {
throw new Meteor.Error(400, 'No user logged in.');
}
Meteor.users.update(
await Meteor.users.update(
{ _id: userId },
{
$unset: {
@@ -122,8 +122,8 @@ Meteor.methods({
}
);
},
has2faEnabled() {
return Accounts._is2faEnabledForUser();
async has2faEnabled() {
return await Accounts._is2faEnabledForUser();
},
});

View File

@@ -1,15 +1,16 @@
import { Accounts } from 'meteor/accounts-base';
import { Random } from 'meteor/random';
const findUserById = id => Meteor.users.findOne(id);
const findUserById =
async id => await Meteor.users.findOne(id);
Tinytest.add('account - 2fa - has2faEnabled - server', test => {
Tinytest.addAsync('account - 2fa - has2faEnabled - server', async test => {
// Create users
const userWithout2FA = Accounts.insertUserDoc(
const userWithout2FA = await Accounts.insertUserDoc(
{},
{ emails: [{ address: `${Random.id()}@meteorapp.com`, verified: true }] }
);
const userWith2FA = Accounts.insertUserDoc(
const userWith2FA = await Accounts.insertUserDoc(
{},
{
emails: [{ address: `${Random.id()}@meteorapp.com`, verified: true }],
@@ -19,10 +20,10 @@ Tinytest.add('account - 2fa - has2faEnabled - server', test => {
}
);
test.equal(Accounts._check2faEnabled(findUserById(userWithout2FA)), false);
test.equal(Accounts._check2faEnabled(findUserById(userWith2FA)), true);
test.equal(Accounts._check2faEnabled(await findUserById(userWithout2FA)), false);
test.equal(Accounts._check2faEnabled(await findUserById(userWith2FA)), true);
// cleanup
Accounts.users.remove(userWithout2FA);
Accounts.users.remove(userWith2FA);
await Accounts.users.remove(userWithout2FA);
await Accounts.users.remove(userWith2FA);
});