This repository has been archived by the owner on May 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement backup per did feature (#53)
* Provider layer * Service layer * Client layer * Use authManager to getBackup
- Loading branch information
Showing
13 changed files
with
231 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { IpfsPinnerProvider } from '@rsksmart/ipfs-cpinner-provider' | ||
import { deleteDatabase, resetDatabase, setupDataVaultClient, startService, testTimestamp } from './util' | ||
import { Server } from 'http' | ||
import { Connection } from 'typeorm' | ||
import MockDate from 'mockdate' | ||
import localStorageMockFactory from './localStorageMockFactory' | ||
|
||
describe('get backup', function (this: { | ||
server: Server, | ||
dbConnection: Connection, | ||
ipfsPinnerProvider: IpfsPinnerProvider, | ||
serviceUrl: string, | ||
serviceDid: string | ||
}) { | ||
const dbName = 'get-backup.sqlite' | ||
|
||
beforeAll(async () => { | ||
const { server, serviceUrl, ipfsPinnerProvider, dbConnection, serviceDid } = await startService(dbName, 4608) | ||
this.server = server | ||
this.ipfsPinnerProvider = ipfsPinnerProvider | ||
this.dbConnection = dbConnection | ||
this.serviceUrl = serviceUrl | ||
this.serviceDid = serviceDid | ||
}) | ||
|
||
afterAll(async () => { | ||
this.server.close() | ||
await deleteDatabase(this.dbConnection, dbName) | ||
}) | ||
|
||
beforeEach(() => { | ||
MockDate.set(testTimestamp) | ||
global.localStorage = localStorageMockFactory() | ||
}) | ||
|
||
afterEach(async () => { | ||
MockDate.reset() | ||
await resetDatabase(this.dbConnection) | ||
}) | ||
|
||
test('should return an empty object if no data saved', async () => { | ||
const { dataVaultClient } = await setupDataVaultClient(this.serviceUrl, this.serviceDid) | ||
|
||
const backup = await dataVaultClient.getBackup() | ||
|
||
expect(backup).toEqual([]) | ||
}) | ||
|
||
test('should return the saved data', async () => { | ||
const { dataVaultClient, did } = await setupDataVaultClient(this.serviceUrl, this.serviceDid) | ||
|
||
const key1 = 'TheKey1' | ||
const key2 = 'TheKey2' | ||
const id1 = await this.ipfsPinnerProvider.create(did, key1, 'some content') | ||
const id2 = await this.ipfsPinnerProvider.create(did, key1, 'another content for same did') | ||
const id3 = await this.ipfsPinnerProvider.create(did, key2, 'another content for another did') | ||
|
||
const backup = await dataVaultClient.getBackup() | ||
|
||
expect(backup).toEqual([ | ||
{ key: key1, id: id1 }, | ||
{ key: key1, id: id2 }, | ||
{ key: key2, id: id3 } | ||
]) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
modules/ipfs-cpinner-service/test/api.permissioned.backup.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { Connection } from 'typeorm' | ||
import express, { Express } from 'express' | ||
import bodyParser from 'body-parser' | ||
import { IpfsPinnerProvider, ipfsPinnerProviderFactory } from '@rsksmart/ipfs-cpinner-provider' | ||
import { createSqliteConnection, deleteDatabase, ipfsApiUrl, mockedLogger } from './util' | ||
import { setupPermissionedApi } from '../src/api' | ||
import request from 'supertest' | ||
|
||
describe('backup', function (this: { | ||
app: Express, | ||
did: string | ||
dbConnection: Connection, | ||
dbName: string, | ||
provider: IpfsPinnerProvider | ||
}) { | ||
const maxStorage = 1000 | ||
|
||
const setup = async () => { | ||
this.dbConnection = await createSqliteConnection(this.dbName) | ||
this.provider = await ipfsPinnerProviderFactory({ dbConnection: this.dbConnection, ipfsApiUrl, maxStorage }) | ||
|
||
setupPermissionedApi(this.app, this.provider, mockedLogger) | ||
} | ||
|
||
beforeEach(() => { | ||
this.did = 'did:ethr:rsk:testnet:0xce83da2a364f37e44ec1a17f7f453a5e24395c70' | ||
const middleware = (req, res, next) => { | ||
req.user = { did: this.did } | ||
next() | ||
} | ||
this.app = express() | ||
this.app.use(bodyParser.json()) | ||
this.app.use(middleware) | ||
}) | ||
|
||
afterEach(() => deleteDatabase(this.dbConnection, this.dbName)) | ||
|
||
it('should return an empty array if nothing created', async () => { | ||
this.dbName = 'backup-1.sqlite' | ||
await setup() | ||
|
||
const { body } = await request(this.app).get('/backup').expect(200) | ||
|
||
expect(body).toEqual([]) | ||
}) | ||
|
||
it('should return an array with the just created content', async () => { | ||
this.dbName = 'backup-2.sqlite' | ||
await setup() | ||
|
||
const key = 'TheKey' | ||
const id = await this.provider.create(this.did, key, 'a content') | ||
|
||
const { body } = await request(this.app).get('/backup').expect(200) | ||
|
||
expect(body).toEqual([{ key, id }]) | ||
}) | ||
|
||
it('should return information related to the logged did even if there is data related to other did', async () => { | ||
this.dbName = 'backup-3.sqlite' | ||
await setup() | ||
|
||
const anotherDid = 'did:ethr:rsk:testnet:0xce83da2a364f37e44ec1a17f7f453a5e24395c65' | ||
await this.provider.create(anotherDid, 'AnotherKey', 'a content') | ||
|
||
const key = 'TheKey' | ||
const id = await this.provider.create(this.did, key, 'another content') | ||
|
||
const { body } = await request(this.app).get('/backup').expect(200) | ||
|
||
expect(body).toEqual([{ key, id }]) | ||
}) | ||
}) |