Determining UUIDs of players for Minecraft servers.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

72 lignes
1.7 KiB

  1. import { FastifyInstance } from 'fastify';
  2. import {
  3. describe,
  4. it,
  5. expect,
  6. beforeAll,
  7. afterAll,
  8. vi,
  9. } from 'vitest';
  10. import { constants } from 'http2';
  11. import { createServer } from '../src/server';
  12. import { addRoutes } from '../src/routes';
  13. import {
  14. UuidServiceImpl,
  15. InvalidArgumentTypeError,
  16. } from '../src/modules/uuid';
  17. describe('Example', () => {
  18. let server: FastifyInstance;
  19. const body = { username: 'username', onlineMode: false };
  20. beforeAll(() => {
  21. server = createServer();
  22. addRoutes(server);
  23. });
  24. afterAll(async () => {
  25. await server.close();
  26. });
  27. it('returns result when successful', async () => {
  28. const response = await server
  29. .inject()
  30. .post('/api/uuid')
  31. .body(body)
  32. .headers({
  33. 'Accept': 'application/json',
  34. });
  35. expect(response.statusCode).toBe(constants.HTTP_STATUS_OK);
  36. });
  37. it('returns error when given invalid inputs', async () => {
  38. vi.spyOn(UuidServiceImpl.prototype, 'getUsernameUuid').mockImplementationOnce(() => {
  39. throw new InvalidArgumentTypeError('Invalid input');
  40. });
  41. const response = await server
  42. .inject()
  43. .post('/api/uuid')
  44. .body(body)
  45. .headers({
  46. 'Accept': 'application/json',
  47. });
  48. expect(response.statusCode).toBe(constants.HTTP_STATUS_BAD_REQUEST);
  49. });
  50. it('returns error when an unexpected error occurs', async () => {
  51. vi.spyOn(UuidServiceImpl.prototype, 'getUsernameUuid').mockImplementationOnce(() => {
  52. throw new Error('Unexpected error');
  53. });
  54. const response = await server
  55. .inject()
  56. .post('/api/uuid')
  57. .body(body)
  58. .headers({
  59. 'Accept': 'application/json',
  60. });
  61. expect(response.statusCode).toBe(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR);
  62. });
  63. });