mirror of
https://github.com/verdaccio/verdaccio.git
synced 2025-01-06 22:40:26 -05:00
126 lines
4 KiB
TypeScript
126 lines
4 KiB
TypeScript
import { Application } from 'express';
|
|
import _ from 'lodash';
|
|
import path from 'path';
|
|
import supertest from 'supertest';
|
|
|
|
import { parseConfigFile } from '@verdaccio/config';
|
|
import { HEADERS, HEADER_TYPE, HTTP_STATUS, TOKEN_BEARER } from '@verdaccio/core';
|
|
import { setup } from '@verdaccio/logger';
|
|
import { Storage } from '@verdaccio/store';
|
|
import {
|
|
generatePackageMetadata,
|
|
initializeServer as initializeServerHelper,
|
|
} from '@verdaccio/test-helper';
|
|
import { GenericBody } from '@verdaccio/types';
|
|
import { buildToken, generateRandomHexString } from '@verdaccio/utils';
|
|
|
|
import apiMiddleware from '../../src';
|
|
|
|
setup();
|
|
|
|
export const getConf = (conf) => {
|
|
const configPath = path.join(__dirname, 'config', conf);
|
|
const config = parseConfigFile(configPath);
|
|
// custom config to avoid conflict with other tests
|
|
config.auth.htpasswd.file = `${config.auth.htpasswd.file}-${generateRandomHexString()}`;
|
|
return config;
|
|
};
|
|
|
|
export async function initializeServer(configName): Promise<Application> {
|
|
return initializeServerHelper(getConf(configName), [apiMiddleware], Storage);
|
|
}
|
|
|
|
export function createUser(app, name: string, password: string): supertest.Test {
|
|
return supertest(app)
|
|
.put(`/-/user/org.couchdb.user:${name}`)
|
|
.send({
|
|
name: name,
|
|
password: password,
|
|
})
|
|
.expect(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON_CHARSET)
|
|
.expect(HTTP_STATUS.CREATED);
|
|
}
|
|
|
|
export async function getNewToken(app: any, credentials: any): Promise<string> {
|
|
const response = await createUser(app, credentials.name, credentials.password);
|
|
const { token, ok } = response.body;
|
|
expect(ok).toBeDefined();
|
|
expect(token).toBeDefined();
|
|
expect(typeof token).toBe('string');
|
|
return token;
|
|
}
|
|
|
|
export async function generateTokenCLI(app, token, payload): Promise<any> {
|
|
return supertest(app)
|
|
.post('/-/npm/v1/tokens')
|
|
.set(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON)
|
|
.send(JSON.stringify(payload))
|
|
.set(HEADERS.AUTHORIZATION, buildToken(TOKEN_BEARER, token))
|
|
.expect(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON_CHARSET);
|
|
}
|
|
|
|
export async function deleteTokenCLI(app, token, tokenToDelete): Promise<any> {
|
|
return supertest(app)
|
|
.delete(`/-/npm/v1/tokens/token/${tokenToDelete}`)
|
|
.set(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON)
|
|
.set(HEADERS.AUTHORIZATION, buildToken(TOKEN_BEARER, token))
|
|
.expect(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON_CHARSET)
|
|
.expect(HTTP_STATUS.OK);
|
|
}
|
|
|
|
export function publishVersionWithToken(
|
|
app,
|
|
pkgName: string,
|
|
version: string,
|
|
token: string,
|
|
distTags?: GenericBody
|
|
): supertest.Test {
|
|
const pkgMetadata = generatePackageMetadata(pkgName, version, distTags);
|
|
|
|
return supertest(app)
|
|
.put(`/${encodeURIComponent(pkgName)}`)
|
|
.set(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON)
|
|
.set(HEADERS.AUTHORIZATION, buildToken(TOKEN_BEARER, token))
|
|
.send(JSON.stringify(pkgMetadata))
|
|
.set('accept', HEADERS.GZIP)
|
|
.set(HEADER_TYPE.ACCEPT_ENCODING, HEADERS.JSON);
|
|
}
|
|
|
|
export function publishVersion(
|
|
app,
|
|
pkgName: string,
|
|
version: string,
|
|
distTags?: GenericBody
|
|
): supertest.Test {
|
|
const pkgMetadata = generatePackageMetadata(pkgName, version, distTags);
|
|
|
|
return supertest(app)
|
|
.put(`/${encodeURIComponent(pkgName)}`)
|
|
.set(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON)
|
|
.send(JSON.stringify(pkgMetadata))
|
|
.set('accept', HEADERS.GZIP)
|
|
.set(HEADER_TYPE.ACCEPT_ENCODING, HEADERS.JSON);
|
|
}
|
|
|
|
export function getDisTags(app, pkgName) {
|
|
return supertest(app)
|
|
.get(`/-/package/${encodeURIComponent(pkgName)}/dist-tags`)
|
|
.set(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON)
|
|
.expect(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON_CHARSET)
|
|
.expect(HTTP_STATUS.OK);
|
|
}
|
|
|
|
export function getPackage(
|
|
app: any,
|
|
token: string,
|
|
pkgName: string,
|
|
statusCode: number = HTTP_STATUS.OK
|
|
): supertest.Test {
|
|
const test = supertest(app).get(`/${pkgName}`);
|
|
|
|
if (_.isNil(token) === false || _.isEmpty(token) === false) {
|
|
test.set(HEADERS.AUTHORIZATION, buildToken(TOKEN_BEARER, token));
|
|
}
|
|
|
|
return test.expect(HEADER_TYPE.CONTENT_TYPE, HEADERS.JSON_CHARSET).expect(statusCode);
|
|
}
|