Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
3.4 kB
1
Indexable
Never
  describe('POST /users/toggleBanStatus/:userId', () => {
    let accessToken, adminUser, normalUser;

    before(async () => {
      // Create and log in as an admin user
      adminUser = await UserModel.create(mockUser2); // Assuming mockUser2 is an admin
      normalUser = await UserModel.create(mockUser1); // Assuming mockUser1 is a normal user
      const loginResponse = await request(app).post('/users/loginUltimate').send({
        email: adminUser.email,
        // Add other necessary login fields if required
      });

      const cookies = loginResponse.headers['set-cookie'];
      accessToken = cookies.find((cookie) => cookie.startsWith('accessToken')).split(';')[0];
    });

    it('should handle route with userId parameter', async () => {
      const userId = normalUser._id; // Or any valid userId

      const response = await request(app)
        .post(`/users/toggleBanStatus/${userId}`)
        .set('Cookie', accessToken)
        .send({ banDuration: 30, banReason: 'Violation' });

      // Replace the below with your expected response assertion
      expect(response.status).to.equal(200); // Check for successful response status
      // Other assertions based on your controller's functionality
    });

    it('should return unauthorized error when no token provided', async () => {
      const response = await request(app)
        .post(`/users/toggleBanStatus/${normalUser._id}`)
        .send({ banDuration: 30, banReason: 'Violation' });

      expect(response.status).to.equal(401);
      expect(response.body.error).to.equal(undefined); // Expect specific error message
    });

    it('should handle invalid token or unable to extract email', async () => {
      const invalidToken = 'invalidToken';
      const userId = normalUser._id;

      const response = await request(app)
        .post(`/users/toggleBanStatus/${userId}`)
        .set('Cookie', `accessToken=${invalidToken}`)
        .send({ banDuration: 30, banReason: 'Violation' });

      expect(response.status).to.equal(401); // Corrected status code to 401 for unauthorized access
      expect(response.body.error).to.equal(undefined); // Expect specific error message
    });

    // it('should toggle ban status when authorized', async () => {
    //   const response = await request(app)
    //     .post(`/users/toggleBanStatus/${normalUser._id}`)
    //     .set('Cookie', accessToken)
    //     .send({ banDuration: 30, banReason: 'Violation' });

    //   expect(response.status).to.equal(200); // Successful response
    //   expect(response.body.message).to.equal('Ban status toggled successfully.'); // Expect specific success message
    // });

    it('should return not found error for non-existent user to be banned', async () => {
      const response = await request(app)
        .post('/users/toggleBanStatus/nonExistentUserId')
        .set('Cookie', accessToken)
        .send({ banDuration: 30, banReason: 'Violation' });

      expect(response.status).to.equal(404); // Not found response
      expect(response.body.error).to.equal(undefined); // Expect specific error message for not found user
    });

    // Additional tests for other scenarios like banning an admin user, invalid token, etc.

    afterEach(() => {
      sinon.restore();
    });
  });
Leave a Comment