Sunday, 1 May 2022

How to use nock in order to mock Firebase/Firestore in NodeJS?

I have a REST API which written in NodeJS. The REST API gets a file and information and then uploads the file to the drive and stores the information inside my Firebase/Firestore DB. I'm trying to write tests in order to test my REST API, so I need to mock Firestore. I already mock the google-drive using nock by tracking the HTTPS requests that are made by googleapis. Now I'm trying to do the same thing with Firestore.

I tried different methods like, for example, for authentication I tried (based on the docs):

nock('https://firestore.googleapis.com/$discovery/rest')
.persist()
.post('')
.reply(200, { 'access_token': 'abc', 'refresh_token': '123', 'expires_in': 10 })

But nock does not follow the requests. How do I use nock to track the following code? Does firebase package actually makes requests to the API?

const firebase = require("firebase");

// Required for side-effects
require("firebase/firestore");

// First code - authenatiocation
firebase.initializeApp(firebaseConfig);
db = firebase.firestore();

// ...
// Second code - Get data from DB
const snapshot = await db.collection(collection).orderBy("timestamp", "desc").get();

// ...
// Third code - upload new report
const response = await db.collection(collection).doc(document_id).set(data);

If it's not possible with nock, then it can be achieved?



from How to use nock in order to mock Firebase/Firestore in NodeJS?

"redirect_uri_mismatch" when using "popup" mode

We build our web application and integrate with Google OAuth2 according to this doc Google Identity Services JavaScript SDK

We would like to use "popup" mode, but when trying to exchange access_token with authentication code, /token return error like "

{"error": "redirect_uri_mismatch","error_description": "Bad Request"}"

We have passed the redirect_url param when calling /token, and it is exactly the one configured on the credential page.

What's more weird is that when we switch to "redirect" mode, everything works fine.

Any help? Thanks a lot!



from "redirect_uri_mismatch" when using "popup" mode

How to delete all files by date?

trying to delete all files, which are newer than specified time on versions Q+

    val result = resolver.delete(
        MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "datetaken > ?",
        arrayOf(timestamp.toString())
    )

however they are not deleted, result shows always 0.



from How to delete all files by date?