Merge branch 'main' into feature/332
このコミットが含まれているのは:
@@ -1,4 +1,5 @@
|
||||
require 'rails_helper'
|
||||
require 'base64'
|
||||
require 'set'
|
||||
|
||||
include ActiveSupport::Testing::TimeHelpers
|
||||
@@ -8,6 +9,11 @@ RSpec.describe 'Posts API', type: :request do
|
||||
# resized_thumbnail! が MiniMagick 依存でコケやすいので request spec ではスタブしとくのが無難。
|
||||
before do
|
||||
allow_any_instance_of(Post).to receive(:resized_thumbnail!).and_return(true)
|
||||
allow(Post).to receive(:resized_thumbnail_attachment).and_return(
|
||||
io: StringIO.new('dummy'),
|
||||
filename: 'resized_thumbnail.jpg',
|
||||
content_type: 'image/jpeg'
|
||||
)
|
||||
end
|
||||
|
||||
def create_nico_tag!(name)
|
||||
@@ -19,6 +25,18 @@ RSpec.describe 'Posts API', type: :request do
|
||||
Rack::Test::UploadedFile.new(StringIO.new('dummy'), 'image/jpeg', original_filename: 'dummy.jpg')
|
||||
end
|
||||
|
||||
def real_thumbnail_upload
|
||||
gif =
|
||||
Base64.decode64(
|
||||
'R0lGODdhAQABAIABAP///wAAACwAAAAAAQABAAACAkQBADs=')
|
||||
|
||||
Rack::Test::UploadedFile.new(
|
||||
StringIO.new(gif),
|
||||
'image/gif',
|
||||
original_filename: 'thumbnail.gif'
|
||||
)
|
||||
end
|
||||
|
||||
def post_write_params params = { }
|
||||
{ parent_post_ids: '' }.merge(params)
|
||||
end
|
||||
@@ -696,6 +714,66 @@ RSpec.describe 'Posts API', type: :request do
|
||||
expect(json['tags'][0]).to have_key('name')
|
||||
end
|
||||
|
||||
it '201 when posting manually with a thumbnail' do
|
||||
sign_in_as(member)
|
||||
allow(Post).to receive(:resized_thumbnail_attachment).and_call_original
|
||||
|
||||
post '/posts', params: post_write_params(
|
||||
title: 'thumbnail post',
|
||||
url: 'https://example.com/thumbnail-post',
|
||||
tags: 'spec_tag',
|
||||
thumbnail: real_thumbnail_upload
|
||||
)
|
||||
|
||||
expect(response).to have_http_status(:created)
|
||||
post_record = Post.find(json.fetch('id'))
|
||||
expect(post_record.thumbnail).to be_attached
|
||||
expect(post_record.thumbnail.blob.content_type).to eq('image/jpeg')
|
||||
expect { post_record.thumbnail.download }.not_to raise_error
|
||||
end
|
||||
|
||||
it 'resizes the thumbnail before the create transaction begins' do
|
||||
sign_in_as(member)
|
||||
|
||||
open_transactions = []
|
||||
baseline_open_transactions = Post.connection.open_transactions
|
||||
allow(Post).to receive(:resized_thumbnail_attachment) do |_upload|
|
||||
open_transactions << Post.connection.open_transactions
|
||||
{ io: StringIO.new('dummy'),
|
||||
filename: 'resized_thumbnail.jpg',
|
||||
content_type: 'image/jpeg' }
|
||||
end
|
||||
|
||||
post '/posts', params: post_write_params(
|
||||
title: 'transaction post',
|
||||
url: 'https://example.com/transaction-post',
|
||||
tags: 'spec_tag',
|
||||
thumbnail: dummy_upload
|
||||
)
|
||||
|
||||
expect(response).to have_http_status(:created)
|
||||
expect(open_transactions).to eq([baseline_open_transactions])
|
||||
end
|
||||
|
||||
it 'returns 422 and does not create a post when thumbnail resize fails' do
|
||||
sign_in_as(member)
|
||||
allow(Post).to receive(:resized_thumbnail_attachment).and_raise(MiniMagick::Error)
|
||||
|
||||
expect {
|
||||
post '/posts', params: post_write_params(
|
||||
title: 'broken thumbnail post',
|
||||
url: 'https://example.com/broken-thumbnail-post',
|
||||
tags: 'spec_tag',
|
||||
thumbnail: dummy_upload
|
||||
)
|
||||
}.not_to change(Post, :count)
|
||||
|
||||
expect(response).to have_http_status(:unprocessable_entity)
|
||||
expect(json.fetch('errors')).to include(
|
||||
'thumbnail' => ['サムネイル画像の変換に失敗しました.']
|
||||
)
|
||||
end
|
||||
|
||||
it '201 and creates post + tags when member and tags have aliases' do
|
||||
sign_in_as(member)
|
||||
|
||||
|
||||
@@ -581,6 +581,58 @@ RSpec.describe 'Tags API', type: :request do
|
||||
expect(response).to have_http_status(:ok)
|
||||
expect(wiki_page.reload.wiki_versions.count).to eq(before_wiki_version_count)
|
||||
end
|
||||
|
||||
it 'full update で旧名 alias が残った tag を PATCH で旧名へ戻せる' do
|
||||
put "/tags/#{ tag.id }", params: {
|
||||
name: 'patch_roundtrip_target',
|
||||
category: 'general',
|
||||
aliases: 'unko',
|
||||
parent_tags: '',
|
||||
deprecated: '0',
|
||||
}
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
expect(TagName.find_by!(name: 'spec_tag').canonical).to eq(tag.reload.tag_name)
|
||||
|
||||
patch "/tags/#{ tag.id }", params: { name: 'spec_tag' }
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
|
||||
tag.reload
|
||||
|
||||
expect(tag.name).to eq('spec_tag')
|
||||
expect(tag.tag_name.canonical_id).to be_nil
|
||||
expect(TagName.find_by!(name: 'patch_roundtrip_target').canonical).to eq(tag.tag_name)
|
||||
end
|
||||
|
||||
it '別 tag の正規名には変更できない' do
|
||||
wiki_page =
|
||||
Wiki::Commit.create_content!(
|
||||
tag_name: tag.tag_name,
|
||||
body: 'patch collision wiki',
|
||||
created_by_user: member_user,
|
||||
message: 'init')
|
||||
|
||||
patch "/tags/#{ tag.id }", params: { name: 'unknown' }
|
||||
|
||||
expect(response).to have_http_status(:unprocessable_entity)
|
||||
expect(json.fetch('errors')).to include(
|
||||
'name' => ['その名前は既に使はれてゐます.']
|
||||
)
|
||||
|
||||
expect(tag.reload.name).to eq('spec_tag')
|
||||
expect(tag.tag_name.aliases.map(&:name)).to contain_exactly('unko')
|
||||
expect(wiki_page.reload.tag_name).to eq(tag.tag_name)
|
||||
end
|
||||
|
||||
it 'system tag の name は変更できない' do
|
||||
system_tag = Tag.bot
|
||||
|
||||
patch "/tags/#{ system_tag.id }", params: { name: 'patch_system_tag_renamed' }
|
||||
|
||||
expect(response).to have_http_status(:unprocessable_entity)
|
||||
expect(system_tag.reload.name).to eq('bot操作')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1039,6 +1091,42 @@ RSpec.describe 'Tags API', type: :request do
|
||||
expect(versions.second.created_by_user_id).to eq(member_user.id)
|
||||
end
|
||||
|
||||
it '同じ tag の旧 alias へ戻しても rename できる' do
|
||||
put "/tags/#{ tag.id }", params: {
|
||||
name: 'put_roundtrip_b',
|
||||
category: 'general',
|
||||
aliases: 'unko',
|
||||
parent_tags: '',
|
||||
deprecated: '0',
|
||||
}
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
|
||||
put "/tags/#{ tag.id }", params: {
|
||||
name: 'spec_tag',
|
||||
category: 'general',
|
||||
aliases: 'unko',
|
||||
parent_tags: '',
|
||||
deprecated: '0',
|
||||
}
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
|
||||
tag.reload
|
||||
|
||||
expect(tag.name).to eq('spec_tag')
|
||||
expect(TagName.find_by!(name: 'put_roundtrip_b').canonical).to eq(tag.tag_name)
|
||||
expect(tag.tag_name.aliases.map(&:name)).to contain_exactly('put_roundtrip_b', 'unko')
|
||||
expect(tag.tag_name.aliases.map(&:name)).not_to include('spec_tag')
|
||||
expect(alias_tn.reload.canonical).to eq(tag.tag_name)
|
||||
|
||||
version = tag.tag_versions.order(:version_no).last
|
||||
|
||||
expect(version.event_type).to eq('update')
|
||||
expect(version.name).to eq('spec_tag')
|
||||
expect(version.aliases.split).to contain_exactly('put_roundtrip_b', 'unko')
|
||||
end
|
||||
|
||||
it 'parent tag の snapshot も作成する' do
|
||||
old_parent = Tag.create!(
|
||||
tag_name: TagName.create!(name: 'put_snapshot_old_parent'),
|
||||
@@ -1163,6 +1251,48 @@ RSpec.describe 'Tags API', type: :request do
|
||||
)
|
||||
end
|
||||
|
||||
it 'wiki を持つ tag を旧 alias へ戻しても wiki を新 canonical へ移す' do
|
||||
wiki_page =
|
||||
Wiki::Commit.create_content!(
|
||||
tag_name: tag.tag_name,
|
||||
body: 'wiki body before',
|
||||
created_by_user: member_user,
|
||||
message: 'init')
|
||||
|
||||
expect {
|
||||
put "/tags/#{ tag.id }", params: {
|
||||
name: 'put_wiki_roundtrip_b',
|
||||
category: 'general',
|
||||
aliases: 'unko',
|
||||
parent_tags: '',
|
||||
deprecated: '0',
|
||||
}
|
||||
|
||||
put "/tags/#{ tag.id }", params: {
|
||||
name: 'spec_tag',
|
||||
category: 'general',
|
||||
aliases: 'unko',
|
||||
parent_tags: '',
|
||||
deprecated: '0',
|
||||
}
|
||||
}
|
||||
.to change(TagVersion, :count).by(3)
|
||||
.and change(WikiVersion, :count).by(2)
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
|
||||
tag.reload
|
||||
|
||||
expect(wiki_page.reload.tag_name).to eq(tag.tag_name)
|
||||
expect(TagName.find_by!(name: 'put_wiki_roundtrip_b').wiki_page).to be_nil
|
||||
expect(TagName.find_by!(name: 'put_wiki_roundtrip_b').canonical).to eq(tag.tag_name)
|
||||
|
||||
versions = wiki_page.wiki_versions.order(:version_no).last(2)
|
||||
|
||||
expect(versions.map(&:event_type)).to eq(['update', 'update'])
|
||||
expect(versions.map(&:title)).to eq(['put_wiki_roundtrip_b', 'spec_tag'])
|
||||
end
|
||||
|
||||
it '別名を他 tag から奪った場合、奪はれた側の tag version も作成する' do
|
||||
old_owner = Tag.create!(
|
||||
tag_name: TagName.create!(name: 'put_alias_old_owner'),
|
||||
@@ -1201,6 +1331,48 @@ RSpec.describe 'Tags API', type: :request do
|
||||
expect(old_owner_versions.second.aliases.split).not_to include('put_stolen_alias')
|
||||
end
|
||||
|
||||
it '別 tag の alias 名を rename で奪へる' do
|
||||
old_owner = Tag.create!(
|
||||
tag_name: TagName.create!(name: 'put_alias_collision_owner'),
|
||||
category: :general
|
||||
)
|
||||
stolen_alias = TagName.create!(
|
||||
name: 'put_alias_collision_name',
|
||||
canonical: old_owner.tag_name
|
||||
)
|
||||
wiki_page =
|
||||
Wiki::Commit.create_content!(
|
||||
tag_name: tag.tag_name,
|
||||
body: 'put collision wiki',
|
||||
created_by_user: member_user,
|
||||
message: 'init')
|
||||
|
||||
put "/tags/#{ tag.id }", params: {
|
||||
name: 'put_alias_collision_name',
|
||||
category: 'general',
|
||||
aliases: 'unko',
|
||||
parent_tags: '',
|
||||
deprecated: '0',
|
||||
}
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
|
||||
tag.reload
|
||||
old_owner.reload
|
||||
stolen_alias.reload
|
||||
|
||||
expect(tag.name).to eq('put_alias_collision_name')
|
||||
expect(stolen_alias.canonical_id).to be_nil
|
||||
expect(TagName.find_by!(name: 'spec_tag').canonical).to eq(tag.tag_name)
|
||||
expect(old_owner.tag_name.aliases.map(&:name)).not_to include('put_alias_collision_name')
|
||||
|
||||
old_owner_versions = old_owner.tag_versions.order(:version_no)
|
||||
|
||||
expect(old_owner_versions.last.event_type).to eq('update')
|
||||
expect(old_owner_versions.last.aliases.split).not_to include('put_alias_collision_name')
|
||||
expect(wiki_page.reload.tag_name).to eq(tag.tag_name)
|
||||
end
|
||||
|
||||
it 'parent_tags に指定すると循環する tag は 422 にする' do
|
||||
child = Tag.create!(
|
||||
tag_name: TagName.create!(name: 'put_cycle_child'),
|
||||
|
||||
新しい課題から参照
ユーザをブロックする