Skip to content

Commit

Permalink
Add some more tests and remove unnecessary code
Browse files Browse the repository at this point in the history
  • Loading branch information
KidkArolis committed Jan 8, 2018
1 parent a05fc91 commit c9dae10
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 12 deletions.
79 changes: 71 additions & 8 deletions __tests__/registries/npm-registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,22 @@ function createMocks(): Object {
}

describe('request', () => {
// a helper function for creating an instance of npm registry,
// making requests and inspecting request parameters
function createRegistry(config: Object): Object {
const testCwd = '.';
const {mockRequestManager, mockRegistries, mockReporter} = createMocks();
const npmRegistry = new NpmRegistry(testCwd, mockRegistries, mockRequestManager, mockReporter);
npmRegistry.config = config;
return {
request(url: string): Object {
npmRegistry.request(url);
const requestParams = mockRequestManager.request.mock.calls[0][0];
return requestParams;
},
};
}

test('should call requestManager.request with url', () => {
const testCwd = '.';
const {mockRequestManager, mockRegistries, mockReporter} = createMocks();
Expand Down Expand Up @@ -207,20 +223,67 @@ describe('request', () => {
});

test('should add authorization header with token for default registry when using npm login --scope=@foo', () => {
const testCwd = '.';
const {mockRequestManager, mockRegistries, mockReporter} = createMocks();
const npmRegistry = new NpmRegistry(testCwd, mockRegistries, mockRequestManager, mockReporter);

const url = 'https://npmjs.registry.org/@foo%2fyarn.tgz';

npmRegistry.config = {
const config = {
'//npmjs.registry.org/:_authToken': 'testScopedAuthToken',
'@foo:registry': 'https://npmjs.registry.org/',
};
npmRegistry.request(url);

const requestParams = mockRequestManager.request.mock.calls[0][0];
const requestParams = createRegistry(config).request(url);
expect(requestParams.headers.authorization).toBe('Bearer testScopedAuthToken');
});

test('should add authorization header with token for yarn registry as default with a scoped package', () => {
const url = 'https://registry.yarnpkg.com/@testScope%2fyarn.tgz';
const config = {
'//registry.yarnpkg.com/:_authToken': 'testScopedAuthToken',
registry: 'https://registry.yarnpkg.com',
};

const requestParams = createRegistry(config).request(url);
expect(requestParams.headers.authorization).toBe('Bearer testScopedAuthToken');
});

test('should add authorization header with token for per scope yarn registry with a scoped package', () => {
const url = 'https://registry.yarnpkg.com/@testScope%2fyarn.tgz';
const config = {
'//registry.yarnpkg.com/:_authToken': 'testScopedAuthToken',
'@testScope:registry': 'https://registry.yarnpkg.com',
};
const requestParams = createRegistry(config).request(url);
expect(requestParams.headers.authorization).toBe('Bearer testScopedAuthToken');
});

test('should not add authorization header if default registry is yarn, but authed against npm', () => {
const url = 'https://registry.yarnpkg.com/@testScope%2fyarn.tgz';
const config = {
'//registry.npmjs.com/:_authToken': 'testScopedAuthToken',
registry: 'https://registry.yarnpkg.com/',
};

const requestParams = createRegistry(config).request(url);
expect(requestParams.headers.authorization).toBeUndefined();
});

test('should not add authorization header if request pathname does not match registry pathname', () => {
const url = 'https://custom.registry.com/tarball/path/@testScope%2fyarn.tgz';
const config = {
'//custom.registry.com/meta/path/:_authToken': 'testScopedAuthToken',
'@testScope:registry': 'https://custom.registry.com/meta/path/',
};

const requestParams = createRegistry(config).request(url);
expect(requestParams.headers.authorization).toBeUndefined();
});

test('should not add authorization header if request pathname matches registry pathname', () => {
const url = 'https://custom.registry.com/custom/path/@testScope%2fyarn.tgz';
const config = {
'//custom.registry.com/custom/path/:_authToken': 'testScopedAuthToken',
'@testScope:registry': 'https://custom.registry.com/custom/path/',
};

const requestParams = createRegistry(config).request(url);
expect(requestParams.headers.authorization).toBe('Bearer testScopedAuthToken');
});
});
Expand Down
4 changes: 0 additions & 4 deletions src/registries/npm-registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ export default class NpmRegistry extends Registry {
}

request(pathname: string, opts?: RegistryRequestOptions = {}, packageName: ?string): Promise<*> {
// packageName needs to be escaped when if it is passed
const packageIdent = (packageName && NpmRegistry.escapeName(packageName)) || pathname;
const registry = this.getRegistry(packageIdent);
const requestUrl = this.getRequestUrl(registry, pathname);
Expand Down Expand Up @@ -333,9 +332,6 @@ export default class NpmRegistry extends Registry {

getAvailableRegistries(): Array<string> {
const availableRegistries = super.getAvailableRegistries();
if (availableRegistries.indexOf(YARN_REGISTRY) === -1) {
availableRegistries.push(YARN_REGISTRY);
}
if (availableRegistries.indexOf(DEFAULT_REGISTRY) === -1) {
availableRegistries.push(DEFAULT_REGISTRY);
}
Expand Down

0 comments on commit c9dae10

Please sign in to comment.