Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Where exactly is the quickstart #71

Open
stot3 opened this issue May 28, 2019 · 12 comments
Open

Where exactly is the quickstart #71

stot3 opened this issue May 28, 2019 · 12 comments

Comments

@stot3
Copy link

stot3 commented May 28, 2019

I thought there would be an example showing the capabilities of the firebase emulator, perhaps it is not in the file I was looking for.

@samtstern
Copy link
Contributor

This example shows how to do unit testing with the emulator:
https://github.com/firebase/quickstart-nodejs/tree/master/firestore-emulator/javascript-quickstart

We are working on new docs / samples to show how to do more end-to-end emulator testing.

@stot3
Copy link
Author

stot3 commented May 28, 2019

Okay, I appreciate the help.

@samtstern
Copy link
Contributor

@stot3 what sort of tutorials would you like to see?

@stot3
Copy link
Author

stot3 commented May 28, 2019

I think most of this can be solved, if I learn to test the functions, but atm, I'm building a file converter (png to webp), I want to just pass a file to the emulator, or perhaps a mocked up http call, then see the results.

@ChrisChiasson
Copy link

ChrisChiasson commented Sep 7, 2019

Noting this:

To use this, some people may clone the entire quickstart-nodejs directory. When starting the quickstart, cd to the specific quickstart related to your project (i.e. cd firestore-emulator/javascript-quickstart).

I'm lost in the quickstart at this point:

Add your project to the emulator (use firebase init if no project setup)
firebase use --add your-project-name

I used firebase init and made up a project name, but it failed.

chrischiasson@penguin:~/quickstart-nodejs/firestore-emulator/javascript-quickstart$ firebase init

     ######## #### ########  ######## ########     ###     ######  ########
     ##        ##  ##     ## ##       ##     ##  ##   ##  ##       ##
     ######    ##  ########  ######   ########  #########  ######  ######
     ##        ##  ##    ##  ##       ##     ## ##     ##       ## ##
     ##       #### ##     ## ######## ########  ##     ##  ######  ########

You're about to initialize a Firebase project in this directory:

  /home/chrischiasson/quickstart-nodejs/firestore-emulator/javascript-quickstart

Before we get started, keep in mind:

  * You are initializing in an existing Firebase project directory

? Which Firebase CLI features do you want to set up for this folder? Press Space to select features, then 
Enter to confirm your choices. Firestore: Deploy rules and create indexes for Firestore

=== Project Setup

First, let's associate this project directory with a Firebase project.
You can create multiple project aliases by running firebase use --add, 
but for now we'll just set up a default project.

? Please select an option: Create a new project
i  If you want to create a project in a Google Cloud organization or folder, please use "firebase projects:create" instead, and return to this command when you've created the project.
? Please specify a unique project id (warning: cannot be modified afterward) [6-30 characters]:
 testingproject
? What would you like to call your project? (defaults to your project ID) 
✖ Creating Google Cloud Platform project

Error: Failed to create Google Cloud project. See firebase-debug.log for more info.

What should I try next?
I'm trying to debug some rules like this:

  match /recaptcha/{userId}{
   allow read: if ownsId(userId);
   allow write: if ownsId(userId)
                && request.resource.data.size() == 1
                && request.resource.data.hasAll(['response'])
                && request.resource.data.response is string;
   allow delete: if ownsId(userId);
  }

ownsId looks like this:

  function ownsId(userId){
   return request.auth.uid== userId;
  }

Everything worked before I added the 3 lines of additional conditions (the lines beginning with ampersands) to the write rule (which I will eventually change to a create rule). There is a Firebase Function that monitors create operations that triggers onCreate. The normal flow is that the user calls delete (await docRef.delete()), followed by set to send the recaptcha token response, then the Firebase Function updates the document with the server-side validation json from Google. The 3 new rules are there to prevent the client from faking Google's server-side json. I realize I could structure it differently to avoid this situation, but it is important for me to learn how the rules work. With the new rules, the set operation never finishes, much like this stack-overflow question & self-answer, except nothing else is going on in the background. The only change is these three new rules, and there are no errors to report.
https://stackoverflow.com/questions/57339474/firestore-set-suddenly-not-returning-in-then-statement-and-not-adding-docs

@samtstern
Copy link
Contributor

samtstern commented Sep 7, 2019 via email

@ChrisChiasson
Copy link

Thanks! Your question led me to do some searching, and I am going to attempt to rewrite the rule using the function mentioned in this answer (the hasOnly function): https://stackoverflow.com/questions/52245118/how-does-resource-data-size-work-in-firestore-rules-what-is-being-counted

@ChrisChiasson
Copy link

It works using hasOnly and eliminating size. Thank you very very much @samtstern ! It's hard to beat an experienced person's intuition!

  match /recaptcha/{userId}{
   allow read: if ownsId(userId);
   allow create: if ownsId(userId)
                 && request.resource.data.keys().hasOnly(['response'])
                 && request.resource.data.response is string;
   allow delete: if ownsId(userId);
  }

@samtstern
Copy link
Contributor

@ChrisChiasson so if you're still interested I'm happy to debug your situation and get the Firestore emulator working for you!

A few notes:

  • Not sure what's going on with use --add but you can always just skip right to firebase --project=your_project_id emulators:start to get the Firestore emulator started with the right project id.
  • In security rules we added a debug() function. In production it does nothing at all but in the emulator it gives you new logs. You can imagine it like this:
function debug(x) {
  console.log(x);
  return x;
}

So if I was debugging your original problem I might have done:

  match /recaptcha/{userId}{
   allow read: if ownsId(userId);
   allow create: if ownsId(userId)
                 // This will print out the request data in the Firestore emulator logs
                 && debug(request.resource.data).keys().hasOnly(['response'])
                 && request.resource.data.response is string;
   allow delete: if ownsId(userId);
  }

@ChrisChiasson
Copy link

Thanks! I'll almost certainly need the emulator again in the coming weeks, so I will be checking back and re-reading multiple times. The reason I didn't use use --add is that I was trying to just run the quickstart as-is (i.e. to verify the quickstart was working before I rewrote its rules to the ones I was trying to debug). That's why I tried to do firebase init. Does the quickstart actually write anything to the live firestore? If not, I could just give it the name of my existing project instead of a new one (maybe this is what the tutorial intended all along?). Thanks again!

@samtstern
Copy link
Contributor

samtstern commented Sep 9, 2019

@ChrisChiasson if properly configured the quickstart will not write to production. That said if you forget to point things at the emulator (or change the config) it certainly can write to production so better safe than sorry! I wouldn't point this at your most valuable projects. It's normally better to have a "dev" project as a sandbox.

@ChrisChiasson
Copy link

Duly noted

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants