goldibex / targaryen

Test Firebase security rules without connecting to Firebase.
ISC License
242 stars 36 forks source link

Targaryen is broken with Jest? #151

Closed lukepighetti closed 6 years ago

lukepighetti commented 6 years ago

Honestly it's probably not, but I'm at a loss here.

./projects.test.js

const targaryen = require("targaryen/plugins/jest");
const rules = require("../database.rules.json");

expect.extend({
  toAllowRead: targaryen.toAllowRead,
  toAllowUpdate: targaryen.toAllowUpdate,
  toAllowWrite: targaryen.toAllowWrite
});

const initialData = require("./projects.mock.json");

describe("basic rules", function() {
  const database = targaryen.getDatabase(rules, initialData);

  expect(database.as({ uid: "foo" })).toAllowRead("/projects");
});

../database.rules.json

{
  "rules": {
    "projects": {
      ".read": "auth != null",
      ".write": "false"
    }
}

./projects.mock.json

{
  "projects": {
    "1337": {
      "title": "Testproject",
      "avatar": "https://pbs.twimg.com/profile_images/990749041364492288/test.jpg",
      "tags": {
        "PoW": true,
        "ANN": true,
        "X16R": true
      },
      "www": "https://test.org",
      "social": {
        "discord": "https://discord.gg/test",
        "twitter": "https://twitter.com/test",
        "github": "https://github.com/test",
        "reddit": "https://www.reddit.com/r/test/",
        "telegram": "https://t.me/test",
        "medium": "https://medium.com/@test"
      },
      "time_created": "1521590400"
    }
  }
}

../package.json

{
  "name": "rules",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "dependencies": {},
  "devDependencies": {
    "jest": "^23.6.0",
    "targaryen": "^3.1.0"
  },
  "scripts": {
    "test": "jest"
  },
  "author": "",
  "license": "ISC"
}

declaration exception

 FAIL  tests/projects.test.js
  basic rules
    ✕ encountered a declaration exception (6ms)

  ● basic rules › encountered a declaration exception

    value: value is undefined

      11 |
      12 | describe("basic rules", function() {
    > 13 |   const database = targaryen.getDatabase(rules, initialData);
         |                              ^
      14 |
      15 |   expect(database.as({ uid: "foo" })).toAllowRead("/projects");
      16 |   // expect(database.as(targaryen.users.authenticated)).toAllowRead("/projects");                          

it seems like it's telling me that getDatabase() is undefined? I'm at a loss here, I seem to be the only person online having this problem so I'm sure I made a mistake, but I cannot figure out where.

lukepighetti commented 6 years ago

I switched my example to match the Jest docs exactly and I get the same error. https://github.com/goldibex/targaryen/tree/master/docs/jest

lukepighetti commented 6 years ago

So I just decided to try using Targaryan's built in integration tests with Targaryan installed globally and I get this error

$ targaryen database.rules.json database.test.json
/usr/local/lib/node_modules/targaryen/lib/parser/statement/identifier.js:26
      throw new ParseError(this, `${this.name} is undefined`);      ^
Error: value: value is undefined
    at IdentifierNode.inferType (/usr/local/lib/node_modules/targaryen/lib/parser/statement/identifier.js:26:13)    at new Node (/usr/local/lib/node_modules/targaryen/lib/parser/statement/base.js:50:30)
    at new IdentifierNode (/usr/local/lib/node_modules/targaryen/lib/parser/statement/identifier.js:18:1)    at Function.from (/usr/local/lib/node_modules/targaryen/lib/parser/statement/base.js:175:12)
    at BinaryNode.init (/usr/local/lib/node_modules/targaryen/lib/parser/statement/binary.js:31:22)    at new Node (/usr/local/lib/node_modules/targaryen/lib/parser/statement/base.js:49:10)
    at new BinaryNode (/usr/local/lib/node_modules/targaryen/lib/parser/statement/binary.js:24:1)    at Function.from (/usr/local/lib/node_modules/targaryen/lib/parser/statement/base.js:175:12)
    at ConditionalNode.init (/usr/local/lib/node_modules/targaryen/lib/parser/statement/conditional.js:21:22)    at new Node (/usr/local/lib/node_modules/targaryen/lib/parser/statement/base.js:49:10)

Is this package broken? Just want to know if I should continue spending time on it or not. Cheers.

lukepighetti commented 6 years ago

You can imagine my embarrassment when I realized I used "value" instead of "this" in my Bolt rules.