286 行
14 KiB
Ruby
286 行
14 KiB
Ruby
require 'rails_helper'
|
|
|
|
RSpec.describe 'Post history privacy', type: :request do
|
|
let(:origin) { Time.utc(2020, 1, 1) }
|
|
let!(:post_record) do
|
|
create(:post, title: 'current', created_at: origin, updated_at: origin)
|
|
end
|
|
let!(:other_post) do
|
|
create(:post, created_at: origin, updated_at: origin + 3.days + 1.hour)
|
|
end
|
|
let!(:tag) { create(:tag, primary_name: 'private-history-tag') }
|
|
let!(:external) { create(:external_tag, name: 'external-history-tag') }
|
|
|
|
before do
|
|
['create', 'discard'].each_with_index do |event, index|
|
|
TagVersion.create!(tag:, version_no: index + 1, event_type: event,
|
|
name: 'private-history-tag', category: 'general', aliases: '', parent_tag_ids: '',
|
|
names: { ja: { name: 'private-history-tag', script_code: 'Jpan',
|
|
auto_generated: false, aliases: [] } },
|
|
created_at: origin + index.hours)
|
|
end
|
|
NicoTagVersion.create!(external_tag: external, version_no: 1, event_type: 'create',
|
|
linked_tag_ids: tag.id.to_s,
|
|
created_at: origin)
|
|
['create', 'discard', 'restore', 'update'].each_with_index do |event, index|
|
|
snapshots =
|
|
if index < 2
|
|
[{ tag_id: tag.id, version_no: 1, name: 'private-history-tag', sections: [] }]
|
|
elsif index == 3
|
|
[{ external_tag_id: external.id }]
|
|
else
|
|
[]
|
|
end
|
|
PostVersion.create!(post: post_record, version_no: index + 1, event_type: event,
|
|
title: "version #{ index + 1 }", url: post_record.url, tags: '',
|
|
thumbnail_base: "history-#{ index + 1 }", video_ms: (index + 1) * 1000,
|
|
original_created_from: origin - 1.year + index.days,
|
|
original_created_before: origin - 1.year + (index + 1).days,
|
|
tags_json: snapshots, parent_post_ids: '', created_at: origin + (index + 1).days)
|
|
end
|
|
end
|
|
|
|
it 'evaluates each reference condition against the restored snapshot alone' do
|
|
HistoryAccess.set(user: nil) do
|
|
restored = PostVersion.where(post_id: post_record.id, version_no: 3)
|
|
HistoryAccess.reference_sql('post_versions', 'post_versions').each do |condition|
|
|
expect(restored.where(Arel.sql(condition)).pluck(:version_no)).to eq([3])
|
|
end
|
|
expect(HistoryAccess.visible(PostVersion.where(post_id: post_record.id))
|
|
.pluck(:version_no)).to eq([3])
|
|
end
|
|
end
|
|
|
|
it 'keeps every version through a later discard private after another restore' do
|
|
['discard', 'restore'].each_with_index do |event, index|
|
|
PostVersion.create!(post: post_record, version_no: index + 5, event_type: event,
|
|
title: 'later snapshot', url: post_record.url, tags: '', tags_json: [],
|
|
parent_post_ids: '', created_at: origin + (index + 5).days)
|
|
end
|
|
|
|
get '/posts/versions', params: { post: post_record.id }
|
|
|
|
expect(response).to have_http_status(:ok)
|
|
expect(response.parsed_body.fetch('versions').pluck('version_no')).to eq([6])
|
|
expect(response.parsed_body.fetch('count')).to eq(1)
|
|
expect(response.parsed_body.fetch('versions').first.fetch('title').fetch('prev')).to be_nil
|
|
end
|
|
|
|
['anonymous', 'guest', 'member', 'admin'].each do |role|
|
|
context "as #{ role }" do
|
|
before do
|
|
sign_in_as(create(:user, role.to_sym)) unless role == 'anonymous'
|
|
end
|
|
|
|
it 'uses only authorised history timestamps for display' do
|
|
get '/posts'
|
|
expect(response).to have_http_status(:ok)
|
|
row = response.parsed_body.fetch('posts').find { _1.fetch('id') == post_record.id }
|
|
expected = origin + (role == 'admin' ? 4 : 3).days
|
|
expect(Time.zone.parse(row.fetch('updated_at'))).to eq(expected)
|
|
end
|
|
|
|
it 'uses the same visibility for date bounds and count' do
|
|
get '/posts', params: { updated_from: (origin + 4.days).iso8601,
|
|
updated_to: (origin + 4.days).iso8601 }
|
|
expect(response).to have_http_status(:ok)
|
|
ids = response.parsed_body.fetch('posts').pluck('id')
|
|
expect(ids).to eq(role == 'admin' ? [post_record.id] : [])
|
|
expect(response.parsed_body.fetch('count')).to eq(ids.length)
|
|
end
|
|
|
|
it 'sorts using authorised timestamps in both directions' do
|
|
['asc', 'desc'].each do |direction|
|
|
get '/posts', params: { order: "updated_at:#{ direction }" }
|
|
expect(response).to have_http_status(:ok)
|
|
expected = role == 'admin' ? [other_post.id, post_record.id] :
|
|
[post_record.id, other_post.id]
|
|
expected.reverse! if direction == 'desc'
|
|
expect(response.parsed_body.fetch('posts').pluck('id')).to eq(expected)
|
|
end
|
|
end
|
|
|
|
it 'applies authorised timestamp ordering before pagination' do
|
|
get '/posts', params: { order: 'updated_at:desc', limit: 1 }
|
|
|
|
expect(response).to have_http_status(:ok)
|
|
expected = role == 'admin' ? post_record.id : other_post.id
|
|
expect(response.parsed_body.fetch('posts').pluck('id')).to eq([expected])
|
|
expect(response.parsed_body.fetch('count')).to eq(2)
|
|
end
|
|
|
|
it 'counts only visible versions and masks a private previous version' do
|
|
get '/posts/versions', params: { post: post_record.id }
|
|
expect(response).to have_http_status(:ok)
|
|
body = response.parsed_body
|
|
expected = role == 'admin' ? [4, 3, 2, 1] : [3]
|
|
expect(body.fetch('count')).to eq(expected.length)
|
|
expect(body.fetch('versions').pluck('version_no')).to eq(expected)
|
|
restored = body.fetch('versions').find { _1.fetch('version_no') == 3 }
|
|
expect(restored.fetch('title').fetch('prev')).to eq(role == 'admin' ? 'version 2' : nil)
|
|
unless role == 'admin'
|
|
fields = [
|
|
'url', 'thumbnail_base', 'video_ms',
|
|
'original_created_from', 'original_created_before']
|
|
fields.each do |field|
|
|
expect(restored.fetch(field).fetch('prev')).to be_nil
|
|
end
|
|
expect(restored.fetch('tags')).to eq([])
|
|
end
|
|
end
|
|
|
|
it 'uses only an authorised previous version for tag filtering' do
|
|
get '/posts/versions', params: { post: post_record.id, tag: tag.id }
|
|
expect(response).to have_http_status(:ok)
|
|
expected = role == 'admin' ? [3, 2, 1] : []
|
|
expect(response.parsed_body.fetch('versions').pluck('version_no')).to eq(expected)
|
|
expect(response.parsed_body.fetch('count')).to eq(expected.length)
|
|
end
|
|
|
|
it 'returns an empty history with an exact count' do
|
|
get '/posts/versions', params: { post: other_post.id }
|
|
|
|
expect(response).to have_http_status(:ok)
|
|
expect(response.parsed_body).to include('versions' => [], 'count' => 0,
|
|
'population_count' => 4)
|
|
end
|
|
|
|
it 'keeps counts and timestamp ties consistent across history pages' do
|
|
rows = 25.times.map do |index|
|
|
PostVersion.create!(post: post_record, version_no: index + 5, event_type: 'update',
|
|
title: 'page snapshot', url: post_record.url, tags: '', tags_json: [],
|
|
parent_post_ids: '', created_at: origin + 5.days)
|
|
end
|
|
rows << PostVersion.create!(post: other_post, version_no: 1, event_type: 'create',
|
|
title: 'another post', url: other_post.url, tags: '', tags_json: [],
|
|
parent_post_ids: '', created_at: origin + 5.days)
|
|
earlier = role == 'admin' ? [4, 3, 2, 1] : [3]
|
|
expected = rows.reverse.map { [_1.post_id, _1.version_no] } +
|
|
earlier.map { [post_record.id, _1] }
|
|
|
|
expected.each_slice(7).with_index do |page_rows, index|
|
|
get '/posts/versions', params: { page: index + 1, limit: 7 }
|
|
|
|
expect(response).to have_http_status(:ok)
|
|
body = response.parsed_body
|
|
expect(body.fetch('count')).to eq(expected.length)
|
|
expect(body.fetch('population_count')).to eq(30)
|
|
expect(body.fetch('versions').map { [_1.fetch('post_id'), _1.fetch('version_no')] })
|
|
.to eq(page_rows)
|
|
end
|
|
|
|
get '/posts/versions', params: { page: 10, limit: 7 }
|
|
expect(response).to have_http_status(:ok)
|
|
expect(response.parsed_body).to include('versions' => [], 'count' => expected.length)
|
|
end
|
|
|
|
it 'loads the page and its authorised diff in one data query' do
|
|
statements = []
|
|
listener = lambda do |_name, _start, _finish, _id, payload|
|
|
next if ['SCHEMA', 'TRANSACTION'].include?(payload[:name])
|
|
|
|
statements << payload[:sql] if payload[:sql].match?(/\ASELECT/i)
|
|
end
|
|
ActiveSupport::Notifications.subscribed(listener, 'sql.active_record') do
|
|
get '/posts/versions', params: { post: post_record.id, limit: 1 }
|
|
end
|
|
|
|
expect(response).to have_http_status(:ok)
|
|
history_queries = statements.select { _1.include?('post_versions') }
|
|
counts, details = history_queries.partition { _1.match?(/\ASELECT COUNT\(/i) }
|
|
expect(counts.length).to eq(2)
|
|
expect(counts.join(' ')).not_to include('LEFT JOIN post_versions prev')
|
|
expect(details.length).to eq(1)
|
|
end
|
|
|
|
it 'hides references to a discarded parent even after its restore' do
|
|
['discard', 'restore'].each_with_index do |event, index|
|
|
PostVersion.create!(post: other_post, version_no: index + 1, event_type: event,
|
|
title: 'parent snapshot', url: other_post.url, tags: '', tags_json: [],
|
|
parent_post_ids: '', created_at: origin + index.hours)
|
|
end
|
|
[other_post.id.to_s, ''].each_with_index do |parents, index|
|
|
PostVersion.create!(post: post_record, version_no: index + 5, event_type: 'update',
|
|
title: 'parent reference', url: post_record.url, tags: '', tags_json: [],
|
|
parent_post_ids: parents, created_at: origin + (index + 5).days)
|
|
end
|
|
|
|
get '/posts/versions', params: { post: post_record.id }
|
|
|
|
expect(response).to have_http_status(:ok)
|
|
body = response.parsed_body
|
|
expected = role == 'admin' ? [6, 5, 4, 3, 2, 1] : [6, 3]
|
|
expect(body.fetch('versions').pluck('version_no')).to eq(expected)
|
|
expect(body.fetch('count')).to eq(expected.length)
|
|
previous = body.fetch('versions').first.fetch('title').fetch('prev')
|
|
expect(previous).to eq(role == 'admin' ? 'parent reference' : nil)
|
|
end
|
|
|
|
it 'keeps restored tag references private' do
|
|
TagVersion.create!(tag:, version_no: 3, event_type: 'restore',
|
|
name: 'private-history-tag', category: 'general', aliases: '', parent_tag_ids: '',
|
|
names: { ja: { name: 'private-history-tag', script_code: 'Jpan',
|
|
auto_generated: false, aliases: [] } },
|
|
created_at: origin + 2.hours)
|
|
PostVersion.create!(post: post_record, version_no: 5, event_type: 'update',
|
|
title: 'restored tag reference', url: post_record.url, tags: '',
|
|
tags_json: [{ tag_id: tag.id, version_no: 3, sections: [] }],
|
|
parent_post_ids: '', created_at: origin + 5.days)
|
|
|
|
get '/posts/versions', params: { post: post_record.id }
|
|
|
|
expect(response).to have_http_status(:ok)
|
|
expected = role == 'admin' ? [5, 4, 3, 2, 1] : [3]
|
|
expect(response.parsed_body.fetch('versions').pluck('version_no')).to eq(expected)
|
|
expect(response.parsed_body.fetch('count')).to eq(expected.length)
|
|
end
|
|
|
|
it 'uses the external linkage recorded at the post snapshot time' do
|
|
NicoTagVersion.create!(external_tag: external, version_no: 2, event_type: 'update',
|
|
linked_tag_ids: '', created_at: origin + 5.days)
|
|
PostVersion.create!(post: post_record, version_no: 5, event_type: 'update',
|
|
title: 'public external reference', url: post_record.url, tags: '',
|
|
tags_json: [{ external_tag_id: external.id }], parent_post_ids: '',
|
|
created_at: origin + 6.days)
|
|
|
|
get '/posts/versions', params: { post: post_record.id, external_tag: external.id }
|
|
|
|
expect(response).to have_http_status(:ok)
|
|
body = response.parsed_body
|
|
expected = role == 'admin' ? [5, 4] : [5]
|
|
expect(body.fetch('versions').pluck('version_no')).to eq(expected)
|
|
expect(body.fetch('count')).to eq(expected.length)
|
|
latest = body.fetch('versions').first
|
|
previous = latest.fetch('title').fetch('prev')
|
|
expect(previous).to eq(role == 'admin' ? 'version 4' : nil)
|
|
expect(latest.fetch('tags')).to contain_exactly(a_hash_including(
|
|
'name' => 'nico:external-history-tag',
|
|
'type' => role == 'admin' ? 'context' : 'added'))
|
|
end
|
|
|
|
it 'hides references to historical external discard events after restore' do
|
|
public_external = create(:external_tag)
|
|
# Discard/restore events can remain in history despite current event validation.
|
|
snapshots = ['create', 'discard', 'restore'].each_with_index.map do |event, index|
|
|
{ tag_id: public_external.id, version_no: index + 1, event_type: event,
|
|
linked_tag_ids: '', created_at: origin + index.days }
|
|
end
|
|
NicoTagVersion.insert_all!(snapshots)
|
|
[5, 6].each do |number|
|
|
PostVersion.create!(post: post_record, version_no: number, event_type: 'update',
|
|
title: 'external discard reference', url: post_record.url, tags: '',
|
|
tags_json: [{ external_tag_id: public_external.id }], parent_post_ids: '',
|
|
created_at: origin + number.days)
|
|
end
|
|
|
|
get '/posts/versions', params: { post: post_record.id, external_tag: public_external.id }
|
|
|
|
expect(response).to have_http_status(:ok)
|
|
expected = role == 'admin' ? [6, 5] : []
|
|
expect(response.parsed_body.fetch('versions').pluck('version_no')).to eq(expected)
|
|
expect(response.parsed_body.fetch('count')).to eq(expected.length)
|
|
end
|
|
end
|
|
end
|
|
end
|