Skip to content

Commit 9249048

Browse files
committed
new tests offers visibility
1 parent 40b6fc4 commit 9249048

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

spec/controllers/offers_controller_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,32 @@
5252
expect(assigns(:offers)).to eq([other_offer])
5353
end
5454
end
55+
56+
context "when filtering by organization" do
57+
let(:organization1) { Organization.find_by(name: "Banco de Tiempo Local") }
58+
let(:organization2) { Organization.find_by(name: "El otro Banco de Tiempo :)") }
59+
let(:user1) { User.find_by(email: "user@timeoverflow.org") }
60+
let(:user2) { User.find_by(email: "user2@timeoverflow.org") }
61+
let!(:offer1) { Offer.find_by(title: "Ruby on Rails nivel principiante") ||
62+
Fabricate(:offer, user: user1, title: "Ruby on Rails nivel principiante") }
63+
let!(:offer2) { Offer.find_by(title: "Cocina low cost") ||
64+
Fabricate(:offer, user: user2, title: "Cocina low cost") }
65+
66+
before { login(user1) }
67+
68+
it 'displays only offers from the selected organization' do
69+
get :index, params: { organization_id: organization1.id }
70+
71+
expect(assigns(:offers)).to include(offer1)
72+
expect(assigns(:offers)).not_to include(offer2)
73+
end
74+
75+
it 'displays all offers when no organization is selected' do
76+
get :index
77+
78+
expect(assigns(:offers)).to include(offer1, offer2)
79+
end
80+
end
5581
end
5682

5783
context "with another organization" do
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
require 'spec_helper'
2+
3+
RSpec.feature 'Offers organization filtering' do
4+
let(:organization) { Fabricate(:organization) }
5+
let(:other_organization) { Fabricate(:organization) }
6+
let(:category) { Fabricate(:category) }
7+
let(:member) { Fabricate(:member, organization: organization) }
8+
let(:other_member) { Fabricate(:member, organization: other_organization) }
9+
let(:user) { member.user }
10+
11+
before do
12+
user.terms_accepted_at = Time.current
13+
user.save!
14+
15+
# Create an accepted alliance
16+
OrganizationAlliance.create!(
17+
source_organization: organization,
18+
target_organization: other_organization,
19+
status: "accepted"
20+
)
21+
22+
# Create posts in both organizations
23+
Fabricate(:offer,
24+
user: user,
25+
organization: organization,
26+
category: category,
27+
title: "Local offer",
28+
active: true)
29+
30+
Fabricate(:offer,
31+
user: other_member.user,
32+
organization: other_organization,
33+
category: category,
34+
title: "Allied offer",
35+
active: true)
36+
37+
# Log in as user
38+
sign_in_with(user.email, user.password)
39+
end
40+
41+
scenario 'User filters posts by allied organization' do
42+
visit offers_path
43+
44+
# Should see posts from both organizations by default
45+
expect(page).to have_content("Local offer")
46+
expect(page).to have_content("Allied offer")
47+
48+
# Click on the organization dropdown toggle
49+
find('a.dropdown-toggle', text: Organization.model_name.human(count: :other)).click
50+
51+
# Find the organization in the dropdown menu and click it directly by url
52+
query_params = { org: other_organization.id }
53+
link_path = "#{offers_path}?#{query_params.to_query}"
54+
visit link_path
55+
56+
# Should see only posts from selected organization
57+
expect(page).to have_content("Allied offer")
58+
expect(page).not_to have_content("Local offer")
59+
end
60+
end

spec/models/post_spec.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,29 @@
66
it { is_expected.to have_many(:movements) }
77
it { is_expected.to have_many(:events) }
88
end
9+
10+
describe '.by_organizations' do
11+
let(:organization) { Fabricate(:organization) }
12+
let(:other_organization) { Fabricate(:organization) }
13+
let(:member) { Fabricate(:member, organization: organization) }
14+
let(:other_member) { Fabricate(:member, organization: other_organization) }
15+
let(:category) { Fabricate(:category) }
16+
let!(:post1) { Fabricate(:offer, user: member.user, organization: organization, category: category) }
17+
let!(:post2) { Fabricate(:offer, user: other_member.user, organization: other_organization, category: category) }
18+
19+
it 'returns posts from the specified organizations' do
20+
expect(Post.by_organizations([organization.id])).to include(post1)
21+
expect(Post.by_organizations([organization.id])).not_to include(post2)
22+
23+
expect(Post.by_organizations([other_organization.id])).to include(post2)
24+
expect(Post.by_organizations([other_organization.id])).not_to include(post1)
25+
26+
expect(Post.by_organizations([organization.id, other_organization.id])).to include(post1, post2)
27+
end
28+
29+
it 'returns all posts if no organization ids are provided' do
30+
expect(Post.by_organizations(nil)).to include(post1, post2)
31+
expect(Post.by_organizations([])).to include(post1, post2)
32+
end
33+
end
934
end

0 commit comments

Comments
 (0)