ぼざクリタグ広場 https://hub.nizika.monster
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

710 lines
23 KiB

  1. include ActiveSupport::Testing::TimeHelpers
  2. require 'rails_helper'
  3. require 'set'
  4. RSpec.describe 'Posts API', type: :request do
  5. # create / update で thumbnail.attach は走るが、
  6. # resized_thumbnail! が MiniMagick 依存でコケやすいので request spec ではスタブしとくのが無難。
  7. before do
  8. allow_any_instance_of(Post).to receive(:resized_thumbnail!).and_return(true)
  9. end
  10. def dummy_upload
  11. # 中身は何でもいい(加工処理はスタブしてる)
  12. Rack::Test::UploadedFile.new(StringIO.new('dummy'), 'image/jpeg', original_filename: 'dummy.jpg')
  13. end
  14. let!(:tag_name) { TagName.create!(name: 'spec_tag') }
  15. let!(:tag) { Tag.create!(tag_name: tag_name, category: :general) }
  16. let!(:post_record) do
  17. Post.create!(title: 'spec post', url: 'https://example.com/spec').tap do |p|
  18. PostTag.create!(post: p, tag: tag)
  19. end
  20. end
  21. describe "GET /posts" do
  22. let!(:user) { create_member_user! }
  23. let!(:tag_name) { TagName.create!(name: "spec_tag") }
  24. let!(:tag) { Tag.create!(tag_name:, category: :general) }
  25. let!(:tag_name2) { TagName.create!(name: 'unko') }
  26. let!(:tag2) { Tag.create!(tag_name: tag_name2, category: :deerjikist) }
  27. let!(:alias_tag_name) { TagName.create!(name: 'manko', canonical: tag_name) }
  28. let!(:hit_post) do
  29. Post.create!(uploaded_user: user, title: "hello spec world",
  30. url: 'https://example.com/spec2').tap do |p|
  31. PostTag.create!(post: p, tag:)
  32. end
  33. end
  34. let!(:miss_post) do
  35. Post.create!(uploaded_user: user, title: "unrelated title",
  36. url: 'https://example.com/spec3').tap do |p|
  37. PostTag.create!(post: p, tag: tag2)
  38. end
  39. end
  40. it "returns posts with tag name in JSON" do
  41. get "/posts"
  42. expect(response).to have_http_status(:ok)
  43. posts = json.fetch("posts")
  44. # 全postの全tagが name を含むこと
  45. expect(posts).not_to be_empty
  46. posts.each do |p|
  47. expect(p['tags']).to be_an(Array)
  48. p['tags'].each do |t|
  49. expect(t).to include('name', 'category', 'has_wiki')
  50. end
  51. end
  52. expect(json['count']).to be_an(Integer)
  53. # spec_tag を含む投稿が存在すること
  54. all_tag_names = posts.flat_map { |p| p["tags"].map { |t| t["name"] } }
  55. expect(all_tag_names).to include("spec_tag")
  56. end
  57. context "when q is provided" do
  58. it "filters posts by q (hit case)" do
  59. get "/posts", params: { tags: "spec_tag" }
  60. expect(response).to have_http_status(:ok)
  61. posts = json.fetch('posts')
  62. ids = posts.map { |p| p['id'] }
  63. expect(ids).to include(hit_post.id)
  64. expect(ids).not_to include(miss_post.id)
  65. expect(json['count']).to be_an(Integer)
  66. posts.each do |p|
  67. expect(p['tags']).to be_an(Array)
  68. p['tags'].each do |t|
  69. expect(t).to include('name', 'category', 'has_wiki')
  70. end
  71. end
  72. end
  73. it "filters posts by q (hit case by alias)" do
  74. get "/posts", params: { tags: "manko" }
  75. expect(response).to have_http_status(:ok)
  76. posts = json.fetch('posts')
  77. ids = posts.map { |p| p['id'] }
  78. expect(ids).to include(hit_post.id)
  79. expect(ids).not_to include(miss_post.id)
  80. expect(json['count']).to be_an(Integer)
  81. posts.each do |p|
  82. expect(p['tags']).to be_an(Array)
  83. p['tags'].each do |t|
  84. expect(t).to include('name', 'category', 'has_wiki')
  85. end
  86. end
  87. end
  88. it "returns empty posts when nothing matches" do
  89. get "/posts", params: { tags: "no_such_keyword_12345" }
  90. expect(response).to have_http_status(:ok)
  91. expect(json.fetch("posts")).to eq([])
  92. expect(json.fetch('count')).to eq(0)
  93. end
  94. end
  95. context 'when tags contain not:' do
  96. let!(:foo_tag_name) { TagName.create!(name: 'not_spec_foo') }
  97. let!(:foo_tag) { Tag.create!(tag_name: foo_tag_name, category: :general) }
  98. let!(:bar_tag_name) { TagName.create!(name: 'not_spec_bar') }
  99. let!(:bar_tag) { Tag.create!(tag_name: bar_tag_name, category: :general) }
  100. let!(:baz_tag_name) { TagName.create!(name: 'not_spec_baz') }
  101. let!(:baz_tag) { Tag.create!(tag_name: baz_tag_name, category: :general) }
  102. let!(:foo_alias_tag_name) do
  103. TagName.create!(name: 'not_spec_foo_alias', canonical: foo_tag_name)
  104. end
  105. let!(:foo_only_post) do
  106. Post.create!(uploaded_user: user, title: 'foo only',
  107. url: 'https://example.com/not-spec-foo').tap do |p|
  108. PostTag.create!(post: p, tag: foo_tag)
  109. end
  110. end
  111. let!(:bar_only_post) do
  112. Post.create!(uploaded_user: user, title: 'bar only',
  113. url: 'https://example.com/not-spec-bar').tap do |p|
  114. PostTag.create!(post: p, tag: bar_tag)
  115. end
  116. end
  117. let!(:baz_only_post) do
  118. Post.create!(uploaded_user: user, title: 'baz only',
  119. url: 'https://example.com/not-spec-baz').tap do |p|
  120. PostTag.create!(post: p, tag: baz_tag)
  121. end
  122. end
  123. let!(:foo_bar_post) do
  124. Post.create!(uploaded_user: user, title: 'foo bar',
  125. url: 'https://example.com/not-spec-foo-bar').tap do |p|
  126. PostTag.create!(post: p, tag: foo_tag)
  127. PostTag.create!(post: p, tag: bar_tag)
  128. end
  129. end
  130. let!(:foo_baz_post) do
  131. Post.create!(uploaded_user: user, title: 'foo baz',
  132. url: 'https://example.com/not-spec-foo-baz').tap do |p|
  133. PostTag.create!(post: p, tag: foo_tag)
  134. PostTag.create!(post: p, tag: baz_tag)
  135. end
  136. end
  137. let(:controlled_ids) do
  138. [foo_only_post.id, bar_only_post.id, baz_only_post.id,
  139. foo_bar_post.id, foo_baz_post.id]
  140. end
  141. it 'supports not search' do
  142. get '/posts', params: { tags: 'not:not_spec_foo' }
  143. expect(response).to have_http_status(:ok)
  144. expect(json.fetch('posts').map { |p| p['id'] } & controlled_ids).to match_array(
  145. [bar_only_post.id, baz_only_post.id]
  146. )
  147. end
  148. it 'supports alias in not search' do
  149. get '/posts', params: { tags: 'not:not_spec_foo_alias' }
  150. expect(response).to have_http_status(:ok)
  151. expect(json.fetch('posts').map { |p| p['id'] } & controlled_ids).to match_array(
  152. [bar_only_post.id, baz_only_post.id]
  153. )
  154. end
  155. it 'treats multiple not terms as AND when match is omitted' do
  156. get '/posts', params: { tags: 'not:not_spec_foo not:not_spec_bar' }
  157. expect(response).to have_http_status(:ok)
  158. expect(json.fetch('posts').map { |p| p['id'] } & controlled_ids).to match_array(
  159. [baz_only_post.id]
  160. )
  161. end
  162. it 'treats multiple not terms as OR when match=any' do
  163. get '/posts', params: { tags: 'not:not_spec_foo not:not_spec_bar', match: 'any' }
  164. expect(response).to have_http_status(:ok)
  165. expect(json.fetch('posts').map { |p| p['id'] } & controlled_ids).to match_array(
  166. [foo_only_post.id, bar_only_post.id, baz_only_post.id, foo_baz_post.id]
  167. )
  168. end
  169. it 'supports mixed positive and negative search with AND' do
  170. get '/posts', params: { tags: 'not_spec_foo not:not_spec_bar' }
  171. expect(response).to have_http_status(:ok)
  172. expect(json.fetch('posts').map { |p| p['id'] } & controlled_ids).to match_array(
  173. [foo_only_post.id, foo_baz_post.id]
  174. )
  175. end
  176. it 'supports mixed positive and negative search with OR when match=any' do
  177. get '/posts', params: { tags: 'not_spec_foo not:not_spec_bar', match: 'any' }
  178. expect(response).to have_http_status(:ok)
  179. expect(json.fetch('posts').map { |p| p['id'] } & controlled_ids).to match_array(
  180. [foo_only_post.id, baz_only_post.id, foo_bar_post.id, foo_baz_post.id]
  181. )
  182. end
  183. end
  184. context 'when url is provided' do
  185. let!(:url_hit_post) do
  186. Post.create!(uploaded_user: user, title: 'url hit',
  187. url: 'https://example.com/needle-url-xyz').tap do |p|
  188. PostTag.create!(post: p, tag:)
  189. end
  190. end
  191. let!(:url_miss_post) do
  192. Post.create!(uploaded_user: user, title: 'url miss',
  193. url: 'https://example.com/other-url').tap do |p|
  194. PostTag.create!(post: p, tag:)
  195. end
  196. end
  197. it 'filters posts by url substring' do
  198. get '/posts', params: { url: 'needle-url-xyz' }
  199. expect(response).to have_http_status(:ok)
  200. ids = json.fetch('posts').map { |p| p['id'] }
  201. expect(ids).to include(url_hit_post.id)
  202. expect(ids).not_to include(url_miss_post.id)
  203. expect(json.fetch('count')).to eq(1)
  204. end
  205. end
  206. context 'when title is provided' do
  207. let!(:title_hit_post) do
  208. Post.create!(uploaded_user: user, title: 'needle-title-xyz',
  209. url: 'https://example.com/title-hit').tap do |p|
  210. PostTag.create!(post: p, tag:)
  211. end
  212. end
  213. let!(:title_miss_post) do
  214. Post.create!(uploaded_user: user, title: 'other title',
  215. url: 'https://example.com/title-miss').tap do |p|
  216. PostTag.create!(post: p, tag:)
  217. end
  218. end
  219. it 'filters posts by title substring' do
  220. get '/posts', params: { title: 'needle-title-xyz' }
  221. expect(response).to have_http_status(:ok)
  222. ids = json.fetch('posts').map { |p| p['id'] }
  223. expect(ids).to include(title_hit_post.id)
  224. expect(ids).not_to include(title_miss_post.id)
  225. expect(json.fetch('count')).to eq(1)
  226. end
  227. end
  228. context 'when created_from/created_to are provided' do
  229. let(:t_created_hit) { Time.zone.local(2010, 1, 5, 12, 0, 0) }
  230. let(:t_created_miss) { Time.zone.local(2012, 1, 5, 12, 0, 0) }
  231. let!(:created_hit_post) do
  232. travel_to(t_created_hit) do
  233. Post.create!(uploaded_user: user, title: 'created hit',
  234. url: 'https://example.com/created-hit').tap do |p|
  235. PostTag.create!(post: p, tag:)
  236. end
  237. end
  238. end
  239. let!(:created_miss_post) do
  240. travel_to(t_created_miss) do
  241. Post.create!(uploaded_user: user, title: 'created miss',
  242. url: 'https://example.com/created-miss').tap do |p|
  243. PostTag.create!(post: p, tag:)
  244. end
  245. end
  246. end
  247. it 'filters posts by created_at range' do
  248. get '/posts', params: {
  249. created_from: Time.zone.local(2010, 1, 1, 0, 0, 0).iso8601,
  250. created_to: Time.zone.local(2010, 12, 31, 23, 59, 59).iso8601
  251. }
  252. expect(response).to have_http_status(:ok)
  253. ids = json.fetch('posts').map { |p| p['id'] }
  254. expect(ids).to include(created_hit_post.id)
  255. expect(ids).not_to include(created_miss_post.id)
  256. expect(json.fetch('count')).to eq(1)
  257. end
  258. end
  259. context 'when updated_from/updated_to are provided' do
  260. let(:t0) { Time.zone.local(2011, 2, 1, 12, 0, 0) }
  261. let(:t1) { Time.zone.local(2011, 2, 10, 12, 0, 0) }
  262. let!(:updated_hit_post) do
  263. p = nil
  264. travel_to(t0) do
  265. p = Post.create!(uploaded_user: user, title: 'updated hit',
  266. url: 'https://example.com/updated-hit').tap do |pp|
  267. PostTag.create!(post: pp, tag:)
  268. end
  269. end
  270. travel_to(t1) do
  271. p.update!(title: 'updated hit v2')
  272. end
  273. p
  274. end
  275. let!(:updated_miss_post) do
  276. travel_to(Time.zone.local(2013, 1, 1, 12, 0, 0)) do
  277. Post.create!(uploaded_user: user, title: 'updated miss',
  278. url: 'https://example.com/updated-miss').tap do |p|
  279. PostTag.create!(post: p, tag:)
  280. end
  281. end
  282. end
  283. it 'filters posts by updated_at range' do
  284. get '/posts', params: {
  285. updated_from: Time.zone.local(2011, 2, 5, 0, 0, 0).iso8601,
  286. updated_to: Time.zone.local(2011, 2, 20, 23, 59, 59).iso8601
  287. }
  288. expect(response).to have_http_status(:ok)
  289. ids = json.fetch('posts').map { |p| p['id'] }
  290. expect(ids).to include(updated_hit_post.id)
  291. expect(ids).not_to include(updated_miss_post.id)
  292. expect(json.fetch('count')).to eq(1)
  293. end
  294. end
  295. context 'when original_created_from/original_created_to are provided' do
  296. # 注意: controller の現状ロジックに合わせてる
  297. # original_created_from は `original_created_before > ?`
  298. # original_created_to は `original_created_from <= ?`
  299. let!(:oc_hit_post) do
  300. Post.create!(uploaded_user: user, title: 'oc hit',
  301. url: 'https://example.com/oc-hit',
  302. original_created_from: Time.zone.local(2015, 1, 1, 0, 0, 0),
  303. original_created_before: Time.zone.local(2015, 1, 10, 0, 0, 0)).tap do |p|
  304. PostTag.create!(post: p, tag:)
  305. end
  306. end
  307. # original_created_from の条件は「original_created_before > param」なので、
  308. # before が param 以下になるようにする(ただし before >= from は守る)
  309. let!(:oc_miss_post_for_from) do
  310. Post.create!(
  311. uploaded_user: user,
  312. title: 'oc miss for from',
  313. url: 'https://example.com/oc-miss-from',
  314. original_created_from: Time.zone.local(2014, 12, 1, 0, 0, 0),
  315. original_created_before: Time.zone.local(2015, 1, 1, 0, 0, 0)
  316. ).tap { |p| PostTag.create!(post: p, tag:) }
  317. end
  318. # original_created_to の条件は「original_created_from <= param」なので、
  319. # from が param より後になるようにする(before >= from は守る)
  320. let!(:oc_miss_post_for_to) do
  321. Post.create!(
  322. uploaded_user: user,
  323. title: 'oc miss for to',
  324. url: 'https://example.com/oc-miss-to',
  325. original_created_from: Time.zone.local(2015, 2, 1, 0, 0, 0),
  326. original_created_before: Time.zone.local(2015, 2, 10, 0, 0, 0)
  327. ).tap { |p| PostTag.create!(post: p, tag:) }
  328. end
  329. it 'filters posts by original_created_from (current controller behavior)' do
  330. get '/posts', params: {
  331. original_created_from: Time.zone.local(2015, 1, 5, 0, 0, 0).iso8601
  332. }
  333. expect(response).to have_http_status(:ok)
  334. ids = json.fetch('posts').map { |p| p['id'] }
  335. expect(ids).to include(oc_hit_post.id)
  336. expect(ids).not_to include(oc_miss_post_for_from.id)
  337. expect(json.fetch('count')).to eq(2)
  338. end
  339. it 'filters posts by original_created_to (current controller behavior)' do
  340. get '/posts', params: {
  341. original_created_to: Time.zone.local(2015, 1, 15, 0, 0, 0).iso8601
  342. }
  343. expect(response).to have_http_status(:ok)
  344. ids = json.fetch('posts').map { |p| p['id'] }
  345. expect(ids).to include(oc_hit_post.id)
  346. expect(ids).not_to include(oc_miss_post_for_to.id)
  347. expect(json.fetch('count')).to eq(2)
  348. end
  349. end
  350. end
  351. describe 'GET /posts/:id' do
  352. subject(:request) { get "/posts/#{post_id}" }
  353. context 'when post exists' do
  354. let(:post_id) { post_record.id }
  355. it 'returns post with tag tree + related + viewed' do
  356. request
  357. expect(response).to have_http_status(:ok)
  358. expect(json).to include('id' => post_record.id)
  359. expect(json).to have_key('tags')
  360. expect(json['tags']).to be_an(Array)
  361. # show は build_tag_tree_for を使うので、tags はツリー形式(children 付き)
  362. node = json['tags'][0]
  363. expect(node).to include('id', 'name', 'category', 'post_count', 'children', 'has_wiki')
  364. expect(node['name']).to eq('spec_tag')
  365. expect(json).to have_key('related')
  366. expect(json['related']).to be_an(Array)
  367. expect(json).to have_key('viewed')
  368. expect([true, false]).to include(json['viewed'])
  369. end
  370. end
  371. context 'when post does not exist' do
  372. let(:post_id) { 999_999_999 }
  373. it 'returns 404' do
  374. request
  375. expect(response).to have_http_status(:not_found)
  376. end
  377. end
  378. end
  379. describe 'POST /posts' do
  380. let(:member) { create(:user, :member) }
  381. let!(:alias_tag_name) { TagName.create!(name: 'manko', canonical: tag_name) }
  382. it '401 when not logged in' do
  383. sign_out
  384. post '/posts', params: { title: 't', url: 'https://example.com/x', tags: 'a', thumbnail: dummy_upload }
  385. expect(response).to have_http_status(:unauthorized)
  386. end
  387. it '403 when not member' do
  388. sign_in_as(create(:user, role: 'guest'))
  389. post '/posts', params: { title: 't', url: 'https://example.com/x', tags: 'a', thumbnail: dummy_upload }
  390. expect(response).to have_http_status(:forbidden)
  391. end
  392. it '201 and creates post + tags when member' do
  393. sign_in_as(member)
  394. post '/posts', params: {
  395. title: 'new post',
  396. url: 'https://example.com/new',
  397. tags: 'spec_tag', # 既存タグ名を投げる
  398. thumbnail: dummy_upload
  399. }
  400. expect(response).to have_http_status(:created)
  401. expect(json).to include('id', 'title', 'url')
  402. # tags が name を含むこと(API 側の serialization が正しいこと)
  403. expect(json).to have_key('tags')
  404. expect(json['tags']).to be_an(Array)
  405. expect(json['tags'][0]).to have_key('name')
  406. end
  407. it '201 and creates post + tags when member and tags have aliases' do
  408. sign_in_as(member)
  409. post '/posts', params: {
  410. title: 'new post',
  411. url: 'https://example.com/new',
  412. tags: 'manko', # 既存タグ名を投げる
  413. thumbnail: dummy_upload
  414. }
  415. expect(response).to have_http_status(:created)
  416. expect(json).to include('id', 'title', 'url')
  417. # tags が name を含むこと(API 側の serialization が正しいこと)
  418. names = json.fetch('tags').map { |t| t['name'] }
  419. expect(names).to include('spec_tag')
  420. expect(names).not_to include('manko')
  421. end
  422. context "when nico tag already exists in tags" do
  423. before do
  424. Tag.find_or_create_by!(tag_name: TagName.find_or_create_by!(name: 'nico:nico_tag'),
  425. category: :nico)
  426. end
  427. it 'return 400' do
  428. sign_in_as(member)
  429. post '/posts', params: {
  430. title: 'new post',
  431. url: 'https://example.com/nico_tag',
  432. tags: 'nico:nico_tag',
  433. thumbnail: dummy_upload }
  434. expect(response).to have_http_status(:bad_request)
  435. end
  436. end
  437. context 'when url is blank' do
  438. it 'returns 422' do
  439. sign_in_as(member)
  440. post '/posts', params: {
  441. title: 'new post',
  442. url: ' ',
  443. tags: 'spec_tag', # 既存タグ名を投げる
  444. thumbnail: dummy_upload }
  445. expect(response).to have_http_status(:unprocessable_entity)
  446. end
  447. end
  448. context 'when url is invalid' do
  449. it 'returns 422' do
  450. sign_in_as(member)
  451. post '/posts', params: {
  452. title: 'new post',
  453. url: 'ぼざクリタグ広場',
  454. tags: 'spec_tag', # 既存タグ名を投げる
  455. thumbnail: dummy_upload
  456. }
  457. expect(response).to have_http_status(:unprocessable_entity)
  458. end
  459. end
  460. end
  461. describe 'PUT /posts/:id' do
  462. let(:member) { create(:user, :member) }
  463. it '401 when not logged in' do
  464. sign_out
  465. put "/posts/#{post_record.id}", params: { title: 'updated', tags: 'spec_tag' }
  466. expect(response).to have_http_status(:unauthorized)
  467. end
  468. it '403 when not member' do
  469. sign_in_as(create(:user, role: 'guest'))
  470. put "/posts/#{post_record.id}", params: { title: 'updated', tags: 'spec_tag' }
  471. expect(response).to have_http_status(:forbidden)
  472. end
  473. it '200 and updates title + resync tags when member' do
  474. sign_in_as(member)
  475. # 追加で別タグも作って、更新時に入れ替わることを見る
  476. tn2 = TagName.create!(name: 'spec_tag_2')
  477. Tag.create!(tag_name: tn2, category: :general)
  478. put "/posts/#{post_record.id}", params: {
  479. title: 'updated title',
  480. tags: 'spec_tag_2'
  481. }
  482. expect(response).to have_http_status(:ok)
  483. expect(json).to have_key('tags')
  484. expect(json['tags']).to be_an(Array)
  485. # show と同様、update 後レスポンスもツリー形式
  486. names = json['tags'].map { |n| n['name'] }
  487. expect(names).to include('spec_tag_2')
  488. end
  489. context "when nico tag already exists in tags" do
  490. before do
  491. Tag.find_or_create_by!(tag_name: TagName.find_or_create_by!(name: 'nico:nico_tag'),
  492. category: :nico)
  493. end
  494. it 'return 400' do
  495. sign_in_as(member)
  496. put "/posts/#{ post_record.id }", params: {
  497. title: 'updated title',
  498. tags: 'nico:nico_tag' }
  499. expect(response).to have_http_status(:bad_request)
  500. end
  501. end
  502. end
  503. describe 'GET /posts/random' do
  504. it '404 when no posts' do
  505. PostTag.delete_all
  506. Post.delete_all
  507. get '/posts/random'
  508. expect(response).to have_http_status(:not_found)
  509. end
  510. it '200 and returns viewed boolean' do
  511. get '/posts/random'
  512. expect(response).to have_http_status(:ok)
  513. expect(json).to have_key('viewed')
  514. expect([true, false]).to include(json['viewed'])
  515. end
  516. end
  517. describe 'GET /posts/changes' do
  518. let(:member) { create(:user, :member) }
  519. it 'returns add/remove events (history) for a post' do
  520. # add
  521. tn2 = TagName.create!(name: 'spec_tag2')
  522. tag2 = Tag.create!(tag_name: tn2, category: :general)
  523. pt = PostTag.create!(post: post_record, tag: tag2, created_user: member)
  524. # remove (discard)
  525. pt.discard_by!(member)
  526. get '/posts/changes', params: { id: post_record.id }
  527. expect(response).to have_http_status(:ok)
  528. expect(json).to include('changes', 'count')
  529. expect(json['changes']).to be_an(Array)
  530. expect(json['count']).to be >= 2
  531. types = json['changes'].map { |e| e['change_type'] }.uniq
  532. expect(types).to include('add')
  533. expect(types).to include('remove')
  534. end
  535. end
  536. describe 'POST /posts/:id/viewed' do
  537. let(:user) { create(:user) }
  538. it '401 when not logged in' do
  539. sign_out
  540. post "/posts/#{ post_record.id }/viewed"
  541. expect(response).to have_http_status(:unauthorized)
  542. end
  543. it '204 and marks viewed when logged in' do
  544. sign_in_as(user)
  545. post "/posts/#{ post_record.id }/viewed"
  546. expect(response).to have_http_status(:no_content)
  547. expect(user.reload.viewed?(post_record)).to be(true)
  548. end
  549. end
  550. describe 'DELETE /posts/:id/viewed' do
  551. let(:user) { create(:user) }
  552. it '401 when not logged in' do
  553. sign_out
  554. delete "/posts/#{ post_record.id }/viewed"
  555. expect(response).to have_http_status(:unauthorized)
  556. end
  557. it '204 and unmarks viewed when logged in' do
  558. sign_in_as(user)
  559. # 先に viewed 付けてから外す
  560. user.viewed_posts << post_record
  561. delete "/posts/#{ post_record.id }/viewed"
  562. expect(response).to have_http_status(:no_content)
  563. expect(user.reload.viewed?(post_record)).to be(false)
  564. end
  565. end
  566. end