PageRenderTime 41ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/spec/controllers/import/github_controller_spec.rb

https://gitlab.com/zarevavasyl/gitlab-ce
Ruby | 161 lines | 137 code | 24 blank | 0 comment | 0 complexity | 16caa55ee016a2fb9553d97f5d9491e1 MD5 | raw file
  1. require 'spec_helper'
  2. require_relative 'import_spec_helper'
  3. describe Import::GithubController do
  4. include ImportSpecHelper
  5. let(:user) { create(:user) }
  6. let(:token) { "asdasd12345" }
  7. let(:access_params) { { github_access_token: token } }
  8. def assign_session_token
  9. session[:github_access_token] = token
  10. end
  11. before do
  12. sign_in(user)
  13. allow(controller).to receive(:github_import_enabled?).and_return(true)
  14. end
  15. describe "GET callback" do
  16. it "updates access token" do
  17. token = "asdasd12345"
  18. allow_any_instance_of(Gitlab::GithubImport::Client).
  19. to receive(:get_token).and_return(token)
  20. allow_any_instance_of(Gitlab::GithubImport::Client).
  21. to receive(:github_options).and_return({})
  22. stub_omniauth_provider('github')
  23. get :callback
  24. expect(session[:github_access_token]).to eq(token)
  25. expect(controller).to redirect_to(status_import_github_url)
  26. end
  27. end
  28. describe "GET status" do
  29. before do
  30. @repo = OpenStruct.new(login: 'vim', full_name: 'asd/vim')
  31. @org = OpenStruct.new(login: 'company')
  32. @org_repo = OpenStruct.new(login: 'company', full_name: 'company/repo')
  33. assign_session_token
  34. end
  35. it "assigns variables" do
  36. @project = create(:project, import_type: 'github', creator_id: user.id)
  37. stub_client(repos: [@repo, @org_repo], orgs: [@org], org_repos: [@org_repo])
  38. get :status
  39. expect(assigns(:already_added_projects)).to eq([@project])
  40. expect(assigns(:repos)).to eq([@repo, @org_repo])
  41. end
  42. it "does not show already added project" do
  43. @project = create(:project, import_type: 'github', creator_id: user.id, import_source: 'asd/vim')
  44. stub_client(repos: [@repo], orgs: [])
  45. get :status
  46. expect(assigns(:already_added_projects)).to eq([@project])
  47. expect(assigns(:repos)).to eq([])
  48. end
  49. end
  50. describe "POST create" do
  51. let(:github_username) { user.username }
  52. let(:github_user) { OpenStruct.new(login: github_username) }
  53. let(:github_repo) do
  54. OpenStruct.new(
  55. name: 'vim',
  56. full_name: "#{github_username}/vim",
  57. owner: OpenStruct.new(login: github_username)
  58. )
  59. end
  60. before do
  61. stub_client(user: github_user, repo: github_repo)
  62. assign_session_token
  63. end
  64. context "when the repository owner is the GitHub user" do
  65. context "when the GitHub user and GitLab user's usernames match" do
  66. it "takes the current user's namespace" do
  67. expect(Gitlab::GithubImport::ProjectCreator).
  68. to receive(:new).with(github_repo, user.namespace, user, access_params).
  69. and_return(double(execute: true))
  70. post :create, format: :js
  71. end
  72. end
  73. context "when the GitHub user and GitLab user's usernames don't match" do
  74. let(:github_username) { "someone_else" }
  75. it "takes the current user's namespace" do
  76. expect(Gitlab::GithubImport::ProjectCreator).
  77. to receive(:new).with(github_repo, user.namespace, user, access_params).
  78. and_return(double(execute: true))
  79. post :create, format: :js
  80. end
  81. end
  82. end
  83. context "when the repository owner is not the GitHub user" do
  84. let(:other_username) { "someone_else" }
  85. before do
  86. github_repo.owner = OpenStruct.new(login: other_username)
  87. assign_session_token
  88. end
  89. context "when a namespace with the GitHub user's username already exists" do
  90. let!(:existing_namespace) { create(:namespace, name: other_username, owner: user) }
  91. context "when the namespace is owned by the GitLab user" do
  92. it "takes the existing namespace" do
  93. expect(Gitlab::GithubImport::ProjectCreator).
  94. to receive(:new).with(github_repo, existing_namespace, user, access_params).
  95. and_return(double(execute: true))
  96. post :create, format: :js
  97. end
  98. end
  99. context "when the namespace is not owned by the GitLab user" do
  100. before do
  101. existing_namespace.owner = create(:user)
  102. existing_namespace.save
  103. end
  104. it "doesn't create a project" do
  105. expect(Gitlab::GithubImport::ProjectCreator).
  106. not_to receive(:new)
  107. post :create, format: :js
  108. end
  109. end
  110. end
  111. context "when a namespace with the GitHub user's username doesn't exist" do
  112. it "creates the namespace" do
  113. expect(Gitlab::GithubImport::ProjectCreator).
  114. to receive(:new).and_return(double(execute: true))
  115. post :create, format: :js
  116. expect(Namespace.where(name: other_username).first).not_to be_nil
  117. end
  118. it "takes the new namespace" do
  119. expect(Gitlab::GithubImport::ProjectCreator).
  120. to receive(:new).with(github_repo, an_instance_of(Group), user, access_params).
  121. and_return(double(execute: true))
  122. post :create, format: :js
  123. end
  124. end
  125. end
  126. end
  127. end