ladjs / supertest

🕷 Super-agent driven library for testing node.js HTTP servers using a fluent API. Maintained for @forwardemail, @ladjs, @spamscanner, @breejs, @cabinjs, and @lassjs.
MIT License
13.82k stars 759 forks source link

How to get an access to request object before request ? #635

Open golubvladimir opened 4 years ago

golubvladimir commented 4 years ago

Without supertest:

  it('success', () => {
    const req = {
      user: {
        name: 'Jack',
        type: 'admin'
      }
    };

    const next = jest.fn();

    authenticateAdmin(req, {}, next);

    expect(next).toBeCalled();
  });

With jest

  beforeEach(() => {
    const app = express();

    app.post(
      '/',
      authenticateAdmin,
      () => {}
    );
  });

  it('success', () => {
    request(app)
      .post('/')
      .send({
        user: {
          name: 'Jack',
          type: 'admin'
        }
      })
  });

But with send, I send body object. How to set an object property of request ?

I want to test it:

module.exports = (req, res, next) =>  {
  const user = req.user;

  console.log({ req });

  if (user.type !== 'admin') {
    return res.status(403).json({ error: req.t('errors.auth.access_error') });
  }

  next();
};
golubvladimir commented 4 years ago
  beforeEach(() => {
    const user = {
      name: 'Jack',
      type: 'admin'
    };

    app.post(
      '/',
      [
        (req, res, next) => {
          req.user = user;
          next();
        },
        authenticateAdmin
      ],
      (req, res) => {
        res.status(200).json({ result: true });
      }
    );
  });

  it('success', (done) => {
    request(app)
      .post('/')
      .end((err, res) => {
        if (err) return done(err);

        console.log(res.body.result);

        done();
      });
  });
jonathansamines commented 4 years ago

@golubvladimir All supertest will do is to create an http request. If req.user is result of some form on authentication mechanism, you have to replicate (at http level) whatever that mechanism requires to complete the authentication process.

Corinos commented 4 years ago

@jonathansamines How do we access the request before it's sent? I'm trying to debug whether my request is actually being made or not, as I don't seem to be getting any response at all, so I want to first confirm that the request has been built properly, then somehow confirm that it has been sent.

benjaminhera commented 1 year ago

Any updates regarding this issue?

chtpl commented 9 months ago

I would also love to have this feature

icep0ps commented 6 months ago

I am using passportjs which sets the req.user object on the request so if you have a similar setup this is how I would suggest you do it, authenticate the user before all my tests. To do this I followed this guide and I put the following code at the top of my tests files.

const app = App.init(Database);
const user = request.agent(app);

beforeAll(async () => {
  await user.post('/auth/login').send({
    username: 'anime_lover123',
    password: 'examplePassword123',
  });
});