setup-dotnet/__tests__/authutil.test.ts

337 lines
12 KiB
TypeScript
Raw Permalink Normal View History

import * as io from '@actions/io';
import fs from 'fs';
import path from 'path';
2019-09-10 00:27:23 +07:00
2019-10-04 07:29:20 +07:00
const fakeSourcesDirForTesting = path.join(
2019-09-10 00:27:23 +07:00
__dirname,
'runner',
path.join(Math.random().toString(36).substring(7)),
2019-10-04 07:29:20 +07:00
's'
2019-09-10 00:27:23 +07:00
);
const invalidNuGetConfig: string = `<?xml version="1.0" encoding="utf-8"?>`;
const emptyNuGetConfig: string = `<?xml version="1.0" encoding="utf-8"?>
<configuration>
</configuration>`;
const nugetorgNuGetConfig: string = `<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
</packageSources>
</configuration>`;
const gprnugetorgNuGetConfig: string = `<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
2019-11-27 04:27:50 +07:00
<add key="GPR" value="https://nuget.pkg.github.com/OwnerName/index.json" protocolVersion="3" />
2019-09-10 00:27:23 +07:00
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
</packageSources>
</configuration>`;
const gprNuGetConfig: string = `<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
2019-11-27 04:27:50 +07:00
<add key="GPR" value="https://nuget.pkg.github.com/OwnerName/index.json" protocolVersion="3" />
2019-09-10 00:27:23 +07:00
</packageSources>
</configuration>`;
const twogprNuGetConfig: string = `<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
2019-11-27 06:08:48 +07:00
<add key="GPR-GitHub" value="https://nuget.pkg.github.com/OwnerName/index.json" protocolVersion="3" />
2019-09-10 00:27:23 +07:00
<add key="GPR-Actions" value="https://nuget.pkg.github.com/actions/index.json" protocolVersion="3" />
</packageSources>
</configuration>`;
const spaceNuGetConfig: string = `<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
2019-11-27 04:27:50 +07:00
<add key="GPR GitHub" value="https://nuget.pkg.github.com/OwnerName/index.json" protocolVersion="3" />
2019-09-10 00:27:23 +07:00
</packageSources>
</configuration>`;
2019-10-04 07:29:20 +07:00
const azureartifactsNuGetConfig: string = `<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="AzureArtifacts" value="https://pkgs.dev.azure.com/amullans/_packaging/GitHubBuilds/nuget/v3/index.json" protocolVersion="3" />
</packageSources>
</configuration>`;
const azureartifactsnugetorgNuGetConfig: string = `<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="AzureArtifacts" value="https://pkgs.dev.azure.com/amullans/_packaging/GitHubBuilds/nuget/v3/index.json" protocolVersion="3" />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
</packageSources>
</configuration>`;
// We want a NuGet.config one level above the sources directory, so it doesn't trample a user's NuGet.config but is still picked up by NuGet/dotnet.
const nugetConfigFile = path.join(fakeSourcesDirForTesting, '../nuget.config');
2019-09-10 00:27:23 +07:00
process.env['GITHUB_REPOSITORY'] = 'OwnerName/repo';
import * as auth from '../src/authutil';
describe('authutil tests', () => {
2019-10-04 07:29:20 +07:00
beforeEach(async () => {
await io.rmRF(fakeSourcesDirForTesting);
await io.mkdirP(fakeSourcesDirForTesting);
}, 30000);
afterAll(async () => {
await io.rmRF(fakeSourcesDirForTesting);
}, 30000);
2019-09-10 00:27:23 +07:00
beforeEach(() => {
if (fs.existsSync(nugetConfigFile)) {
fs.unlinkSync(nugetConfigFile);
}
process.env['INPUT_OWNER'] = '';
process.env['NUGET_AUTH_TOKEN'] = '';
});
it('No existing config, sets up a full NuGet.config with URL and user/PAT for GPR', async () => {
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
2019-11-27 06:08:48 +07:00
await auth.configAuthentication(
'https://nuget.pkg.github.com/OwnerName/index.json',
'',
fakeSourcesDirForTesting
2019-11-27 06:08:48 +07:00
);
2019-09-10 00:27:23 +07:00
expect(fs.existsSync(nugetConfigFile)).toBe(true);
expect(
fs.readFileSync(nugetConfigFile, {encoding: 'utf8'})
).toMatchSnapshot();
});
it('No existing config, auth token environment variable not provided, throws', async () => {
let thrown = false;
try {
2019-11-27 06:08:48 +07:00
await auth.configAuthentication(
'https://nuget.pkg.github.com/OwnerName/index.json',
'',
fakeSourcesDirForTesting
2019-11-27 06:08:48 +07:00
);
2019-09-10 00:27:23 +07:00
} catch {
thrown = true;
}
expect(thrown).toBe(true);
});
it('No existing config, sets up a full NuGet.config with URL and other owner/PAT for GPR', async () => {
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
process.env['INPUT_OWNER'] = 'otherorg';
2019-11-27 06:08:48 +07:00
await auth.configAuthentication(
'https://nuget.pkg.github.com/otherorg/index.json',
'',
fakeSourcesDirForTesting
2019-11-27 06:08:48 +07:00
);
2019-09-10 00:27:23 +07:00
expect(fs.existsSync(nugetConfigFile)).toBe(true);
expect(
fs.readFileSync(nugetConfigFile, {encoding: 'utf8'})
).toMatchSnapshot();
});
it('Existing config (invalid), tries to parse an invalid NuGet.config and throws', async () => {
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
2019-10-04 07:29:20 +07:00
const inputNuGetConfigPath: string = path.join(
fakeSourcesDirForTesting,
'nuget.config'
);
2019-09-10 00:27:23 +07:00
fs.writeFileSync(inputNuGetConfigPath, invalidNuGetConfig);
let thrown = false;
try {
2019-11-27 06:08:48 +07:00
await auth.configAuthentication(
'https://nuget.pkg.github.com/OwnerName/index.json',
'',
fakeSourcesDirForTesting
2019-11-27 06:08:48 +07:00
);
2019-09-10 00:27:23 +07:00
} catch {
thrown = true;
}
expect(thrown).toBe(true);
});
it('Existing config w/ no sources, sets up a full NuGet.config with URL and user/PAT for GPR', async () => {
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
2019-10-04 07:29:20 +07:00
const inputNuGetConfigPath: string = path.join(
fakeSourcesDirForTesting,
'nuget.config'
);
2019-09-10 00:27:23 +07:00
fs.writeFileSync(inputNuGetConfigPath, emptyNuGetConfig);
2019-11-27 06:08:48 +07:00
await auth.configAuthentication(
'https://nuget.pkg.github.com/OwnerName/index.json',
'',
fakeSourcesDirForTesting
2019-11-27 06:08:48 +07:00
);
2019-09-10 00:27:23 +07:00
expect(fs.existsSync(nugetConfigFile)).toBe(true);
expect(
fs.readFileSync(nugetConfigFile, {encoding: 'utf8'})
).toMatchSnapshot();
});
it('Existing config w/ no GPR sources, sets up a full NuGet.config with URL and user/PAT for GPR', async () => {
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
2019-10-04 07:29:20 +07:00
const inputNuGetConfigPath: string = path.join(
fakeSourcesDirForTesting,
'nuget.config'
);
2019-09-10 00:27:23 +07:00
fs.writeFileSync(inputNuGetConfigPath, nugetorgNuGetConfig);
2019-11-27 06:08:48 +07:00
await auth.configAuthentication(
'https://nuget.pkg.github.com/OwnerName/index.json',
'',
fakeSourcesDirForTesting
2019-11-27 06:08:48 +07:00
);
2019-09-10 00:27:23 +07:00
expect(fs.existsSync(nugetConfigFile)).toBe(true);
expect(
fs.readFileSync(nugetConfigFile, {encoding: 'utf8'})
).toMatchSnapshot();
});
it('Existing config w/ only GPR source, sets up a partial NuGet.config user/PAT for GPR', async () => {
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
2019-10-04 07:29:20 +07:00
const inputNuGetConfigPath: string = path.join(
fakeSourcesDirForTesting,
'nuget.config'
);
2019-09-10 00:27:23 +07:00
fs.writeFileSync(inputNuGetConfigPath, gprNuGetConfig);
2019-11-27 06:08:48 +07:00
await auth.configAuthentication(
'https://nuget.pkg.github.com/OwnerName/index.json',
'',
fakeSourcesDirForTesting
2019-11-27 06:08:48 +07:00
);
2019-09-10 00:27:23 +07:00
expect(fs.existsSync(nugetConfigFile)).toBe(true);
expect(
fs.readFileSync(nugetConfigFile, {encoding: 'utf8'})
).toMatchSnapshot();
});
it('Existing config w/ GPR source and NuGet.org, sets up a partial NuGet.config user/PAT for GPR', async () => {
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
2019-10-04 07:29:20 +07:00
const inputNuGetConfigPath: string = path.join(
fakeSourcesDirForTesting,
'nuget.config'
);
2019-09-10 00:27:23 +07:00
fs.writeFileSync(inputNuGetConfigPath, gprnugetorgNuGetConfig);
2019-11-27 06:08:48 +07:00
await auth.configAuthentication(
'https://nuget.pkg.github.com/OwnerName/index.json',
'',
fakeSourcesDirForTesting
2019-11-27 06:08:48 +07:00
);
2019-09-10 00:27:23 +07:00
expect(fs.existsSync(nugetConfigFile)).toBe(true);
expect(
fs.readFileSync(nugetConfigFile, {encoding: 'utf8'})
).toMatchSnapshot();
});
it('Existing config w/ two GPR sources, sets up a partial NuGet.config user/PAT for GPR', async () => {
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
2019-10-04 07:29:20 +07:00
const inputNuGetConfigPath: string = path.join(
fakeSourcesDirForTesting,
'nuget.config'
);
2019-09-10 00:27:23 +07:00
fs.writeFileSync(inputNuGetConfigPath, twogprNuGetConfig);
await auth.configAuthentication(
'https://nuget.pkg.github.com',
'',
fakeSourcesDirForTesting
);
2019-09-10 00:27:23 +07:00
expect(fs.existsSync(nugetConfigFile)).toBe(true);
expect(
fs.readFileSync(nugetConfigFile, {encoding: 'utf8'})
).toMatchSnapshot();
});
it('Existing config w/ spaces in key, throws for now', async () => {
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
2019-10-04 07:29:20 +07:00
const inputNuGetConfigPath: string = path.join(
fakeSourcesDirForTesting,
'nuget.config'
);
2019-09-10 00:27:23 +07:00
fs.writeFileSync(inputNuGetConfigPath, spaceNuGetConfig);
let thrown = false;
try {
2019-11-27 06:08:48 +07:00
await auth.configAuthentication(
'https://nuget.pkg.github.com/OwnerName/index.json',
'',
fakeSourcesDirForTesting
2019-11-27 06:08:48 +07:00
);
2019-09-10 00:27:23 +07:00
} catch {
thrown = true;
}
expect(thrown).toBe(true);
});
it('Existing config not in repo root, sets up a partial NuGet.config user/PAT for GPR', async () => {
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
2019-10-04 07:29:20 +07:00
const inputNuGetConfigDirectory: string = path.join(
fakeSourcesDirForTesting,
'subfolder'
);
2019-09-10 00:27:23 +07:00
const inputNuGetConfigPath: string = path.join(
inputNuGetConfigDirectory,
'nuget.config'
);
fs.mkdirSync(inputNuGetConfigDirectory, {recursive: true});
fs.writeFileSync(inputNuGetConfigPath, gprNuGetConfig);
await auth.configAuthentication(
2019-11-27 04:27:50 +07:00
'https://nuget.pkg.github.com/OwnerName/index.json',
'subfolder/nuget.config',
fakeSourcesDirForTesting
2019-09-10 00:27:23 +07:00
);
expect(fs.existsSync(nugetConfigFile)).toBe(true);
expect(
fs.readFileSync(nugetConfigFile, {encoding: 'utf8'})
).toMatchSnapshot();
});
2019-10-04 07:29:20 +07:00
it('Existing config w/ only Azure Artifacts source, sets up a partial NuGet.config user/PAT for GPR', async () => {
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
const inputNuGetConfigPath: string = path.join(
fakeSourcesDirForTesting,
'nuget.config'
);
fs.writeFileSync(inputNuGetConfigPath, azureartifactsNuGetConfig);
await auth.configAuthentication(
'https://pkgs.dev.azure.com/amullans/_packaging/GitHubBuilds/nuget/v3/index.json',
'',
fakeSourcesDirForTesting
2019-10-04 07:29:20 +07:00
);
expect(fs.existsSync(nugetConfigFile)).toBe(true);
expect(
fs.readFileSync(nugetConfigFile, {encoding: 'utf8'})
).toMatchSnapshot();
});
it('Existing config w/ Azure Artifacts source and NuGet.org, sets up a partial NuGet.config user/PAT for GPR', async () => {
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
const inputNuGetConfigPath: string = path.join(
fakeSourcesDirForTesting,
'nuget.config'
);
fs.writeFileSync(inputNuGetConfigPath, azureartifactsnugetorgNuGetConfig);
await auth.configAuthentication(
'https://pkgs.dev.azure.com/amullans/_packaging/GitHubBuilds/nuget/v3/index.json',
'',
fakeSourcesDirForTesting
2019-10-04 07:29:20 +07:00
);
expect(fs.existsSync(nugetConfigFile)).toBe(true);
expect(
fs.readFileSync(nugetConfigFile, {encoding: 'utf8'})
).toMatchSnapshot();
});
it('No existing config, sets up a full NuGet.config with URL and token for other source', async () => {
process.env['NUGET_AUTH_TOKEN'] = 'TEST_FAKE_AUTH_TOKEN';
await auth.configAuthentication(
'https://pkgs.dev.azure.com/amullans/_packaging/GitHubBuilds/nuget/v3/index.json',
'',
fakeSourcesDirForTesting
2019-10-04 07:29:20 +07:00
);
expect(fs.existsSync(nugetConfigFile)).toBe(true);
expect(
fs.readFileSync(nugetConfigFile, {encoding: 'utf8'})
).toMatchSnapshot();
});
2019-09-10 00:27:23 +07:00
});