Skip to content

Commit b08e275

Browse files
louispt1noracato
authored andcommitted
couple_existing_user relies on user_email not id, updated spec to test scenario user coupling more extensively
1 parent cee2940 commit b08e275

File tree

3 files changed

+72
-6
lines changed

3 files changed

+72
-6
lines changed

app/models/scenario_user.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class ScenarioUser < ApplicationRecord
1616
def couple_existing_user
1717
return unless user_email.present? && user_id.blank?
1818

19-
couple_to(User.find_by(id: user_id))
19+
couple_to(User.find_by(user_email: user_email))
2020
end
2121

2222
# Couples the record to an existing User.

spec/models/scenario_user_spec.rb

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,60 @@
4343
end
4444
end
4545

46+
describe '#couple_existing_user' do
47+
context 'when a user with the email exists in the system' do
48+
let(:existing_user) { create(:user, user_email: '[email protected]') }
49+
let(:scenario_user) do
50+
build(:scenario_user, scenario: scenario, user_email: '[email protected]', user_id: nil)
51+
end
52+
53+
before { existing_user }
54+
55+
it 'couples the scenario_user to the existing user on create' do
56+
scenario_user.save!
57+
expect(scenario_user.user_id).to eq(existing_user.id)
58+
end
59+
60+
it 'removes the user_email after coupling' do
61+
scenario_user.save!
62+
expect(scenario_user.user_email).to be_nil
63+
end
64+
end
65+
66+
context 'when no user with the email exists' do
67+
let(:scenario_user) do
68+
build(:scenario_user, scenario: scenario, user_email: '[email protected]', user_id: nil)
69+
end
70+
71+
it 'keeps the user_email' do
72+
scenario_user.save!
73+
expect(scenario_user.user_email).to eq('[email protected]')
74+
end
75+
76+
it 'does not set a user_id' do
77+
scenario_user.save!
78+
expect(scenario_user.user_id).to be_nil
79+
end
80+
end
81+
82+
context 'when user_id is already set' do
83+
let(:user) { create(:user) }
84+
let(:scenario_user) do
85+
build(:scenario_user, scenario: scenario, user_id: user.id, user_email: nil)
86+
end
87+
88+
it 'does not change the user_id' do
89+
scenario_user.save!
90+
expect(scenario_user.user_id).to eq(user.id)
91+
end
92+
93+
it 'keeps user_email nil' do
94+
scenario_user.save!
95+
expect(scenario_user.user_email).to be_nil
96+
end
97+
end
98+
end
99+
46100
it 'allows updating the role if not the last scenario owner' do
47101
# The first user added will automatically become the scenario owner
48102
scenario.user = create(:user)

spec/requests/api/v3/scenario_users_controller_spec.rb

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
let(:json) { JSON.parse(response.body) }
4343

4444
context 'with proper params' do
45-
let(:user_viewer) { create(:user, email: '[email protected]') }
45+
let(:user_viewer) { create(:user, user_email: '[email protected]') }
4646

4747
before do
4848
user_viewer
@@ -64,14 +64,26 @@
6464

6565
it 'adds the given users to the scenario' do
6666
expect(json).to contain_exactly(
67-
a_hash_including('user_id' => nil, 'user_email' => '[email protected]',
67+
a_hash_including('user_id' => user_viewer.id, 'user_email' => nil,
6868
'role' => 'scenario_viewer', 'role_id' => 1, 'scenario_id' => scenario.id),
6969
a_hash_including('user_id' => nil, 'user_email' => '[email protected]',
7070
'role' => 'scenario_collaborator', 'role_id' => 2, 'scenario_id' => scenario.id),
7171
a_hash_including('user_id' => nil, 'user_email' => '[email protected]',
7272
'role' => 'scenario_owner', 'role_id' => 3, 'scenario_id' => scenario.id)
7373
)
7474
end
75+
76+
it 'couples the existing user automatically' do
77+
viewer_scenario_user = scenario.scenario_users.find_by(user_id: user_viewer.id)
78+
expect(viewer_scenario_user).to be_present
79+
expect(viewer_scenario_user.user_email).to be_nil
80+
end
81+
82+
it 'keeps pending users as email-only' do
83+
collaborator = scenario.scenario_users.find_by(user_email: '[email protected]')
84+
expect(collaborator).to be_present
85+
expect(collaborator.user_id).to be_nil
86+
end
7587
end
7688

7789
context 'with missing role information' do
@@ -132,7 +144,7 @@
132144
end
133145

134146
context 'when the user was already present on the scenario' do
135-
let(:user_viewer) { create(:user, email: '[email protected]') }
147+
let(:user_viewer) { create(:user, user_email: '[email protected]') }
136148

137149
before do
138150
create(
@@ -143,7 +155,7 @@
143155
headers: access_token_header(user, :delete),
144156
params: {
145157
scenario_users: [
146-
{ user_id: user_viewer.id, role: 'scenario_viewer' }
158+
{ user_email: '[email protected]', role: 'scenario_viewer' }
147159
]
148160
}
149161
end
@@ -153,7 +165,7 @@
153165
end
154166

155167
it 'returns an error' do
156-
expect(json['errors']['']).to include('base')
168+
expect(json['errors']['[email protected]']).to include('duplicate')
157169
end
158170
end
159171
end

0 commit comments

Comments
 (0)