Skip to content

Commit e77eaac

Browse files
committed
Add unit tests
1 parent a69d45a commit e77eaac

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

__tests__/authutil.test.ts

+81
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ describe('authutil tests', () => {
123123
expect(rc['registry']).toBe('https://registry.npmjs.org/');
124124
expect(rc['always-auth']).toBe('true');
125125
});
126+
126127
it('It is already set the NODE_AUTH_TOKEN export it ', async () => {
127128
process.env.NODE_AUTH_TOKEN = 'foobar';
128129
await auth.configAuthentication('npm.pkg.github.com', 'false');
@@ -132,4 +133,84 @@ describe('authutil tests', () => {
132133
expect(rc['always-auth']).toBe('false');
133134
expect(process.env.NODE_AUTH_TOKEN).toEqual('foobar');
134135
});
136+
137+
it('configAuthentication should overwrite non-scoped with non-scoped', async () => {
138+
fs.writeFileSync(rcFile, 'registry=NNN');
139+
await auth.configAuthentication('https://registry.npmjs.org/', 'true');
140+
let contents = fs.readFileSync(rcFile, {encoding: 'utf8'});
141+
expect(contents).toBe(
142+
`//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}${os.EOL}registry=https://registry.npmjs.org/${os.EOL}always-auth=true`
143+
);
144+
});
145+
146+
it('configAuthentication should overwrite only non-scoped', async () => {
147+
fs.writeFileSync(rcFile, `registry=NNN${os.EOL}@myscope:registry=MMM`);
148+
await auth.configAuthentication('https://registry.npmjs.org/', 'true');
149+
let contents = fs.readFileSync(rcFile, {encoding: 'utf8'});
150+
expect(contents).toBe(
151+
`@myscope:registry=MMM${os.EOL}//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}${os.EOL}registry=https://registry.npmjs.org/${os.EOL}always-auth=true`
152+
);
153+
});
154+
155+
it('configAuthentication should add non-scoped to scoped', async () => {
156+
fs.writeFileSync(rcFile, '@myscope:registry=NNN');
157+
await auth.configAuthentication('https://registry.npmjs.org/', 'true');
158+
let contents = fs.readFileSync(rcFile, {encoding: 'utf8'});
159+
expect(contents).toBe(
160+
`@myscope:registry=NNN${os.EOL}//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}${os.EOL}registry=https://registry.npmjs.org/${os.EOL}always-auth=true`
161+
);
162+
});
163+
164+
it('configAuthentication should overwrite scoped with scoped', async () => {
165+
process.env['INPUT_SCOPE'] = 'myscope';
166+
fs.writeFileSync(rcFile, `@myscope:registry=NNN`);
167+
await auth.configAuthentication('https://registry.npmjs.org/', 'true');
168+
let contents = fs.readFileSync(rcFile, {encoding: 'utf8'});
169+
expect(contents).toBe(
170+
`//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}${os.EOL}@myscope:registry=https://registry.npmjs.org/${os.EOL}always-auth=true`
171+
);
172+
});
173+
174+
it('configAuthentication should overwrite only scoped', async () => {
175+
process.env['INPUT_SCOPE'] = 'myscope';
176+
fs.writeFileSync(rcFile, `registry=NNN${os.EOL}@myscope:registry=MMM`);
177+
await auth.configAuthentication('https://registry.npmjs.org/', 'true');
178+
let contents = fs.readFileSync(rcFile, {encoding: 'utf8'});
179+
expect(contents).toBe(
180+
`registry=NNN${os.EOL}//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}${os.EOL}@myscope:registry=https://registry.npmjs.org/${os.EOL}always-auth=true`
181+
);
182+
});
183+
184+
it('configAuthentication should add scoped to non-scoped', async () => {
185+
process.env['INPUT_SCOPE'] = 'myscope';
186+
fs.writeFileSync(rcFile, `registry=MMM`);
187+
await auth.configAuthentication('https://registry.npmjs.org/', 'true');
188+
let contents = fs.readFileSync(rcFile, {encoding: 'utf8'});
189+
expect(contents).toBe(
190+
`registry=MMM${os.EOL}//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}${os.EOL}@myscope:registry=https://registry.npmjs.org/${os.EOL}always-auth=true`
191+
);
192+
});
193+
194+
it('configAuthentication should overwrite only one scoped', async () => {
195+
process.env['INPUT_SCOPE'] = 'myscope';
196+
fs.writeFileSync(
197+
rcFile,
198+
`@otherscope:registry=NNN${os.EOL}@myscope:registry=MMM`
199+
);
200+
await auth.configAuthentication('https://registry.npmjs.org/', 'true');
201+
let contents = fs.readFileSync(rcFile, {encoding: 'utf8'});
202+
expect(contents).toBe(
203+
`@otherscope:registry=NNN${os.EOL}//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}${os.EOL}@myscope:registry=https://registry.npmjs.org/${os.EOL}always-auth=true`
204+
);
205+
});
206+
207+
it('configAuthentication should add scoped to another scoped', async () => {
208+
process.env['INPUT_SCOPE'] = 'myscope';
209+
fs.writeFileSync(rcFile, `@otherscope:registry=MMM`);
210+
await auth.configAuthentication('https://registry.npmjs.org/', 'true');
211+
let contents = fs.readFileSync(rcFile, {encoding: 'utf8'});
212+
expect(contents).toBe(
213+
`@otherscope:registry=MMM${os.EOL}//registry.npmjs.org/:_authToken=\${NODE_AUTH_TOKEN}${os.EOL}@myscope:registry=https://registry.npmjs.org/${os.EOL}always-auth=true`
214+
);
215+
});
135216
});

0 commit comments

Comments
 (0)