goldibex / targaryen

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

Wrong result for array-like collections #122

Closed barmalejj closed 7 years ago

barmalejj commented 7 years ago

Hey!

Great library! I was looking for something like that, BUT ... it doesn't work well for array-like collections. Example: lets say we have collection like that: (We're are using bolt in our project)

type SomeAttr {
  // some mandatory property
  itemId: String
  ...
  // some optional property
  someAttrProp: String | Null
  ...
}
type Item {
  ...
  someAttrsCollection: SomeAttr[]
  ...
}

This code after compile results with rules like

...
"someAttrsCollection": {
  "$key1": {
    ".validate": "newData.hasChildren() && newData.hasChild('itemId')",
    ...
    "someAttrProp": {
      ".validate": "newData.isString()"
    }
    ...
  }
}
...

so, if we make write operation like that: /item/someAttrsCollection/0/someAttrProp, 'some value' it should be successful (and it is in real database!!!), but unit test with targaryen fails (write is denied) complaining to this rule ".validate": "newData.hasChildren() && newData.hasChild('itemId')"

dinoboff commented 7 years ago

@barmalejj

Can you check https://github.com/dinoboff/targaryen/commit/267efe3c9f96db26c2fa75d9502ba1c56f33dc39; the test passes. How can I reproduce your error?

barmalejj commented 7 years ago

Here:

someAttrsCollection: {
 +            0: {
 +              itemId: 'foo'
 +            }
 +          }

is a mistake. someAttrsCollection should be an array. Like:

someAttrsCollection: [
 +            0: {
 +              itemId: 'foo'
 +            }
 +          ]
dinoboff commented 7 years ago

Update with data set to to an array... the tests are still passes.

dinoboff commented 7 years ago

@barmalejj

ps: I added an assertion to test the write operation fails if there's no itemId. https://github.com/dinoboff/targaryen/commit/8ed4e2a73380ee3076d969e40c2157c6957e4131

dinoboff commented 7 years ago

Feel free to reopen if some inconsistent test (as a bare minimum, db data, its rules, and simulation write path and data).