/spec/lib/gitlab/database/rename_reserved_paths_migration/v1/rename_namespaces_spec.rb

https://gitlab.com/dandunckelman/gitlab-ce · Ruby · 289 lines · 222 code · 67 blank · 0 comment · 5 complexity · 0d0b08c8454585e18ce6dd33e0cd709f MD5 · raw file

  1. require 'spec_helper'
  2. describe Gitlab::Database::RenameReservedPathsMigration::V1::RenameNamespaces, :truncate do
  3. let(:migration) { FakeRenameReservedPathMigrationV1.new }
  4. let(:subject) { described_class.new(['the-path'], migration) }
  5. let(:namespace) { create(:group, name: 'the-path') }
  6. before do
  7. allow(migration).to receive(:say)
  8. TestEnv.clean_test_path
  9. end
  10. def migration_namespace(namespace)
  11. Gitlab::Database::RenameReservedPathsMigration::V1::MigrationClasses::
  12. Namespace.find(namespace.id)
  13. end
  14. describe '#namespaces_for_paths' do
  15. context 'nested namespaces' do
  16. let(:subject) { described_class.new(['parent/the-Path'], migration) }
  17. it 'includes the namespace' do
  18. parent = create(:group, path: 'parent')
  19. child = create(:group, path: 'the-path', parent: parent)
  20. found_ids = subject.namespaces_for_paths(type: :child)
  21. .map(&:id)
  22. expect(found_ids).to contain_exactly(child.id)
  23. end
  24. end
  25. context 'for child namespaces' do
  26. it 'only returns child namespaces with the correct path' do
  27. _root_namespace = create(:group, path: 'THE-path')
  28. _other_path = create(:group,
  29. path: 'other',
  30. parent: create(:group))
  31. namespace = create(:group,
  32. path: 'the-path',
  33. parent: create(:group))
  34. found_ids = subject.namespaces_for_paths(type: :child)
  35. .map(&:id)
  36. expect(found_ids).to contain_exactly(namespace.id)
  37. end
  38. it 'has no namespaces that look the same' do
  39. _root_namespace = create(:group, path: 'THE-path')
  40. _similar_path = create(:group,
  41. path: 'not-really-the-path',
  42. parent: create(:group))
  43. namespace = create(:group,
  44. path: 'the-path',
  45. parent: create(:group))
  46. found_ids = subject.namespaces_for_paths(type: :child)
  47. .map(&:id)
  48. expect(found_ids).to contain_exactly(namespace.id)
  49. end
  50. end
  51. context 'for top levelnamespaces' do
  52. it 'only returns child namespaces with the correct path' do
  53. root_namespace = create(:group, path: 'the-path')
  54. _other_path = create(:group, path: 'other')
  55. _child_namespace = create(:group,
  56. path: 'the-path',
  57. parent: create(:group))
  58. found_ids = subject.namespaces_for_paths(type: :top_level)
  59. .map(&:id)
  60. expect(found_ids).to contain_exactly(root_namespace.id)
  61. end
  62. it 'has no namespaces that just look the same' do
  63. root_namespace = create(:group, path: 'the-path')
  64. _similar_path = create(:group, path: 'not-really-the-path')
  65. _child_namespace = create(:group,
  66. path: 'the-path',
  67. parent: create(:group))
  68. found_ids = subject.namespaces_for_paths(type: :top_level)
  69. .map(&:id)
  70. expect(found_ids).to contain_exactly(root_namespace.id)
  71. end
  72. end
  73. end
  74. describe '#move_repositories' do
  75. let(:namespace) { create(:group, name: 'hello-group') }
  76. it 'moves a project for a namespace' do
  77. create(:project, namespace: namespace, path: 'hello-project')
  78. expected_path = File.join(TestEnv.repos_path, 'bye-group', 'hello-project.git')
  79. subject.move_repositories(namespace, 'hello-group', 'bye-group')
  80. expect(File.directory?(expected_path)).to be(true)
  81. end
  82. it 'moves a namespace in a subdirectory correctly' do
  83. child_namespace = create(:group, name: 'sub-group', parent: namespace)
  84. create(:project, namespace: child_namespace, path: 'hello-project')
  85. expected_path = File.join(TestEnv.repos_path, 'hello-group', 'renamed-sub-group', 'hello-project.git')
  86. subject.move_repositories(child_namespace, 'hello-group/sub-group', 'hello-group/renamed-sub-group')
  87. expect(File.directory?(expected_path)).to be(true)
  88. end
  89. it 'moves a parent namespace with subdirectories' do
  90. child_namespace = create(:group, name: 'sub-group', parent: namespace)
  91. create(:project, namespace: child_namespace, path: 'hello-project')
  92. expected_path = File.join(TestEnv.repos_path, 'renamed-group', 'sub-group', 'hello-project.git')
  93. subject.move_repositories(child_namespace, 'hello-group', 'renamed-group')
  94. expect(File.directory?(expected_path)).to be(true)
  95. end
  96. end
  97. describe "#child_ids_for_parent" do
  98. it "collects child ids for all levels" do
  99. parent = create(:group)
  100. first_child = create(:group, parent: parent)
  101. second_child = create(:group, parent: parent)
  102. third_child = create(:group, parent: second_child)
  103. all_ids = [parent.id, first_child.id, second_child.id, third_child.id]
  104. collected_ids = subject.child_ids_for_parent(parent, ids: [parent.id])
  105. expect(collected_ids).to contain_exactly(*all_ids)
  106. end
  107. end
  108. describe "#rename_namespace" do
  109. it 'renames paths & routes for the namespace' do
  110. expect(subject).to receive(:rename_path_for_routable)
  111. .with(namespace)
  112. .and_call_original
  113. subject.rename_namespace(namespace)
  114. expect(namespace.reload.path).to eq('the-path0')
  115. end
  116. it 'tracks the rename' do
  117. expect(subject).to receive(:track_rename)
  118. .with('namespace', 'the-path', 'the-path0')
  119. subject.rename_namespace(namespace)
  120. end
  121. it 'renames things related to the namespace' do
  122. expect(subject).to receive(:rename_namespace_dependencies)
  123. .with(namespace, 'the-path', 'the-path0')
  124. subject.rename_namespace(namespace)
  125. end
  126. end
  127. describe '#rename_namespace_dependencies' do
  128. it "moves the the repository for a project in the namespace" do
  129. create(:project, namespace: namespace, path: "the-path-project")
  130. expected_repo = File.join(TestEnv.repos_path, "the-path0", "the-path-project.git")
  131. subject.rename_namespace_dependencies(namespace, 'the-path', 'the-path0')
  132. expect(File.directory?(expected_repo)).to be(true)
  133. end
  134. it "moves the uploads for the namespace" do
  135. expect(subject).to receive(:move_uploads).with("the-path", "the-path0")
  136. subject.rename_namespace_dependencies(namespace, 'the-path', 'the-path0')
  137. end
  138. it "moves the pages for the namespace" do
  139. expect(subject).to receive(:move_pages).with("the-path", "the-path0")
  140. subject.rename_namespace_dependencies(namespace, 'the-path', 'the-path0')
  141. end
  142. it 'invalidates the markdown cache of related projects' do
  143. project = create(:empty_project, namespace: namespace, path: "the-path-project")
  144. expect(subject).to receive(:remove_cached_html_for_projects).with([project.id])
  145. subject.rename_namespace_dependencies(namespace, 'the-path', 'the-path0')
  146. end
  147. it "doesn't rename users for other namespaces" do
  148. expect(subject).not_to receive(:rename_user)
  149. subject.rename_namespace_dependencies(namespace, 'the-path', 'the-path0')
  150. end
  151. it 'renames the username of a namespace for a user' do
  152. user = create(:user, username: 'the-path')
  153. expect(subject).to receive(:rename_user).with('the-path', 'the-path0')
  154. subject.rename_namespace_dependencies(user.namespace, 'the-path', 'the-path0')
  155. end
  156. end
  157. describe '#rename_user' do
  158. it 'renames a username' do
  159. subject = described_class.new([], migration)
  160. user = create(:user, username: 'broken')
  161. subject.rename_user('broken', 'broken0')
  162. expect(user.reload.username).to eq('broken0')
  163. end
  164. end
  165. describe '#rename_namespaces' do
  166. let!(:top_level_namespace) { create(:group, path: 'the-path') }
  167. let!(:child_namespace) do
  168. create(:group, path: 'the-path', parent: create(:group))
  169. end
  170. it 'renames top level namespaces the namespace' do
  171. expect(subject).to receive(:rename_namespace)
  172. .with(migration_namespace(top_level_namespace))
  173. subject.rename_namespaces(type: :top_level)
  174. end
  175. it 'renames child namespaces' do
  176. expect(subject).to receive(:rename_namespace)
  177. .with(migration_namespace(child_namespace))
  178. subject.rename_namespaces(type: :child)
  179. end
  180. end
  181. describe '#revert_renames', redis: true do
  182. it 'renames the routes back to the previous values' do
  183. project = create(:project, path: 'a-project', namespace: namespace)
  184. subject.rename_namespace(namespace)
  185. expect(subject).to receive(:perform_rename)
  186. .with(
  187. kind_of(Gitlab::Database::RenameReservedPathsMigration::V1::MigrationClasses::Namespace),
  188. 'the-path0',
  189. 'the-path'
  190. ).and_call_original
  191. subject.revert_renames
  192. expect(namespace.reload.path).to eq('the-path')
  193. expect(namespace.reload.route.path).to eq('the-path')
  194. expect(project.reload.route.path).to eq('the-path/a-project')
  195. end
  196. it 'moves the repositories back to their original place' do
  197. project = create(:project, path: 'a-project', namespace: namespace)
  198. project.create_repository
  199. subject.rename_namespace(namespace)
  200. expected_path = File.join(TestEnv.repos_path, 'the-path', 'a-project.git')
  201. expect(subject).to receive(:rename_namespace_dependencies)
  202. .with(
  203. kind_of(Gitlab::Database::RenameReservedPathsMigration::V1::MigrationClasses::Namespace),
  204. 'the-path0',
  205. 'the-path'
  206. ).and_call_original
  207. subject.revert_renames
  208. expect(File.directory?(expected_path)).to be_truthy
  209. end
  210. it "doesn't break when the namespace was renamed" do
  211. subject.rename_namespace(namespace)
  212. namespace.update_attributes!(path: 'renamed-afterwards')
  213. expect { subject.revert_renames }.not_to raise_error
  214. end
  215. end
  216. end