Socialify

Folder ..

Viewing encryption.test.js
50 lines (40 loc) • 1.3 KB

 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
import { hash, encrypt, decrypt } from 'main/application/helpers/encription'

const cryptor = {
  encrypt: jest.fn(() => 'encrypted data'),
  decrypt: jest.fn(() => '{"entries":[{"a":"b"}]}')
}

describe('Encryption helpers', () => {
  describe('#hash', () => {
    test('returns hashed value', () => {
      expect(hash('password')).toEqual(
        'sQnzu7wkTrgkQZF+0G1hi5AI3Qmzvv0bXgc5THBqi7mAsdd4Xll27ASbRt9fEyavWi6m0QP9B8lThf+rDKy8hg=='
      )
    })
  })

  describe('#encrypt', () => {
    const data = { entries: [{ a: 'b' }] }
    let result

    beforeEach(() => {
      result = encrypt(data, cryptor)
    })

    test('calls cryptor encrypt method', () => {
      expect(cryptor.encrypt).toHaveBeenCalledWith('{"entries":[{"a":"b"}]}')
    })

    test('returns ecnrypted data in base64 format', () => {
      expect(result).toEqual('ZW5jcnlwdGVkIGRhdGE=')
    })
  })

  describe('#decrypt', () => {
    const data = 'ZW5jcnlwdGVkIGRhdGE='
    let result

    beforeEach(() => {
      result = decrypt(data, cryptor)
    })

    test('calls cryptor encrypt method', () => {
      expect(cryptor.decrypt).toHaveBeenCalledWith('encrypted data')
    })

    test('returns ecnrypted data in base64 format', () => {
      expect(result).toEqual({ entries: [{ a: 'b' }] })
    })
  })
})