|
50 | 50 | expect(JSON.parse(response.body)).to eq({ "message" => "Internal server error" }) |
51 | 51 | end |
52 | 52 | end |
| 53 | + |
| 54 | + context "when the request does not conform to the OpenAPI specification" do |
| 55 | + it "returns a bad request status code" do |
| 56 | + post api_v0_answer_feedback_path(conversation_id: conversation.id, answer_id: question.id), |
| 57 | + params: { useful: "not a boolean" }, |
| 58 | + as: :json |
| 59 | + |
| 60 | + expect(response).to have_http_status(:bad_request) |
| 61 | + end |
| 62 | + |
| 63 | + it "returns the correct JSON in the body" do |
| 64 | + post api_v0_answer_feedback_path(conversation_id: conversation.id, answer_id: question.id), |
| 65 | + params: { useful: "not a boolean" }, |
| 66 | + as: :json |
| 67 | + expect(JSON.parse(response.body)).to match({ "message" => /useful expected boolean, but received String: "not a boolean"/ }) |
| 68 | + end |
| 69 | + end |
53 | 70 | end |
54 | 71 |
|
55 | 72 | describe "GET :show" do |
|
66 | 83 | end |
67 | 84 |
|
68 | 85 | it "returns a 404 if the conversation cannot be found" do |
69 | | - get api_v0_show_conversation_path(-1) |
| 86 | + get api_v0_show_conversation_path(SecureRandom.uuid) |
70 | 87 |
|
71 | 88 | expect(response).to have_http_status(:not_found) |
72 | 89 | end |
|
77 | 94 | let!(:answer) { create(:answer, question:) } |
78 | 95 |
|
79 | 96 | it "returns a success status" do |
80 | | - get api_v0_answer_question_path(conversation, question) |
| 97 | + get api_v0_answer_question_path(conversation, question), as: :json |
81 | 98 | expect(response).to have_http_status(:ok) |
82 | 99 | end |
83 | 100 |
|
84 | 101 | it "returns the expected JSON" do |
85 | | - get api_v0_answer_question_path(conversation, question) |
| 102 | + get api_v0_answer_question_path(conversation, question), as: :json |
86 | 103 |
|
87 | 104 | eager_loaded_answer = Answer.includes(:sources, :feedback).find(answer.id) |
88 | 105 | expected_response = AnswerBlueprint.render_as_json(eager_loaded_answer) |
|
92 | 109 | it "returns the correct JSON for answer sources" do |
93 | 110 | source = create(:answer_source, answer:) |
94 | 111 |
|
95 | | - get api_v0_answer_question_path(conversation, question) |
| 112 | + get api_v0_answer_question_path(conversation, question), as: :json |
96 | 113 |
|
97 | 114 | expect(JSON.parse(response.body)["sources"]) |
98 | 115 | .to eq([{ url: source.url, title: "#{source.title}: #{source.heading}" }.as_json]) |
|
101 | 118 |
|
102 | 119 | context "when an answer has not been generated for the question" do |
103 | 120 | it "returns an accepted status" do |
104 | | - get api_v0_answer_question_path(conversation, question) |
| 121 | + get api_v0_answer_question_path(conversation, question), as: :json |
105 | 122 | expect(response).to have_http_status(:accepted) |
106 | 123 | end |
107 | 124 |
|
108 | 125 | it "returns an empty JSON response" do |
109 | | - get api_v0_answer_question_path(conversation, question) |
| 126 | + get api_v0_answer_question_path(conversation, question), as: :json |
110 | 127 | expect(JSON.parse(response.body)).to eq({}) |
111 | 128 | end |
112 | 129 | end |
113 | 130 | end |
114 | 131 |
|
115 | 132 | describe "POST :answer_feedback" do |
116 | | - context "when the params are valid" do |
117 | | - let!(:answer) { create(:answer, question:) } |
| 133 | + let!(:answer) { create(:answer, question:) } |
118 | 134 |
|
119 | | - context "and the answer has no feedback" do |
120 | | - it "returns a created status" do |
121 | | - post api_v0_answer_feedback_path(conversation, answer), params: { useful: true } |
122 | | - expect(response).to have_http_status(:created) |
123 | | - end |
124 | | - |
125 | | - it "returns an empty JSON" do |
126 | | - post api_v0_answer_feedback_path(conversation, answer), params: { useful: true } |
127 | | - |
128 | | - expect(JSON.parse(response.body)).to eq({}) |
129 | | - end |
130 | | - |
131 | | - it "creates feedback for the answer" do |
132 | | - expect { |
133 | | - post api_v0_answer_feedback_path(conversation, answer), params: { useful: true } |
134 | | - }.to change(AnswerFeedback, :count).by(1) |
135 | | - |
136 | | - answer_feedback = AnswerFeedback.includes(:answer).last |
137 | | - expect(answer_feedback.answer).to eq(answer) |
138 | | - expect(answer_feedback.useful).to be true |
139 | | - end |
| 135 | + context "when the answer has no feedback" do |
| 136 | + it "returns a created status" do |
| 137 | + post api_v0_answer_feedback_path(conversation, answer), params: { useful: true }, as: :json |
| 138 | + expect(response).to have_http_status(:created) |
140 | 139 | end |
141 | 140 |
|
142 | | - context "and an answer already has feedback" do |
143 | | - before do |
144 | | - create(:answer_feedback, answer:) |
145 | | - end |
| 141 | + it "returns an empty JSON" do |
| 142 | + post api_v0_answer_feedback_path(conversation, answer), params: { useful: true }, as: :json |
146 | 143 |
|
147 | | - it "returns an unprocessable_entity status" do |
148 | | - post api_v0_answer_feedback_path(conversation, answer), params: { useful: true }, as: :json |
149 | | - expect(response).to have_http_status(:unprocessable_entity) |
150 | | - end |
| 144 | + expect(JSON.parse(response.body)).to eq({}) |
| 145 | + end |
151 | 146 |
|
152 | | - it "returns the correct expected JSON" do |
| 147 | + it "creates feedback for the answer" do |
| 148 | + expect { |
153 | 149 | post api_v0_answer_feedback_path(conversation, answer), params: { useful: true }, as: :json |
| 150 | + }.to change(AnswerFeedback, :count).by(1) |
154 | 151 |
|
155 | | - expect(JSON.parse(response.body)) |
156 | | - .to eq({ "message" => "Unprocessable entity", "errors" => { "base" => ["Feedback already provided for this answer"] } }) |
157 | | - end |
| 152 | + answer_feedback = AnswerFeedback.includes(:answer).last |
| 153 | + expect(answer_feedback.answer).to eq(answer) |
| 154 | + expect(answer_feedback.useful).to be true |
158 | 155 | end |
159 | 156 | end |
160 | 157 |
|
161 | | - context "when the params are invalid" do |
162 | | - it "returns an unprocessable_entity status" do |
163 | | - answer = create(:answer, question:) |
| 158 | + context "when an answer already has feedback" do |
| 159 | + before do |
| 160 | + create(:answer_feedback, answer:) |
| 161 | + end |
164 | 162 |
|
165 | | - post api_v0_answer_feedback_path(conversation, answer) |
| 163 | + it "returns an unprocessable_entity status" do |
| 164 | + post api_v0_answer_feedback_path(conversation, answer), params: { useful: true }, as: :json |
166 | 165 | expect(response).to have_http_status(:unprocessable_entity) |
167 | 166 | end |
168 | 167 |
|
169 | 168 | it "returns the correct expected JSON" do |
170 | | - answer = create(:answer, question:) |
171 | | - |
172 | | - post api_v0_answer_feedback_path(conversation, answer) |
| 169 | + post api_v0_answer_feedback_path(conversation, answer), params: { useful: true }, as: :json |
173 | 170 |
|
174 | 171 | expect(JSON.parse(response.body)) |
175 | 172 | .to eq( |
176 | 173 | { |
177 | 174 | "message" => "Unprocessable entity", |
178 | | - "errors" => { "useful" => ["Useful must be true or false"] }, |
| 175 | + "errors" => { "base" => ["Feedback already provided for this answer"] }, |
179 | 176 | }, |
180 | 177 | ) |
181 | 178 | end |
|
0 commit comments