Skip to content
This repository has been archived by the owner on Oct 29, 2024. It is now read-only.

Commit

Permalink
✅ Add file upload tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ledermann committed Jun 3, 2017
1 parent 5460b3f commit e6f852a
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 15 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@

# Ignore simplecov result
/coverage/*

# Ignore Shrine uploads
/public/uploads
25 changes: 14 additions & 11 deletions config/initializers/shrine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@
return if ARGV.join.include?('assets:precompile')

require 'shrine'
require 'shrine/storage/s3'

s3_options = {
access_key_id: ENV.fetch('AWS_ACCESS_KEY_ID'),
secret_access_key: ENV.fetch('AWS_SECRET_ACCESS_KEY'),
region: ENV.fetch('AWS_REGION'),
bucket: ENV.fetch('AWS_BUCKET')
}
unless Rails.env.test?
require 'shrine/storage/s3'

Shrine.storages = {
cache: Shrine::Storage::S3.new(prefix: "#{Rails.env}/cache", **s3_options),
store: Shrine::Storage::S3.new(prefix: "#{Rails.env}/store", **s3_options)
}
s3_options = {
access_key_id: ENV.fetch('AWS_ACCESS_KEY_ID'),
secret_access_key: ENV.fetch('AWS_SECRET_ACCESS_KEY'),
region: ENV.fetch('AWS_REGION'),
bucket: ENV.fetch('AWS_BUCKET')
}

Shrine.storages = {
cache: Shrine::Storage::S3.new(prefix: "#{Rails.env}/cache", **s3_options),
store: Shrine::Storage::S3.new(prefix: "#{Rails.env}/store", **s3_options)
}
end

Shrine.plugin :activerecord
Shrine.plugin :cached_attachment_data # for forms
23 changes: 19 additions & 4 deletions spec/features/posts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
expect(page).to_not have_selector('td', text: 'Example')
end

scenario 'creates a new page', js: true do
scenario 'creates a new page' do
visit posts_path(as: create(:admin))

click_on 'Add new Post'
Expand All @@ -145,15 +145,30 @@
expect(page).to have_button('Create Post')

fill_in 'post[title]', with: 'Bar'
click_on 'Create Post'
expect(page).to have_selector('div.form-group.has-danger')
fill_in 'post[content]', with: 'dolor sit amet'
expect(page).to_not have_selector('div.form-group.has-danger')
attach_file 'post[image]', Rails.root.join('spec/fixtures', 'example.jpg')
click_on 'Create Post'

expect(page).to have_text 'Post was successfully created.'
expect(page).to have_selector('h1', text: 'Bar')
expect(page).to have_selector('p', text: 'dolor sit amet')
expect(page).to have_selector('img', class: 'img-fluid')
end

scenario 'creates a new page with client side validation', js: true do
visit new_post_path(as: create(:admin))

fill_in 'post[title]', with: 'Bar'
click_on 'Create Post'

expect(page).to have_selector('.form-group.has-danger')

fill_in 'post[content]', with: 'dolor sit amet'
expect(page).to_not have_selector('.form-group.has-danger')

click_on 'Create Post'

expect(page).to have_text 'Post was successfully created.'
end
end
end
Binary file added spec/fixtures/example.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions spec/fixtures/example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Simple text file
3 changes: 3 additions & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
require 'email_spec'
require 'email_spec/rspec'

# needed for fixture_file_upload
include ActionDispatch::TestProcess

# Checks for pending migration and applies them before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.maintain_test_schema!
Expand Down
13 changes: 13 additions & 0 deletions spec/support/shrine.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'fileutils'
require 'shrine/storage/file_system'

Shrine.storages = {
cache: Shrine::Storage::FileSystem.new('public', prefix: 'uploads/cache'),
store: Shrine::Storage::FileSystem.new('public', prefix: 'uploads/store')
}

RSpec.configure do |config|
config.before :suite do
FileUtils.rm_r 'public/uploads'
end
end
24 changes: 24 additions & 0 deletions spec/uploaders/image_uploader_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'rails_helper'

describe ImageUploader do
let(:post) { create :post, image: fixture_file_upload('spec/fixtures/example.jpg') }

it 'generates image versions' do
expect(post.image.keys).to eq([:original, :thumbnail, :panorama])
end

it 'stores image' do
expect(post.image[:original].storage_key).to eq('store')
end

it 'stores mime type' do
expect(post.image[:original].mime_type).to eq('image/jpeg')
end

it 'rejects other files than images' do
post = build :post, image: fixture_file_upload('spec/fixtures/example.txt')

post.valid?
expect(post.errors[:image]).to eq ["isn't of allowed type (allowed types: image/jpeg, image/png, image/gif)"]
end
end

0 comments on commit e6f852a

Please sign in to comment.