Skip to content

Commit

Permalink
Improve Rubocop config, fix offenses
Browse files Browse the repository at this point in the history
  • Loading branch information
n-rodriguez committed Sep 3, 2024
1 parent bd6fcbe commit 0e6c71f
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 61 deletions.
23 changes: 22 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ AllCops:
- bin/*
- lib/generators/**/*.rb
- gemfiles/*
- spec/**/*
- spec/dummy/**/*

Gemspec/RequireMFA:
Enabled: false
Expand All @@ -36,6 +36,7 @@ Layout/LineLength:
Max: 130
Exclude:
- remotipart.gemspec
- spec/remotipart/features/comments_spec.rb

Layout/EmptyLines:
Enabled: false
Expand All @@ -58,3 +59,23 @@ Layout/EmptyLinesAroundModuleBody:

Naming/BlockForwarding:
Enabled: false

#########
# RSPEC #
#########

RSpec/MetadataStyle:
EnforcedStyle: hash

RSpec/ExampleLength:
Exclude:
- spec/remotipart/features/comments_spec.rb

RSpec/MultipleExpectations:
Max: 9

RSpec/NotToNot:
EnforcedStyle: to_not

Capybara/ClickLinkOrButtonStyle:
EnforcedStyle: strict
2 changes: 2 additions & 0 deletions spec/config_capybara.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

def register_driver(driver_name, args = [])
opts = { js_errors: true, headless: true, window_size: [1920, 1200], browser_options: {} }
args.each do |arg|
Expand Down
8 changes: 5 additions & 3 deletions spec/config_rspec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# Configure RSpec
RSpec.configure do |config|
config.include Rails.application.routes.url_helpers
Expand Down Expand Up @@ -31,16 +33,16 @@
DatabaseCleaner.strategy = :truncation
end

config.before(:each) do
config.before do
DatabaseCleaner.start
end

config.after(:each) do
config.after do
DatabaseCleaner.clean
end

if ENV.key?('GITHUB_ACTIONS')
config.around(:each) do |ex|
config.around do |ex|
ex.run_with_retry retry: 3
end
end
Expand Down
103 changes: 53 additions & 50 deletions spec/remotipart/features/comments_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'comments', type: :feature do
Expand Down Expand Up @@ -31,7 +33,7 @@
expect(page).to have_link('Cancel')
end

it "cancels creating a comment", js: true do
it 'cancels creating a comment', js: true do
visit root_path
click_link 'New Comment'

Expand All @@ -44,7 +46,7 @@
expect(page).to have_link('New Comment')
end

it "deletes a comment", js: true do
it 'deletes a comment', js: true do
Comment.create(subject: 'The Great Yogurt', body: 'The Schwarz is strong with this one.')
visit root_path

Expand All @@ -58,7 +60,7 @@
end
end

it "uploads a file", js: true do
it 'uploads a file', js: true do
visit root_path
click_link 'New Comment with Attachment'

Expand All @@ -83,19 +85,20 @@
end

within '#comments' do
expect(page).to have_selector("td", text: comment_subject)
expect(page).to have_selector("td", text: comment_body)
expect(page).to have_selector("a", text: File.basename(file_path))
expect(page).to have_selector("a", text: File.basename(other_file_path))
expect(page).to have_css('td', text: comment_subject)
expect(page).to have_css('td', text: comment_body)
expect(page).to have_css('a', text: File.basename(file_path))
expect(page).to have_css('a', text: File.basename(other_file_path))
end
end

it "Disables submit button while submitting", js: true do
it 'Disables submit button while submitting', js: true do
visit root_path

click_link 'New Comment'

# Needed to make test wait for above to finish
form = find('form')
find('form')

button = find_button('Create Comment')
page.execute_script(%q{$('form').append('<input name="pause" type="hidden" value=1 />');})
Expand All @@ -105,15 +108,15 @@
click_button 'Create Comment'

expect(button[:disabled]).to be true
expect(button.value).to eq "Submitting..."
expect(button.value).to eq 'Submitting...'

sleep 1.5

expect(button[:disabled]).to be false
expect(button.value).to eq "Create Comment"
expect(button.value).to eq 'Create Comment'
end

it "triggers ajax:remotipartSubmit event hook", js: true do
it 'triggers ajax:remotipartSubmit event hook', js: true do
visit root_path
page.execute_script("$(document).delegate('form', 'ajax:remotipartSubmit', function() { $('#comments').after('remotipart!'); });")

Expand All @@ -127,7 +130,7 @@
expect(page).to have_content('remotipart!')
end

it "allows remotipart submission to be cancelable via event hook", js: true do
it 'allows remotipart submission to be cancelable via event hook', js: true do
visit root_path
page.execute_script("$(document).delegate('form', 'ajax:remotipartSubmit', function() { $('#comments').after('remotipart!'); return false; });")

Expand All @@ -148,14 +151,14 @@
end
end

it "allows custom data-type on form", js: true do
it 'allows custom data-type on form', js: true do
visit root_path
page.execute_script("$(document).delegate('form', 'ajax:success', function(evt, data, status, xhr) { $('#comments').after(xhr.responseText); });")

click_link 'New Comment with Attachment'

# Needed to make test wait for above to finish
form = find('form')
find('form')
page.execute_script("$('form').attr('data-type', 'html');")

file_path = File.join(fixture_path, 'qr.jpg')
Expand All @@ -167,14 +170,14 @@
expect(page).to have_content('HTML response')
end

it "allows users to use ajax response data safely", js: true do
it 'allows users to use ajax response data safely', js: true do
visit root_path
page.execute_script("$(document).delegate('form', 'ajax:success', function(evt, data, status, xhr) { $('#comments').after(data); });")

click_link 'New Comment with Attachment'

# Needed to make test wait for above to finish
form = find('form')
find('form')
page.execute_script("$('form').attr('data-type', 'html');")

file_path = File.join(fixture_path, 'qr.jpg')
Expand All @@ -186,14 +189,14 @@
expect(page).to have_content('HTML response')
end

it "escapes html response content properly", js: true do
it 'escapes html response content properly', js: true do
visit root_path
page.execute_script("$(document).delegate('form', 'ajax:success', function(evt, data, status, xhr) { $('#comments').after(xhr.responseText); });")

click_link 'New Comment with Attachment'

# Needed to make test wait for above to finish
form = find('form')
find('form')
page.execute_script("$('form').attr('data-type', 'html');")
page.execute_script("$('form').append('<input type=\"hidden\" name=\"template\" value=\"escape\" />');")

Expand All @@ -206,29 +209,29 @@
expect(find('input[name="quote"]').value).to eq '"'
end

it "returns the correct response status", js: true do
it 'returns the correct response status', js: true do
visit root_path

click_link 'New Comment with Attachment'
# Needed to make test wait for above to finish
input = find('#comment_subject')
find_by_id('comment_subject')
page.execute_script("$('#comment_subject').removeAttr('required');")

file_path = File.join(fixture_path, 'qr.jpg')
fill_in 'comment_body', with: 'there'
attach_file 'comment_attachment', file_path
click_button 'Create Comment'

#within '#error_explanation' do
# expect(page).to have_content "Subject can't be blank"
#end
expect(page).to have_content "Error status code: 422"
# within '#error_explanation' do
# expect(page).to have_content "Subject can't be blank"
# end
expect(page).to have_content 'Error status code: 422'

message = Rails.version >= "7.1" ? "Content" : "Entity"
message = Rails.version >= '7.1' ? 'Content' : 'Entity'
expect(page).to have_content "Error status message: Unprocessable #{message}"
end

it "passes the method as _method parameter (rails convention)", js: true do
it 'passes the method as _method parameter (rails convention)', js: true do
visit root_path

click_link 'New Comment with Attachment'
Expand All @@ -244,7 +247,7 @@
expect(page).to have_content 'PUT request!'
end

it "does not submit via remotipart unless file is present", js: true do
it 'does not submit via remotipart unless file is present', js: true do
visit root_path
page.execute_script("$(document).delegate('form', 'ajax:remotipartSubmit', function() { $('#comments').after('remotipart!'); });")

Expand All @@ -257,12 +260,12 @@
expect(page).to have_no_content('remotipart!')
end

it "fires all the ajax callbacks on the form", js: true do
it 'fires all the ajax callbacks on the form', js: true do
visit root_path
click_link 'New Comment with Attachment'

# Needed to make test wait for above to finish
form = find('form')
find('form')

page.execute_script("$('form').bind('ajax:beforeSend', function() { $('#comments').after('thebefore'); });")
page.execute_script("$(document).delegate('form', 'ajax:success', function() { $('#comments').after('success'); });")
Expand All @@ -279,12 +282,12 @@
expect(page).to have_content('complete')
end

it "fires the ajax callbacks for json data-type with remotipart", js: true do
it 'fires the ajax callbacks for json data-type with remotipart', js: true do
visit root_path
click_link 'New Comment with Attachment'

# Needed to make test wait for above to finish
form = find('form')
find('form')

page.execute_script("$('form').data('type', 'json');")

Expand All @@ -303,12 +306,12 @@
expect(page).to have_content('complete')
end

it "only fires the beforeSend hook once", js: true do
it 'only fires the beforeSend hook once', js: true do
visit root_path
click_link 'New Comment with Attachment'

# Needed to make test wait for above to finish
form = find('form')
find('form')

page.execute_script("$('form').bind('ajax:beforeSend', function() { $('#comments').after('<div class=\"ajax\">ajax!</div>'); });")

Expand All @@ -318,10 +321,10 @@
attach_file 'comment_attachment', file_path
click_button 'Create Comment'

expect(page).to have_css("div.ajax", :count => 1)
expect(page).to have_css('div.ajax', count: 1)
end

it "cleans up after itself when uploading files", js: true do
it 'cleans up after itself when uploading files', js: true do
visit root_path
page.execute_script("$(document).delegate('form', 'ajax:remotipartSubmit', function(evt, xhr, data) { if ($(this).data('remotipartSubmitted')) { $('#comments').after('remotipart before!'); } });")

Expand All @@ -340,7 +343,7 @@
expect(page).to have_content('no remotipart after!')
end

it "submits via remotipart when a file upload is present", js: true do
it 'submits via remotipart when a file upload is present', js: true do
visit root_path
page.execute_script("$(document).delegate('form', 'ajax:remotipartSubmit', function(evt, xhr, data) { $('#comments').after('<div class=\"remotipart\">remotipart!</div>'); });")

Expand All @@ -353,10 +356,10 @@
attach_file 'comment_attachment', file_path
click_button 'Create Comment'

expect(page).to have_css("div.remotipart")
expect(page).to have_css('div.remotipart')
end

it "does not submit via remotipart when a file upload is not present", js: true do
it 'does not submit via remotipart when a file upload is not present', js: true do
visit root_path
page.execute_script("$(document).delegate('form', 'ajax:remotipartSubmit', function(evt, xhr, data) { $('#comments').after('<div class=\"remotipart\">remotipart!</div>'); });")

Expand All @@ -367,11 +370,11 @@
fill_in 'comment_body', with: 'there'
click_button 'Create Comment'

expect(page).not_to have_css("div.remotipart")
expect(page).to have_no_css('div.remotipart')
end

it "Disables submit button while submitting with remotipart", js: true do
skip "actually it works"
it 'Disables submit button while submitting with remotipart', js: true do
skip 'actually it works'

visit root_path

Expand All @@ -392,18 +395,18 @@
attach_file 'comment_attachment', file_path
click_button 'Create Comment'

expect(page.evaluate_script("window.commitButtonDisabled")).to be true
expect(page.evaluate_script("window.commitButtonValue")).to eq "Submitting..."
expect(page.evaluate_script('window.commitButtonDisabled')).to be true
expect(page.evaluate_script('window.commitButtonValue')).to eq 'Submitting...'

expect(button[:disabled]).to be false
expect(button.value).to eq "Create Comment"
expect(button.value).to eq 'Create Comment'
end

it "submits the clicked button with the form like non-file remote form", js: true do
it 'submits the clicked button with the form like non-file remote form', js: true do
visit root_path
click_link 'New Comment with Attachment'

form = find('form')
find('form')
page.execute_script("$('form').bind('ajax:remotipartSubmit', function(e, xhr, settings) { $('#comments').after('<div class=\"params\">' + $.param(settings.data) + '</div>'); });")

file_path = File.join(fixture_path, 'qr.jpg')
Expand All @@ -416,8 +419,8 @@
end

it "doesn't allow XSS via script injection for text responses", js: true do
visit "/say?message=%3C/textarea%3E%3Csvg/onload=alert(domain)%3E&remotipart_submitted=x"
expect(page).to have_selector("textarea")
expect(find("textarea").value).to eq('</textarea><svg/onload=alert(domain)>')
visit '/say?message=%3C/textarea%3E%3Csvg/onload=alert(domain)%3E&remotipart_submitted=x'
expect(page).to have_css('textarea')
expect(find('textarea').value).to eq('</textarea><svg/onload=alert(domain)>')
end
end
Loading

0 comments on commit 0e6c71f

Please sign in to comment.