Move login_as after creating data for a test

It doesn't really affect the tests, but it helps isolate useless
assignments, and we already did it this way 94% of the time.
This commit is contained in:
Javi Martín
2019-09-23 00:51:37 +02:00
parent 24aa4744f1
commit 5cbd160f11
15 changed files with 64 additions and 84 deletions

View File

@@ -3,9 +3,8 @@ require "rails_helper"
describe "Admin Notifications" do
before do
admin = create(:administrator)
login_as(admin.user)
create(:budget)
login_as(create(:administrator).user)
end
context "Show" do

View File

@@ -3,9 +3,8 @@ require "rails_helper"
describe "Admin newsletter emails" do
before do
admin = create(:administrator)
login_as(admin.user)
create(:budget)
login_as(create(:administrator).user)
end
context "Show" do

View File

@@ -3,13 +3,13 @@ require "rails_helper"
describe "Homepage" do
before do
admin = create(:administrator).user
login_as(admin)
Setting["homepage.widgets.feeds.proposals"] = false
Setting["homepage.widgets.feeds.debates"] = false
Setting["homepage.widgets.feeds.processes"] = false
Setting["feature.user.recommendations"] = false
admin = create(:administrator).user
login_as(admin)
end
let!(:proposals_feed) { create(:widget_feed, kind: "proposals") }

View File

@@ -2,8 +2,8 @@ require "rails_helper"
describe "Admin edit translatable records" do
before do
login_as(create(:administrator).user)
translatable.update(attributes)
login_as(create(:administrator).user)
end
let(:fields) { translatable.translated_attribute_names }

View File

@@ -19,11 +19,10 @@ describe "Polls" do
context "Admin index" do
scenario "Budget polls should not appear in the list" do
login_as(create(:administrator).user)
poll = create(:poll)
budget_poll = create(:poll, :for_budget)
login_as(create(:administrator).user)
visit admin_polls_path
expect(page).to have_content(poll.name)

View File

@@ -786,7 +786,6 @@ describe "Budget Investments" do
scenario "Set votes for investments randomized with a seed" do
voter = create(:user, :level_two)
login_as(voter)
per_page.times { create(:budget_investment, heading: heading) }
@@ -797,6 +796,7 @@ describe "Budget Investments" do
voted_investments << investment
end
login_as(voter)
visit budget_investments_path(budget, heading_id: heading.id)
voted_investments.each do |investment|
@@ -927,13 +927,12 @@ describe "Budget Investments" do
factory = :budget_investment
scenario "Show up to 5 suggestions", :js do
login_as(author)
%w[first second third fourth fifth sixth].each do |ordinal|
create(factory, title: "#{ordinal.titleize} #{factory}, has search term", budget: budget)
end
create(factory, title: "This is the last #{factory}", budget: budget)
login_as(author)
visit new_budget_investment_path(budget)
fill_in "Title", with: "search"
@@ -943,12 +942,11 @@ describe "Budget Investments" do
end
scenario "No found suggestions", :js do
login_as(author)
%w[first second third fourth fifth sixth].each do |ordinal|
create(factory, title: "#{ordinal.titleize} #{factory}, has search term", budget: budget)
end
login_as(author)
visit new_budget_investment_path(budget)
fill_in "Title", with: "item"
@@ -958,12 +956,11 @@ describe "Budget Investments" do
end
scenario "Don't show suggestions from a different budget", :js do
login_as(author)
%w[first second third fourth fifth sixth].each do |ordinal|
create(factory, title: "#{ordinal.titleize} #{factory}, has search term", budget: budget)
end
login_as(author)
visit new_budget_investment_path(other_budget)
fill_in "Title", with: "search"
@@ -1005,11 +1002,11 @@ describe "Budget Investments" do
end
scenario "Show" do
investment = create(:budget_investment, heading: heading)
user = create(:user)
login_as(user)
investment = create(:budget_investment, heading: heading)
visit budget_investment_path(budget, id: investment.id)
expect(page).to have_content(investment.title)
@@ -1146,9 +1143,6 @@ describe "Budget Investments" do
end
scenario "Show (unfeasible budget investment) only when valuation finished" do
user = create(:user)
login_as(user)
investment = create(:budget_investment,
:unfeasible,
budget: budget,
@@ -1164,6 +1158,9 @@ describe "Budget Investments" do
heading: heading,
unfeasibility_explanation: "The unfeasible explanation")
user = create(:user)
login_as(user)
visit budget_investment_path(budget, id: investment.id)
expect(page).not_to have_content("Unfeasibility explanation")
@@ -1180,9 +1177,6 @@ describe "Budget Investments" do
end
scenario "Show (selected budget investment)" do
user = create(:user)
login_as(user)
investment = create(:budget_investment,
:feasible,
:finished,
@@ -1191,6 +1185,9 @@ describe "Budget Investments" do
group: group,
heading: heading)
user = create(:user)
login_as(user)
visit budget_investment_path(budget, id: investment.id)
expect(page).to have_content("This investment project has been selected for balloting phase")
@@ -1198,8 +1195,6 @@ describe "Budget Investments" do
scenario "Show (winner budget investment) only if budget is finished" do
budget.update(phase: "balloting")
user = create(:user)
login_as(user)
investment = create(:budget_investment,
:feasible,
@@ -1210,6 +1205,9 @@ describe "Budget Investments" do
group: group,
heading: heading)
user = create(:user)
login_as(user)
visit budget_investment_path(budget, id: investment.id)
expect(page).not_to have_content("Winning investment project")
@@ -1223,8 +1221,6 @@ describe "Budget Investments" do
scenario "Show (not selected budget investment)" do
budget.update(phase: "balloting")
user = create(:user)
login_as(user)
investment = create(:budget_investment,
:feasible,
@@ -1233,15 +1229,15 @@ describe "Budget Investments" do
group: group,
heading: heading)
user = create(:user)
login_as(user)
visit budget_investment_path(budget, id: investment.id)
expect(page).to have_content("This investment project has not been selected for balloting phase")
end
scenario "Show title (no message)" do
user = create(:user)
login_as(user)
investment = create(:budget_investment,
:feasible,
:finished,
@@ -1249,6 +1245,9 @@ describe "Budget Investments" do
group: group,
heading: heading)
user = create(:user)
login_as(user)
visit budget_investment_path(budget, id: investment.id)
within("aside") do
@@ -1258,9 +1257,6 @@ describe "Budget Investments" do
end
scenario "Show (unfeasible budget investment with valuation not finished)" do
user = create(:user)
login_as(user)
investment = create(:budget_investment,
:unfeasible,
:unfinished,
@@ -1269,6 +1265,9 @@ describe "Budget Investments" do
heading: heading,
unfeasibility_explanation: "Local government is not competent in this matter")
user = create(:user)
login_as(user)
visit budget_investment_path(budget, id: investment.id)
expect(page).not_to have_content("Unfeasibility explanation")
@@ -1309,11 +1308,10 @@ describe "Budget Investments" do
context "Destroy" do
scenario "Admin cannot destroy budget investments" do
admin = create(:administrator)
user = create(:user, :level_two)
investment = create(:budget_investment, heading: heading, author: user)
login_as(admin.user)
login_as(create(:administrator).user)
visit user_path(user)
within("#budget_investment_#{investment.id}") do

View File

@@ -86,13 +86,12 @@ describe "Votes" do
end
scenario "Disable voting on investments", :js do
manuela = create(:user, verified_at: Time.current)
login_as(manuela)
budget.update(phase: "reviewing")
investment = create(:budget_investment, budget: budget, heading: heading)
manuela = create(:user, verified_at: Time.current)
login_as(manuela)
visit budget_investments_path(budget, heading_id: heading.id)
within("#budget_investment_#{investment.id}") do

View File

@@ -205,9 +205,10 @@ describe "Commenting topics from proposals" do
end
scenario "Create", :js do
login_as(user)
community = proposal.community
topic = create(:topic, community: community)
login_as(user)
visit community_topic_path(community, topic)
fill_in "comment-body-topic_#{topic.id}", with: "Have you thought about...?"
@@ -223,9 +224,10 @@ describe "Commenting topics from proposals" do
end
scenario "Errors on create", :js do
login_as(user)
community = proposal.community
topic = create(:topic, community: community)
login_as(user)
visit community_topic_path(community, topic)
click_button "Publish comment"
@@ -754,9 +756,10 @@ describe "Commenting topics from budget investments" do
end
scenario "Create", :js do
login_as(user)
community = investment.community
topic = create(:topic, community: community)
login_as(user)
visit community_topic_path(community, topic)
fill_in "comment-body-topic_#{topic.id}", with: "Have you thought about...?"
@@ -772,9 +775,10 @@ describe "Commenting topics from budget investments" do
end
scenario "Errors on create", :js do
login_as(user)
community = investment.community
topic = create(:topic, community: community)
login_as(user)
visit community_topic_path(community, topic)
click_button "Publish comment"

View File

@@ -961,7 +961,6 @@ describe "Debates" do
scenario "Reorder by recommendations results maintaing search" do
user = create(:user, recommended_debates: true)
login_as(user)
debate1 = create(:debate, title: "Show you got", cached_votes_total: 10, tag_list: "Sport")
debate2 = create(:debate, title: "Show what you got", cached_votes_total: 1, tag_list: "Sport")
@@ -970,6 +969,7 @@ describe "Debates" do
proposal1 = create(:proposal, tag_list: "Sport")
create(:follow, followable: proposal1, user: user)
login_as(user)
visit debates_path
fill_in "search", with: "Show you got"
click_button "Search"
@@ -1088,9 +1088,6 @@ describe "Debates" do
context "Suggesting debates" do
scenario "Shows up to 5 suggestions", :js do
author = create(:user)
login_as(author)
debate1 = create(:debate, title: "First debate has 1 vote", cached_votes_up: 1)
debate2 = create(:debate, title: "Second debate has 2 votes", cached_votes_up: 2)
debate3 = create(:debate, title: "Third debate has 3 votes", cached_votes_up: 3)
@@ -1099,6 +1096,7 @@ describe "Debates" do
debate6 = create(:debate, title: "Sixth debate has 6 votes", description: "This is the sixth debate", cached_votes_up: 6)
debate7 = create(:debate, title: "This has seven votes, and is not suggest", description: "This is the seven", cached_votes_up: 7)
login_as(create(:user))
visit new_debate_path
fill_in "Debate title", with: "debate"
check "debate_terms_of_service"
@@ -1109,12 +1107,10 @@ describe "Debates" do
end
scenario "No found suggestions", :js do
author = create(:user)
login_as(author)
debate1 = create(:debate, title: "First debate has 10 vote", cached_votes_up: 10)
debate2 = create(:debate, title: "Second debate has 2 votes", cached_votes_up: 2)
login_as(create(:user))
visit new_debate_path
fill_in "Debate title", with: "proposal"
check "debate_terms_of_service"
@@ -1126,11 +1122,9 @@ describe "Debates" do
end
scenario "Mark/Unmark a debate as featured" do
admin = create(:administrator)
login_as(admin.user)
debate = create(:debate)
login_as(create(:administrator).user)
visit debates_path
within("#debates") do
expect(page).not_to have_content "Featured"
@@ -1159,12 +1153,10 @@ describe "Debates" do
end
scenario "Index include featured debates" do
admin = create(:administrator)
login_as(admin.user)
debate1 = create(:debate, featured_at: Time.current)
debate2 = create(:debate)
login_as(create(:administrator).user)
visit debates_path
within("#debates") do
expect(page).to have_content("Featured")
@@ -1172,12 +1164,10 @@ describe "Debates" do
end
scenario "Index do not show featured debates if none is marked as featured" do
admin = create(:administrator)
login_as(admin.user)
debate1 = create(:debate)
debate2 = create(:debate)
login_as(create(:administrator).user)
visit debates_path
within("#debates") do
expect(page).not_to have_content("Featured")

View File

@@ -7,10 +7,10 @@ describe "Voters" do
let(:officer) { create(:poll_officer) }
before do
login_as(officer.user)
create(:geozone, :in_census)
create(:poll_shift, officer: officer, booth: booth, date: Date.current, task: :vote_collection)
create(:poll_officer_assignment, officer: officer, poll: poll, booth: booth)
login_as(officer.user)
set_officing_booth(booth)
end

View File

@@ -1481,7 +1481,6 @@ describe "Proposals" do
scenario "Reorder by recommendations results maintaing search" do
user = create(:user, recommended_proposals: true)
login_as(user)
proposal1 = create(:proposal, title: "Show you got", cached_votes_up: 10, tag_list: "Sport")
proposal2 = create(:proposal, title: "Show what you got", cached_votes_up: 1, tag_list: "Sport")
@@ -1490,6 +1489,7 @@ describe "Proposals" do
proposal5 = create(:proposal, tag_list: "Sport")
create(:follow, followable: proposal5, user: user)
login_as(user)
visit proposals_path
fill_in "search", with: "Show you got"
click_button "Search"
@@ -1726,9 +1726,6 @@ describe "Proposals" do
context "Suggesting proposals" do
scenario "Show up to 5 suggestions", :js do
author = create(:user)
login_as(author)
create(:proposal, title: "First proposal, has search term")
create(:proposal, title: "Second title")
create(:proposal, title: "Third proposal, has search term")
@@ -1737,6 +1734,7 @@ describe "Proposals" do
create(:proposal, title: "Sixth proposal, has search term")
create(:proposal, title: "Seventh proposal, has search term")
login_as(create(:user))
visit new_proposal_path
fill_in "Proposal title", with: "search"
check "proposal_terms_of_service"
@@ -1747,12 +1745,10 @@ describe "Proposals" do
end
scenario "No found suggestions", :js do
author = create(:user)
login_as(author)
create(:proposal, title: "First proposal").update_column(:confidence_score, 10)
create(:proposal, title: "Second proposal").update_column(:confidence_score, 8)
login_as(create(:user))
visit new_proposal_path
fill_in "Proposal title", with: "debate"
check "proposal_terms_of_service"

View File

@@ -101,11 +101,10 @@ describe "Tags" do
end
scenario "Turbolinks sanity check from budget's show", :js do
login_as(author)
education = create(:tag, name: "Education", kind: "category")
health = create(:tag, name: "Health", kind: "category")
login_as(author)
visit budget_path(budget)
click_link "Create a budget investment"
@@ -126,11 +125,10 @@ describe "Tags" do
end
scenario "Turbolinks sanity check from budget heading's show", :js do
login_as(author)
education = create(:tag, name: "Education", kind: "category")
health = create(:tag, name: "Health", kind: "category")
login_as(author)
visit budget_investments_path(budget, heading_id: heading.id)
click_link "Create a budget investment"

View File

@@ -83,12 +83,10 @@ describe "Tags" do
end
scenario "Category with category tags", :js do
user = create(:user)
login_as(user)
education = create(:tag, :category, name: "Education")
health = create(:tag, :category, name: "Health")
login_as(create(:user))
visit new_proposal_path
fill_in "Proposal title", with: "Help refugees"

View File

@@ -253,8 +253,8 @@ shared_examples "nested documentable" do |login_as_name, documentable_factory_na
if path.include? "edit"
scenario "Should show persisted documents and remove nested_field" do
login_as user_to_login
create(:document, documentable: documentable)
login_as user_to_login
visit send(path, arguments)
expect(page).to have_css ".document", count: 1
@@ -262,16 +262,16 @@ shared_examples "nested documentable" do |login_as_name, documentable_factory_na
scenario "Should not show add document button when
documentable has reached maximum of documents allowed", :js do
login_as user_to_login
create_list(:document, documentable.class.max_documents_allowed, documentable: documentable)
login_as user_to_login
visit send(path, arguments)
expect(page).to have_css "#new_document_link", visible: false
end
scenario "Should show add document button after destroy one document", :js do
login_as user_to_login
create_list(:document, documentable.class.max_documents_allowed, documentable: documentable)
login_as user_to_login
visit send(path, arguments)
last_document = all("#nested-documents .document").last
within last_document do
@@ -282,8 +282,8 @@ shared_examples "nested documentable" do |login_as_name, documentable_factory_na
end
scenario "Should remove nested field after remove document", :js do
login_as user_to_login
create(:document, documentable: documentable)
login_as user_to_login
visit send(path, arguments)
click_on "Remove document"

View File

@@ -216,24 +216,24 @@ shared_examples "nested imageable" do |imageable_factory_name, path, imageable_p
if path.include? "edit"
scenario "Should show persisted image" do
login_as user
create(:image, imageable: imageable)
login_as user
visit send(path, arguments)
expect(page).to have_css ".image", count: 1
end
scenario "Should not show add image button when image already exists", :js do
login_as user
create(:image, imageable: imageable)
login_as user
visit send(path, arguments)
expect(page).to have_css "a#new_image_link", visible: false
end
scenario "Should remove nested field after remove image", :js do
login_as user
create(:image, imageable: imageable)
login_as user
visit send(path, arguments)
click_on "Remove image"
@@ -241,8 +241,8 @@ shared_examples "nested imageable" do |imageable_factory_name, path, imageable_p
end
scenario "Should show add image button after remove image", :js do
login_as user
create(:image, imageable: imageable)
login_as user
visit send(path, arguments)
click_on "Remove image"