aboutsummaryrefslogtreecommitdiffhomepage
path: root/lib/config/decrypt/legacy.spec.ts
blob: 936cd1c369ff5628d1bf136f4b8f1f218999d7d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { Fixtures } from '../../../test/fixtures';
import { CONFIG_VALIDATION } from '../../constants/error-messages';
import { decryptConfig } from '../decrypt';
import { GlobalConfig } from '../global';
import type { RenovateConfig } from '../types';

const privateKey = Fixtures.get('private.pem', '..');
const repository = 'abc/def';

describe('config/decrypt/legacy', () => {
  describe('decryptConfig()', () => {
    let config: RenovateConfig;

    beforeEach(() => {
      config = {};
      GlobalConfig.reset();
    });

    it('handles invalid encrypted type', async () => {
      config.encrypted = 1;
      GlobalConfig.set({ privateKey });
      const res = await decryptConfig(config, repository);
      expect(res.encrypted).toBeUndefined();
    });

    it('handles invalid encrypted value', async () => {
      config.encrypted = { a: 1 };
      GlobalConfig.set({ privateKey, privateKeyOld: 'invalid-key' });
      await expect(decryptConfig(config, repository)).rejects.toThrow(
        CONFIG_VALIDATION,
      );
    });

    it('replaces npm token placeholder in npmrc', async () => {
      GlobalConfig.set({
        privateKey: 'invalid-key',
        privateKeyOld: privateKey,
      }); // test old key failover
      config.npmrc =
        '//registry.npmjs.org/:_authToken=${NPM_TOKEN}\n//registry.npmjs.org/:_authToken=${NPM_TOKEN}\n';
      config.encrypted = {
        npmToken:
          'FLA9YHIzpE7YetAg/P0X46npGRCMqn7hgyzwX5ZQ9wYgu9BRRbTiBVsUIFTyM5BuP1Q22slT2GkWvFvum7GU236Y6QiT7Nr8SLvtsJn2XUuq8H7REFKzdy3+wqyyWbCErYTFyY1dcPM7Ht+CaGDWdd8u/FsoX7AdMRs/X1jNUo6iSmlUiyGlYDKF+QMnCJom1VPVgZXWsGKdjI2MLny991QMaiv0VajmFIh4ENv4CtXOl/1twvIl/6XTXAaqpJJKDTPZEuydi+PHDZmal2RAOfrkH4m0UURa7SlfpUlIg+EaqbNGp85hCYXLwRcEET1OnYr3rH1oYkcYJ40any1tvQ==',
      };
      const res = await decryptConfig(config, repository);
      expect(res.encrypted).toBeUndefined();
      expect(res.npmToken).toBe('abcdef-ghijklm-nopqf-stuvwxyz');
    });

    it('appends npm token in npmrc', async () => {
      GlobalConfig.set({ privateKey });
      config.npmrc = 'foo=bar\n';
      config.encrypted = {
        npmToken:
          'FLA9YHIzpE7YetAg/P0X46npGRCMqn7hgyzwX5ZQ9wYgu9BRRbTiBVsUIFTyM5BuP1Q22slT2GkWvFvum7GU236Y6QiT7Nr8SLvtsJn2XUuq8H7REFKzdy3+wqyyWbCErYTFyY1dcPM7Ht+CaGDWdd8u/FsoX7AdMRs/X1jNUo6iSmlUiyGlYDKF+QMnCJom1VPVgZXWsGKdjI2MLny991QMaiv0VajmFIh4ENv4CtXOl/1twvIl/6XTXAaqpJJKDTPZEuydi+PHDZmal2RAOfrkH4m0UURa7SlfpUlIg+EaqbNGp85hCYXLwRcEET1OnYr3rH1oYkcYJ40any1tvQ==',
      };
      const res = await decryptConfig(config, repository);
      expect(res.encrypted).toBeUndefined();
      expect(res.npmToken).toBe('abcdef-ghijklm-nopqf-stuvwxyz');
    });

    it('decrypts nested', async () => {
      GlobalConfig.set({ privateKey });
      config.packageFiles = [
        {
          packageFile: 'package.json',
          devDependencies: {
            encrypted: {
              branchPrefix:
                'FLA9YHIzpE7YetAg/P0X46npGRCMqn7hgyzwX5ZQ9wYgu9BRRbTiBVsUIFTyM5BuP1Q22slT2GkWvFvum7GU236Y6QiT7Nr8SLvtsJn2XUuq8H7REFKzdy3+wqyyWbCErYTFyY1dcPM7Ht+CaGDWdd8u/FsoX7AdMRs/X1jNUo6iSmlUiyGlYDKF+QMnCJom1VPVgZXWsGKdjI2MLny991QMaiv0VajmFIh4ENv4CtXOl/1twvIl/6XTXAaqpJJKDTPZEuydi+PHDZmal2RAOfrkH4m0UURa7SlfpUlIg+EaqbNGp85hCYXLwRcEET1OnYr3rH1oYkcYJ40any1tvQ==',
              npmToken:
                'FLA9YHIzpE7YetAg/P0X46npGRCMqn7hgyzwX5ZQ9wYgu9BRRbTiBVsUIFTyM5BuP1Q22slT2GkWvFvum7GU236Y6QiT7Nr8SLvtsJn2XUuq8H7REFKzdy3+wqyyWbCErYTFyY1dcPM7Ht+CaGDWdd8u/FsoX7AdMRs/X1jNUo6iSmlUiyGlYDKF+QMnCJom1VPVgZXWsGKdjI2MLny991QMaiv0VajmFIh4ENv4CtXOl/1twvIl/6XTXAaqpJJKDTPZEuydi+PHDZmal2RAOfrkH4m0UURa7SlfpUlIg+EaqbNGp85hCYXLwRcEET1OnYr3rH1oYkcYJ40any1tvQ==',
            },
          },
        },
        'backend/package.json',
      ];
      // TODO: fix types #22198
      const res = (await decryptConfig(config, repository)) as any;
      expect(res.encrypted).toBeUndefined();
      expect(res.packageFiles[0].devDependencies.encrypted).toBeUndefined();
      expect(res.packageFiles[0].devDependencies.branchPrefix).toBe(
        'abcdef-ghijklm-nopqf-stuvwxyz',
      );
      expect(res.packageFiles[0].devDependencies.npmToken).toBe(
        'abcdef-ghijklm-nopqf-stuvwxyz',
      );
    });
  });
});