Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.6 kB
1
Indexable
Never
describe('GET /users', () => {
    let userServiceStub;

    beforeEach(() => {
      // This stubs the UserService's getAllUsers method
      userServiceStub = sinon.stub(UserService.prototype, 'getAllUsers');
    });

    afterEach(() => {
      sinon.restore();
    });

    it('should return an error if user is not authenticated', async () => {
      const res = await request(app).get('/users/');
      expect(res.status).to.equal(401);
      expect(res.body.message).to.equal(undefined);
    });
    it('should retrieve all users when the admin user is logged in', async () => {
      await UserModel.create(mockUser2);

      const loginResponse = await request(app).post('/users/loginUltimate').send({ email: mockUser2.email });
      expect(loginResponse.status).to.equal(200);

      expect(loginResponse.headers).to.have.property('set-cookie');
      const cookies = loginResponse.headers['set-cookie'];
      const accessTokenCookie = cookies.find((cookie) => cookie.startsWith('accessToken'));

      expect(accessTokenCookie).to.exist;

      const res = await request(app).get('/users').set('Cookie', accessTokenCookie);

      expect(res.status).to.equal(200);
    });

    it('should return an error if no users are found', async () => {
      userServiceStub.resolves([]); // Simulate an empty user list

      const res = await request(app).get('/users'); // Adjust this if your route is different

      expect(res.status).to.equal(401); // Or whatever your logic is for no users found
      expect(res.body.error).to.equal('Authorization token is required');
    });
  });
Leave a Comment