diff options
Diffstat (limited to 'spec/services')
-rw-r--r-- | spec/services/fork_service_spec.rb | 57 | ||||
-rw-r--r-- | spec/services/issues/bulk_update_context_spec.rb | 113 | ||||
-rw-r--r-- | spec/services/projects_create_service_spec.rb | 142 | ||||
-rw-r--r-- | spec/services/projects_update_service_spec.rb | 111 | ||||
-rw-r--r-- | spec/services/search_service_spec.rb | 54 |
5 files changed, 477 insertions, 0 deletions
diff --git a/spec/services/fork_service_spec.rb b/spec/services/fork_service_spec.rb new file mode 100644 index 00000000000..b6573095dbd --- /dev/null +++ b/spec/services/fork_service_spec.rb @@ -0,0 +1,57 @@ +require 'spec_helper' + +describe Projects::ForkService do + describe :fork_by_user do + before do + @from_namespace = create(:namespace) + @from_user = create(:user, namespace: @from_namespace ) + @from_project = create(:project, creator_id: @from_user.id, namespace: @from_namespace) + @to_namespace = create(:namespace) + @to_user = create(:user, namespace: @to_namespace) + end + + context 'fork project' do + + it "successfully creates project in the user namespace" do + @to_project = fork_project(@from_project, @to_user) + + @to_project.owner.should == @to_user + @to_project.namespace.should == @to_user.namespace + end + end + + context 'fork project failure' do + + it "fails due to transaction failure" do + # make the mock gitlab-shell fail + @to_project = fork_project(@from_project, @to_user, false) + + @to_project.errors.should_not be_empty + @to_project.errors[:base].should include("Fork transaction failed.") + end + + end + + context 'project already exists' do + + it "should fail due to validation, not transaction failure" do + @existing_project = create(:project, creator_id: @to_user.id, name: @from_project.name, namespace: @to_namespace) + @to_project = fork_project(@from_project, @to_user) + + @existing_project.persisted?.should be_true + @to_project.errors[:base].should include("Invalid fork destination") + @to_project.errors[:base].should_not include("Fork transaction failed.") + end + + end + end + + def fork_project(from_project, user, fork_success = true) + context = Projects::ForkService.new(from_project, user) + shell = double("gitlab_shell") + shell.stub(fork_repository: fork_success) + context.stub(gitlab_shell: shell) + context.execute + end + +end diff --git a/spec/services/issues/bulk_update_context_spec.rb b/spec/services/issues/bulk_update_context_spec.rb new file mode 100644 index 00000000000..548109a8450 --- /dev/null +++ b/spec/services/issues/bulk_update_context_spec.rb @@ -0,0 +1,113 @@ +require 'spec_helper' + +describe Issues::BulkUpdateService do + before(:each) { ActiveRecord::Base.observers.enable(:user_observer) } + after(:each) { ActiveRecord::Base.observers.disable(:user_observer) } + + let(:issue) { + create(:issue, project: @project) + } + + before do + @user = create :user + opts = { + name: "GitLab", + namespace: @user.namespace + } + @project = Projects::CreateService.new(@user, opts).execute + end + + describe :close_issue do + + before do + @issues = 5.times.collect do + create(:issue, project: @project) + end + @params = { + update: { + status: 'closed', + issues_ids: @issues.map(&:id) + } + } + end + + it { + result = Issues::BulkUpdateService.new(@project, @user, @params).execute + result[:success].should be_true + result[:count].should == @issues.count + + @project.issues.opened.should be_empty + @project.issues.closed.should_not be_empty + } + + end + + describe :reopen_issues do + + before do + @issues = 5.times.collect do + create(:closed_issue, project: @project) + end + @params = { + update: { + status: 'reopen', + issues_ids: @issues.map(&:id) + } + } + end + + it { + result = Issues::BulkUpdateService.new(@project, @user, @params).execute + result[:success].should be_true + result[:count].should == @issues.count + + @project.issues.closed.should be_empty + @project.issues.opened.should_not be_empty + } + + end + + describe :update_assignee do + + before do + @new_assignee = create :user + @params = { + update: { + issues_ids: [issue.id], + assignee_id: @new_assignee.id + } + } + end + + it { + result = Issues::BulkUpdateService.new(@project, @user, @params).execute + result[:success].should be_true + result[:count].should == 1 + + @project.issues.first.assignee.should == @new_assignee + } + + end + + describe :update_milestone do + + before do + @milestone = create :milestone + @params = { + update: { + issues_ids: [issue.id], + milestone_id: @milestone.id + } + } + end + + it { + result = Issues::BulkUpdateService.new(@project, @user, @params).execute + result[:success].should be_true + result[:count].should == 1 + + @project.issues.first.milestone.should == @milestone + } + end + +end diff --git a/spec/services/projects_create_service_spec.rb b/spec/services/projects_create_service_spec.rb new file mode 100644 index 00000000000..0a41832a211 --- /dev/null +++ b/spec/services/projects_create_service_spec.rb @@ -0,0 +1,142 @@ +require 'spec_helper' + +describe Projects::CreateService do + before(:each) { ActiveRecord::Base.observers.enable(:user_observer) } + after(:each) { ActiveRecord::Base.observers.disable(:user_observer) } + + describe :create_by_user do + before do + @user = create :user + @admin = create :user, admin: true + @opts = { + name: "GitLab", + namespace: @user.namespace + } + end + + context 'user namespace' do + before do + @project = create_project(@user, @opts) + end + + it { @project.should be_valid } + it { @project.owner.should == @user } + it { @project.namespace.should == @user.namespace } + end + + context 'group namespace' do + before do + @group = create :group + @group.add_owner(@user) + + @opts.merge!(namespace_id: @group.id) + @project = create_project(@user, @opts) + end + + it { @project.should be_valid } + it { @project.owner.should == @group } + it { @project.namespace.should == @group } + end + + context 'respect configured visibility setting' do + before(:each) do + @settings = double("settings") + @settings.stub(:issues) { true } + @settings.stub(:merge_requests) { true } + @settings.stub(:wiki) { true } + @settings.stub(:wall) { true } + @settings.stub(:snippets) { true } + stub_const("Settings", Class.new) + @restrictions = double("restrictions") + @restrictions.stub(:restricted_visibility_levels) { [] } + Settings.stub_chain(:gitlab).and_return(@restrictions) + Settings.stub_chain(:gitlab, :default_projects_features).and_return(@settings) + end + + context 'should be public when setting is public' do + before do + @settings.stub(:visibility_level) { Gitlab::VisibilityLevel::PUBLIC } + @project = create_project(@user, @opts) + end + + it { @project.public?.should be_true } + end + + context 'should be private when setting is private' do + before do + @settings.stub(:visibility_level) { Gitlab::VisibilityLevel::PRIVATE } + @project = create_project(@user, @opts) + end + + it { @project.private?.should be_true } + end + + context 'should be internal when setting is internal' do + before do + @settings.stub(:visibility_level) { Gitlab::VisibilityLevel::INTERNAL } + @project = create_project(@user, @opts) + end + + it { @project.internal?.should be_true } + end + end + + context 'respect configured visibility restrictions setting' do + before(:each) do + @settings = double("settings") + @settings.stub(:issues) { true } + @settings.stub(:merge_requests) { true } + @settings.stub(:wiki) { true } + @settings.stub(:wall) { true } + @settings.stub(:snippets) { true } + @settings.stub(:visibility_level) { Gitlab::VisibilityLevel::PRIVATE } + stub_const("Settings", Class.new) + @restrictions = double("restrictions") + @restrictions.stub(:restricted_visibility_levels) { [ Gitlab::VisibilityLevel::PUBLIC ] } + Settings.stub_chain(:gitlab).and_return(@restrictions) + Settings.stub_chain(:gitlab, :default_projects_features).and_return(@settings) + end + + context 'should be private when option is public' do + before do + @opts.merge!(visibility_level: Gitlab::VisibilityLevel::PUBLIC) + @project = create_project(@user, @opts) + end + + it { @project.private?.should be_true } + end + + context 'should be public when option is public for admin' do + before do + @opts.merge!(visibility_level: Gitlab::VisibilityLevel::PUBLIC) + @project = create_project(@admin, @opts) + end + + it { @project.public?.should be_true } + end + + context 'should be private when option is private' do + before do + @opts.merge!(visibility_level: Gitlab::VisibilityLevel::PRIVATE) + @project = create_project(@user, @opts) + end + + it { @project.private?.should be_true } + end + + context 'should be internal when option is internal' do + before do + @opts.merge!(visibility_level: Gitlab::VisibilityLevel::INTERNAL) + @project = create_project(@user, @opts) + end + + it { @project.internal?.should be_true } + end + end + end + + def create_project(user, opts) + Projects::CreateService.new(user, opts).execute + end +end + diff --git a/spec/services/projects_update_service_spec.rb b/spec/services/projects_update_service_spec.rb new file mode 100644 index 00000000000..1854c0d8233 --- /dev/null +++ b/spec/services/projects_update_service_spec.rb @@ -0,0 +1,111 @@ +require 'spec_helper' + +describe Projects::UpdateService do + before(:each) { ActiveRecord::Base.observers.enable(:user_observer) } + after(:each) { ActiveRecord::Base.observers.disable(:user_observer) } + + describe :update_by_user do + before do + @user = create :user + @admin = create :user, admin: true + @project = create :project, creator_id: @user.id, namespace: @user.namespace + @opts = { project: {} } + end + + context 'should be private when updated to private' do + before do + @created_private = @project.private? + + @opts[:project].merge!(visibility_level: Gitlab::VisibilityLevel::PRIVATE) + update_project(@project, @user, @opts) + end + + it { @created_private.should be_true } + it { @project.private?.should be_true } + end + + context 'should be internal when updated to internal' do + before do + @created_private = @project.private? + + @opts[:project].merge!(visibility_level: Gitlab::VisibilityLevel::INTERNAL) + update_project(@project, @user, @opts) + end + + it { @created_private.should be_true } + it { @project.internal?.should be_true } + end + + context 'should be public when updated to public' do + before do + @created_private = @project.private? + + @opts[:project].merge!(visibility_level: Gitlab::VisibilityLevel::PUBLIC) + update_project(@project, @user, @opts) + end + + it { @created_private.should be_true } + it { @project.public?.should be_true } + end + + context 'respect configured visibility restrictions setting' do + before(:each) do + @restrictions = double("restrictions") + @restrictions.stub(:restricted_visibility_levels) { [ Gitlab::VisibilityLevel::PUBLIC ] } + Settings.stub_chain(:gitlab).and_return(@restrictions) + end + + context 'should be private when updated to private' do + before do + @created_private = @project.private? + + @opts[:project].merge!(visibility_level: Gitlab::VisibilityLevel::PRIVATE) + update_project(@project, @user, @opts) + end + + it { @created_private.should be_true } + it { @project.private?.should be_true } + end + + context 'should be internal when updated to internal' do + before do + @created_private = @project.private? + + @opts[:project].merge!(visibility_level: Gitlab::VisibilityLevel::INTERNAL) + update_project(@project, @user, @opts) + end + + it { @created_private.should be_true } + it { @project.internal?.should be_true } + end + + context 'should be private when updated to public' do + before do + @created_private = @project.private? + + @opts[:project].merge!(visibility_level: Gitlab::VisibilityLevel::PUBLIC) + update_project(@project, @user, @opts) + end + + it { @created_private.should be_true } + it { @project.private?.should be_true } + end + + context 'should be public when updated to public by admin' do + before do + @created_private = @project.private? + + @opts[:project].merge!(visibility_level: Gitlab::VisibilityLevel::PUBLIC) + update_project(@project, @admin, @opts) + end + + it { @created_private.should be_true } + it { @project.public?.should be_true } + end + end + end + + def update_project(project, user, opts) + Projects::UpdateService.new(project, user, opts).execute + end +end diff --git a/spec/services/search_service_spec.rb b/spec/services/search_service_spec.rb new file mode 100644 index 00000000000..457cb3c0ca3 --- /dev/null +++ b/spec/services/search_service_spec.rb @@ -0,0 +1,54 @@ +require 'spec_helper' + +describe 'Search::GlobalService' do + let(:found_namespace) { create(:namespace, name: 'searchable namespace', path:'another_thing') } + let(:user) { create(:user, namespace: found_namespace) } + let!(:found_project) { create(:project, name: 'searchable_project', creator_id: user.id, namespace: found_namespace, visibility_level: Gitlab::VisibilityLevel::PRIVATE) } + + let(:unfound_namespace) { create(:namespace, name: 'unfound namespace', path: 'yet_something_else') } + let!(:unfound_project) { create(:project, name: 'unfound_project', creator_id: user.id, namespace: unfound_namespace, visibility_level: Gitlab::VisibilityLevel::PRIVATE) } + + let(:internal_namespace) { create(:namespace, path: 'something_internal',name: 'searchable internal namespace') } + let(:internal_user) { create(:user, namespace: internal_namespace) } + let!(:internal_project) { create(:project, name: 'searchable_internal_project', creator_id: internal_user.id, namespace: internal_namespace, visibility_level: Gitlab::VisibilityLevel::INTERNAL) } + + let(:public_namespace) { create(:namespace, path: 'something_public',name: 'searchable public namespace') } + let(:public_user) { create(:user, namespace: public_namespace) } + let!(:public_project) { create(:project, name: 'searchable_public_project', creator_id: public_user.id, namespace: public_namespace, visibility_level: Gitlab::VisibilityLevel::PUBLIC) } + + describe '#execute' do + context 'unauthenticated' do + it 'should return public projects only' do + context = Search::GlobalService.new(nil, search: "searchable") + results = context.execute + results[:projects].should have(1).items + results[:projects].should include(public_project) + end + end + + context 'authenticated' do + it 'should return public, internal and private projects' do + context = Search::GlobalService.new(user, search: "searchable") + results = context.execute + results[:projects].should have(3).items + results[:projects].should include(public_project) + results[:projects].should include(found_project) + results[:projects].should include(internal_project) + end + + it 'should return only public & internal projects' do + context = Search::GlobalService.new(internal_user, search: "searchable") + results = context.execute + results[:projects].should have(2).items + results[:projects].should include(internal_project) + results[:projects].should include(public_project) + end + + it 'namespace name should be searchable' do + context = Search::GlobalService.new(user, search: "searchable namespace") + results = context.execute + results[:projects].should == [found_project] + end + end + end +end |