firebase / firebase-js-sdk

Firebase Javascript SDK
https://firebase.google.com/docs/web/setup
Other
4.85k stars 893 forks source link

firebase/rules-unit-testing library can't create document with Geopoint field #4723

Closed AgungLaksana closed 3 years ago

AgungLaksana commented 3 years ago

I am using

I am trying to perform unit testing of my security rules using library @firebase/rules-unit-testing. when I try to create a document using test app firestore like my code below, it will always have error if it has property which is a Geopoint.

the error is:

FirebaseError: Function DocumentReference.set() called with invalid data. Unsupported field value: a custom object (found in field coordinate in document events/test-580f807-454d-44b3-bd3d-8df40ca1d16e)

the custom object is Geopoint, because I have a field called coordinate which is a Geopoint. but I need to test my security rules, I have to test this rule: request.resource.data.coordinate is latlng

how to test if it is latlng or not If I can't create document with Geopoint?

i can create a document that has Geopoint as its field using firebase.initializeAdminApp().firestore() (admin), but it will always has an error if I create it using firebase.initializeTestApp().firestore()


import * as firebase from "@firebase/rules-unit-testing";

function getFirestore(auth?: TokenOptions) {
    return firebase.initializeTestApp({projectId: projectID, auth: auth}).firestore();
}

it("should create an event document", async () => {

     const db = getFirestore(myAuth);
     const ref = db.collection("events").doc("myEvent");
     const event = new Event("event title", new admin.firestore.GeoPoint(-6.931980, 107.559540))
     const eventData = { ...event};

     const promise = ref.set(eventData); 
     await firebase.assertSucceeds(promise); // <--- error in here
});
google-oss-bot commented 3 years ago

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

thebrianchen commented 3 years ago

@AgungLaksana Thanks for writing in! The issue here is that the web and admin SDKs have separate GeoPoint classes that are not interoperable with each other. If you're using the web SDK with initializeTestApp(), you'll have to the GeoPoint class from the web SDK. In your case, you'll need to use the web SDK's GeoPoint class:

 import * as firebase from "@firebase/rules-unit-testing";
 const event = new Event("event title", new firebase.firestore.GeoPoint(-6.931980, 107.559540))
 const eventData = { ...event};

 const promise = ref.set(eventData); 
 await firebase.assertSucceeds(promise); // 

For more context, see #3910 and #4129. I'm going to mark this issue as closed since this is WAI, but feel free to comment if you have other questions!