-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test parser against live firebase evaluation.
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
Showing
7 changed files
with
577 additions
and
3 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,3 +30,5 @@ node_modules | |
|
||
# Editors | ||
*.sublime-* | ||
service-account.json | ||
secret.json |
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
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
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)); | ||
} |
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
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); | ||
} | ||
); | ||
}; |
Oops, something went wrong.