ぼざクリタグ広場 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.
 
 
 
 
 

563 lines
17 KiB

  1. require 'cgi'
  2. require 'rails_helper'
  3. require 'securerandom'
  4. RSpec.describe 'Wiki API', type: :request do
  5. def auth_headers(user)
  6. { 'X-Transfer-Code' => user.inheritance_code }
  7. end
  8. let!(:user) { create_member_user! }
  9. let!(:tn) { TagName.create!(name: 'spec_wiki_title') }
  10. let!(:page) do
  11. Wiki::Commit.create_content!(
  12. tag_name: tn,
  13. body: 'init',
  14. created_by_user: user,
  15. message: 'init')
  16. end
  17. describe 'GET /wiki' do
  18. it 'returns wiki pages with title' do
  19. get '/wiki'
  20. expect(response).to have_http_status(:ok)
  21. expect(json).to be_an(Array)
  22. expect(json).not_to be_empty
  23. expect(json[0]).to have_key('title')
  24. expect(json.map { |p| p['title'] }).to include('spec_wiki_title')
  25. end
  26. end
  27. describe 'GET /wiki/:id' do
  28. subject(:request) do
  29. get "/wiki/#{ page_id }"
  30. end
  31. let(:page_id) { page.id }
  32. context 'when wiki page exists' do
  33. it 'returns wiki page with title' do
  34. request
  35. expect(response).to have_http_status(:ok)
  36. expect(json).to include(
  37. 'id' => page.id,
  38. 'title' => 'spec_wiki_title')
  39. end
  40. end
  41. context 'when wiki page does not exist' do
  42. let(:page_id) { 9_999_999 }
  43. it 'returns 404' do
  44. request
  45. expect(response).to have_http_status(:not_found)
  46. end
  47. end
  48. end
  49. describe 'POST /wiki' do
  50. let(:endpoint) { '/wiki' }
  51. let(:member) { create(:user, role: 'member') }
  52. let(:guest) { create(:user, role: 'guest') }
  53. def auth_headers(user)
  54. { 'X-Transfer-Code' => user.inheritance_code }
  55. end
  56. context 'when not logged in' do
  57. it 'returns 401' do
  58. post endpoint, params: { title: 'Test', body: 'Hello' }
  59. expect(response).to have_http_status(:unauthorized)
  60. end
  61. end
  62. context 'when logged in but not member' do
  63. it 'returns 403' do
  64. post endpoint, params: { title: 'Test', body: 'Hello' }, headers: auth_headers(guest)
  65. expect(response).to have_http_status(:forbidden)
  66. end
  67. end
  68. context 'when params invalid' do
  69. it 'returns 422 when title blank' do
  70. post endpoint, params: { title: '', body: 'Hello' }, headers: auth_headers(member)
  71. expect(response).to have_http_status(:unprocessable_entity)
  72. end
  73. it 'returns 422 when body blank' do
  74. post endpoint, params: { title: 'Test', body: '' }, headers: auth_headers(member)
  75. expect(response).to have_http_status(:unprocessable_entity)
  76. end
  77. end
  78. context 'when success' do
  79. it 'creates wiki_page and first content revision' do
  80. expect do
  81. post endpoint, params: { title: 'TestPage', body: "a\nb\nc", message: 'init' },
  82. headers: auth_headers(member)
  83. end
  84. .to change(WikiPage, :count).by(1)
  85. .and change(WikiRevision, :count).by(1)
  86. .and change(WikiVersion, :count).by(1)
  87. expect(response).to have_http_status(:created)
  88. page_id = json.fetch('id')
  89. expect(json.fetch('title')).to eq('TestPage')
  90. created_page = WikiPage.find(page_id)
  91. version = created_page.wiki_versions.order(:version_no).last
  92. expect(version).to have_attributes(
  93. version_no: 1,
  94. event_type: 'create',
  95. title: 'TestPage',
  96. body: "a\nb\nc",
  97. created_by_user_id: member.id
  98. )
  99. rev = created_page.current_revision
  100. expect(rev).to be_present
  101. expect(rev).to be_content
  102. expect(rev.message).to eq('init')
  103. expect(created_page.body).to eq("a\nb\nc")
  104. expect(rev.lines_count).to eq(3)
  105. expect(rev.wiki_revision_lines.order(:position).pluck(:position)).to eq([0, 1, 2])
  106. expect(rev.wiki_lines.pluck(:body)).to match_array(['a', 'b', 'c'])
  107. end
  108. it 'reuses existing WikiLine rows by sha256' do
  109. # 先に同じ行を作っておく
  110. WikiLine.create!(sha256: Digest::SHA256.hexdigest('a'), body: 'a', created_at: Time.current, updated_at: Time.current)
  111. post endpoint,
  112. params: { title: 'Reuse', body: "a\na" },
  113. headers: auth_headers(member)
  114. page = WikiPage.find(JSON.parse(response.body).fetch('id'))
  115. rev = page.current_revision
  116. expect(rev.lines_count).to eq(2)
  117. # "a" の WikiLine が増殖しない(1行のはず)
  118. expect(WikiLine.where(body: 'a').count).to eq(1)
  119. end
  120. it 'deduplicates duplicated new lines before upsert' do
  121. duplicated = 'duplicated_line_for_wiki_line_upsert_spec'
  122. post endpoint,
  123. params: { title: 'DuplicateNewLine', body: "#{ duplicated }\n#{ duplicated }" },
  124. headers: auth_headers(member)
  125. expect(response).to have_http_status(:created)
  126. page = WikiPage.find(json.fetch('id'))
  127. rev = page.current_revision
  128. expect(rev.lines_count).to eq(2)
  129. expect(WikiLine.where(body: duplicated).count).to eq(1)
  130. expect(rev.wiki_revision_lines.count).to eq(2)
  131. expect(rev.wiki_revision_lines.pluck(:wiki_line_id).uniq.size).to eq(1)
  132. end
  133. it 'normalises CRLF and strips trailing newlines' do
  134. post endpoint,
  135. params: { title: 'NormalisedBody', body: "a\r\nb\r\n\r\n", message: 'normalise' },
  136. headers: auth_headers(member)
  137. expect(response).to have_http_status(:created)
  138. page = WikiPage.find(json.fetch('id'))
  139. rev = page.current_revision
  140. version = page.wiki_versions.order(:version_no).last
  141. expect(page.body).to eq("a\nb")
  142. expect(version.body).to eq("a\nb")
  143. expect(rev.lines_count).to eq(2)
  144. expect(rev.wiki_lines.order('wiki_revision_lines.position').map(&:body)).to eq(['a', 'b'])
  145. end
  146. end
  147. end
  148. describe 'PUT /wiki/:id' do
  149. let(:member) { create(:user, role: 'member', inheritance_code: SecureRandom.hex(16)) }
  150. let(:guest) { create(:user, role: 'guest', inheritance_code: SecureRandom.hex(16)) }
  151. def auth_headers(user)
  152. { 'X-Transfer-Code' => user.inheritance_code }
  153. end
  154. let!(:test_tag_name) { TagName.create!(name: 'TestPage') }
  155. let!(:page) do
  156. Wiki::Commit.create_content!(
  157. tag_name: test_tag_name,
  158. body: "a\nb",
  159. created_by_user: member,
  160. message: 'init')
  161. end
  162. context 'when not logged in' do
  163. it 'returns 401' do
  164. put "/wiki/#{page.id}", params: { title: 'TestPage', body: 'x' }
  165. expect(response).to have_http_status(:unauthorized)
  166. end
  167. end
  168. context 'when logged in but not member' do
  169. it 'returns 403' do
  170. put "/wiki/#{page.id}",
  171. params: { title: 'TestPage', body: 'x' },
  172. headers: auth_headers(guest)
  173. expect(response).to have_http_status(:forbidden)
  174. end
  175. end
  176. context 'when params invalid' do
  177. it 'returns 422 when body blank' do
  178. put "/wiki/#{page.id}",
  179. params: { title: 'TestPage', body: '' },
  180. headers: auth_headers(member)
  181. expect(response).to have_http_status(:unprocessable_entity)
  182. end
  183. end
  184. context 'when success' do
  185. it 'creates new revision and returns 200' do
  186. current_id = page.wiki_revisions.maximum(:id)
  187. expect do
  188. put "/wiki/#{page.id}",
  189. params: { title: 'TestPage', body: "x\ny", message: 'edit', base_revision_id: current_id },
  190. headers: auth_headers(member)
  191. end
  192. .to change(WikiRevision, :count).by(1)
  193. .and change(WikiVersion, :count).by(1)
  194. version = page.wiki_versions.order(:version_no).last
  195. expect(version).to have_attributes(
  196. event_type: 'update',
  197. title: 'TestPage',
  198. body: "x\ny",
  199. created_by_user_id: member.id
  200. )
  201. expect(response).to have_http_status(:ok)
  202. page.reload
  203. rev = page.current_revision
  204. expect(rev).to be_content
  205. expect(rev.message).to eq('edit')
  206. expect(page.body).to eq("x\ny")
  207. expect(rev.base_revision_id).to eq(current_id)
  208. end
  209. it 'wiki body だけを変更しても tag version は作成しない' do
  210. linked_tag_name = TagName.create!(name: 'wiki_body_only_tag')
  211. linked_tag = Tag.create!(tag_name: linked_tag_name, category: :general)
  212. TagVersionRecorder.record!(
  213. tag: linked_tag,
  214. event_type: :create,
  215. created_by_user: member)
  216. linked_page =
  217. Wiki::Commit.create_content!(
  218. tag_name: linked_tag_name,
  219. body: 'before',
  220. created_by_user: member,
  221. message: 'init')
  222. current_id = linked_page.current_revision.id
  223. before_count = linked_tag.reload.tag_versions.count
  224. expect {
  225. put "/wiki/#{ linked_page.id }",
  226. params: {
  227. title: 'wiki_body_only_tag',
  228. body: 'after',
  229. message: 'edit',
  230. base_revision_id: current_id,
  231. },
  232. headers: auth_headers(member)
  233. }
  234. .to change(WikiRevision, :count).by(1)
  235. .and change(WikiVersion, :count).by(1)
  236. expect(response).to have_http_status(:ok)
  237. expect(linked_tag.reload.tag_versions.count).to eq(before_count)
  238. end
  239. end
  240. context 'when conflict' do
  241. it 'returns 409 when base_revision_id mismatches' do
  242. # 先に別ユーザ(同じ member でもOK)が 1 回更新して先頭を進める
  243. Wiki::Commit.content!(page: page, body: "zzz", created_user: member, message: 'other edit')
  244. page.reload
  245. stale_id = page.wiki_revisions.order(:id).first.id # わざと古い id
  246. put "/wiki/#{page.id}",
  247. params: { title: 'TestPage', body: 'x', base_revision_id: stale_id },
  248. headers: auth_headers(member)
  249. expect(response).to have_http_status(:conflict)
  250. json = JSON.parse(response.body)
  251. expect(json['error']).to eq('conflict')
  252. end
  253. end
  254. context 'when page not found' do
  255. it 'returns 404' do
  256. put "/wiki/99999999",
  257. params: { title: 'X', body: 'x' },
  258. headers: auth_headers(member)
  259. expect(response).to have_http_status(:not_found)
  260. end
  261. end
  262. end
  263. describe 'GET /wiki/title/:title' do
  264. it 'returns wiki page by title' do
  265. get "/wiki/title/#{CGI.escape('spec_wiki_title')}"
  266. expect(response).to have_http_status(:ok)
  267. expect(json).to have_key('id')
  268. expect(json).to have_key('title')
  269. expect(json['id']).to eq(page.id)
  270. expect(json['title']).to eq('spec_wiki_title')
  271. end
  272. it 'returns 404 when not found' do
  273. get "/wiki/title/#{ CGI.escape('nope') }"
  274. expect(response).to have_http_status(:not_found)
  275. end
  276. end
  277. describe 'GET /wiki/search' do
  278. before do
  279. Wiki::Commit.create_content!(
  280. tag_name: TagName.create!(name: 'spec_wiki_title_2'),
  281. body: 'search body 2',
  282. created_by_user: user,
  283. message: 'init')
  284. Wiki::Commit.create_content!(
  285. tag_name: TagName.create!(name: 'unrelated_title'),
  286. body: 'unrelated body',
  287. created_by_user: user,
  288. message: 'init')
  289. end
  290. it 'returns up to 20 pages filtered by title like' do
  291. get "/wiki/search?title=#{CGI.escape('spec_wiki')}"
  292. expect(response).to have_http_status(:ok)
  293. expect(json).to be_an(Array)
  294. titles = json.map { |p| p['title'] }
  295. expect(titles).to include('spec_wiki_title')
  296. expect(titles).to include('spec_wiki_title_2')
  297. expect(titles).not_to include('unrelated_title')
  298. end
  299. it 'returns all when title param is blank' do
  300. get "/wiki/search?title=#{CGI.escape('')}"
  301. expect(response).to have_http_status(:ok)
  302. expect(json).to be_an(Array)
  303. expect(json.map { |p| p['title'] }).to include('spec_wiki_title')
  304. end
  305. end
  306. describe 'GET /wiki/changes' do
  307. let!(:rev1) do
  308. Wiki::Commit.content!(page: page, body: "a\nb", created_user: user, message: 'r1')
  309. page.current_revision
  310. end
  311. let!(:rev2) do
  312. Wiki::Commit.content!(page: page, body: "a\nc", created_user: user, message: 'r2')
  313. page.current_revision
  314. end
  315. it 'returns latest revisions (optionally filtered by page id)' do
  316. get "/wiki/changes?id=#{page.id}"
  317. expect(response).to have_http_status(:ok)
  318. expect(json).to be_an(Array)
  319. expect(json).not_to be_empty
  320. top = json.first
  321. expect(top).to include(
  322. 'revision_id' => rev2.id,
  323. 'pred' => rev2.base_revision_id,
  324. 'succ' => nil,
  325. 'kind' => 'content',
  326. 'message' => 'r2'
  327. )
  328. expect(top['wiki_page']).to include('id' => page.id, 'title' => 'spec_wiki_title')
  329. expect(top['user']).to include('id' => user.id, 'name' => user.name)
  330. expect(top).to have_key('timestamp')
  331. # order desc をざっくり担保
  332. ids = json.map { |x| x['revision_id'] }
  333. expect(ids).to eq(ids.sort.reverse)
  334. end
  335. it 'returns empty array when page has no revisions and filtered by id' do
  336. # 別ページを作って revision 無し
  337. tn2 = TagName.create!(name: 'spec_no_rev')
  338. # 異常データ: revision 無し WikiPage を直接作る
  339. p2 = WikiPage.create!(
  340. tag_name: tn2,
  341. body: 'init',
  342. created_user: user,
  343. updated_user: user)
  344. get "/wiki/changes?id=#{p2.id}"
  345. expect(response).to have_http_status(:ok)
  346. expect(json).to eq([])
  347. end
  348. end
  349. describe 'GET /wiki/title/:title/exists' do
  350. it 'returns 204 when exists' do
  351. get "/wiki/title/#{CGI.escape('spec_wiki_title')}/exists"
  352. expect(response).to have_http_status(:no_content)
  353. expect(response.body).to be_empty
  354. end
  355. it 'returns 404 when not exists' do
  356. get "/wiki/title/#{CGI.escape('nope')}/exists"
  357. expect(response).to have_http_status(:not_found)
  358. end
  359. end
  360. describe 'GET /wiki/:id/exists' do
  361. it 'returns 204 when exists' do
  362. get "/wiki/#{page.id}/exists"
  363. expect(response).to have_http_status(:no_content)
  364. expect(response.body).to be_empty
  365. end
  366. it 'returns 404 when not exists' do
  367. get "/wiki/99999999/exists"
  368. expect(response).to have_http_status(:not_found)
  369. end
  370. end
  371. describe 'GET /wiki/:id/diff' do
  372. let!(:rev_a) do
  373. Wiki::Commit.content!(page: page, body: "a\nb\nc", created_user: user, message: 'A')
  374. page.current_revision
  375. end
  376. let!(:rev_b) do
  377. Wiki::Commit.content!(page: page, body: "a\nx\nc", created_user: user, message: 'B')
  378. page.current_revision
  379. end
  380. it 'returns diff json between revisions' do
  381. get "/wiki/#{page.id}/diff?from=#{rev_a.id}&to=#{rev_b.id}"
  382. expect(response).to have_http_status(:ok)
  383. expect(json).to include(
  384. 'wiki_page_id' => page.id,
  385. 'title' => 'spec_wiki_title',
  386. 'older_revision_id' => rev_a.id,
  387. 'newer_revision_id' => rev_b.id
  388. )
  389. expect(json['diff']).to be_an(Array)
  390. # ざっくり「b が消えて x が増えた」が含まれることを確認
  391. types = json['diff'].map { |x| x['type'] }
  392. expect(types).to include('removed', 'added').or include('removed').and include('added')
  393. end
  394. it 'uses latest as "to" when to is omitted' do
  395. get "/wiki/#{page.id}/diff?from=#{rev_a.id}"
  396. expect(response).to have_http_status(:ok)
  397. expect(json['older_revision_id']).to eq(rev_a.id)
  398. expect(json['newer_revision_id']).to eq(page.current_revision.id)
  399. end
  400. end
  401. describe 'Wiki::Commit.redirect!' do
  402. it 'raises because redirect revisions are deprecated' do
  403. target_tag_name = TagName.create!(name: 'redirect_deprecated_target')
  404. target =
  405. Wiki::Commit.create_content!(
  406. tag_name: target_tag_name,
  407. body: 'target',
  408. created_by_user: user,
  409. message: 'init')
  410. expect {
  411. Wiki::Commit.redirect!(
  412. page: page,
  413. redirect_page: target,
  414. created_user: user,
  415. message: 'redirect',
  416. base_revision_id: page.current_revision.id
  417. )
  418. }.to raise_error(RuntimeError, '廃止しました.')
  419. end
  420. end
  421. it 'wiki title を変更すると対応する tag の version を作成する' do
  422. linked_tag_name = TagName.create!(name: 'wiki_linked_tag_for_version')
  423. linked_tag = Tag.create!(tag_name: linked_tag_name, category: :general)
  424. linked_page =
  425. Wiki::Commit.create_content!(
  426. tag_name: linked_tag_name,
  427. body: 'before',
  428. created_by_user: user,
  429. message: 'init')
  430. current_id = linked_page.current_revision.id
  431. expect {
  432. put "/wiki/#{ linked_page.id }",
  433. params: {
  434. title: 'wiki_linked_tag_for_version_renamed',
  435. body: 'after',
  436. message: 'rename',
  437. base_revision_id: current_id,
  438. },
  439. headers: auth_headers(user)
  440. }
  441. .to change(WikiRevision, :count).by(1)
  442. .and change(WikiVersion, :count).by(1)
  443. .and change { linked_tag.reload.tag_versions.count }.by(2)
  444. expect(response).to have_http_status(:ok)
  445. linked_tag.reload
  446. expect(linked_tag.name).to eq('wiki_linked_tag_for_version_renamed')
  447. versions = linked_tag.tag_versions.order(:version_no)
  448. expect(versions.first.event_type).to eq('create')
  449. expect(versions.first.name).to eq('wiki_linked_tag_for_version')
  450. expect(versions.second.event_type).to eq('update')
  451. expect(versions.second.name).to eq('wiki_linked_tag_for_version_renamed')
  452. end
  453. end