Having exceptions is better than having silent bugs.
There are a few methods I've kept the same way they were.
The `RelatedContentScore#score_with_opposite` method is a bit peculiar:
it creates scores for both itself and the opposite related content,
which means the opposite related content will try to create the same
scores as well.
We've already got a test to check `Budget::Ballot#add_investment` when
creating a line fails ("Edge case voting a non-elegible investment").
Finally, the method `User#send_oauth_confirmation_instructions` doesn't
update the record when the email address isn't already present, leading
to the test "Try to register with the email of an already existing user,
when an unconfirmed email was provided by oauth" fo fail if we raise an
exception for an invalid user. That's because updating a user's email
doesn't update the database automatically, but instead a confirmation
email is sent.
There are also a few false positives for classes which don't have bang
methods (like the GraphQL classes) or destroying attachments.
For these reasons, I'm adding the rule with a "Refactor" severity,
meaning it's a rule we can break if necessary.
223 lines
8.2 KiB
Ruby
223 lines
8.2 KiB
Ruby
require "rails_helper"
|
|
|
|
describe UserSegments do
|
|
let(:user1) { create(:user) }
|
|
let(:user2) { create(:user) }
|
|
let(:user3) { create(:user) }
|
|
|
|
describe "#all_users" do
|
|
it "returns all active users enabled" do
|
|
active_user = create(:user)
|
|
erased_user = create(:user, erased_at: Time.current)
|
|
|
|
expect(UserSegments.all_users).to eq [active_user]
|
|
expect(UserSegments.all_users).not_to include erased_user
|
|
end
|
|
end
|
|
|
|
describe "#administrators" do
|
|
it "returns all active administrators users" do
|
|
active_user = create(:user)
|
|
active_admin = create(:administrator).user
|
|
erased_user = create(:user, erased_at: Time.current)
|
|
|
|
expect(UserSegments.administrators).to eq [active_admin]
|
|
expect(UserSegments.administrators).not_to include active_user
|
|
expect(UserSegments.administrators).not_to include erased_user
|
|
end
|
|
end
|
|
|
|
describe "#all_proposal_authors" do
|
|
it "returns users that have created a proposal even if is archived or retired" do
|
|
create(:proposal, author: user1)
|
|
create(:proposal, :archived, author: user2)
|
|
create(:proposal, :retired, author: user3)
|
|
|
|
all_proposal_authors = UserSegments.all_proposal_authors
|
|
|
|
expect(all_proposal_authors).to match_array [user1, user2, user3]
|
|
end
|
|
|
|
it "does not return duplicated users" do
|
|
create(:proposal, author: user1)
|
|
create(:proposal, :archived, author: user1)
|
|
create(:proposal, :retired, author: user1)
|
|
|
|
all_proposal_authors = UserSegments.all_proposal_authors
|
|
|
|
expect(all_proposal_authors).to eq [user1]
|
|
end
|
|
end
|
|
|
|
describe "#proposal_authors" do
|
|
it "returns users that have created a proposal" do
|
|
create(:proposal, author: user1)
|
|
|
|
proposal_authors = UserSegments.proposal_authors
|
|
|
|
expect(proposal_authors).to eq [user1]
|
|
end
|
|
|
|
it "does not return duplicated users" do
|
|
create(:proposal, author: user1)
|
|
create(:proposal, author: user1)
|
|
|
|
proposal_authors = UserSegments.proposal_authors
|
|
expect(proposal_authors).to contain_exactly(user1)
|
|
end
|
|
end
|
|
|
|
describe "#investment_authors" do
|
|
it "returns users that have created a budget investment" do
|
|
investment = create(:budget_investment, author: user1)
|
|
budget = create(:budget)
|
|
investment.update!(budget: budget)
|
|
|
|
investment_authors = UserSegments.investment_authors
|
|
|
|
expect(investment_authors).to eq [user1]
|
|
end
|
|
|
|
it "does not return duplicated users" do
|
|
investment1 = create(:budget_investment, author: user1)
|
|
investment2 = create(:budget_investment, author: user1)
|
|
budget = create(:budget)
|
|
investment1.update!(budget: budget)
|
|
investment2.update!(budget: budget)
|
|
|
|
investment_authors = UserSegments.investment_authors
|
|
expect(investment_authors).to contain_exactly(user1)
|
|
end
|
|
end
|
|
|
|
describe "#feasible_and_undecided_investment_authors" do
|
|
it "returns authors of a feasible or an undecided budget investment" do
|
|
user4 = create(:user)
|
|
user5 = create(:user)
|
|
user6 = create(:user)
|
|
|
|
feasible_investment_finished = create(:budget_investment, :feasible, :finished, author: user1)
|
|
undecided_investment_finished = create(:budget_investment, :undecided, :finished, author: user2)
|
|
feasible_investment_unfinished = create(:budget_investment, :feasible, author: user3)
|
|
undecided_investment_unfinished = create(:budget_investment, :undecided, author: user4)
|
|
unfeasible_investment_unfinished = create(:budget_investment, :unfeasible, author: user5)
|
|
unfeasible_investment_finished = create(:budget_investment, :unfeasible, :finished, author: user6)
|
|
|
|
budget = create(:budget)
|
|
feasible_investment_finished.update!(budget: budget)
|
|
undecided_investment_finished.update!(budget: budget)
|
|
feasible_investment_unfinished.update!(budget: budget)
|
|
undecided_investment_unfinished.update!(budget: budget)
|
|
unfeasible_investment_unfinished.update!(budget: budget)
|
|
unfeasible_investment_finished.update!(budget: budget)
|
|
|
|
investment_authors = UserSegments.feasible_and_undecided_investment_authors
|
|
expect(investment_authors).to match_array [user1, user2, user3, user4, user5]
|
|
expect(investment_authors).not_to include user6
|
|
end
|
|
|
|
it "does not return duplicated users" do
|
|
feasible_investment = create(:budget_investment, :feasible, author: user1)
|
|
undecided_investment = create(:budget_investment, :undecided, author: user1)
|
|
budget = create(:budget)
|
|
feasible_investment.update!(budget: budget)
|
|
undecided_investment.update!(budget: budget)
|
|
|
|
investment_authors = UserSegments.feasible_and_undecided_investment_authors
|
|
expect(investment_authors).to contain_exactly(user1)
|
|
end
|
|
end
|
|
|
|
describe "#selected_investment_authors" do
|
|
it "returns authors of selected budget investments" do
|
|
selected_investment = create(:budget_investment, :selected, author: user1)
|
|
unselected_investment = create(:budget_investment, :unselected, author: user2)
|
|
budget = create(:budget)
|
|
selected_investment.update!(budget: budget)
|
|
unselected_investment.update!(budget: budget)
|
|
|
|
investment_authors = UserSegments.selected_investment_authors
|
|
|
|
expect(investment_authors).to eq [user1]
|
|
end
|
|
|
|
it "does not return duplicated users" do
|
|
selected_investment1 = create(:budget_investment, :selected, author: user1)
|
|
selected_investment2 = create(:budget_investment, :selected, author: user1)
|
|
budget = create(:budget)
|
|
selected_investment1.update!(budget: budget)
|
|
selected_investment2.update!(budget: budget)
|
|
|
|
investment_authors = UserSegments.selected_investment_authors
|
|
expect(investment_authors).to contain_exactly(user1)
|
|
end
|
|
end
|
|
|
|
describe "#winner_investment_authors" do
|
|
it "returns authors of winner budget investments" do
|
|
winner_investment = create(:budget_investment, :winner, author: user1)
|
|
selected_investment = create(:budget_investment, :selected, author: user2)
|
|
budget = create(:budget)
|
|
winner_investment.update!(budget: budget)
|
|
selected_investment.update!(budget: budget)
|
|
|
|
investment_authors = UserSegments.winner_investment_authors
|
|
|
|
expect(investment_authors).to eq [user1]
|
|
end
|
|
|
|
it "does not return duplicated users" do
|
|
winner_investment1 = create(:budget_investment, :winner, author: user1)
|
|
winner_investment2 = create(:budget_investment, :winner, author: user1)
|
|
budget = create(:budget)
|
|
winner_investment1.update!(budget: budget)
|
|
winner_investment2.update!(budget: budget)
|
|
|
|
investment_authors = UserSegments.winner_investment_authors
|
|
expect(investment_authors).to contain_exactly(user1)
|
|
end
|
|
end
|
|
|
|
describe "#current_budget_investments" do
|
|
it "only returns investments from the current budget" do
|
|
investment1 = create(:budget_investment, author: create(:user))
|
|
investment2 = create(:budget_investment, author: create(:user))
|
|
budget = create(:budget)
|
|
investment1.update!(budget: budget)
|
|
|
|
current_budget_investments = UserSegments.current_budget_investments
|
|
|
|
expect(current_budget_investments).to eq [investment1]
|
|
expect(current_budget_investments).not_to include investment2
|
|
end
|
|
end
|
|
|
|
describe "#not_supported_on_current_budget" do
|
|
it "only returns users that haven't supported investments on current budget" do
|
|
investment1 = create(:budget_investment)
|
|
investment2 = create(:budget_investment)
|
|
budget = create(:budget)
|
|
investment1.vote_by(voter: user1, vote: "yes")
|
|
investment2.vote_by(voter: user2, vote: "yes")
|
|
investment1.update!(budget: budget)
|
|
investment2.update!(budget: budget)
|
|
|
|
not_supported_on_current_budget = UserSegments.not_supported_on_current_budget
|
|
expect(not_supported_on_current_budget).to include user3
|
|
expect(not_supported_on_current_budget).not_to include user1
|
|
expect(not_supported_on_current_budget).not_to include user2
|
|
end
|
|
end
|
|
|
|
describe "#user_segment_emails" do
|
|
it "returns list of emails sorted by user creation date" do
|
|
create(:user, email: "first@email.com", created_at: 1.day.ago)
|
|
create(:user, email: "last@email.com")
|
|
|
|
emails = UserSegments.user_segment_emails(:all_users)
|
|
expect(emails).to eq ["first@email.com", "last@email.com"]
|
|
end
|
|
end
|
|
|
|
end
|