Merge remote-tracking branch 'origin/main' into feature/414
このコミットが含まれているのは:
@@ -0,0 +1,196 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe 'Counts masked search', type: :request do
|
||||
let!(:tag) { create(:tag, primary_name: '秘密のタグ') }
|
||||
let(:actor) { create(:user, :member) }
|
||||
let(:admin) { create(:user, :admin) }
|
||||
let(:base) do
|
||||
{ 'resource' => 'tags', 'definition' => 'history', 'mode' => 'summary',
|
||||
'locale' => 'ja', 'execution_at' => '2025-01-01T00:00:00Z',
|
||||
'timezone' => 'UTC', 'rows' => '', 'cols' => '', 'sort' => '' }
|
||||
end
|
||||
|
||||
before do
|
||||
[1, 2].each do |number|
|
||||
TagVersion.create!(tag:, version_no: number,
|
||||
event_type: number == 1 ? 'create' : 'discard', name: '秘密のタグ',
|
||||
category: 'general', aliases: '秘密の別名', parent_tag_ids: '',
|
||||
names: { ja: { name: '秘密のタグ', script_code: 'Jpan', auto_generated: false,
|
||||
aliases: [{ name: '秘密の別名', script_code: 'Jpan' }] } },
|
||||
created_by_user: actor, created_at: Time.utc(2023, number))
|
||||
end
|
||||
end
|
||||
|
||||
def query(extra = {})
|
||||
get '/counts/query', params: base.merge(extra)
|
||||
expect(response).to have_http_status(:ok), extra.inspect
|
||||
response.parsed_body
|
||||
end
|
||||
|
||||
def predicate(field, operator, value, index = 0)
|
||||
prefix = "conditions.#{ index }"
|
||||
result = { 'conditions' => '', "#{ prefix }.field" => field,
|
||||
"#{ prefix }.operator" => operator }
|
||||
if ['in', 'any', 'all', 'none', 'not_in'].include?(operator)
|
||||
result.merge!("#{ prefix }.values" => '1', 'condition_values' => '',
|
||||
"condition_values.#{ index }.condition" => index.to_s,
|
||||
"condition_values.#{ index }.value_type" => 'string',
|
||||
"condition_values.#{ index }.value" => value)
|
||||
else
|
||||
result["#{ prefix }.value"] = value
|
||||
end
|
||||
result
|
||||
end
|
||||
|
||||
it 'rejects every name and alias predicate on private history' do
|
||||
['name', 'name_ja', 'names.name', 'aliases.name', 'names.aliases.name'].each do |field|
|
||||
secret = field.include?('aliases') ? '秘密の別名' : '秘密のタグ'
|
||||
multiple = !['name', 'name_ja'].include?(field)
|
||||
operators = multiple ? ['any', 'all', 'none'] : ['in', 'not_in']
|
||||
['eq', 'ne', 'like', *operators].each do |operator|
|
||||
[secret, '********', '%', ''].each do |value|
|
||||
expected = (value == '' && ['eq', 'ne', 'like'].include?(operator)) ? 2 : 0
|
||||
expect(query(predicate(field, operator, value))['count']).to eq(expected)
|
||||
end
|
||||
end
|
||||
blank = predicate(field, 'eq', '').merge('conditions.0.blank' => '1')
|
||||
expect(query(blank)['count']).to eq(0)
|
||||
end
|
||||
end
|
||||
|
||||
it 'ignores missing and empty scalar values unless blank is checked' do
|
||||
['name', 'name_ja', 'names.name', 'aliases.name'].each do |field|
|
||||
['eq', 'ne', 'like'].each do |operator|
|
||||
condition = predicate(field, operator, '')
|
||||
[condition, condition.except('conditions.0.value')].each do |empty|
|
||||
expect(query(empty)['count']).to eq(2)
|
||||
expect(query(empty.merge('conditions.0.blank' => '0'))['count']).to eq(2)
|
||||
expect(query(empty.merge('conditions.0.blank' => '1'))['count']).to eq(0)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
it 'keeps details totals unchanged for inactive private predicates' do
|
||||
settings = predicate('name', 'ne', '').merge('mode' => 'details')
|
||||
result = query(settings)
|
||||
expect(result['count']).to eq(0)
|
||||
expect(result['versions']).to be_empty
|
||||
expect(result['output_count']).to eq(2)
|
||||
active = query(settings.merge('conditions.0.blank' => '1'))
|
||||
expect(active['output_count']).to eq(0)
|
||||
end
|
||||
|
||||
it 'ignores missing sets but preserves explicit empty set semantics' do
|
||||
{ 'name' => ['in', 'not_in'], 'aliases.name' => ['any', 'all', 'none'] }.each do
|
||||
|field, operators|
|
||||
operators.each do |operator|
|
||||
condition = { 'conditions' => '', 'conditions.0.field' => field,
|
||||
'conditions.0.operator' => operator }
|
||||
expect(query(condition)['count']).to eq(2)
|
||||
empty = condition.merge('conditions.0.values' => '1', 'condition_values' => '')
|
||||
expected = ['in', 'any'].include?(operator) ? 0 : 2
|
||||
expect(query(empty)['count']).to eq(expected)
|
||||
expect(query(condition.merge('conditions.0.blank' => '1'))['count']).to eq(0)
|
||||
# An explicit empty string element is a real value, not an unspecified set.
|
||||
expect(query(predicate(field, operator, ''))['count']).to eq(0)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
it 'ignores empty private predicates regardless of their position' do
|
||||
first = predicate('name', 'ne', '')
|
||||
.merge(predicate('resource_id', 'eq', tag.id.to_s, 1))
|
||||
last = predicate('resource_id', 'eq', tag.id.to_s)
|
||||
.merge(predicate('aliases.name', 'like', '', 1))
|
||||
expect(query(first)['count']).to eq(2)
|
||||
expect(query(last)['count']).to eq(2)
|
||||
active = predicate('name', 'ne', 'unrelated')
|
||||
.merge(predicate('aliases.name', 'like', '', 1))
|
||||
expect(query(active)['count']).to eq(0)
|
||||
end
|
||||
|
||||
it 'searches public literal markers and treats axis predicates as explicit' do
|
||||
public_tag = create(:tag, primary_name: '********')
|
||||
TagVersion.create!(tag: public_tag, version_no: 1, event_type: 'create',
|
||||
name: '********', category: 'general', aliases: '', parent_tag_ids: '',
|
||||
names: { ja: { name: '********', script_code: 'Jpan', auto_generated: false,
|
||||
aliases: [] } }, created_at: Time.utc(2023))
|
||||
expect(query['count']).to eq(3)
|
||||
['eq', 'ne', 'like', 'in', 'not_in'].each do |operator|
|
||||
expected = ['ne', 'not_in'].include?(operator) ? 0 : 1
|
||||
expect(query(predicate('name', operator, '********'))['count']).to eq(expected)
|
||||
end
|
||||
blank = predicate('aliases.name', 'eq', '').merge('conditions.0.blank' => '1')
|
||||
expect(query(blank)['count']).to eq(1)
|
||||
['********', '', nil].each do |value|
|
||||
condition = { 'conditions' => '', 'conditions.0.field' => 'name',
|
||||
'conditions.0.operator' => 'axis_eq',
|
||||
'conditions.0.value_type' => value.nil? ? 'null' : 'string' }
|
||||
condition['conditions.0.value'] = value unless value.nil?
|
||||
expected = value == '********' ? 1 : 0
|
||||
expect(query(condition)['count']).to eq(expected)
|
||||
drill = { 'drill' => '', 'drill.0.field' => 'name',
|
||||
'drill.0.value_type' => value.nil? ? 'null' : 'string' }
|
||||
drill['drill.0.value'] = value unless value.nil?
|
||||
expect(query(drill)['count']).to eq(expected)
|
||||
end
|
||||
end
|
||||
|
||||
it 'keeps public identity and timeline predicates searchable' do
|
||||
expect(query['count']).to eq(2)
|
||||
values = { 'resource_id' => tag.id.to_s, 'version_no' => '2',
|
||||
'event_type' => 'discard', 'user' => actor.id.to_s,
|
||||
'created_at' => '2023-02-01T00:00:00Z' }
|
||||
values.each do |field, value|
|
||||
expected = ['resource_id', 'user'].include?(field) ? 2 : 1
|
||||
expect(query(predicate(field, 'eq', value))['count']).to eq(expected)
|
||||
end
|
||||
condition = predicate('name', 'ne', '秘密のタグ')
|
||||
.merge(predicate('event_type', 'eq', 'discard', 1))
|
||||
expect(query(condition)['count']).to eq(0)
|
||||
expect(query(predicate('category', 'eq', '********'))['count']).to eq(0)
|
||||
expect(query(predicate('category', 'eq', 'general'))['count']).to eq(0)
|
||||
end
|
||||
|
||||
it 'keeps public predicates available after combining multiple conditions' do
|
||||
condition = predicate('resource_id', 'eq', tag.id.to_s)
|
||||
.merge(predicate('event_type', 'eq', 'discard', 1))
|
||||
expect(query(condition)['count']).to eq(1)
|
||||
condition.merge!(predicate('name', 'ne', 'unrelated', 2))
|
||||
expect(query(condition)['count']).to eq(0)
|
||||
end
|
||||
|
||||
it 'keeps real name searches for administrators and excludes private details' do
|
||||
condition = predicate('name', 'eq', '秘密のタグ')
|
||||
details = query(condition.merge('mode' => 'details'))
|
||||
expect(details['versions']).to be_empty
|
||||
expect(details['output_count']).to eq(0)
|
||||
masked = query(predicate('name', 'eq', '********').merge('mode' => 'details'))
|
||||
expect(masked['versions']).to be_empty
|
||||
expect(masked['output_count']).to eq(0)
|
||||
sign_in_as(admin)
|
||||
expect(query(condition)['count']).to eq(2)
|
||||
expect(query(condition.merge('mode' => 'details'))['versions'].length).to eq(2)
|
||||
end
|
||||
|
||||
it 'rejects private names reached through a post tag snapshot' do
|
||||
post = create(:post)
|
||||
PostVersion.create!(post:, version_no: 1, event_type: 'create', title: 'post',
|
||||
url: post.url, tags: '', parent_post_ids: '',
|
||||
tags_json: [{ tag_id: tag.id, version_no: 1, name: '秘密のタグ',
|
||||
category: 'general', sections: [] }],
|
||||
created_at: Time.utc(2024))
|
||||
['tags.name', 'tags.names.name', 'tags.name_ja'].each do |field|
|
||||
['eq', 'ne', 'like', 'any', 'all', 'none'].each do |operator|
|
||||
['秘密のタグ', '********', '%'].each do |value|
|
||||
settings = predicate(field, operator, value).merge('resource' => 'posts')
|
||||
expect(query(settings)['count']).to eq(0)
|
||||
end
|
||||
end
|
||||
blank = predicate(field, 'eq', '').merge('resource' => 'posts',
|
||||
'conditions.0.blank' => '1')
|
||||
expect(query(blank)['count']).to eq(0)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -21,7 +21,7 @@ RSpec.describe 'Counts table API', type: :request do
|
||||
names: { ja: { name: 'alpha', script_code: 'Jpan', auto_generated: false, aliases: [] } },
|
||||
created_at: Time.utc(2023))
|
||||
NicoTagVersion.create!(external_tag: external, version_no: 1,
|
||||
event_type: 'create', name: 'beta', linked_tags: '', created_at: Time.utc(2023))
|
||||
event_type: 'create', linked_tag_ids: '', created_at: Time.utc(2023))
|
||||
snapshots = [
|
||||
{ tag_id: tag.id, version_no: 1, name: 'alpha', category: 'general', sections: [] },
|
||||
{ external_tag_id: external.id }]
|
||||
@@ -54,6 +54,115 @@ RSpec.describe 'Counts table API', type: :request do
|
||||
|
||||
def versions(result) = result.fetch('versions').map { _1.fetch('version_no') }
|
||||
|
||||
context 'nico link ID snapshots through the V1 field name' do
|
||||
let(:base) do
|
||||
super().merge(resource: 'nico_tags')
|
||||
end
|
||||
|
||||
before do
|
||||
NicoTagVersion.create!(external_tag: external, version_no: 2,
|
||||
event_type: 'update', linked_tag_ids: tag.id.to_s,
|
||||
created_at: Time.utc(2024, 1, 1))
|
||||
NicoTagVersion.create!(external_tag: external, version_no: 3,
|
||||
event_type: 'update', linked_tag_ids: '',
|
||||
created_at: Time.utc(2024, 2, 1))
|
||||
end
|
||||
|
||||
it 'returns ID values under linked_tags without exposing the physical column key' do
|
||||
result = query
|
||||
expect(versions(result)).to eq([1, 2, 3])
|
||||
expect(result['versions'].map { _1.fetch('linked_tags') }).to eq(['', tag.id.to_s, ''])
|
||||
result['versions'].each do |row|
|
||||
expect(row).not_to have_key('linked_tag_ids')
|
||||
expect(row.fetch('name')).to eq('nico:beta')
|
||||
end
|
||||
end
|
||||
|
||||
it 'preloads external names separately for aliased link-change details' do
|
||||
statements = []
|
||||
listener = lambda do |_name, _start, _finish, _id, payload|
|
||||
statements << payload[:sql]
|
||||
end
|
||||
ActiveSupport::Notifications.subscribed(listener, 'sql.active_record') do
|
||||
result = query(axis('changed_field', 'linked_tags'))
|
||||
expect(versions(result)).to eq([2, 3])
|
||||
expect(result.fetch('versions').map { _1.fetch('name') })
|
||||
.to eq(['nico:beta', 'nico:beta'])
|
||||
end
|
||||
|
||||
details = statements.find { _1.include?('AS counts_latest') }
|
||||
expect(details).not_to be_nil
|
||||
expect(details).not_to include('JOIN `external_tags`')
|
||||
names = statements.select { _1.match?(/SELECT .* FROM `external_tags`/) }
|
||||
expect(names.length).to eq(1)
|
||||
end
|
||||
|
||||
it 'advertises the legacy field, label and default columns only' do
|
||||
get '/counts/definitions/nico_tags', params: { locale: 'ja' }
|
||||
expect(response).to have_http_status(:ok)
|
||||
body = response.parsed_body
|
||||
['fields', 'filter_fields', 'output_fields'].each do |collection|
|
||||
keys = body.fetch(collection).map { _1.fetch('key') }
|
||||
expect(keys).to include('linked_tags')
|
||||
expect(keys).not_to include('linked_tag_ids')
|
||||
end
|
||||
field = body.fetch('output_fields').find { _1['key'] == 'linked_tags' }
|
||||
expect(field.fetch('label')).to eq('counts:attributes.linked_tags')
|
||||
body.fetch('axes').each_value do |keys|
|
||||
expect(keys).to include('linked_tags')
|
||||
expect(keys).not_to include('linked_tag_ids')
|
||||
end
|
||||
body.fetch('defaults').select { _1['mode'] == 'details' }.each do |entry|
|
||||
settings = entry.fetch('params').to_h { [_1.fetch('key'), _1.fetch('value')] }
|
||||
expect(settings.fetch('columns').split).to include('name', 'linked_tags')
|
||||
expect(settings.fetch('columns').split).not_to include('linked_tag_ids')
|
||||
end
|
||||
end
|
||||
|
||||
it 'filters IDs and legacy drills and compares link changes through linked_tags' do
|
||||
predicate = { conditions: '', 'conditions.0.field' => 'linked_tags',
|
||||
'conditions.0.operator' => 'eq', 'conditions.0.value' => tag.id.to_s }
|
||||
expect(versions(query(predicate))).to eq([2])
|
||||
expect(versions(query(axis('linked_tags', tag.id.to_s, root: 'drill')))).to eq([2])
|
||||
expect(versions(query(axis('changed_field', 'linked_tags')))).to eq([2, 3])
|
||||
expect(versions(query(predicate.merge('conditions.0.value' => 'alpha')))).to be_empty
|
||||
end
|
||||
|
||||
it 'sorts ID snapshots through the public field in both directions' do
|
||||
expect(versions(query(sort: 'linked_tags:asc version_no:asc'))).to eq([1, 3, 2])
|
||||
expect(versions(query(sort: 'linked_tags:desc version_no:asc'))).to eq([2, 1, 3])
|
||||
end
|
||||
|
||||
it 'restores historical link IDs before applying a latest snapshot filter' do
|
||||
predicate = { definition: 'latest', at: '2024-01-15T00:00:00Z',
|
||||
conditions: '', 'conditions.0.field' => 'linked_tags',
|
||||
'conditions.0.operator' => 'eq', 'conditions.0.value' => tag.id.to_s }
|
||||
expect(versions(query(predicate))).to eq([2])
|
||||
expect(versions(query(predicate.except(:at)))).to be_empty
|
||||
end
|
||||
|
||||
it 'groups by linked_tags ID values and accepts the resulting drill condition' do
|
||||
sign_in_as(create(:user, :admin))
|
||||
result = query(mode: 'summary', rows: 'linked_tags', cols: '', sort: '')
|
||||
expect(result.fetch('cells')).to match_array([
|
||||
{ 'values' => [''], 'count' => 2 },
|
||||
{ 'values' => [tag.id.to_s], 'count' => 1 }])
|
||||
expect(versions(query(axis('linked_tags', tag.id.to_s)))).to eq([2])
|
||||
end
|
||||
|
||||
it 'rejects the physical column as a public filter, axis, sort or change field' do
|
||||
sign_in_as(create(:user, :admin))
|
||||
[
|
||||
axis('linked_tag_ids', tag.id.to_s),
|
||||
axis('changed_field', 'linked_tag_ids'),
|
||||
{ mode: 'summary', rows: 'linked_tag_ids', cols: '', sort: '' },
|
||||
{ sort: 'linked_tag_ids:asc' }].each do |invalid|
|
||||
get '/counts/query', params: base.merge(invalid)
|
||||
expect(response).to have_http_status(:bad_request), invalid.inspect
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
it 'identifies a totals timeout without exposing SQL in the error response' do
|
||||
allow_any_instance_of(CountsPivotTotals).to receive(:call)
|
||||
.and_raise(CountsPivotTotals::TimedOut, 'private SQL')
|
||||
@@ -233,11 +342,10 @@ RSpec.describe 'Counts table API', type: :request do
|
||||
expect(versions(query(tags: 'renamed-alpha'))).to eq([1, 2, 4])
|
||||
end
|
||||
|
||||
it 'resolves external names from current registrations too' do
|
||||
external.update!(name: 'renamed-beta')
|
||||
expect(versions(query(tags: 'nico:renamed-beta'))).to eq([1])
|
||||
expect(versions(query(tags: 'nico:beta'))).to eq([])
|
||||
expect(versions(query(tags: 'not:nico:renamed-beta'))).to eq([2, 3, 4])
|
||||
it 'resolves immutable external names across historical snapshots' do
|
||||
expect(versions(query(tags: 'nico:beta'))).to eq([1])
|
||||
expect(versions(query(tags: 'nico:renamed-beta'))).to eq([])
|
||||
expect(versions(query(tags: 'not:nico:beta'))).to eq([2, 3, 4])
|
||||
end
|
||||
end
|
||||
|
||||
@@ -372,15 +480,25 @@ RSpec.describe 'Counts table API', type: :request do
|
||||
expect(result['cells'].sum { _1['count'] }).to eq(4)
|
||||
expect(response.body).not_to include('hidden title')
|
||||
masked = query(mode: 'summary', rows: 'resource_id', cols: '')
|
||||
expect(masked['cells'].map { _1['values'] }).to eq([[CountsAxes::MASKED_VALUE]])
|
||||
expect(masked['cells'].map { _1['values'] }).to eq([[record.id]])
|
||||
expect(masked['cells'].first['count']).to eq(4)
|
||||
expect(query['versions']).to be_empty
|
||||
end
|
||||
|
||||
it 'rejects private summary predicates instead of exposing a count oracle' do
|
||||
get '/counts/query', params: base.merge(mode: 'summary', resource_id: record.id.to_s)
|
||||
expect(response).to have_http_status(:forbidden)
|
||||
expect(json).to eq('error' => 'private_condition')
|
||||
it 'allows public identity predicates without exposing private snapshot fields' do
|
||||
PostVersion.create!(post: record, version_no: 4, event_type: 'discard',
|
||||
url: record.url, title: 'hidden title', tags: '', tags_json: [],
|
||||
parent_post_ids: '', created_at: Time.utc(2024, 3))
|
||||
|
||||
get '/counts/query', params: base.merge(mode: 'summary',
|
||||
resource_id: record.id.to_s, rows: 'resource_id', cols: 'title')
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
expect(json['count']).to eq(4)
|
||||
expect(json['cells'].sum { _1['count'] }).to eq(4)
|
||||
expect(json['cells'].map { _1['values'].last })
|
||||
.to include(CountsAxes::MASKED_VALUE)
|
||||
expect(response.body).not_to include('hidden title')
|
||||
end
|
||||
|
||||
it 'reports the combined axis limit without replacing it with a generic 400' do
|
||||
|
||||
@@ -38,12 +38,9 @@ RSpec.describe 'NicoTags', type: :request do
|
||||
end
|
||||
|
||||
it 'filters by nico tag name, linked tag name, and link status' do
|
||||
linked = create(:external_tag)
|
||||
linked.update!(name: 'search_linked')
|
||||
unlinked = create(:external_tag)
|
||||
unlinked.update!(name: 'search_unlinked')
|
||||
other = create(:external_tag)
|
||||
other.update!(name: 'other')
|
||||
linked = create(:external_tag, name: 'search_linked')
|
||||
unlinked = create(:external_tag, name: 'search_unlinked')
|
||||
other = create(:external_tag, name: 'other')
|
||||
destination = create(:tag, category: :general)
|
||||
primary_tag_name_for(destination, 'ja').update!(name: 'destination_search')
|
||||
NicoTagRelation.create!(nico_tag: linked, tag: destination)
|
||||
@@ -80,11 +77,9 @@ RSpec.describe 'NicoTags', type: :request do
|
||||
end
|
||||
|
||||
it 'sorts by name and timestamps' do
|
||||
older = create(:external_tag)
|
||||
older.update!(name: 'ordered_a')
|
||||
older = create(:external_tag, name: 'ordered_a')
|
||||
older.update_columns(created_at: 2.days.ago)
|
||||
newer = create(:external_tag)
|
||||
newer.update!(name: 'ordered_b')
|
||||
newer = create(:external_tag, name: 'ordered_b')
|
||||
newer.update_columns(created_at: 1.day.ago)
|
||||
older_post_tag =
|
||||
PostExternalTag.create!(post: create(:post), external_tag: older)
|
||||
@@ -194,9 +189,8 @@ RSpec.describe 'NicoTags', type: :request do
|
||||
|
||||
versions = nico_tag.reload.nico_tag_versions.order(:version_no)
|
||||
expect(versions.map(&:event_type)).to eq(['create', 'update'])
|
||||
expect(versions.last.linked_tags.split).to match_array([
|
||||
'nico_linked_a',
|
||||
'nico_linked_b'])
|
||||
expect(versions.last.linked_tag_ids.split.map(&:to_i))
|
||||
.to match_array([linked_a.id, linked_b.id])
|
||||
expect(versions.last.created_by_user_id).to eq(admin.id)
|
||||
end
|
||||
|
||||
@@ -215,7 +209,7 @@ RSpec.describe 'NicoTags', type: :request do
|
||||
expect(json).to eq([])
|
||||
expect(nico_tag.reload.linked_tags).to be_empty
|
||||
expect(nico_tag.nico_tag_versions.order(:version_no).last)
|
||||
.to have_attributes(linked_tags: '', created_by_user: member)
|
||||
.to have_attributes(linked_tag_ids: '', created_by_user: member)
|
||||
end
|
||||
|
||||
it 'returns the tags field error when a nico tag is specified directly' do
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
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: '',
|
||||
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', '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)
|
||||
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
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -631,6 +631,37 @@ RSpec.describe 'Posts API', type: :request do
|
||||
create(:post_tag, post: plain_post, tag:, created_at: t3)
|
||||
end
|
||||
|
||||
it 'displays history time after pagination without joining history to posts or count' do
|
||||
queries = []
|
||||
subscriber = ->(event) { queries << event.payload.fetch(:sql) }
|
||||
ActiveSupport::Notifications.subscribed(subscriber, 'sql.active_record') do
|
||||
get '/posts', params: { url: '/version-time/', order: 'url:asc', limit: 1 }
|
||||
end
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
expect(json.fetch('posts').pluck('id')).to eq([history_post.id])
|
||||
expect(Time.zone.parse(json.fetch('posts').first.fetch('updated_at'))).to eq(t2)
|
||||
expect(json.fetch('count')).to eq(3)
|
||||
post_queries = queries.select { _1.include?('FROM `posts`') }
|
||||
expect(post_queries).not_to be_empty
|
||||
expect(post_queries.grep(/post_versions/)).to be_empty
|
||||
end
|
||||
|
||||
it 'omits the history aggregate from count when only sorting by update time' do
|
||||
queries = []
|
||||
subscriber = ->(event) { queries << event.payload.fetch(:sql) }
|
||||
ActiveSupport::Notifications.subscribed(subscriber, 'sql.active_record') do
|
||||
get '/posts', params: { url: '/version-time/', order: 'updated_at:desc', limit: 1 }
|
||||
end
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
expect(json.fetch('posts').pluck('id')).to eq([newer_post.id])
|
||||
expect(json.fetch('count')).to eq(3)
|
||||
count_queries = queries.select { _1.include?('FROM `posts`') && _1.match?(/COUNT\(/i) }
|
||||
expect(count_queries).not_to be_empty
|
||||
expect(count_queries.grep(/post_versions/)).to be_empty
|
||||
end
|
||||
|
||||
['asc', 'desc'].each do |direction|
|
||||
it "sorts by the later of post update and latest version time (#{ direction })" do
|
||||
get '/posts', params: { url: '/version-time/', order: "updated_at:#{ direction }" }
|
||||
@@ -660,6 +691,16 @@ RSpec.describe 'Posts API', type: :request do
|
||||
expect(json.fetch('posts').map { |item| item.fetch('id') }).to eq([history_post.id])
|
||||
expect(json.fetch('count')).to eq(1)
|
||||
end
|
||||
|
||||
it 'counts every timestamp match before pagination' do
|
||||
get '/posts', params: { url: '/version-time/', order: 'updated_at:asc',
|
||||
updated_from: t1.iso8601, updated_to: t3.iso8601,
|
||||
limit: 1, page: 2 }
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
expect(json.fetch('posts').pluck('id')).to eq([history_post.id])
|
||||
expect(json.fetch('count')).to eq(3)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when original_created_from/original_created_to are provided' do
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする