Skip to content

Commit

Permalink
Test parser against live firebase evaluation.
Browse files Browse the repository at this point in the history
We save live firebase evaluation in a JSON file (and commit it) and run test against them. The alternative of running targaryen and live test in parallel would take too long.

The cron job should be run everyday to make sure Firebase evaluation hasn’t change (TODO).
  • Loading branch information
dinoboff committed Dec 1, 2016
1 parent 52c0ed0 commit d103cbe
Show file tree
Hide file tree
Showing 7 changed files with 577 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ node_modules

# Editors
*.sublime-*
service-account.json
secret.json
50 changes: 50 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,56 @@ git remote add upstream https://github.com/goldibex/targaryen.git
npm install
```

## Reporting a parsing error

If the error relates to rule parsing and evaluation, you can use
`./bin/targaryen-specs`; e.g.:
```
$ ./bin/targaryen-specs -a '{"tests": [{"rule": "1/0 > 2"}]}'
{
"users": {
"unauth": null
},
"tests": [
{
"rule": "1/0 > 2",
"user": "unauth",
"isValid": true,
"failAtRuntime": false,
"evaluateTo": false
}
]
}
{ Error: Targaryen and Firebase evaluation of "1/0 > 2" diverges.
The rule should evaluate to false.
at MatchError (/targaryen/lib/parser/specs.js:28:5)
at Rule.match (/targaryen/lib/parser/specs.js:235:13)
at fixtures.tests.filter.forEach.t (/targaryen/bin/targaryen-specs:103:21)
at Array.forEach (native)
at test (/targaryen/bin/targaryen-specs:103:6)
at process._tickCallback (internal/process/next_tick.js:103:7)
spec:
Rule {
rule: '1/0 > 2',
user: 'unauth',
wildchildren: undefined,
data: undefined,
isValid: true,
failAtRuntime: false,
evaluateTo: false },
targaryen: { isValid: true, failAtRuntime: false, evaluateTo: true } }
```

To add it to the list of test fixture in `test/spec/lib/parser/fixtures.json`:
```
./bin/targaryen-specs -s test/spec/lib/parser/fixtures.json -i -a '{"tests": [{"rule": "1/0 > 2"}]}'
# or
npm run fixtures -- -a '{"tests": [{"rule": "1/0 > 2"}]}'
```

For other type of bug, you should submit regular mocha tests if possible.


## Feature branch

Expand Down
105 changes: 105 additions & 0 deletions bin/targaryen-specs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#!/usr/bin/env node
/**
* Create parsing test fixtures by evaluating rules against a live Firebase DB.
*
* Expect in the stdin the JSON encoded rules to update. The JSON result will
* be sent to stdout.
*
* It uses the npm "debug" package to send debug info to stderr.
*
*/
/* eslint no-console: "off" */

'use strict';

const HELP = `targaryen-specs [-v] [-h] [-s SPECS_SOURCE] [-i] [-a NEW_SPECS_TO_APPEND]
Usages:
# Evaluate rules live in ./fixtures.json and print them in stdout
targaryen-specs -s ./fixtures.json
# Evaluate rules live in ./fixtures.json and print debug info to sdterr
targaryen-specs -v -s ./fixtures.json
# Evaluate rules live in ./fixtures.json and save the results in place
targaryen-specs -i -s ./fixtures.json
# Evaluate and append new rules to ./fixtures.json
targaryen-specs -i -s ./fixtures.json -a '{"tests": [{"rule": "true"}]'
`;

const argv = require('minimist')(process.argv.slice(2), {boolean: ['i', 'v', 'h']});

const VERBOSE = argv.v;
const PRINT_HELP = argv.h;
const SOURCE = argv.s;
const IN_PLACE = argv.i;
const NEW_SPECS = JSON.parse(argv.a || 'null');

if (PRINT_HELP) {
console.error(HELP);
process.exit(0);
}

if (VERBOSE && !process.env.DEBUG) {
process.env.DEBUG = '*';
}

const specs = require('../lib/parser/specs');
const path = require('path');
const fs = require('fs');

const OLD_SPECS = SOURCE ? require(path.resolve(SOURCE)) : {users: {}, tests: []};

const task = NEW_SPECS ? append(NEW_SPECS, OLD_SPECS) : update(OLD_SPECS);

task.then(save).then(test).catch(e => {
console.error(e);
process.exit(1);
});

function append(newFixtures, fixtures) {
const users = Object.assign(
{},
fixtures.users,
newFixtures.users,
{unauth: null}
);
const newTests = newFixtures.tests.map(
t => Object.assign({user: 'unauth'}, t)
);

return specs.test(newTests, users).then(tests => ({
users,
tests: fixtures.tests.concat(tests)
}));
}

function update(fixtures) {
const users = Object.assign({}, fixtures.users, {unauth: null});
const oldTests = fixtures.tests.map(
t => Object.assign({user: 'unauth'}, t)
);

return specs.test(oldTests, users).then(tests => ({users, tests}));
}

function save(fixtures) {
const output = JSON.stringify(fixtures, null, 2);

if (!IN_PLACE) {
console.log(output);
return fixtures;
}

fs.writeFileSync(SOURCE, output);
return fixtures;
}

function test(fixtures) {
fixtures.tests
.filter(t => typeof t.compare === 'function')
.forEach(t => t.compare(fixtures.users));
}
121 changes: 121 additions & 0 deletions lib/firebase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/**
* Client helper for a live Firebase DB.
*/

'use strict';

const log = require('debug')('targaryen:firebase');
const FirebaseTokenGenerator = require('firebase-token-generator');
const path = require('path');
const request = require('request-promise-native');

const secretPath = process.env.TARGARYEN_SECRET || path.resolve('secret.json');

/**
* Deploy rules to a Firebase Database.
*
* By default it will look for the Firebase secret key in "./secret.json"
* (should hold the "projectId" and "secret")
*
* @param {object|string} rules Rules to upload
* @param {{secret: {projecId: string, token: string}}} options Client options
* @return {Promise<void,Error>}
*/
exports.deployRules = function(rules, options) {
options = options || {};

const secret = options.secret || require(secretPath);
const databaseURL = `https://${secret.projectId}.firebaseio.com`;

const uri = `${databaseURL}/.settings/rules.json?auth=${secret.token}`;
const method = 'PUT';
const body = typeof rules === 'string' ? rules : JSON.stringify({rules}, undefined, 2);

return request({uri, method, body});
};

/**
* Deploy data to a Firebase Database.
*
* By default it will look for the Firebase secret key in "./secret.json"
* (should hold the "projectId" and "secret").
*
* @param {any} data root data to import
* @param {{secret: {projecId: string, token: string}}} options Client options
* @return {Promise<void,Error>}
*/
exports.deployData = function(data, options) {
options = options || {};

const secret = options.secret || require(secretPath);
const databaseURL = `https://${secret.projectId}.firebaseio.com`;

const uri = `${databaseURL}/.json?auth=${secret.token}`;
const method = 'PUT';
const body = JSON.stringify(data || null);

return request({uri, method, body});
};

/**
* Create legacy id token for firebase REST api authentication.
*
* By default it will look for the Firebase secret key in "./secret.json"
* (should hold the "projectId" and "secret").
*
* @param {object} users Map of name to auth object.
* @param {{secret: {projecId: string, token: string}}} options Client options
* @return {object}
*/
exports.tokens = function(users, options) {
options = options || {};

const secret = options.secret || require(secretPath);
const tokenGenerator = new FirebaseTokenGenerator(secret.token);

return Object.keys(users || {}).reduce((tokens, name) => {
const user = users[name];

tokens[name] = user ? tokenGenerator.createToken(user) : null;

return tokens;
}, {});
};

/**
* Test a path can be read with the given id token.
*
* Resolve to true if it can or false if it couldn't. Reject if there was an
* issue with the request.
*
* By default it will look for the Firebase secret key in "./secret.json"
* (should hold the "projectId" and "secret").
*
* @param {string} path Path to read.
* @param {string} token Legacy id token to use.
* @param {{secret: {projecId: string}}} options Client options
* @return {Promise<boolean,Error>}
*/
exports.canRead = function(path, token, options) {
options = options || {};

const secret = options.secret || require(secretPath);
const databaseURL = `https://${secret.projectId}.firebaseio.com`;

const auth = token ? `?auth=${token}` : '';
const uri = `${databaseURL}/${path}.json${auth}`;
const method = 'GET';

log(`${method} ${uri}`);

return request({uri, method}).then(
() => true,
e => {
if (e.statusCode === 403 || e.statusCode === 401) {
return false;
}

return Promise.reject(e);
}
);
};
Loading

0 comments on commit d103cbe

Please sign in to comment.