PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/spec/models/project_wiki_spec.rb

https://gitlab.com/alexsanford/gitlab-ce
Ruby | 308 lines | 248 code | 60 blank | 0 comment | 4 complexity | adc7265b43b7638e5793ebc839e19e4b MD5 | raw file
  1. require "spec_helper"
  2. describe ProjectWiki, models: true do
  3. let(:project) { create(:empty_project) }
  4. let(:repository) { project.repository }
  5. let(:user) { project.owner }
  6. let(:gitlab_shell) { Gitlab::Shell.new }
  7. let(:project_wiki) { ProjectWiki.new(project, user) }
  8. subject { project_wiki }
  9. before { project_wiki.wiki }
  10. describe "#path_with_namespace" do
  11. it "returns the project path with namespace with the .wiki extension" do
  12. expect(subject.path_with_namespace).to eq(project.path_with_namespace + ".wiki")
  13. end
  14. end
  15. describe '#web_url' do
  16. it 'returns the full web URL to the wiki' do
  17. expect(subject.web_url).to eq("#{Gitlab.config.gitlab.url}/#{project.path_with_namespace}/wikis/home")
  18. end
  19. end
  20. describe "#url_to_repo" do
  21. it "returns the correct ssh url to the repo" do
  22. expect(subject.url_to_repo).to eq(gitlab_shell.url_to_repo(subject.path_with_namespace))
  23. end
  24. end
  25. describe "#ssh_url_to_repo" do
  26. it "equals #url_to_repo" do
  27. expect(subject.ssh_url_to_repo).to eq(subject.url_to_repo)
  28. end
  29. end
  30. describe "#http_url_to_repo" do
  31. let(:project) { create :empty_project }
  32. context 'when no user is given' do
  33. it 'returns the url to the repo without a username' do
  34. expected_url = "#{Gitlab.config.gitlab.url}/#{subject.path_with_namespace}.git"
  35. expect(project_wiki.http_url_to_repo).to eq(expected_url)
  36. expect(project_wiki.http_url_to_repo).not_to include('@')
  37. end
  38. end
  39. context 'when user is given' do
  40. it 'returns the url to the repo with the username' do
  41. user = build_stubbed(:user)
  42. expect(project_wiki.http_url_to_repo(user)).to start_with("http://#{user.username}@")
  43. end
  44. end
  45. end
  46. describe "#wiki_base_path" do
  47. it "returns the wiki base path" do
  48. wiki_base_path = "#{Gitlab.config.gitlab.relative_url_root}/#{project.path_with_namespace}/wikis"
  49. expect(subject.wiki_base_path).to eq(wiki_base_path)
  50. end
  51. end
  52. describe "#wiki" do
  53. it "contains a Gollum::Wiki instance" do
  54. expect(subject.wiki).to be_a Gollum::Wiki
  55. end
  56. it "creates a new wiki repo if one does not yet exist" do
  57. expect(project_wiki.create_page("index", "test content")).to be_truthy
  58. end
  59. it "raises CouldNotCreateWikiError if it can't create the wiki repository" do
  60. allow(project_wiki).to receive(:init_repo).and_return(false)
  61. expect { project_wiki.send(:create_repo!) }.to raise_exception(ProjectWiki::CouldNotCreateWikiError)
  62. end
  63. end
  64. describe "#empty?" do
  65. context "when the wiki repository is empty" do
  66. before do
  67. allow_any_instance_of(Gitlab::Shell).to receive(:add_repository) do
  68. create_temp_repo("#{Rails.root}/tmp/test-git-base-path/non-existant.wiki.git")
  69. end
  70. allow(project).to receive(:path_with_namespace).and_return("non-existant")
  71. end
  72. describe '#empty?' do
  73. subject { super().empty? }
  74. it { is_expected.to be_truthy }
  75. end
  76. end
  77. context "when the wiki has pages" do
  78. before do
  79. project_wiki.create_page("index", "This is an awesome new Gollum Wiki")
  80. end
  81. describe '#empty?' do
  82. subject { super().empty? }
  83. it { is_expected.to be_falsey }
  84. end
  85. end
  86. end
  87. describe "#pages" do
  88. before do
  89. create_page("index", "This is an awesome new Gollum Wiki")
  90. @pages = subject.pages
  91. end
  92. after do
  93. destroy_page(@pages.first.page)
  94. end
  95. it "returns an array of WikiPage instances" do
  96. expect(@pages.first).to be_a WikiPage
  97. end
  98. it "returns the correct number of pages" do
  99. expect(@pages.count).to eq(1)
  100. end
  101. end
  102. describe "#find_page" do
  103. before do
  104. create_page("index page", "This is an awesome Gollum Wiki")
  105. end
  106. after do
  107. destroy_page(subject.pages.first.page)
  108. end
  109. it "returns the latest version of the page if it exists" do
  110. page = subject.find_page("index page")
  111. expect(page.title).to eq("index page")
  112. end
  113. it "returns nil if the page does not exist" do
  114. expect(subject.find_page("non-existant")).to eq(nil)
  115. end
  116. it "can find a page by slug" do
  117. page = subject.find_page("index-page")
  118. expect(page.title).to eq("index page")
  119. end
  120. it "returns a WikiPage instance" do
  121. page = subject.find_page("index page")
  122. expect(page).to be_a WikiPage
  123. end
  124. end
  125. describe '#find_file' do
  126. before do
  127. file = Gollum::File.new(subject.wiki)
  128. allow_any_instance_of(Gollum::Wiki).
  129. to receive(:file).with('image.jpg', 'master', true).
  130. and_return(file)
  131. allow_any_instance_of(Gollum::File).
  132. to receive(:mime_type).
  133. and_return('image/jpeg')
  134. allow_any_instance_of(Gollum::Wiki).
  135. to receive(:file).with('non-existant', 'master', true).
  136. and_return(nil)
  137. end
  138. after do
  139. allow_any_instance_of(Gollum::Wiki).to receive(:file).and_call_original
  140. allow_any_instance_of(Gollum::File).to receive(:mime_type).and_call_original
  141. end
  142. it 'returns the latest version of the file if it exists' do
  143. file = subject.find_file('image.jpg')
  144. expect(file.mime_type).to eq('image/jpeg')
  145. end
  146. it 'returns nil if the page does not exist' do
  147. expect(subject.find_file('non-existant')).to eq(nil)
  148. end
  149. it 'returns a Gollum::File instance' do
  150. file = subject.find_file('image.jpg')
  151. expect(file).to be_a Gollum::File
  152. end
  153. end
  154. describe "#create_page" do
  155. after do
  156. destroy_page(subject.pages.first.page)
  157. end
  158. it "creates a new wiki page" do
  159. expect(subject.create_page("test page", "this is content")).not_to eq(false)
  160. expect(subject.pages.count).to eq(1)
  161. end
  162. it "returns false when a duplicate page exists" do
  163. subject.create_page("test page", "content")
  164. expect(subject.create_page("test page", "content")).to eq(false)
  165. end
  166. it "stores an error message when a duplicate page exists" do
  167. 2.times { subject.create_page("test page", "content") }
  168. expect(subject.error_message).to match(/Duplicate page:/)
  169. end
  170. it "sets the correct commit message" do
  171. subject.create_page("test page", "some content", :markdown, "commit message")
  172. expect(subject.pages.first.page.version.message).to eq("commit message")
  173. end
  174. it 'updates project activity' do
  175. expect(subject).to receive(:update_project_activity)
  176. subject.create_page('Test Page', 'This is content')
  177. end
  178. end
  179. describe "#update_page" do
  180. before do
  181. create_page("update-page", "some content")
  182. @gollum_page = subject.wiki.paged("update-page")
  183. subject.update_page(@gollum_page, "some other content", :markdown, "updated page")
  184. @page = subject.pages.first.page
  185. end
  186. after do
  187. destroy_page(@page)
  188. end
  189. it "updates the content of the page" do
  190. expect(@page.raw_data).to eq("some other content")
  191. end
  192. it "sets the correct commit message" do
  193. expect(@page.version.message).to eq("updated page")
  194. end
  195. it 'updates project activity' do
  196. expect(subject).to receive(:update_project_activity)
  197. subject.update_page(@gollum_page, 'Yet more content', :markdown, 'Updated page again')
  198. end
  199. end
  200. describe "#delete_page" do
  201. before do
  202. create_page("index", "some content")
  203. @page = subject.wiki.paged("index")
  204. end
  205. it "deletes the page" do
  206. subject.delete_page(@page)
  207. expect(subject.pages.count).to eq(0)
  208. end
  209. it 'updates project activity' do
  210. expect(subject).to receive(:update_project_activity)
  211. subject.delete_page(@page)
  212. end
  213. end
  214. describe '#create_repo!' do
  215. it 'creates a repository' do
  216. expect(subject).to receive(:init_repo).
  217. with(subject.path_with_namespace).
  218. and_return(true)
  219. expect(subject.repository).to receive(:after_create)
  220. expect(subject.create_repo!).to be_an_instance_of(Gollum::Wiki)
  221. end
  222. end
  223. describe '#hook_attrs' do
  224. it 'returns a hash with values' do
  225. expect(subject.hook_attrs).to be_a Hash
  226. expect(subject.hook_attrs.keys).to contain_exactly(:web_url, :git_ssh_url, :git_http_url, :path_with_namespace, :default_branch)
  227. end
  228. end
  229. private
  230. def create_temp_repo(path)
  231. FileUtils.mkdir_p path
  232. system(*%W(#{Gitlab.config.git.bin_path} init --quiet --bare -- #{path}))
  233. end
  234. def remove_temp_repo(path)
  235. FileUtils.rm_rf path
  236. end
  237. def commit_details
  238. { name: user.name, email: user.email, message: "test commit" }
  239. end
  240. def create_page(name, content)
  241. subject.wiki.write_page(name, :markdown, content, commit_details)
  242. end
  243. def destroy_page(page)
  244. subject.wiki.delete_page(page, commit_details)
  245. end
  246. end