diff options
Diffstat (limited to 'spec')
-rw-r--r-- | spec/controllers/projects/releases_controller_spec.rb | 57 | ||||
-rw-r--r-- | spec/factories/broadcast_messages.rb | 6 | ||||
-rw-r--r-- | spec/lib/api/helpers/custom_validators_spec.rb | 32 | ||||
-rw-r--r-- | spec/lib/gitlab/lograge/custom_options_spec.rb | 50 | ||||
-rw-r--r-- | spec/lib/gitlab/sidekiq_queue_spec.rb | 9 | ||||
-rw-r--r-- | spec/lib/gitlab/utils/json_size_estimator_spec.rb | 39 | ||||
-rw-r--r-- | spec/presenters/project_hook_presenter_spec.rb (renamed from spec/presenters/hooks/project_hook_presenter_spec.rb) | 0 | ||||
-rw-r--r-- | spec/presenters/service_hook_presenter_spec.rb (renamed from spec/presenters/hooks/service_hook_presenter_spec.rb) | 0 | ||||
-rw-r--r-- | spec/requests/api/users_spec.rb | 19 | ||||
-rw-r--r-- | spec/services/post_receive_service_spec.rb | 12 |
10 files changed, 212 insertions, 12 deletions
diff --git a/spec/controllers/projects/releases_controller_spec.rb b/spec/controllers/projects/releases_controller_spec.rb index a03fabad2de..58fb2a95ce6 100644 --- a/spec/controllers/projects/releases_controller_spec.rb +++ b/spec/controllers/projects/releases_controller_spec.rb @@ -198,6 +198,63 @@ describe Projects::ReleasesController do end end + context 'GET #downloads' do + subject do + get :downloads, params: { + namespace_id: project.namespace, + project_id: project, + tag: tag, + filepath: filepath + } + end + + before do + sign_in(user) + end + + let(:release) { create(:release, project: project, tag: tag ) } + let(:tag) { 'v11.9.0-rc2' } + let(:db_filepath) { '/binaries/linux-amd64' } + let!(:link) do + create :release_link, + release: release, + name: 'linux-amd64 binaries', + filepath: db_filepath, + url: 'https://downloads.example.com/bin/gitlab-linux-amd64' + end + + context 'valid filepath' do + let(:filepath) { CGI.escape('/binaries/linux-amd64') } + + it 'redirects to the asset direct link' do + subject + + expect(response).to redirect_to(link.url) + end + end + + context 'invalid filepath' do + let(:filepath) { CGI.escape('/binaries/win32') } + + it 'is not found' do + subject + + expect(response).to have_gitlab_http_status(:not_found) + end + end + + context 'ignores filepath extension' do + let(:db_filepath) { '/binaries/linux-amd64.json' } + let(:filepath) { CGI.escape(db_filepath) } + + it 'redirects to the asset direct link' do + subject + + expect(response).to redirect_to(link.url) + end + end + end + describe 'GET #evidence' do let_it_be(:tag_name) { "v1.1.0-evidence" } let!(:release) { create(:release, :with_evidence, project: project, tag: tag_name) } diff --git a/spec/factories/broadcast_messages.rb b/spec/factories/broadcast_messages.rb index ed6e267e7c4..fa8d255ae79 100644 --- a/spec/factories/broadcast_messages.rb +++ b/spec/factories/broadcast_messages.rb @@ -6,6 +6,8 @@ FactoryBot.define do starts_at { 1.day.ago } ends_at { 1.day.from_now } + broadcast_type { :banner } + trait :expired do starts_at { 5.days.ago } ends_at { 3.days.ago } @@ -15,5 +17,9 @@ FactoryBot.define do starts_at { 5.days.from_now } ends_at { 6.days.from_now } end + + trait :notification do + broadcast_type { :notification } + end end end diff --git a/spec/lib/api/helpers/custom_validators_spec.rb b/spec/lib/api/helpers/custom_validators_spec.rb index 10505210e65..66b86d0a055 100644 --- a/spec/lib/api/helpers/custom_validators_spec.rb +++ b/spec/lib/api/helpers/custom_validators_spec.rb @@ -29,6 +29,38 @@ describe API::Helpers::CustomValidators do end end + describe API::Helpers::CustomValidators::GitSha do + let(:sha) { RepoHelpers.sample_commit.id } + let(:short_sha) { sha[0, Gitlab::Git::Commit::MIN_SHA_LENGTH] } + let(:too_short_sha) { sha[0, Gitlab::Git::Commit::MIN_SHA_LENGTH - 1] } + + subject do + described_class.new(['test'], {}, false, scope.new) + end + + context 'valid sha' do + it 'does not raise a validation error' do + expect_no_validation_error('test' => sha) + expect_no_validation_error('test' => short_sha) + end + end + + context 'empty params' do + it 'raises a validation error' do + expect_validation_error('test' => nil) + expect_validation_error('test' => '') + end + end + + context 'invalid sha' do + it 'raises a validation error' do + expect_validation_error('test' => "#{sha}2") # Sha length > 40 + expect_validation_error('test' => 'somestring') + expect_validation_error('test' => too_short_sha) # sha length < MIN_SHA_LENGTH (7) + end + end + end + describe API::Helpers::CustomValidators::FilePath do subject do described_class.new(['test'], {}, false, scope.new) diff --git a/spec/lib/gitlab/lograge/custom_options_spec.rb b/spec/lib/gitlab/lograge/custom_options_spec.rb new file mode 100644 index 00000000000..48d06283b7a --- /dev/null +++ b/spec/lib/gitlab/lograge/custom_options_spec.rb @@ -0,0 +1,50 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Gitlab::Lograge::CustomOptions do + describe '.call' do + let(:params) do + { + 'controller' => 'ApplicationController', + 'action' => 'show', + 'format' => 'html', + 'a' => 'b' + } + end + + let(:event) do + ActiveSupport::Notifications::Event.new( + 'test', + 1, + 2, + 'transaction_id', + { params: params, user_id: 'test' } + ) + end + + subject { described_class.call(event) } + + it 'ignores some parameters' do + param_keys = subject[:params].map { |param| param[:key] } + + expect(param_keys).not_to include(*described_class::IGNORE_PARAMS) + end + + it 'formats the parameters' do + expect(subject[:params]).to eq([{ key: 'a', value: 'b' }]) + end + + it 'adds the current time' do + travel_to(5.days.ago) do + expected_time = Time.now.utc.iso8601(3) + + expect(subject[:time]).to eq(expected_time) + end + end + + it 'adds the user id' do + expect(subject[:user_id]).to eq('test') + end + end +end diff --git a/spec/lib/gitlab/sidekiq_queue_spec.rb b/spec/lib/gitlab/sidekiq_queue_spec.rb index 7a4d47563b6..9516ea10511 100644 --- a/spec/lib/gitlab/sidekiq_queue_spec.rb +++ b/spec/lib/gitlab/sidekiq_queue_spec.rb @@ -31,14 +31,7 @@ describe Gitlab::SidekiqQueue do context 'when the queue is not processed in time' do before do - calls = 0 - - allow(sidekiq_queue).to receive(:job_matches?).and_wrap_original do |m, *args| - raise Timeout::Error if calls > 0 - - calls += 1 - m.call(*args) - end + allow(Gitlab::Metrics::System).to receive(:monotonic_time).and_return(1, 2, 12) end it 'returns a non-completion flag, the number of jobs deleted, and the remaining queue size' do diff --git a/spec/lib/gitlab/utils/json_size_estimator_spec.rb b/spec/lib/gitlab/utils/json_size_estimator_spec.rb new file mode 100644 index 00000000000..ae24e25558a --- /dev/null +++ b/spec/lib/gitlab/utils/json_size_estimator_spec.rb @@ -0,0 +1,39 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Gitlab::Utils::JsonSizeEstimator do + RSpec::Matchers.define :match_json_bytesize_of do |expected| + match do |actual| + actual == expected.to_json.bytesize + end + end + + def estimate(object) + described_class.estimate(object) + end + + [ + [], + [[[[]]]], + [1, "str", 3.14, ["str", { a: -1 }]], + {}, + { a: {} }, + { a: { b: { c: [1, 2, 3], e: Time.now, f: nil } } }, + { 100 => 500 }, + { '狸' => '狸' }, + nil + ].each do |example| + it { expect(estimate(example)).to match_json_bytesize_of(example) } + end + + it 'calls #to_s on unknown object' do + klass = Class.new do + def to_s + 'hello' + end + end + + expect(estimate(klass.new)).to match_json_bytesize_of(klass.new.to_s) # "hello" + end +end diff --git a/spec/presenters/hooks/project_hook_presenter_spec.rb b/spec/presenters/project_hook_presenter_spec.rb index 773e8ccf51e..773e8ccf51e 100644 --- a/spec/presenters/hooks/project_hook_presenter_spec.rb +++ b/spec/presenters/project_hook_presenter_spec.rb diff --git a/spec/presenters/hooks/service_hook_presenter_spec.rb b/spec/presenters/service_hook_presenter_spec.rb index bea57768e3e..bea57768e3e 100644 --- a/spec/presenters/hooks/service_hook_presenter_spec.rb +++ b/spec/presenters/service_hook_presenter_spec.rb diff --git a/spec/requests/api/users_spec.rb b/spec/requests/api/users_spec.rb index 8b9d0391b79..4a89069cbec 100644 --- a/spec/requests/api/users_spec.rb +++ b/spec/requests/api/users_spec.rb @@ -2165,14 +2165,20 @@ describe API::Users, :do_not_mock_admin_mode do end describe 'POST /users/:id/block' do + let(:blocked_user) { create(:user, state: 'blocked') } + before do admin end it 'blocks existing user' do post api("/users/#{user.id}/block", admin) - expect(response).to have_gitlab_http_status(:created) - expect(user.reload.state).to eq('blocked') + + aggregate_failures do + expect(response).to have_gitlab_http_status(:created) + expect(response.body).to eq('true') + expect(user.reload.state).to eq('blocked') + end end it 'does not re-block ldap blocked users' do @@ -2192,6 +2198,15 @@ describe API::Users, :do_not_mock_admin_mode do expect(response).to have_gitlab_http_status(:not_found) expect(json_response['message']).to eq('404 User Not Found') end + + it 'returns a 201 if user is already blocked' do + post api("/users/#{blocked_user.id}/block", admin) + + aggregate_failures do + expect(response).to have_gitlab_http_status(:created) + expect(response.body).to eq('null') + end + end end describe 'POST /users/:id/unblock' do diff --git a/spec/services/post_receive_service_spec.rb b/spec/services/post_receive_service_spec.rb index 9b9200fd33e..64b4a1125e8 100644 --- a/spec/services/post_receive_service_spec.rb +++ b/spec/services/post_receive_service_spec.rb @@ -130,14 +130,22 @@ describe PostReceiveService do end end - context 'broadcast message exists' do + context 'broadcast message banner exists' do it 'outputs a broadcast message' do - broadcast_message = create(:broadcast_message, starts_at: 1.day.ago, ends_at: 1.day.from_now) + broadcast_message = create(:broadcast_message) expect(subject).to include(build_alert_message(broadcast_message.message)) end end + context 'broadcast message notification exists' do + it 'does not output a broadcast message' do + create(:broadcast_message, :notification) + + expect(has_alert_messages?(subject)).to be_falsey + end + end + context 'broadcast message does not exist' do it 'does not output a broadcast message' do expect(has_alert_messages?(subject)).to be_falsey |