Mock cloud storage bucket

This commit is contained in:
Hongbo Wu 2023-03-02 22:21:09 +08:00
parent 76b63efdc1
commit 06b6583f29
3 changed files with 15 additions and 10 deletions

View file

@ -9,7 +9,7 @@ import { env } from '../env'
* the default app engine service account on the IAM page. We also need to
* enable IAM related APIs on the project.
*/
export const storage = env.fileUpload?.gcsUploadSAKeyFilePath
const storage = env.fileUpload?.gcsUploadSAKeyFilePath
? new Storage({ keyFilename: env.fileUpload.gcsUploadSAKeyFilePath })
: new Storage()
const bucketName = env.fileUpload.gcsUploadBucket

View file

@ -1,6 +1,6 @@
import { Writable } from 'stream'
export class MockStorage {
class MockStorage {
buckets: { [name: string]: MockBucket }
constructor() {
@ -12,7 +12,7 @@ export class MockStorage {
}
}
class MockBucket {
export class MockBucket {
name: string
files: { [path: string]: MockFile }

View file

@ -22,8 +22,8 @@ import { getHighlightUrl } from '../../src/services/highlights'
import { deletePage } from '../../src/elastic/pages'
import { READWISE_API_URL } from '../../src/services/integrations/readwise'
import sinon from 'sinon'
import { MockStorage } from '../mock_storage'
import * as uploads from '../../src/utils/uploads'
import { Storage } from '@google-cloud/storage'
import { MockBucket } from '../mock_storage'
describe('Integrations routers', () => {
const baseUrl = '/svc/pubsub/integrations'
@ -346,9 +346,7 @@ describe('Integrations routers', () => {
token,
type: IntegrationType.Import,
})
// mock cloud storage bucket
// @ts-ignore
sinon.replace(uploads, 'storage', new MockStorage('test-bucket'))
// mock Pocket API
nock('https://getpocket.com', {
reqheaders: {
@ -385,7 +383,14 @@ describe('Integrations routers', () => {
context('when integration is pocket', () => {
it('returns 200 with OK', async () => {
const res = await request
// mock cloud storage
const mockBucket = new MockBucket('test')
sinon.replace(
Storage.prototype,
'bucket',
sinon.fake.returns(mockBucket as never)
)
await request
.post(`${baseUrl}/import`)
.send({
integrationId: integration.id,
@ -393,7 +398,7 @@ describe('Integrations routers', () => {
.set('Cookie', `auth=${authToken}`)
.expect(200)
expect(res.text).to.eql('OK')
sinon.restore()
})
})
})