Commit 5fedc463 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Merge branch 'refactor-create-service-spec' into 'master'

Refactor create service spec

## What does this MR do?
Use let instead instance variables to cache create user operation.

Union some example into one.

## Are there points in the code the reviewer needs to double check?

## Why was this MR needed?
Time before: 1 minute 11.81 seconds

Time after: 52.47 seconds

- [x] [Changelog entry](https://docs.gitlab.com/ce/development/changelog.html) added
- n/a [Documentation created/updated](https://gitlab.com/gitlab-org/gitlab-ce/blob/master/doc/development/doc_styleguide.md)
- n/a API support added
- Tests
  - n/a Added for this feature/bug
  - [x] All builds are passing
- [x] Conform by the [merge request performance guides](http://docs.gitlab.com/ce/development/merge_request_performance_guidelines.html)
- [x] Conform by the [style guides](https://gitlab.com/gitlab-org/gitlab-ce/blob/master/CONTRIBUTING.md#style-guides)
- [x] Branch has no merge conflicts with `master` (if it does - rebase it please)
- [x] [Squashed related commits together](https://git-scm.com/book/en/Git-Tools-Rewriting-History#Squashing-Commits)

## What are the relevant issue numbers?

See merge request !7609
parents 4e96c531 daf26fa0
---
title: Refactor create service spec
merge_request: 7609
author: Semyon Pupkov
require 'spec_helper' require 'spec_helper'
describe Projects::CreateService, services: true do describe Projects::CreateService, '#execute', services: true do
describe :create_by_user do let(:user) { create :user }
before do let(:opts) do
@user = create :user {
@opts = {
name: "GitLab", name: "GitLab",
namespace: @user.namespace namespace: user.namespace
} }
end end
it 'creates labels on Project creation if there are templates' do it 'creates labels on Project creation if there are templates' do
Label.create(title: "bug", template: true) Label.create(title: "bug", template: true)
project = create_project(@user, @opts) project = create_project(user, opts)
project.reload
expect(project.labels).not_to be_empty expect(project.labels).not_to be_empty
end end
context 'user namespace' do context 'user namespace' do
before do it do
@project = create_project(@user, @opts) project = create_project(user, opts)
end
it { expect(@project).to be_valid } expect(project).to be_valid
it { expect(@project.owner).to eq(@user) } expect(project.owner).to eq(user)
it { expect(@project.team.masters).to include(@user) } expect(project.team.masters).to include(user)
it { expect(@project.namespace).to eq(@user.namespace) } expect(project.namespace).to eq(user.namespace)
end
end end
context 'group namespace' do context 'group namespace' do
let(:group) do
create(:group).tap do |group|
group.add_owner(user)
end
end
before do before do
@group = create :group user.refresh_authorized_projects # Ensure cache is warm
@group.add_owner(@user) end
@user.refresh_authorized_projects # Ensure cache is warm it do
project = create_project(user, opts.merge!(namespace_id: group.id))
@opts.merge!(namespace_id: @group.id) expect(project).to be_valid
@project = create_project(@user, @opts) expect(project.owner).to eq(group)
expect(project.namespace).to eq(group)
expect(user.authorized_projects).to include(project)
end end
it { expect(@project).to be_valid }
it { expect(@project.owner).to eq(@group) }
it { expect(@project.namespace).to eq(@group) }
it { expect(@user.authorized_projects).to include(@project) }
end end
context 'error handling' do context 'error handling' do
it 'handles invalid options' do it 'handles invalid options' do
@opts.merge!({ default_branch: 'master' } ) opts.merge!({ default_branch: 'master' } )
expect(create_project(@user, @opts)).to eq(nil) expect(create_project(user, opts)).to eq(nil)
end end
end end
context 'wiki_enabled creates repository directory' do context 'wiki_enabled creates repository directory' do
context 'wiki_enabled true creates wiki repository directory' do context 'wiki_enabled true creates wiki repository directory' do
before do it do
@project = create_project(@user, @opts) project = create_project(user, opts)
@path = ProjectWiki.new(@project, @user).send(:path_to_repo) path = ProjectWiki.new(project, user).send(:path_to_repo)
end
it { expect(File.exist?(@path)).to be_truthy } expect(File.exist?(path)).to be_truthy
end
end end
context 'wiki_enabled false does not create wiki repository directory' do context 'wiki_enabled false does not create wiki repository directory' do
before do it do
@opts.merge!(wiki_enabled: false) opts.merge!(wiki_enabled: false)
@project = create_project(@user, @opts) project = create_project(user, opts)
@path = ProjectWiki.new(@project, @user).send(:path_to_repo) path = ProjectWiki.new(project, user).send(:path_to_repo)
end
it { expect(File.exist?(@path)).to be_falsey } expect(File.exist?(path)).to be_falsey
end
end end
end end
context 'builds_enabled global setting' do context 'builds_enabled global setting' do
let(:project) { create_project(@user, @opts) } let(:project) { create_project(user, opts) }
subject { project.builds_enabled? } subject { project.builds_enabled? }
...@@ -100,13 +102,13 @@ describe Projects::CreateService, services: true do ...@@ -100,13 +102,13 @@ describe Projects::CreateService, services: true do
before do before do
stub_application_setting(restricted_visibility_levels: [Gitlab::VisibilityLevel::PUBLIC]) stub_application_setting(restricted_visibility_levels: [Gitlab::VisibilityLevel::PUBLIC])
@opts.merge!( opts.merge!(
visibility_level: Gitlab::VisibilityLevel.options['Public'] visibility_level: Gitlab::VisibilityLevel.options['Public']
) )
end end
it 'does not allow a restricted visibility level for non-admins' do it 'does not allow a restricted visibility level for non-admins' do
project = create_project(@user, @opts) project = create_project(user, opts)
expect(project).to respond_to(:errors) expect(project).to respond_to(:errors)
expect(project.errors.messages).to have_key(:visibility_level) expect(project.errors.messages).to have_key(:visibility_level)
expect(project.errors.messages[:visibility_level].first).to( expect(project.errors.messages[:visibility_level].first).to(
...@@ -116,7 +118,7 @@ describe Projects::CreateService, services: true do ...@@ -116,7 +118,7 @@ describe Projects::CreateService, services: true do
it 'allows a restricted visibility level for admins' do it 'allows a restricted visibility level for admins' do
admin = create(:admin) admin = create(:admin)
project = create_project(admin, @opts) project = create_project(admin, opts)
expect(project.errors.any?).to be(false) expect(project.errors.any?).to be(false)
expect(project.saved?).to be(true) expect(project.saved?).to be(true)
...@@ -127,10 +129,10 @@ describe Projects::CreateService, services: true do ...@@ -127,10 +129,10 @@ describe Projects::CreateService, services: true do
it 'synchronously creates the repository' do it 'synchronously creates the repository' do
expect_any_instance_of(Project).to receive(:create_repository) expect_any_instance_of(Project).to receive(:create_repository)
project = create_project(@user, @opts) project = create_project(user, opts)
expect(project).to be_valid expect(project).to be_valid
expect(project.owner).to eq(@user) expect(project.owner).to eq(user)
expect(project.namespace).to eq(@user.namespace) expect(project.namespace).to eq(user.namespace)
end end
end end
...@@ -140,13 +142,11 @@ describe Projects::CreateService, services: true do ...@@ -140,13 +142,11 @@ describe Projects::CreateService, services: true do
end end
it 'creates a service from this template' do it 'creates a service from this template' do
project = create_project(@user, @opts) project = create_project(user, opts)
project.reload
expect(project.services.count).to eq 1 expect(project.services.count).to eq 1
end end
end end
end
def create_project(user, opts) def create_project(user, opts)
Projects::CreateService.new(user, opts).execute Projects::CreateService.new(user, opts).execute
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment