Created
March 11, 2022 10:00
-
-
Save tomeustace/697a3ae4af7cbebedb72207ba5e729d0 to your computer and use it in GitHub Desktop.
Cypress commands for populating Firebase Auth and Firestore emulators with data during test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// support/commands.ts | |
import { getAuth, connectAuthEmulator, signInWithEmailAndPassword } from "firebase/auth"; | |
import firebase = require("firebase/compat"); | |
const firebaseConfig = {<your-config} | |
firebase.default.initializeApp(firebaseConfig); | |
const auth = getAuth(); | |
connectAuthEmulator(auth, "http://localhost:9099"); | |
const db = firebase.default.firestore(); | |
db.settings({ | |
ssl: false, | |
experimentalForceLongPolling: true, | |
merge: true | |
}); | |
db.useEmulator('127.0.0.1', 4001); | |
const authUrl = "http://localhost:9099/identitytoolkit.googleapis.com/v1/accounts:signUp?key=bob"; | |
Cypress.Commands.add("createAuthUser", async(displayName, email, password) => { | |
await createAuthUser(displayName, email, password); | |
}); | |
Cypress.Commands.add("createTestUser", async(authEmail, authPassword, uuid, email, displayName) => { | |
await createTestUser(authEmail, authPassword, uuid, email, displayName); | |
}); | |
/* | |
* create user in auth emulator | |
*/ | |
async function createAuthUser(displayName, email, password) { | |
await fetch(authUrl, { | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/json' | |
}, | |
body: JSON.stringify({ | |
displayName, email, password | |
}) | |
}) | |
.then((response) => response.json()) | |
.then((user) => { | |
console.log("created user", user); | |
}); | |
} | |
/* | |
* Adds user entry to users collection in firestore emulator | |
*/ | |
function createTestUser(authEmail, authPassword, uuid, email, displayName) { | |
return signInWithEmailAndPassword(auth, authEmail, authPassword).then((user) => { | |
const usrDoc = {uuid, email, displayName, emailVerified: true} | |
db.collection('users').doc(uuid).set(usrDoc); | |
}) | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
beforeEach(() => { | |
// create a user that can be used as main user for tests | |
cy.createAuthUser("bob", "bob@bob.com", "bobby1234"); | |
cy.createTestUser("bob@bob.com", "bobby1234", testUid, "teddy@bob.com", "teddy"); | |
cy.intercept('POST', 'https://identitytoolkit.googleapis.com/v1/**/*', () => { | |
console.log("intercepted."); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment