bolt icon indicating copy to clipboard operation
bolt copied to clipboard

Testing Docs

Open aputinski opened this issue 10 years ago • 13 comments

It would be great to have an overview of how to get tests running for your own rules.

I keep getting stuck at ReferenceError: suite is not defined

aputinski avatar Nov 14 '15 14:11 aputinski

+1

scolyer avatar Dec 01 '15 09:12 scolyer

Sorry about the sparse documentation - the testing framework IS used in the test suite for the Bolt compiler, but I haven't yet written docs for how you use it in your own project. In the meantime - here are some rough notes that can get you started (hopefully):

Since my use of the rules engine is WITHIN the Bolt package - your use of it from the outside will be a little different. But here are the basic steps:

$ npm install mocha chai
  • Create a test file (e.g. rules-test.js)
  • In the test file:
var bolt = require('firebase-bolt');
var rulesSuite = bolt.rulesSuite;

rulesSuite("My Test", function(test) {
  test.database(<your app sandbox-name>, <your server secret>);
  test.rules(<path-to-your-bolt-rules-file>);
  ...<your tests here>...
});

See this file for a sample of how to write a test:

https://github.com/firebase/bolt/blob/master/src/test/mail-test.ts

Use mocha to run your test.

Note that the test with DELETE ALL THE DATA IN YOUR FIREBASE APP - so please only use an app name you want to use as a "sandbox" version of your database - not your REAL ONE!

mckoss avatar Dec 18 '15 21:12 mckoss

@aputinski you have to launch mocha with --ui tdd in order to have the suite function injected into the tests.

vIceBerg avatar Dec 20 '15 18:12 vIceBerg

there must be a firebase-bolt rules testing framework for testing bolt rules for project

MOHAMMADArsalan avatar Jan 18 '16 07:01 MOHAMMADArsalan

In my rules I do not allow anyone to write the user data, I do it through a node application which has server secret token. How do I write using the admin/utimate access using the test framework?

ziaukhan avatar Jan 31 '16 12:01 ziaukhan

Got it, .as('admin') is letting me write anywhere without regard to rules.

ziaukhan avatar Jan 31 '16 13:01 ziaukhan

Yes @ziaukhan !

mckoss avatar Feb 01 '16 04:02 mckoss

@mckoss sorry to disturb you again, but what if I want to simulate a unauthenticated user what should I put in as() function? If I am leaving it out it is using the last as(). Thanks

ziaukhan avatar Feb 01 '16 09:02 ziaukhan

Special user names are:

.as('anon') - The anonymous (not-signed-in) user.
.as('admin') - A full-access user (rules don't apply so you can write anything as this user.

mckoss avatar Feb 01 '16 19:02 mckoss

I am struggling to find , I have the json file that was generated through IAM, but it doesn't contain anything that looks like server secret. What am I doing wrong?

RomansBermans avatar May 25 '16 15:05 RomansBermans

The test library was written for the Firebase 2.X version - I have not updated it yet for Firebase 3.X. The tools/configure-project script will prompt you to get a server secret from the console and it will write it to a local file.

mckoss avatar May 25 '16 16:05 mckoss

Thanks. Makes sense. Any estimates when it will be updated for Firebase 3.X? Also is this library the best way to test rules for Firebase 3.X? I am after best practices on how I should write tests (Node.js + Firebase Server SDK + Mocha & Chai)? Any hints will be greatly appreciated.

RomansBermans avatar May 25 '16 18:05 RomansBermans

Hi everyone, unfortunately this project looks pretty dead, which is too bad because Bolt is incredible.

Should I be using Targaryan, the method laid out here, or something else in 2018?

lukepighetti avatar Sep 20 '18 13:09 lukepighetti