gitbookio / expect-firestore Goto Github PK
View Code? Open in Web Editor NEWAPI client and Jasmine matchers for the Firestore Rules API
API client and Jasmine matchers for the Firestore Rules API
Function mocks need to make sure to use the full /databases/
path:
function: "get",
args: [{ exact_value: "/databases/(default)/documents/" + path }],
result: {
value: {
// ...
}
}
(note @SamyPesse I am filing these as I find them, I will try and find time to fix them myself)
The module should have methods: database.canList(auth, collection)
and database.cannotList(auth, collection)
If I use the following data everything works fine:
const mockData = {
stories: [
{
key: 'story1',
fields: {
name: 'Story 1',
'roles.user123': 'owner'
},
collections: {} // Note
}
]
};
But I make this change:
const mockData = {
stories: [
{
key: 'story1',
fields: {
name: 'Story 1',
'roles.user123': 'owner'
}
}
]
};
I get this error:
RangeError: Maximum call stack size exceeded
at assertPath (path.js:5:20)
at Object.join (path.js:1211:7)
at /usr/local/google/home/samstern/Projects/firebase/snippets-rules/node_modules/expect-firestore/lib/database.js:102:50
at Array.reduce (native)
at /usr/local/google/home/samstern/Projects/firebase/snippets-rules/node_modules/expect-firestore/lib/database.js:101:56
at Array.reduce (native)
at Database.getDocuments (/usr/local/google/home/samstern/Projects/firebase/snippets-rules/node_modules/expect-firestore/lib/database.js:98:45)
at /usr/local/google/home/samstern/Projects/firebase/snippets-rules/node_modules/expect-firestore/lib/database.js:103:42
at Array.reduce (native)
at /usr/local/google/home/samstern/Projects/firebase/snippets-rules/node_modules/expect-firestore/lib/database.js:101:56
If I dive into the call stack and go to expect-firestore/lib/database.js:102:50
:
101 var docs = collections[collectionName].reduce(function (_docs, doc) {
102 var docPath = _path2.default.join(collectionPath, doc.key);
103 var children = _this.getDocuments(doc.collections, docPath);
104
105 return [{
106 path: docPath,
107 doc: doc
108 }].concat(_docs).concat(children);
109 }, []);
Right now we send requests that look likes this for an update:
"testSuite": {
"testCases": [
{
"expectation": "ALLOW",
"request": {
"auth": {
"uid": "writeruser"
},
"path": "/databases/(default)/documents/stories/story1",
"method": "update",
"resource": {
"data": {
"content": "Something new!"
}
}
},
"resource": {
"data": {
"title": "Story 1",
"content": "Something new!",
"roles": {
"owneruser": "owner",
"writeruser": "writer",
"readeruser": "reader"
}
}
},
In Firestore rules, request.resource
should reflect the complete state of resource
with the current request applied.
It seems in this case, the update change is being made to resource
and not request.resource
which is not correct.
This module is missing an API to test batched writes.
await database.canCommit([
firebase.Batch.set('users/userA', { .. }),
firebase.Batch.delete('users/userA')
])
The goal is to have an API that match the firestore naming as much as possible:
batch.commit
-> database.canCommit(batch: Array<firestore.Batch.Operation>)
batch.set
-> firestore.Batch.set(path: string, value: any)
batch.delete
-> firestore.Batch.delete(path: string)
batch.update
-> firestore.Batch.update(path: string, data: Object)
The main blocker is that the Firestore rules API doesn't seem to support batch updates. Can you confirm @samtstern ?
Trying to use this module, I get the error (sanitized output):
[...]/expect-firestore/lib/database.js:171
var _ref2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
ReferenceError: regeneratorRuntime is not defined
As far as I know, simply changing the babel config would fix it (transform-runtime plugin I think).
Edit did not saw #1
The problem comes here:
expect-firestore/src/database.js
Line 345 in 532d450
Resource here should be set to the previous state of the data, not what data is being set/updated.
Right now the library has this mapping:
post
patch
get
The methods should actually be the same ones that are in the rules after the allow
keyword. So for Firestore those are create
, update
, delete
, get
, list
, read
, and write
(where read
and write
are combinations of some of the more granular ones).
As far as I am aware, the functions in the README
do not exist.
Instead I had to hack up my own version:
function clearMockData(database: any) {
// TODO: should not have to call both .data and .collections
database.data = undefined;
database.collections = undefined;
database.rules = undefined;
}
function setMockDataAndRules(database: any, data: any, rulesFile: string) {
database.data = data;
database.collections = data;
database.rules = getRulesFile(rulesFile);
}
You can test deleting using batch operations, but it would be nice if there was canDelete
and cannotDelete
, too.
I use Custom Claims to handle multiple roles in my firebase auth system. Firestore rules support these as well. Can i test these with expect-firestore?
When access fails I'd love to get more than false
. Would be good to get the full API response so that I can find unexpected errors or make sure that the error is of the kind I expected.
I work on Firebase and I wanted to add your awesome library to our firebase/snippets-rules
github repository. I am running into a strange error, hoping you can help (I'm not a JavaScript expert at all so excuse me if this is totally user error).
I am trying out this library and decided to run a basic test:
import * as expectFirestore from 'expect-firestore';
const database = expectFirestore.Database({
credential: {},
data: {},
rules: ''
});
// Note: doesn't matter if this function is async or not, or if I await the promises or not
async function main() {
database.authorize();
// Test a get
database.canGet({ uid: 'some-user' }, 'users/userA');
}
main();
When I run it, this errors:
$ ts-node test/index.ts
/usr/local/google/home/samstern/Projects/firebase/snippets-rules/node_modules/expect-firestore/lib/global.js:37
}();
^
ReferenceError: regeneratorRuntime is not defined
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.