|
| 1 | +import { Config } from '@oclif/core'; |
| 2 | +import chalk from 'chalk'; |
| 3 | +import fs from 'fs-extra'; |
| 4 | +import { vol } from 'memfs'; |
| 5 | + |
| 6 | +import { EnvironmentSecretType, EnvironmentVariableVisibility } from '../../../graphql/generated'; |
| 7 | +import { |
| 8 | + EnvironmentVariableWithFileContent, |
| 9 | + EnvironmentVariablesQuery, |
| 10 | +} from '../../../graphql/queries/EnvironmentVariablesQuery'; |
| 11 | +import Log from '../../../log'; |
| 12 | +import { confirmAsync } from '../../../prompts'; |
| 13 | +import EnvPull from '../pull'; |
| 14 | + |
| 15 | +jest.mock('fs'); |
| 16 | +jest.mock('../../../graphql/queries/EnvironmentVariablesQuery'); |
| 17 | +// jest.mock('../../../log'); |
| 18 | +jest.mock('../../../prompts'); |
| 19 | +jest.mock('../../../utils/prompts'); |
| 20 | + |
| 21 | +beforeEach(async () => { |
| 22 | + vol.reset(); |
| 23 | +}); |
| 24 | + |
| 25 | +describe(EnvPull, () => { |
| 26 | + const mockConfig = {} as unknown as Config; |
| 27 | + const graphqlClient = {}; |
| 28 | + const projectId = 'test-project-id'; |
| 29 | + const mockContext = { |
| 30 | + projectId, |
| 31 | + loggedIn: { graphqlClient }, |
| 32 | + projectDir: '/mock/project/dir', |
| 33 | + }; |
| 34 | + |
| 35 | + beforeEach(() => { |
| 36 | + jest.resetAllMocks(); |
| 37 | + vol.reset(); |
| 38 | + }); |
| 39 | + |
| 40 | + it('pulls environment variables and writes to .env file', async () => { |
| 41 | + const mockVariables = [ |
| 42 | + { |
| 43 | + name: 'TEST_VAR', |
| 44 | + value: 'value', |
| 45 | + type: EnvironmentSecretType.String, |
| 46 | + visibility: EnvironmentVariableVisibility.Public, |
| 47 | + }, |
| 48 | + { |
| 49 | + name: 'FILE_VAR', |
| 50 | + valueWithFileContent: 'value', |
| 51 | + type: EnvironmentSecretType.FileBase64, |
| 52 | + visibility: EnvironmentVariableVisibility.Public, |
| 53 | + }, |
| 54 | + { |
| 55 | + name: 'SECRET_VAR', |
| 56 | + value: null, |
| 57 | + type: EnvironmentSecretType.String, |
| 58 | + visibility: EnvironmentVariableVisibility.Secret, |
| 59 | + }, |
| 60 | + { |
| 61 | + name: 'SENSITIVE_VAR', |
| 62 | + value: 'value', |
| 63 | + type: EnvironmentSecretType.String, |
| 64 | + visibility: EnvironmentVariableVisibility.Sensitive, |
| 65 | + }, |
| 66 | + ]; |
| 67 | + jest |
| 68 | + .mocked(EnvironmentVariablesQuery.byAppIdWithSensitiveAsync) |
| 69 | + .mockResolvedValue(mockVariables as EnvironmentVariableWithFileContent[]); |
| 70 | + |
| 71 | + // @ts-expect-error |
| 72 | + jest.spyOn(fs, 'writeFile').mockResolvedValue(undefined); |
| 73 | + // @ts-expect-error |
| 74 | + jest.spyOn(Log, 'log').mockResolvedValue(undefined); |
| 75 | + |
| 76 | + const command = new EnvPull(['--environment', 'production'], mockConfig); |
| 77 | + // @ts-expect-error |
| 78 | + jest.spyOn(command, 'getContextAsync').mockReturnValue(mockContext); |
| 79 | + |
| 80 | + await command.runAsync(); |
| 81 | + |
| 82 | + expect(EnvironmentVariablesQuery.byAppIdWithSensitiveAsync).toHaveBeenCalledWith( |
| 83 | + graphqlClient, |
| 84 | + { |
| 85 | + appId: projectId, |
| 86 | + environment: 'PRODUCTION', |
| 87 | + includeFileContent: true, |
| 88 | + } |
| 89 | + ); |
| 90 | + |
| 91 | + const expectedFileContent = [ |
| 92 | + '# Environment: production', |
| 93 | + '', |
| 94 | + 'TEST_VAR=value', |
| 95 | + 'FILE_VAR=/mock/project/dir/.eas/.env/FILE_VAR', |
| 96 | + '# SECRET_VAR=***** (secret)', |
| 97 | + 'SENSITIVE_VAR=value', |
| 98 | + ]; |
| 99 | + |
| 100 | + expect(fs.writeFile).toHaveBeenNthCalledWith( |
| 101 | + 1, |
| 102 | + '/mock/project/dir/.eas/.env/FILE_VAR', |
| 103 | + 'value', |
| 104 | + 'base64' |
| 105 | + ); |
| 106 | + |
| 107 | + expect(fs.writeFile).toHaveBeenNthCalledWith(2, '.env.local', expectedFileContent.join('\n')); |
| 108 | + |
| 109 | + expect(Log.log).toHaveBeenCalledWith( |
| 110 | + `Pulled plain text and sensitive environment variables from "production" environment to .env.local.` |
| 111 | + ); |
| 112 | + }); |
| 113 | + |
| 114 | + it('throws an error if the environment is invalid', async () => { |
| 115 | + const command = new EnvPull(['--environment', 'invalid'], mockConfig); |
| 116 | + // @ts-expect-error |
| 117 | + jest.spyOn(command, 'getContextAsync').mockReturnValue(mockContext); |
| 118 | + |
| 119 | + await expect(command.runAsync()).rejects.toThrow( |
| 120 | + /Expected --environment=invalid to be one of: development, preview, production/ |
| 121 | + ); |
| 122 | + }); |
| 123 | + |
| 124 | + it('overwrites existing .env file if confirmed', async () => { |
| 125 | + jest |
| 126 | + .mocked(EnvironmentVariablesQuery.byAppIdWithSensitiveAsync) |
| 127 | + .mockResolvedValue([] as EnvironmentVariableWithFileContent[]); |
| 128 | + |
| 129 | + vol.fromJSON({ |
| 130 | + './.env.local': '', |
| 131 | + }); |
| 132 | + |
| 133 | + // @ts-expect-error |
| 134 | + jest.spyOn(fs, 'writeFile').mockResolvedValue(undefined); |
| 135 | + jest.mocked(confirmAsync).mockResolvedValue(true); |
| 136 | + |
| 137 | + const command = new EnvPull(['--environment', 'production'], mockConfig); |
| 138 | + // @ts-expect-error |
| 139 | + jest.spyOn(command, 'getContextAsync').mockReturnValue(mockContext); |
| 140 | + |
| 141 | + await command.runAsync(); |
| 142 | + |
| 143 | + expect(confirmAsync).toHaveBeenCalledWith({ |
| 144 | + message: 'File .env.local already exists. Do you want to overwrite it?', |
| 145 | + }); |
| 146 | + expect(fs.writeFile).toHaveBeenCalled(); |
| 147 | + }); |
| 148 | + |
| 149 | + it('aborts if user declines to overwrite existing .env file', async () => { |
| 150 | + vol.fromJSON({ |
| 151 | + './.env.local': 'existing content', |
| 152 | + }); |
| 153 | + jest.mocked(confirmAsync).mockResolvedValue(false); |
| 154 | + // @ts-expect-error |
| 155 | + jest.spyOn(fs, 'writeFile').mockResolvedValue(undefined); |
| 156 | + |
| 157 | + const command = new EnvPull(['--environment', 'production'], mockConfig); |
| 158 | + // @ts-expect-error |
| 159 | + jest.spyOn(command, 'getContextAsync').mockReturnValue(mockContext); |
| 160 | + |
| 161 | + await expect(command.runAsync()).rejects.toThrow('File .env.local already exists.'); |
| 162 | + expect(fs.writeFile).not.toHaveBeenCalled(); |
| 163 | + }); |
| 164 | + |
| 165 | + it('handles secret variables correctly', async () => { |
| 166 | + const mockVariables = [ |
| 167 | + { |
| 168 | + name: 'SECRET_VAR', |
| 169 | + value: '*****', |
| 170 | + type: EnvironmentSecretType.String, |
| 171 | + visibility: EnvironmentVariableVisibility.Secret, |
| 172 | + }, |
| 173 | + ]; |
| 174 | + jest |
| 175 | + .mocked(EnvironmentVariablesQuery.byAppIdWithSensitiveAsync) |
| 176 | + .mockResolvedValue(mockVariables as EnvironmentVariableWithFileContent[]); |
| 177 | + |
| 178 | + const command = new EnvPull(['--environment', 'production', '--non-interactive'], mockConfig); |
| 179 | + |
| 180 | + // @ts-expect-error |
| 181 | + jest.spyOn(command, 'getContextAsync').mockReturnValue(mockContext); |
| 182 | + // @ts-expect-error |
| 183 | + jest.spyOn(fs, 'writeFile').mockResolvedValue(undefined); |
| 184 | + jest.spyOn(Log, 'log').mockImplementation(() => {}); |
| 185 | + |
| 186 | + await command.runAsync(); |
| 187 | + |
| 188 | + expect(fs.writeFile).toHaveBeenCalledWith( |
| 189 | + '.env.local', |
| 190 | + expect.stringContaining('# SECRET_VAR=***** (secret)') |
| 191 | + ); |
| 192 | + expect(Log.log).toHaveBeenCalledWith( |
| 193 | + "The following variables have the secret visibility and can't be read outside of EAS servers. Set their values manually in your .env file: SECRET_VAR." |
| 194 | + ); |
| 195 | + }); |
| 196 | + |
| 197 | + it('diffLogAsync generates correct diff log', async () => { |
| 198 | + const mockVariables = [ |
| 199 | + { name: 'NEW_VAR', value: 'new_value', type: EnvironmentSecretType.String }, |
| 200 | + { name: 'UNCHANGED_VAR', value: 'unchanged_value', type: EnvironmentSecretType.String }, |
| 201 | + { |
| 202 | + name: 'UNCHANGED_FILE_VAR', |
| 203 | + valueWithFileContent: Buffer.from('unchanged_value').toString('base64'), |
| 204 | + type: EnvironmentSecretType.FileBase64, |
| 205 | + }, |
| 206 | + { name: 'CHANGED_VAR', value: 'changed_value', type: EnvironmentSecretType.String }, |
| 207 | + { |
| 208 | + name: 'CHANGED_FILE_VAR', |
| 209 | + valueWithFileContent: Buffer.from('changed_value').toString('base64'), |
| 210 | + type: EnvironmentSecretType.FileBase64, |
| 211 | + }, |
| 212 | + ]; |
| 213 | + |
| 214 | + vol.fromJSON({ |
| 215 | + './.eas/.env/UNCHANGED_FILE_VAR': 'unchanged_value', |
| 216 | + './.eas/.env/CHANGED_FILE_VAR': 'changing_value', |
| 217 | + }); |
| 218 | + |
| 219 | + const currentEnvLocal = { |
| 220 | + UNCHANGED_VAR: 'unchanged_value', |
| 221 | + CHANGED_VAR: 'changing_value', |
| 222 | + UNCHANGED_FILE_VAR: './.eas/.env/UNCHANGED_FILE_VAR', |
| 223 | + CHANGED_FILE_VAR: './.eas/.env/CHANGED_FILE_VAR', |
| 224 | + REMOVED_VAR: 'removed_value', |
| 225 | + }; |
| 226 | + |
| 227 | + const command = new EnvPull([], mockConfig); |
| 228 | + // @ts-expect-error |
| 229 | + jest.spyOn(command, 'getContextAsync').mockReturnValue(mockContext); |
| 230 | + |
| 231 | + // @ts-expect-error |
| 232 | + const diffLog = await command.diffLogAsync(mockVariables, currentEnvLocal); |
| 233 | + |
| 234 | + expect(diffLog).toEqual([ |
| 235 | + chalk.green('+ NEW_VAR'), |
| 236 | + ' UNCHANGED_VAR', |
| 237 | + ' UNCHANGED_FILE_VAR', |
| 238 | + chalk.yellow('~ CHANGED_VAR'), |
| 239 | + chalk.yellow('~ CHANGED_FILE_VAR'), |
| 240 | + chalk.red('- REMOVED_VAR'), |
| 241 | + ]); |
| 242 | + }); |
| 243 | +}); |
0 commit comments