0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-12-16 21:56:25 -05:00
verdaccio/packages/store/test/storage.spec.ts
Juan Picado a828271d63
refactor: fetch package endpoint and basic integration fastify (#2750)
* refactor: download manifest endpoint and integrate fastify

* fix file not found issue

* add temporary migration method to storage memory

* sanitize fs methods

* restore tests

* chore: restore sanitilize

will do later

* add tests

* add changeset

* lint

* trying something test

* chore: lint

* restore code

* fix e2e

* fix lint
2021-12-12 18:00:19 +01:00

245 lines
7.2 KiB
TypeScript

import nock from 'nock';
import * as httpMocks from 'node-mocks-http';
import { Config } from '@verdaccio/config';
import { HEADERS, errorUtils } from '@verdaccio/core';
import { generatePackageMetadata } from '@verdaccio/helper';
import { setup } from '@verdaccio/logger';
import { configExample, generateRamdonStorage } from '@verdaccio/mock';
import { Storage } from '../src';
setup([]);
const domain = 'http://localhost:4873';
const fakeHost = 'localhost:4873';
const fooManifest = generatePackageMetadata('foo', '1.0.0');
describe('storage', () => {
beforeEach(() => {
nock.cleanAll();
nock.abortPendingRequests();
jest.clearAllMocks();
});
describe('add packages', () => {
test('add package item', async () => {
nock(domain).get('/foo').reply(404);
const config = new Config(
configExample({
storage: generateRamdonStorage(),
})
);
const storage = new Storage(config);
await storage.init(config);
await storage.addPackage('foo', fooManifest, (err) => {
expect(err).toBeNull();
});
});
});
// TODO: getPackageNext should replace getPackage eventually
describe('get packages getPackageNext()', () => {
describe('with uplinks', () => {
test('should get 201 and merge from uplink', async () => {
nock(domain).get('/foo').reply(201, fooManifest);
const config = new Config(
configExample({
storage: generateRamdonStorage(),
})
);
const req = httpMocks.createRequest({
method: 'GET',
connection: { remoteAddress: fakeHost },
headers: {
host: fakeHost,
[HEADERS.FORWARDED_PROTO]: 'http',
},
url: '/',
});
const storage = new Storage(config);
await storage.init(config);
await expect(
storage.getPackageNext({
name: 'foo',
uplinksLook: true,
req,
requestOptions: {
headers: req.headers as any,
protocol: req.protocol,
host: req.get('host'),
},
})
).resolves.toEqual(expect.objectContaining({ name: 'foo' }));
});
test('should get 201 and merge from uplink with version', async () => {
nock(domain).get('/foo').reply(201, fooManifest);
const config = new Config(
configExample({
storage: generateRamdonStorage(),
})
);
const req = httpMocks.createRequest({
method: 'GET',
connection: { remoteAddress: fakeHost },
headers: {
host: fakeHost,
[HEADERS.FORWARDED_PROTO]: 'http',
},
url: '/',
});
const storage = new Storage(config);
await storage.init(config);
await expect(
storage.getPackageNext({
name: 'foo',
version: '1.0.0',
uplinksLook: true,
req,
requestOptions: {
headers: req.headers as any,
protocol: req.protocol,
host: req.get('host'),
},
})
).resolves.toEqual(expect.objectContaining({ name: 'foo' }));
});
test('should get 201 and merge from uplink with dist-tag', async () => {
nock(domain).get('/foo').reply(201, fooManifest);
const config = new Config(
configExample({
storage: generateRamdonStorage(),
})
);
const req = httpMocks.createRequest({
method: 'GET',
connection: { remoteAddress: fakeHost },
headers: {
host: fakeHost,
[HEADERS.FORWARDED_PROTO]: 'http',
},
url: '/',
});
const storage = new Storage(config);
await storage.init(config);
await expect(
storage.getPackageNext({
name: 'foo',
version: 'latest',
uplinksLook: true,
req,
requestOptions: {
headers: req.headers as any,
protocol: req.protocol,
host: req.get('host'),
},
})
).resolves.toEqual(expect.objectContaining({ name: 'foo' }));
});
test('should get 404 for version does not exist', async () => {
nock(domain).get('/foo').reply(201, fooManifest);
const config = new Config(
configExample({
storage: generateRamdonStorage(),
})
);
const req = httpMocks.createRequest({
method: 'GET',
connection: { remoteAddress: fakeHost },
headers: {
host: fakeHost,
[HEADERS.FORWARDED_PROTO]: 'http',
},
url: '/',
});
const storage = new Storage(config);
await storage.init(config);
await expect(
storage.getPackageNext({
name: 'foo',
version: '1.0.0-does-not-exist',
uplinksLook: true,
req,
requestOptions: {
headers: req.headers as any,
protocol: req.protocol,
host: req.get('host'),
},
})
).rejects.toThrow(
errorUtils.getNotFound("this version doesn't exist: 1.0.0-does-not-exist")
);
});
test('should get 404', async () => {
nock(domain).get('/foo2').reply(404);
const config = new Config(
configExample({
storage: generateRamdonStorage(),
})
);
const req = httpMocks.createRequest({
method: 'GET',
connection: { remoteAddress: fakeHost },
headers: {
host: fakeHost,
[HEADERS.FORWARDED_PROTO]: 'http',
},
url: '/',
});
const storage = new Storage(config);
await storage.init(config);
await expect(
storage.getPackageNext({
name: 'foo2',
uplinksLook: true,
req,
requestOptions: {
headers: req.headers as any,
protocol: req.protocol,
host: req.get('host'),
},
})
).rejects.toThrow(errorUtils.getNotFound());
});
test('should get ETIMEDOUT with uplink', async () => {
nock(domain).get('/foo2').replyWithError({
code: 'ETIMEDOUT',
errno: 'ETIMEDOUT',
});
const config = new Config(
configExample({
storage: generateRamdonStorage(),
})
);
const req = httpMocks.createRequest({
method: 'GET',
connection: { remoteAddress: fakeHost },
headers: {
host: fakeHost,
[HEADERS.FORWARDED_PROTO]: 'http',
},
url: '/',
});
const storage = new Storage(config);
await storage.init(config);
await expect(
storage.getPackageNext({
name: 'foo2',
uplinksLook: true,
req,
requestOptions: {
headers: req.headers as any,
protocol: req.protocol,
host: req.get('host'),
},
})
).rejects.toThrow(errorUtils.getServiceUnavailable());
});
});
});
});