AstroCol/tests/backend/api.test.ts

135 lines
4.3 KiB
TypeScript
Raw Normal View History

2025-02-16 14:52:42 +00:00
import config from '../../config.json';
describe('/api/auth', () => {
test('GET /testAccessToken', async () => {
const token = "A.MTczOTY1ODI1NTA5MA.NjUzZTk2NzdlNWMzZDM2YjE1YmE3NGVi.hQW5wy0J1siFhTYvdU5GJw"
const response = await fetch('https://localhost:4321/api/auth/testAccessToken', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
}
});
const data = await response.json();
expect(data.code).toBe(200);
});
test('POST /generateAccessToken', async () => {
const response = await fetch('https://localhost:4321/api/auth/generateAccessToken', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${config.MASTER_ACCESSTOKEN}`
},
body: JSON.stringify({ username: "test" })
});
const data = await response.json();
expect(data.code).toBe(404);
});
});
describe('/api/fleet', () => {
test('GET /getDistance', async () => {
const response = await fetch('https://localhost:4321/api/fleet/getDistance?source=661d1163567111a5b4be8829&destination=66cf55650df1e5fe8a5c20d1', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
}
});
const data = await response.json();
expect(data.code).toBe(200);
expect(data.distance).toBe(120);
});
test('GET /getDistance (missing parameters)', async () => {
const response = await fetch('https://localhost:4321/api/fleet/getDistance', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
}
});
const data = await response.json();
expect(data.code).toBe(400);
});
test('GET /getDistance (source not found)', async () => {
const response = await fetch('https://localhost:4321/api/fleet/getDistance?source=123&destination=66cf55650df1e5fe8a5c20d1', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
}
});
const data = await response.json();
expect(data.code).toBe(404);
});
test('GET /getDistance (destination not found)', async () => {
const response = await fetch('https://localhost:4321/api/fleet/getDistance?source=661d1163567111a5b4be8829&destination=123', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
}
});
const data = await response.json();
expect(data.code).toBe(404);
});
test('GET /status', async () => {
const token = "A.MTczOTY1ODI1NTA5MA.NjUzZTk2NzdlNWMzZDM2YjE1YmE3NGVi.hQW5wy0J1siFhTYvdU5GJw"
const response = await fetch('https://localhost:4321/api/fleet/status', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
}
});
const data = await response.json();
expect(data.code).toBe(200);
});
});
describe('/api/lang', () => {
test('GET .json', async () => {
const response = await fetch('https://localhost:4321/api/lang.json', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
}
});
const data: { id: string, name: string }[] = await response.json();
expect(data.find(lang => lang.id === "en")).toBeTruthy();
});
test('GET /en.json', async () => {
const response = await fetch('https://localhost:4321/api/lang/en.json', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
}
});
const data = await response.json();
expect(data.code).toBe(200);
});
test('GET /123.json (not found)', async () => {
const response = await fetch('https://localhost:4321/api/lang/123.json', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
}
});
const data = await response.json();
expect(data.code).toBe(404);
});
});