Olivier Massot 4 месяцев назад
Родитель
Сommit
e074d9b388

+ 1 - 1
services/data/normalizer/hydraNormalizer.ts

@@ -171,7 +171,7 @@ class HydraNormalizer {
         continue
       }
 
-      const targetEntity = iriEncodedFields[field].$entity()
+      const targetEntity = iriEncodedFields[field].entity
 
       if (_.isArray(value)) {
         instance[field] = value.map((iri: string) => {

+ 3 - 2
services/utils/urlUtils.ts

@@ -142,11 +142,12 @@ const UrlUtils = {
    * @see https://api-platform.com/docs/admin/handling-relations/
    */
   makeIRI(entity: string, id: number | string) {
+    const originalId = id
     if (_.isString(id)) {
       id = parseInt(id)
     }
-    if (!_.isNumber(id)) {
-      throw new TypeError('Invalid id : ' + id)
+    if (!_.isNumber(id) || _.isNaN(id)) {
+      throw new TypeError('Invalid id : ' + originalId)
     }
     return `/api/${entity}/${id}`
   },

+ 1 - 1
tests/units/services/data/apiRequestService.test.ts

@@ -59,7 +59,7 @@ describe('get', () => {
       HTTP_METHOD.GET,
       'https://myapi.com/api/item',
       null,
-      { a: '1' },
+      { a: 1 },
       { b: '2' },
     )
   })

+ 3 - 3
tests/units/services/data/entityManager.test.ts

@@ -145,7 +145,7 @@ describe('getModelFor', () => {
     expect(model!.entity).toEqual('my-model')
   })
   test('non existing model', async () => {
-    expect(
+    await expect(
       async () => await entityManager.getModelFor('non-existing-model'),
     ).rejects.toThrowError(
       "No model found for entity name 'non-existing-model'",
@@ -165,8 +165,8 @@ describe('getModelFromIri', () => {
     console.log(result)
     expect(result).toEqual(DummyApiResource)
   })
-  test('invalide Iri', () => {
-    expect(
+  test('invalide Iri', async () => {
+    await expect(
       async () => await entityManager.getModelFromIri('/invalid'),
     ).rejects.toThrowError('cannot parse the IRI')
   })

+ 49 - 91
tests/units/services/data/normalizer/hydraNormalizer.test.ts

@@ -122,6 +122,14 @@ describe('denormalize', () => {
 })
 
 describe('getData', () => {
+  // @ts-ignore
+  const initialDenormalizeItem = HydraNormalizer.denormalizeItem
+
+  afterEach(() => {
+    // @ts-ignore
+    HydraNormalizer.denormalizeItem = initialDenormalizeItem
+  })
+
   test('With collection', () => {
     const data = {
       '@context': '/api/contexts/Foo',
@@ -130,8 +138,13 @@ describe('getData', () => {
       member: ['foo'],
     }
 
+    const model = DummyApiModel
+
+    // @ts-ignore
+    HydraNormalizer.denormalizeItem = vi.fn((data, model) => data)
+
     // @ts-ignore
-    expect(HydraNormalizer.getData(data)).toEqual(['foo'])
+    expect(HydraNormalizer.getData(data, model)).toEqual(['foo'])
   })
 
   test('With item', () => {
@@ -142,8 +155,13 @@ describe('getData', () => {
       param1: 'a',
     }
 
+    const model = DummyApiModel
+
+    // @ts-ignore
+    HydraNormalizer.denormalizeItem = vi.fn((data, model) => data)
+
     // @ts-ignore
-    expect(HydraNormalizer.getData(data)).toEqual(data)
+    expect(HydraNormalizer.getData(data, model)).toEqual(data)
   })
 })
 
@@ -152,16 +170,16 @@ describe('getMetadata', () => {
     const data = {
       '@context': '/api/contexts/Foo',
       '@id': '/api/foo',
-      '@type': 'hydra:Collection',
-      'hydra:member': ['foo'],
-      'hydra:totalItems': 10,
-      'hydra:view': {
+      '@type': 'Collection',
+      'member': ['foo'],
+      'totalItems': 10,
+      'view': {
         '@id': '/api/foo?page=3',
-        '@type': 'hydra:PartialCollectionView',
-        'hydra:first': '/api/foo?page=1',
-        'hydra:last': '/api/foo?page=5',
-        'hydra:next': '/api/foo?page=4',
-        'hydra:previous': '/api/foo?page=2',
+        '@type': 'PartialCollectionView',
+        'first': '/api/foo?page=1',
+        'last': '/api/foo?page=5',
+        'next': '/api/foo?page=4',
+        'previous': '/api/foo?page=2',
       },
     }
 
@@ -179,13 +197,13 @@ describe('getMetadata', () => {
     const data = {
       '@context': '/api/contexts/Foo',
       '@id': '/api/foo',
-      '@type': 'hydra:Collection',
-      'hydra:member': ['foo'],
-      'hydra:totalItems': 10,
-      'hydra:view': {
+      '@type': 'Collection',
+      'member': ['foo'],
+      'totalItems': 10,
+      'view': {
         '@id': '/api/foo?page=3',
-        '@type': 'hydra:PartialCollectionView',
-        'hydra:first': '/api/foo?page=1',
+        '@type': 'PartialCollectionView',
+        'first': '/api/foo?page=1',
       },
     }
 
@@ -234,20 +252,18 @@ describe('denormalizeItem', () => {
     }
 
     const model = DummyApiModel
-    const expected = new DummyApiModel(item)
-
-    // @ts-ignore
-    HydraNormalizer.denormalizeEntity = vi.fn((model, item) => {
-      return expected
+    const expected = new DummyApiModel({
+      '@id': '/api/dummyModel/1',
+      id: 1,
+      name: 'foo',
+      oneToOneRelation: null,
+      oneToManyRelation: [],
     })
 
     // @ts-ignore
     const result = HydraNormalizer.denormalizeItem(item, model)
 
     expect(result).toEqual(expected)
-
-    // @ts-ignore
-    expect(HydraNormalizer.denormalizeEntity).toHaveBeenCalledWith(model, item)
   })
 
   test('with no @id prop', () => {
@@ -256,17 +272,10 @@ describe('denormalizeItem', () => {
       name: 'foo',
     }
 
-    console.error = vi.fn((msg) => {})
-
     // @ts-ignore
     const result = HydraNormalizer.denormalizeItem(item)
 
     expect(result).toEqual(item)
-
-    expect(console.error).toHaveBeenCalledWith(
-      'De-normalization error : the item is not hydra formatted',
-      item,
-    )
   })
 
   test('with enum', () => {
@@ -277,18 +286,10 @@ describe('denormalizeItem', () => {
       C: 3,
     }
 
-    // @ts-ignore
-    HydraNormalizer.denormalizeEnum = vi.fn((item) => {
-      return item
-    })
-
     // @ts-ignore
     const result = HydraNormalizer.denormalizeItem(item)
 
     expect(result).toEqual(item)
-
-    // @ts-ignore
-    expect(HydraNormalizer.denormalizeEnum).toHaveBeenCalledWith(item)
   })
 
   test('with unparsable iri', () => {
@@ -298,75 +299,32 @@ describe('denormalizeItem', () => {
       name: 'foo',
     }
 
-    // @ts-ignore
-    HydraNormalizer.parseEntityIRI = vi.fn((iri) => {
-      throw new Error('parsing error')
-    })
-
-    console.error = vi.fn((msg) => {})
-
-    // @ts-ignore
-    const result = HydraNormalizer.denormalizeItem(item)
-
-    expect(result).toEqual(item)
-
-    // @ts-ignore
-    expect(console.error).toHaveBeenCalledWith(
-      'De-normalization error : could not parse the IRI',
-      item,
-    )
+    expect(
+      // @ts-ignore
+      () => HydraNormalizer.denormalizeItem(item),
+    ).toThrowError()
   })
 
   test('With valid iri and existing model', () => {
-
-    //@ts-expect-error problème de typage sans conséquence
-    HydraNormalizer.models = { dummyModel: DummyApiModel }
-
     const item = {
       '@id': '/api/dummyModel/1',
       id: 1,
       name: 'foo',
     }
 
-    // @ts-ignore
-    HydraNormalizer.parseEntityIRI = vi.fn((iri) => {
-      return { entity: 'dummyModel' }
-    })
-
-    const expected = new DummyApiModel(item)
-
-    // @ts-ignore
-    HydraNormalizer.denormalizeEntity = vi.fn((model, item) => {
-      return expected
-    })
-
-    // @ts-ignore
-    const result = HydraNormalizer.denormalizeItem(item)
-
-    expect(result).toEqual(expected)
-
-    // @ts-ignore
-    expect(HydraNormalizer.denormalizeEntity).toHaveBeenCalledWith(
-      DummyApiModel,
-      item,
-    )
+    expect(
+      // @ts-ignore
+      () => HydraNormalizer.denormalizeItem(item),
+    ).toThrowError()
   })
 
   test('With valid iri and un-existing model', () => {
-    //@ts-expect-error problème de typage sans conséquence
-    HydraNormalizer.models = { dummyModel: DummyApiModel }
-
     const item = {
       '@id': '/api/unknownModel/1',
       id: 1,
       name: 'foo',
     }
 
-    // @ts-ignore
-    HydraNormalizer.parseEntityIRI = vi.fn((iri) => {
-      return 'unknownModel'
-    })
-
     expect(
       // @ts-ignore
       () => HydraNormalizer.denormalizeItem(item),

+ 3 - 6
tests/units/services/rights/abilityBuilder.test.ts

@@ -440,8 +440,7 @@ describe('execAndValidateCondition', () => {
   })
 
   test('organizationHasWebsite', () => {
-    // @ts-expect-error testing with intentionally wrong type
-    organizationProfile.getWebsite = true
+    organizationProfile.getWebsite = 'https://example.com'
     expect(
     // @ts-expect-error accessing protected method for testing purposes
       abilityBuilder.execAndValidateCondition({
@@ -449,8 +448,7 @@ describe('execAndValidateCondition', () => {
       }),
     ).toBeTruthy()
 
-    // @ts-expect-error testing with intentionally wrong type
-    organizationProfile.getWebsite = false
+    organizationProfile.getWebsite = null
     expect(
     // @ts-expect-error accessing protected method for testing purposes
       abilityBuilder.execAndValidateCondition({
@@ -460,8 +458,7 @@ describe('execAndValidateCondition', () => {
   })
 
   test('with expected result', () => {
-    // @ts-expect-error testing with intentionally wrong type
-    organizationProfile.getWebsite = true
+    organizationProfile.getWebsite = 'https://example.com'
 
     expect(
       // @ts-expect-error accessing protected method for testing purposes