quickstart-nodejs icon indicating copy to clipboard operation
quickstart-nodejs copied to clipboard

Where exactly is the quickstart

Open stot3 opened this issue 5 years ago • 12 comments

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.

stot3 avatar May 28 '19 19:05 stot3

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.

samtstern avatar May 28 '19 19:05 samtstern

Okay, I appreciate the help.

stot3 avatar May 28 '19 19:05 stot3

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

samtstern avatar May 28 '19 19:05 samtstern

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.

stot3 avatar May 28 '19 19:05 stot3

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

ChrisChiasson avatar Sep 07 '19 00:09 ChrisChiasson

Could you try removing the line about "size" and see if it works then?

I'm on my phone so I can't type out the full instructions to get the emulator working but happy to help with that next time I'm at my computer.

On Fri, Sep 6, 2019, 5:33 PM ChrisChiasson [email protected] wrote:

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, 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

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/firebase/quickstart-nodejs/issues/71?email_source=notifications&email_token=ACATB2S3YBARSCB5DHINMHTQILZHBA5CNFSM4HQGN3F2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD6ELXUI#issuecomment-529054673, or mute the thread https://github.com/notifications/unsubscribe-auth/ACATB2XKHDBNWYYRXHOAUCLQILZHBANCNFSM4HQGN3FQ .

samtstern avatar Sep 07 '19 00:09 samtstern

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 avatar Sep 07 '19 00:09 ChrisChiasson

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);
  }

ChrisChiasson avatar Sep 07 '19 01:09 ChrisChiasson

@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);
  }

samtstern avatar Sep 09 '19 16:09 samtstern

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!

ChrisChiasson avatar Sep 09 '19 17:09 ChrisChiasson

@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.

samtstern avatar Sep 09 '19 18:09 samtstern

Duly noted

ChrisChiasson avatar Sep 12 '19 17:09 ChrisChiasson