@@ -257,3 +257,112 @@ describe("EnvironmentVariablesRepository.getVariableValuesForKeys", () => {
257257 }
258258 ) ;
259259} ) ;
260+
261+ describe ( "EnvironmentVariablesRepository.editValue" , ( ) => {
262+ postgresTest (
263+ "permanently marks an existing value as secret while updating its value" ,
264+ async ( { prisma } ) => {
265+ const { user, organization, project } = await createTestOrgProjectWithMember ( prisma ) ;
266+ const environment = await createRuntimeEnvironment ( prisma , {
267+ projectId : project . id ,
268+ organizationId : organization . id ,
269+ type : "PRODUCTION" ,
270+ } ) ;
271+ const repository = new EnvironmentVariablesRepository ( prisma , prisma ) ;
272+
273+ await createEnvironmentVariable ( repository , project . id , {
274+ environmentId : environment . id ,
275+ key : "BECOMES_SECRET" ,
276+ value : "plain-value" ,
277+ userId : user . id ,
278+ } ) ;
279+
280+ const variable = await prisma . environmentVariable . findFirstOrThrow ( {
281+ where : { projectId : project . id , key : "BECOMES_SECRET" } ,
282+ include : { values : { where : { environmentId : environment . id } } } ,
283+ } ) ;
284+ const originalVersion = variable . values [ 0 ] ! . version ;
285+
286+ const result = await repository . editValue ( project . id , {
287+ id : variable . id ,
288+ environmentId : environment . id ,
289+ value : "new-secret-value" ,
290+ isSecret : true ,
291+ lastUpdatedBy : { type : "user" , userId : user . id } ,
292+ } ) ;
293+
294+ expect ( result ) . toEqual ( { success : true } ) ;
295+
296+ const updatedValue = await prisma . environmentVariableValue . findUniqueOrThrow ( {
297+ where : {
298+ variableId_environmentId : {
299+ variableId : variable . id ,
300+ environmentId : environment . id ,
301+ } ,
302+ } ,
303+ } ) ;
304+ expect ( updatedValue . isSecret ) . toBe ( true ) ;
305+ expect ( updatedValue . version ) . toBe ( originalVersion + 1 ) ;
306+
307+ const unredacted = await repository . getEnvironment ( project . id , environment . id ) ;
308+ expect ( unredacted ) . toEqual ( [
309+ expect . objectContaining ( { key : "BECOMES_SECRET" , value : "new-secret-value" } ) ,
310+ ] ) ;
311+
312+ const redacted = await repository . getEnvironmentWithRedactedSecrets (
313+ project . id ,
314+ environment . id
315+ ) ;
316+ expect ( redacted ) . toEqual ( [
317+ expect . objectContaining ( { key : "BECOMES_SECRET" , value : "<redacted>" , isSecret : true } ) ,
318+ ] ) ;
319+ }
320+ ) ;
321+
322+ postgresTest ( "does not change an existing secret value back to plaintext" , async ( { prisma } ) => {
323+ const { user, organization, project } = await createTestOrgProjectWithMember ( prisma ) ;
324+ const environment = await createRuntimeEnvironment ( prisma , {
325+ projectId : project . id ,
326+ organizationId : organization . id ,
327+ type : "PRODUCTION" ,
328+ } ) ;
329+ const repository = new EnvironmentVariablesRepository ( prisma , prisma ) ;
330+
331+ await createEnvironmentVariable ( repository , project . id , {
332+ environmentId : environment . id ,
333+ key : "STAYS_SECRET" ,
334+ value : "original-secret" ,
335+ isSecret : true ,
336+ userId : user . id ,
337+ } ) ;
338+
339+ const variable = await prisma . environmentVariable . findFirstOrThrow ( {
340+ where : { projectId : project . id , key : "STAYS_SECRET" } ,
341+ } ) ;
342+
343+ const result = await repository . editValue ( project . id , {
344+ id : variable . id ,
345+ environmentId : environment . id ,
346+ value : "updated-secret" ,
347+ isSecret : false ,
348+ lastUpdatedBy : { type : "user" , userId : user . id } ,
349+ } ) ;
350+
351+ expect ( result ) . toEqual ( { success : true } ) ;
352+
353+ const updatedValue = await prisma . environmentVariableValue . findUniqueOrThrow ( {
354+ where : {
355+ variableId_environmentId : {
356+ variableId : variable . id ,
357+ environmentId : environment . id ,
358+ } ,
359+ } ,
360+ } ) ;
361+ expect ( updatedValue . isSecret ) . toBe ( true ) ;
362+
363+ const unredacted = await repository . getEnvironment ( project . id , environment . id ) ;
364+ expect ( unredacted ) . toEqual ( [
365+ expect . objectContaining ( { key : "STAYS_SECRET" , value : "updated-secret" } ) ,
366+ ] ) ;
367+ } ) ;
368+ } ) ;
0 commit comments