querystring#encode TypeScript Examples

The following examples show how to use querystring#encode. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: admin-users.test.ts    From office-booker with MIT License 6 votes vote down vote up
test('can create and delete bookings for other people', async () => {
  const createBookingBody = {
    user: otherUser,
    office: { id: officeQuotas[0].id },
    date: format(new Date(), 'yyyy-MM-dd'),
    parking: false,
  };
  const createResponse = await app
    .post('/api/bookings')
    .send(createBookingBody)
    .set('bearer', adminUserEmail);
  expect(createResponse.status).toBe(200);
  expect(Object.keys(createResponse.body)).toEqual([
    'id',
    'created',
    'user',
    'date',
    'office',
    'lastCancellation',
    'parking',
  ]);
  expect(typeof createResponse.body?.id).toBe('string');
  expect(createResponse.body).toMatchObject(createBookingBody);

  const getCreatedBookingResponse = await app
    .get(`/api/bookings?user=${otherUser}`)
    .set('bearer', adminUserEmail);
  expect(getCreatedBookingResponse.body).toContainEqual(createResponse.body);

  const deleteResponse = await app
    .delete(`/api/bookings/${createResponse.body.id}?${encode({ user: otherUser })}`)
    .set('bearer', adminUserEmail);
  expect(deleteResponse.status).toBe(204);

  const getDeletedBookingResponse = await app
    .get(`/api/bookings?user=${otherUser}`)
    .set('bearer', adminUserEmail);
  expect(getDeletedBookingResponse.body).not.toContainEqual(createResponse.body);
});
Example #2
Source File: office-admin-users.test.ts    From office-booker with MIT License 6 votes vote down vote up
test(`can't delete past bookings for other people`, async () => {
  const id = officeQuotas[1].id + '_' + format(new Date(), 'yyyy-MM-dd').replace(/-/gi, '');
  const yesterday = new Date();
  yesterday.setDate(yesterday.getDate() - 1);
  const createBookingBody = {
    id: id,
    user: otherUser,
    officeId: officeQuotas[1].id,
    date: format(yesterday, 'yyyy-MM-dd'),
    parking: false,
  };

  const createdBooking = await createBooking(config, createBookingBody);

  const deleteResponse = await app
    .delete(`/api/bookings/${createdBooking?.id}?${encode({ user: otherUser })}`)
    .set('bearer', officeAdminEmail);
  expect(deleteResponse.status).toBe(403);
});
Example #3
Source File: office-admin-users.test.ts    From office-booker with MIT License 5 votes vote down vote up
test('can create and delete bookings for other people for their office', async () => {
  const createBookingBody = {
    user: otherUser,
    office: { id: office.id },
    date: format(new Date(), 'yyyy-MM-dd'),
    parking: false,
  };

  const createResponse = await app
    .post('/api/bookings')
    .send(createBookingBody)
    .set('bearer', officeAdminEmail);
  expect(createResponse.status).toBe(200);
  expect(Object.keys(createResponse.body)).toEqual([
    'id',
    'created',
    'user',
    'date',
    'office',
    'lastCancellation',
    'parking',
  ]);
  expect(typeof createResponse.body?.id).toBe('string');
  expect(createResponse.body).toMatchObject(createBookingBody);

  const getCreatedBookingResponse = await app
    .get(`/api/bookings?user=${otherUser}&office=${office.id}`)
    .set('bearer', officeAdminEmail);
  expect(getCreatedBookingResponse.body).toContainEqual(createResponse.body);

  const deleteResponse = await app
    .delete(`/api/bookings/${createResponse.body.id}?${encode({ user: otherUser })}`)
    .set('bearer', officeAdminEmail);
  expect(deleteResponse.status).toBe(204);

  const getDeletedBookingResponse = await app
    .get(`/api/bookings?user=${otherUser}`)
    .set('bearer', officeAdminEmail);
  expect(getDeletedBookingResponse.body).not.toContainEqual(createResponse.body);
});
Example #4
Source File: non-admin-users.test.ts    From office-booker with MIT License 4 votes vote down vote up
describe.each(Object.keys(userTypes))('Non-admin user actions', (userType) => {
  const email = userTypes[userType];
  describe(`${userType} user`, () => {
    test(`can get self`, async () => {
      const response = await app.get(`/api/users/${email}`).set('bearer', email);
      expect(response.ok).toBe(true);
      expect(response.body).toEqual({
        email,
        quota: 1,
        admin: false,
        autoApproved: false,
        role: { name: 'Default' },
        permissions: {
          canEditUsers: false,
          canManageAllBookings: false,
          canViewAdminPanel: false,
          canViewUsers: false,
          officesCanManageBookingsFor: [],
        },
      });
    });

    test(`can't get others`, async () => {
      const response = await app.get(`/api/users/${otherUser}`).set('bearer', email);
      expectForbidden(response);
    });

    test(`can't see all bookings`, async () => {
      const response = await app.get('/api/bookings').set('bearer', email);
      expectForbidden(response);
    });

    test(`can't create bookings for other people`, async () => {
      const response = await app
        .post('/api/bookings')
        .send({
          user: otherUser,
          office: { id: officeQuotas[0].id },
          date: format(new Date(), 'yyyy-MM-dd'),
          parking: false,
        })
        .set('bearer', email);
      expectForbidden(response);
    });

    test(`can't delete booking for other people`, async () => {
      const response = await app
        .delete(`/api/bookings/${format(new Date(), 'yyyyMMdd')}?${encode({ user: otherUser })}`)
        .set('bearer', email);
      expectForbidden(response);
    });

    test(`can't delete today's booking`, async () => {
      const createBookingBody = {
        user: email,
        office: { id: officeQuotas[0].id },
        date: format(new Date(), 'yyyy-MM-dd'),
        parking: true,
      };
      const createResponse = await app
        .post('/api/bookings')
        .send(createBookingBody)
        .set('bearer', email);
      expect(createResponse.ok).toBe(true);

      const response = await app
        .delete(`/api/bookings/${createResponse.body.id}`)
        .set('bearer', email);
      expect(response.status).toBe(403);
      expect(response.body).toMatchObject({
        message: 'No longer able to cancel booking',
      });
    });

    test(`can't set user quotas`, async () => {
      const putUserBody = {
        quota: 42,
      };
      const response = await app.put(`/api/users/${email}`).send(putUserBody).set('bearer', email);
      expectForbidden(response);
    });

    test(`can't query admin users`, async () => {
      const response = await app.get(`/api/users?role=System Admin`).set('bearer', email);
      expectForbidden(response);
    });

    test(`can't query custom quota users`, async () => {
      const response = await app.get(`/api/users?quota=custom`).set('bearer', email);
      expectForbidden(response);
    });
  });
});