|
6 | 6 | login_as(create(:signon_user, :conversation_api)) |
7 | 7 | end |
8 | 8 |
|
9 | | - shared_examples "responds with forbidden if user doesn't have conversation-api permission" do |path, method| |
10 | | - let(:route_params) { {} } |
11 | | - let(:params) { {} } |
12 | | - |
13 | | - describe "responds with forbidden if user doesn't have conversation-api permission" do |
14 | | - it "returns with 403 for #{path}" do |
15 | | - login_as(create(:signon_user)) |
16 | | - process(method.to_sym, public_send(path.to_sym, *route_params), params:, as: :json) |
| 9 | + shared_examples "responds with forbidden if user doesn't have conversation-api permission" do |routes:| |
| 10 | + before { login_as(create(:signon_user)) } |
| 11 | + |
| 12 | + routes.each do |route| |
| 13 | + describe "responds with forbidden if user doesn't have conversation-api permission" do |
| 14 | + it "returns with 403 for #{route[:method]} #{route[:path]}" do |
| 15 | + process( |
| 16 | + route[:method.to_sym], |
| 17 | + public_send(route[:path].to_sym, *(route[:route_params] || [])), |
| 18 | + params: route[:params] || {}, |
| 19 | + as: :json, |
| 20 | + ) |
17 | 21 |
|
18 | | - expect(response).to have_http_status(:forbidden) |
| 22 | + expect(response).to have_http_status(:forbidden) |
| 23 | + end |
19 | 24 | end |
20 | 25 | end |
21 | 26 | end |
22 | 27 |
|
23 | 28 | it_behaves_like "responds with forbidden if user doesn't have conversation-api permission", |
24 | | - :api_v0_create_conversation_path, |
25 | | - :post do |
26 | | - let(:route_params) { [] } |
27 | | - let(:params) { { user_question: "" } } |
28 | | - end |
29 | | - |
30 | | - it_behaves_like "responds with forbidden if user doesn't have conversation-api permission", |
31 | | - :api_v0_show_conversation_path, |
32 | | - :get do |
33 | | - let(:route_params) { [SecureRandom.uuid] } |
34 | | - end |
35 | | - |
36 | | - it_behaves_like "responds with forbidden if user doesn't have conversation-api permission", |
37 | | - :api_v0_answer_question_path, |
38 | | - :get do |
39 | | - let(:route_params) { [SecureRandom.uuid, SecureRandom.uuid] } |
40 | | - end |
41 | | - |
42 | | - it_behaves_like "responds with forbidden if user doesn't have conversation-api permission", |
43 | | - :api_v0_answer_feedback_path, |
44 | | - :post do |
45 | | - let(:route_params) { [SecureRandom.uuid, SecureRandom.uuid] } |
46 | | - let(:params) { { useful: true } } |
47 | | - end |
| 29 | + routes: [ |
| 30 | + { |
| 31 | + path: :api_v0_create_conversation_path, |
| 32 | + method: :post, |
| 33 | + params: { user_question: "question" }, |
| 34 | + }, |
| 35 | + { |
| 36 | + path: :api_v0_show_conversation_path, |
| 37 | + method: :get, |
| 38 | + route_params: [SecureRandom.uuid], |
| 39 | + }, |
| 40 | + { |
| 41 | + path: :api_v0_update_conversation_path, |
| 42 | + method: :put, |
| 43 | + route_params: [SecureRandom.uuid], |
| 44 | + params: { user_question: "question" }, |
| 45 | + }, |
| 46 | + { |
| 47 | + path: :api_v0_answer_question_path, |
| 48 | + method: :get, |
| 49 | + route_params: [SecureRandom.uuid, SecureRandom.uuid], |
| 50 | + }, |
| 51 | + { |
| 52 | + path: :api_v0_answer_feedback_path, |
| 53 | + method: :post, |
| 54 | + route_params: [SecureRandom.uuid, SecureRandom.uuid], |
| 55 | + params: { useful: true }, |
| 56 | + }, |
| 57 | + ] |
48 | 58 |
|
49 | 59 | describe "middleware ensures adherance to the OpenAPI specification" do |
50 | 60 | context "when the response returned does not conform to the OpenAPI specification" do |
|
141 | 151 | end |
142 | 152 | end |
143 | 153 |
|
| 154 | + describe "PUT :update" do |
| 155 | + context "when the params are valid" do |
| 156 | + let(:user_question) { "What is the capital of France?" } |
| 157 | + let(:params) { { user_question: } } |
| 158 | + |
| 159 | + it "returns a created status" do |
| 160 | + put api_v0_update_conversation_path(conversation), params:, as: :json |
| 161 | + expect(response).to have_http_status(:created) |
| 162 | + end |
| 163 | + |
| 164 | + it "creates a question on the conversation" do |
| 165 | + expect { |
| 166 | + put api_v0_update_conversation_path(conversation), params:, as: :json |
| 167 | + }.to change(conversation.questions, :count).by(1) |
| 168 | + expect(conversation.questions.strict_loading(false).last.message) |
| 169 | + .to eq(user_question) |
| 170 | + end |
| 171 | + |
| 172 | + it "returns the expected JSON" do |
| 173 | + put api_v0_update_conversation_path(conversation), params:, as: :json |
| 174 | + |
| 175 | + expected_response = QuestionBlueprint.render_as_json( |
| 176 | + conversation.questions.strict_loading(false).last, |
| 177 | + view: :pending, |
| 178 | + ) |
| 179 | + expect(JSON.parse(response.body)).to eq(expected_response) |
| 180 | + end |
| 181 | + end |
| 182 | + |
| 183 | + context "when the params are invalid" do |
| 184 | + let(:params) { { user_question: "" } } |
| 185 | + |
| 186 | + it "returns an unprocessable_entity status" do |
| 187 | + put api_v0_update_conversation_path(conversation), params:, as: :json |
| 188 | + expect(response).to have_http_status(:unprocessable_entity) |
| 189 | + end |
| 190 | + |
| 191 | + it "returns the correct expected JSON" do |
| 192 | + put api_v0_update_conversation_path(conversation), params:, as: :json |
| 193 | + |
| 194 | + expect(JSON.parse(response.body)) |
| 195 | + .to eq( |
| 196 | + { |
| 197 | + "message" => "Unprocessable entity", |
| 198 | + "errors" => { "user_question" => [Form::CreateQuestion::USER_QUESTION_PRESENCE_ERROR_MESSAGE] }, |
| 199 | + }, |
| 200 | + ) |
| 201 | + end |
| 202 | + end |
| 203 | + end |
| 204 | + |
144 | 205 | describe "GET :answer" do |
145 | 206 | context "when an answer has been generated for the question" do |
146 | 207 | let!(:answer) { create(:answer, question:) } |
|
0 commit comments