|
1 | 1 | RSpec.describe "ConversationsController" do |
2 | 2 | delegate :helpers, to: ConversationsController |
| 3 | + let(:signon_user) { create(:signon_user) } |
| 4 | + |
| 5 | + before { login_as(signon_user) } |
3 | 6 |
|
4 | 7 | it_behaves_like "handles a request for a user who hasn't completed onboarding", |
5 | 8 | routes: { show_conversation_path: %i[get], update_conversation_path: %i[post] } |
|
80 | 83 | end |
81 | 84 |
|
82 | 85 | context "when the conversation is active" do |
83 | | - let(:conversation) { create(:conversation, :not_expired) } |
| 86 | + let(:conversation) { create(:conversation, :not_expired, signon_user:) } |
84 | 87 |
|
85 | 88 | before do |
86 | 89 | cookies[:conversation_id] = conversation.id |
|
103 | 106 | end |
104 | 107 |
|
105 | 108 | context "and there is a question without an answer" do |
106 | | - let(:conversation) { create(:conversation) } |
| 109 | + let(:conversation) { create(:conversation, signon_user:) } |
107 | 110 |
|
108 | 111 | it "renders the question and pending answer url correctly" do |
109 | 112 | question = create(:question, conversation:) |
|
117 | 120 | end |
118 | 121 |
|
119 | 122 | 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:) } |
121 | 124 |
|
122 | 125 | it "renders the answer and an answer feedback form" do |
123 | 126 | question = create(:question, :with_answer, conversation:) |
|
135 | 138 | end |
136 | 139 |
|
137 | 140 | 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:) } |
139 | 142 |
|
140 | 143 | it "doesn't render a feedback form" do |
141 | 144 | question = create(:question, :with_answer, conversation:) |
|
150 | 153 | end |
151 | 154 |
|
152 | 155 | 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:) } |
154 | 157 |
|
155 | 158 | it "renders the sources correctly for answers with the success status" do |
156 | 159 | question = create(:question, conversation:) |
|
193 | 196 | end |
194 | 197 |
|
195 | 198 | 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:) } |
197 | 200 |
|
198 | 201 | it "only renders the max number of question from rails config" do |
199 | 202 | allow(Rails.configuration.conversations).to receive(:max_question_count).and_return(1) |
|
209 | 212 |
|
210 | 213 | context "and the response format is JSON" do |
211 | 214 | before do |
212 | | - conversation = create(:conversation, :not_expired) |
| 215 | + conversation = create(:conversation, :not_expired, signon_user:) |
213 | 216 | cookies[:conversation_id] = conversation.id |
214 | 217 | end |
215 | 218 |
|
|
233 | 236 |
|
234 | 237 | describe "POST :update" do |
235 | 238 | include_context "with onboarding completed" |
236 | | - let(:conversation) { create(:conversation, :not_expired) } |
237 | 239 |
|
238 | 240 | it "sets the converation_id cookie with valid params" do |
239 | 241 | freeze_time do |
|
253 | 255 | .to have_selector("h1", text: "GOV.UK Chat is generating an answer") |
254 | 256 | end |
255 | 257 |
|
| 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 | + |
256 | 265 | context "and the params are invalid while the last question is answered" do |
257 | 266 | it "renders the conversation with an error" do |
258 | 267 | post update_conversation_path, params: { create_question: { user_question: "" } } |
|
267 | 276 | end |
268 | 277 |
|
269 | 278 | 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)]) } |
271 | 280 |
|
272 | 281 | before do |
273 | 282 | cookies[:conversation_id] = conversation.id |
|
284 | 293 | end |
285 | 294 |
|
286 | 295 | context "and the converation_id cookie is present" do |
| 296 | + let(:conversation) { create(:conversation, :not_expired, signon_user:) } |
| 297 | + |
287 | 298 | before do |
288 | 299 | cookies[:conversation_id] = conversation.id |
289 | 300 | end |
|
304 | 315 | end |
305 | 316 |
|
306 | 317 | context "when the request format is JSON" do |
| 318 | + let(:conversation) { create(:conversation, :not_expired, signon_user:) } |
| 319 | + |
307 | 320 | before do |
308 | 321 | cookies[:conversation_id] = conversation.id |
309 | 322 | end |
|
340 | 353 |
|
341 | 354 | describe "GET :answer" do |
342 | 355 | include_context "with onboarding completed" |
343 | | - let(:conversation) { create(:conversation) } |
| 356 | + let(:conversation) { create(:conversation, signon_user:) } |
344 | 357 |
|
345 | 358 | before do |
346 | 359 | cookies[:conversation_id] = conversation.id |
|
444 | 457 | describe "POST :answer_feedback" do |
445 | 458 | include_context "with onboarding completed" |
446 | 459 |
|
447 | | - let(:conversation) { create(:conversation) } |
| 460 | + let(:conversation) { create(:conversation, signon_user:) } |
448 | 461 | let(:question) { create(:question, conversation:) } |
449 | 462 |
|
450 | 463 | before do |
|
530 | 543 | include_context "with onboarding completed" |
531 | 544 |
|
532 | 545 | context "when the conversation is active" do |
533 | | - let(:conversation) { create(:conversation, :not_expired) } |
| 546 | + let(:conversation) { create(:conversation, :not_expired, signon_user:) } |
534 | 547 |
|
535 | 548 | before do |
536 | 549 | cookies[:conversation_id] = conversation.id |
|
550 | 563 | include_context "with onboarding completed" |
551 | 564 |
|
552 | 565 | context "when the conversation is active" do |
553 | | - let(:conversation) { create(:conversation, :not_expired) } |
| 566 | + let(:conversation) { create(:conversation, :not_expired, signon_user:) } |
554 | 567 |
|
555 | 568 | before do |
556 | 569 | cookies[:conversation_id] = conversation.id |
|
0 commit comments