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

197 lines
5.9 KiB

  1. require 'cgi'
  2. require 'rails_helper'
  3. RSpec.describe 'Wiki history integrity', type: :request do
  4. let!(:user) { create_member_user! }
  5. def auth_headers user
  6. { 'X-Transfer-Code' => user.inheritance_code }
  7. end
  8. def create_wiki_page title:, body: 'body', message: 'init', user: self.user
  9. Wiki::Commit.create_content!(
  10. tag_name: TagName.create!(name: title),
  11. body:,
  12. created_by_user: user,
  13. message:)
  14. end
  15. describe 'POST /wiki' do
  16. it 'creates wiki_page, wiki_revision, and wiki_version atomically' do
  17. expect {
  18. post '/wiki',
  19. params: {
  20. title: 'wiki_history_create_atomic',
  21. body: "a\nb\nc",
  22. message: 'initial commit',
  23. },
  24. headers: auth_headers(user)
  25. }
  26. .to change(WikiPage, :count).by(1)
  27. .and change(WikiRevision, :count).by(1)
  28. .and change(WikiVersion, :count).by(1)
  29. expect(response).to have_http_status(:created)
  30. page = WikiPage.find(json.fetch('id'))
  31. revision = page.current_revision
  32. version = page.wiki_versions.order(:version_no).last
  33. expect(page.title).to eq('wiki_history_create_atomic')
  34. expect(page.body).to eq("a\nb\nc")
  35. expect(revision).to be_content
  36. expect(revision.message).to eq('initial commit')
  37. expect(revision.lines_count).to eq(3)
  38. expect(version).to have_attributes(
  39. version_no: 1,
  40. event_type: 'create',
  41. title: 'wiki_history_create_atomic',
  42. body: "a\nb\nc",
  43. reason: 'initial commit',
  44. created_by_user_id: user.id
  45. )
  46. end
  47. it 'returns 422 and creates nothing when normalised body is blank' do
  48. expect {
  49. post '/wiki',
  50. params: {
  51. title: 'wiki_history_blank_body',
  52. body: "\r\n\r\n",
  53. message: 'blank',
  54. },
  55. headers: auth_headers(user)
  56. }
  57. .not_to change(WikiPage, :count)
  58. expect(response).to have_http_status(:unprocessable_entity)
  59. expect(WikiPage.joins(:tag_name).where(tag_names: { name: 'wiki_history_blank_body' })).not_to exist
  60. end
  61. it 'returns 422 and creates no partial page when title already exists' do
  62. create_wiki_page(title: 'wiki_history_duplicate_title', body: 'first')
  63. expect {
  64. post '/wiki',
  65. params: {
  66. title: 'wiki_history_duplicate_title',
  67. body: 'second',
  68. message: 'duplicate',
  69. },
  70. headers: auth_headers(user)
  71. }
  72. .not_to change(WikiPage, :count)
  73. expect(response).to have_http_status(:unprocessable_entity)
  74. expect(WikiPage.joins(:tag_name).where(tag_names: { name: 'wiki_history_duplicate_title' }).count).to eq(1)
  75. end
  76. end
  77. describe 'PUT /wiki/:id' do
  78. it 'updates body and records wiki_revision and wiki_version' do
  79. page = create_wiki_page(title: 'wiki_history_update_body', body: 'before')
  80. current_id = page.current_revision.id
  81. expect {
  82. put "/wiki/#{ page.id }",
  83. params: {
  84. title: 'wiki_history_update_body',
  85. body: 'after',
  86. message: 'edit body',
  87. base_revision_id: current_id,
  88. },
  89. headers: auth_headers(user)
  90. }
  91. .to change(WikiRevision, :count).by(1)
  92. .and change(WikiVersion, :count).by(1)
  93. expect(response).to have_http_status(:ok)
  94. page.reload
  95. version = page.wiki_versions.order(:version_no).last
  96. expect(page.title).to eq('wiki_history_update_body')
  97. expect(page.body).to eq('after')
  98. expect(version).to have_attributes(
  99. event_type: 'update',
  100. title: 'wiki_history_update_body',
  101. body: 'after',
  102. reason: 'edit body',
  103. created_by_user_id: user.id
  104. )
  105. end
  106. it 'renames title and records wiki_version with new title' do
  107. page = create_wiki_page(title: 'wiki_history_rename_before', body: 'before')
  108. current_id = page.current_revision.id
  109. expect {
  110. put "/wiki/#{ page.id }",
  111. params: {
  112. title: 'wiki_history_rename_after',
  113. body: 'after',
  114. message: 'rename',
  115. base_revision_id: current_id,
  116. },
  117. headers: auth_headers(user)
  118. }
  119. .to change(WikiRevision, :count).by(1)
  120. .and change(WikiVersion, :count).by(1)
  121. expect(response).to have_http_status(:ok)
  122. page.reload
  123. version = page.wiki_versions.order(:version_no).last
  124. expect(page.title).to eq('wiki_history_rename_after')
  125. expect(page.body).to eq('after')
  126. expect(version).to have_attributes(
  127. event_type: 'update',
  128. title: 'wiki_history_rename_after',
  129. body: 'after',
  130. reason: 'rename',
  131. created_by_user_id: user.id
  132. )
  133. end
  134. it 'does not change title, body, revision, or version on stale base_revision_id' do
  135. page = create_wiki_page(title: 'wiki_history_conflict_page', body: 'first')
  136. stale_id = page.current_revision.id
  137. Wiki::Commit.content!(
  138. page:,
  139. body: 'second',
  140. created_user: user,
  141. message: 'other edit',
  142. base_revision_id: stale_id)
  143. page.reload
  144. current_title = page.title
  145. current_body = page.body
  146. revision_count = page.wiki_revisions.count
  147. version_count = page.wiki_versions.count
  148. put "/wiki/#{ page.id }",
  149. params: {
  150. title: 'wiki_history_conflict_renamed',
  151. body: 'third',
  152. message: 'stale edit',
  153. base_revision_id: stale_id,
  154. },
  155. headers: auth_headers(user)
  156. expect(response).to have_http_status(:conflict)
  157. page.reload
  158. expect(page.title).to eq(current_title)
  159. expect(page.body).to eq(current_body)
  160. expect(page.wiki_revisions.count).to eq(revision_count)
  161. expect(page.wiki_versions.count).to eq(version_count)
  162. end
  163. end
  164. end