@@ -490,6 +490,136 @@ describe('test git-proxy-cli', function () {
490490 } ) ;
491491 } ) ;
492492
493+ // *** create user ***
494+
495+ describe ( 'test git-proxy-cli :: create-user' , function ( ) {
496+ before ( async function ( ) {
497+ await helper . addUserToDb ( TEST_USER , TEST_PASSWORD , TEST_EMAIL , TEST_GIT_ACCOUNT ) ;
498+ } ) ;
499+
500+ after ( async function ( ) {
501+ await helper . removeUserFromDb ( TEST_USER ) ;
502+ } ) ;
503+
504+ it ( 'attempt to create user should fail when server is down' , async function ( ) {
505+ try {
506+ // start server -> login -> stop server
507+ await helper . startServer ( service ) ;
508+ await helper . runCli ( `npx -- @finos/git-proxy-cli login --username admin --password admin` ) ;
509+ } finally {
510+ await helper . closeServer ( service . httpServer ) ;
511+ }
512+
513+ const cli = `npx -- @finos/git-proxy-cli create-user --username newuser --password newpass --email new@email.com --gitAccount newgit` ;
514+ const expectedExitCode = 2 ;
515+ const expectedMessages = null ;
516+ const expectedErrorMessages = [ 'Error: Create User:' ] ;
517+ await helper . runCli ( cli , expectedExitCode , expectedMessages , expectedErrorMessages ) ;
518+ } ) ;
519+
520+ it ( 'attempt to create user should fail when not authenticated' , async function ( ) {
521+ await helper . removeCookiesFile ( ) ;
522+
523+ const cli = `npx -- @finos/git-proxy-cli create-user --username newuser --password newpass --email new@email.com --gitAccount newgit` ;
524+ const expectedExitCode = 1 ;
525+ const expectedMessages = null ;
526+ const expectedErrorMessages = [ 'Error: Create User: Authentication required' ] ;
527+ await helper . runCli ( cli , expectedExitCode , expectedMessages , expectedErrorMessages ) ;
528+ } ) ;
529+
530+ it ( 'attempt to create user should fail when not admin' , async function ( ) {
531+ try {
532+ await helper . startServer ( service ) ;
533+ await helper . runCli (
534+ `npx -- @finos/git-proxy-cli login --username testuser --password testpassword` ,
535+ ) ;
536+
537+ const cli = `npx -- @finos/git-proxy-cli create-user --username newuser --password newpass --email new@email.com --gitAccount newgit` ;
538+ const expectedExitCode = 3 ;
539+ const expectedMessages = null ;
540+ const expectedErrorMessages = [ 'Error: Create User: Authentication required' ] ;
541+ await helper . runCli ( cli , expectedExitCode , expectedMessages , expectedErrorMessages ) ;
542+ } finally {
543+ await helper . closeServer ( service . httpServer ) ;
544+ }
545+ } ) ;
546+
547+ it ( 'attempt to create user should fail with missing required fields' , async function ( ) {
548+ try {
549+ await helper . startServer ( service ) ;
550+ await helper . runCli ( `npx -- @finos/git-proxy-cli login --username admin --password admin` ) ;
551+
552+ const cli = `npx -- @finos/git-proxy-cli create-user --username newuser --password "" --email new@email.com --gitAccount newgit` ;
553+ const expectedExitCode = 4 ;
554+ const expectedMessages = null ;
555+ const expectedErrorMessages = [ 'Error: Create User: Missing required fields' ] ;
556+ await helper . runCli ( cli , expectedExitCode , expectedMessages , expectedErrorMessages ) ;
557+ } finally {
558+ await helper . closeServer ( service . httpServer ) ;
559+ }
560+ } ) ;
561+
562+ it ( 'should successfully create a new user' , async function ( ) {
563+ const uniqueUsername = `newuser_${ Date . now ( ) } ` ;
564+ try {
565+ await helper . startServer ( service ) ;
566+ await helper . runCli ( `npx -- @finos/git-proxy-cli login --username admin --password admin` ) ;
567+
568+ const cli = `npx -- @finos/git-proxy-cli create-user --username ${ uniqueUsername } --password newpass --email new@email.com --gitAccount newgit` ;
569+ const expectedExitCode = 0 ;
570+ const expectedMessages = [ `User '${ uniqueUsername } ' created successfully` ] ;
571+ const expectedErrorMessages = null ;
572+ await helper . runCli ( cli , expectedExitCode , expectedMessages , expectedErrorMessages ) ;
573+
574+ // Verify we can login with the new user
575+ await helper . runCli (
576+ `npx -- @finos/git-proxy-cli login --username ${ uniqueUsername } --password newpass` ,
577+ 0 ,
578+ [ `Login "${ uniqueUsername } " <new@email.com>: OK` ] ,
579+ null ,
580+ ) ;
581+ } finally {
582+ await helper . closeServer ( service . httpServer ) ;
583+ // Clean up the created user
584+ try {
585+ await helper . removeUserFromDb ( uniqueUsername ) ;
586+ } catch ( error ) {
587+ // Ignore cleanup errors
588+ }
589+ }
590+ } ) ;
591+
592+ it ( 'should successfully create a new admin user' , async function ( ) {
593+ const uniqueUsername = `newadmin_${ Date . now ( ) } ` ;
594+ try {
595+ await helper . startServer ( service ) ;
596+ await helper . runCli ( `npx -- @finos/git-proxy-cli login --username admin --password admin` ) ;
597+
598+ const cli = `npx -- @finos/git-proxy-cli create-user --username ${ uniqueUsername } --password newpass --email ${ uniqueUsername } @email.com --gitAccount newgit --admin` ;
599+ const expectedExitCode = 0 ;
600+ const expectedMessages = [ `User '${ uniqueUsername } ' created successfully` ] ;
601+ const expectedErrorMessages = null ;
602+ await helper . runCli ( cli , expectedExitCode , expectedMessages , expectedErrorMessages ) ;
603+
604+ // Verify we can login with the new admin user
605+ await helper . runCli (
606+ `npx -- @finos/git-proxy-cli login --username ${ uniqueUsername } --password newpass` ,
607+ 0 ,
608+ [ `Login "${ uniqueUsername } " <${ uniqueUsername } @email.com> (admin): OK` ] ,
609+ null ,
610+ ) ;
611+ } finally {
612+ await helper . closeServer ( service . httpServer ) ;
613+ // Clean up the created user
614+ try {
615+ await helper . removeUserFromDb ( uniqueUsername ) ;
616+ } catch ( error ) {
617+ console . error ( 'Error cleaning up user' , error ) ;
618+ }
619+ }
620+ } ) ;
621+ } ) ;
622+
493623 // *** tests require push in db ***
494624
495625 describe ( 'test git-proxy-cli :: git push administration' , function ( ) {
0 commit comments