Delete Account unit tests: added tests related to Google reauthentication

This commit is contained in:
gianantoniopini
2021-05-20 15:57:01 +02:00
parent 7cb469657d
commit 9e63d0b2c7
18 changed files with 363 additions and 56 deletions

View File

@ -2,6 +2,8 @@
import { v4 as uuidv4 } from 'uuid';
import Constants from '../constants/auth';
import AuthProvider from './authProvider';
import GoogleAuthProvider from './googleAuthProvider';
import User from './user';
import { delay } from '../../../src/utils/index';
@ -55,8 +57,47 @@ class Auth {
this._currentUser = new User(
user.displayName,
user.email,
user.isAnonymous,
user.providerId,
user.uid,
user.isAnonymous,
this.signOut,
);
await delay(Constants.defaultDelayInMilliseconds);
this.onAuthStateChangedObservers.forEach((observer) =>
observer(this._currentUser),
);
return this._currentUser;
}
/**
* Authenticates with popup.
*
* @param {AuthProvider} provider The provider to authenticate.
*/
async signInWithPopup(provider) {
if (!provider) {
throw new Error('provider must be provided.');
} else if (!(provider instanceof AuthProvider)) {
throw new Error('provider should be an AuthProvider.');
}
if (!(provider instanceof GoogleAuthProvider)) {
throw new Error(
`${provider.constructor.name} is currently not supported.`,
);
}
const user = Constants.googleUser3;
this._currentUser = new User(
user.displayName,
user.email,
user.providerId,
user.uid,
user.isAnonymous,
this.signOut,
);

View File

@ -0,0 +1,23 @@
/* eslint-disable no-underscore-dangle */
class AuthProvider {
/**
* Creates a new auth provider.
*
* @param {string} providerId Provider ID.
*/
constructor(providerId) {
if (!providerId) {
throw new Error('providerId must be provided.');
} else if (typeof providerId !== 'string') {
throw new Error('providerId should be a string.');
} else {
this._providerId = providerId;
}
}
get providerId() {
return this._providerId;
}
}
export default AuthProvider;

View File

@ -0,0 +1,10 @@
import AuthProvider from './authProvider';
import Constants from '../constants/auth';
class GoogleAuthProvider extends AuthProvider {
constructor() {
super(Constants.googleAuthProviderId);
}
}
export default GoogleAuthProvider;

View File

@ -1,25 +1,23 @@
/* eslint-disable no-underscore-dangle */
import Constants from '../constants/auth';
// eslint-disable-next-line no-unused-vars
import AuthProvider from './authProvider';
import UserInfo from './userInfo';
import { delay } from '../../../src/utils/index';
class User {
class User extends UserInfo {
/**
* Creates a new user.
*
* @param {string|null} displayName Display name.
* @param {string|null} email Email.
* @param {boolean} isAnonymous Is anonymous.
* @param {string} providerId Auth provider ID.
* @param {string} uid The user's unique ID.
* @param {boolean} isAnonymous Is anonymous.
* @param {function():Promise<void>} deleteUser Delete user callback.
*/
constructor(displayName, email, isAnonymous, uid, deleteUser) {
if (!uid) {
throw new Error('uid must be provided.');
} else if (typeof uid !== 'string') {
throw new Error('uid should be a string.');
} else {
this._uid = uid;
}
constructor(displayName, email, providerId, uid, isAnonymous, deleteUser) {
super(displayName, email, providerId, uid);
if (!deleteUser) {
throw new Error('deleteUser must be provided.');
@ -29,25 +27,22 @@ class User {
this._deleteUser = deleteUser;
}
this._displayName = displayName;
this._email = email;
this._isAnonymous = isAnonymous;
}
get displayName() {
return this._displayName;
}
get email() {
return this._email;
this._providerData = [];
if (!isAnonymous) {
this._providerData.push(
new UserInfo(displayName, email, providerId, uid),
);
}
}
get isAnonymous() {
return this._isAnonymous;
}
get uid() {
return this._uid;
get providerData() {
return this._providerData;
}
async delete() {
@ -55,6 +50,18 @@ class User {
await this._deleteUser();
}
/**
* Reauthenticates the user with popup.
*
* @param {AuthProvider} provider The provider to authenticate.
*/
// eslint-disable-next-line no-unused-vars
async reauthenticateWithPopup(provider) {
await delay(Constants.defaultDelayInMilliseconds);
return this;
}
}
export default User;

View File

@ -0,0 +1,47 @@
/* eslint-disable no-underscore-dangle */
class UserInfo {
/**
* Creates a new user profile information.
*
* @param {string|null} displayName Display name.
* @param {string|null} email Email.
* @param {string} providerId Auth provider ID.
* @param {string} uid The user's unique ID.
*/
constructor(displayName, email, providerId, uid) {
if (!uid) {
throw new Error('uid must be provided.');
} else if (typeof uid !== 'string') {
throw new Error('uid should be a string.');
} else {
this._uid = uid;
}
if (typeof providerId !== 'string') {
throw new Error('providerId should be a string.');
} else {
this._providerId = providerId;
}
this._displayName = displayName;
this._email = email;
}
get displayName() {
return this._displayName;
}
get email() {
return this._email;
}
get providerId() {
return this._providerId;
}
get uid() {
return this._uid;
}
}
export default UserInfo;