Skip to content

Commit d0b4572

Browse files
authored
Merge pull request #288 from alphagov/2530-associate-signon-user-with-web-conversation
Associate web conversation with Signon user
2 parents f571432 + 17b30e1 commit d0b4572

8 files changed

Lines changed: 65 additions & 39 deletions

File tree

app/controllers/conversations_controller.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def clear_confirm
3737
end
3838

3939
def update
40-
@conversation ||= Conversation.new
40+
@conversation ||= Conversation.new(signon_user: current_user, source: :web)
4141
@create_question = Form::CreateQuestion.new(user_question_params.merge(conversation: @conversation))
4242

4343
if @create_question.valid?
@@ -111,7 +111,8 @@ def find_conversation
111111
return if cookies[:conversation_id].blank?
112112

113113
@conversation = Conversation.active
114-
.find_by!(id: cookies[:conversation_id], source: :web)
114+
.where(signon_user: current_user, source: :web)
115+
.find_by!(id: cookies[:conversation_id])
115116
set_conversation_cookie(@conversation)
116117
rescue ActiveRecord::RecordNotFound
117118
cookies.delete(:conversation_id)

app/helpers/admin/questions_helper.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ def question_show_summary_list_rows(question, answer, question_number, total_que
6262
].compact
6363

6464
signon_user = conversation.signon_user
65-
if signon_user.present? && conversation.source_api?
65+
if signon_user.present?
6666
rows << {
67-
field: "API user",
67+
field: "Signon user",
6868
value: safe_join([
6969
signon_user.name,
7070
" (",

spec/helpers/admin/questions_helper_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,10 @@
143143

144144
it "returns a row with a link to filter the questions table by the signon user" do
145145
signon_user = create(:signon_user)
146-
conversation.update!(signon_user:, source: :api)
146+
conversation.update!(signon_user:)
147147
result = helper.question_show_summary_list_rows(question, nil, 1, 1)
148148

149-
row = result.find { |r| r[:field] == "API user" }
149+
row = result.find { |r| r[:field] == "Signon user" }
150150
expect(row[:value])
151151
.to include(conversation.signon_user.name)
152152
.and have_link(

spec/requests/conversations_spec.rb

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
RSpec.describe "ConversationsController" do
22
delegate :helpers, to: ConversationsController
3+
let(:signon_user) { create(:signon_user) }
4+
5+
before { login_as(signon_user) }
36

47
it_behaves_like "handles a request for a user who hasn't completed onboarding",
58
routes: { show_conversation_path: %i[get], update_conversation_path: %i[post] }
@@ -80,7 +83,7 @@
8083
end
8184

8285
context "when the conversation is active" do
83-
let(:conversation) { create(:conversation, :not_expired) }
86+
let(:conversation) { create(:conversation, :not_expired, signon_user:) }
8487

8588
before do
8689
cookies[:conversation_id] = conversation.id
@@ -103,7 +106,7 @@
103106
end
104107

105108
context "and there is a question without an answer" do
106-
let(:conversation) { create(:conversation) }
109+
let(:conversation) { create(:conversation, signon_user:) }
107110

108111
it "renders the question and pending answer url correctly" do
109112
question = create(:question, conversation:)
@@ -117,7 +120,7 @@
117120
end
118121

119122
context "and there is a question with an answer that doesn't have feedback" do
120-
let(:conversation) { create(:conversation) }
123+
let(:conversation) { create(:conversation, signon_user:) }
121124

122125
it "renders the answer and an answer feedback form" do
123126
question = create(:question, :with_answer, conversation:)
@@ -135,7 +138,7 @@
135138
end
136139

137140
context "and there is a question with an answer that has feedback" do
138-
let(:conversation) { create(:conversation) }
141+
let(:conversation) { create(:conversation, signon_user:) }
139142

140143
it "doesn't render a feedback form" do
141144
question = create(:question, :with_answer, conversation:)
@@ -150,7 +153,7 @@
150153
end
151154

152155
context "and there is a question with an answer that has sources" do
153-
let(:conversation) { create(:conversation) }
156+
let(:conversation) { create(:conversation, signon_user:) }
154157

155158
it "renders the sources correctly for answers with the success status" do
156159
question = create(:question, conversation:)
@@ -193,7 +196,7 @@
193196
end
194197

195198
context "and there are more questions than the max number of questions" do
196-
let(:conversation) { create(:conversation) }
199+
let(:conversation) { create(:conversation, signon_user:) }
197200

198201
it "only renders the max number of question from rails config" do
199202
allow(Rails.configuration.conversations).to receive(:max_question_count).and_return(1)
@@ -209,7 +212,7 @@
209212

210213
context "and the response format is JSON" do
211214
before do
212-
conversation = create(:conversation, :not_expired)
215+
conversation = create(:conversation, :not_expired, signon_user:)
213216
cookies[:conversation_id] = conversation.id
214217
end
215218

@@ -233,7 +236,6 @@
233236

234237
describe "POST :update" do
235238
include_context "with onboarding completed"
236-
let(:conversation) { create(:conversation, :not_expired) }
237239

238240
it "sets the converation_id cookie with valid params" do
239241
freeze_time do
@@ -253,6 +255,13 @@
253255
.to have_selector("h1", text: "GOV.UK Chat is generating an answer")
254256
end
255257

258+
it "associates the signon user with the conversation" do
259+
post update_conversation_path, params: { create_question: { user_question: "How much tax should I be paying?" } }
260+
261+
conversation = Conversation.includes(:signon_user).last
262+
expect(conversation.signon_user).to eq(signon_user)
263+
end
264+
256265
context "and the params are invalid while the last question is answered" do
257266
it "renders the conversation with an error" do
258267
post update_conversation_path, params: { create_question: { user_question: "" } }
@@ -267,7 +276,7 @@
267276
end
268277

269278
context "and the params are invalid while the last question is not answered" do
270-
let(:conversation) { create(:conversation, questions: [create(:question)]) }
279+
let(:conversation) { create(:conversation, signon_user:, questions: [create(:question)]) }
271280

272281
before do
273282
cookies[:conversation_id] = conversation.id
@@ -284,6 +293,8 @@
284293
end
285294

286295
context "and the converation_id cookie is present" do
296+
let(:conversation) { create(:conversation, :not_expired, signon_user:) }
297+
287298
before do
288299
cookies[:conversation_id] = conversation.id
289300
end
@@ -304,6 +315,8 @@
304315
end
305316

306317
context "when the request format is JSON" do
318+
let(:conversation) { create(:conversation, :not_expired, signon_user:) }
319+
307320
before do
308321
cookies[:conversation_id] = conversation.id
309322
end
@@ -340,7 +353,7 @@
340353

341354
describe "GET :answer" do
342355
include_context "with onboarding completed"
343-
let(:conversation) { create(:conversation) }
356+
let(:conversation) { create(:conversation, signon_user:) }
344357

345358
before do
346359
cookies[:conversation_id] = conversation.id
@@ -444,7 +457,7 @@
444457
describe "POST :answer_feedback" do
445458
include_context "with onboarding completed"
446459

447-
let(:conversation) { create(:conversation) }
460+
let(:conversation) { create(:conversation, signon_user:) }
448461
let(:question) { create(:question, conversation:) }
449462

450463
before do
@@ -530,7 +543,7 @@
530543
include_context "with onboarding completed"
531544

532545
context "when the conversation is active" do
533-
let(:conversation) { create(:conversation, :not_expired) }
546+
let(:conversation) { create(:conversation, :not_expired, signon_user:) }
534547

535548
before do
536549
cookies[:conversation_id] = conversation.id
@@ -550,7 +563,7 @@
550563
include_context "with onboarding completed"
551564

552565
context "when the conversation is active" do
553-
let(:conversation) { create(:conversation, :not_expired) }
566+
let(:conversation) { create(:conversation, :not_expired, signon_user:) }
554567

555568
before do
556569
cookies[:conversation_id] = conversation.id

spec/support/system_spec_helpers.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
module SystemSpecHelpers
2+
def given_i_am_signed_in
3+
@signon_user = create(:signon_user)
4+
login_as(@signon_user)
5+
end
6+
27
def given_i_am_using_the_claude_structured_answer_strategy
38
allow(Rails.configuration)
49
.to receive(:answer_strategy)

spec/system/conversation_js_features_spec.rb

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
RSpec.describe "Conversation JavaScript features", :chunked_content_index, :dismiss_cookie_banner, :js do
22
scenario "questions with answers" do
3-
given_i_have_confirmed_i_understand_chat_risks
3+
given_i_am_signed_in
4+
and_i_have_confirmed_i_understand_chat_risks
45
when_i_enter_a_first_question
56
then_i_see_the_first_question_was_accepted
67

@@ -15,7 +16,8 @@
1516
end
1617

1718
scenario "client side validation" do
18-
given_i_have_confirmed_i_understand_chat_risks
19+
given_i_am_signed_in
20+
and_i_have_confirmed_i_understand_chat_risks
1921
when_i_enter_an_empty_question
2022
then_i_see_a_presence_validation_message
2123

@@ -24,7 +26,8 @@
2426
end
2527

2628
scenario "server side validation" do
27-
given_i_have_confirmed_i_understand_chat_risks
29+
given_i_am_signed_in
30+
and_i_have_confirmed_i_understand_chat_risks
2831
when_i_enter_a_question_with_pii
2932
then_i_see_a_pii_validation_message
3033

@@ -33,7 +36,8 @@
3336
end
3437

3538
scenario "reloading the page while an answer is pending" do
36-
given_i_have_confirmed_i_understand_chat_risks
39+
given_i_am_signed_in
40+
and_i_have_confirmed_i_understand_chat_risks
3741
when_i_enter_a_first_question
3842
then_i_see_the_first_question_was_accepted
3943

@@ -43,7 +47,8 @@
4347
end
4448

4549
scenario "User gives feedback on an answer" do
46-
given_i_have_confirmed_i_understand_chat_risks
50+
given_i_am_signed_in
51+
and_i_have_confirmed_i_understand_chat_risks
4752
when_i_enter_a_first_question
4853
then_i_see_the_first_question_was_accepted
4954

@@ -53,7 +58,8 @@
5358
end
5459

5560
scenario "character limits" do
56-
given_i_have_confirmed_i_understand_chat_risks
61+
given_i_am_signed_in
62+
and_i_have_confirmed_i_understand_chat_risks
5763
when_i_type_in_a_question_approaching_the_character_count_limit
5864
then_i_see_a_character_count_warning
5965

@@ -62,7 +68,8 @@
6268
end
6369

6470
scenario "loading messages" do
65-
given_i_have_confirmed_i_understand_chat_risks
71+
given_i_am_signed_in
72+
and_i_have_confirmed_i_understand_chat_risks
6673
when_i_enter_a_first_question_with_a_slow_response
6774
then_i_see_a_question_loading_message
6875

@@ -75,7 +82,8 @@
7582
end
7683

7784
scenario "showing clear chat link in navigation" do
78-
given_i_have_confirmed_i_understand_chat_risks
85+
given_i_am_signed_in
86+
and_i_have_confirmed_i_understand_chat_risks
7987
then_i_cant_see_the_clear_chat_link
8088

8189
when_i_enter_a_first_question
@@ -84,7 +92,8 @@
8492
end
8593

8694
scenario "print link is added to navigation" do
87-
given_i_have_confirmed_i_understand_chat_risks
95+
given_i_am_signed_in
96+
and_i_have_confirmed_i_understand_chat_risks
8897
then_i_see_a_print_link_in_the_menu
8998
end
9099

@@ -97,7 +106,7 @@ def when_i_enter_a_first_question
97106

98107
def when_i_enter_a_first_question_with_a_slow_response
99108
@first_question = "How do I setup a workplace pension?"
100-
conversation = build(:conversation)
109+
conversation = build(:conversation, signon_user: @signon_user)
101110
prepared_question = create(:question, message: @first_question, conversation:)
102111

103112
allow(Question).to receive(:new).and_return(prepared_question)
@@ -287,10 +296,6 @@ def then_i_see_a_print_link_in_the_menu
287296
expect(page).to have_button("Print or save this chat")
288297
end
289298

290-
def and_i_have_an_active_conversation
291-
create(:conversation)
292-
end
293-
294299
def when_i_visit_the_conversation_page
295300
visit show_conversation_path
296301
end

spec/system/user_clears_chat_spec.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
RSpec.describe "User clears chat" do
22
scenario do
3-
given_i_have_an_active_conversation_with_an_answered_question
3+
given_i_am_signed_in
4+
and_i_have_an_active_conversation_with_an_answered_question
45
and_i_visit_the_conversation_page
56
when_i_click_the_clear_chat_link
67
and_i_cancel
@@ -10,8 +11,8 @@
1011
then_i_cannot_see_my_previous_questions_and_answer
1112
end
1213

13-
def given_i_have_an_active_conversation_with_an_answered_question
14-
@conversation = create(:conversation)
14+
def and_i_have_an_active_conversation_with_an_answered_question
15+
@conversation = create(:conversation, signon_user: @signon_user)
1516
set_rack_cookie(:conversation_id, @conversation.id)
1617
answer = build(:answer, message: "Example answer")
1718
create(:question, answer:, conversation: @conversation, message: "Example question")

spec/system/user_gives_feedback_on_answer_spec.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
RSpec.describe "User gives feedback on an answer" do
22
scenario do
3-
given_i_have_an_active_conversation_with_an_answered_question
3+
given_i_am_signed_in
4+
and_i_have_an_active_conversation_with_an_answered_question
45
when_i_visit_the_conversation_page
56
and_i_click_that_the_answer_was_useful
67
then_i_see_that_my_feedback_was_submitted
78
end
89

9-
def given_i_have_an_active_conversation_with_an_answered_question
10-
@conversation = create(:conversation)
10+
def and_i_have_an_active_conversation_with_an_answered_question
11+
@conversation = create(:conversation, signon_user: @signon_user)
1112
set_rack_cookie(:conversation_id, @conversation.id)
1213
create(:question, :with_answer, conversation: @conversation)
1314
end

0 commit comments

Comments
 (0)