828 行
27 KiB
Ruby
828 行
27 KiB
Ruby
include ActiveSupport::Testing::TimeHelpers
|
|
|
|
require 'rails_helper'
|
|
require 'set'
|
|
|
|
RSpec.describe 'Posts API', type: :request do
|
|
# create / update で thumbnail.attach は走るが、
|
|
# resized_thumbnail! が MiniMagick 依存でコケやすいので request spec ではスタブしとくのが無難。
|
|
before do
|
|
allow_any_instance_of(Post).to receive(:resized_thumbnail!).and_return(true)
|
|
end
|
|
|
|
def dummy_upload
|
|
# 中身は何でもいい(加工処理はスタブしてる)
|
|
Rack::Test::UploadedFile.new(StringIO.new('dummy'), 'image/jpeg', original_filename: 'dummy.jpg')
|
|
end
|
|
|
|
let!(:tag_name) { TagName.create!(name: 'spec_tag') }
|
|
let!(:tag) { Tag.create!(tag_name: tag_name, category: :general) }
|
|
|
|
let!(:post_record) do
|
|
Post.create!(title: 'spec post', url: 'https://example.com/spec').tap do |p|
|
|
PostTag.create!(post: p, tag: tag)
|
|
end
|
|
end
|
|
|
|
describe "GET /posts" do
|
|
let!(:user) { create_member_user! }
|
|
|
|
let!(:tag_name) { TagName.create!(name: "spec_tag") }
|
|
let!(:tag) { Tag.create!(tag_name:, category: :general) }
|
|
let!(:tag_name2) { TagName.create!(name: 'unko') }
|
|
let!(:tag2) { Tag.create!(tag_name: tag_name2, category: :deerjikist) }
|
|
let!(:alias_tag_name) { TagName.create!(name: 'manko', canonical: tag_name) }
|
|
|
|
let!(:hit_post) do
|
|
Post.create!(uploaded_user: user, title: "hello spec world",
|
|
url: 'https://example.com/spec2').tap do |p|
|
|
PostTag.create!(post: p, tag:)
|
|
end
|
|
end
|
|
|
|
let!(:miss_post) do
|
|
Post.create!(uploaded_user: user, title: "unrelated title",
|
|
url: 'https://example.com/spec3').tap do |p|
|
|
PostTag.create!(post: p, tag: tag2)
|
|
end
|
|
end
|
|
|
|
it "returns posts with tag name in JSON" do
|
|
get "/posts"
|
|
|
|
expect(response).to have_http_status(:ok)
|
|
posts = json.fetch("posts")
|
|
|
|
# 全postの全tagが name を含むこと
|
|
expect(posts).not_to be_empty
|
|
posts.each do |p|
|
|
expect(p['tags']).to be_an(Array)
|
|
p['tags'].each do |t|
|
|
expect(t).to include('name', 'category', 'has_wiki')
|
|
end
|
|
end
|
|
expect(json['count']).to be_an(Integer)
|
|
|
|
# spec_tag を含む投稿が存在すること
|
|
all_tag_names = posts.flat_map { |p| p["tags"].map { |t| t["name"] } }
|
|
expect(all_tag_names).to include("spec_tag")
|
|
end
|
|
|
|
context "when q is provided" do
|
|
it "filters posts by q (hit case)" do
|
|
get "/posts", params: { tags: "spec_tag" }
|
|
|
|
expect(response).to have_http_status(:ok)
|
|
posts = json.fetch('posts')
|
|
ids = posts.map { |p| p['id'] }
|
|
|
|
expect(ids).to include(hit_post.id)
|
|
expect(ids).not_to include(miss_post.id)
|
|
expect(json['count']).to be_an(Integer)
|
|
|
|
posts.each do |p|
|
|
expect(p['tags']).to be_an(Array)
|
|
p['tags'].each do |t|
|
|
expect(t).to include('name', 'category', 'has_wiki')
|
|
end
|
|
end
|
|
end
|
|
|
|
it "filters posts by q (hit case by alias)" do
|
|
get "/posts", params: { tags: "manko" }
|
|
|
|
expect(response).to have_http_status(:ok)
|
|
posts = json.fetch('posts')
|
|
ids = posts.map { |p| p['id'] }
|
|
|
|
expect(ids).to include(hit_post.id)
|
|
expect(ids).not_to include(miss_post.id)
|
|
expect(json['count']).to be_an(Integer)
|
|
|
|
posts.each do |p|
|
|
expect(p['tags']).to be_an(Array)
|
|
p['tags'].each do |t|
|
|
expect(t).to include('name', 'category', 'has_wiki')
|
|
end
|
|
end
|
|
end
|
|
|
|
it "returns empty posts when nothing matches" do
|
|
get "/posts", params: { tags: "no_such_keyword_12345" }
|
|
|
|
expect(response).to have_http_status(:ok)
|
|
expect(json.fetch("posts")).to eq([])
|
|
expect(json.fetch('count')).to eq(0)
|
|
end
|
|
end
|
|
|
|
context 'when tags contain not:' do
|
|
let!(:foo_tag_name) { TagName.create!(name: 'not_spec_foo') }
|
|
let!(:foo_tag) { Tag.create!(tag_name: foo_tag_name, category: :general) }
|
|
|
|
let!(:bar_tag_name) { TagName.create!(name: 'not_spec_bar') }
|
|
let!(:bar_tag) { Tag.create!(tag_name: bar_tag_name, category: :general) }
|
|
|
|
let!(:baz_tag_name) { TagName.create!(name: 'not_spec_baz') }
|
|
let!(:baz_tag) { Tag.create!(tag_name: baz_tag_name, category: :general) }
|
|
|
|
let!(:foo_alias_tag_name) do
|
|
TagName.create!(name: 'not_spec_foo_alias', canonical: foo_tag_name)
|
|
end
|
|
|
|
let!(:foo_only_post) do
|
|
Post.create!(uploaded_user: user, title: 'foo only',
|
|
url: 'https://example.com/not-spec-foo').tap do |p|
|
|
PostTag.create!(post: p, tag: foo_tag)
|
|
end
|
|
end
|
|
|
|
let!(:bar_only_post) do
|
|
Post.create!(uploaded_user: user, title: 'bar only',
|
|
url: 'https://example.com/not-spec-bar').tap do |p|
|
|
PostTag.create!(post: p, tag: bar_tag)
|
|
end
|
|
end
|
|
|
|
let!(:baz_only_post) do
|
|
Post.create!(uploaded_user: user, title: 'baz only',
|
|
url: 'https://example.com/not-spec-baz').tap do |p|
|
|
PostTag.create!(post: p, tag: baz_tag)
|
|
end
|
|
end
|
|
|
|
let!(:foo_bar_post) do
|
|
Post.create!(uploaded_user: user, title: 'foo bar',
|
|
url: 'https://example.com/not-spec-foo-bar').tap do |p|
|
|
PostTag.create!(post: p, tag: foo_tag)
|
|
PostTag.create!(post: p, tag: bar_tag)
|
|
end
|
|
end
|
|
|
|
let!(:foo_baz_post) do
|
|
Post.create!(uploaded_user: user, title: 'foo baz',
|
|
url: 'https://example.com/not-spec-foo-baz').tap do |p|
|
|
PostTag.create!(post: p, tag: foo_tag)
|
|
PostTag.create!(post: p, tag: baz_tag)
|
|
end
|
|
end
|
|
|
|
let(:controlled_ids) do
|
|
[foo_only_post.id, bar_only_post.id, baz_only_post.id,
|
|
foo_bar_post.id, foo_baz_post.id]
|
|
end
|
|
|
|
it 'supports not search' do
|
|
get '/posts', params: { tags: 'not:not_spec_foo' }
|
|
|
|
expect(response).to have_http_status(:ok)
|
|
|
|
expect(json.fetch('posts').map { |p| p['id'] } & controlled_ids).to match_array(
|
|
[bar_only_post.id, baz_only_post.id]
|
|
)
|
|
end
|
|
|
|
it 'supports alias in not search' do
|
|
get '/posts', params: { tags: 'not:not_spec_foo_alias' }
|
|
|
|
expect(response).to have_http_status(:ok)
|
|
|
|
expect(json.fetch('posts').map { |p| p['id'] } & controlled_ids).to match_array(
|
|
[bar_only_post.id, baz_only_post.id]
|
|
)
|
|
end
|
|
|
|
it 'treats multiple not terms as AND when match is omitted' do
|
|
get '/posts', params: { tags: 'not:not_spec_foo not:not_spec_bar' }
|
|
|
|
expect(response).to have_http_status(:ok)
|
|
|
|
expect(json.fetch('posts').map { |p| p['id'] } & controlled_ids).to match_array(
|
|
[baz_only_post.id]
|
|
)
|
|
end
|
|
|
|
it 'treats multiple not terms as OR when match=any' do
|
|
get '/posts', params: { tags: 'not:not_spec_foo not:not_spec_bar', match: 'any' }
|
|
|
|
expect(response).to have_http_status(:ok)
|
|
|
|
expect(json.fetch('posts').map { |p| p['id'] } & controlled_ids).to match_array(
|
|
[foo_only_post.id, bar_only_post.id, baz_only_post.id, foo_baz_post.id]
|
|
)
|
|
end
|
|
|
|
it 'supports mixed positive and negative search with AND' do
|
|
get '/posts', params: { tags: 'not_spec_foo not:not_spec_bar' }
|
|
|
|
expect(response).to have_http_status(:ok)
|
|
|
|
expect(json.fetch('posts').map { |p| p['id'] } & controlled_ids).to match_array(
|
|
[foo_only_post.id, foo_baz_post.id]
|
|
)
|
|
end
|
|
|
|
it 'supports mixed positive and negative search with OR when match=any' do
|
|
get '/posts', params: { tags: 'not_spec_foo not:not_spec_bar', match: 'any' }
|
|
|
|
expect(response).to have_http_status(:ok)
|
|
|
|
expect(json.fetch('posts').map { |p| p['id'] } & controlled_ids).to match_array(
|
|
[foo_only_post.id, baz_only_post.id, foo_bar_post.id, foo_baz_post.id]
|
|
)
|
|
end
|
|
end
|
|
|
|
context 'when url is provided' do
|
|
let!(:url_hit_post) do
|
|
Post.create!(uploaded_user: user, title: 'url hit',
|
|
url: 'https://example.com/needle-url-xyz').tap do |p|
|
|
PostTag.create!(post: p, tag:)
|
|
end
|
|
end
|
|
|
|
let!(:url_miss_post) do
|
|
Post.create!(uploaded_user: user, title: 'url miss',
|
|
url: 'https://example.com/other-url').tap do |p|
|
|
PostTag.create!(post: p, tag:)
|
|
end
|
|
end
|
|
|
|
it 'filters posts by url substring' do
|
|
get '/posts', params: { url: 'needle-url-xyz' }
|
|
|
|
expect(response).to have_http_status(:ok)
|
|
ids = json.fetch('posts').map { |p| p['id'] }
|
|
|
|
expect(ids).to include(url_hit_post.id)
|
|
expect(ids).not_to include(url_miss_post.id)
|
|
expect(json.fetch('count')).to eq(1)
|
|
end
|
|
end
|
|
|
|
context 'when title is provided' do
|
|
let!(:title_hit_post) do
|
|
Post.create!(uploaded_user: user, title: 'needle-title-xyz',
|
|
url: 'https://example.com/title-hit').tap do |p|
|
|
PostTag.create!(post: p, tag:)
|
|
end
|
|
end
|
|
|
|
let!(:title_miss_post) do
|
|
Post.create!(uploaded_user: user, title: 'other title',
|
|
url: 'https://example.com/title-miss').tap do |p|
|
|
PostTag.create!(post: p, tag:)
|
|
end
|
|
end
|
|
|
|
it 'filters posts by title substring' do
|
|
get '/posts', params: { title: 'needle-title-xyz' }
|
|
|
|
expect(response).to have_http_status(:ok)
|
|
ids = json.fetch('posts').map { |p| p['id'] }
|
|
|
|
expect(ids).to include(title_hit_post.id)
|
|
expect(ids).not_to include(title_miss_post.id)
|
|
expect(json.fetch('count')).to eq(1)
|
|
end
|
|
end
|
|
|
|
[:title, :url].each do |field|
|
|
context "when #{field} contains wildcards" do
|
|
let!(:search_posts) do
|
|
['needle%25end', 'needleX25end', 'needle_end', 'needleXend', 'needleABend'].map do |value|
|
|
Post.create!(uploaded_user: user, title: "prefix #{value} suffix",
|
|
url: "https://example.com/prefix-#{value}-suffix")
|
|
end
|
|
end
|
|
|
|
{
|
|
'needle%25end' => [0],
|
|
'needle_end' => [2],
|
|
'needle*end' => [0, 1, 2, 3, 4],
|
|
'needle?end' => [2, 3],
|
|
'needle%*end' => [0],
|
|
'needle_?nd' => [2]
|
|
}.each do |query, expected_indices|
|
|
it "matches #{query.inspect} using only * and ? as wildcards" do
|
|
get '/posts', params: { field => query }
|
|
|
|
expect(response).to have_http_status(:ok)
|
|
expected_ids = expected_indices.map { |i| search_posts[i].id }
|
|
expect(json.fetch('posts').map { |p| p['id'] }).to match_array(expected_ids)
|
|
expect(json.fetch('count')).to eq(expected_ids.size)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
context 'when created_from/created_to are provided' do
|
|
let(:t_created_hit) { Time.zone.local(2010, 1, 5, 12, 0, 0) }
|
|
let(:t_created_miss) { Time.zone.local(2012, 1, 5, 12, 0, 0) }
|
|
|
|
let!(:created_hit_post) do
|
|
travel_to(t_created_hit) do
|
|
Post.create!(uploaded_user: user, title: 'created hit',
|
|
url: 'https://example.com/created-hit').tap do |p|
|
|
PostTag.create!(post: p, tag:)
|
|
end
|
|
end
|
|
end
|
|
|
|
let!(:created_miss_post) do
|
|
travel_to(t_created_miss) do
|
|
Post.create!(uploaded_user: user, title: 'created miss',
|
|
url: 'https://example.com/created-miss').tap do |p|
|
|
PostTag.create!(post: p, tag:)
|
|
end
|
|
end
|
|
end
|
|
|
|
it 'filters posts by created_at range' do
|
|
get '/posts', params: {
|
|
created_from: Time.zone.local(2010, 1, 1, 0, 0, 0).iso8601,
|
|
created_to: Time.zone.local(2010, 12, 31, 23, 59, 59).iso8601
|
|
}
|
|
|
|
expect(response).to have_http_status(:ok)
|
|
ids = json.fetch('posts').map { |p| p['id'] }
|
|
|
|
expect(ids).to include(created_hit_post.id)
|
|
expect(ids).not_to include(created_miss_post.id)
|
|
expect(json.fetch('count')).to eq(1)
|
|
end
|
|
end
|
|
|
|
context 'when updated_from/updated_to are provided' do
|
|
let(:t0) { Time.zone.local(2011, 2, 1, 12, 0, 0) }
|
|
let(:t1) { Time.zone.local(2011, 2, 10, 12, 0, 0) }
|
|
|
|
let!(:updated_hit_post) do
|
|
p = nil
|
|
travel_to(t0) do
|
|
p = Post.create!(uploaded_user: user, title: 'updated hit',
|
|
url: 'https://example.com/updated-hit').tap do |pp|
|
|
PostTag.create!(post: pp, tag:)
|
|
end
|
|
end
|
|
travel_to(t1) do
|
|
p.update!(title: 'updated hit v2')
|
|
end
|
|
p
|
|
end
|
|
|
|
let!(:updated_miss_post) do
|
|
travel_to(Time.zone.local(2013, 1, 1, 12, 0, 0)) do
|
|
Post.create!(uploaded_user: user, title: 'updated miss',
|
|
url: 'https://example.com/updated-miss').tap do |p|
|
|
PostTag.create!(post: p, tag:)
|
|
end
|
|
end
|
|
end
|
|
|
|
it 'filters posts by updated_at range' do
|
|
get '/posts', params: {
|
|
updated_from: Time.zone.local(2011, 2, 5, 0, 0, 0).iso8601,
|
|
updated_to: Time.zone.local(2011, 2, 20, 23, 59, 59).iso8601
|
|
}
|
|
|
|
expect(response).to have_http_status(:ok)
|
|
ids = json.fetch('posts').map { |p| p['id'] }
|
|
|
|
expect(ids).to include(updated_hit_post.id)
|
|
expect(ids).not_to include(updated_miss_post.id)
|
|
expect(json.fetch('count')).to eq(1)
|
|
end
|
|
end
|
|
|
|
context 'when original_created_from/original_created_to are provided' do
|
|
# 注意: controller の現状ロジックに合わせてる
|
|
# original_created_from は `original_created_before > ?`
|
|
# original_created_to は `original_created_from <= ?`
|
|
|
|
let!(:oc_hit_post) do
|
|
Post.create!(uploaded_user: user, title: 'oc hit',
|
|
url: 'https://example.com/oc-hit',
|
|
original_created_from: Time.zone.local(2015, 1, 1, 0, 0, 0),
|
|
original_created_before: Time.zone.local(2015, 1, 10, 0, 0, 0)).tap do |p|
|
|
PostTag.create!(post: p, tag:)
|
|
end
|
|
end
|
|
|
|
# original_created_from の条件は「original_created_before > param」なので、
|
|
# before が param 以下になるようにする(ただし before >= from は守る)
|
|
let!(:oc_miss_post_for_from) do
|
|
Post.create!(
|
|
uploaded_user: user,
|
|
title: 'oc miss for from',
|
|
url: 'https://example.com/oc-miss-from',
|
|
original_created_from: Time.zone.local(2014, 12, 1, 0, 0, 0),
|
|
original_created_before: Time.zone.local(2015, 1, 1, 0, 0, 0)
|
|
).tap { |p| PostTag.create!(post: p, tag:) }
|
|
end
|
|
|
|
# original_created_to の条件は「original_created_from <= param」なので、
|
|
# from が param より後になるようにする(before >= from は守る)
|
|
let!(:oc_miss_post_for_to) do
|
|
Post.create!(
|
|
uploaded_user: user,
|
|
title: 'oc miss for to',
|
|
url: 'https://example.com/oc-miss-to',
|
|
original_created_from: Time.zone.local(2015, 2, 1, 0, 0, 0),
|
|
original_created_before: Time.zone.local(2015, 2, 10, 0, 0, 0)
|
|
).tap { |p| PostTag.create!(post: p, tag:) }
|
|
end
|
|
|
|
it 'filters posts by original_created_from (current controller behavior)' do
|
|
get '/posts', params: {
|
|
original_created_from: Time.zone.local(2015, 1, 5, 0, 0, 0).iso8601
|
|
}
|
|
|
|
expect(response).to have_http_status(:ok)
|
|
ids = json.fetch('posts').map { |p| p['id'] }
|
|
|
|
expect(ids).to include(oc_hit_post.id)
|
|
expect(ids).not_to include(oc_miss_post_for_from.id)
|
|
expect(json.fetch('count')).to eq(2)
|
|
end
|
|
|
|
it 'filters posts by original_created_to (current controller behavior)' do
|
|
get '/posts', params: {
|
|
original_created_to: Time.zone.local(2015, 1, 15, 0, 0, 0).iso8601
|
|
}
|
|
|
|
expect(response).to have_http_status(:ok)
|
|
ids = json.fetch('posts').map { |p| p['id'] }
|
|
|
|
expect(ids).to include(oc_hit_post.id)
|
|
expect(ids).not_to include(oc_miss_post_for_to.id)
|
|
expect(json.fetch('count')).to eq(2)
|
|
end
|
|
end
|
|
end
|
|
|
|
describe 'GET /posts/:id' do
|
|
subject(:request) { get "/posts/#{post_id}" }
|
|
|
|
context 'when post exists' do
|
|
let(:post_id) { post_record.id }
|
|
|
|
it 'returns post with tag tree + related + viewed' do
|
|
request
|
|
expect(response).to have_http_status(:ok)
|
|
|
|
expect(json).to include('id' => post_record.id)
|
|
expect(json).to have_key('tags')
|
|
expect(json['tags']).to be_an(Array)
|
|
|
|
# show は build_tag_tree_for を使うので、tags はツリー形式(children 付き)
|
|
node = json['tags'][0]
|
|
expect(node).to include('id', 'name', 'category', 'post_count', 'children', 'has_wiki')
|
|
expect(node['name']).to eq('spec_tag')
|
|
|
|
expect(json).to have_key('related')
|
|
expect(json['related']).to be_an(Array)
|
|
|
|
expect(json).to have_key('viewed')
|
|
expect([true, false]).to include(json['viewed'])
|
|
end
|
|
end
|
|
|
|
context 'when post does not exist' do
|
|
let(:post_id) { 999_999_999 }
|
|
|
|
it 'returns 404' do
|
|
request
|
|
expect(response).to have_http_status(:not_found)
|
|
end
|
|
end
|
|
end
|
|
|
|
describe 'POST /posts' do
|
|
let(:member) { create(:user, :member) }
|
|
let!(:alias_tag_name) { TagName.create!(name: 'manko', canonical: tag_name) }
|
|
|
|
it '401 when not logged in' do
|
|
sign_out
|
|
post '/posts', params: { title: 't', url: 'https://example.com/x', tags: 'a', thumbnail: dummy_upload }
|
|
expect(response).to have_http_status(:unauthorized)
|
|
end
|
|
|
|
it '403 when not member' do
|
|
sign_in_as(create(:user, role: 'guest'))
|
|
post '/posts', params: { title: 't', url: 'https://example.com/x', tags: 'a', thumbnail: dummy_upload }
|
|
expect(response).to have_http_status(:forbidden)
|
|
end
|
|
|
|
it '201 and creates post + tags when member' do
|
|
sign_in_as(member)
|
|
|
|
post '/posts', params: {
|
|
title: 'new post',
|
|
url: 'https://example.com/new',
|
|
tags: 'spec_tag', # 既存タグ名を投げる
|
|
thumbnail: dummy_upload
|
|
}
|
|
|
|
expect(response).to have_http_status(:created)
|
|
expect(json).to include('id', 'title', 'url')
|
|
|
|
# tags が name を含むこと(API 側の serialization が正しいこと)
|
|
expect(json).to have_key('tags')
|
|
expect(json['tags']).to be_an(Array)
|
|
expect(json['tags'][0]).to have_key('name')
|
|
end
|
|
|
|
it '201 and creates post + tags when member and tags have aliases' do
|
|
sign_in_as(member)
|
|
|
|
post '/posts', params: {
|
|
title: 'new post',
|
|
url: 'https://example.com/new',
|
|
tags: 'manko', # 既存タグ名を投げる
|
|
thumbnail: dummy_upload
|
|
}
|
|
|
|
expect(response).to have_http_status(:created)
|
|
expect(json).to include('id', 'title', 'url')
|
|
|
|
# tags が name を含むこと(API 側の serialization が正しいこと)
|
|
names = json.fetch('tags').map { |t| t['name'] }
|
|
expect(names).to include('spec_tag')
|
|
expect(names).not_to include('manko')
|
|
end
|
|
|
|
context "when nico tag already exists in tags" do
|
|
before do
|
|
Tag.find_undiscard_or_create_by!(
|
|
tag_name: TagName.find_undiscard_or_create_by!(name: 'nico:nico_tag'),
|
|
category: :nico)
|
|
end
|
|
|
|
it 'return 400' do
|
|
sign_in_as(member)
|
|
|
|
post '/posts', params: {
|
|
title: 'new post',
|
|
url: 'https://example.com/nico_tag',
|
|
tags: 'nico:nico_tag',
|
|
thumbnail: dummy_upload }
|
|
|
|
expect(response).to have_http_status(:bad_request)
|
|
end
|
|
end
|
|
|
|
context 'when url is blank' do
|
|
it 'returns 422' do
|
|
sign_in_as(member)
|
|
|
|
post '/posts', params: {
|
|
title: 'new post',
|
|
url: ' ',
|
|
tags: 'spec_tag', # 既存タグ名を投げる
|
|
thumbnail: dummy_upload }
|
|
|
|
expect(response).to have_http_status(:unprocessable_entity)
|
|
end
|
|
end
|
|
|
|
context 'when url is invalid' do
|
|
it 'returns 422' do
|
|
sign_in_as(member)
|
|
|
|
post '/posts', params: {
|
|
title: 'new post',
|
|
url: 'ぼざクリタグ広場',
|
|
tags: 'spec_tag', # 既存タグ名を投げる
|
|
thumbnail: dummy_upload
|
|
}
|
|
|
|
expect(response).to have_http_status(:unprocessable_entity)
|
|
end
|
|
end
|
|
end
|
|
|
|
describe 'PUT /posts/:id' do
|
|
let(:member) { create(:user, :member) }
|
|
|
|
it '401 when not logged in' do
|
|
sign_out
|
|
put "/posts/#{post_record.id}", params: { title: 'updated', tags: 'spec_tag' }
|
|
expect(response).to have_http_status(:unauthorized)
|
|
end
|
|
|
|
it '403 when not member' do
|
|
sign_in_as(create(:user, role: 'guest'))
|
|
put "/posts/#{post_record.id}", params: { title: 'updated', tags: 'spec_tag' }
|
|
expect(response).to have_http_status(:forbidden)
|
|
end
|
|
|
|
it '200 and updates title + resync tags when member' do
|
|
sign_in_as(member)
|
|
|
|
# 追加で別タグも作って、更新時に入れ替わることを見る
|
|
tn2 = TagName.create!(name: 'spec_tag_2')
|
|
Tag.create!(tag_name: tn2, category: :general)
|
|
|
|
put "/posts/#{post_record.id}", params: {
|
|
title: 'updated title',
|
|
tags: 'spec_tag_2'
|
|
}
|
|
|
|
expect(response).to have_http_status(:ok)
|
|
expect(json).to have_key('tags')
|
|
expect(json['tags']).to be_an(Array)
|
|
|
|
# show と同様、update 後レスポンスもツリー形式
|
|
names = json['tags'].map { |n| n['name'] }
|
|
expect(names).to include('spec_tag_2')
|
|
end
|
|
|
|
context "when nico tag already exists in tags" do
|
|
before do
|
|
Tag.find_undiscard_or_create_by!(
|
|
tag_name: TagName.find_undiscard_or_create_by!(name: 'nico:nico_tag'),
|
|
category: :nico)
|
|
end
|
|
|
|
it 'return 400' do
|
|
sign_in_as(member)
|
|
|
|
put "/posts/#{ post_record.id }", params: {
|
|
title: 'updated title',
|
|
tags: 'nico:nico_tag' }
|
|
|
|
expect(response).to have_http_status(:bad_request)
|
|
end
|
|
end
|
|
end
|
|
|
|
describe 'GET /posts/random' do
|
|
it '404 when no posts' do
|
|
PostTag.delete_all
|
|
Post.delete_all
|
|
get '/posts/random'
|
|
expect(response).to have_http_status(:not_found)
|
|
end
|
|
|
|
it '200 and returns viewed boolean' do
|
|
get '/posts/random'
|
|
expect(response).to have_http_status(:ok)
|
|
expect(json).to have_key('viewed')
|
|
expect([true, false]).to include(json['viewed'])
|
|
end
|
|
end
|
|
|
|
describe 'GET /posts/changes' do
|
|
let(:member) { create(:user, :member) }
|
|
|
|
it 'returns add/remove events (history) for a post' do
|
|
# add
|
|
tn2 = TagName.create!(name: 'spec_tag2')
|
|
tag2 = Tag.create!(tag_name: tn2, category: :general)
|
|
pt = PostTag.create!(post: post_record, tag: tag2, created_user: member)
|
|
|
|
# remove (discard)
|
|
pt.discard_by!(member)
|
|
|
|
get '/posts/changes', params: { id: post_record.id }
|
|
|
|
expect(response).to have_http_status(:ok)
|
|
expect(json).to include('changes', 'count')
|
|
expect(json['changes']).to be_an(Array)
|
|
expect(json['count']).to be >= 2
|
|
|
|
types = json['changes'].map { |e| e['change_type'] }.uniq
|
|
expect(types).to include('add')
|
|
expect(types).to include('remove')
|
|
end
|
|
|
|
it 'filters history by tag' do
|
|
tn2 = TagName.create!(name: 'history_tag_hit')
|
|
tag2 = Tag.create!(tag_name: tn2, category: :general)
|
|
|
|
tn3 = TagName.create!(name: 'history_tag_miss')
|
|
tag3 = Tag.create!(tag_name: tn3, category: :general)
|
|
|
|
other_post = Post.create!(
|
|
title: 'other post',
|
|
url: 'https://example.com/history-other'
|
|
)
|
|
|
|
# hit: add
|
|
PostTag.create!(post: post_record, tag: tag2, created_user: member)
|
|
|
|
# hit: add + remove
|
|
pt2 = PostTag.create!(post: other_post, tag: tag2, created_user: member)
|
|
pt2.discard_by!(member)
|
|
|
|
# miss: add + remove
|
|
pt3 = PostTag.create!(post: post_record, tag: tag3, created_user: member)
|
|
pt3.discard_by!(member)
|
|
|
|
get '/posts/changes', params: { tag: tag2.id }
|
|
|
|
expect(response).to have_http_status(:ok)
|
|
expect(json).to include('changes', 'count')
|
|
expect(json['count']).to eq(3)
|
|
|
|
changes = json.fetch('changes')
|
|
|
|
expect(changes.map { |e| e.dig('tag', 'id') }.uniq).to eq([tag2.id])
|
|
expect(changes.map { |e| e['change_type'] }).to match_array(%w[add add remove])
|
|
expect(changes.map { |e| e.dig('post', 'id') }).to match_array([
|
|
post_record.id,
|
|
other_post.id,
|
|
other_post.id
|
|
])
|
|
end
|
|
|
|
it 'filters history by post and tag together' do
|
|
tn2 = TagName.create!(name: 'history_tag_combo_hit')
|
|
tag2 = Tag.create!(tag_name: tn2, category: :general)
|
|
|
|
tn3 = TagName.create!(name: 'history_tag_combo_miss')
|
|
tag3 = Tag.create!(tag_name: tn3, category: :general)
|
|
|
|
other_post = Post.create!(
|
|
title: 'other combo post',
|
|
url: 'https://example.com/history-combo-other'
|
|
)
|
|
|
|
# hit
|
|
PostTag.create!(post: post_record, tag: tag2, created_user: member)
|
|
|
|
# miss by post
|
|
pt2 = PostTag.create!(post: other_post, tag: tag2, created_user: member)
|
|
pt2.discard_by!(member)
|
|
|
|
# miss by tag
|
|
pt3 = PostTag.create!(post: post_record, tag: tag3, created_user: member)
|
|
pt3.discard_by!(member)
|
|
|
|
get '/posts/changes', params: { id: post_record.id, tag: tag2.id }
|
|
|
|
expect(response).to have_http_status(:ok)
|
|
expect(json).to include('changes', 'count')
|
|
expect(json['count']).to eq(1)
|
|
|
|
changes = json.fetch('changes')
|
|
expect(changes.size).to eq(1)
|
|
expect(changes[0]['change_type']).to eq('add')
|
|
expect(changes[0].dig('post', 'id')).to eq(post_record.id)
|
|
expect(changes[0].dig('tag', 'id')).to eq(tag2.id)
|
|
end
|
|
|
|
it 'returns empty history when tag does not match' do
|
|
tn2 = TagName.create!(name: 'history_tag_no_hit')
|
|
tag2 = Tag.create!(tag_name: tn2, category: :general)
|
|
|
|
get '/posts/changes', params: { tag: tag2.id }
|
|
|
|
expect(response).to have_http_status(:ok)
|
|
expect(json.fetch('changes')).to eq([])
|
|
expect(json.fetch('count')).to eq(0)
|
|
end
|
|
end
|
|
|
|
describe 'POST /posts/:id/viewed' do
|
|
let(:user) { create(:user) }
|
|
|
|
it '401 when not logged in' do
|
|
sign_out
|
|
post "/posts/#{ post_record.id }/viewed"
|
|
expect(response).to have_http_status(:unauthorized)
|
|
end
|
|
|
|
it '204 and marks viewed when logged in' do
|
|
sign_in_as(user)
|
|
post "/posts/#{ post_record.id }/viewed"
|
|
expect(response).to have_http_status(:no_content)
|
|
|
|
expect(user.reload.viewed?(post_record)).to be(true)
|
|
end
|
|
end
|
|
|
|
describe 'DELETE /posts/:id/viewed' do
|
|
let(:user) { create(:user) }
|
|
|
|
it '401 when not logged in' do
|
|
sign_out
|
|
delete "/posts/#{ post_record.id }/viewed"
|
|
expect(response).to have_http_status(:unauthorized)
|
|
end
|
|
|
|
it '204 and unmarks viewed when logged in' do
|
|
sign_in_as(user)
|
|
|
|
# 先に viewed 付けてから外す
|
|
user.viewed_posts << post_record
|
|
|
|
delete "/posts/#{ post_record.id }/viewed"
|
|
expect(response).to have_http_status(:no_content)
|
|
|
|
expect(user.reload.viewed?(post_record)).to be(false)
|
|
end
|
|
end
|
|
end
|