|
| 1 | +import { Connection } from 'typeorm' |
| 2 | +import express, { Express } from 'express' |
| 3 | +import bodyParser from 'body-parser' |
| 4 | +import { IpfsPinnerProvider, ipfsPinnerProviderFactory } from '@rsksmart/ipfs-cpinner-provider' |
| 5 | +import { createSqliteConnection, deleteDatabase, ipfsApiUrl, mockedLogger } from './util' |
| 6 | +import { setupPermissionedApi } from '../src/api' |
| 7 | +import request from 'supertest' |
| 8 | + |
| 9 | +describe('backup', function (this: { |
| 10 | + app: Express, |
| 11 | + did: string |
| 12 | + dbConnection: Connection, |
| 13 | + dbName: string, |
| 14 | + provider: IpfsPinnerProvider |
| 15 | +}) { |
| 16 | + const maxStorage = 1000 |
| 17 | + |
| 18 | + const setup = async () => { |
| 19 | + this.dbConnection = await createSqliteConnection(this.dbName) |
| 20 | + this.provider = await ipfsPinnerProviderFactory({ dbConnection: this.dbConnection, ipfsApiUrl, maxStorage }) |
| 21 | + |
| 22 | + setupPermissionedApi(this.app, this.provider, mockedLogger) |
| 23 | + } |
| 24 | + |
| 25 | + beforeEach(() => { |
| 26 | + this.did = 'did:ethr:rsk:testnet:0xce83da2a364f37e44ec1a17f7f453a5e24395c70' |
| 27 | + const middleware = (req, res, next) => { |
| 28 | + req.user = { did: this.did } |
| 29 | + next() |
| 30 | + } |
| 31 | + this.app = express() |
| 32 | + this.app.use(bodyParser.json()) |
| 33 | + this.app.use(middleware) |
| 34 | + }) |
| 35 | + |
| 36 | + afterEach(() => deleteDatabase(this.dbConnection, this.dbName)) |
| 37 | + |
| 38 | + it('should return an empty array if nothing created', async () => { |
| 39 | + this.dbName = 'backup-1.sqlite' |
| 40 | + await setup() |
| 41 | + |
| 42 | + const { body } = await request(this.app).get('/backup').expect(200) |
| 43 | + |
| 44 | + expect(body).toEqual([]) |
| 45 | + }) |
| 46 | + |
| 47 | + it('should return an array with the just created content', async () => { |
| 48 | + this.dbName = 'backup-2.sqlite' |
| 49 | + await setup() |
| 50 | + |
| 51 | + const key = 'TheKey' |
| 52 | + const id = await this.provider.create(this.did, key, 'a content') |
| 53 | + |
| 54 | + const { body } = await request(this.app).get('/backup').expect(200) |
| 55 | + |
| 56 | + expect(body).toEqual([{ key, id }]) |
| 57 | + }) |
| 58 | + |
| 59 | + it('should return information related to the logged did even if there is data related to other did', async () => { |
| 60 | + this.dbName = 'backup-3.sqlite' |
| 61 | + await setup() |
| 62 | + |
| 63 | + const anotherDid = 'did:ethr:rsk:testnet:0xce83da2a364f37e44ec1a17f7f453a5e24395c65' |
| 64 | + await this.provider.create(anotherDid, 'AnotherKey', 'a content') |
| 65 | + |
| 66 | + const key = 'TheKey' |
| 67 | + const id = await this.provider.create(this.did, key, 'another content') |
| 68 | + |
| 69 | + const { body } = await request(this.app).get('/backup').expect(200) |
| 70 | + |
| 71 | + expect(body).toEqual([{ key, id }]) |
| 72 | + }) |
| 73 | +}) |
0 commit comments