require 'rails_helper' RSpec.describe 'Submissions', type: :request do let(:user) { create(:user) } let(:admin) { create(:user, :admin) } let(:opinion) { { category: 'opinion', content: '御意見です.' } } def record_version(target, event_type) recorder = { Post => [PostVersionRecorder, :post], Tag => [TagVersionRecorder, :tag], Material => [MaterialVersionRecorder, :material] }.fetch(target.class) recorder.first.record!(**{ recorder.last => target }, event_type:, created_by_user: admin) target.reload end describe 'POST /submissions' do it 'requires an authenticated user' do expect { post '/submissions', params: opinion } .not_to change(Submission, :count) expect(response).to have_http_status(:unauthorized) end ['guest', 'member', 'admin'].each do |role| it "accepts opinions from #{ role } users using server identity" do actor = create(:user, role:) expect { post '/submissions', params: opinion.merge(user_id: admin.id, role: 'admin'), headers: cookie_headers(actor) }.to change(Submission, :count).by(1) expect(response).to have_http_status(:created) expect(Submission.last.user).to eq(actor) expect(Submission.last.target_id).to be_nil expect(response.parsed_body.fetch('user')).to eq( { 'id' => actor.id, 'name' => actor.name }) expect(response.body).not_to include(actor.inheritance_code) end end [{ content: '' }, { content: ' ' }, { content: 'x' * 5001 }, { category: 'unknown' }, { target_id: 1 }].each do |attributes| it "rejects invalid opinion attributes #{ attributes.keys }" do sign_in_as(user) expect { post '/submissions', params: opinion.merge(attributes) } .not_to change(Submission, :count) expect(response).to have_http_status(:unprocessable_entity) expect(response.parsed_body.fetch('type')).to eq('validation_error') end end it 'rejects banned users' do sign_in_as(create(:user, :banned)) expect { post '/submissions', params: opinion } .not_to change(Submission, :count) expect(response).to have_http_status(:forbidden) end it 'rejects banned IP addresses for reads and writes' do create(:ip_address, :banned, ip_address: IPAddr.new('192.0.2.1').hton) headers = { 'REMOTE_ADDR' => '192.0.2.1' } sign_in_as(user) expect { post '/submissions', params: opinion, headers: } .not_to change(Submission, :count) expect(response).to have_http_status(:forbidden) get '/submissions', headers: expect(response).to have_http_status(:forbidden) end end describe 'GET /submissions' do it 'allows anonymous browsing without exposing user credentials' do submission = Submission.create!(**opinion, user:) get '/submissions' expect(response).to have_http_status(:ok) expect(response.parsed_body.fetch('submissions')).to contain_exactly( include('id' => submission.id, 'content' => opinion[:content], 'user' => { 'id' => user.id, 'name' => user.name })) expect(response.body).not_to include(user.inheritance_code) expect(response.headers['Cache-Control']).to include('no-store') end it 'paginates newest first with stable ordering and a public count' do submissions = Array.new(21) { Submission.create!(**opinion, user:) } get '/submissions' expect(response.parsed_body.fetch('count')).to eq(21) expect(response.parsed_body.fetch('page_size')).to eq(20) expect(response.parsed_body.fetch('submissions').pluck('id')) .to eq(submissions.reverse.first(20).map(&:id)) get '/submissions', params: { page: 2 } expect(response.parsed_body.fetch('submissions').pluck('id')) .to eq([submissions.first.id]) end end ['post_removal', 'tag_removal', 'material_removal'].each do |category| context category do let(:target) do case category when 'post_removal' then create(:post) when 'tag_removal' then create(:tag) when 'material_removal' then Material.create!(url: 'https://example.com/material') end end let(:attributes) { { category:, target_id: target.id, content: '除票を申請します.' } } it 'stores a guest request without changing the target or its history' do sign_in_as(user) original = target.attributes version_model = Submission::TARGETS.fetch(category)[1] expect { post '/submissions', params: attributes.merge(target_version_no: 99999) }.not_to change(version_model, :count) expect(response).to have_http_status(:created) expect(target.reload.attributes).to eq(original) expect(Submission.last).to have_attributes( category:, target_id: target.id, target_version_no: target.version_no, user:) sign_out get '/submissions' expect(response.parsed_body.fetch('submissions').pluck('id')) .to eq([Submission.last.id]) end [nil, 0, -1, '1.5', '1x', 2**63].each do |target_id| it "rejects invalid target ID #{ target_id.inspect }" do sign_in_as(user) expect { post '/submissions', params: attributes.merge(target_id:) } .not_to change(Submission, :count) expect(response).to have_http_status(:unprocessable_entity) expect(response.parsed_body.fetch('errors')).to have_key('target_id') end end it 'rejects a missing target' do sign_in_as(user) post '/submissions', params: attributes.merge(target_id: 2**62) expect(response).to have_http_status(:unprocessable_entity) end it 'hides pre-removal requests after restores and every later removal' do record_version(target, 'create') original = Submission.create!(**attributes, user:) record_version(target, 'discard') sign_in_as(user) post '/submissions', params: attributes expect(response).to have_http_status(:unprocessable_entity) boundary = HistoryAccess.set(user: admin) { Submission.create!(**attributes, user: admin) } record_version(target, 'restore') restored = Submission.create!(**attributes, user:) [nil, user, create(:user, :member)].each do |actor| sign_in_as(actor) get '/submissions', params: { role: 'admin' } expect(response.parsed_body.fetch('submissions').pluck('id')).to eq([restored.id]) expect(response.parsed_body.fetch('count')).to eq(1) end record_version(target, 'discard') record_version(target, 'restore') sign_out get '/submissions' expect(response.parsed_body.fetch('submissions')).to eq([]) expect(response.parsed_body.fetch('count')).to eq(0) sign_in_as(admin) get '/submissions' expect(response.parsed_body.fetch('submissions').pluck('id')) .to eq([restored.id, boundary.id, original.id]) expect(response.parsed_body.fetch('count')).to eq(3) end end end end