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

Creating mock data fails because permissions #1

Open
johanneshayry opened this issue Feb 18, 2020 · 3 comments
Open

Creating mock data fails because permissions #1

johanneshayry opened this issue Feb 18, 2020 · 3 comments

Comments

@johanneshayry
Copy link

Firestore emulator automatically applies the rules set in firebase.json. This could be mentioned in the code comments/tutorial. To write data you need to use initializeAdminApp, otherwise, the mock data write will probably fail because of the rules.

@avacadoadam
Copy link

@johanneshayry Hey I encounter the same problem.
When trying to use a empty null auth object.
I got a error
Error: auth must be an object with a 'sub' or 'uid' field
Even when I created a object with sub and uid field it still failed.

Can you post the code you used with initalizeAdminApp to insert mock data.

@avacadoadam
Copy link

Just a update I added the code to the helper function to insert the mock data with admin and return the test database using firestore rules

module.exports.setup = async (auth, data) => {
  const projectId = "rules-spec-${Date.now()}";
  const app = await firebase.initializeTestApp({
    projectId,
    auth
  });

  const db = app.firestore();
  const dbAdmin = firebase.initializeAdminApp({projectId}).firestore();

    if (data) {
      for (const key in data) {
        const ref = dbAdmin.doc(key);
        await ref.set(data[key]);
      }
    }
  
  // Apply rules
  await firebase.loadFirestoreRules({
    projectId,
    rules: fs.readFileSync('firestore.rules', 'utf8')
  });

  return db;
};  

@AkshayAdiga1997
Copy link

@avacadoadam Remove "rules": "firestore.rules", in firebase.json.

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