|
| 1 | +component extends="coldbox.system.testing.BaseTestCase" { |
| 2 | + |
| 3 | + function beforeAll() { |
| 4 | + super.beforeAll(); |
| 5 | + |
| 6 | + // Clean out tmp directory before all tests |
| 7 | + local.tempFolder = expandPath( "../../../models/tmp" ); |
| 8 | + if ( directoryExists( local.tempFolder ) ) { |
| 9 | + directoryDelete( local.tempFolder, true ); |
| 10 | + } |
| 11 | + directoryCreate( local.tempFolder ); |
| 12 | + |
| 13 | + // Ensure /resources exists |
| 14 | + local.resourcePath = expandPath( "../resources" ); |
| 15 | + if ( !directoryExists( local.resourcePath ) ) { |
| 16 | + directoryCreate( local.resourcePath ); |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + function run( testResults, testBox ) { |
| 21 | + describe( "FileUpload.cfc", function() { |
| 22 | + |
| 23 | + beforeEach( function( currentSpec ) { |
| 24 | + setup(); |
| 25 | + |
| 26 | + testComponent = getInstance( "wires.TestComponent" ); |
| 27 | + testComponent |
| 28 | + ._withEvent( getRequestContext() ) |
| 29 | + ._withPath( "wires.TestComponent" ); |
| 30 | + |
| 31 | + prepareMock( testComponent ); |
| 32 | + |
| 33 | + fileUpload = getInstance( "FileUpload@cbwire" ); |
| 34 | + prepareMock( fileUpload ); |
| 35 | + }); |
| 36 | + |
| 37 | + it( "should return an object", function() { |
| 38 | + var result = getInstance( "FileUpload@cbwire" ); |
| 39 | + expect( isObject( result ) ).toBeTrue(); |
| 40 | + }); |
| 41 | + |
| 42 | + it( "should load without error", function() { |
| 43 | + var result = loadMockedFileUpload( "test", "text", "plain" ); |
| 44 | + expect( isObject( result ) ).toBeTrue(); |
| 45 | + }); |
| 46 | + |
| 47 | + it( "should return the meta data", function() { |
| 48 | + var result = loadMockedFileUpload( "test", "text", "plain" ); |
| 49 | + expect( result.getMeta().uuid ).toBe( "test" ); |
| 50 | + }); |
| 51 | + |
| 52 | + it( "should return fileupload: uuid on serializeIt", function() { |
| 53 | + var result = loadMockedFileUpload( "test", "text", "plain" ); |
| 54 | + expect( result.serializeIt() ).toBe( "fileupload:test" ); |
| 55 | + }); |
| 56 | + |
| 57 | + it( "should return expected preview URL", function() { |
| 58 | + var result = loadMockedFileUpload( "test", "text", "plain" ); |
| 59 | + expect( result.getPreviewURL() ).toBe( "/cbwire/preview-file/test" ); |
| 60 | + }); |
| 61 | + |
| 62 | + it( "should determine is image", function() { |
| 63 | + var result = loadMockedFileUpload( "test", "image", "png" ); |
| 64 | + expect( result.isImage() ).toBeTrue(); |
| 65 | + }); |
| 66 | + |
| 67 | + it( "should determine is NOT image", function() { |
| 68 | + var result = loadMockedFileUpload( "test", "text", "plain" ); |
| 69 | + expect( result.isImage() ).toBeFalse(); |
| 70 | + }); |
| 71 | + |
| 72 | + it( "should return the file size", function() { |
| 73 | + var result = loadMockedFileUpload( "test", "text", "plain" ); |
| 74 | + expect( result.getSize() ).toBe( 1234 ); |
| 75 | + }); |
| 76 | + |
| 77 | + it( "should return the MIME type", function() { |
| 78 | + var result = loadMockedFileUpload( "test", "text", "plain" ); |
| 79 | + expect( result.getMIMEType() ).toBe( "text/plain" ); |
| 80 | + }); |
| 81 | + |
| 82 | + it( "should return the temporary storage path", function() { |
| 83 | + var result = loadMockedFileUpload( "test", "text", "plain" ); |
| 84 | + expect( result.getTemporaryStoragePath() ).toBe( expandPath( "../resources/logo.png" ) ); |
| 85 | + }); |
| 86 | + |
| 87 | + it( "should return binary file contents when calling get", function() { |
| 88 | + var result = loadMockedFileUpload( "test", "text", "plain" ); |
| 89 | + expect( isBinary( result.get() ) ).toBeTrue(); |
| 90 | + }); |
| 91 | + |
| 92 | + it ( "should return base 64 encoded string when calling getBase64", function() { |
| 93 | + var result = loadMockedFileUpload( "test", "text", "plain" ); |
| 94 | + expect( isBinary( toBinary( result.getBase64() ) ) ).toBeTrue(); |
| 95 | + }); |
| 96 | + |
| 97 | + it( "should return base 64 src attribute contents when calling getBase64Src", function() { |
| 98 | + var result = loadMockedFileUpload( "test", "text", "plain" ); |
| 99 | + expect( result.getBase64Src() ).toInclude( "data:" ); |
| 100 | + }); |
| 101 | + |
| 102 | + }); |
| 103 | + } |
| 104 | + |
| 105 | + private function writeTestMetaFile( required path, required struct data ) { |
| 106 | + if ( fileExists( arguments.path ) ) { |
| 107 | + fileDelete( arguments.path ); |
| 108 | + } |
| 109 | + fileWrite( arguments.path, serializeJSON( arguments.data ) ); |
| 110 | + } |
| 111 | + |
| 112 | + private function loadMockedFileUpload( |
| 113 | + required string uuid, |
| 114 | + required string contentType, |
| 115 | + required string contentSubType |
| 116 | + ) { |
| 117 | + var metaPath = expandPath( "../resources/fileupload_metadata.json" ); |
| 118 | + |
| 119 | + writeTestMetaFile( |
| 120 | + path = metaPath, |
| 121 | + data = { |
| 122 | + "uuid" : arguments.uuid, |
| 123 | + "serverDirectory" : expandPath( "../resources" ), |
| 124 | + "serverFile" : "logo.png", |
| 125 | + "contentType" : arguments.contentType, |
| 126 | + "contentSubType" : arguments.contentSubType, |
| 127 | + "fileSize" : 1234 |
| 128 | + } |
| 129 | + ); |
| 130 | + |
| 131 | + fileUpload.$( "getMetaPath", metaPath ); |
| 132 | + return fileUpload.load( testComponent, "test", arguments.uuid ); |
| 133 | + } |
| 134 | +} |
0 commit comments