UNPKG

11.7 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const tslib_1 = require("tslib");
4const fs = require("fs-extra");
5const netrc_1 = require("./netrc");
6fs.mkdirpSync('tmp');
7process.env.NETRC_PARSER_DEBUG = '1';
8const skipOnWindows = process.platform === 'win32' ? test.skip : test;
9test('can read system netrc', () => {
10 let netrc = new netrc_1.Netrc();
11 netrc.loadSync();
12 expect(netrc.machines).toBeTruthy();
13});
14test('can read system netrc async', () => tslib_1.__awaiter(this, void 0, void 0, function* () {
15 let netrc = new netrc_1.Netrc();
16 yield netrc.load();
17 expect(netrc.machines).toBeTruthy();
18}));
19test('bad default order', () => tslib_1.__awaiter(this, void 0, void 0, function* () {
20 const f = `tmp/netrc`;
21 fs.writeFileSync(f, `# I am a comment
22 machine mail.google.com
23 login joe@gmail.com
24 account gmail
25 password somethingSecret
26 # I am another comment
27
28 default
29 login anonymous
30 password joe@example.com
31
32 machine ray login demo password mypassword
33`);
34 const netrc = new netrc_1.Netrc(f);
35 yield netrc.load();
36 expect(netrc.machines['mail.google.com'].login).toEqual('joe@gmail.com');
37 expect(netrc.machines['mail.google.com'].account).toEqual('gmail');
38 expect(netrc.machines['mail.google.com'].password).toEqual('somethingSecret');
39 expect(netrc.machines['ray'].login).toEqual('demo');
40 expect(netrc.machines['ray'].password).toEqual('mypassword');
41}));
42test('it loads the netrc file with comments', () => {
43 const f = `tmp/netrc`;
44 fs.writeFileSync(f, `machine api.dickeyxxx.com # foo
45 login jeff@foo.com
46 password myapikey`);
47 const netrc = new netrc_1.Netrc(f);
48 netrc.loadSync();
49 expect(netrc.machines['api.dickeyxxx.com'].login).toEqual('jeff@foo.com');
50 expect(netrc.machines['api.dickeyxxx.com'].password).toEqual('myapikey');
51});
52test('default only', () => {
53 const f = `tmp/netrc`;
54 fs.writeFileSync(f, `# this is my netrc with only a default
55default
56 login ld # this is my default username
57 password pdcom
58 password myapikey`);
59 const netrc = new netrc_1.Netrc(f);
60 netrc.loadSync();
61 expect(netrc.default.login).toEqual('ld');
62 expect(netrc.default.password).toEqual('pdcom');
63});
64test('good', () => {
65 const f = `tmp/netrc`;
66 fs.writeFileSync(f, `# I am a comment
67machine mail.google.com
68\tlogin joe@gmail.com
69 account justagmail #end of line comment with trailing space
70 password somethingSecret
71 # I am another comment
72
73macdef allput
74put src/*
75
76macdef allput2
77 put src/*
78put src2/*
79
80machine ray login demo password mypassword
81
82machine weirdlogin login uname password pass#pass
83
84default
85 login anonymous
86 password joe@example.com
87`);
88 const netrc = new netrc_1.Netrc(f);
89 netrc.loadSync();
90 expect(netrc.machines['mail.google.com'].login).toEqual('joe@gmail.com');
91 expect(netrc.machines['mail.google.com'].account).toEqual('justagmail');
92 expect(netrc.machines['mail.google.com'].password).toEqual('somethingSecret');
93});
94const gpgEncrypted = `-----BEGIN PGP MESSAGE-----
95Version: GnuPG v2
96
97hIwD1rghrTHCzmIBA/9JIhd9NaY64C7QMIOa8KV/e97Hs9he6EAHdhDUMeb6/5HU
98KaxHX77rHjF0TxNUumQrMTfp+EjKzjuDqTxrv0TnpqB8JYhwLqVCGPM+OvjNlILy
99/EdDpkqEaKqM4KArRQjE4n8ifAi5CbldI/mO+oBvHTq5StJDNEhE+xMjRzGJ29LA
100VQEWWdR291Z8Y0cbZwX2DmGsPuo6tX0JeWQlG9ms8966wVk2LKFuUyynHBVjcsjv
101REKnai8ZixhaKRBE/NOiLo/Eqp6nI7/i8YU+mYV0rFljpLSnQ7LJcgw3ItyKXQ9F
102ws16ShzCIGM11JFySwb0NoV6H9VSakfu2LN1RpKFD2lvc6i75N0NWf0Jh/mKHFz+
103ugLe8sik/Zu8grrxtOVxfgtjFEQvjT3u02D4pDQP1lNp7SjVfqUC+XnxWQC+SQVC
104kKvydwB3oZqwHp6jpgLVTxjTfhm1vNTB7gAbgNOF63yQ/Wmrn3Pe38huh+TIKJCy
105pQgBLBordnqQajWt1ao+8AZiAsOooF0wJqm/mH1Og5/ADuhvZEQ=
106=PGaL
107-----END PGP MESSAGE-----`;
108skipOnWindows('good.gpg sync', () => {
109 const f = `tmp/netrc.gpg`;
110 fs.writeFileSync(f, gpgEncrypted);
111 const netrc = new netrc_1.Netrc(f);
112 netrc.loadSync();
113 expect(netrc.machines['mail.google.com'].login).toEqual('joe@gmail.com');
114 expect(netrc.machines['mail.google.com'].account).toEqual('justagmail');
115 expect(netrc.machines['mail.google.com'].password).toEqual('somethingSecret');
116 netrc.saveSync();
117 expect(fs.readFileSync(f, { encoding: 'utf8' })).toContain('-----BEGIN PGP MESSAGE-----');
118});
119skipOnWindows('good.gpg', () => tslib_1.__awaiter(this, void 0, void 0, function* () {
120 const f = `tmp/netrc.gpg`;
121 yield fs.writeFile(f, gpgEncrypted);
122 const netrc = new netrc_1.Netrc(f);
123 yield netrc.load();
124 expect(netrc.machines['mail.google.com'].login).toEqual('joe@gmail.com');
125 expect(netrc.machines['mail.google.com'].account).toEqual('justagmail');
126 expect(netrc.machines['mail.google.com'].password).toEqual('somethingSecret');
127 yield netrc.save();
128 expect(fs.readFileSync(f, { encoding: 'utf8' })).toContain('-----BEGIN PGP MESSAGE-----');
129}));
130test('invalid', () => {
131 expect.assertions(1);
132 const f = `tmp/netrc`;
133 fs.writeFileSync(f, 'machine');
134 try {
135 let netrc = new netrc_1.Netrc(f);
136 netrc.loadSync();
137 }
138 catch (err) {
139 expect(err.message).toContain('Unexpected character during netrc parsing');
140 }
141});
142test('invalid async', () => tslib_1.__awaiter(this, void 0, void 0, function* () {
143 expect.assertions(1);
144 const f = `tmp/netrc`;
145 fs.writeFileSync(f, 'machine');
146 try {
147 let netrc = new netrc_1.Netrc(f);
148 yield netrc.load();
149 }
150 catch (err) {
151 expect(err.message).toContain('Unexpected character during netrc parsing');
152 }
153}));
154test('saving', () => {
155 const f = `tmp/netrc`;
156 fs.writeFileSync(f, `# I am a comment
157machine mail.google.com
158\tlogin joe@gmail.com
159 password somethingSecret #end of line comment with trailing space
160 # I am another comment
161
162macdef allput
163put src/*
164
165macdef allput2
166 put src/*
167put src2/*
168
169machine ray login demo password mypassword
170
171machine weirdlogin login uname password pass#pass
172
173default
174 login anonymous
175 password joe@example.com
176`);
177 const netrc = new netrc_1.Netrc(f);
178 netrc.loadSync();
179 netrc.machines['mail.google.com'].login = 'joe2@gmail.com';
180 netrc.machines['mail.google.com'].account = 'justanaccount';
181 netrc.machines.ray.login = 'demo2';
182 netrc.machines.ray.account = 'newaccount';
183 netrc.machines['new'] = { login: 'myuser', password: 'mypass' };
184 netrc.saveSync();
185 expect(fs.readFileSync(f, 'utf8')).toEqual(`# I am a comment
186machine mail.google.com
187\taccount justanaccount
188\tlogin joe2@gmail.com
189 password somethingSecret #end of line comment with trailing space
190 # I am another comment
191
192macdef allput
193put src/*
194
195macdef allput2
196 put src/*
197put src2/*
198
199machine ray account newaccount login demo2 password mypassword
200
201machine weirdlogin login uname password pass#pass
202
203default
204 login anonymous
205 password joe@example.com
206
207machine new
208 login myuser
209 password mypass
210`);
211});
212test('adding a machine should create a new entry', () => tslib_1.__awaiter(this, void 0, void 0, function* () {
213 const f = `tmp/netrc`;
214 const beforeSave = `machine api.dickeyxxx.com # foo
215 login jeff@foo.com
216 password myapikey`;
217 fs.writeFileSync(f, beforeSave);
218 const netrc = new netrc_1.Netrc(f);
219 yield netrc.load();
220 netrc.machines['foo.bar.com'] = { login: 'foo@bar.com', password: 'foopassword' };
221 yield netrc.save();
222 const afterSave = `machine api.dickeyxxx.com # foo
223 login jeff@foo.com
224 password myapikey
225
226machine foo.bar.com
227 login foo@bar.com
228 password foopassword\n`;
229 expect(fs.readFileSync(f, 'utf8')).toEqual(afterSave);
230}));
231test('removing a machine', () => tslib_1.__awaiter(this, void 0, void 0, function* () {
232 const f = `tmp/netrc`;
233 const beforeSave = `machine api.dickeyxxx.com # foo
234 login jeff@foo.com
235 password myapikey
236machine foo.bar.com
237 password foopassword
238 login foo@bar.com
239`;
240 fs.writeFileSync(f, beforeSave);
241 const netrc = new netrc_1.Netrc(f);
242 yield netrc.load();
243 delete netrc.machines['api.dickeyxxx.com'];
244 yield netrc.save();
245 const afterSave = `machine foo.bar.com
246 password foopassword
247 login foo@bar.com\n`;
248 expect(fs.readFileSync(f, 'utf8')).toEqual(afterSave);
249}));
250test('setting machine to undefined', () => tslib_1.__awaiter(this, void 0, void 0, function* () {
251 const f = `tmp/netrc`;
252 const beforeSave = `machine api.dickeyxxx.com # foo
253 login jeff@foo.com
254 password myapikey
255machine foo.bar.com
256 password foopassword
257 login foo@bar.com
258`;
259 fs.writeFileSync(f, beforeSave);
260 const netrc = new netrc_1.Netrc(f);
261 yield netrc.load();
262 netrc.machines['api.dickeyxxx.com'] = undefined;
263 yield netrc.save();
264 const afterSave = `machine foo.bar.com
265 password foopassword
266 login foo@bar.com\n`;
267 expect(fs.readFileSync(f, 'utf8')).toEqual(afterSave);
268}));
269test('empty netrc', () => tslib_1.__awaiter(this, void 0, void 0, function* () {
270 const f = `tmp/netrc`;
271 const beforeSave = '';
272 fs.writeFileSync(f, beforeSave);
273 const netrc = new netrc_1.Netrc(f);
274 yield netrc.load();
275 netrc.machines['api.dickeyxxx.com'] = { login: 'foo', password: 'bar' };
276 netrc.machines['foo.dickeyxxx.com'] = { login: 'foo2', password: 'bar2' };
277 yield netrc.save();
278 const afterSave = `machine api.dickeyxxx.com
279 login foo
280 password bar
281
282machine foo.dickeyxxx.com
283 login foo2
284 password bar2
285`;
286 expect(fs.readFileSync(f, 'utf8')).toEqual(afterSave);
287}));
288test('set existing', () => tslib_1.__awaiter(this, void 0, void 0, function* () {
289 const f = `tmp/netrc`;
290 const beforeSave = 'machine foo password p login u';
291 fs.writeFileSync(f, beforeSave);
292 const netrc = new netrc_1.Netrc(f);
293 yield netrc.load();
294 netrc.machines['foo'] = { login: 'foo', password: 'bar' };
295 yield netrc.save();
296 const afterSave = `machine foo
297 login foo
298 password bar
299`;
300 expect(fs.readFileSync(f, 'utf8')).toEqual(afterSave);
301}));
302test('set new prop', () => tslib_1.__awaiter(this, void 0, void 0, function* () {
303 const f = `tmp/netrc`;
304 const beforeSave = 'machine foo password p login u';
305 fs.writeFileSync(f, beforeSave);
306 const netrc = new netrc_1.Netrc(f);
307 yield netrc.load();
308 netrc.machines['foo'].login = 'uu';
309 netrc.machines['foo'].account = 'bar';
310 netrc.machines['foo'].password = undefined;
311 expect('foo' in netrc.machines).toEqual(true);
312 expect('bar' in netrc.machines).toEqual(false);
313 delete netrc.machines['bar'];
314 yield netrc.save();
315 const afterSave = `machine foo account bar login uu
316`;
317 expect(fs.readFileSync(f, 'utf8')).toEqual(afterSave);
318}));
319test('file not found', () => tslib_1.__awaiter(this, void 0, void 0, function* () {
320 const f = `tmp/netrc`;
321 fs.removeSync(f);
322 const netrc = new netrc_1.Netrc(f);
323 yield netrc.load();
324 netrc.machines['foo'] = { login: 'u', password: 'p' };
325 yield netrc.save();
326 const afterSave = `machine foo\n login u\n password p\n`;
327 expect(fs.readFileSync(f, 'utf8')).toEqual(afterSave);
328}));
329test('file not found sync', () => {
330 const f = `tmp/netrc`;
331 fs.removeSync(f);
332 const netrc = new netrc_1.Netrc(f);
333 netrc.loadSync();
334 netrc.machines['foo'] = { login: 'u', password: 'p' };
335 netrc.saveSync();
336 const afterSave = `machine foo\n login u\n password p\n`;
337 expect(fs.readFileSync(f, 'utf8')).toEqual(afterSave);
338});
339test('default setting', () => {
340 const f = `tmp/netrc`;
341 fs.removeSync(f);
342 const netrc = new netrc_1.Netrc(f);
343 netrc.loadSync();
344 netrc.default = { login: 'u', password: 'p' };
345 netrc.saveSync();
346 const afterSave = `\ndefault\n login u\n password p\n`;
347 expect(fs.readFileSync(f, 'utf8')).toEqual(afterSave);
348 netrc.default = undefined;
349 netrc.saveSync();
350 expect(fs.readFileSync(f, 'utf8')).toEqual(`\n`);
351});