Comparing version 0.30.0 to 0.30.1
@@ -1,3 +0,3 @@ | ||
import { IMockAuth } from "./auth/types"; | ||
import { IMockAuth } from "./@types/auth-types"; | ||
export declare const auth: () => Promise<IMockAuth>; | ||
export declare const authApi: IMockAuth; |
@@ -1,9 +0,17 @@ | ||
import { IMockAuthConfig, User, IEmailUser } from "./types"; | ||
import { IMockAuthConfig } from "../@types/config-types"; | ||
import { User } from "@firebase/auth-types"; | ||
import { IEmailUser } from "../@types/auth-types"; | ||
export declare function clearAuthUsers(): void; | ||
export declare type Observer = (user: User | null) => any; | ||
export declare type IMockAdminApi = typeof authAdminApi; | ||
export declare const authAdminApi: { | ||
/** | ||
* Updates the Auth configuration | ||
* | ||
* @param config the new config parameters passed in | ||
*/ | ||
configureAuth(config: IMockAuthConfig): void; | ||
getValidEmailUsers(): IEmailUser[]; | ||
getAuthConfig(): IMockAuthConfig; | ||
addUserToAuth(u: import("@firebase/auth-types").User, p: string): void; | ||
addUserToAuth(u: User, p: string): void; | ||
updateEmailUser(email: string, updates: Partial<IEmailUser>): void; | ||
@@ -20,7 +28,7 @@ /** | ||
*/ | ||
setAnonymousUser(uid: string): import("./types").IMockAuth; | ||
setAnonymousUid(uid: string): import("../@types/auth-types").IMockAuth; | ||
/** | ||
* Gets a UID for an anonymous user; this UID will | ||
* be randomly generated unless it has been set | ||
* statically with the `setAnonymousUser()` method | ||
* statically with the `setAnonymousUid()` method | ||
*/ | ||
@@ -31,3 +39,3 @@ getAnonymousUid(): string; | ||
*/ | ||
getCurrentUser(): import("@firebase/auth-types").User; | ||
getCurrentUser(): User; | ||
/** | ||
@@ -39,3 +47,3 @@ * Set the current user to a new user and notify all | ||
*/ | ||
login(u: import("@firebase/auth-types").User): void; | ||
login(u: User): void; | ||
/** | ||
@@ -52,3 +60,3 @@ * Clear the current user and notify all observers of the | ||
*/ | ||
addAuthObserver(observer: (user: import("@firebase/auth-types").User) => any): void; | ||
addAuthObserver(observer: (user: User) => any): void; | ||
/** | ||
@@ -55,0 +63,0 @@ * Get a list of all the callback observers which have registered |
@@ -12,2 +12,6 @@ "use strict"; | ||
}; | ||
function clearAuthUsers() { | ||
authConfig.validEmailUsers = []; | ||
} | ||
exports.clearAuthUsers = clearAuthUsers; | ||
let ANONYMOUS_USER_ID; | ||
@@ -24,2 +28,7 @@ /** | ||
exports.authAdminApi = { | ||
/** | ||
* Updates the Auth configuration | ||
* | ||
* @param config the new config parameters passed in | ||
*/ | ||
configureAuth(config) { | ||
@@ -67,3 +76,3 @@ authConfig = Object.assign({}, authConfig, config); | ||
*/ | ||
setAnonymousUser(uid) { | ||
setAnonymousUid(uid) { | ||
ANONYMOUS_USER_ID = uid; | ||
@@ -75,3 +84,3 @@ return auth_1.authApi; | ||
* be randomly generated unless it has been set | ||
* statically with the `setAnonymousUser()` method | ||
* statically with the `setAnonymousUid()` method | ||
*/ | ||
@@ -78,0 +87,0 @@ getAnonymousUid() { |
@@ -1,2 +0,2 @@ | ||
import { FirebaseAuth } from "./types"; | ||
import { FirebaseAuth } from "../@types/auth-types"; | ||
import { Omit } from "common-types"; | ||
@@ -3,0 +3,0 @@ import { notImplemented } from "./notImplemented"; |
@@ -52,3 +52,3 @@ "use strict"; | ||
} | ||
if (!authMockHelpers_1.checkIfEmailIsValidFormat(email)) { | ||
if (!authMockHelpers_1.emailIsValidFormat(email)) { | ||
throw new FireMockError_1.FireMockError(`invalid email: ${email}`, "auth/invalid-email"); | ||
@@ -62,3 +62,3 @@ } | ||
} | ||
if (!authMockHelpers_1.validEmailUserPassword(email, found.password)) { | ||
if (!authMockHelpers_1.emailHasCorrectPassword(email, found.password)) { | ||
throw new FireMockError_1.FireMockError(`Invalid password for ${email}`, "auth/wrong-password"); | ||
@@ -93,11 +93,8 @@ } | ||
} | ||
if (authMockHelpers_1.checkIfEmailUserExists(email)) { | ||
if (authMockHelpers_1.emailExistsAsUserInAuth(email)) { | ||
throw new FireMockError_1.FireMockError(`"${email}" user already exists`, "auth/email-already-in-use"); | ||
} | ||
if (authMockHelpers_1.checkIfEmailIsValidFormat(email)) { | ||
throw new FireMockError_1.FireMockError(`"${email}" user already exists`, "auth/invalid-email"); | ||
if (!authMockHelpers_1.emailIsValidFormat(email)) { | ||
throw new FireMockError_1.FireMockError(`"${email}" is not a valid email format`, "auth/invalid-email"); | ||
} | ||
if (!authMockHelpers_1.validEmailUserPassword(email, password)) { | ||
throw new FireMockError_1.FireMockError(`invalid password for "${email}" user`, "firemock/denied"); | ||
} | ||
const partial = { | ||
@@ -121,3 +118,4 @@ user: { | ||
authAdmin_1.authAdminApi.login(u.user); | ||
return completeUserCredential_1.completeUserCredential(partial); | ||
console.log(u.user); | ||
return u; | ||
}, | ||
@@ -124,0 +122,0 @@ async confirmPasswordReset(code, newPassword) { |
import { User } from "@firebase/auth-types"; | ||
export declare function checkIfEmailUserExists(email: string): boolean; | ||
export declare function checkIfEmailIsValidFormat(email: string): boolean; | ||
export declare function validEmailUserPassword(email: string, password: string): boolean; | ||
export declare function emailExistsAsUserInAuth(email: string): boolean; | ||
export declare function emailIsValidFormat(email: string): boolean; | ||
export declare function emailHasCorrectPassword(email: string, password: string): boolean; | ||
export declare function emailVerified(email: string): boolean; | ||
@@ -6,0 +6,0 @@ export declare function userUid(email: string): string; |
@@ -5,16 +5,16 @@ "use strict"; | ||
const email_validator_1 = require("email-validator"); | ||
function checkIfEmailUserExists(email) { | ||
const emails = authAdmin_1.authAdminApi.getValidEmailUsers(); | ||
return emails.map(e => e.email).includes(email); | ||
function emailExistsAsUserInAuth(email) { | ||
const emails = authAdmin_1.authAdminApi.getValidEmailUsers().map(i => i.email); | ||
return emails.includes(email); | ||
} | ||
exports.checkIfEmailUserExists = checkIfEmailUserExists; | ||
function checkIfEmailIsValidFormat(email) { | ||
exports.emailExistsAsUserInAuth = emailExistsAsUserInAuth; | ||
function emailIsValidFormat(email) { | ||
return email_validator_1.validate(email); | ||
} | ||
exports.checkIfEmailIsValidFormat = checkIfEmailIsValidFormat; | ||
function validEmailUserPassword(email, password) { | ||
exports.emailIsValidFormat = emailIsValidFormat; | ||
function emailHasCorrectPassword(email, password) { | ||
const config = authAdmin_1.authAdminApi.getValidEmailUsers().find(i => i.email === email); | ||
return config ? config.password === password : false; | ||
} | ||
exports.validEmailUserPassword = validEmailUserPassword; | ||
exports.emailHasCorrectPassword = emailHasCorrectPassword; | ||
function emailVerified(email) { | ||
@@ -27,3 +27,3 @@ const config = authAdmin_1.authAdminApi.getValidEmailUsers().find(i => i.email === email); | ||
const config = authAdmin_1.authAdminApi.getValidEmailUsers().find(i => i.email === email); | ||
return config.uid || createUid(); | ||
return config ? config.uid || createUid() : createUid(); | ||
} | ||
@@ -30,0 +30,0 @@ exports.userUid = userUid; |
@@ -1,2 +0,2 @@ | ||
import { IPartialUserCredential, UserCredential } from "./types"; | ||
import { IPartialUserCredential, UserCredential } from "../@types/auth-types"; | ||
/** | ||
@@ -3,0 +3,0 @@ * takes a partial user auth and adds enough to make it officially |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const authAdmin_1 = require("./authAdmin"); | ||
const deepmerge_1 = __importDefault(require("deepmerge")); | ||
/** | ||
@@ -9,108 +13,109 @@ * takes a partial user auth and adds enough to make it officially | ||
function completeUserCredential(partial) { | ||
const combined = Object.assign({}, fakeUserCredential, partial); | ||
return combined; | ||
} | ||
exports.completeUserCredential = completeUserCredential; | ||
const fakeUserCredential = { | ||
user: { | ||
async delete() { | ||
return; | ||
}, | ||
emailVerified: false, | ||
async getIdTokenResult() { | ||
return { | ||
token: "abc", | ||
expirationTime: "format?", | ||
authTime: "format?", | ||
issuedAtTime: "format?", | ||
signInProvider: "fake", | ||
claims: { | ||
foobar: "abc" | ||
const fakeUserCredential = { | ||
user: { | ||
async delete() { | ||
return; | ||
}, | ||
emailVerified: false, | ||
async getIdTokenResult() { | ||
return { | ||
token: "abc", | ||
expirationTime: "format?", | ||
authTime: "format?", | ||
issuedAtTime: "format?", | ||
signInProvider: "fake", | ||
claims: { | ||
foobar: "abc" | ||
} | ||
}; | ||
}, | ||
async getIdToken() { | ||
return "abc"; | ||
}, | ||
async linkAndRetrieveDataWithCredential(credential) { | ||
return completeUserCredential({}); | ||
}, | ||
async linkWithCredential(credential) { | ||
return completeUserCredential({}); | ||
}, | ||
async linkWithPhoneNumber(phoneNUmber, applicationVerificer) { | ||
return fakeApplicationVerifier; | ||
}, | ||
async linkWithPopup(provider) { | ||
return completeUserCredential({}); | ||
}, | ||
async linkWithRedirect(provider) { | ||
return; | ||
}, | ||
async reauthenticateAndRetrieveDataWithCredential(credential) { | ||
return completeUserCredential({}); | ||
}, | ||
// async reauthenticateWithCredential(credential: AuthCredential) { | ||
// return; | ||
// }, | ||
async reauthenticateWithCredential(credential) { | ||
return completeUserCredential({}); | ||
}, | ||
async reauthenticateWithPhoneNumber(phoneNumber, applicationVerifier) { | ||
return fakeApplicationVerifier; | ||
}, | ||
async reauthenticateWithPopup(provider) { | ||
return completeUserCredential({}); | ||
}, | ||
async reauthenticateWithRedirect(provider) { | ||
return; | ||
}, | ||
async reload() { | ||
return; | ||
}, | ||
async sendEmailVerification(actionCodeSettings) { | ||
return; | ||
}, | ||
toJSON() { | ||
return {}; | ||
}, | ||
async unlink(provider) { | ||
return completeUserCredential({}).user; | ||
}, | ||
async updateEmail(newEmail) { | ||
return; | ||
}, | ||
updatePassword: async (password) => { | ||
if (partial.user.email) { | ||
authAdmin_1.authAdminApi.updateEmailUser(partial.user.email, { password }); | ||
} | ||
}; | ||
}, | ||
async updatePhoneNumber(phoneCredential) { | ||
return; | ||
}, | ||
async updateProfile(profile) { | ||
return; | ||
}, | ||
displayName: "", | ||
email: "", | ||
isAnonymous: true, | ||
metadata: {}, | ||
phoneNumber: "", | ||
photoURL: "", | ||
providerData: [], | ||
providerId: "", | ||
refreshToken: "", | ||
uid: authAdmin_1.authAdminApi.getAnonymousUid() | ||
}, | ||
async getIdToken() { | ||
return "abc"; | ||
additionalUserInfo: { | ||
isNewUser: false, | ||
profile: "", | ||
providerId: "", | ||
username: "fake" | ||
}, | ||
async linkAndRetrieveDataWithCredential(credential) { | ||
return completeUserCredential({}); | ||
}, | ||
async linkWithCredential(credential) { | ||
return completeUserCredential({}); | ||
}, | ||
async linkWithPhoneNumber(phoneNUmber, applicationVerificer) { | ||
return fakeApplicationVerifier; | ||
}, | ||
async linkWithPopup(provider) { | ||
return completeUserCredential({}); | ||
}, | ||
async linkWithRedirect(provider) { | ||
return; | ||
}, | ||
async reauthenticateAndRetrieveDataWithCredential(credential) { | ||
return completeUserCredential({}); | ||
}, | ||
// async reauthenticateWithCredential(credential: AuthCredential) { | ||
// return; | ||
// }, | ||
async reauthenticateWithCredential(credential) { | ||
return completeUserCredential({}); | ||
}, | ||
async reauthenticateWithPhoneNumber(phoneNumber, applicationVerifier) { | ||
return fakeApplicationVerifier; | ||
}, | ||
async reauthenticateWithPopup(provider) { | ||
return completeUserCredential({}); | ||
}, | ||
async reauthenticateWithRedirect(provider) { | ||
return; | ||
}, | ||
async reload() { | ||
return; | ||
}, | ||
async sendEmailVerification(actionCodeSettings) { | ||
return; | ||
}, | ||
toJSON() { | ||
return {}; | ||
}, | ||
async unlink(provider) { | ||
return completeUserCredential({}).user; | ||
}, | ||
async updateEmail(newEmail) { | ||
return; | ||
}, | ||
async updatePassword(newPassword) { | ||
return; | ||
}, | ||
async updatePhoneNumber(phoneCredential) { | ||
return; | ||
}, | ||
async updateProfile(profile) { | ||
return; | ||
}, | ||
displayName: "", | ||
email: "", | ||
isAnonymous: true, | ||
metadata: {}, | ||
phoneNumber: "", | ||
photoURL: "", | ||
providerData: [], | ||
providerId: "", | ||
refreshToken: "", | ||
uid: authAdmin_1.authAdminApi.getAnonymousUid() | ||
}, | ||
additionalUserInfo: { | ||
isNewUser: false, | ||
profile: "", | ||
providerId: "", | ||
username: "fake" | ||
}, | ||
operationType: "", | ||
credential: { | ||
signInMethod: "fake", | ||
providerId: "fake", | ||
toJSON: () => "" // added recently | ||
} | ||
}; | ||
operationType: "", | ||
credential: { | ||
signInMethod: "fake", | ||
providerId: "fake", | ||
toJSON: () => "" // added recently | ||
} | ||
}; | ||
return deepmerge_1.default(fakeUserCredential, partial); | ||
} | ||
exports.completeUserCredential = completeUserCredential; | ||
const fakeApplicationVerifier = { | ||
@@ -117,0 +122,0 @@ async confirm(verificationCode) { |
export * from "./authAdmin"; | ||
export * from "./completeUserCredential"; | ||
export * from "./notImplemented"; | ||
export * from "./types"; | ||
export * from "../@types/auth-types"; |
@@ -1,2 +0,2 @@ | ||
import { FirebaseAuth } from "./types"; | ||
import { FirebaseAuth } from "../@types/auth-types"; | ||
export declare const notImplemented: Partial<FirebaseAuth>; |
import { IDictionary } from "common-types"; | ||
import { IListener } from "./query"; | ||
import { DataSnapshot, EventType } from "@firebase/database-types"; | ||
import { IFirebaseEventHandler } from "./types"; | ||
import { IFirebaseEventHandler } from "./@types/db-types"; | ||
export declare type FirebaseDatabase = import("@firebase/database-types").FirebaseDatabase; | ||
@@ -6,0 +6,0 @@ export declare let db: IDictionary; |
@@ -16,2 +16,3 @@ "use strict"; | ||
const auth_1 = require("./auth"); | ||
const deepmerge_1 = __importDefault(require("deepmerge")); | ||
exports.db = []; | ||
@@ -46,3 +47,3 @@ let _listeners = []; | ||
function updateDatabase(state) { | ||
exports.db = Object.assign({}, exports.db, state); | ||
exports.db = deepmerge_1.default(exports.db, state); | ||
} | ||
@@ -102,3 +103,4 @@ exports.updateDatabase = updateDatabase; | ||
if (typeof value === "object" && | ||
Object.keys(value).every(k => (oldValue ? oldValue[k] : null) === value[k])) { | ||
Object.keys(value).every(k => (oldValue ? oldValue[k] : null) === | ||
value[k])) { | ||
changed = false; | ||
@@ -177,3 +179,3 @@ } | ||
const justKey = (obj) => (obj ? Object.keys(obj)[0] : null); | ||
const justValue = (obj) => (justKey(obj) ? obj[justKey(obj)] : null); | ||
const justValue = (obj) => justKey(obj) ? obj[justKey(obj)] : null; | ||
getListeners().forEach(l => { | ||
@@ -298,3 +300,5 @@ const eventPathsUnderListener = eventPaths.filter(e => e.includes(l.path)); | ||
.filter(l => l.context === context); | ||
_listeners = _listeners.filter(l => l.context !== context || l.callback !== callback || l.eventType !== eventType); | ||
_listeners = _listeners.filter(l => l.context !== context || | ||
l.callback !== callback || | ||
l.eventType !== eventType); | ||
return cancelCallback(removed); | ||
@@ -332,3 +336,5 @@ } | ||
function listenerCount(type) { | ||
return type ? _listeners.filter(l => l.eventType === type).length : _listeners.length; | ||
return type | ||
? _listeners.filter(l => l.eventType === type).length | ||
: _listeners.length; | ||
} | ||
@@ -368,5 +374,14 @@ exports.listenerCount = listenerCount; | ||
function getListeners(lookFor) { | ||
const childEvents = ["child_added", "child_changed", "child_removed", "child_moved"]; | ||
const childEvents = [ | ||
"child_added", | ||
"child_changed", | ||
"child_removed", | ||
"child_moved" | ||
]; | ||
const allEvents = childEvents.concat(["value"]); | ||
const events = !lookFor ? allEvents : lookFor === "child" ? childEvents : lookFor; | ||
const events = !lookFor | ||
? allEvents | ||
: lookFor === "child" | ||
? childEvents | ||
: lookFor; | ||
return _listeners.filter(l => events.includes(l.eventType)); | ||
@@ -417,3 +432,5 @@ } | ||
// root set | ||
e.callback(new index_1.SnapShot(e.listenerPath, e.value === null || e.value === undefined ? undefined : { [e.key]: e.value })); | ||
e.callback(new index_1.SnapShot(e.listenerPath, e.value === null || e.value === undefined | ||
? undefined | ||
: { [e.key]: e.value })); | ||
} | ||
@@ -420,0 +437,0 @@ else { |
@@ -13,3 +13,3 @@ export * from "./mock"; | ||
export * from "./auth/index"; | ||
export * from "./types"; | ||
export * from "./@types/db-types"; | ||
export { GenericEventHandler, HandleValueEvent, HandleChangeEvent, HandleMoveEvent, HandleNewEvent, HandleRemoveEvent } from "./query"; |
/// <reference types="faker" /> | ||
import { IDictionary } from "common-types"; | ||
import { Schema, Reference, Deployment } from "./index"; | ||
import { Schema, Reference, Deployment, IMockSetup } from "./index"; | ||
import { DelayType } from "./util"; | ||
import { MockHelper } from "./MockHelper"; | ||
import { IMockAuthConfig, IMockSetup, IMockConfigOptions } from "./auth/types"; | ||
import { SchemaCallback } from "./types"; | ||
import { SchemaCallback } from "./@types/db-types"; | ||
import { IMockConfigOptions, IMockAuthConfig } from "./@types/config-types"; | ||
export declare let faker: Faker.FakerStatic; | ||
@@ -38,3 +38,3 @@ export declare class Mock { | ||
/** | ||
* Update the mock DB with a raw JS object/hash | ||
* Update (non-desctructively) the mock DB with a raw JS object/hash | ||
*/ | ||
@@ -41,0 +41,0 @@ updateDB(state: IDictionary): void; |
@@ -37,2 +37,3 @@ "use strict"; | ||
database_1.clearDatabase(); | ||
authAdmin_1.clearAuthUsers(); | ||
if (dataOrMock && typeof dataOrMock === "object") { | ||
@@ -62,6 +63,8 @@ this.updateDB(dataOrMock); | ||
allowEmailLinks: false, | ||
allowPhoneLogins: false | ||
allowPhoneLogins: false, | ||
validEmailUsers: [] | ||
}; | ||
const defaultDbConfig = {}; | ||
const obj = new Mock(options.db || defaultDbConfig, options.auth || defaultAuthConfig); | ||
const obj = new Mock(options.db || defaultDbConfig, options.auth | ||
? Object.assign({}, defaultAuthConfig, options.auth) : defaultAuthConfig); | ||
await obj.importFakerLibrary(); | ||
@@ -77,3 +80,3 @@ return obj; | ||
/** | ||
* Update the mock DB with a raw JS object/hash | ||
* Update (non-desctructively) the mock DB with a raw JS object/hash | ||
*/ | ||
@@ -80,0 +83,0 @@ updateDB(state) { |
@@ -1,3 +0,3 @@ | ||
import { IMockAuth } from "./auth/types"; | ||
import { IMockAuth } from "./@types/auth-types"; | ||
export declare const auth: () => Promise<IMockAuth>; | ||
export declare const authApi: IMockAuth; |
@@ -1,9 +0,17 @@ | ||
import { IMockAuthConfig, User, IEmailUser } from "./types"; | ||
import { IMockAuthConfig } from "../@types/config-types"; | ||
import { User } from "@firebase/auth-types"; | ||
import { IEmailUser } from "../@types/auth-types"; | ||
export declare function clearAuthUsers(): void; | ||
export declare type Observer = (user: User | null) => any; | ||
export declare type IMockAdminApi = typeof authAdminApi; | ||
export declare const authAdminApi: { | ||
/** | ||
* Updates the Auth configuration | ||
* | ||
* @param config the new config parameters passed in | ||
*/ | ||
configureAuth(config: IMockAuthConfig): void; | ||
getValidEmailUsers(): IEmailUser[]; | ||
getAuthConfig(): IMockAuthConfig; | ||
addUserToAuth(u: import("@firebase/auth-types").User, p: string): void; | ||
addUserToAuth(u: User, p: string): void; | ||
updateEmailUser(email: string, updates: Partial<IEmailUser>): void; | ||
@@ -20,7 +28,7 @@ /** | ||
*/ | ||
setAnonymousUser(uid: string): import("./types").IMockAuth; | ||
setAnonymousUid(uid: string): import("../@types/auth-types").IMockAuth; | ||
/** | ||
* Gets a UID for an anonymous user; this UID will | ||
* be randomly generated unless it has been set | ||
* statically with the `setAnonymousUser()` method | ||
* statically with the `setAnonymousUid()` method | ||
*/ | ||
@@ -31,3 +39,3 @@ getAnonymousUid(): string; | ||
*/ | ||
getCurrentUser(): import("@firebase/auth-types").User; | ||
getCurrentUser(): User; | ||
/** | ||
@@ -39,3 +47,3 @@ * Set the current user to a new user and notify all | ||
*/ | ||
login(u: import("@firebase/auth-types").User): void; | ||
login(u: User): void; | ||
/** | ||
@@ -52,3 +60,3 @@ * Clear the current user and notify all observers of the | ||
*/ | ||
addAuthObserver(observer: (user: import("@firebase/auth-types").User) => any): void; | ||
addAuthObserver(observer: (user: User) => any): void; | ||
/** | ||
@@ -55,0 +63,0 @@ * Get a list of all the callback observers which have registered |
@@ -10,2 +10,5 @@ import { authApi } from "../auth"; | ||
}; | ||
export function clearAuthUsers() { | ||
authConfig.validEmailUsers = []; | ||
} | ||
let ANONYMOUS_USER_ID; | ||
@@ -22,2 +25,7 @@ /** | ||
export const authAdminApi = { | ||
/** | ||
* Updates the Auth configuration | ||
* | ||
* @param config the new config parameters passed in | ||
*/ | ||
configureAuth(config) { | ||
@@ -65,3 +73,3 @@ authConfig = Object.assign({}, authConfig, config); | ||
*/ | ||
setAnonymousUser(uid) { | ||
setAnonymousUid(uid) { | ||
ANONYMOUS_USER_ID = uid; | ||
@@ -73,3 +81,3 @@ return authApi; | ||
* be randomly generated unless it has been set | ||
* statically with the `setAnonymousUser()` method | ||
* statically with the `setAnonymousUid()` method | ||
*/ | ||
@@ -76,0 +84,0 @@ getAnonymousUid() { |
@@ -1,2 +0,2 @@ | ||
import { FirebaseAuth } from "./types"; | ||
import { FirebaseAuth } from "../@types/auth-types"; | ||
import { Omit } from "common-types"; | ||
@@ -3,0 +3,0 @@ import { notImplemented } from "./notImplemented"; |
@@ -7,3 +7,3 @@ import { networkDelay } from "../util"; | ||
import { FireMockError } from "../errors/FireMockError"; | ||
import { checkIfEmailUserExists, validEmailUserPassword, emailVerified, userUid, emailValidationAllowed, checkIfEmailIsValidFormat } from "./authMockHelpers"; | ||
import { emailExistsAsUserInAuth, emailHasCorrectPassword, emailVerified, userUid, emailValidationAllowed, emailIsValidFormat } from "./authMockHelpers"; | ||
export const implemented = { | ||
@@ -51,3 +51,3 @@ app: { | ||
} | ||
if (!checkIfEmailIsValidFormat(email)) { | ||
if (!emailIsValidFormat(email)) { | ||
throw new FireMockError(`invalid email: ${email}`, "auth/invalid-email"); | ||
@@ -61,3 +61,3 @@ } | ||
} | ||
if (!validEmailUserPassword(email, found.password)) { | ||
if (!emailHasCorrectPassword(email, found.password)) { | ||
throw new FireMockError(`Invalid password for ${email}`, "auth/wrong-password"); | ||
@@ -92,11 +92,8 @@ } | ||
} | ||
if (checkIfEmailUserExists(email)) { | ||
if (emailExistsAsUserInAuth(email)) { | ||
throw new FireMockError(`"${email}" user already exists`, "auth/email-already-in-use"); | ||
} | ||
if (checkIfEmailIsValidFormat(email)) { | ||
throw new FireMockError(`"${email}" user already exists`, "auth/invalid-email"); | ||
if (!emailIsValidFormat(email)) { | ||
throw new FireMockError(`"${email}" is not a valid email format`, "auth/invalid-email"); | ||
} | ||
if (!validEmailUserPassword(email, password)) { | ||
throw new FireMockError(`invalid password for "${email}" user`, "firemock/denied"); | ||
} | ||
const partial = { | ||
@@ -120,3 +117,4 @@ user: { | ||
authAdminApi.login(u.user); | ||
return completeUserCredential(partial); | ||
console.log(u.user); | ||
return u; | ||
}, | ||
@@ -123,0 +121,0 @@ async confirmPasswordReset(code, newPassword) { |
import { User } from "@firebase/auth-types"; | ||
export declare function checkIfEmailUserExists(email: string): boolean; | ||
export declare function checkIfEmailIsValidFormat(email: string): boolean; | ||
export declare function validEmailUserPassword(email: string, password: string): boolean; | ||
export declare function emailExistsAsUserInAuth(email: string): boolean; | ||
export declare function emailIsValidFormat(email: string): boolean; | ||
export declare function emailHasCorrectPassword(email: string, password: string): boolean; | ||
export declare function emailVerified(email: string): boolean; | ||
@@ -6,0 +6,0 @@ export declare function userUid(email: string): string; |
import { authAdminApi } from "./authAdmin"; | ||
import { validate } from "email-validator"; | ||
export function checkIfEmailUserExists(email) { | ||
const emails = authAdminApi.getValidEmailUsers(); | ||
return emails.map(e => e.email).includes(email); | ||
export function emailExistsAsUserInAuth(email) { | ||
const emails = authAdminApi.getValidEmailUsers().map(i => i.email); | ||
return emails.includes(email); | ||
} | ||
export function checkIfEmailIsValidFormat(email) { | ||
export function emailIsValidFormat(email) { | ||
return validate(email); | ||
} | ||
export function validEmailUserPassword(email, password) { | ||
export function emailHasCorrectPassword(email, password) { | ||
const config = authAdminApi.getValidEmailUsers().find(i => i.email === email); | ||
@@ -20,3 +20,3 @@ return config ? config.password === password : false; | ||
const config = authAdminApi.getValidEmailUsers().find(i => i.email === email); | ||
return config.uid || createUid(); | ||
return config ? config.uid || createUid() : createUid(); | ||
} | ||
@@ -23,0 +23,0 @@ export function createUid() { |
@@ -1,2 +0,2 @@ | ||
import { IPartialUserCredential, UserCredential } from "./types"; | ||
import { IPartialUserCredential, UserCredential } from "../@types/auth-types"; | ||
/** | ||
@@ -3,0 +3,0 @@ * takes a partial user auth and adds enough to make it officially |
import { authAdminApi } from "./authAdmin"; | ||
import merge from "deepmerge"; | ||
/** | ||
@@ -7,107 +8,108 @@ * takes a partial user auth and adds enough to make it officially | ||
export function completeUserCredential(partial) { | ||
const combined = Object.assign({}, fakeUserCredential, partial); | ||
return combined; | ||
} | ||
const fakeUserCredential = { | ||
user: { | ||
async delete() { | ||
return; | ||
}, | ||
emailVerified: false, | ||
async getIdTokenResult() { | ||
return { | ||
token: "abc", | ||
expirationTime: "format?", | ||
authTime: "format?", | ||
issuedAtTime: "format?", | ||
signInProvider: "fake", | ||
claims: { | ||
foobar: "abc" | ||
const fakeUserCredential = { | ||
user: { | ||
async delete() { | ||
return; | ||
}, | ||
emailVerified: false, | ||
async getIdTokenResult() { | ||
return { | ||
token: "abc", | ||
expirationTime: "format?", | ||
authTime: "format?", | ||
issuedAtTime: "format?", | ||
signInProvider: "fake", | ||
claims: { | ||
foobar: "abc" | ||
} | ||
}; | ||
}, | ||
async getIdToken() { | ||
return "abc"; | ||
}, | ||
async linkAndRetrieveDataWithCredential(credential) { | ||
return completeUserCredential({}); | ||
}, | ||
async linkWithCredential(credential) { | ||
return completeUserCredential({}); | ||
}, | ||
async linkWithPhoneNumber(phoneNUmber, applicationVerificer) { | ||
return fakeApplicationVerifier; | ||
}, | ||
async linkWithPopup(provider) { | ||
return completeUserCredential({}); | ||
}, | ||
async linkWithRedirect(provider) { | ||
return; | ||
}, | ||
async reauthenticateAndRetrieveDataWithCredential(credential) { | ||
return completeUserCredential({}); | ||
}, | ||
// async reauthenticateWithCredential(credential: AuthCredential) { | ||
// return; | ||
// }, | ||
async reauthenticateWithCredential(credential) { | ||
return completeUserCredential({}); | ||
}, | ||
async reauthenticateWithPhoneNumber(phoneNumber, applicationVerifier) { | ||
return fakeApplicationVerifier; | ||
}, | ||
async reauthenticateWithPopup(provider) { | ||
return completeUserCredential({}); | ||
}, | ||
async reauthenticateWithRedirect(provider) { | ||
return; | ||
}, | ||
async reload() { | ||
return; | ||
}, | ||
async sendEmailVerification(actionCodeSettings) { | ||
return; | ||
}, | ||
toJSON() { | ||
return {}; | ||
}, | ||
async unlink(provider) { | ||
return completeUserCredential({}).user; | ||
}, | ||
async updateEmail(newEmail) { | ||
return; | ||
}, | ||
updatePassword: async (password) => { | ||
if (partial.user.email) { | ||
authAdminApi.updateEmailUser(partial.user.email, { password }); | ||
} | ||
}; | ||
}, | ||
async updatePhoneNumber(phoneCredential) { | ||
return; | ||
}, | ||
async updateProfile(profile) { | ||
return; | ||
}, | ||
displayName: "", | ||
email: "", | ||
isAnonymous: true, | ||
metadata: {}, | ||
phoneNumber: "", | ||
photoURL: "", | ||
providerData: [], | ||
providerId: "", | ||
refreshToken: "", | ||
uid: authAdminApi.getAnonymousUid() | ||
}, | ||
async getIdToken() { | ||
return "abc"; | ||
additionalUserInfo: { | ||
isNewUser: false, | ||
profile: "", | ||
providerId: "", | ||
username: "fake" | ||
}, | ||
async linkAndRetrieveDataWithCredential(credential) { | ||
return completeUserCredential({}); | ||
}, | ||
async linkWithCredential(credential) { | ||
return completeUserCredential({}); | ||
}, | ||
async linkWithPhoneNumber(phoneNUmber, applicationVerificer) { | ||
return fakeApplicationVerifier; | ||
}, | ||
async linkWithPopup(provider) { | ||
return completeUserCredential({}); | ||
}, | ||
async linkWithRedirect(provider) { | ||
return; | ||
}, | ||
async reauthenticateAndRetrieveDataWithCredential(credential) { | ||
return completeUserCredential({}); | ||
}, | ||
// async reauthenticateWithCredential(credential: AuthCredential) { | ||
// return; | ||
// }, | ||
async reauthenticateWithCredential(credential) { | ||
return completeUserCredential({}); | ||
}, | ||
async reauthenticateWithPhoneNumber(phoneNumber, applicationVerifier) { | ||
return fakeApplicationVerifier; | ||
}, | ||
async reauthenticateWithPopup(provider) { | ||
return completeUserCredential({}); | ||
}, | ||
async reauthenticateWithRedirect(provider) { | ||
return; | ||
}, | ||
async reload() { | ||
return; | ||
}, | ||
async sendEmailVerification(actionCodeSettings) { | ||
return; | ||
}, | ||
toJSON() { | ||
return {}; | ||
}, | ||
async unlink(provider) { | ||
return completeUserCredential({}).user; | ||
}, | ||
async updateEmail(newEmail) { | ||
return; | ||
}, | ||
async updatePassword(newPassword) { | ||
return; | ||
}, | ||
async updatePhoneNumber(phoneCredential) { | ||
return; | ||
}, | ||
async updateProfile(profile) { | ||
return; | ||
}, | ||
displayName: "", | ||
email: "", | ||
isAnonymous: true, | ||
metadata: {}, | ||
phoneNumber: "", | ||
photoURL: "", | ||
providerData: [], | ||
providerId: "", | ||
refreshToken: "", | ||
uid: authAdminApi.getAnonymousUid() | ||
}, | ||
additionalUserInfo: { | ||
isNewUser: false, | ||
profile: "", | ||
providerId: "", | ||
username: "fake" | ||
}, | ||
operationType: "", | ||
credential: { | ||
signInMethod: "fake", | ||
providerId: "fake", | ||
toJSON: () => "" // added recently | ||
} | ||
}; | ||
operationType: "", | ||
credential: { | ||
signInMethod: "fake", | ||
providerId: "fake", | ||
toJSON: () => "" // added recently | ||
} | ||
}; | ||
return merge(fakeUserCredential, partial); | ||
} | ||
const fakeApplicationVerifier = { | ||
@@ -114,0 +116,0 @@ async confirm(verificationCode) { |
export * from "./authAdmin"; | ||
export * from "./completeUserCredential"; | ||
export * from "./notImplemented"; | ||
export * from "./types"; | ||
export * from "../@types/auth-types"; |
@@ -1,2 +0,2 @@ | ||
import { FirebaseAuth } from "./types"; | ||
import { FirebaseAuth } from "../@types/auth-types"; | ||
export declare const notImplemented: Partial<FirebaseAuth>; |
import { IDictionary } from "common-types"; | ||
import { IListener } from "./query"; | ||
import { DataSnapshot, EventType } from "@firebase/database-types"; | ||
import { IFirebaseEventHandler } from "./types"; | ||
import { IFirebaseEventHandler } from "./@types/db-types"; | ||
export declare type FirebaseDatabase = import("@firebase/database-types").FirebaseDatabase; | ||
@@ -6,0 +6,0 @@ export declare let db: IDictionary; |
@@ -11,2 +11,3 @@ // tslint:disable:no-implicit-dependencies | ||
import { auth as mockedAuth } from "./auth"; | ||
import deepmerge from "deepmerge"; | ||
export let db = []; | ||
@@ -37,3 +38,3 @@ let _listeners = []; | ||
export function updateDatabase(state) { | ||
db = Object.assign({}, db, state); | ||
db = deepmerge(db, state); | ||
} | ||
@@ -89,3 +90,4 @@ export async function auth() { | ||
if (typeof value === "object" && | ||
Object.keys(value).every(k => (oldValue ? oldValue[k] : null) === value[k])) { | ||
Object.keys(value).every(k => (oldValue ? oldValue[k] : null) === | ||
value[k])) { | ||
changed = false; | ||
@@ -162,3 +164,3 @@ } | ||
const justKey = (obj) => (obj ? Object.keys(obj)[0] : null); | ||
const justValue = (obj) => (justKey(obj) ? obj[justKey(obj)] : null); | ||
const justValue = (obj) => justKey(obj) ? obj[justKey(obj)] : null; | ||
getListeners().forEach(l => { | ||
@@ -280,3 +282,5 @@ const eventPathsUnderListener = eventPaths.filter(e => e.includes(l.path)); | ||
.filter(l => l.context === context); | ||
_listeners = _listeners.filter(l => l.context !== context || l.callback !== callback || l.eventType !== eventType); | ||
_listeners = _listeners.filter(l => l.context !== context || | ||
l.callback !== callback || | ||
l.eventType !== eventType); | ||
return cancelCallback(removed); | ||
@@ -312,3 +316,5 @@ } | ||
export function listenerCount(type) { | ||
return type ? _listeners.filter(l => l.eventType === type).length : _listeners.length; | ||
return type | ||
? _listeners.filter(l => l.eventType === type).length | ||
: _listeners.length; | ||
} | ||
@@ -346,5 +352,14 @@ /** | ||
export function getListeners(lookFor) { | ||
const childEvents = ["child_added", "child_changed", "child_removed", "child_moved"]; | ||
const childEvents = [ | ||
"child_added", | ||
"child_changed", | ||
"child_removed", | ||
"child_moved" | ||
]; | ||
const allEvents = childEvents.concat(["value"]); | ||
const events = !lookFor ? allEvents : lookFor === "child" ? childEvents : lookFor; | ||
const events = !lookFor | ||
? allEvents | ||
: lookFor === "child" | ||
? childEvents | ||
: lookFor; | ||
return _listeners.filter(l => events.includes(l.eventType)); | ||
@@ -394,3 +409,5 @@ } | ||
// root set | ||
e.callback(new SnapShot(e.listenerPath, e.value === null || e.value === undefined ? undefined : { [e.key]: e.value })); | ||
e.callback(new SnapShot(e.listenerPath, e.value === null || e.value === undefined | ||
? undefined | ||
: { [e.key]: e.value })); | ||
} | ||
@@ -397,0 +414,0 @@ else { |
@@ -13,3 +13,3 @@ export * from "./mock"; | ||
export * from "./auth/index"; | ||
export * from "./types"; | ||
export * from "./@types/db-types"; | ||
export { GenericEventHandler, HandleValueEvent, HandleChangeEvent, HandleMoveEvent, HandleNewEvent, HandleRemoveEvent } from "./query"; |
/// <reference types="faker" /> | ||
import { IDictionary } from "common-types"; | ||
import { Schema, Reference, Deployment } from "./index"; | ||
import { Schema, Reference, Deployment, IMockSetup } from "./index"; | ||
import { DelayType } from "./util"; | ||
import { MockHelper } from "./MockHelper"; | ||
import { IMockAuthConfig, IMockSetup, IMockConfigOptions } from "./auth/types"; | ||
import { SchemaCallback } from "./types"; | ||
import { SchemaCallback } from "./@types/db-types"; | ||
import { IMockConfigOptions, IMockAuthConfig } from "./@types/config-types"; | ||
export declare let faker: Faker.FakerStatic; | ||
@@ -38,3 +38,3 @@ export declare class Mock { | ||
/** | ||
* Update the mock DB with a raw JS object/hash | ||
* Update (non-desctructively) the mock DB with a raw JS object/hash | ||
*/ | ||
@@ -41,0 +41,0 @@ updateDB(state: IDictionary): void; |
@@ -6,3 +6,3 @@ import { Queue, Schema, Reference, Deployment } from "./index"; | ||
import { auth as fireAuth } from "./auth"; | ||
import { authAdminApi } from "./auth/authAdmin"; | ||
import { authAdminApi, clearAuthUsers } from "./auth/authAdmin"; | ||
import { FireMockError } from "./errors/FireMockError"; | ||
@@ -30,2 +30,3 @@ export let faker; | ||
clearDatabase(); | ||
clearAuthUsers(); | ||
if (dataOrMock && typeof dataOrMock === "object") { | ||
@@ -55,6 +56,8 @@ this.updateDB(dataOrMock); | ||
allowEmailLinks: false, | ||
allowPhoneLogins: false | ||
allowPhoneLogins: false, | ||
validEmailUsers: [] | ||
}; | ||
const defaultDbConfig = {}; | ||
const obj = new Mock(options.db || defaultDbConfig, options.auth || defaultAuthConfig); | ||
const obj = new Mock(options.db || defaultDbConfig, options.auth | ||
? Object.assign({}, defaultAuthConfig, options.auth) : defaultAuthConfig); | ||
await obj.importFakerLibrary(); | ||
@@ -70,3 +73,3 @@ return obj; | ||
/** | ||
* Update the mock DB with a raw JS object/hash | ||
* Update (non-desctructively) the mock DB with a raw JS object/hash | ||
*/ | ||
@@ -73,0 +76,0 @@ updateDB(state) { |
{ | ||
"name": "firemock", | ||
"version": "0.30.0", | ||
"version": "0.30.1", | ||
"description": "firemock", | ||
@@ -37,6 +37,6 @@ "license": "MIT", | ||
"@firebase/auth-types": "^0.7.2", | ||
"@firebase/database-types": "^0.4.2", | ||
"@firebase/database-types": "^0.4.3", | ||
"@firebase/util": "^0.2.25", | ||
"@types/faker": "^4.1.5", | ||
"common-types": "^1.11.15", | ||
"common-types": "^1.11.23", | ||
"deepmerge": "^4.0.0", | ||
@@ -43,0 +43,0 @@ "email-validator": "^2.0.4", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
374851
159
6618
Updatedcommon-types@^1.11.23