Spaces:
Sleeping
Sleeping
File size: 24,506 Bytes
2e6d0d7 62e3094 75a60d6 2e6d0d7 75a60d6 2e6d0d7 75a60d6 ad871c9 75a60d6 ad871c9 75a60d6 ad871c9 75a60d6 ad871c9 75a60d6 eab7289 75a60d6 eab7289 2e6d0d7 7db7c77 2e6d0d7 e9d5bbe 2e6d0d7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 | import { beforeAll, afterAll, afterEach, describe, it, expect, vi } from 'vitest';
import { startServer, stopServer, request } from '../helpers';
beforeAll(async () => {
process.env.ADMIN_KEY = 'integration-test-admin-key';
await startServer();
}, 20000);
afterAll(() => {
stopServer();
});
afterEach(() => {
vi.unstubAllGlobals();
});
describe('Health & Root', () => {
it('GET /health returns 200 with server status', async () => {
const res = await request('/health');
expect(res.status).toBe(200);
expect(res.body.status).toBe('ok');
expect(res.body.tables).toEqual(
expect.arrayContaining(['users', 'posts', 'comments', 'albums', 'photos', 'todos']),
);
expect(res.body.rateLimit).toBeDefined();
expect(res.body.rateLimit.enabled).toBe(false);
});
it('GET /api/health works as alias', async () => {
const res = await request('/api/health');
expect(res.status).toBe(200);
expect(res.body.status).toBe('ok');
});
it('GET /health/ works with trailing slash', async () => {
const res = await request('/health/');
expect(res.status).toBe(200);
expect(res.body.status).toBe('ok');
});
it('GET /api/health/ works with trailing slash', async () => {
const res = await request('/api/health/');
expect(res.status).toBe(200);
expect(res.body.status).toBe('ok');
});
it('GET / returns API info', async () => {
const res = await request('/');
expect(res.status).toBe(200);
expect(res.body.message).toContain('JSON API Server');
expect(res.body.version).toBe('1.0.0');
expect(res.body.endpoints).toBeInstanceOf(Array);
});
it('GET /api works as alias', async () => {
const res = await request('/api');
expect(res.status).toBe(200);
expect(res.body.version).toBe('1.0.0');
});
it('GET /api/ works with trailing slash', async () => {
const res = await request('/api/');
expect(res.status).toBe(200);
expect(res.body.version).toBe('1.0.0');
});
});
describe('CORS', () => {
it('OPTIONS returns 204 with CORS headers', async () => {
const res = await request('/api/users', { method: 'OPTIONS' });
expect(res.status).toBe(204);
expect(res.headers['access-control-allow-origin']).toBe('*');
});
});
describe('List resources', () => {
it('GET /api/users returns all users', async () => {
const res = await request('/api/users');
expect(res.status).toBe(200);
expect(Array.isArray(res.body)).toBe(true);
expect(res.body.length).toBe(2);
expect(res.body[0]).toHaveProperty('name');
});
it('GET /api/posts returns all posts', async () => {
const res = await request('/api/posts');
expect(res.status).toBe(200);
expect(Array.isArray(res.body)).toBe(true);
expect(res.body.length).toBe(3);
});
it('GET /api/comments returns all comments', async () => {
const res = await request('/api/comments');
expect(res.status).toBe(200);
expect(res.body.length).toBe(3);
});
it('GET /api/albums returns all albums', async () => {
const res = await request('/api/albums');
expect(res.status).toBe(200);
expect(res.body.length).toBe(3);
});
it('GET /api/photos returns all photos', async () => {
const res = await request('/api/photos');
expect(res.status).toBe(200);
expect(res.body.length).toBe(2);
});
it('GET /api/todos returns all todos', async () => {
const res = await request('/api/todos');
expect(res.status).toBe(200);
expect(res.body.length).toBe(3);
});
});
describe('Get single resource', () => {
it('GET /api/users/1 returns the user', async () => {
const res = await request('/api/users/1');
expect(res.status).toBe(200);
expect(res.body.id).toBe(1);
expect(res.body.name).toBe('Alice Johnson');
expect(res.body.username).toBe('alice');
expect(res.body.email).toBe('alice@example.com');
expect(typeof res.body.address).toBe('object');
expect(typeof res.body.company).toBe('object');
});
it('GET /api/users/2 returns another user', async () => {
const res = await request('/api/users/2');
expect(res.status).toBe(200);
expect(res.body.name).toBe('Bob Smith');
});
it('GET /api/posts/1 returns fields correctly', async () => {
const res = await request('/api/posts/1');
expect(res.status).toBe(200);
expect(res.body.userId).toBe(1);
expect(res.body.title).toBe('First Post');
});
it('GET /api/todos/1 returns completed as boolean', async () => {
const res = await request('/api/todos/1');
expect(res.status).toBe(200);
expect(res.body.completed).toBe(false);
});
it('GET /api/todos/2 returns completed as boolean (true)', async () => {
const res = await request('/api/todos/2');
expect(res.status).toBe(200);
expect(res.body.completed).toBe(true);
});
});
describe('404 handling', () => {
it('GET /api/users/9999 returns 404', async () => {
const res = await request('/api/users/9999');
expect(res.status).toBe(404);
expect(res.body.error).toBeDefined();
});
it('GET /api/unknown returns 404', async () => {
const res = await request('/api/unknown');
expect(res.status).toBe(404);
expect(res.body.error).toContain('Unknown route');
});
it('GET /users without /api prefix returns 404', async () => {
const res = await request('/users');
expect(res.status).toBe(404);
expect(res.body.error).toContain('Unknown route');
});
it('GET /api/users/ with trailing slash still works', async () => {
const res = await request('/api/users/');
expect(res.status).toBe(200);
expect(Array.isArray(res.body)).toBe(true);
});
});
describe('400 handling', () => {
it('GET /api/users/abc returns 400 for invalid ID format', async () => {
const res = await request('/api/users/abc');
expect(res.status).toBe(400);
expect(res.body.error).toBe('Invalid ID format');
});
});
describe('Filtering', () => {
it('GET /api/todos?userId=1 filters by userId', async () => {
const res = await request('/api/todos?userId=1');
expect(res.status).toBe(200);
expect(res.body.length).toBe(2);
res.body.forEach((t) => expect(t.userId).toBe(1));
});
it('GET /api/todos?userId=1&completed=false supports multiple filters', async () => {
const res = await request('/api/todos?userId=1&completed=false');
expect(res.status).toBe(200);
expect(res.body.length).toBe(1);
expect(res.body[0].title).toBe('Buy groceries');
expect(res.body[0].completed).toBe(false);
});
it('GET /api/todos?completed=true returns only completed', async () => {
const res = await request('/api/todos?completed=true');
expect(res.status).toBe(200);
expect(res.body.every((t) => t.completed === true)).toBe(true);
});
it('GET /api/posts?userId=2 filters by user', async () => {
const res = await request('/api/posts?userId=2');
expect(res.status).toBe(200);
expect(res.body.length).toBe(1);
expect(res.body[0].title).toBe('Bobs Post');
});
});
describe('Nested routes', () => {
it('GET /api/users/1/posts returns user posts', async () => {
const res = await request('/api/users/1/posts');
expect(res.status).toBe(200);
expect(res.body.length).toBe(2);
res.body.forEach((p) => expect(p.userId).toBe(1));
});
it('GET /api/users/2/posts returns other user posts', async () => {
const res = await request('/api/users/2/posts');
expect(res.status).toBe(200);
expect(res.body.length).toBe(1);
});
it('GET /api/posts/1/comments returns post comments', async () => {
const res = await request('/api/posts/1/comments');
expect(res.status).toBe(200);
expect(res.body.length).toBe(2);
res.body.forEach((c) => expect(c.postId).toBe(1));
});
it('GET /api/posts/2/comments returns other post comments', async () => {
const res = await request('/api/posts/2/comments');
expect(res.status).toBe(200);
expect(res.body.length).toBe(1);
});
it('GET /api/users/1/albums returns user albums', async () => {
const res = await request('/api/users/1/albums');
expect(res.status).toBe(200);
expect(res.body.length).toBe(2);
res.body.forEach((a) => expect(a.userId).toBe(1));
});
it('GET /api/users/1/todos returns user todos', async () => {
const res = await request('/api/users/1/todos');
expect(res.status).toBe(200);
expect(res.body.length).toBe(2);
});
it('GET /api/albums/1/photos returns album photos', async () => {
const res = await request('/api/albums/1/photos');
expect(res.status).toBe(200);
expect(res.body.length).toBe(2);
res.body.forEach((p) => expect(p.albumId).toBe(1));
});
it('GET /api/users/9999/posts returns 404 when parent not found', async () => {
const res = await request('/api/users/9999/posts');
expect(res.status).toBe(404);
});
it('GET /api/users/1/unknown returns 404 for unknown nested route', async () => {
const res = await request('/api/users/1/unknown');
expect(res.status).toBe(404);
});
});
describe('POST — Create', () => {
let createdId;
it('POST /api/posts creates a new post', async () => {
const res = await request('/api/posts', {
method: 'POST',
body: { userId: 1, title: 'New Post', body: 'Created via test' },
});
expect(res.status).toBe(201);
expect(res.body.title).toBe('New Post');
expect(res.body.userId).toBe(1);
expect(res.body.id).toBeGreaterThan(0);
createdId = res.body.id;
});
it('Created post is retrievable via GET', async () => {
const res = await request('/api/posts/' + createdId);
expect(res.status).toBe(200);
expect(res.body.title).toBe('New Post');
});
it('POST with explicit id creates with that id', async () => {
const res = await request('/api/posts', {
method: 'POST',
body: { id: 100, userId: 2, title: 'Custom ID', body: 'Body' },
});
expect(res.status).toBe(201);
expect(res.body.id).toBe(100);
});
it('POST with invalid JSON body returns 400', async () => {
const res = await request('/api/posts', {
method: 'POST',
rawBody: 'not-json',
headers: { 'Content-Type': 'text/plain' },
});
expect(res.status).toBe(400);
expect(res.body.error).toBe('Invalid JSON body');
});
});
describe('PATCH — Partial update', () => {
it('PATCH /api/posts/1 updates title only', async () => {
const res = await request('/api/posts/1', {
method: 'PATCH',
body: { title: 'Patched Title' },
});
expect(res.status).toBe(200);
expect(res.body.title).toBe('Patched Title');
expect(res.body.body).toBe('This is the body of the first post');
});
it('PATCH without id returns 400', async () => {
const res = await request('/api/posts', { method: 'PATCH', body: {} });
expect(res.status).toBe(400);
});
it('PATCH non-existent returns 404', async () => {
const res = await request('/api/posts/9999', { method: 'PATCH', body: {} });
expect(res.status).toBe(404);
});
});
describe('PUT — Full replacement', () => {
it('PUT /api/posts/2 replaces the resource', async () => {
const res = await request('/api/posts/2', {
method: 'PUT',
body: { userId: 2, title: 'Replaced', body: 'Completely new content' },
});
expect(res.status).toBe(200);
expect(res.body.title).toBe('Replaced');
expect(res.body.body).toBe('Completely new content');
expect(res.body.userId).toBe(2);
});
it('PUT without id returns 400', async () => {
const res = await request('/api/posts', { method: 'PUT', body: {} });
expect(res.status).toBe(400);
});
it('PUT non-existent returns 404', async () => {
const res = await request('/api/posts/9999', { method: 'PUT', body: {} });
expect(res.status).toBe(404);
});
});
describe('DELETE', () => {
let deleteId;
it('DELETE /api/posts/3 deletes the resource', async () => {
const res = await request('/api/posts/3', { method: 'DELETE' });
expect(res.status).toBe(200);
});
it('Deleted resource returns 404', async () => {
const res = await request('/api/posts/3');
expect(res.status).toBe(404);
});
it('DELETE without id returns 400', async () => {
const res = await request('/api/posts', { method: 'DELETE' });
expect(res.status).toBe(400);
});
it('DELETE non-existent returns 404', async () => {
const res = await request('/api/posts/9999', { method: 'DELETE' });
expect(res.status).toBe(404);
});
});
describe('Method not allowed', () => {
it('TRACE returns 405', async () => {
const res = await request('/api/users', { method: 'TRACE' });
expect(res.status).toBe(405);
expect(res.body.error).toContain('TRACE');
});
});
describe('Response headers', () => {
it('Includes CORS headers on GET', async () => {
const res = await request('/api/users');
expect(res.headers['access-control-allow-origin']).toBe('*');
expect(res.headers['content-type']).toContain('application/json');
expect(res.headers['x-powered-by']).toContain('json-api-server');
});
it('Rate limiting is disabled — no rate limit headers', async () => {
const res = await request('/api/users');
expect(res.headers['x-ratelimit-limit']).toBeUndefined();
});
});
describe('CORS headers on error responses', () => {
it('404 response includes CORS headers', async () => {
const res = await request('/api/users/9999');
expect(res.status).toBe(404);
expect(res.headers['access-control-allow-origin']).toBe('*');
});
});
describe('Pagination', () => {
it('GET /api/posts?_page=1&_limit=2 returns first page', async () => {
const res = await request('/api/posts?_page=1&_limit=2');
expect(res.status).toBe(200);
expect(res.body.length).toBe(2);
expect(res.body[0].id).toBe(1);
expect(res.body[1].id).toBe(2);
});
it('GET /api/posts?_page=2&_limit=2 returns second page', async () => {
const res = await request('/api/posts?_page=2&_limit=2');
expect(res.status).toBe(200);
expect(res.body.length).toBe(2);
expect(res.body[0].id).toBe(4);
expect(res.body[1].id).toBe(100);
});
it('GET /api/posts?_start=1&_end=3 returns sliced results', async () => {
const res = await request('/api/posts?_start=1&_end=3');
expect(res.status).toBe(200);
expect(res.body.length).toBe(2);
expect(res.body[0].id).toBe(2);
expect(res.body[1].id).toBe(4);
});
it('GET /api/todos?_start=1 returns all from offset', async () => {
const res = await request('/api/todos?_start=1');
expect(res.status).toBe(200);
expect(res.body.length).toBe(2);
});
it('GET /api/posts?_limit=1 limits results', async () => {
const res = await request('/api/posts?_limit=1');
expect(res.status).toBe(200);
expect(res.body.length).toBe(1);
});
it('GET /api/posts?_page=0 returns 400', async () => {
const res = await request('/api/posts?_page=0');
expect(res.status).toBe(400);
expect(res.body.error).toBe('Invalid _page: must be a positive integer');
});
it('GET /api/posts?_page=-1 returns 400', async () => {
const res = await request('/api/posts?_page=-1');
expect(res.status).toBe(400);
expect(res.body.error).toBe('Invalid _page: must be a non-negative integer');
});
it('GET /api/posts?_limit=0 returns 400', async () => {
const res = await request('/api/posts?_limit=0');
expect(res.status).toBe(400);
expect(res.body.error).toBe('Invalid _limit: must be a positive integer');
});
it('GET /api/posts?_page=&_limit=1 handles empty string by deleting param', async () => {
const res = await request('/api/posts?_page=&_limit=1');
expect(res.status).toBe(200);
expect(res.body.length).toBe(1);
});
});
describe('Search', () => {
it('GET /api/posts?q=first searches body content', async () => {
const res = await request('/api/posts?q=first');
expect(res.status).toBe(200);
expect(res.body.length).toBe(1);
expect(res.body[0].title).toBe('Patched Title');
});
it('GET /api/todos?q=groceries finds matching todos', async () => {
const res = await request('/api/todos?q=groceries');
expect(res.status).toBe(200);
expect(res.body.length).toBe(1);
expect(res.body[0].title).toBe('Buy groceries');
});
it('GET /api/posts?q=Post&userId=1 combines search and filter', async () => {
const res = await request('/api/posts?q=Post&userId=1');
expect(res.status).toBe(200);
expect(res.body.length).toBe(2);
res.body.forEach((p) => {
expect(p.userId).toBe(1);
});
});
it('GET /api/posts?q=nonexistent returns empty array', async () => {
const res = await request('/api/posts?q=nonexistent');
expect(res.status).toBe(200);
expect(res.body.length).toBe(0);
});
});
describe('Sorting', () => {
it('GET /api/posts?_sort=title&_order=asc sorts ascending', async () => {
const res = await request('/api/posts?_sort=title&_order=asc');
expect(res.status).toBe(200);
expect(res.body.length).toBe(4);
expect(res.body[0].title).toBe('Custom ID');
expect(res.body[1].title).toBe('New Post');
expect(res.body[2].title).toBe('Patched Title');
expect(res.body[3].title).toBe('Replaced');
});
it('GET /api/posts?_sort=title&_order=desc sorts descending', async () => {
const res = await request('/api/posts?_sort=title&_order=desc');
expect(res.status).toBe(200);
expect(res.body.length).toBe(4);
expect(res.body[0].title).toBe('Replaced');
expect(res.body[1].title).toBe('Patched Title');
expect(res.body[2].title).toBe('New Post');
expect(res.body[3].title).toBe('Custom ID');
});
it('GET /api/posts?_sort=title defaults to asc', async () => {
const res = await request('/api/posts?_sort=title');
expect(res.status).toBe(200);
expect(res.body[0].title).toBe('Custom ID');
expect(res.body[1].title).toBe('New Post');
});
it('GET /api/posts?_sort=id&_order=desc&_limit=2 combines sort with pagination', async () => {
const res = await request('/api/posts?_sort=id&_order=desc&_limit=2');
expect(res.status).toBe(200);
expect(res.body.length).toBe(2);
expect(res.body[0].id).toBe(100);
expect(res.body[1].id).toBe(4);
});
});
describe('Admin routes', () => {
let adminToken = 'integration-test-admin-key';
it('GET /api/admin/settings returns 401 without Authorization header', async () => {
const res = await request('/api/admin/settings');
expect(res.status).toBe(401);
expect(res.body.error).toBeDefined();
});
it('GET /api/admin/settings returns 401 with wrong token', async () => {
const res = await request('/api/admin/settings', {
headers: { authorization: 'Bearer wrong-key' },
});
expect(res.status).toBe(401);
});
it('GET /api/admin/settings returns 200 with valid auth', async () => {
const res = await request('/api/admin/settings', {
headers: { authorization: `Bearer ${adminToken}` },
});
expect(res.status).toBe(200);
expect(Array.isArray(res.body)).toBe(true);
expect(res.body.length).toBeGreaterThan(0);
});
it('GET /api/admin/settings hides sensitive values (ADMIN_KEY, REDIS_PASSWORD)', async () => {
const res = await request('/api/admin/settings', {
headers: { authorization: `Bearer ${adminToken}` },
});
expect(res.status).toBe(200);
const adminKeySetting = res.body.find(s => s.key === 'ADMIN_KEY');
expect(adminKeySetting).toBeDefined();
expect(adminKeySetting.value).toBe('***');
const redisPwSetting = res.body.find(s => s.key === 'REDIS_PASSWORD');
expect(redisPwSetting).toBeDefined();
expect(redisPwSetting.value).toBe('***');
});
it('PATCH /api/admin/settings/:key updates a setting value', async () => {
const res = await request('/api/admin/settings/NODE_ENV', {
method: 'PATCH',
headers: { authorization: `Bearer ${adminToken}` },
body: { value: 'staging' },
});
expect(res.status).toBe(200);
expect(res.body.value).toBe('staging');
});
it('PATCH /api/admin/settings/:key with invalid body returns 400', async () => {
const res = await request('/api/admin/settings/NODE_ENV', {
method: 'PATCH',
headers: { authorization: `Bearer ${adminToken}` },
rawBody: 'not-json',
});
expect(res.status).toBe(400);
});
it('PATCH /api/admin/settings/:key for nonexistent setting returns 404', async () => {
const res = await request('/api/admin/settings/NONEXISTENT_KEY', {
method: 'PATCH',
headers: { authorization: `Bearer ${adminToken}` },
body: { value: 'test' },
});
expect(res.status).toBe(404);
});
it('Unknown admin route returns 404', async () => {
const res = await request('/api/admin/unknown', {
headers: { authorization: `Bearer ${adminToken}` },
});
expect(res.status).toBe(404);
});
it('Admin routes use cached auth token on repeated requests', async () => {
const res1 = await request('/api/admin/settings', {
headers: { authorization: `Bearer ${adminToken}` },
});
expect(res1.status).toBe(200);
const res2 = await request('/api/admin/settings', {
headers: { authorization: `Bearer ${adminToken}` },
});
expect(res2.status).toBe(200);
});
it('POST /api/admin/reset-database re-seeds successfully', async () => {
const payload = {
users: [{ id: 1, name: 'Alice', username: 'alice', email: 'alice@example.com', phone: '1', website: 'w', address: { city: 'Hanoi' }, company: { name: 'ACME' } }],
posts: [{ id: 1, userId: 1, title: 'Post', body: 'Body' }],
comments: [{ id: 1, postId: 1, name: 'Commenter', email: 'c@example.com', body: 'Comment' }],
albums: [{ id: 1, userId: 1, title: 'Album' }],
photos: [{ id: 1, albumId: 1, title: 'Photo', url: 'u', thumbnailUrl: 't' }],
todos: [{ id: 1, userId: 1, title: 'Todo', completed: true }],
};
vi.stubGlobal('fetch', async (url) => ({
ok: true,
json: async () => payload[new URL(url).pathname.slice(1)],
}));
const res = await request('/api/admin/reset-database', {
method: 'POST',
headers: { authorization: `Bearer ${adminToken}` },
body: { confirm: true },
});
expect(res.status).toBe(200);
expect(res.body.message).toContain('reset');
});
it('PATCH RATE_LIMIT_MAX updates health endpoint immediately', async () => {
const patchRes = await request('/api/admin/settings/RATE_LIMIT_MAX', {
method: 'PATCH',
headers: { authorization: `Bearer ${adminToken}` },
body: { value: '50' },
});
expect(patchRes.status).toBe(200);
const healthRes = await request('/health');
expect(healthRes.status).toBe(200);
expect(healthRes.body.rateLimit.max).toBe(50);
await request('/api/admin/settings/RATE_LIMIT_MAX', {
method: 'PATCH',
headers: { authorization: `Bearer ${adminToken}` },
body: { value: '100' },
});
});
it('PATCH RATE_LIMIT_WINDOW_MS updates health endpoint immediately', async () => {
const patchRes = await request('/api/admin/settings/RATE_LIMIT_WINDOW_MS', {
method: 'PATCH',
headers: { authorization: `Bearer ${adminToken}` },
body: { value: '30000' },
});
expect(patchRes.status).toBe(200);
const healthRes = await request('/health');
expect(healthRes.status).toBe(200);
expect(healthRes.body.rateLimit.windowMs).toBe(30000);
});
it('PATCH RATE_LIMIT_ENABLED updates health endpoint immediately', async () => {
const patchRes = await request('/api/admin/settings/RATE_LIMIT_ENABLED', {
method: 'PATCH',
headers: { authorization: `Bearer ${adminToken}` },
body: { value: 'true' },
});
expect(patchRes.status).toBe(200);
const healthRes = await request('/health');
expect(healthRes.status).toBe(200);
expect(healthRes.body.rateLimit.enabled).toBe(true);
await request('/api/admin/settings/RATE_LIMIT_ENABLED', {
method: 'PATCH',
headers: { authorization: `Bearer ${adminToken}` },
body: { value: 'false' },
});
});
it('PATCH REDIS_HOST triggers applyRedisUpdate and catches reconnect error', async () => {
const res = await request('/api/admin/settings/REDIS_HOST', {
method: 'PATCH',
headers: { authorization: `Bearer ${adminToken}` },
body: { value: '127.0.0.1' },
});
expect(res.status).toBe(200);
expect(res.body.value).toBe('127.0.0.1');
});
});
|