ソースを参照

complete unit tests

Olivier Massot 1 年間 前
コミット
cd3e1039d3

+ 9 - 4
services/utils/stringUtils.ts

@@ -1,3 +1,4 @@
+import crypto from 'crypto'
 
 export default class StringUtils {
   /**
@@ -36,10 +37,14 @@ export default class StringUtils {
   public static async hash(input: string, algorithm: string = 'SHA-256') {
     const textAsBuffer = new TextEncoder().encode(input)
 
-    const hashBuffer = await window.crypto.subtle.digest(
-      algorithm,
-      textAsBuffer,
-    )
+    const isNode =
+      typeof process !== 'undefined' &&
+      process.versions != null &&
+      process.versions.node != null
+
+    const cryptoLib = isNode ? crypto : window.crypto
+
+    const hashBuffer = await cryptoLib.subtle.digest(algorithm, textAsBuffer)
 
     const hashArray = Array.from(new Uint8Array(hashBuffer))
     return hashArray.map((item) => item.toString(16).padStart(2, '0')).join('')

+ 22 - 1
tests/units/services/data/entityManager.test.ts

@@ -28,6 +28,15 @@ class DummyApiModel extends ApiModel {
 }
 
 class TestableEntityManager extends EntityManager {
+  public _getProfileMask: () => object
+
+  public removeTempAfterPersist(
+    model: typeof ApiResource,
+    tempInstanceId: number | string,
+  ) {
+    return super.removeTempAfterPersist(model, tempInstanceId)
+  }
+
   public makeProfileHash() {
     return super.makeProfileHash()
   }
@@ -507,7 +516,7 @@ describe('persist', () => {
     // @ts-ignore
     expect(entityManager.removeTempAfterPersist).toHaveBeenCalledWith(
       DummyApiModel,
-      instance.id
+      instance.id,
     )
     // @ts-ignore
     expect(entityManager.makeProfileHash).toHaveBeenCalledTimes(1)
@@ -949,3 +958,15 @@ describe('removeTempAfterPersist', () => {
     )
   })
 })
+
+describe('makeProfileHash', () => {
+  test('simple call', async () => {
+    entityManager._getProfileMask = vi.fn(() => {
+      return { a: 1 }
+    })
+
+    expect(await entityManager.makeProfileHash()).toEqual(
+      '9f89c740ceb46d7418c924a78ac57941d5e96520',
+    )
+  })
+})

+ 19 - 0
tests/units/services/utils/objectUtils.test.ts

@@ -1,5 +1,6 @@
 import { describe, test, it, expect } from 'vitest'
 import ObjectUtils from '~/services/utils/objectUtils'
+import StringUtils from '~/services/utils/stringUtils'
 
 describe('cloneAndFlatten', () => {
   test('If the object is already flat, it should return an identical object', () => {
@@ -88,3 +89,21 @@ describe('sortObjectsByKey', () => {
   //     expect(ObjectUtils.sortObjectsByKey({})).toThrowError('Expecting an object parameter')
   // })
 })
+
+describe('hash', () => {
+  test('empty object', async () => {
+    expect(await ObjectUtils.hash({})).toBe(
+      'bf21a9e8fbc5a3846fb05b4fa0859e0917b2202f',
+    )
+  })
+  test('simple object', async () => {
+    expect(await ObjectUtils.hash({ a: 1, b: 2, c: 3 })).toBe(
+      'e7ec4a8f2309bdd4c4c57cb2adfb79c91a293597',
+    )
+  })
+  test('simple unsorted object', async () => {
+    expect(await ObjectUtils.hash({ b: 2, a: 1, c: 3 })).toBe(
+      'e7ec4a8f2309bdd4c4c57cb2adfb79c91a293597',
+    )
+  })
+})

+ 11 - 0
tests/units/services/utils/stringUtils.test.ts

@@ -21,3 +21,14 @@ describe('parseInt', () => {
     expect(StringUtils.parseInt('6')).toBe(6)
   })
 })
+
+describe('hash', () => {
+  test('simple cases', async () => {
+    expect(await StringUtils.hash('azerty')).toBe(
+      'f2d81a260dea8a100dd517984e53c56a7523d96942a834b9cdc249bd4e8c7aa9',
+    )
+    expect(await StringUtils.hash('azerty', 'SHA-1')).toBe(
+      '9cf95dacd226dcf43da376cdb6cbba7035218921',
+    )
+  })
+})