|
2 | 2 |
|
3 | 3 | require 'rails_helper' |
4 | 4 |
|
5 | | -RSpec.describe 'BetterTogether::Conversations' do |
| 5 | +RSpec.describe 'BetterTogether::Conversations', :as_user do |
6 | 6 | include RequestSpecHelper |
7 | 7 |
|
8 | | - before do |
9 | | - configure_host_platform |
10 | | - end |
11 | | - |
12 | 8 | let!(:manager_user) do |
13 | 9 | create(:user, :confirmed, :platform_manager, email: '[email protected]', password: 'password12345') |
14 | 10 | end |
|
18 | 14 | let!(:non_opted_person) { create(:better_together_person, name: 'Non Opted User') } |
19 | 15 |
|
20 | 16 | describe 'GET /conversations/new' do |
21 | | - context 'as a regular member' do |
22 | | - let!(:regular_user) { create(:user, :confirmed, email: '[email protected]', password: 'password12345') } |
23 | | - |
24 | | - before do |
25 | | - login(regular_user.email, 'password12345') |
26 | | - end |
| 17 | + context 'as a regular member', :as_user do |
27 | 18 |
|
28 | 19 | it 'lists platform managers and opted-in members, but excludes non-opted members' do |
29 | 20 | get better_together.new_conversation_path(locale: I18n.default_locale) |
|
36 | 27 | end |
37 | 28 | end |
38 | 29 |
|
39 | | - context 'as a platform manager' do |
40 | | - before do |
41 | | - login(manager_user.email, 'password12345') |
42 | | - end |
| 30 | + context 'as a platform manager', :as_platform_manager do |
43 | 31 |
|
44 | 32 | it 'lists all people as available participants' do |
45 | 33 | get better_together.new_conversation_path(locale: I18n.default_locale) |
|
52 | 40 | end |
53 | 41 |
|
54 | 42 | describe 'POST /conversations' do |
55 | | - context 'as a regular member' do |
56 | | - let!(:regular_user) { create(:user, :confirmed, email: '[email protected]', password: 'password12345') } |
57 | | - |
58 | | - before { login(regular_user.email, 'password12345') } |
| 43 | + context 'as a regular member', :as_user do |
59 | 44 |
|
60 | 45 | it 'creates conversation with permitted participants (opted-in) and excludes non-permitted' do |
61 | 46 | post better_together.conversations_path(locale: I18n.default_locale), params: { |
|
65 | 50 | } |
66 | 51 | } |
67 | 52 | expect(response).to have_http_status(:found) |
| 53 | + # Do not follow redirect here; just check DB state |
68 | 54 | convo = BetterTogether::Conversation.order(created_at: :desc).first |
69 | | - expect(convo.creator).to eq(regular_user.person) |
| 55 | + user = BetterTogether:: User.find_by(email: '[email protected]') |
| 56 | + expect(convo.creator).to eq(user.person) |
70 | 57 | ids = convo.participants.pluck(:id) |
71 | | - expect(ids).to include(regular_user.person.id) # creator always added |
| 58 | + expect(ids).to include(user.person.id) # creator always added |
72 | 59 | expect(ids).to include(opted_in_person.id) # allowed |
73 | 60 | expect(ids).not_to include(non_opted_person.id) # filtered out |
74 | 61 | end |
|
89 | 76 | end |
90 | 77 |
|
91 | 78 | describe 'PATCH /conversations/:id' do |
92 | | - context 'as a regular member' do |
93 | | - let!(:regular_user) { create(:user, :confirmed, email: '[email protected]', password: 'password12345') } |
| 79 | + context 'as a regular member', :as_user do |
94 | 80 | let!(:conversation) do |
95 | | - create('better_together/conversation', creator: regular_user.person).tap do |c| |
96 | | - c.participants << regular_user.person unless c.participants.exists?(regular_user.person.id) |
| 81 | + # Ensure the conversation reflects policy by using the logged-in user's person |
| 82 | + user = BetterTogether:: User.find_by(email: '[email protected]') |
| 83 | + create('better_together/conversation', creator: user.person).tap do |c| |
| 84 | + c.participants << user.person unless c.participants.exists?(user.person.id) |
97 | 85 | end |
98 | 86 | end |
99 | 87 |
|
100 | | - before { login(regular_user.email, 'password12345') } |
101 | | - |
102 | 88 | it 'does not add non-permitted participants on update' do |
| 89 | + user = BetterTogether:: User.find_by(email: '[email protected]') |
103 | 90 | patch better_together.conversation_path(conversation, locale: I18n.default_locale), params: { |
104 | 91 | conversation: { |
105 | 92 | title: conversation.title, |
106 | | - participant_ids: [regular_user.person.id, non_opted_person.id] |
| 93 | + participant_ids: [user.person.id, non_opted_person.id] |
107 | 94 | } |
108 | 95 | } |
109 | 96 | expect(response).to have_http_status(:found) |
110 | 97 | conversation.reload |
111 | 98 | ids = conversation.participants.pluck(:id) |
112 | | - expect(ids).to include(regular_user.person.id) |
| 99 | + expect(ids).to include(user.person.id) |
113 | 100 | expect(ids).not_to include(non_opted_person.id) |
114 | 101 | end |
115 | 102 |
|
|
0 commit comments