Retrospring/spec/controllers/ajax/answer_controller_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

392 lines
12 KiB
Ruby
Raw Normal View History

2020-04-28 13:13:19 -07:00
# coding: utf-8
# frozen_string_literal: true
require "rails_helper"
2020-04-30 10:15:32 -07:00
describe Ajax::AnswerController, :ajax_controller, type: :controller do
include ActiveSupport::Testing::TimeHelpers
2020-04-28 13:13:19 -07:00
let(:question) { FactoryBot.create(:question, user: FactoryBot.build(:user, privacy_allow_stranger_answers: asker_allows_strangers)) }
let(:asker_allows_strangers) { true }
describe "#create" do
let(:params) do
{
2022-12-28 20:17:04 -08:00
id:,
answer:,
share: shared_services&.to_json,
2023-02-19 11:58:47 -08:00
inbox:,
2020-04-28 13:13:19 -07:00
}.compact
end
2022-12-28 20:17:04 -08:00
subject { post(:create, params:) }
2020-04-28 13:13:19 -07:00
context "when user is signed in" do
shared_examples "creates the answer" do
it "creates an answer" do
expect { subject }.to(change { Answer.count }.by(1))
end
include_examples "returns the expected response"
include_examples "touches user timestamp", :inbox_updated_at
2020-04-28 13:13:19 -07:00
end
shared_examples "does not create the answer" do
it "does not create an answer" do
expect { subject }.not_to(change { Answer.count })
end
include_examples "returns the expected response"
end
shared_examples "fails when answer content is empty" do
let(:expected_response) do
{
"success" => false,
# caught by rescue_from, so status is not peter_dinklage
2022-12-28 20:17:04 -08:00
"status" => "parameter_error",
2023-02-19 11:58:47 -08:00
"message" => anything,
2020-04-28 13:13:19 -07:00
}
end
["", " ", "\r \n"].each do |a|
context "when answer is #{a.inspect}" do
let(:answer) { a }
include_examples "does not create the answer"
end
end
end
before(:each) { sign_in(user) }
context "when all parameters are given" do
let(:answer) { "Werfen Sie nicht länger das Fenster zum Geld hinaus!" }
let(:shared_services) { %w[twitter] }
context "when inbox is true" do
let(:id) { FactoryBot.create(:inbox_entry, user: inbox_user, question:).id }
2020-04-28 13:13:19 -07:00
let(:inbox) { true }
context "when the inbox entry belongs to the user" do
let(:inbox_user) { user }
let(:expected_response) do
{
"success" => true,
2022-12-28 20:17:04 -08:00
"status" => "okay",
2023-02-19 11:58:47 -08:00
"message" => anything,
2020-04-28 13:13:19 -07:00
}
end
include_examples "creates the answer"
it_behaves_like "fails when answer content is empty"
2023-02-19 11:58:47 -08:00
context "when the user has sharing enabled" do
before do
user.sharing_enabled = true
user.save
end
let(:expected_response) do
super().merge(
"sharing" => {
2023-10-18 11:54:41 -07:00
"url" => a_string_matching("https://#{APP_CONFIG['hostname']}/"),
"text" => a_string_matching("Werfen Sie nicht länger das Fenster zum Geld hinaus!"),
2023-02-19 11:58:47 -08:00
"twitter" => a_string_matching("https://twitter.com/"),
"tumblr" => a_string_matching("https://www.tumblr.com/"),
"telegram" => a_string_matching("https://t.me/"),
"custom" => a_string_matching(/Werfen\+Sie\+nicht\+l%C3%A4nger\+das\+Fenster\+zum\+Geld\+hinaus%21/),
}
)
end
include_examples "creates the answer"
end
2020-04-28 13:13:19 -07:00
end
context "when the inbox entry does not belong to the user" do
let(:inbox_user) { FactoryBot.create(:user) }
let(:expected_response) do
{
"success" => false,
2022-12-28 20:17:04 -08:00
"status" => "fail",
2023-02-19 11:58:47 -08:00
"message" => anything,
2020-04-28 13:13:19 -07:00
}
end
include_examples "does not create the answer"
it_behaves_like "fails when answer content is empty"
end
context "when the question author mutes the user" do
let(:inbox_user) { user }
let(:expected_response) do
{
"success" => true,
2022-12-28 20:17:04 -08:00
"status" => "okay",
2023-02-19 11:58:47 -08:00
"message" => anything,
}
end
before do
question.user.mute(inbox_user)
end
include_examples "creates the answer"
it_behaves_like "fails when answer content is empty"
it "does not create a notification for the question author" do
2022-12-28 20:17:04 -08:00
expect { subject }.to change { question.user.notifications.count }.by(0)
end
end
2020-04-28 13:13:19 -07:00
end
context "when inbox is false" do
let(:id) { question.id }
let(:inbox) { false }
context "when question asker allows strangers to answer (i.e. question was not in inbox)" do
let(:asker_allows_strangers) { true }
let(:expected_response) do
{
"success" => true,
2022-12-28 20:17:04 -08:00
"status" => "okay",
2020-04-28 13:13:19 -07:00
"message" => anything,
2023-02-19 11:58:47 -08:00
"render" => anything,
2020-04-28 13:13:19 -07:00
}
end
include_examples "creates the answer"
it_behaves_like "fails when answer content is empty"
2023-02-19 11:58:47 -08:00
context "when the user has sharing enabled" do
before do
user.sharing_enabled = true
user.save
end
let(:expected_response) do
super().merge(
"sharing" => {
2023-10-18 11:54:41 -07:00
"url" => a_string_matching("https://#{APP_CONFIG['hostname']}/"),
"text" => a_string_matching("Werfen Sie nicht länger das Fenster zum Geld hinaus!"),
2023-02-19 11:58:47 -08:00
"twitter" => a_string_matching("https://twitter.com/"),
"tumblr" => a_string_matching("https://www.tumblr.com/"),
"telegram" => a_string_matching("https://t.me/"),
"custom" => a_string_matching(/Werfen\+Sie\+nicht\+l%C3%A4nger\+das\+Fenster\+zum\+Geld\+hinaus%21/),
}
)
end
include_examples "creates the answer"
end
2020-04-28 13:13:19 -07:00
end
context "when question asker does not allow strangers to answer (i.e. question was not in inbox)" do
let(:asker_allows_strangers) { false }
let(:expected_response) do
{
"success" => false,
2022-12-28 20:17:04 -08:00
"status" => "privacy_stronk",
2023-02-19 11:58:47 -08:00
"message" => anything,
2020-04-28 13:13:19 -07:00
}
end
include_examples "does not create the answer"
end
context "when user is blocked by the question author" do
let!(:block) { Relationships::Block.create(source_id: question.user.id, target_id: user.id) }
let(:shared_services) { %w[] }
let(:answer) { "u suck >:3" }
let(:expected_response) do
{
"success" => false,
2022-06-12 04:46:48 -07:00
"status" => "answering_other_blocked_self",
2023-02-19 11:58:47 -08:00
"message" => I18n.t("errors.answering_other_blocked_self"),
}
end
include_examples "returns the expected response"
end
context "when question author is blocked by the user" do
let!(:block) { Relationships::Block.create(source_id: user.id, target_id: question.user.id) }
let(:shared_services) { %w[] }
let(:answer) { "u suck >:3" }
let(:expected_response) do
{
"success" => false,
2022-06-12 04:46:48 -07:00
"status" => "answering_self_blocked_other",
2023-02-19 11:58:47 -08:00
"message" => I18n.t("errors.answering_self_blocked_other"),
}
end
include_examples "returns the expected response"
end
2020-04-28 13:13:19 -07:00
end
end
context "when some parameters are missing" do
let(:id) { nil }
let(:answer) { "Trollolo" }
let(:inbox) { nil }
let(:shared_services) { [] }
let(:expected_response) do
{
"success" => false,
# caught by rescue_from, so status is not peter_dinklage
2022-12-28 20:17:04 -08:00
"status" => "parameter_error",
2023-02-19 11:58:47 -08:00
"message" => anything,
2020-04-28 13:13:19 -07:00
}
end
include_examples "returns the expected response"
end
end
context "when user is not signed in" do
let(:id) { question.id }
let(:inbox) { false }
let(:shared_services) { %w[twitter] }
let(:answer) { "HACKED" }
let(:expected_response) do
{
"success" => false,
2022-12-28 20:17:04 -08:00
"status" => "err",
2023-02-19 11:58:47 -08:00
"message" => anything,
2020-04-28 13:13:19 -07:00
}
end
include_examples "returns the expected response"
end
end
describe "#destroy" do
let(:answer_user) { user }
2020-04-30 09:39:33 -07:00
let(:question) { FactoryBot.create(:question) }
2022-12-28 20:17:04 -08:00
let(:answer) { FactoryBot.create(:answer, user: answer_user, question:) }
2020-04-28 13:13:19 -07:00
let(:answer_id) { answer.id }
let(:params) do
{
2023-02-19 11:58:47 -08:00
answer: answer_id,
2020-04-28 13:13:19 -07:00
}
end
2022-12-28 20:17:04 -08:00
subject { delete(:destroy, params:) }
2020-04-28 13:13:19 -07:00
context "when user is signed in" do
shared_examples "deletes the answer" do
let(:expected_response) do
{
"success" => true,
2022-12-28 20:17:04 -08:00
"status" => "okay",
2023-02-19 11:58:47 -08:00
"message" => anything,
2020-04-28 13:13:19 -07:00
}
end
it "deletes the answer" do
answer # ensure we already have it in the db
expect { subject }.to(change { Answer.count }.by(-1))
end
include_examples "returns the expected response"
end
shared_examples "does not delete the answer" do
let(:expected_response) do
{
"success" => false,
2022-12-28 20:17:04 -08:00
"status" => "nopriv",
2023-02-19 11:58:47 -08:00
"message" => anything,
2020-04-28 13:13:19 -07:00
}
end
it "does not delete the answer" do
answer # ensure we already have it in the db
expect { subject }.not_to(change { Answer.count })
end
include_examples "returns the expected response"
end
before(:each) { sign_in(user) }
context "when the answer exists and was made by the current user" do
include_examples "deletes the answer"
it "returns the question back to the user's inbox" do
expect { subject }.to(change { InboxEntry.where(question_id: answer.question.id, user_id: user.id).count }.by(1))
2020-04-28 13:13:19 -07:00
end
it "returns the question back to the user's inbox when the user has anonymous questions disabled" do
user.privacy_allow_anonymous_questions = false
user.save
expect { subject }.to(change { InboxEntry.where(question_id: answer.question.id, user_id: user.id).count }.by(1))
end
it "updates the inbox caching timestamp for the user who answered" do
initial_timestamp = 1.day.ago
answer.user.update(inbox_updated_at: initial_timestamp)
2023-08-17 11:54:37 -07:00
travel_to 1.day.from_now do
# using string representation to avoid precision issues
expect { subject }.to(change { answer.user.reload.inbox_updated_at.to_s }.from(initial_timestamp.to_s).to(DateTime.now))
end
end
2020-04-28 13:13:19 -07:00
end
context "when the answer exists and was not made by the current user" do
let(:answer_user) { FactoryBot.create(:user) }
include_examples "does not delete the answer"
%i[moderator administrator].each do |privileged_role|
context "when the current user is a #{privileged_role}" do
around do |example|
user.add_role privileged_role
example.run
user.remove_role privileged_role
end
include_examples "deletes the answer"
end
end
end
context "when the answer does not exist" do
let(:answer_id) { "sonic_the_hedgehog" }
let(:expected_response) do
{
"success" => false,
2022-12-28 20:17:04 -08:00
"status" => anything,
2023-02-19 11:58:47 -08:00
"message" => anything,
2020-04-28 13:13:19 -07:00
}
end
include_examples "returns the expected response"
end
end
context "when user is not signed in" do
let(:expected_response) do
{
"success" => false,
2022-12-28 20:17:04 -08:00
"status" => "nopriv",
2023-02-19 11:58:47 -08:00
"message" => anything,
2020-04-28 13:13:19 -07:00
}
end
include_examples "returns the expected response"
end
end
end