File size: 4,306 Bytes
1e92f2d |
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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
import fs from 'fs';
import parser from '@automattic/calypso-config/parser';
jest.mock( 'fs', () => ( {
existsSync: jest.fn(),
readFileSync: jest.fn(),
} ) );
function mockFs( files ) {
fs.existsSync.mockImplementation( ( path ) => !! files[ path ] );
fs.readFileSync.mockImplementation( ( path ) => files[ path ] );
}
const withEnv = ( env = {} ) => {
for ( const [ k, v ] of Object.entries( env ) ) {
process.env[ k ] = v;
}
};
function setValidSecrets() {
mockFs( {
'/valid-path/secrets.json': JSON.stringify( {
secret: 'very',
wpcom_calypso_rest_api_key: 'rest_api',
wpcom_calypso_support_session_rest_api_key: 'session_api',
} ),
'/valid-path/empty-secrets.json': JSON.stringify( {
secret: 'fromempty',
} ),
} );
}
function setValidEnvFiles() {
mockFs( {
'/valid-path/_shared.json': JSON.stringify( {
shared_only: 'shared',
myenv_override: 'shared',
features: {
enabledFeature1: true,
disabledFeature1: false,
enabledFeature2: true,
disabledFeature2: false,
},
} ),
'/valid-path/myenv.json': JSON.stringify( {
myenv_only: 'myenv',
myenv_override: 'myenv',
myenvlocal_override: 'myenv',
} ),
'/valid-path/myenv.local.json': JSON.stringify( {
myenvlocal_only: 'myenvlocal',
myenvlocal_override: 'myenvlocal',
} ),
} );
}
function setEmptySecrets() {
mockFs( {
'/valid-path/secrets.json': JSON.stringify( {
secret: 'very',
} ),
'/valid-path/_shared.json': JSON.stringify( {
features: {
'wpcom-user-bootstrap': true,
},
} ),
} );
}
describe( 'parser', () => {
let realProcessEnv;
beforeEach( () => {
realProcessEnv = { ...process.env };
} );
afterEach( () => {
fs.readFileSync.mockReset();
fs.readFileSync.mockReset();
process.env = { ...realProcessEnv };
} );
test( 'should return empty objects for an invalid path', () => {
const data = parser( '/invalid-path' );
expect( data ).toEqual( { serverData: {}, clientData: {} } );
} );
test( 'server should have secrets and client should not', () => {
setValidSecrets();
const data = parser( '/valid-path' );
expect( data.clientData ).not.toHaveProperty( 'secret' );
expect( data.serverData ).toHaveProperty( 'secret' );
} );
test( 'should cascade configs', () => {
setValidEnvFiles();
const { serverData: data } = parser( '/valid-path', {
env: 'myenv',
} );
expect( data ).toHaveProperty( 'shared_only', 'shared' );
expect( data ).toHaveProperty( 'myenv_only', 'myenv' );
expect( data ).toHaveProperty( 'myenvlocal_only', 'myenvlocal' );
expect( data ).toHaveProperty( 'myenv_override', 'myenv' );
expect( data ).toHaveProperty( 'myenvlocal_override', 'myenvlocal' );
expect( data ).toHaveProperty( 'features', {
enabledFeature1: true,
enabledFeature2: true,
disabledFeature1: false,
disabledFeature2: false,
} );
} );
test( 'should override enabled feature when disabledFeatures set', () => {
setValidEnvFiles();
const { serverData: data } = parser( '/valid-path', {
env: 'myenv',
disabledFeatures: 'enabledFeature2',
} );
expect( data ).toHaveProperty( 'features.enabledFeature2', false );
} );
test( 'should override disabled feature when enabledFeatures set', () => {
setValidEnvFiles();
const { serverData: data } = parser( '/valid-path', {
env: 'myenv',
enabledFeatures: 'disabledFeature2',
} );
expect( data ).toHaveProperty( 'features.disabledFeature2', true );
} );
test( 'should override secrets with env vars', () => {
withEnv( {
WPCOM_CALYPSO_REST_API_KEY: 'foo',
WPCOM_CALYPSO_SUPPORT_SESSION_REST_API_KEY: 'bar',
} );
setValidSecrets();
const { serverData } = parser( '/valid-path' );
expect( serverData.wpcom_calypso_rest_api_key ).toBe( 'foo' );
expect( serverData.wpcom_calypso_support_session_rest_api_key ).toBe( 'bar' );
} );
test( 'should explicitly set user-bootstrapping to false if there are no real secrets', () => {
setEmptySecrets();
const errorSpy = jest.fn();
global.console = { error: errorSpy };
const { serverData, clientData } = parser( '/valid-path' );
expect( serverData.features[ 'wpcom-user-bootstrap' ] ).toBe( false );
expect( clientData.features[ 'wpcom-user-bootstrap' ] ).toBe( false );
expect( errorSpy ).toHaveBeenCalledTimes( 1 );
} );
} );
|