firebase / firebase-js-sdk

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

how to get Geopoint class that can be used for my Firestore security rules unit testing? #6542

Closed AgungLaksana closed 2 years ago

AgungLaksana commented 2 years ago

so previously I have the same exact problem, but the solution is no longer valid for the latest @firebase/rules-unit-testing here is the previous related issue: https://github.com/firebase/firebase-js-sdk/issues/4723

[REQUIRED] Describe your environment

I am using

Operating System version: macOS v12.5 Firebase Product: rules-unit-testing; firestore Node 16 firebase-admin": "^9.11.0" firebase-functions": "^3.15.5" @firebase/rules-unit-testing": "^2.0.4" firebase-tools 10.9.2

[REQUIRED] Describe the problem

Steps to reproduce:

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

new admin.firestore.GeoPoint(this.latitude, this.longitude);

the error is:

FirebaseError: Function DocumentReference.set() called with invalid data. Unsupported field value: a custom object (found in field coordinate in document events/1234).

from my previous issue, it is said that the root cause was

The issue here is that the web and admin SDKs have separate GeoPoint classes that are not interoperable with each other.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:

and the suggested solution was to use the Geopoint from @firebase/rules-unit-testingdependency like below

Relevant Code:

import * as firebase from "@firebase/rules-unit-testing";
const myCoordinate = new firebase.firestore.GeoPoint(-6.931980, 107.559540); // <--- I have to use this Geopoint on my unit test

the problem is, I can no longer find firebase.firestore.GeoPoint on the latest @firebase/rules-unit-testingdependency. so how to get Geopoint that can be used on my security rules unit testing?

AgungLaksana commented 2 years ago

I finally find the solution,

for unit test security rules, I have to change from


import * as admin from "firebase-admin";
const coordinate = new admin.firestore.GeoPoint(latitude, longitude);

to be

import { GeoPoint } from "firebase/firestore"
const coordinate = new GeoPoint(latitude,longitude);

the web and admin SDKs have separate GeoPoint classes that are not interoperable with each other.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: