|
19 | 19 | end |
20 | 20 | end |
21 | 21 |
|
| 22 | + shared_examples "OpenAPI-compliant endpoint" do |path, method, field| |
| 23 | + let(:route_params) { {} } |
| 24 | + |
| 25 | + context "when the response returned does not conform to the OpenAPI specification" do |
| 26 | + it "raises an error and returns the invalid params in the error message for #{path}" do |
| 27 | + expect { process(method.to_sym, public_send(path.to_sym, *route_params)) } |
| 28 | + .to raise_error(Committee::InvalidResponse) do |error| |
| 29 | + expect(error.message).to match(/missing required parameters: #{field}/) |
| 30 | + end |
| 31 | + end |
| 32 | + end |
| 33 | + end |
| 34 | + |
| 35 | + |
22 | 36 | describe "GET :answer" do |
23 | 37 | it_behaves_like "responds with forbidden if user doesn't have conversation-api permission", |
24 | 38 | :api_v0_answer_question_path, |
25 | 39 | :get do |
26 | 40 | let(:route_params) { [create(:conversation), create(:question)] } |
27 | 41 | end |
| 42 | + it_behaves_like "OpenAPI-compliant endpoint", |
| 43 | + :api_v0_answer_question_path, |
| 44 | + :get, |
| 45 | + :created_at do |
| 46 | + let(:question) { create(:question, :with_answer, conversation:) } |
| 47 | + let(:route_params) { [conversation, question] } |
| 48 | + |
| 49 | + before do |
| 50 | + eager_loaded_answer = Answer.includes(:sources, :feedback).find(question.answer.id) |
| 51 | + answer_blueprint = AnswerBlueprint.render_as_hash(eager_loaded_answer) |
| 52 | + answer_blueprint.delete(:created_at) |
| 53 | + allow(AnswerBlueprint).to receive(:render).and_return(answer_blueprint) |
| 54 | + end |
| 55 | + end |
28 | 56 |
|
29 | 57 | context "when an answer has been generated for the question" do |
30 | 58 | let!(:answer) { create(:answer, question:) } |
|
63 | 91 | expect(JSON.parse(response.body)).to eq({}) |
64 | 92 | end |
65 | 93 | end |
66 | | - |
67 | | - context "when the response returned does not conform to the OpenAPI specification" do |
68 | | - it "raises an error and returns the invalid params in the error message" do |
69 | | - answer = create(:answer, question:) |
70 | | - eager_loaded_answer = Answer.includes(:sources, :feedback).find(answer.id) |
71 | | - answer_blueprint = AnswerBlueprint.render_as_hash(eager_loaded_answer) |
72 | | - answer_blueprint.delete(:created_at) |
73 | | - allow(AnswerBlueprint).to receive(:render).and_return(answer_blueprint) |
74 | | - |
75 | | - expect { get api_v0_answer_question_path(conversation, question) } |
76 | | - .to raise_error(Committee::InvalidResponse) do |error| |
77 | | - expect(error.message).to match(/missing required parameters: created_at/) |
78 | | - end |
79 | | - end |
80 | | - end |
81 | 94 | end |
82 | 95 |
|
83 | 96 | describe "POST :answer_feedback" do |
|
87 | 100 | let(:route_params) { [create(:conversation), create(:answer)] } |
88 | 101 | end |
89 | 102 |
|
| 103 | + it_behaves_like "OpenAPI-compliant endpoint", |
| 104 | + :api_v0_answer_feedback_path, |
| 105 | + :post, |
| 106 | + :fields do |
| 107 | + let(:answer) { create(:answer) } |
| 108 | + let(:route_params) { [answer.question.conversation, answer] } |
| 109 | + |
| 110 | + before do |
| 111 | + answer = create(:answer, question:) |
| 112 | + error_blueprint = ValidationErrorBlueprint.render_as_hash(message: "Could not save answer feedback") |
| 113 | + error_blueprint.delete(:fields) |
| 114 | + allow(ValidationErrorBlueprint).to receive(:render).and_return(error_blueprint.as_json) |
| 115 | + end |
| 116 | + end |
| 117 | + |
90 | 118 | context "when the params are valid" do |
91 | 119 | context "and the answer has no feedback" do |
92 | 120 | let!(:answer) { create(:answer, question:) } |
|
0 commit comments