Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

97
Vistas
Why does this test for firebase security rule fails?

I have a collection UserActivity, where each docId is the id of the user. Under different docIds I have subcollection profileVisit where I want to put some data My firebase rule, should allow only create operation. That is, user can create subcollection under Useractivity/{userId}/profileVisit only if they are logged in and they own this resource. So my firebase rule is the following:

match /UsersActivity/{userId} {
      match /profileVisit {
        allow create: if userIsAuthenticated() && userOwnsResource(userId);
      }
    }

function userIsAuthenticated () {
      return request.auth.uid != null;
    }

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

That is, thefollowing operation should be allowed:

firebase
      .firestore()
      .collection("UserActivity")
      .doc(uid)
      .collection("profileVisit")
      .add({
        data: "some data",
      }); 

The test which I wrote is the following:

it("Users can create subcollection profileVisit under their own UsersActivity document if they are signed in", async () => {
    const db = getFirestore(auth);
    const userDoc = db
      .collection("UsersActivity")
      .doc(myId)
      .collection("profileVisit");
    await firebase.assertSucceeds(userDoc.add({ data: "data" }));
  });

This test fails with: FirebaseError: 7 PERMISSION_DENIED: false for 'create'

Can someone explain to me why does my test fail? Have I written my rule correctly or is it the test written in the wrong way?

EDIT

Seems that changing the rule to:

match /UsersActivity/{userId} {
          match /profileVisit/{id} {
            allow create: if userIsAuthenticated() && userOwnsResource(userId);
          }
        } 

allows my test to pass. That is I changed match /profileVisit to match /profileVisit/{id} { Can someone explain me why there is a difference and why I need to add {id} in the end?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

As you discovered yourself in your edit, this doesn't do anything:

match /UsersActivity/{userId} {
  match /profileVisit {
    allow create: if userIsAuthenticated() && userOwnsResource(userId);
  }
}

The match /profileVisit matches the profileVisit collection, but no documents in that, so it's a noop.

To make it match any document, use:

match /UsersActivity/{userId} {
  match /profileVisit/{docId} {
    allow create: if userIsAuthenticated() && userOwnsResource(userId);
  }
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda