tests: Refactor getRooms tests to use response object and update assertion helper
This commit is contained in:
parent
eaa6ef9b44
commit
30fad6995a
@ -1,6 +1,4 @@
|
|||||||
import request from 'supertest';
|
|
||||||
import { describe, it, expect, beforeAll, afterAll, afterEach } from '@jest/globals';
|
import { describe, it, expect, beforeAll, afterAll, afterEach } from '@jest/globals';
|
||||||
import { Express } from 'express';
|
|
||||||
import {
|
import {
|
||||||
createRoom,
|
createRoom,
|
||||||
deleteAllRooms,
|
deleteAllRooms,
|
||||||
@ -8,17 +6,14 @@ import {
|
|||||||
getRooms,
|
getRooms,
|
||||||
startTestServer,
|
startTestServer,
|
||||||
stopTestServer,
|
stopTestServer,
|
||||||
assertRoomsResponse
|
assertSuccessRoomsResponse
|
||||||
} from '../../../utils/helpers.js';
|
} from '../../../utils/helpers.js';
|
||||||
import { MEET_API_BASE_PATH_V1, MEET_API_KEY, API_KEY_HEADER } from '../../../../src/environment.js';
|
|
||||||
|
|
||||||
const endpoint = '/rooms';
|
|
||||||
describe('OpenVidu Meet Room API Tests', () => {
|
describe('OpenVidu Meet Room API Tests', () => {
|
||||||
let app: Express;
|
|
||||||
const validAutoDeletionDate = Date.now() + 2 * 60 * 60 * 1000; // 2 hours ahead
|
const validAutoDeletionDate = Date.now() + 2 * 60 * 60 * 1000; // 2 hours ahead
|
||||||
|
|
||||||
beforeAll(async () => {
|
beforeAll(async () => {
|
||||||
app = await startTestServer();
|
await startTestServer();
|
||||||
});
|
});
|
||||||
|
|
||||||
afterAll(async () => {
|
afterAll(async () => {
|
||||||
@ -42,10 +37,10 @@ describe('OpenVidu Meet Room API Tests', () => {
|
|||||||
roomIdPrefix: 'test-room'
|
roomIdPrefix: 'test-room'
|
||||||
});
|
});
|
||||||
|
|
||||||
const body = await getRooms();
|
const response = await getRooms();
|
||||||
const { rooms } = body;
|
const { rooms } = response.body;
|
||||||
|
|
||||||
assertRoomsResponse(body, 1, 10, false, false);
|
assertSuccessRoomsResponse(response, 1, 10, false, false);
|
||||||
expect(rooms[0].roomId).toBeDefined();
|
expect(rooms[0].roomId).toBeDefined();
|
||||||
expect(rooms[0].roomId).toContain('test-room');
|
expect(rooms[0].roomId).toContain('test-room');
|
||||||
expect(rooms[0].creationDate).toBeDefined();
|
expect(rooms[0].creationDate).toBeDefined();
|
||||||
@ -64,10 +59,10 @@ describe('OpenVidu Meet Room API Tests', () => {
|
|||||||
autoDeletionDate: validAutoDeletionDate
|
autoDeletionDate: validAutoDeletionDate
|
||||||
});
|
});
|
||||||
|
|
||||||
const body = await getRooms({ fields: 'roomId,createdAt' });
|
const response = await getRooms({ fields: 'roomId,createdAt' });
|
||||||
const { rooms } = body;
|
const { rooms } = response.body;
|
||||||
|
|
||||||
assertRoomsResponse(body, 1, 10, false, false);
|
assertSuccessRoomsResponse(response, 1, 10, false, false);
|
||||||
|
|
||||||
expect(rooms[0].roomId).toBeDefined();
|
expect(rooms[0].roomId).toBeDefined();
|
||||||
expect(rooms[0].roomId).toContain('test-room');
|
expect(rooms[0].roomId).toContain('test-room');
|
||||||
@ -92,73 +87,59 @@ describe('OpenVidu Meet Room API Tests', () => {
|
|||||||
});
|
});
|
||||||
await Promise.all(promises);
|
await Promise.all(promises);
|
||||||
|
|
||||||
let body = await getRooms({ maxItems: 3 });
|
let response = await getRooms({ maxItems: 3 });
|
||||||
const { pagination } = body;
|
const { pagination } = response.body;
|
||||||
|
|
||||||
assertRoomsResponse(body, 3, 3, true, true);
|
assertSuccessRoomsResponse(response, 3, 3, true, true);
|
||||||
|
|
||||||
const nextPageToken = pagination.nextPageToken;
|
const nextPageToken = pagination.nextPageToken;
|
||||||
body = await getRooms({ maxItems: 3, nextPageToken });
|
response = await getRooms({ maxItems: 3, nextPageToken });
|
||||||
|
|
||||||
assertRoomsResponse(body, 3, 3, false, false);
|
assertSuccessRoomsResponse(response, 3, 3, false, false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should capped maxItems to the maximum allowed', async () => {
|
it('should capped maxItems to the maximum allowed', async () => {
|
||||||
const body = await getRooms({ maxItems: 101 });
|
const response = await getRooms({ maxItems: 101 });
|
||||||
|
|
||||||
assertRoomsResponse(body, 0, 100, false, false);
|
assertSuccessRoomsResponse(response, 0, 100, false, false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should coerce a floating number to an integer for maxItems', async () => {
|
it('should coerce a floating number to an integer for maxItems', async () => {
|
||||||
const body = await getRooms({ maxItems: 12.78 });
|
const response = await getRooms({ maxItems: 12.78 });
|
||||||
|
|
||||||
assertRoomsResponse(body, 0, 12, false, false);
|
assertSuccessRoomsResponse(response, 0, 12, false, false);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('List Room Validation failures', () => {
|
describe('List Room Validation failures', () => {
|
||||||
it('should fail when maxItems is not a number', async () => {
|
it('should fail when maxItems is not a number', async () => {
|
||||||
const response = await request(app)
|
const response = await getRooms({ maxItems: 'not-a-number' });
|
||||||
.get(`${MEET_API_BASE_PATH_V1}${endpoint}`)
|
|
||||||
.set(API_KEY_HEADER, MEET_API_KEY)
|
|
||||||
.query({ maxItems: 'not-a-number' })
|
|
||||||
.expect(422);
|
|
||||||
|
|
||||||
|
expect(response.status).toBe(422);
|
||||||
expect(response.body.error).toContain('Unprocessable Entity');
|
expect(response.body.error).toContain('Unprocessable Entity');
|
||||||
// Check that the error details mention an invalid number.
|
// Check that the error details mention an invalid number.
|
||||||
expect(JSON.stringify(response.body.details)).toContain('Expected number, received nan');
|
expect(JSON.stringify(response.body.details)).toContain('Expected number, received nan');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should fail when maxItems is negative', async () => {
|
it('should fail when maxItems is negative', async () => {
|
||||||
const response = await request(app)
|
const response = await getRooms({ maxItems: -1 });
|
||||||
.get(`${MEET_API_BASE_PATH_V1}${endpoint}`)
|
|
||||||
.set(API_KEY_HEADER, MEET_API_KEY)
|
|
||||||
.query({ maxItems: -1 })
|
|
||||||
.expect(422);
|
|
||||||
|
|
||||||
console.log(response.body);
|
expect(response.status).toBe(422);
|
||||||
expect(response.body.error).toContain('Unprocessable Entity');
|
expect(response.body.error).toContain('Unprocessable Entity');
|
||||||
expect(JSON.stringify(response.body.details)).toContain('positive number');
|
expect(JSON.stringify(response.body.details)).toContain('positive number');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should fail when maxItems is zero', async () => {
|
it('should fail when maxItems is zero', async () => {
|
||||||
const response = await request(app)
|
const response = await getRooms({ maxItems: 0 });
|
||||||
.get(`${MEET_API_BASE_PATH_V1}${endpoint}`)
|
|
||||||
.set(API_KEY_HEADER, MEET_API_KEY)
|
|
||||||
.query({ maxItems: 0 })
|
|
||||||
.expect(422);
|
|
||||||
|
|
||||||
|
expect(response.status).toBe(422);
|
||||||
expect(response.body.error).toContain('Unprocessable Entity');
|
expect(response.body.error).toContain('Unprocessable Entity');
|
||||||
expect(JSON.stringify(response.body.details)).toContain('positive number');
|
expect(JSON.stringify(response.body.details)).toContain('positive number');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should fail when fields is not a string', async () => {
|
it('should fail when fields is not a string', async () => {
|
||||||
const response = await request(app)
|
const response = await getRooms({ fields: { invalid: 'data' } });
|
||||||
.get(`${MEET_API_BASE_PATH_V1}${endpoint}`)
|
expect(response.status).toBe(422);
|
||||||
.set(API_KEY_HEADER, MEET_API_KEY)
|
|
||||||
.query({ fields: { invalid: 'data' } })
|
|
||||||
.expect(422);
|
|
||||||
|
|
||||||
expect(response.body.error).toContain('Unprocessable Entity');
|
expect(response.body.error).toContain('Unprocessable Entity');
|
||||||
expect(JSON.stringify(response.body.details)).toContain('Expected string');
|
expect(JSON.stringify(response.body.details)).toContain('Expected string');
|
||||||
});
|
});
|
||||||
|
|||||||
@ -157,12 +157,7 @@ export const getRooms = async (query: Record<string, any> = {}) => {
|
|||||||
throw new Error('App instance is not defined');
|
throw new Error('App instance is not defined');
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = await request(app)
|
return await request(app).get(`${MEET_API_BASE_PATH_V1}/rooms`).set(API_KEY_HEADER, MEET_API_KEY).query(query);
|
||||||
.get(`${MEET_API_BASE_PATH_V1}/rooms`)
|
|
||||||
.set(API_KEY_HEADER, MEET_API_KEY)
|
|
||||||
.query(query)
|
|
||||||
.expect(200);
|
|
||||||
return response.body;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -177,13 +172,15 @@ export const getRooms = async (query: Record<string, any> = {}) => {
|
|||||||
* (if true, expects nextPageToken to be defined;
|
* (if true, expects nextPageToken to be defined;
|
||||||
* if false, expects nextPageToken to be undefined)
|
* if false, expects nextPageToken to be undefined)
|
||||||
*/
|
*/
|
||||||
export const assertRoomsResponse = (
|
export const assertSuccessRoomsResponse = (
|
||||||
body: any,
|
response: any,
|
||||||
expectedRoomLength: number,
|
expectedRoomLength: number,
|
||||||
expectedMaxItems: number,
|
expectedMaxItems: number,
|
||||||
expectedTruncated: boolean,
|
expectedTruncated: boolean,
|
||||||
expectedNextPageToken: boolean
|
expectedNextPageToken: boolean
|
||||||
) => {
|
) => {
|
||||||
|
const { body } = response;
|
||||||
|
expect(response.status).toBe(200);
|
||||||
expect(body).toBeDefined();
|
expect(body).toBeDefined();
|
||||||
expect(body.rooms).toBeDefined();
|
expect(body.rooms).toBeDefined();
|
||||||
expect(Array.isArray(body.rooms)).toBe(true);
|
expect(Array.isArray(body.rooms)).toBe(true);
|
||||||
@ -202,9 +199,9 @@ export const assertEmptyRooms = async () => {
|
|||||||
throw new Error('App instance is not defined');
|
throw new Error('App instance is not defined');
|
||||||
}
|
}
|
||||||
|
|
||||||
const body = await getRooms();
|
const response = await getRooms();
|
||||||
|
|
||||||
assertRoomsResponse(body, 0, 10, false, false);
|
assertSuccessRoomsResponse(response, 0, 10, false, false);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user