Skip to content

Commit 241f9b9

Browse files
committed
test(@angular/cli): remove unscoped authentication test cases from registry tests
1 parent e3d5646 commit 241f9b9

4 files changed

Lines changed: 47 additions & 39 deletions

File tree

tests/legacy-cli/e2e/tests/commands/add/secure-registry.ts

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,14 @@ import { expectToFail } from '../../../utils/utils';
77
export default async function () {
88
// The environment variable has priority over the .npmrc
99
delete process.env['NPM_CONFIG_REGISTRY'];
10+
delete process.env['YARN_REGISTRY'];
11+
delete process.env['NPM_CONFIG__AUTH'];
12+
delete process.env['NPM_CONFIG_ALWAYS_AUTH'];
1013
const isNpm = getActivePackageManager() === 'npm';
1114

1215
const command = ['add', '@angular/pwa', '--skip-confirmation'];
1316
await expectFileNotToExist('public/manifest.webmanifest');
1417

15-
// Works with unscoped registry authentication details
16-
if (!isNpm) {
17-
// NPM no longer support unscoped.
18-
await createNpmConfigForAuthentication(false);
19-
await ng(...command);
20-
await expectFileToExist('public/manifest.webmanifest');
21-
await git('clean', '-dxf');
22-
}
2318
// Works with scoped registry authentication details
2419
await expectFileNotToExist('public/manifest.webmanifest');
2520

@@ -28,12 +23,6 @@ export default async function () {
2823
await expectFileToExist('public/manifest.webmanifest');
2924

3025
// Invalid authentication token
31-
if (isNpm) {
32-
// NPM no longer support unscoped.
33-
await createNpmConfigForAuthentication(false, true);
34-
await expectToFail(() => ng(...command));
35-
}
36-
3726
await createNpmConfigForAuthentication(true, true);
3827
await expectToFail(() => ng(...command));
3928

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
import { readFile, writeFile } from 'node:fs/promises';
2+
import { join } from 'node:path';
3+
import { getGlobalVariable } from '../../../utils/env';
14
import { expectFileNotToExist, expectFileToExist } from '../../../utils/fs';
25
import { getActivePackageManager } from '../../../utils/packages';
36
import { git, ng } from '../../../utils/process';
4-
import {
5-
createNpmConfigForAuthentication,
6-
setNpmEnvVarsForAuthentication,
7-
} from '../../../utils/registry';
7+
import { VALID_TOKEN } from '../../../utils/registry';
88

99
export default async function () {
1010
// Yarn specific test that tests YARN_ env variables.
@@ -14,16 +14,39 @@ export default async function () {
1414
}
1515
const command = ['add', '@angular/pwa', '--skip-confirmation'];
1616

17-
// Environment variables only
18-
await expectFileNotToExist('public/manifest.webmanifest');
19-
setNpmEnvVarsForAuthentication(false, true);
20-
await ng(...command);
21-
await expectFileToExist('public/manifest.webmanifest');
22-
await git('clean', '-dxf');
17+
// Clean up any potential env vars first
18+
delete process.env['NPM_CONFIG_REGISTRY'];
19+
delete process.env['YARN_REGISTRY'];
20+
delete process.env['NPM_CONFIG__AUTH'];
21+
delete process.env['NPM_CONFIG_ALWAYS_AUTH'];
2322

24-
// Mix of config file and env vars works
2523
await expectFileNotToExist('public/manifest.webmanifest');
26-
await createNpmConfigForAuthentication(false, true);
27-
await ng(...command);
28-
await expectFileToExist('public/manifest.webmanifest');
24+
25+
// Set the registry via YARN_REGISTRY environment variable
26+
const registryUrl = getGlobalVariable('package-secure-registry') as string;
27+
process.env['YARN_REGISTRY'] = registryUrl;
28+
29+
// Read the original user config to restore later
30+
const tempRoot = getGlobalVariable('tmp-root') as string;
31+
const userNpmrcPath = join(tempRoot, '.npmrc');
32+
const originalUserNpmrc = await readFile(userNpmrcPath, 'utf8');
33+
34+
// Write the scoped auth credentials to the user config .npmrc
35+
const registryHost = registryUrl.replace(/^\w+:/, '');
36+
await writeFile(
37+
userNpmrcPath,
38+
originalUserNpmrc +
39+
`\n${registryHost}/:_auth="${VALID_TOKEN}"` +
40+
`\n${registryHost}/:always-auth=true` +
41+
`\nalways-auth=true\n`,
42+
);
43+
44+
try {
45+
await ng(...command);
46+
await expectFileToExist('public/manifest.webmanifest');
47+
} finally {
48+
// Clean up and restore
49+
delete process.env['YARN_REGISTRY'];
50+
await writeFile(userNpmrcPath, originalUserNpmrc);
51+
}
2952
}

tests/legacy-cli/e2e/tests/update/update-secure-registry.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import assert from 'node:assert';
88
export default async function () {
99
// The environment variable has priority over the .npmrc
1010
delete process.env['NPM_CONFIG_REGISTRY'];
11+
delete process.env['YARN_REGISTRY'];
12+
delete process.env['NPM_CONFIG__AUTH'];
13+
delete process.env['NPM_CONFIG_ALWAYS_AUTH'];
1114
const worksMessage = 'We analyzed your package.json';
1215

1316
const extraArgs: string[] = [];
@@ -16,22 +19,13 @@ export default async function () {
1619
}
1720

1821
// Valid authentication token
19-
await createNpmConfigForAuthentication(false);
20-
const { stdout: stdout1 } = await ng('update', ...extraArgs);
21-
if (!stdout1.includes(worksMessage)) {
22-
throw new Error(`Expected stdout to contain "${worksMessage}"`);
23-
}
24-
2522
await createNpmConfigForAuthentication(true);
2623
const { stdout: stdout2 } = await ng('update', ...extraArgs);
2724
if (!stdout2.includes(worksMessage)) {
2825
throw new Error(`Expected stdout to contain "${worksMessage}"`);
2926
}
3027

3128
// Invalid authentication token
32-
await createNpmConfigForAuthentication(false, true);
33-
await expectToFail(() => ng('update', ...extraArgs));
34-
3529
await createNpmConfigForAuthentication(true, true);
3630
await expectToFail(() => ng('update', ...extraArgs));
3731

tests/legacy-cli/e2e/utils/registry.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export async function createNpmRegistry(
4242
}
4343

4444
// Token was generated using `echo -n 'testing:s3cret' | openssl base64`.
45-
const VALID_TOKEN = `dGVzdGluZzpzM2NyZXQ=`;
45+
export const VALID_TOKEN = `dGVzdGluZzpzM2NyZXQ=`;
4646

4747
export function createNpmConfigForAuthentication(
4848
/**
@@ -69,7 +69,9 @@ export function createNpmConfigForAuthentication(
6969
'.npmrc',
7070
scopedAuthentication
7171
? `
72-
${registry}:_auth="${token}"
72+
${registry}/:_auth="${token}"
73+
${registry}/:always-auth=true
74+
always-auth=true
7375
registry=http:${registry}
7476
`
7577
: `

0 commit comments

Comments
 (0)