|
| 1 | +import { IpfsPinnerProvider } from '@rsksmart/ipfs-cpinner-provider' |
| 2 | +import { deleteDatabase, resetDatabase, setupDataVaultClient, startService, testTimestamp } from './util' |
| 3 | +import { Server } from 'http' |
| 4 | +import { Connection } from 'typeorm' |
| 5 | +import MockDate from 'mockdate' |
| 6 | +import localStorageMockFactory from './localStorageMockFactory' |
| 7 | + |
| 8 | +describe('get backup', function (this: { |
| 9 | + server: Server, |
| 10 | + dbConnection: Connection, |
| 11 | + ipfsPinnerProvider: IpfsPinnerProvider, |
| 12 | + serviceUrl: string, |
| 13 | + serviceDid: string |
| 14 | +}) { |
| 15 | + const dbName = 'get-backup.sqlite' |
| 16 | + |
| 17 | + beforeAll(async () => { |
| 18 | + const { server, serviceUrl, ipfsPinnerProvider, dbConnection, serviceDid } = await startService(dbName, 4608) |
| 19 | + this.server = server |
| 20 | + this.ipfsPinnerProvider = ipfsPinnerProvider |
| 21 | + this.dbConnection = dbConnection |
| 22 | + this.serviceUrl = serviceUrl |
| 23 | + this.serviceDid = serviceDid |
| 24 | + }) |
| 25 | + |
| 26 | + afterAll(async () => { |
| 27 | + this.server.close() |
| 28 | + await deleteDatabase(this.dbConnection, dbName) |
| 29 | + }) |
| 30 | + |
| 31 | + beforeEach(() => { |
| 32 | + MockDate.set(testTimestamp) |
| 33 | + global.localStorage = localStorageMockFactory() |
| 34 | + }) |
| 35 | + |
| 36 | + afterEach(async () => { |
| 37 | + MockDate.reset() |
| 38 | + await resetDatabase(this.dbConnection) |
| 39 | + }) |
| 40 | + |
| 41 | + test('should return an empty object if no data saved', async () => { |
| 42 | + const { dataVaultClient } = await setupDataVaultClient(this.serviceUrl, this.serviceDid) |
| 43 | + |
| 44 | + const backup = await dataVaultClient.getBackup() |
| 45 | + |
| 46 | + expect(backup).toEqual([]) |
| 47 | + }) |
| 48 | + |
| 49 | + test('should return the saved data', async () => { |
| 50 | + const { dataVaultClient, did } = await setupDataVaultClient(this.serviceUrl, this.serviceDid) |
| 51 | + |
| 52 | + const key1 = 'TheKey1' |
| 53 | + const key2 = 'TheKey2' |
| 54 | + const id1 = await this.ipfsPinnerProvider.create(did, key1, 'some content') |
| 55 | + const id2 = await this.ipfsPinnerProvider.create(did, key1, 'another content for same did') |
| 56 | + const id3 = await this.ipfsPinnerProvider.create(did, key2, 'another content for another did') |
| 57 | + |
| 58 | + const backup = await dataVaultClient.getBackup() |
| 59 | + |
| 60 | + expect(backup).toEqual([ |
| 61 | + { key: key1, id: id1 }, |
| 62 | + { key: key1, id: id2 }, |
| 63 | + { key: key2, id: id3 } |
| 64 | + ]) |
| 65 | + }) |
| 66 | +}) |
0 commit comments