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;

View File

@ -1,20 +1,36 @@
const googleAuthProviderId = 'google.com';
const anonymousUser1 = {
displayName: 'Anonymous User 1',
email: 'anonymous1@noemail.com',
isAnonymous: true,
uid: 'anonym123',
providerId: '',
uid: 'anonym1',
};
const anonymousUser2 = {
displayName: 'Anonymous User 2',
email: 'anonymous2@noemail.com',
isAnonymous: true,
uid: 'anonym456',
providerId: '',
uid: 'anonym2',
};
const googleUser3 = {
displayName: 'Google User 3',
email: 'google3@noemail.com',
isAnonymous: false,
providerId: googleAuthProviderId,
uid: 'google3',
};
const defaultDelayInMilliseconds = 100;
class Auth {
static get googleAuthProviderId() {
return googleAuthProviderId;
}
static get anonymousUser1() {
return anonymousUser1;
}
@ -23,6 +39,10 @@ class Auth {
return anonymousUser2;
}
static get googleUser3() {
return googleUser3;
}
static get defaultDelayInMilliseconds() {
return defaultDelayInMilliseconds;
}

View File

@ -9,7 +9,9 @@ const connectedPath = '.info/connected';
const demoStateResume1Id = 'demo_1';
const demoStateResume2Id = 'demo_2';
const initialStateResumeId = 'initst';
const demoStateResume3Id = 'demo_3';
const initialStateResume1Id = 'init_1';
const initialStateResume2Id = 'init_2';
const user1 = {
uid: AuthConstants.anonymousUser1.uid,
@ -19,6 +21,10 @@ const user2 = {
uid: AuthConstants.anonymousUser2.uid,
isAnonymous: AuthConstants.anonymousUser2.isAnonymous,
};
const user3 = {
uid: AuthConstants.googleUser3.uid,
isAnonymous: AuthConstants.googleUser3.isAnonymous,
};
const defaultDelayInMilliseconds = 100;
@ -51,8 +57,16 @@ class Database {
return demoStateResume2Id;
}
static get initialStateResumeId() {
return initialStateResumeId;
static get demoStateResume3Id() {
return demoStateResume3Id;
}
static get initialStateResume1Id() {
return initialStateResume1Id;
}
static get initialStateResume2Id() {
return initialStateResume2Id;
}
static get user1() {
@ -63,6 +77,10 @@ class Database {
return user2;
}
static get user3() {
return user3;
}
static get defaultDelayInMilliseconds() {
return defaultDelayInMilliseconds;
}

View File

@ -101,9 +101,9 @@ class Database {
initializeData() {
const resumes = {};
const date = new Date('December 15, 2020 11:20:25');
const demoStateResume1 = readFile('../../../src/data/demoState.json');
const date = new Date('December 15, 2020 11:20:25');
demoStateResume1.updatedAt = date.valueOf();
date.setMonth(date.getMonth() - 2);
demoStateResume1.createdAt = date.valueOf();
@ -114,11 +114,24 @@ class Database {
demoStateResume2.user = DatabaseConstants.user2.uid;
resumes[DatabaseConstants.demoStateResume2Id] = demoStateResume2;
const initialStateResume = readFile('../../../src/data/initialState.json');
initialStateResume.updatedAt = date.valueOf();
initialStateResume.createdAt = date.valueOf();
initialStateResume.user = DatabaseConstants.user1.uid;
resumes[DatabaseConstants.initialStateResumeId] = initialStateResume;
const initialStateResume1 = readFile('../../../src/data/initialState.json');
initialStateResume1.updatedAt = date.valueOf();
initialStateResume1.createdAt = date.valueOf();
initialStateResume1.user = DatabaseConstants.user1.uid;
resumes[DatabaseConstants.initialStateResume1Id] = initialStateResume1;
const demoStateResume3 = readFile('../../../src/data/demoState.json');
demoStateResume3.updatedAt = date.valueOf();
date.setMonth(date.getMonth() - 2);
demoStateResume3.createdAt = date.valueOf();
demoStateResume3.user = DatabaseConstants.user3.uid;
resumes[DatabaseConstants.demoStateResume3Id] = demoStateResume3;
const initialStateResume2 = readFile('../../../src/data/initialState.json');
initialStateResume2.updatedAt = date.valueOf();
initialStateResume2.createdAt = date.valueOf();
initialStateResume2.user = DatabaseConstants.user3.uid;
resumes[DatabaseConstants.initialStateResume2Id] = initialStateResume2;
Object.keys(resumes).forEach((key) => {
const resume = resumes[key];
@ -131,6 +144,7 @@ class Database {
const users = {};
users[DatabaseConstants.user1.uid] = DatabaseConstants.user1;
users[DatabaseConstants.user2.uid] = DatabaseConstants.user2;
users[DatabaseConstants.user3.uid] = DatabaseConstants.user3;
this._data[DatabaseConstants.usersPath] = users;
}