How to authenticate Supertest requests with Passport?

后端 未结 8 1415
囚心锁ツ
囚心锁ツ 2020-11-28 21:22

I\'m using Passport.js for authentication (local strategy) and testing with Mocha and Supertest.

How can I create a session and make authenticated requests with Supe

8条回答
  •  离开以前
    2020-11-28 22:14

    GraphQl full Example:

    const adminLogin = async (agent) => {
      const userAdmin = await User.findOne({rol:"admin"}).exec();
      if(!userAdmin) return new Promise.reject('Admin not found')
      return agent.post('/graphql').send({
        query: ` mutation { ${loginQuery(userAdmin.email)} }`
      })//.end((err, {body:{data}}) => {})
    }
    
    test("Login Admin", async (done) => {
      const agent = request.agent(app);
      await adminLogin(agent);
      agent
        .post("/graphql")
        .send({query: `{ getGuests { ${GuestInput.join(' ')} } }`})
        .set("Accept", "application/json")
        .expect("Content-Type", /json/)
        .expect(200)
        .end((err, {body:{data}}) => {
          if (err) return done(err);
          expect(data).toBeInstanceOf(Object);
          const {getGuests} = data;
          expect(getGuests).toBeInstanceOf(Array);
          getGuests.map(user => GuestInput.map(checkFields(user)))
          done();
        });
    })
    

提交回复
热议问题