firebase / firebase-functions-test

MIT License
232 stars 48 forks source link

Cannot retrieve auth user created by test.auth.makeUserRecord #155

Open Giulio64 opened 2 years ago

Giulio64 commented 2 years ago

I'm trying to junit test my cloud functions with express with supertest + mocha, along with auth:

/* eslint-disable max-len */
/* eslint-disable no-undef */
// eslint-disable-next-line no-unused-vars
/* ;
;*/

const admin = require("firebase-admin");
const test = require("firebase-functions-test")();
const functions = require("../index");
const bodyParser = require("body-parser");
const request = require("supertest");
const express = require("express");

const UserController = require("../Controller/UserController");
const UserFactory = require("./factory/UserFactory");

/**
 * Contains all the test cases for the creation of a user.
 */
describe("User creation", () => {
  let app;

  before((done) =>{
    app = express();
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({extended: false}));
    UserController.prototype.bindEndpoints(app);
    app.listen((err) =>{
      if (err) {
        console.log(err);
        return done(err);
      }
      done();
    });
  });
  after(() => {
    test.cleanup();
  });
  it("credentials exists - just address OK", (done) => {
    const mockUser = test.auth.makeUserRecord({
      email: "test@email.com",
      uid: "USR",
    });
    request(app)
        .post("/users")
        .send(UserFactory.prototype.generateUserAddressPayload())
        .set("Authorization", "Bearer " + mockUser.uid)
        .expect("Content-Type", /json/)
        .expect(200, (err, response) =>{
          if (err) {
            console.log(response.body);
            done(err);
          }
          done();
        });
  });
});

The post users/ endpoint check the uid in the header verifying if it's linked to a user inside auth, using this function:

  getFirebaseUserAsync(uid) {
    return new Promise((res, rej) => {
      return admin
          .auth()
          .getUser(uid)
          .then((userRecord) => {
            return res({code: 200, data: userRecord.toJSON()});
          })
          .catch(() => {
            return rej({code: 404, message: {
              it: "Actor " + uid + " non trovato",
              en: "Actor " + uid + " not found",
            }});
          });
    });
  }

Unfortunately it always return "not found", even after:


  const mockUser = test.auth.makeUserRecord({
      email: "test@email.com",
      uid: "USR",
    });

Version info

firebase-functions-test:"^0.3.2

"firebase-functions": "^3.18.0"

firebase-admin": "^10.0.2

Steps to reproduce

Just create a test case, mocking a user and try to retrieve it.

Expected behavior

It should retrieve the newly generated user in auth.

Actual behavior

No user found.