Skip to content

Instantly share code, notes, and snippets.

@zeekrey
Created July 6, 2020 12:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeekrey/4ca1a768ec4cc5793c3f41c88ee66eb2 to your computer and use it in GitHub Desktop.
Save zeekrey/4ca1a768ec4cc5793c3f41c88ee66eb2 to your computer and use it in GitHub Desktop.
Just a simple Firebase cleanup script to delete a given user by eMail address before a cypress test runs.
// signup.spec.js
/// <reference types="cypress" />
const path = require('path')
describe('Testing the user system', () => {
let user
beforeEach(() => {
cy.visit('http://localhost:3000/login')
cy.fixture('user.json').then((u) => (user = u))
})
it('successfully signs up', () => {
console.log(user)
cy.exec('node ./cypress/cleanupFirebase.js', {
env: { email: user.email },
}).then((result) => {
console.log(result.code)
console.log(result.stderr)
console.log(result.stdout)
})
/**
* cy.exec(command, options)
* Actived login via eMail and enter a username, eMail address and password.
*/
cy.get('div').contains('Ich habe noch keinen Account').click()
cy.get('input[name=name]').type(user.name)
cy.get('input[name=email]').type(user.email)
cy.get('input[name=password]').type(user.password)
cy.get('form').submit()
/**
* The communication wizard should start.
* Select eMail.
*/
cy.url().should('include', '/welcome')
cy.get('div').contains('E-Mail').click()
/**
* The eMail input should be filled with the allready provided eMail address
*/
cy.get('input[name=email]').should('have.value', user.email)
cy.get('form').submit()
})
})
// cleanupFirebase.js
var admin = require('firebase-admin')
var app = admin.initializeApp()
if (!process.env.email) console.log('no email provided')
admin
.auth()
.getUserByEmail(process.env.email)
.then(function (userRecord) {
// See the UserRecord reference doc for the contents of userRecord.
// console.log('Successfully fetched user data:', userRecord.toJSON())
admin
.auth()
.deleteUser(userRecord.uid)
.then(function () {
console.log('Successfully deleted user')
process.exit(0)
})
.catch(function (error) {
console.log(error.code)
process.exit(1)
})
})
.catch(function (error) {
if (error.code === 'auth/user-not-found') process.exit(0)
console.log('Error fetching user data:', error)
process.exit(1)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment