Duplicate documentable code and rename for imageable
This commit is contained in:
@@ -304,7 +304,7 @@ shared_examples "documentable" do |documentable_factory_name, documentable_path,
|
||||
from: send(documentable_path, arguments))
|
||||
|
||||
expect(page).to have_content "You can upload up to a maximum of #{max_file_size(documentable)} documents."
|
||||
expect(page).to have_content "You can upload #{humanized_accepted_content_types(documentable)} files."
|
||||
expect(page).to have_content "You can upload #{documentable_humanized_accepted_content_types(documentable)} files."
|
||||
expect(page).to have_content "You can upload files up to #{max_file_size(documentable)} MB."
|
||||
end
|
||||
|
||||
|
||||
331
spec/shared/features/imageable.rb
Normal file
331
spec/shared/features/imageable.rb
Normal file
@@ -0,0 +1,331 @@
|
||||
shared_examples "imageable" do |imageable_factory_name, imageable_path, imageable_path_arguments|
|
||||
include ActionView::Helpers
|
||||
include ImagesHelper
|
||||
include ImageablesHelper
|
||||
|
||||
let!(:administrator) { create(:user) }
|
||||
let!(:user) { create(:user) }
|
||||
let!(:arguments) { {} }
|
||||
let!(:imageable) { create(imageable_factory_name, author: user) }
|
||||
let!(:imageable_dom_name) { imageable_factory_name.parameterize }
|
||||
|
||||
before do
|
||||
create(:administrator, user: administrator)
|
||||
|
||||
imageable_path_arguments.each do |argument_name, path_to_value|
|
||||
arguments.merge!("#{argument_name}": imageable.send(path_to_value))
|
||||
end
|
||||
end
|
||||
|
||||
context "Show" do
|
||||
|
||||
scenario "Show descriptive image when exists", :js do
|
||||
image = create(:image, imageable: imageable)
|
||||
|
||||
visit send(imageable_path, arguments)
|
||||
|
||||
expect(page).to have_css("img[alt='#{image.title}']")
|
||||
end
|
||||
|
||||
scenario "Show image title when image exists" do
|
||||
image = create(:image, imageable: imageable)
|
||||
|
||||
visit send(imageable_path, arguments)
|
||||
|
||||
expect(page).to have_content image.title
|
||||
end
|
||||
|
||||
scenario "Should not display upload image button when there is no logged user" do
|
||||
visit send(imageable_path, arguments)
|
||||
|
||||
within "##{dom_id(imageable)}" do
|
||||
expect(page).not_to have_link("Upload image")
|
||||
end
|
||||
end
|
||||
|
||||
scenario "Should not display upload image button when maximum number of images reached " do
|
||||
create_list(:image, 3, imageable: imageable)
|
||||
visit send(imageable_path, arguments)
|
||||
|
||||
within "##{dom_id(imageable)}" do
|
||||
expect(page).not_to have_link("Upload image")
|
||||
end
|
||||
end
|
||||
|
||||
scenario "Should display upload image button when user is logged in and is imageable owner" do
|
||||
login_as(user)
|
||||
|
||||
visit send(imageable_path, arguments)
|
||||
|
||||
within "##{dom_id(imageable)}" do
|
||||
expect(page).to have_link("Upload image")
|
||||
end
|
||||
end
|
||||
|
||||
scenario "Should display upload image button when admin is logged in" do
|
||||
login_as(administrator)
|
||||
|
||||
visit send(imageable_path, arguments)
|
||||
|
||||
within "##{dom_id(imageable)}" do
|
||||
expect(page).to have_link("Upload image")
|
||||
end
|
||||
end
|
||||
|
||||
scenario "Should navigate to new image page when click un upload button" do
|
||||
login_as(user)
|
||||
|
||||
visit send(imageable_path, arguments)
|
||||
click_link "Upload image"
|
||||
|
||||
expect(page).to have_selector("h1", text: "Upload image")
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
context "New" do
|
||||
|
||||
scenario "Should not be able for unathenticated users" do
|
||||
visit new_image_path(imageable_type: imageable.class.name,
|
||||
imageable_id: imageable.id)
|
||||
|
||||
expect(page).to have_content("You must sign in or register to continue.")
|
||||
end
|
||||
|
||||
scenario "Should not be able for other users" do
|
||||
login_as create(:user)
|
||||
|
||||
visit new_image_path(imageable_type: imageable.class.name,
|
||||
imageable_id: imageable.id)
|
||||
|
||||
expect(page).to have_content("You do not have permission to carry out the action 'new' on image. ")
|
||||
end
|
||||
|
||||
scenario "Should be able to imageable author" do
|
||||
login_as imageable.author
|
||||
|
||||
visit new_image_path(imageable_type: imageable.class.name,
|
||||
imageable_id: imageable.id)
|
||||
|
||||
expect(page).to have_selector("h1", text: "Upload image")
|
||||
end
|
||||
|
||||
scenario "Should display file name after file selection", :js do
|
||||
login_as imageable.author
|
||||
visit new_image_path(imageable_type: imageable.class.name,
|
||||
imageable_id: imageable.id)
|
||||
|
||||
attach_file :image_attachment, "spec/fixtures/files/empty.pdf", make_visible: true
|
||||
sleep 1
|
||||
|
||||
expect(page).to have_content "empty.pdf"
|
||||
end
|
||||
|
||||
scenario "Should not display file name after file selection", :js do
|
||||
login_as imageable.author
|
||||
visit new_image_path(imageable_type: imageable.class.name,
|
||||
imageable_id: imageable.id)
|
||||
|
||||
attach_file :image_attachment, "spec/fixtures/files/logo_header.png", make_visible: true
|
||||
sleep 1
|
||||
|
||||
expect(page).not_to have_content "logo_header.jpg"
|
||||
end
|
||||
|
||||
scenario "Should update loading bar style after valid file upload", :js do
|
||||
login_as imageable.author
|
||||
visit new_image_path(imageable_type: imageable.class.name,
|
||||
imageable_id: imageable.id)
|
||||
|
||||
attach_file :image_attachment, "spec/fixtures/files/clippy.jpg", make_visible: true
|
||||
sleep 1
|
||||
|
||||
expect(page).to have_selector ".loading-bar.complete"
|
||||
end
|
||||
|
||||
scenario "Should update loading bar style after unvalid file upload", :js do
|
||||
login_as imageable.author
|
||||
visit new_image_path(imageable_type: imageable.class.name,
|
||||
imageable_id: imageable.id)
|
||||
|
||||
attach_file :image_attachment, "spec/fixtures/files/logo_header.png", make_visible: true
|
||||
sleep 1
|
||||
|
||||
expect(page).to have_selector ".loading-bar.errors"
|
||||
end
|
||||
|
||||
scenario "Should update image title with attachment original file name after file selection if no title defined by user", :js do
|
||||
login_as imageable.author
|
||||
visit new_image_path(imageable_type: imageable.class.name,
|
||||
imageable_id: imageable.id)
|
||||
|
||||
attach_file :image_attachment, "spec/fixtures/files/clippy.jpg", make_visible: true
|
||||
sleep 1
|
||||
|
||||
expect(find("input[name='image[title]']").value).to eq("clippy.jpg")
|
||||
end
|
||||
|
||||
scenario "Should not update image title with attachment original file name after file selection when title already defined by user", :js do
|
||||
login_as imageable.author
|
||||
visit new_image_path(imageable_type: imageable.class.name,
|
||||
imageable_id: imageable.id)
|
||||
|
||||
fill_in :image_title, with: "My custom title"
|
||||
attach_file :image_attachment, "spec/fixtures/files/clippy.jpg", make_visible: true
|
||||
sleep 1
|
||||
|
||||
expect(find("input[name='image[title]']").value).to eq("My custom title")
|
||||
end
|
||||
|
||||
scenario "Should update image cached_attachment field after valid file upload", :js do
|
||||
login_as imageable.author
|
||||
visit new_image_path(imageable_type: imageable.class.name,
|
||||
imageable_id: imageable.id)
|
||||
|
||||
attach_file :image_attachment, "spec/fixtures/files/clippy.jpg", make_visible: true
|
||||
sleep 1
|
||||
|
||||
expect(find("input[name='image[cached_attachment]']", visible: false).value).to include("clippy.jpg")
|
||||
end
|
||||
|
||||
scenario "Should not update image cached_attachment field after unvalid file upload", :js do
|
||||
login_as imageable.author
|
||||
visit new_image_path(imageable_type: imageable.class.name,
|
||||
imageable_id: imageable.id)
|
||||
|
||||
attach_file :image_attachment, "spec/fixtures/files/logo_header.png", make_visible: true
|
||||
sleep 1
|
||||
|
||||
expect(find("input[name='image[cached_attachment]']", visible: false).value).to eq ""
|
||||
end
|
||||
|
||||
scenario "Should show imageable custom recomentations" do
|
||||
login_as imageable.author
|
||||
visit new_image_path(imageable_type: imageable.class.name,
|
||||
imageable_id: imageable.id,
|
||||
from: send(imageable_path, arguments))
|
||||
|
||||
expect(page).to have_content "You can upload only one image. Proposals with image attract more attention of the users."
|
||||
expect(page).to have_content "You can upload #{imageable_humanized_accepted_content_types} image."
|
||||
expect(page).to have_content "You can upload one image up to #{imageable_max_file_size} MB."
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
context "Create" do
|
||||
|
||||
scenario "Should show validation errors" do
|
||||
login_as imageable.author
|
||||
visit new_image_path(imageable_type: imageable.class.name,
|
||||
imageable_id: imageable.id)
|
||||
|
||||
click_on "Upload image"
|
||||
|
||||
expect(page).to have_content "3 errors prevented this Image from being saved: "
|
||||
expect(page).to have_selector "small.error", text: "can't be blank", count: 2
|
||||
end
|
||||
|
||||
scenario "Should show error notice after unsuccessfull image upload" do
|
||||
login_as imageable.author
|
||||
|
||||
visit new_image_path(imageable_type: imageable.class.name,
|
||||
imageable_id: imageable.id,
|
||||
from: send(imageable_path, arguments))
|
||||
attach_file :image_attachment, "spec/fixtures/files/empty.pdf"
|
||||
sleep 1
|
||||
click_on "Upload image"
|
||||
|
||||
expect(page).to have_content "Cannot create image. Check form errors and try again."
|
||||
end
|
||||
|
||||
scenario "Should show success notice after successfull image upload" do
|
||||
login_as imageable.author
|
||||
|
||||
visit new_image_path(imageable_type: imageable.class.name,
|
||||
imageable_id: imageable.id,
|
||||
from: send(imageable_path, arguments))
|
||||
fill_in :image_title, with: "Image title"
|
||||
attach_file :image_attachment, "spec/fixtures/files/clippy.jpg"
|
||||
sleep 1
|
||||
click_on "Upload image"
|
||||
|
||||
expect(page).to have_content "Image was created successfully."
|
||||
end
|
||||
|
||||
scenario "Should redirect to imageable path after successfull image upload" do
|
||||
login_as imageable.author
|
||||
|
||||
visit new_image_path(imageable_type: imageable.class.name,
|
||||
imageable_id: imageable.id,
|
||||
from: send(imageable_path, arguments))
|
||||
fill_in :image_title, with: "Image title"
|
||||
attach_file :image_attachment, "spec/fixtures/files/clippy.jpg"
|
||||
sleep 1
|
||||
click_on "Upload image"
|
||||
|
||||
within "##{dom_id(imageable)}" do
|
||||
expect(page).to have_selector "h1", text: imageable.title
|
||||
end
|
||||
end
|
||||
|
||||
scenario "Should show new image on imageable images tab after successfull image upload" do
|
||||
login_as imageable.author
|
||||
|
||||
visit new_image_path(imageable_type: imageable.class.name,
|
||||
imageable_id: imageable.id,
|
||||
from: send(imageable_path, arguments))
|
||||
fill_in :image_title, with: "Image title"
|
||||
attach_file :image_attachment, "spec/fixtures/files/clippy.jpg"
|
||||
sleep 1
|
||||
click_on "Upload image"
|
||||
|
||||
expect(page).to have_content "Image title"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
context "Destroy" do
|
||||
|
||||
let!(:image) { create(:image, imageable: imageable, user: imageable.author) }
|
||||
|
||||
scenario "Should show success notice after successfull deletion by an admin" do
|
||||
login_as administrator
|
||||
|
||||
visit send(imageable_path, arguments)
|
||||
click_on "Remove image"
|
||||
|
||||
expect(page).to have_content "Image was deleted successfully."
|
||||
end
|
||||
|
||||
scenario "Should show success notice after successfull deletion" do
|
||||
login_as imageable.author
|
||||
|
||||
visit send(imageable_path, arguments)
|
||||
click_on "Remove image"
|
||||
|
||||
expect(page).to have_content "Image was deleted successfully."
|
||||
end
|
||||
|
||||
scenario "Should not show image after successful deletion" do
|
||||
login_as imageable.author
|
||||
|
||||
visit send(imageable_path, arguments)
|
||||
click_on "Remove image"
|
||||
|
||||
expect(page).not_to have_selector "figure img"
|
||||
end
|
||||
|
||||
scenario "Should redirect to imageable path after successful deletion" do
|
||||
login_as imageable.author
|
||||
|
||||
visit send(imageable_path, arguments)
|
||||
click_on "Remove image"
|
||||
|
||||
within "##{dom_id(imageable)}" do
|
||||
expect(page).to have_selector "h1", text: imageable.title
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
76
spec/shared/models/acts_as_imageable.rb
Normal file
76
spec/shared/models/acts_as_imageable.rb
Normal file
@@ -0,0 +1,76 @@
|
||||
shared_examples "acts as imageable" do |imageable_factory|
|
||||
|
||||
let!(:image) { build(:image, imageable_factory.to_sym) }
|
||||
let!(:imageable) { image.imageable }
|
||||
|
||||
it "should be valid" do
|
||||
expect(image).to be_valid
|
||||
end
|
||||
|
||||
describe "file extension" do
|
||||
|
||||
it "should not be valid with '.png' extension" do
|
||||
image.attachment = File.new("spec/fixtures/files/clippy.png")
|
||||
|
||||
expect(image).to_not be_valid
|
||||
expect(image.errors[:attachment].size).to eq(1)
|
||||
end
|
||||
|
||||
it "should not be valid with '.gif' extension" do
|
||||
image.attachment = File.new("spec/fixtures/files/clippy.gif")
|
||||
|
||||
expect(image).to_not be_valid
|
||||
expect(image.errors[:attachment].size).to eq(1)
|
||||
end
|
||||
|
||||
it "should be valid with '.jpg' extension" do
|
||||
image.attachment = File.new("spec/fixtures/files/clippy.jpg")
|
||||
|
||||
expect(image).to be_valid
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
describe "image dimmessions" do
|
||||
|
||||
it "should be valid when image dimmessions are 475X475 at least" do
|
||||
expect(image).to be_valid
|
||||
end
|
||||
|
||||
it "should not be valid when image dimmensions are smaller than 475X475" do
|
||||
image.attachment = File.new("spec/fixtures/files/logo_header.jpg")
|
||||
|
||||
expect(image).not_to be_valid
|
||||
end
|
||||
end
|
||||
|
||||
describe "title" do
|
||||
|
||||
it "should not be valid when correct image attached but no image title provided" do
|
||||
image.title = ''
|
||||
|
||||
expect(image).to_not be_valid
|
||||
end
|
||||
|
||||
it "should not be valid when image title is too short" do
|
||||
image.title = 'a' * 3
|
||||
|
||||
expect(image).to_not be_valid
|
||||
end
|
||||
|
||||
it "should not be valid when image title is too long" do
|
||||
image.title = 'a' * 81
|
||||
|
||||
expect(image).to_not be_valid
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
it "image destroy should remove image from file storage" do
|
||||
image.save
|
||||
image_url = image.attachment.url
|
||||
|
||||
expect{ image.attachment.destroy }.to change{ image.attachment.url }.from(image_url).to("/attachments/original/missing.png")
|
||||
end
|
||||
|
||||
end
|
||||
Reference in New Issue
Block a user