@@ -19,6 +19,8 @@ import { SmartlingException } from "../api/exception";
1919import { TranslationFileNameMode } from "../api/files/params/translation-file-name-mode" ;
2020import { FileLocaleMode } from "../api/files/params/file-locale-mode" ;
2121import { FileFilter } from "../api/files/params/file-filter" ;
22+ import { ImportFileParameters } from "../api/files/params/import-file-parameters" ;
23+ import { TranslationState } from "../api/files/params/translation-state" ;
2224
2325describe ( "SmartlingFilesApi class tests." , ( ) => {
2426 const projectId = "testProjectId" ;
@@ -753,5 +755,112 @@ describe("SmartlingFilesApi class tests.", () => {
753755 ) ;
754756 } ) ;
755757 } ) ;
758+
759+ describe ( "Import file" , ( ) => {
760+ const localeId = "fr-FR" ;
761+
762+ it ( "Import file from disk with all parameters" , async ( ) => {
763+ const params = new ImportFileParameters ( ) ;
764+
765+ params
766+ . setFileFromLocalFilePath ( "./test/data/file.xml" )
767+ . setFileUri ( "test-file-uri" )
768+ . setFileType ( FileType . XML )
769+ . setTranslationState ( TranslationState . PUBLISHED )
770+ . setOverwrite ( true )
771+ . setCallbackUrl ( "https://callback.url" ) ;
772+
773+ await filesApi . importFile ( projectId , localeId , params ) ;
774+
775+ sinon . assert . calledOnce ( filesApiFetchStub ) ;
776+
777+ assert . equal (
778+ filesApiFetchStub . getCall ( 0 ) . args [ 0 ] ,
779+ `https://test.com/files-api/v2/projects/${ projectId } /locales/${ localeId } /file/import`
780+ ) ;
781+
782+ assert . equal (
783+ filesApiFetchStub . getCall ( 0 ) . args [ 1 ] . method ,
784+ "post"
785+ ) ;
786+
787+ assert . equal (
788+ filesApiFetchStub . getCall ( 0 ) . args [ 1 ] . headers . Authorization ,
789+ "test_token_type test_access_token"
790+ ) ;
791+
792+ assert . equal (
793+ filesApiFetchStub . getCall ( 0 ) . args [ 1 ] . headers [ "User-Agent" ] ,
794+ "test_user_agent"
795+ ) ;
796+
797+ const stringified = JSON . stringify ( filesApiFetchStub . getCall ( 0 ) . args [ 1 ] . body ) ;
798+
799+ assert . ok ( stringified . includes ( "Content-Disposition: form-data; name=\\\"fileUri\\\"\\r\\n\\r\\n\",\"test-file-uri\"" ) ) ;
800+ assert . ok ( stringified . includes ( "Content-Disposition: form-data; name=\\\"translationState\\\"\\r\\n\\r\\n\",\"PUBLISHED\"" ) ) ;
801+ assert . ok ( stringified . includes ( "Content-Disposition: form-data; name=\\\"overwrite\\\"\\r\\n\\r\\n\",\"true\"" ) ) ;
802+ assert . ok ( stringified . includes ( "Content-Disposition: form-data; name=\\\"fileType\\\"\\r\\n\\r\\n\",\"xml\"" ) ) ;
803+ assert . ok ( stringified . includes ( "Content-Disposition: form-data; name=\\\"callbackUrl\\\"\\r\\n\\r\\n\",\"https://callback.url\"" ) ) ;
804+
805+ assert . equal (
806+ // eslint-disable-next-line no-underscore-dangle
807+ await streamToString ( filesApiFetchStub . getCall ( 0 ) . args [ 1 ] . body . _streams [ 1 ] ) ,
808+ fs . readFileSync (
809+ fs . realpathSync ( "./test/data/file.xml" ) ,
810+ "utf8"
811+ )
812+ ) ;
813+ } ) ;
814+
815+ it ( "Import file from string content" , async ( ) => {
816+ const params = new ImportFileParameters ( ) ;
817+
818+ params
819+ . setFileContent ( fs . readFileSync (
820+ fs . realpathSync ( "./test/data/file.xml" ) ,
821+ "utf8"
822+ ) )
823+ . setFileUri ( "test-file-uri" )
824+ . setFileType ( FileType . XML ) ;
825+
826+ await filesApi . importFile ( projectId , localeId , params ) ;
827+
828+ sinon . assert . calledOnce ( filesApiFetchStub ) ;
829+
830+ assert . equal (
831+ filesApiFetchStub . getCall ( 0 ) . args [ 0 ] ,
832+ `https://test.com/files-api/v2/projects/${ projectId } /locales/${ localeId } /file/import`
833+ ) ;
834+
835+ assert . equal (
836+ filesApiFetchStub . getCall ( 0 ) . args [ 1 ] . method ,
837+ "post"
838+ ) ;
839+
840+ assert . equal (
841+ filesApiFetchStub . getCall ( 0 ) . args [ 1 ] . headers . Authorization ,
842+ "test_token_type test_access_token"
843+ ) ;
844+
845+ assert . equal (
846+ filesApiFetchStub . getCall ( 0 ) . args [ 1 ] . headers [ "User-Agent" ] ,
847+ "test_user_agent"
848+ ) ;
849+
850+ const stringified = JSON . stringify ( filesApiFetchStub . getCall ( 0 ) . args [ 1 ] . body ) ;
851+
852+ assert . ok ( stringified . includes ( "Content-Disposition: form-data; name=\\\"fileUri\\\"\\r\\n\\r\\n\",\"test-file-uri\"" ) ) ;
853+ assert . ok ( stringified . includes ( "Content-Disposition: form-data; name=\\\"fileType\\\"\\r\\n\\r\\n\",\"xml\"" ) ) ;
854+
855+ assert . equal (
856+ // eslint-disable-next-line no-underscore-dangle
857+ await streamToString ( filesApiFetchStub . getCall ( 0 ) . args [ 1 ] . body . _streams [ 1 ] ) ,
858+ fs . readFileSync (
859+ fs . realpathSync ( "./test/data/file.xml" ) ,
860+ "utf8"
861+ )
862+ ) ;
863+ } ) ;
864+ } ) ;
756865 } ) ;
757866} ) ;
0 commit comments