2022-11-16 13:34:57 -08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require "rails_helper"
|
2021-12-31 04:59:08 -08:00
|
|
|
|
2023-02-07 13:52:36 -08:00
|
|
|
describe AnswerController, type: :controller do
|
|
|
|
include ActiveSupport::Testing::TimeHelpers
|
|
|
|
|
2022-11-16 13:34:57 -08:00
|
|
|
let(:user) do
|
|
|
|
FactoryBot.create :user,
|
|
|
|
otp_module: :disabled,
|
|
|
|
otp_secret_key: "AAAAAAAA"
|
|
|
|
end
|
|
|
|
let(:answer) { FactoryBot.create :answer, user: }
|
2021-12-31 04:59:08 -08:00
|
|
|
|
|
|
|
describe "#show" do
|
|
|
|
subject { get :show, params: { username: user.screen_name, id: answer.id } }
|
|
|
|
|
|
|
|
context "user signed in" do
|
|
|
|
before(:each) { sign_in user }
|
|
|
|
|
|
|
|
it "renders the answer/show template" do
|
|
|
|
subject
|
|
|
|
expect(assigns(:answer)).to eq(answer)
|
|
|
|
expect(response).to render_template("answer/show")
|
|
|
|
end
|
|
|
|
end
|
2022-11-16 13:33:30 -08:00
|
|
|
|
|
|
|
context "user opts out of search indexing" do
|
|
|
|
render_views
|
|
|
|
|
2022-11-16 13:34:57 -08:00
|
|
|
before(:each) do
|
2022-11-16 13:33:30 -08:00
|
|
|
sign_in user
|
|
|
|
user.privacy_noindex = true
|
|
|
|
user.save
|
2022-11-16 13:34:57 -08:00
|
|
|
end
|
2022-11-16 13:33:30 -08:00
|
|
|
|
|
|
|
it "renders the answer/show template" do
|
|
|
|
subject
|
|
|
|
expect(assigns(:answer)).to eq(answer)
|
|
|
|
expect(response.body).to include("<meta content='noindex' name='robots'>")
|
|
|
|
end
|
|
|
|
end
|
2021-12-31 04:59:08 -08:00
|
|
|
end
|
2023-02-07 13:52:36 -08:00
|
|
|
|
|
|
|
describe "#pin" do
|
|
|
|
subject { post :pin, params: { username: user.screen_name, id: answer.id } }
|
|
|
|
|
|
|
|
context "user signed in" do
|
|
|
|
before(:each) { sign_in user }
|
|
|
|
|
|
|
|
it "pins the answer" do
|
|
|
|
travel_to(Time.at(1603290950).utc) do
|
|
|
|
expect { subject }.to change { answer.reload.pinned_at }.from(nil).to(Time.at(1603290950).utc)
|
|
|
|
expect(response).to redirect_to(user_path(user))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "other user signed in" do
|
|
|
|
let(:other_user) { FactoryBot.create(:user) }
|
|
|
|
|
|
|
|
before(:each) { sign_in other_user }
|
|
|
|
|
|
|
|
it "does not pin the answer do" do
|
|
|
|
travel_to(Time.at(1603290950).utc) do
|
|
|
|
expect { subject }.to raise_error(Errors::NotAuthorized)
|
|
|
|
expect(answer.reload.pinned_at).to eq(nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#unpin" do
|
|
|
|
subject { delete :unpin, params: { username: user.screen_name, id: answer.id } }
|
|
|
|
|
|
|
|
context "user signed in" do
|
|
|
|
before(:each) do
|
|
|
|
sign_in user
|
|
|
|
answer.update!(pinned_at: Time.at(1603290950).utc)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "pins the answer" do
|
|
|
|
expect { subject }.to change { answer.reload.pinned_at }.from(Time.at(1603290950).utc).to(nil)
|
|
|
|
expect(response).to redirect_to(user_path(user))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "other user signed in" do
|
|
|
|
let(:other_user) { FactoryBot.create(:user) }
|
|
|
|
|
|
|
|
before(:each) do
|
|
|
|
sign_in other_user
|
|
|
|
answer.update!(pinned_at: Time.at(1603290950).utc)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not pin the answer do" do
|
|
|
|
expect { subject }.to raise_error(Errors::NotAuthorized)
|
|
|
|
expect(answer.reload.pinned_at).to eq(Time.at(1603290950).utc)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-12-31 04:59:08 -08:00
|
|
|
end
|