@@ -33,6 +33,7 @@ describe('commandRenameSecret', () => {
3333 logger : logger ,
3434 } ) ;
3535 } ) ;
36+
3637 afterEach ( async ( ) => {
3738 await polykeyAgent . stop ( ) ;
3839 await fs . promises . rm ( dataDir , {
@@ -41,42 +42,106 @@ describe('commandRenameSecret', () => {
4142 } ) ;
4243 } ) ;
4344
44- test ( 'should rename secrets' , async ( ) => {
45- const vaultName = 'vault ' as VaultName ;
45+ test ( 'should rename secrets using a simple new name ' , async ( ) => {
46+ const vaultName = 'vaultSimpleRename ' as VaultName ;
4647 const vaultId = await polykeyAgent . vaultManager . createVault ( vaultName ) ;
47- const secretName = 'secret ' ;
48- const newSecretName = 'secret-renamed' ;
49- const secretContent = 'this is the secret' ;
48+ const oldSecretName = 'secretOriginal ' ;
49+ const newSecretBaseName = 'secretNewBase' ; // Changed variable name for clarity
50+ const secretContent = 'this is the secret for simple rename ' ;
5051 await polykeyAgent . vaultManager . withVaults ( [ vaultId ] , async ( vault ) => {
51- await vaultOps . addSecret ( vault , secretName , secretContent ) ;
52+ await vaultOps . addSecret ( vault , oldSecretName , secretContent ) ;
5253 } ) ;
5354 command = [
5455 'secrets' ,
5556 'rename' ,
5657 '-np' ,
5758 dataDir ,
58- `${ vaultName } :${ secretName } ` ,
59- newSecretName ,
59+ `${ vaultName } :${ oldSecretName } ` ,
60+ newSecretBaseName , // Use the simple base name
6061 ] ;
6162 const result = await testUtils . pkStdio ( command , {
6263 env : { PK_PASSWORD : password } ,
6364 cwd : dataDir ,
6465 } ) ;
6566 expect ( result . exitCode ) . toBe ( 0 ) ;
67+ expect ( result . stderr ) . toBe ( '' ) ; // Expect no errors
6668 await polykeyAgent . vaultManager . withVaults ( [ vaultId ] , async ( vault ) => {
6769 const list = await vaultOps . listSecrets ( vault ) ;
68- expect ( list . sort ( ) ) . toStrictEqual ( [ newSecretName ] ) ;
70+ expect ( list . sort ( ) ) . toStrictEqual ( [ newSecretBaseName ] ) ;
71+ expect ( list ) . not . toContain ( oldSecretName ) ;
72+ } ) ;
73+ } ) ;
74+
75+ // New test case for the fix
76+ test ( 'should rename secret when new name is a fully qualified path' , async ( ) => {
77+ const vaultName = 'vaultFQRename' as VaultName ; // Use a distinct vault name for the test
78+ const vaultId = await polykeyAgent . vaultManager . createVault ( vaultName ) ;
79+ const oldSecretName = 'oldSecretName' ;
80+ const newSecretBaseName = 'newSecretNameByPath' ; // The actual target base name
81+ const secretContent = 'content for fully qualified path rename test' ;
82+
83+ // Add initial secret
84+ await polykeyAgent . vaultManager . withVaults ( [ vaultId ] , async ( vault ) => {
85+ await vaultOps . addSecret ( vault , oldSecretName , secretContent ) ;
86+ } ) ;
87+
88+ // Construct the fully qualified path for the new secret name
89+ // This format matches the problematic case: VaultName:/NewName
90+ const newSecretFullyQualified = `${ vaultName } :/${ newSecretBaseName } ` ;
91+
92+ command = [
93+ 'secrets' ,
94+ 'rename' ,
95+ '-np' ,
96+ dataDir ,
97+ `${ vaultName } :${ oldSecretName } ` , // Source secret path
98+ newSecretFullyQualified , // New secret name as fully qualified path
99+ ] ;
100+
101+ const result = await testUtils . pkStdio ( command , {
102+ env : { PK_PASSWORD : password } ,
103+ cwd : dataDir ,
104+ } ) ;
105+
106+ expect ( result . exitCode ) . toBe ( 0 ) ;
107+ expect ( result . stderr ) . toBe ( '' ) ; // Expect no errors
108+
109+ // Verify the rename
110+ await polykeyAgent . vaultManager . withVaults ( [ vaultId ] , async ( vault ) => {
111+ const secretsList = await vaultOps . listSecrets ( vault ) ;
112+ expect ( secretsList ) . toContain ( newSecretBaseName ) ; // Check for the base name
113+ expect ( secretsList ) . not . toContain ( oldSecretName ) ;
114+ expect ( secretsList . length ) . toBe ( 1 ) ; // Assuming only this secret is in the test vault
69115 } ) ;
70116 } ) ;
117+
71118 test ( 'should not rename vault root' , async ( ) => {
72- const vaultName = 'vault ' as VaultName ;
119+ const vaultName = 'vaultRootTest ' as VaultName ; // Use a distinct vault name
73120 await polykeyAgent . vaultManager . createVault ( vaultName ) ;
74- command = [ 'secrets' , 'rename' , '-np' , dataDir , vaultName , 'rename' ] ;
75- const result = await testUtils . pkStdio ( command , {
121+ // Attempting to rename the vault itself, or an empty path within it
122+ command = [
123+ 'secrets' ,
124+ 'rename' ,
125+ '-np' ,
126+ dataDir ,
127+ `${ vaultName } :/` ,
128+ 'newNameForRoot' ,
129+ ] ;
130+ let result = await testUtils . pkStdio ( command , {
76131 env : { PK_PASSWORD : password } ,
77132 cwd : dataDir ,
78133 } ) ;
79134 expect ( result . exitCode ) . not . toBe ( 0 ) ;
80135 expect ( result . stderr ) . toInclude ( 'EPERM' ) ;
136+
137+ // Original test for trying to rename the vault name directly (which parseSecretPath handles as [vaultName, undefined, undefined])
138+ // The `CommandRename` checks secretPath[1] == null, which covers `vaultName` (no colon) for the first arg.
139+ command = [ 'secrets' , 'rename' , '-np' , dataDir , vaultName , 'rename' ] ;
140+ result = await testUtils . pkStdio ( command , {
141+ env : { PK_PASSWORD : password } ,
142+ cwd : dataDir ,
143+ } ) ;
144+ expect ( result . exitCode ) . not . toBe ( 0 ) ;
145+ expect ( result . stderr ) . toInclude ( 'EPERM' ) ; // This is because secretPath[1] will be undefined
81146 } ) ;
82147} ) ;
0 commit comments