|
| 1 | +import type { HttpError } from 'react-admin'; |
| 2 | +import fetchMock from 'jest-fetch-mock'; |
| 3 | +import fetchHydra from './fetchHydra.js'; |
| 4 | +import schemaAnalyzer from './schemaAnalyzer.js'; |
| 5 | + |
| 6 | +fetchMock.enableMocks(); |
| 7 | + |
| 8 | +const headers = { |
| 9 | + 'Content-Type': 'application/ld+json; charset=utf-8', |
| 10 | + Link: '<http://localhost/docs.jsonld>; rel="http://www.w3.org/ns/hydra/core#apiDocumentation"', |
| 11 | +}; |
| 12 | + |
| 13 | +test.each([ |
| 14 | + [ |
| 15 | + 'ld+json', |
| 16 | + { |
| 17 | + '@context': '/contexts/ConstraintViolationList', |
| 18 | + '@type': 'ConstraintViolationList', |
| 19 | + 'hydra:title': 'An error occurred', |
| 20 | + 'hydra:description': |
| 21 | + 'plainPassword: Password must be at least 6 characters long.', |
| 22 | + violations: [ |
| 23 | + { |
| 24 | + propertyPath: 'plainPassword', |
| 25 | + message: 'Password must be at least 6 characters long.', |
| 26 | + }, |
| 27 | + ], |
| 28 | + }, |
| 29 | + { plainPassword: 'Password must be at least 6 characters long.' }, |
| 30 | + ], |
| 31 | + [ |
| 32 | + 'problem+json', |
| 33 | + { |
| 34 | + '@id': '\\/validation_errors\\/6b3befbc-2f01-4ddf-be21-b57898905284', |
| 35 | + '@type': 'ConstraintViolationList', |
| 36 | + status: 422, |
| 37 | + violations: [ |
| 38 | + { |
| 39 | + propertyPath: 'entitlements', |
| 40 | + message: |
| 41 | + 'At least one product must be selected if policy is restricted.', |
| 42 | + code: '6b3befbc-2f01-4ddf-be21-b57898905284', |
| 43 | + }, |
| 44 | + ], |
| 45 | + detail: |
| 46 | + 'entitlements: At least one product must be selected if policy is restricted.', |
| 47 | + 'hydra:title': 'An error occurred', |
| 48 | + 'hydra:description': |
| 49 | + 'entitlements: At least one product must be selected if policy is restricted.', |
| 50 | + type: '\\/validation_errors\\/6b3befbc-2f01-4ddf-be21-b57898905284', |
| 51 | + title: 'An error occurred', |
| 52 | + }, |
| 53 | + { |
| 54 | + entitlements: |
| 55 | + 'At least one product must be selected if policy is restricted.', |
| 56 | + }, |
| 57 | + ], |
| 58 | +])( |
| 59 | + '%s violation list expanding', |
| 60 | + async (format: string, resBody: object, expected: object) => { |
| 61 | + fetchMock.mockResponses( |
| 62 | + [ |
| 63 | + JSON.stringify(resBody), |
| 64 | + { |
| 65 | + status: 422, |
| 66 | + statusText: '422 Unprocessable Content', |
| 67 | + headers: { |
| 68 | + ...headers, |
| 69 | + 'Content-Type': `application/${format}; charset=utf-8`, |
| 70 | + }, |
| 71 | + }, |
| 72 | + ], |
| 73 | + [ |
| 74 | + JSON.stringify({ |
| 75 | + '@context': { |
| 76 | + '@vocab': 'http://localhost/docs.jsonld#', |
| 77 | + hydra: 'http://www.w3.org/ns/hydra/core#', |
| 78 | + }, |
| 79 | + }), |
| 80 | + { |
| 81 | + status: 200, |
| 82 | + statusText: 'OK', |
| 83 | + headers, |
| 84 | + }, |
| 85 | + ], |
| 86 | + ); |
| 87 | + |
| 88 | + let violations; |
| 89 | + try { |
| 90 | + await fetchHydra(new URL('http://localhost/users')); |
| 91 | + } catch (error) { |
| 92 | + violations = schemaAnalyzer().getSubmissionErrors(error as HttpError); |
| 93 | + } |
| 94 | + expect(violations).toStrictEqual(expected); |
| 95 | + }, |
| 96 | +); |
0 commit comments