1- import { replaceSecretsValue } from '../../../src/lib/secrets.js' ;
1+ import { replaceSecretsValue , transformEnvToEnvVars } from '../../../src/lib/secrets.js' ;
22
33describe ( 'Secrets' , ( ) => {
44 describe ( 'replaceSecretsValue()' , ( ) => {
5- it ( 'should work' , ( ) => {
6- const spy = vitest . spyOn ( console , 'error' ) ;
7-
5+ it ( 'should replace secret references with their values' , ( ) => {
86 const secrets = {
97 myProdToken : 'mySecretToken' ,
108 mongoUrl : 'mongo://bla@bla:supermongo.com:27017' ,
@@ -13,7 +11,6 @@ describe('Secrets', () => {
1311 TOKEN : '@myProdToken' ,
1412 USER : 'jakub.drobnik@apify.com' ,
1513 MONGO_URL : '@mongoUrl' ,
16- WARNING : '@doesNotExist' ,
1714 } ;
1815 const updatedEnv = replaceSecretsValue ( env , secrets ) ;
1916
@@ -22,9 +19,110 @@ describe('Secrets', () => {
2219 USER : 'jakub.drobnik@apify.com' ,
2320 MONGO_URL : secrets . mongoUrl ,
2421 } ) ;
22+ } ) ;
23+
24+ it ( 'should throw an error when secrets are missing' , ( ) => {
25+ const secrets = {
26+ myProdToken : 'mySecretToken' ,
27+ } ;
28+ const env = {
29+ TOKEN : '@myProdToken' ,
30+ MISSING_ONE : '@doesNotExist' ,
31+ MISSING_TWO : '@alsoMissing' ,
32+ } ;
33+
34+ expect ( ( ) => replaceSecretsValue ( env , secrets ) ) . toThrow (
35+ / T h e f o l l o w i n g s e c r e t s a r e m i s s i n g : \n \s + - d o e s N o t E x i s t \n \s + - a l s o M i s s i n g / ,
36+ ) ;
37+ } ) ;
38+
39+ it ( 'should mention --allow-missing-secrets in the error message' , ( ) => {
40+ const env = { TOKEN : '@doesNotExist' } ;
41+
42+ expect ( ( ) => replaceSecretsValue ( env , { } ) ) . toThrow ( / - - a l l o w - m i s s i n g - s e c r e t s / ) ;
43+ } ) ;
44+
45+ it ( 'should warn instead of throwing when allowMissing is true' , ( ) => {
46+ const spy = vitest . spyOn ( console , 'error' ) ;
47+
48+ const secrets = {
49+ myProdToken : 'mySecretToken' ,
50+ } ;
51+ const env = {
52+ TOKEN : '@myProdToken' ,
53+ MISSING_ONE : '@doesNotExist' ,
54+ MISSING_TWO : '@alsoMissing' ,
55+ } ;
56+
57+ const updatedEnv = replaceSecretsValue ( env , secrets , {
58+ allowMissing : true ,
59+ } ) ;
60+
61+ expect ( updatedEnv ) . toStrictEqual ( {
62+ TOKEN : secrets . myProdToken ,
63+ } ) ;
64+
65+ expect ( spy ) . toHaveBeenCalled ( ) ;
66+ expect ( spy . mock . calls . flat ( ) . join ( ' ' ) ) . to . include ( 'doesNotExist' ) ;
67+
68+ spy . mockRestore ( ) ;
69+ } ) ;
70+ } ) ;
71+
72+ describe ( 'transformEnvToEnvVars()' , ( ) => {
73+ it ( 'should transform env to envVars format with secret resolution' , ( ) => {
74+ const secrets = {
75+ myProdToken : 'mySecretToken' ,
76+ } ;
77+ const env = {
78+ TOKEN : '@myProdToken' ,
79+ USER : 'jakub.drobnik@apify.com' ,
80+ } ;
81+ const envVars = transformEnvToEnvVars ( env , secrets ) ;
82+
83+ expect ( envVars ) . toStrictEqual ( [
84+ { name : 'TOKEN' , value : 'mySecretToken' , isSecret : true } ,
85+ { name : 'USER' , value : 'jakub.drobnik@apify.com' } ,
86+ ] ) ;
87+ } ) ;
88+
89+ it ( 'should throw an error when secrets are missing' , ( ) => {
90+ const secrets = { } ;
91+ const env = {
92+ TOKEN : '@doesNotExist' ,
93+ USER : 'plain-value' ,
94+ } ;
95+
96+ expect ( ( ) => transformEnvToEnvVars ( env , secrets ) ) . toThrow (
97+ / T h e f o l l o w i n g s e c r e t s a r e m i s s i n g : \n \s + - d o e s N o t E x i s t / ,
98+ ) ;
99+ } ) ;
100+
101+ it ( 'should mention --allow-missing-secrets in the error message' , ( ) => {
102+ const env = { TOKEN : '@doesNotExist' } ;
103+
104+ expect ( ( ) => transformEnvToEnvVars ( env , { } ) ) . toThrow ( / - - a l l o w - m i s s i n g - s e c r e t s / ) ;
105+ } ) ;
106+
107+ it ( 'should warn instead of throwing when allowMissing is true' , ( ) => {
108+ const spy = vitest . spyOn ( console , 'error' ) ;
109+
110+ const secrets = { } ;
111+ const env = {
112+ TOKEN : '@doesNotExist' ,
113+ USER : 'plain-value' ,
114+ } ;
115+
116+ const envVars = transformEnvToEnvVars ( env , secrets , {
117+ allowMissing : true ,
118+ } ) ;
119+
120+ expect ( envVars ) . toStrictEqual ( [ { name : 'USER' , value : 'plain-value' } ] ) ;
25121
26122 expect ( spy ) . toHaveBeenCalled ( ) ;
27- expect ( spy . mock . calls [ 0 ] [ 0 ] ) . to . include ( 'Warning:' ) ;
123+ expect ( spy . mock . calls . flat ( ) . join ( ' ' ) ) . to . include ( 'doesNotExist' ) ;
124+
125+ spy . mockRestore ( ) ;
28126 } ) ;
29127 } ) ;
30128} ) ;
0 commit comments