Skip to content

Commit a70b5a5

Browse files
committed
test: add coverage for the otpUrl case
Note: this does not yet assert that the OTP was actually sent over to npm.
1 parent b31950d commit a70b5a5

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

lib/publish.js

+2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ module.exports = async ({npmPublish, pkgRoot, otpUrl}, pkg, context) => {
1919
const registry = getRegistry(pkg, context);
2020
let otpArgs = [];
2121
if (otpUrl) {
22+
logger.log('Retrieving OTP from a remote URL...');
2223
const res = await fetch(otpUrl);
2324
otpArgs = ['--otp', await res.text()];
25+
logger.log('OTP received');
2426
}
2527

2628
logger.log('Publishing version %s to npm registry', version);

test/helpers/otp-server.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import http from 'http';
2+
3+
let server;
4+
5+
async function start() {
6+
server = http.createServer((req, res) => {
7+
res.statusCode = 200;
8+
res.setHeader('Content-Type', 'text/plain');
9+
res.end('this-is-my-otp');
10+
});
11+
12+
return new Promise(resolve => {
13+
server.listen(0, '127.0.0.1', () => resolve(server));
14+
});
15+
}
16+
17+
async function stop() {
18+
return new Promise(resolve => {
19+
server.close(() => resolve());
20+
});
21+
}
22+
23+
function address() {
24+
return server.address();
25+
}
26+
27+
export default {start, stop, address};

test/integration.test.js

+33
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import tempy from 'tempy';
77
import clearModule from 'clear-module';
88
import {WritableStreamBuffer} from 'stream-buffers';
99
import npmRegistry from './helpers/npm-registry';
10+
import otpServer from './helpers/otp-server';
1011

1112
/* eslint camelcase: ["error", {properties: "never"}] */
1213

@@ -23,6 +24,8 @@ const testEnv = {
2324
test.before(async () => {
2425
// Start the local NPM registry
2526
await npmRegistry.start();
27+
// Stop the test OTP server
28+
await otpServer.start();
2629
});
2730

2831
test.beforeEach(t => {
@@ -39,6 +42,8 @@ test.beforeEach(t => {
3942
test.after.always(async () => {
4043
// Stop the local NPM registry
4144
await npmRegistry.stop();
45+
// Stop the test OTP server
46+
await otpServer.start();
4247
});
4348

4449
test('Skip npm auth verification if "npmPublish" is false', async t => {
@@ -285,6 +290,34 @@ test('Publish the package on a dist-tag', async t => {
285290
t.is((await execa('npm', ['view', pkg.name, 'version'], {cwd, env: testEnv})).stdout, '1.0.0');
286291
});
287292

293+
test('Publish the package with OTP', async t => {
294+
const cwd = tempy.directory();
295+
const env = npmRegistry.authEnv;
296+
const pkg = {name: 'publish-otp', version: '0.0.0', publishConfig: {registry: npmRegistry.url}};
297+
await outputJson(path.resolve(cwd, 'package.json'), pkg);
298+
299+
const {port, address} = otpServer.address();
300+
const result = await t.context.m.publish(
301+
{
302+
otpUrl: `http://${address}:${port}/otp`,
303+
},
304+
{
305+
cwd,
306+
env,
307+
options: {},
308+
stdout: t.context.stdout,
309+
stderr: t.context.stderr,
310+
logger: t.context.logger,
311+
nextRelease: {version: '1.0.0'},
312+
}
313+
);
314+
315+
t.deepEqual(result, {name: 'npm package (@latest dist-tag)', url: undefined});
316+
t.is((await readJson(path.resolve(cwd, 'package.json'))).version, '1.0.0');
317+
t.false(await pathExists(path.resolve(cwd, `${pkg.name}-1.0.0.tgz`)));
318+
t.is((await execa('npm', ['view', pkg.name, 'version'], {cwd, env: testEnv})).stdout, '1.0.0');
319+
});
320+
288321
test('Publish the package from a sub-directory', async t => {
289322
const cwd = tempy.directory();
290323
const env = npmRegistry.authEnv;

0 commit comments

Comments
 (0)