* Extract reaction create from controller to service * Success is having no errors * Confirm http status successful * V1 API endpoint for Create Reaction * Actually reset cache count * Tweak JSON response * Reaction create -> toggle * Try rswagger docs for reaction toggle
73 lines
2.5 KiB
Ruby
73 lines
2.5 KiB
Ruby
require "rails_helper"
|
|
|
|
RSpec.describe "Api::V1::Reactions", type: :request do
|
|
let!(:v1_headers) { { "content-type" => "application/json", "Accept" => "application/vnd.forem.api-v1+json" } }
|
|
let(:params) do
|
|
{
|
|
reactable_type: "Article",
|
|
reactable_id: "123",
|
|
category: "like"
|
|
}
|
|
end
|
|
|
|
before { allow(FeatureFlag).to receive(:enabled?).with(:api_v1).and_return(true) }
|
|
|
|
shared_context "when user is authorized" do
|
|
let(:api_secret) { create(:api_secret) }
|
|
let(:user) { api_secret.user }
|
|
let(:auth_header) { v1_headers.merge({ "api-key" => api_secret.secret }) }
|
|
end
|
|
|
|
context "when unauthenticated and post to create" do
|
|
it "returns unauthorized" do
|
|
post api_reactions_toggle_path, params: params.to_json, headers: v1_headers
|
|
expect(response).to have_http_status(:unauthorized)
|
|
end
|
|
end
|
|
|
|
context "when unauthorized and post to create" do
|
|
it "returns unauthorized" do
|
|
post api_reactions_toggle_path, params: params.to_json,
|
|
headers: v1_headers.merge({ "api-key" => "invalid api key" })
|
|
expect(response).to have_http_status(:unauthorized)
|
|
end
|
|
end
|
|
|
|
context "when authorized and post to create" do
|
|
include_context "when user is authorized"
|
|
|
|
before do
|
|
allow(Rails.cache).to receive(:delete)
|
|
allow(ReactionToggle).to receive(:toggle).and_return(result)
|
|
end
|
|
|
|
context "when toggled successfully" do
|
|
let(:result) { ReactionToggle::Result.new reaction: Reaction.new }
|
|
|
|
it "responds with success" do
|
|
post api_reactions_toggle_path, params: params.to_json, headers: auth_header
|
|
expect(response).to have_http_status(:success)
|
|
end
|
|
|
|
it "responds with expected JSON" do
|
|
post api_reactions_toggle_path, params: params.to_json, headers: auth_header
|
|
expect(JSON.parse(response.body).keys).to contain_exactly("id", "result", "category", "reactable_type",
|
|
"reactable_id")
|
|
end
|
|
end
|
|
|
|
context "when toggled unsuccessfully" do
|
|
let(:result) do
|
|
ReactionToggle::Result.new.tap do |bad_result|
|
|
allow(bad_result).to receive(:success?).and_return(false)
|
|
allow(bad_result).to receive(:errors_as_sentence).and_return("Stuff was bad")
|
|
end
|
|
end
|
|
|
|
it "responds with success" do
|
|
post api_reactions_toggle_path, params: params.to_json, headers: auth_header
|
|
expect(response).to have_http_status(:unprocessable_entity)
|
|
end
|
|
end
|
|
end
|
|
end
|