@@ -10,12 +10,16 @@ OF ANY KIND, either express or implied. See the License for the specific languag
1010governing permissions and limitations under the License.
1111*/
1212const inquirer = require ( 'inquirer' )
13+ const fs = require ( 'fs-extra' )
1314
1415// mock prompt before import
1516const mockPrompt = jest . fn ( )
1617inquirer . createPromptModule . mockReturnValue ( mockPrompt )
1718
1819const { importConsoleConfig, downloadConsoleConfigToBuffer } = require ( '../../../src/lib/import' )
20+ const { SERVICE_API_KEY_ENV , IMS_OAUTH_S2S_ENV } = require ( '../../../src/lib/defaults' )
21+
22+ jest . mock ( 'fs-extra' )
1923
2024beforeEach ( ( ) => {
2125 jest . clearAllMocks ( )
@@ -29,4 +33,46 @@ test('exports', () => {
2933 expect ( downloadConsoleConfigToBuffer ) . toBeInstanceOf ( Function )
3034} )
3135
36+ describe ( 'importConsoleConfig' , ( ) => {
37+ test ( 'with oauth_server_to_server credentials, adds IMS_OAUTH_S2S to env vars' , async ( ) => {
38+ const configContent = fixtureFile ( 'oauths2s/valid.config.json' )
39+ // The file is read twice: once by importConsoleConfig (loadFunc) and once by importConfigJson
40+ fs . readFileSync . mockReturnValue ( configContent )
41+
42+ const config = await importConsoleConfig ( '/some/config/path' , { overwrite : true } )
43+
44+ expect ( config ) . toBeDefined ( )
45+ expect ( config . project . name ) . toEqual ( 'TestProject123' )
46+
47+ // Check that writeFile was called with the IMS_OAUTH_S2S_ENV variable
48+ const envWriteCall = fs . writeFile . mock . calls . find ( call => call [ 0 ] . endsWith ( '.env' ) )
49+ expect ( envWriteCall ) . toBeDefined ( )
50+ expect ( envWriteCall [ 1 ] ) . toContain ( SERVICE_API_KEY_ENV )
51+ expect ( envWriteCall [ 1 ] ) . toContain ( IMS_OAUTH_S2S_ENV )
52+
53+ // Verify the IMS_OAUTH_S2S value contains expected credential data
54+ const envContent = envWriteCall [ 1 ]
55+ expect ( envContent ) . toContain ( '"client_id":"CXCXCXCXCXCXCXCXC"' )
56+ expect ( envContent ) . toContain ( '"client_secret":"SFSFSFSFSFSFSFSFSFSFSFSFSFS"' )
57+ expect ( envContent ) . toContain ( '"org_id":"XOXOXOXOXOXOX@AdobeOrg"' )
58+ } )
59+
60+ test ( 'with jwt credentials only, does not add IMS_OAUTH_S2S to env vars' , async ( ) => {
61+ const configContent = fixtureFile ( 'valid.config.json' )
62+ // The file is read twice: once by importConsoleConfig (loadFunc) and once by importConfigJson
63+ fs . readFileSync . mockReturnValue ( configContent )
64+
65+ const config = await importConsoleConfig ( '/some/config/path' , { overwrite : true } )
66+
67+ expect ( config ) . toBeDefined ( )
68+ expect ( config . project . name ) . toEqual ( 'TestProject123' )
69+
70+ // Check that writeFile was called without the IMS_OAUTH_S2S_ENV variable
71+ const envWriteCall = fs . writeFile . mock . calls . find ( call => call [ 0 ] . endsWith ( '.env' ) )
72+ expect ( envWriteCall ) . toBeDefined ( )
73+ expect ( envWriteCall [ 1 ] ) . toContain ( SERVICE_API_KEY_ENV )
74+ expect ( envWriteCall [ 1 ] ) . not . toContain ( IMS_OAUTH_S2S_ENV )
75+ } )
76+ } )
77+
3278// The functions in this module are largely tested by use.test.js
0 commit comments