Skip to content

Commit

Permalink
Chore: tests covering image upload problems (#1458)
Browse files Browse the repository at this point in the history
Fixes #1428: Image uploading feedback

- verify the error box is present when problems exist
- verify the image field has feedback text
- fixes code in app that does not express this behavior correctly

Pages covered:
- site: new, edit
- partner: new, edit
- article: new, edit
- user: new, edit, profile
  • Loading branch information
ivan-kocienski-gfsc authored Sep 15, 2022
1 parent 4af2a54 commit 55ff9b4
Show file tree
Hide file tree
Showing 8 changed files with 216 additions and 12 deletions.
20 changes: 10 additions & 10 deletions app/components/error/_error.html.erb
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<% if properties[:object].errors.any? %>
<div class="card text-white bg-danger mb-3" >
<div class="card-header"><h6><%= pluralize(properties[:object].errors.count, "error") %> prohibited this <%= properties[:object].class.name %> from being saved</h6></div>
<div class="card-body">
<div class="card-text">
<ul class="list-unstyle mb-0">
<% properties[:object].errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
<div id='form-errors' class="card text-white bg-danger mb-3" >
<div class="card-header"><h6><%= pluralize(properties[:object].errors.count, "error") %> prohibited this <%= properties[:object].class.name %> from being saved</h6></div>
<div class="card-body">
<div class="card-text">
<ul class="list-unstyle mb-0">
<% properties[:object].errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
</div>
</div>
</div>
<% end %>
2 changes: 1 addition & 1 deletion app/controllers/admin/sites_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def destroy
private

def set_site
@site = Site.friendly.find(params[:id])
@site ||= Site.friendly.find(params[:id])
end

def set_variables_for_sites_neighbourhoods_selection
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/users/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%= render_component "error", object: current_user %>
<%= render_component "error", object: f.object %>

<div class="form-group">
<%= render "contact", f: f %>
Expand Down
Binary file added test/fixtures/files/bad-cat-picture.bmp
Binary file not shown.
49 changes: 49 additions & 0 deletions test/integration/admin/articles_integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,53 @@ class Admin::ArticlesTest < ActionDispatch::IntegrationTest

assert_select 'select#article_author_id option[selected="selected"]', @partner_admin.admin_name
end

test 'new article image upload problem feedback' do
sign_in @root

new_article_params = {
title: 'a new article',
body: 'alpha beta cappa delta epsilon foxtrot etc',
author_id: @root.id,
article_image: fixture_file_upload("bad-cat-picture.bmp")
}

post admin_articles_path, params: { article: new_article_params }
assert_not response.redirect?

assert_select "h6", text: "1 error prohibited this Article from being saved"

# top of page form error box
assert_select '#form-errors li',
text: "Article image You are not allowed to upload \"bmp\" files, allowed types: svg, jpg, jpeg, png"

assert_select 'form .article_article_image .invalid-feedback',
text: "Article image You are not allowed to upload \"bmp\" files, allowed types: svg, jpg, jpeg, png"
end

test 'update article image upload problem feedback' do
sign_in @root

article = create(:article)

article_params = {
title: article.title,
body: article.body,
author_id: article.author_id,
article_image: fixture_file_upload("bad-cat-picture.bmp")
}

put admin_article_path(article), params: { article: article_params }
assert_not response.successful?

assert_select "h6", text: "1 error prohibited this Article from being saved"

# top of page form error box
assert_select '#form-errors li',
text: "Article image You are not allowed to upload \"bmp\" files, allowed types: svg, jpg, jpeg, png"

assert_select 'form .article_article_image .invalid-feedback',
text: "Article image You are not allowed to upload \"bmp\" files, allowed types: svg, jpg, jpeg, png"
end

end
41 changes: 41 additions & 0 deletions test/integration/admin/partner_integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,47 @@ class PartnerIntegrationTest < ActionDispatch::IntegrationTest
tag = tag_options.first
assert tag.attributes.key?('selected')
end

test 'Partner create form gives feedback on bad image selection' do
new_partner_params = {
name: 'A partner',
address_attributes: {
street_address: @partner.address.street_address,
postcode: @partner.address.postcode
},
image: fixture_file_upload("bad-cat-picture.bmp")
}

sign_in @admin
post admin_partners_path, params: { partner: new_partner_params }

assert_not response.redirect?

assert_select "h6", text: "1 error prohibited this Partner from being saved"
assert_select '#form-errors li', text: "Image You are not allowed to upload \"bmp\" files, allowed types: jpg, jpeg, gif, png"
assert_select 'form .partner_image .invalid-feedback', text: "Image You are not allowed to upload \"bmp\" files, allowed types: jpg, jpeg, gif, png"
end

test 'Partner update form gives feedback on bad image selection' do
partner_params = {
name: @partner.name,
address_attributes: {
street_address: @partner.address.street_address,
postcode: @partner.address.postcode
},
image: fixture_file_upload("bad-cat-picture.bmp")
}

sign_in @admin
put admin_partner_path(@partner), params: { partner: partner_params }

assert_not response.redirect?

assert_select "h6", text: "1 error prohibited this Partner from being saved"
assert_select '#form-errors li', text: "Image You are not allowed to upload \"bmp\" files, allowed types: jpg, jpeg, gif, png"
assert_select 'form .partner_image .invalid-feedback', text: "Image You are not allowed to upload \"bmp\" files, allowed types: jpg, jpeg, gif, png"
end

end

# Capybara feature test that doesn't work and i have no time to fix
Expand Down
54 changes: 54 additions & 0 deletions test/integration/admin/sites_integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,58 @@ class AdminSitesIntegrationTest < ActionDispatch::IntegrationTest
tag = tag_options.first
assert tag.attributes.key?('selected')
end

test 'new site image upload problem feedback' do
sign_in @root

new_site_params = {
name: 'a new site',
domain: 'a-domain',
slug: 'a-slug',
logo: fixture_file_upload("bad-cat-picture.bmp"),
footer_logo: fixture_file_upload("bad-cat-picture.bmp"),
hero_image: fixture_file_upload("bad-cat-picture.bmp"),
}

post admin_sites_path, params: { site: new_site_params }
assert_not response.successful?

assert_select "h6", text: "3 errors prohibited this Site from being saved"

# top of page form error box
assert_select '#form-errors li', text: "Logo You are not allowed to upload \"bmp\" files, allowed types: svg, png"
assert_select '#form-errors li', text: "Footer logo You are not allowed to upload \"bmp\" files, allowed types: svg, png"
assert_select '#form-errors li', text: "Hero image You are not allowed to upload \"bmp\" files, allowed types: jpg, jpeg, png"

assert_select 'form .site_logo .invalid-feedback', text: "Logo You are not allowed to upload \"bmp\" files, allowed types: svg, png"
assert_select 'form .site_footer_logo .invalid-feedback', text: "Footer logo You are not allowed to upload \"bmp\" files, allowed types: svg, png"
assert_select 'form .site_hero_image .invalid-feedback', text: "Hero image You are not allowed to upload \"bmp\" files, allowed types: jpg, jpeg, png"
end

test 'update site image upload problem feedback' do
sign_in @root

site_params = {
name: 'a new site',
domain: 'a-domain',
slug: 'a-slug',
logo: fixture_file_upload("bad-cat-picture.bmp"),
footer_logo: fixture_file_upload("bad-cat-picture.bmp"),
hero_image: fixture_file_upload("bad-cat-picture.bmp"),
}

put admin_site_path(@site), params: { site: site_params }
assert_not response.successful?

assert_select "h6", text: "3 errors prohibited this Site from being saved"

# top of page form error box
assert_select '#form-errors li', text: "Logo You are not allowed to upload \"bmp\" files, allowed types: svg, png"
assert_select '#form-errors li', text: "Footer logo You are not allowed to upload \"bmp\" files, allowed types: svg, png"
assert_select '#form-errors li', text: "Hero image You are not allowed to upload \"bmp\" files, allowed types: jpg, jpeg, png"

assert_select 'form .site_logo .invalid-feedback', text: "Logo You are not allowed to upload \"bmp\" files, allowed types: svg, png"
assert_select 'form .site_footer_logo .invalid-feedback', text: "Footer logo You are not allowed to upload \"bmp\" files, allowed types: svg, png"
assert_select 'form .site_hero_image .invalid-feedback', text: "Hero image You are not allowed to upload \"bmp\" files, allowed types: jpg, jpeg, png"
end
end
60 changes: 60 additions & 0 deletions test/integration/admin/user_integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -236,4 +236,64 @@ class AdminUserIntegrationTest < ActionDispatch::IntegrationTest

assert_select "p.has-no-admin-rights-warning"
end

test 'new user avatar upload problem feedback' do
sign_in @root

new_user_params = {
email: '[email protected]',
role: 'root',
avatar: fixture_file_upload("bad-cat-picture.bmp"),
}

post admin_users_path, params: { user: new_user_params }
assert_not response.redirect?

assert_select "h6", text: "1 error prohibited this User from being saved"

# top of page form error box
assert_select '#form-errors li', text: "Avatar You are not allowed to upload \"bmp\" files, allowed types: jpg, jpeg, png"

assert_select 'form .user_avatar .invalid-feedback', text: "Avatar You are not allowed to upload \"bmp\" files, allowed types: jpg, jpeg, png"
end

test 'update user avatar upload problem feedback' do
sign_in @root

user_params = {
email: @root.email,
role: @root.role,
avatar: fixture_file_upload("bad-cat-picture.bmp"),
}

put admin_user_path(@root), params: { user: user_params }
assert_not response.redirect?

assert_select "h6", text: "1 error prohibited this User from being saved"

# top of page form error box
assert_select '#form-errors li', text: "Avatar You are not allowed to upload \"bmp\" files, allowed types: jpg, jpeg, png"

assert_select 'form .user_avatar .invalid-feedback', text: "Avatar You are not allowed to upload \"bmp\" files, allowed types: jpg, jpeg, png"
end

test 'update profile avatar upload problem feedback' do
sign_in @root

user_params = {
email: @root.email,
avatar: fixture_file_upload("bad-cat-picture.bmp"),
}

patch update_profile_admin_user_path(@root), params: { user: user_params }
assert_not response.redirect?

assert_select "h6", text: "1 error prohibited this User from being saved"

# top of page form error box
assert_select '#form-errors li', text: "Avatar You are not allowed to upload \"bmp\" files, allowed types: jpg, jpeg, png"

assert_select 'form .user_avatar .invalid-feedback', text: "Avatar You are not allowed to upload \"bmp\" files, allowed types: jpg, jpeg, png"
end

end

0 comments on commit 55ff9b4

Please sign in to comment.