Socket
Book a DemoInstallSign in
Socket

chai-fireproof

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

chai-fireproof

Firebase and Fireproof support for the Chai assertion library.

0.2.0
latest
npmnpm
Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

chai-fireproof

Build Status

Chai assertions and helpers for Firebase and Fireproof.

Requirements

You need to be able to generate Fireproof references.

Usage

Object assertions

First load the plugin like any other Chai plugin: gulp.task('test:setup', 'Set up tests.', ['build'], function() {

var Firebase = require('firebase'), chai = require('chai');

require('./dist/chai-fireproof'); global.chai = chai; global.expect = chai.expect;

if (!process.env.FIREBASE_TEST_URL || !process.env.FIREBASE_TEST_SECRET) {

gutil.log('Please set FIREBASE_TEST_URL and FIREBASE_TEST_SECRET.');
process.exit(1);

}

global.root = new Fireproof(new Firebase(process.env.FIREBASE_TEST_URL)); global.authToken = process.env.FIREBASE_TEST_SECRET;

});

chai.use(require('chai-fireproof'));

Now you can create assertions on Fireproof objects like anything else. Note that these return promises that you'll have to pass back to your test framework or handle yourself.

An example with Mocha:

describe('My Firebase', function() {
  
  var root = new Fireproof(new Firebase('https://metropolis.firebaseio.com'));

  it('should have some data in there already', function() {
    return expect(root.child('robots')).to.exist;
  });

  it('should have some users in there', function() {

    return expect(root.child('citizens')).to.deep.equal({
      fred: {
        name: 'Freder Frederson',
        hometown: 'Metropolis',
        assignment: 'Utopia'
      },
      maria: {
        name: 'Maria',
        hometown: 'Metropolis',
        assignment: 'Underworld'
      }
    });

  });

  it('should have the water level in there', function() {
    return expect(root.child('waterLevel')).to.be.lessThan(5);
  })

});

Security testing

chai-fireproof includes test assertions for validating that security rules work the way they're supposed to. Note that these assertions return promises that you'll have to pass back to your test framework or handle yourself.

Security testing has the following additional requirements:

  • firebaseio-demo.com is unsupported, as Firebase doesn't check rules there.
  • Call chai.setFirebaseAuthToken() with an auth token before you make any assertions.

There are four new flags and one new method on assertions:

  • can
  • cannot
  • read
  • write
  • ref()

So you can write assertions that match the following syntax:

return expect({ uid: 'metropolis:maria' }).can.read.ref(root.child('users/maria'));
return expect({ uid: 'metropolis:robotmaria'}).cannot.read.ref(root.child('users/maria'));

The expectation object (that's to say, the thing wrapped in the assertion) is a Javascript object with authentication credentials. For write tests, you can supply an object to attempt to write to the ref (for testing validation rules et al.):

return expect({ uid: 'metropolis:robotmaria'}).cannot.write(true)
.to.ref(root.child('city/agitation'));

The following rules apply:

  • If the expectation object is null, the assertion assumes you mean an unauthenticated user.
  • Every user object except null must supply a value for uid.
  • To make a token an admin token, set admin: true. Note that this definitionally will cause any test to pass.

Documentation

There's API documentation here.

Warning

In order to capture annoying Firebase console messages, this library monkeypatches console.log(), console.warn(), and console.error(). Your mileage may vary, void where prohibited by law.

Keywords

Fireproof

FAQs

Package last updated on 10 Dec 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.