|
| 1 | +require('../spec_helper') |
| 2 | + |
| 3 | +const _ = require('lodash') |
| 4 | +const { filterRuntimeConfigForRecording } = require('../../lib/config') |
| 5 | +const { getCloudRecordingConfigKeys } = require('@packages/config') |
| 6 | + |
| 7 | +describe('lib/config filterRuntimeConfigForRecording', () => { |
| 8 | + it('returns an empty object for an empty config', () => { |
| 9 | + expect(filterRuntimeConfigForRecording({})).to.eql({}) |
| 10 | + }) |
| 11 | + |
| 12 | + it('removes rawJson, resolved, and keys not in the cloud recording allowlist', () => { |
| 13 | + const filtered = filterRuntimeConfigForRecording({ |
| 14 | + projectId: 'abc', |
| 15 | + baseUrl: 'http://localhost', |
| 16 | + rawJson: { huge: 'x'.repeat(1000) }, |
| 17 | + resolved: { baseUrl: { value: 'x', from: 'config' } }, |
| 18 | + socketId: 'sock-1', |
| 19 | + clientRoute: '/__/', |
| 20 | + arbitraryUserKey: { nested: true }, |
| 21 | + }) |
| 22 | + |
| 23 | + expect(filtered.rawJson).to.be.undefined |
| 24 | + expect(filtered.resolved).to.be.undefined |
| 25 | + expect(filtered.socketId).to.be.undefined |
| 26 | + expect(filtered.clientRoute).to.be.undefined |
| 27 | + expect(filtered.arbitraryUserKey).to.be.undefined |
| 28 | + expect(filtered.projectId).to.eq('abc') |
| 29 | + expect(filtered.baseUrl).to.eq('http://localhost') |
| 30 | + }) |
| 31 | + |
| 32 | + it('replaces env values with type placeholders', () => { |
| 33 | + const filtered = filterRuntimeConfigForRecording({ |
| 34 | + env: { |
| 35 | + STR: 'secret', |
| 36 | + NUM: 42, |
| 37 | + BOOL: false, |
| 38 | + OBJ: { a: 1 }, |
| 39 | + }, |
| 40 | + }) |
| 41 | + |
| 42 | + expect(filtered.env).to.eql({ |
| 43 | + STR: 'omitted: string', |
| 44 | + NUM: 'omitted: number', |
| 45 | + BOOL: 'omitted: boolean', |
| 46 | + OBJ: 'omitted: object', |
| 47 | + }) |
| 48 | + }) |
| 49 | + |
| 50 | + it('replaces expose values with type placeholders like env', () => { |
| 51 | + const filtered = filterRuntimeConfigForRecording({ |
| 52 | + expose: { |
| 53 | + API_URL: 'https://secret.example', |
| 54 | + FLAG: true, |
| 55 | + }, |
| 56 | + }) |
| 57 | + |
| 58 | + expect(filtered.expose).to.eql({ |
| 59 | + API_URL: 'omitted: string', |
| 60 | + FLAG: 'omitted: boolean', |
| 61 | + }) |
| 62 | + }) |
| 63 | + |
| 64 | + it('omits devServer webpackConfig and viteConfig but keeps other devServer fields', () => { |
| 65 | + const filtered = filterRuntimeConfigForRecording({ |
| 66 | + devServer: { |
| 67 | + bundler: 'webpack', |
| 68 | + framework: 'react', |
| 69 | + webpackConfig: { entry: 'app' }, |
| 70 | + viteConfig: { root: '/tmp' }, |
| 71 | + }, |
| 72 | + }) |
| 73 | + |
| 74 | + expect(filtered.devServer).to.eql({ |
| 75 | + bundler: 'webpack', |
| 76 | + framework: 'react', |
| 77 | + webpackConfig: 'omitted', |
| 78 | + viteConfig: 'omitted', |
| 79 | + }) |
| 80 | + }) |
| 81 | + |
| 82 | + it('preserves bundler and framework on devServerConfig and redacts other fields', () => { |
| 83 | + const filtered = filterRuntimeConfigForRecording({ |
| 84 | + devServerConfig: { |
| 85 | + bundler: 'vite', |
| 86 | + framework: 'vue', |
| 87 | + viteConfig: { build: { target: 'esnext' } }, |
| 88 | + mode: 'development', |
| 89 | + }, |
| 90 | + }) |
| 91 | + |
| 92 | + expect(filtered.devServerConfig).to.eql({ |
| 93 | + bundler: 'vite', |
| 94 | + framework: 'vue', |
| 95 | + viteConfig: 'omitted: object', |
| 96 | + mode: 'omitted: string', |
| 97 | + }) |
| 98 | + }) |
| 99 | + |
| 100 | + it('redacts non-object devServerConfig with a type placeholder string', () => { |
| 101 | + expect(filterRuntimeConfigForRecording({ devServerConfig: null }).devServerConfig) |
| 102 | + .to.eq('omitted: object') |
| 103 | + |
| 104 | + expect(filterRuntimeConfigForRecording({ devServerConfig: 'oops' }).devServerConfig) |
| 105 | + .to.eq('omitted: string') |
| 106 | + |
| 107 | + expect(filterRuntimeConfigForRecording({ devServerConfig: [] }).devServerConfig) |
| 108 | + .to.eq('omitted: object') |
| 109 | + }) |
| 110 | + |
| 111 | + it('does not set devServerConfig when undefined', () => { |
| 112 | + const filtered = filterRuntimeConfigForRecording({ projectId: 'x' }) |
| 113 | + |
| 114 | + expect(filtered).not.to.have.property('devServerConfig') |
| 115 | + }) |
| 116 | + |
| 117 | + it('keeps indexHtmlFile and allowlisted public keys only', () => { |
| 118 | + const filtered = filterRuntimeConfigForRecording({ |
| 119 | + indexHtmlFile: 'cypress/support/component-index.html', |
| 120 | + specPattern: '**/*.cy.ts', |
| 121 | + video: true, |
| 122 | + notARealOption: 'drop-me', |
| 123 | + }) |
| 124 | + |
| 125 | + expect(filtered.indexHtmlFile).to.eq('cypress/support/component-index.html') |
| 126 | + expect(filtered.specPattern).to.eq('**/*.cy.ts') |
| 127 | + expect(filtered.video).to.eq(true) |
| 128 | + expect(filtered.notARealOption).to.be.undefined |
| 129 | + }) |
| 130 | + |
| 131 | + it('output keys are a subset of getCloudRecordingConfigKeys()', () => { |
| 132 | + const allow = new Set(getCloudRecordingConfigKeys()) |
| 133 | + const filtered = filterRuntimeConfigForRecording({ |
| 134 | + projectId: 'p', |
| 135 | + devServer: { bundler: 'webpack', framework: 'react' }, |
| 136 | + devServerConfig: { bundler: 'webpack', framework: 'react' }, |
| 137 | + env: { K: 1 }, |
| 138 | + expose: { X: 'y' }, |
| 139 | + indexHtmlFile: 'index.html', |
| 140 | + extra: 'should-not-appear', |
| 141 | + }) |
| 142 | + |
| 143 | + _.each(_.keys(filtered), (key) => { |
| 144 | + expect(allow.has(key), `unexpected key on filtered config: ${key}`).to.equal(true) |
| 145 | + }) |
| 146 | + }) |
| 147 | +}) |
0 commit comments