From e33c42e232ed82af2a22575664fd537ee90b22c7 Mon Sep 17 00:00:00 2001 From: Lukas Hodel Date: Mon, 8 Jul 2013 18:02:24 +0200 Subject: [PATCH 01/12] Adds config var for uploader_action_path --- lib/active_admin/editor/config.rb | 7 +++++++ spec/lib/config_spec.rb | 21 +++++++++++++++++++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/lib/active_admin/editor/config.rb b/lib/active_admin/editor/config.rb index 1478296..10780b0 100644 --- a/lib/active_admin/editor/config.rb +++ b/lib/active_admin/editor/config.rb @@ -29,6 +29,9 @@ class Configuration # wysiwyg stylesheets that get included in the backend and the frontend. attr_accessor :stylesheets + # action which should handle file upload + attr_accessor :uploader_action_path + def storage_dir @storage_dir ||= 'uploads' end @@ -50,6 +53,10 @@ def s3_configured? def parser_rules @parser_rules ||= PARSER_RULES.dup end + + def uploader_action_path=(action) + @uploader_action_path = (action.nil?) ? action : "/#{ action.to_s.gsub(/(^\/|\/$)/, '') }" + end end end end diff --git a/spec/lib/config_spec.rb b/spec/lib/config_spec.rb index 7d28f1e..3214450 100644 --- a/spec/lib/config_spec.rb +++ b/spec/lib/config_spec.rb @@ -8,6 +8,7 @@ configuration.aws_access_secret = nil configuration.s3_bucket = nil configuration.storage_dir = 'uploads' + configuration.uploader_action_path = nil end context 'by default' do @@ -15,8 +16,9 @@ its(:aws_access_secret) { should be_nil } its(:s3_bucket) { should be_nil } its(:storage_dir) { should eq 'uploads' } + its(:uploader_action_path){ should be_nil } end - + describe '.s3_configured?' do subject { configuration.s3_configured? } @@ -26,7 +28,7 @@ it { should be_false } end - + context 'when key, secret and bucket are set' do before do configuration.aws_access_key_id = 'foo' @@ -51,4 +53,19 @@ expect(subject).to eq 'uploads' end end + + describe ".uploader_action_path" do + subject { configuration.uploader_action_path } + + it 'strips trailing slashes' do + configuration.uploader_action_path = '/action/' + expect(subject).to eq '/action' + end + + it 'add leading slash' do + configuration.uploader_action_path= 'action' + expect(subject).to eq '/action' + end + end + end From 1da10fc08239bff06fb9e942d4b122c0cbe962d4 Mon Sep 17 00:00:00 2001 From: Lukas Hodel Date: Mon, 8 Jul 2013 18:07:34 +0200 Subject: [PATCH 02/12] Adds editor logic for uploading to custom action --- .../active_admin/editor/config.js.erb | 3 +- .../javascripts/active_admin/editor/editor.js | 60 ++++++++++++++++++- 2 files changed, 60 insertions(+), 3 deletions(-) diff --git a/app/assets/javascripts/active_admin/editor/config.js.erb b/app/assets/javascripts/active_admin/editor/config.js.erb index 49a0014..e5d5ef8 100644 --- a/app/assets/javascripts/active_admin/editor/config.js.erb +++ b/app/assets/javascripts/active_admin/editor/config.js.erb @@ -8,6 +8,7 @@ config.stylesheets = <%= ActiveAdmin::Editor.configuration.stylesheets.map { |stylesheet| asset_path stylesheet }.to_json %> config.spinner = '<%= asset_path 'active_admin/editor/loader.gif' %>' - config.uploads_enabled = <%= ActiveAdmin::Editor.configuration.s3_configured? %> + config.uploads_enabled = <%= ActiveAdmin::Editor.configuration.uploads_enabled? %> config.parserRules = <%= ActiveAdmin::Editor.configuration.parser_rules.to_json %> + config.uploader_action_path = '<%= ActiveAdmin::Editor.configuration.uploader_action_path.to_s })(window) diff --git a/app/assets/javascripts/active_admin/editor/editor.js b/app/assets/javascripts/active_admin/editor/editor.js index 11365b6..5fb628b 100644 --- a/app/assets/javascripts/active_admin/editor/editor.js +++ b/app/assets/javascripts/active_admin/editor/editor.js @@ -47,7 +47,7 @@ * Adds a file input attached to the supplied text input. And upload is * triggered if the source of the input is changed. * - * @input Text input to attach a file input to. + * @input Text input to attach a file input to. */ Editor.prototype._addUploader = function(input) { var $input = $(input) @@ -93,6 +93,62 @@ return this.__uploading } + /** + * Uploads a file to S3 or an custom action. + * + * @file The file to upload + * @callback A function to be called when the upload completes. + */ + Editor.prototype.upload = function(file, callback) { + if (config.uploader_action_path == null) { + this.s3_upload(file, callback); + } else { + this.action_upload(file, callback); + } + } + + /** + * Uploads a file to a confured action under config.uploader_action_path. + * When the upload is complete, calls callback with the location of the uploaded file. + * + * @file The file to upload + * @callback A function to be called when the upload completes. + */ + Editor.prototype.action_upload = function(file, callback) { + var _this = this + _this._uploading(true) + + var xhr = new XMLHttpRequest() + , fd = new FormData() + + fd.append('_method', 'POST') + fd.append('authenticity_token', $('meta[name="csrf-token"]').attr('content')) + fd.append('file', file) + + xhr.upload.addEventListener('progress', function(e) { + _this.loaded = e.loaded + _this.total = e.total + _this.progress = e.loaded / e.total + }, false) + + xhr.onreadystatechange = function() { + if (xhr.readyState != 4) { return } + _this._uploading(false) + if (xhr.status == 200) { + callback(xhr.getResponseHeader('Location')) + } else { + alert('Failed to upload file. Have you implemented action "' + config.uploader_action_path + '" correctly?') + } + } + + xhr.open('POST', 'http://localhost:3000' + config.uploader_action_path, true) + xhr.send(fd) + + return xhr + } + + /** + /** * Uploads a file to S3. When the upload is complete, calls callback with the * location of the uploaded file. @@ -100,7 +156,7 @@ * @file The file to upload * @callback A function to be called when the upload completes. */ - Editor.prototype.upload = function(file, callback) { + Editor.prototype.s3_upload = function(file, callback) { var _this = this _this._uploading(true) From 5f1859321cfc6de635a396fe8ac0ee670b30784b Mon Sep 17 00:00:00 2001 From: Lukas Hodel Date: Mon, 8 Jul 2013 18:19:15 +0200 Subject: [PATCH 03/12] Fixes typo in config.js.erb --- app/assets/javascripts/active_admin/editor/config.js.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/assets/javascripts/active_admin/editor/config.js.erb b/app/assets/javascripts/active_admin/editor/config.js.erb index e5d5ef8..c61b3d7 100644 --- a/app/assets/javascripts/active_admin/editor/config.js.erb +++ b/app/assets/javascripts/active_admin/editor/config.js.erb @@ -10,5 +10,5 @@ config.spinner = '<%= asset_path 'active_admin/editor/loader.gif' %>' config.uploads_enabled = <%= ActiveAdmin::Editor.configuration.uploads_enabled? %> config.parserRules = <%= ActiveAdmin::Editor.configuration.parser_rules.to_json %> - config.uploader_action_path = '<%= ActiveAdmin::Editor.configuration.uploader_action_path.to_s + config.uploader_action_path = '<%= ActiveAdmin::Editor.configuration.uploader_action_path.to_s %>' })(window) From 244c303e8b51336b2557cea662e019669a3d5378 Mon Sep 17 00:00:00 2001 From: Lukas Hodel Date: Mon, 8 Jul 2013 18:25:55 +0200 Subject: [PATCH 04/12] Adds enable detectin for custom upload path --- lib/active_admin/editor/config.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/active_admin/editor/config.rb b/lib/active_admin/editor/config.rb index 10780b0..60950de 100644 --- a/lib/active_admin/editor/config.rb +++ b/lib/active_admin/editor/config.rb @@ -50,6 +50,10 @@ def s3_configured? s3_bucket.present? end + def uploads_enabled? + s3_configured? or @uploader_action_path.present? + end + def parser_rules @parser_rules ||= PARSER_RULES.dup end From a1baa6e56d174718bc9a76c6df0dcd05fbf28f2e Mon Sep 17 00:00:00 2001 From: Lukas Hodel Date: Mon, 8 Jul 2013 18:53:22 +0200 Subject: [PATCH 05/12] Adds readme for custom upload action --- README.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/README.md b/README.md index a7e2094..fbd30ed 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,29 @@ Then add the following CORS configuration to the S3 bucket: ``` +## Uploads with custom uploader action + +You also can upload images via a own provided action. For example so you can preprocess the images. For this you have to set the uploader_action_path in the initializer. When you have set the S3 config and the uploader_action_path, the uploader_action_path will win. + +```ruby +ActiveAdmin::Editor.configure do |config| + config.uploader_action_path = '/path/to/the/uploader' +end +``` + +__pseudocode of an uploader action within active admin__ + +```ruby +collection_action :upload_image, :method => :post do + img = Uploader.new(image: params[:file]) + img.uplaode + + # IMPORTANT the image url must be set as the headers location porperty + render json: {location: img.remote_url} , location: img.remote_url +end +``` + + ## Configuration You can configure the editor in the initializer installed with `rails g From bfa5ffb68d913fa334852a9120c8467dccaf1c99 Mon Sep 17 00:00:00 2001 From: Lukas Hodel Date: Mon, 8 Jul 2013 18:56:06 +0200 Subject: [PATCH 06/12] Fixes typo --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index fbd30ed..eb69e5f 100644 --- a/README.md +++ b/README.md @@ -82,7 +82,7 @@ Then add the following CORS configuration to the S3 bucket: ## Uploads with custom uploader action -You also can upload images via a own provided action. For example so you can preprocess the images. For this you have to set the uploader_action_path in the initializer. When you have set the S3 config and the uploader_action_path, the uploader_action_path will win. +You also can upload images via a own provided action. For example when you want to preprocess the images. For this you have to set the uploader_action_path in the initializer. When you have set the S3 config and the uploader_action_path, the uploader_action_path will win. ```ruby ActiveAdmin::Editor.configure do |config| @@ -94,8 +94,8 @@ __pseudocode of an uploader action within active admin__ ```ruby collection_action :upload_image, :method => :post do - img = Uploader.new(image: params[:file]) - img.uplaode + img = ImageUploader.new(image: params[:file]) + img.upload # IMPORTANT the image url must be set as the headers location porperty render json: {location: img.remote_url} , location: img.remote_url From 388acdbe0e83b574cdbf9f6b0807eae125dfb11a Mon Sep 17 00:00:00 2001 From: Lukas Hodel Date: Tue, 9 Jul 2013 09:47:46 +0200 Subject: [PATCH 07/12] Fixes js specs --- .../javascripts/active_admin/editor/editor.js | 2 +- spec/javascripts/editor_spec.js | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/app/assets/javascripts/active_admin/editor/editor.js b/app/assets/javascripts/active_admin/editor/editor.js index 5fb628b..c8d50ca 100644 --- a/app/assets/javascripts/active_admin/editor/editor.js +++ b/app/assets/javascripts/active_admin/editor/editor.js @@ -172,7 +172,7 @@ fd.append('Content-Type', file.type) fd.append('file', file) - xhr.upload.addEventListener('progress', function(e) { + xhr.s3_upload.addEventListener('progress', function(e) { _this.loaded = e.loaded _this.total = e.total _this.progress = e.loaded / e.total diff --git a/spec/javascripts/editor_spec.js b/spec/javascripts/editor_spec.js index c459b83..c0ddae7 100644 --- a/spec/javascripts/editor_spec.js +++ b/spec/javascripts/editor_spec.js @@ -82,22 +82,22 @@ describe('Editor', function() { }) }) - describe('.upload', function() { + describe('.s3_upload', function() { beforeEach(function() { - this.xhr.prototype.upload = { addEventListener: sinon.stub() } + this.xhr.prototype.s3_upload = { addEventListener: sinon.stub() } }) it('opens the connection to the proper bucket', function() { this.xhr.prototype.open = sinon.stub() this.xhr.prototype.send = sinon.stub() this.config.s3_bucket = 'bucket' - xhr = this.editor.upload(sinon.stub(), function() {}) + xhr = this.editor.s3_upload(sinon.stub(), function() {}) expect(xhr.open).to.have.been.calledWith('POST', 'https://bucket.s3.amazonaws.com', true) }) it('sends the request', function() { this.xhr.prototype.send = sinon.stub() - xhr = this.editor.upload(sinon.stub(), function() {}) + xhr = this.editor.s3_upload(sinon.stub(), function() {}) expect(xhr.send).to.have.been.called }) @@ -106,7 +106,7 @@ describe('Editor', function() { this.xhr.prototype.open = sinon.stub() this.xhr.prototype.send = sinon.stub() this.config.s3_bucket = 'bucket' - xhr = this.editor.upload(sinon.stub(), function(location) { + xhr = this.editor.s3_upload(sinon.stub(), function(location) { expect(location).to.eq('foo') done() }) @@ -123,7 +123,7 @@ describe('Editor', function() { this.xhr.prototype.send = sinon.stub() this.config.s3_bucket = 'bucket' alert = sinon.stub() - xhr = this.editor.upload(sinon.stub(), function() {}) + xhr = this.editor.s3_upload(sinon.stub(), function() {}) xhr.readyState = 4 xhr.status = 403 xhr.onreadystatechange() @@ -146,7 +146,7 @@ describe('Editor', function() { this.config.storage_dir = 'uploads' this.config.aws_access_key_id = 'access key' - this.editor.upload(file, function() {}) + this.editor.s3_upload(file, function() {}) }) it('sets "key"', function() { From f27fdaf6e3180d75fa48c0dc9066c7df8957a5b9 Mon Sep 17 00:00:00 2001 From: Lukas Hodel Date: Tue, 9 Jul 2013 10:29:52 +0200 Subject: [PATCH 08/12] Adds js specs for action_upload --- .../javascripts/active_admin/editor/editor.js | 7 +- spec/javascripts/editor_spec.js | 79 +++++++++++++++++++ spec/javascripts/templates/editor.jst.ejs | 3 + 3 files changed, 86 insertions(+), 3 deletions(-) diff --git a/app/assets/javascripts/active_admin/editor/editor.js b/app/assets/javascripts/active_admin/editor/editor.js index c8d50ca..ea3fef9 100644 --- a/app/assets/javascripts/active_admin/editor/editor.js +++ b/app/assets/javascripts/active_admin/editor/editor.js @@ -122,10 +122,10 @@ , fd = new FormData() fd.append('_method', 'POST') - fd.append('authenticity_token', $('meta[name="csrf-token"]').attr('content')) + fd.append($('meta[name="csrf-param"]').attr('content'), $('meta[name="csrf-token"]').attr('content')) fd.append('file', file) - xhr.upload.addEventListener('progress', function(e) { + xhr.action_upload.addEventListener('progress', function(e) { _this.loaded = e.loaded _this.total = e.total _this.progress = e.loaded / e.total @@ -141,7 +141,8 @@ } } - xhr.open('POST', 'http://localhost:3000' + config.uploader_action_path, true) + action_url = window.location.protocol + '//' + window.location.host + config.uploader_action_path + xhr.open('POST', action_url, true) xhr.send(fd) return xhr diff --git a/spec/javascripts/editor_spec.js b/spec/javascripts/editor_spec.js index c0ddae7..567e2c2 100644 --- a/spec/javascripts/editor_spec.js +++ b/spec/javascripts/editor_spec.js @@ -178,4 +178,83 @@ describe('Editor', function() { }) }) }) + + describe('.action_upload', function() { + beforeEach(function() { + this.xhr.prototype.action_upload = { addEventListener: sinon.stub() } + }) + + it('opens the connection to the uploader action', function() { + this.xhr.prototype.open = sinon.stub() + this.xhr.prototype.send = sinon.stub() + this.config.uploader_action_path = '/path/to/action' + xhr = this.editor.action_upload(sinon.stub(), function() {}) + expect(xhr.open).to.have.been.calledWith('POST', 'http://localhost:3500/path/to/action', true) + }) + + it('sends the request', function() { + this.xhr.prototype.send = sinon.stub() + xhr = this.editor.action_upload(sinon.stub(), function() {}) + expect(xhr.send).to.have.been.called + }) + + describe('when the upload succeeds', function() { + it('calls the callback with the location', function(done) { + this.xhr.prototype.open = sinon.stub() + this.xhr.prototype.send = sinon.stub() + this.config.uploader_action_path = '/path/to/action' + xhr = this.editor.action_upload(sinon.stub(), function(location) { + expect(location).to.eq('foo') + done() + }) + xhr.getResponseHeader = sinon.stub().returns('foo') + xhr.readyState = 4 + xhr.status = 200 + xhr.onreadystatechange() + }) + }) + + describe('when the upload fails', function() { + it('shows an alert', function() { + this.xhr.prototype.open = sinon.stub() + this.xhr.prototype.send = sinon.stub() + this.config.uploader_action_path = '/path/to/action' + alert = sinon.stub() + xhr = this.editor.action_upload(sinon.stub(), function() {}) + xhr.readyState = 4 + xhr.status = 403 + xhr.onreadystatechange() + expect(alert).to.have.been.calledWith('Failed to upload file. Have you implemented action "' + this.config.uploader_action_path + '" correctly?') + }) + }) + + describe('form data', function() { + beforeEach(function() { + file = this.file = { name: 'foobar', type: 'image/jpg' } + append = this.append = sinon.stub() + FormData = function() { return { append: append } } + + Date.now = function() { return { toString: function() { return '1234' } } } + + this.xhr.prototype.open = sinon.stub() + this.xhr.prototype.send = sinon.stub() + + this.config.uploader_action_path = '/path/to/action' + + this.editor.action_upload(file, function() {}) + }) + + it('sets "_method"', function() { + expect(this.append).to.have.been.calledWith('_method', 'POST') + }) + + it('sets "authenticy_token"', function() { + expect(this.append).to.have.been.calledWith('authenticity_token', 'aMmw0/cl9FYg9Xi/SLCcdR0PASH1QOJrlQNr9rJOQ4g=') + }) + + it('sets "file"', function() { + expect(this.append).to.have.been.calledWith('file', this.file) + }) + }) + }) }) diff --git a/spec/javascripts/templates/editor.jst.ejs b/spec/javascripts/templates/editor.jst.ejs index 58ac953..052c083 100644 --- a/spec/javascripts/templates/editor.jst.ejs +++ b/spec/javascripts/templates/editor.jst.ejs @@ -1,3 +1,6 @@ + + +
  • From 296e946a6249dd749c2c302f93b9fefac5042873 Mon Sep 17 00:00:00 2001 From: Lukas Hodel Date: Tue, 9 Jul 2013 11:04:35 +0200 Subject: [PATCH 09/12] Fixes Editor.upload so that it returns the xhr --- app/assets/javascripts/active_admin/editor/editor.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/assets/javascripts/active_admin/editor/editor.js b/app/assets/javascripts/active_admin/editor/editor.js index ea3fef9..6a4f0a2 100644 --- a/app/assets/javascripts/active_admin/editor/editor.js +++ b/app/assets/javascripts/active_admin/editor/editor.js @@ -101,9 +101,9 @@ */ Editor.prototype.upload = function(file, callback) { if (config.uploader_action_path == null) { - this.s3_upload(file, callback); + return this.s3_upload(file, callback) } else { - this.action_upload(file, callback); + return this.action_upload(file, callback) } } From 76217ab36d9a6396f8d1c1084581e6d4bf6cac28 Mon Sep 17 00:00:00 2001 From: Lukas Hodel Date: Tue, 9 Jul 2013 11:10:40 +0200 Subject: [PATCH 10/12] Fixes upload_action spec so that the action_url is generated on the fly --- spec/javascripts/editor_spec.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/spec/javascripts/editor_spec.js b/spec/javascripts/editor_spec.js index 567e2c2..dcde02e 100644 --- a/spec/javascripts/editor_spec.js +++ b/spec/javascripts/editor_spec.js @@ -189,7 +189,8 @@ describe('Editor', function() { this.xhr.prototype.send = sinon.stub() this.config.uploader_action_path = '/path/to/action' xhr = this.editor.action_upload(sinon.stub(), function() {}) - expect(xhr.open).to.have.been.calledWith('POST', 'http://localhost:3500/path/to/action', true) + action_url = window.location.protocol + '//' + window.location.host + this.config.uploader_action_path + expect(xhr.open).to.have.been.calledWith('POST', action_url, true) }) it('sends the request', function() { From 01744d7d6aa5ff5e0c87552860e34accdf36fbc8 Mon Sep 17 00:00:00 2001 From: Lukas Hodel Date: Tue, 9 Jul 2013 11:39:23 +0200 Subject: [PATCH 11/12] Fixes xhr.upload event listeners --- app/assets/javascripts/active_admin/editor/config.js.erb | 7 ++++++- app/assets/javascripts/active_admin/editor/editor.js | 4 ++-- spec/javascripts/editor_spec.js | 4 ++-- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/app/assets/javascripts/active_admin/editor/config.js.erb b/app/assets/javascripts/active_admin/editor/config.js.erb index c61b3d7..f3b7792 100644 --- a/app/assets/javascripts/active_admin/editor/config.js.erb +++ b/app/assets/javascripts/active_admin/editor/config.js.erb @@ -10,5 +10,10 @@ config.spinner = '<%= asset_path 'active_admin/editor/loader.gif' %>' config.uploads_enabled = <%= ActiveAdmin::Editor.configuration.uploads_enabled? %> config.parserRules = <%= ActiveAdmin::Editor.configuration.parser_rules.to_json %> - config.uploader_action_path = '<%= ActiveAdmin::Editor.configuration.uploader_action_path.to_s %>' + + <% if path = ActiveAdmin::Editor.configuration.uploader_action_path %> + config.uploader_action_path = '<%= path.to_s %>' + <% else %> + config.uploader_action_path = null + <% end %> })(window) diff --git a/app/assets/javascripts/active_admin/editor/editor.js b/app/assets/javascripts/active_admin/editor/editor.js index 6a4f0a2..260a096 100644 --- a/app/assets/javascripts/active_admin/editor/editor.js +++ b/app/assets/javascripts/active_admin/editor/editor.js @@ -125,7 +125,7 @@ fd.append($('meta[name="csrf-param"]').attr('content'), $('meta[name="csrf-token"]').attr('content')) fd.append('file', file) - xhr.action_upload.addEventListener('progress', function(e) { + xhr.upload.addEventListener('progress', function(e) { _this.loaded = e.loaded _this.total = e.total _this.progress = e.loaded / e.total @@ -173,7 +173,7 @@ fd.append('Content-Type', file.type) fd.append('file', file) - xhr.s3_upload.addEventListener('progress', function(e) { + xhr.upload.addEventListener('progress', function(e) { _this.loaded = e.loaded _this.total = e.total _this.progress = e.loaded / e.total diff --git a/spec/javascripts/editor_spec.js b/spec/javascripts/editor_spec.js index dcde02e..e6060b6 100644 --- a/spec/javascripts/editor_spec.js +++ b/spec/javascripts/editor_spec.js @@ -84,7 +84,7 @@ describe('Editor', function() { describe('.s3_upload', function() { beforeEach(function() { - this.xhr.prototype.s3_upload = { addEventListener: sinon.stub() } + this.xhr.prototype.upload = { addEventListener: sinon.stub() } }) it('opens the connection to the proper bucket', function() { @@ -181,7 +181,7 @@ describe('Editor', function() { describe('.action_upload', function() { beforeEach(function() { - this.xhr.prototype.action_upload = { addEventListener: sinon.stub() } + this.xhr.prototype.upload = { addEventListener: sinon.stub() } }) it('opens the connection to the uploader action', function() { From 911f2e025da189d56120d39f28cbf1a89b29c25d Mon Sep 17 00:00:00 2001 From: Lukas Hodel Date: Tue, 9 Jul 2013 11:59:25 +0200 Subject: [PATCH 12/12] Adds Editor.upload mocha specs --- spec/javascripts/editor_spec.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/spec/javascripts/editor_spec.js b/spec/javascripts/editor_spec.js index e6060b6..7510005 100644 --- a/spec/javascripts/editor_spec.js +++ b/spec/javascripts/editor_spec.js @@ -82,6 +82,28 @@ describe('Editor', function() { }) }) + describe('.upload', function() { + it('calls s3_upload when uploader_action_path is not set', function() { + this.editor.s3_upload = sinon.stub() + this.editor.action_upload = sinon.stub() + this.config.s3_bucket = 'bucket' + this.config.uploader_action_path= null + xhr = this.editor.upload(sinon.stub(), function() {}) + expect(this.editor.s3_upload).to.have.been.called + expect(this.editor.action_upload).not.to.have.been.called + }) + + it('calls action_upload when uploader_action_path is set', function() { + this.editor.s3_upload = sinon.stub() + this.editor.action_upload = sinon.stub() + this.config.s3_bucket = 'bucket' + this.config.uploader_action_path= '/uploader/action' + xhr = this.editor.upload(sinon.stub(), function() {}) + expect(this.editor.s3_upload).not.to.have.been.called + expect(this.editor.action_upload).to.have.been.called + }) + }) + describe('.s3_upload', function() { beforeEach(function() { this.xhr.prototype.upload = { addEventListener: sinon.stub() }