diff options
Diffstat (limited to 'spec/lib/api')
-rw-r--r-- | spec/lib/api/entities/merge_request_changes_spec.rb | 57 | ||||
-rw-r--r-- | spec/lib/api/every_api_endpoint_spec.rb | 68 | ||||
-rw-r--r-- | spec/lib/api/helpers_spec.rb | 6 | ||||
-rw-r--r-- | spec/lib/api/validations/validators/email_or_email_list_spec.rb | 28 |
4 files changed, 156 insertions, 3 deletions
diff --git a/spec/lib/api/entities/merge_request_changes_spec.rb b/spec/lib/api/entities/merge_request_changes_spec.rb new file mode 100644 index 00000000000..f46d8981328 --- /dev/null +++ b/spec/lib/api/entities/merge_request_changes_spec.rb @@ -0,0 +1,57 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe ::API::Entities::MergeRequestChanges do + let_it_be(:user) { create(:user) } + let_it_be(:merge_request) { create(:merge_request) } + let(:entity) { described_class.new(merge_request, current_user: user) } + + subject(:basic_entity) { entity.as_json } + + it "exposes basic entity fields" do + is_expected.to include(:changes, :overflow) + end + + context "when #expose_raw_diffs? returns false" do + before do + expect(entity).to receive(:expose_raw_diffs?).twice.and_return(false) + expect_any_instance_of(Gitlab::Git::DiffCollection).to receive(:overflow?) + end + + it "does not access merge_request.raw_diffs" do + expect(merge_request).not_to receive(:raw_diffs) + + basic_entity + end + end + + context "when #expose_raw_diffs? returns true" do + before do + expect(entity).to receive(:expose_raw_diffs?).twice.and_return(true) + expect_any_instance_of(Gitlab::Git::DiffCollection).not_to receive(:overflow?) + end + + it "does not access merge_request.raw_diffs" do + expect(merge_request).to receive(:raw_diffs) + + basic_entity + end + end + + describe ":overflow field" do + context "when :access_raw_diffs is true" do + let_it_be(:entity_with_raw_diffs) do + described_class.new(merge_request, current_user: user, access_raw_diffs: true).as_json + end + + before do + expect_any_instance_of(Gitlab::Git::DiffCollection).not_to receive(:overflow?) + end + + it "reports false" do + expect(entity_with_raw_diffs[:overflow]).to be_falsy + end + end + end +end diff --git a/spec/lib/api/every_api_endpoint_spec.rb b/spec/lib/api/every_api_endpoint_spec.rb new file mode 100644 index 00000000000..ebf75e733d0 --- /dev/null +++ b/spec/lib/api/every_api_endpoint_spec.rb @@ -0,0 +1,68 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe 'Every API endpoint' do + context 'feature categories' do + let_it_be(:feature_categories) do + YAML.load_file(Rails.root.join('config', 'feature_categories.yml')).map(&:to_sym).to_set + end + + let_it_be(:api_endpoints) do + API::API.routes.map do |route| + [route.app.options[:for], API::Base.path_for_app(route.app)] + end + end + + let_it_be(:routes_without_category) do + api_endpoints.map do |(klass, path)| + next if klass.try(:feature_category_for_action, path) + + "#{klass}##{path}" + end.compact.uniq + end + + it "has feature categories" do + expect(routes_without_category).to be_empty, "#{routes_without_category} did not have a category" + end + + it "recognizes the feature categories" do + routes_unknown_category = api_endpoints.map do |(klass, path)| + used_category = klass.try(:feature_category_for_action, path) + next unless used_category + next if used_category == :not_owned + + [path, used_category] unless feature_categories.include?(used_category) + end.compact + + expect(routes_unknown_category).to be_empty, "#{routes_unknown_category.first(10)} had an unknown category" + end + + # This is required for API::Base.path_for_app to work, as it picks + # the first path + it "has no routes with multiple paths" do + routes_with_multiple_paths = API::API.routes.select { |route| route.app.options[:path].length != 1 } + failure_routes = routes_with_multiple_paths.map { |route| "#{route.app.options[:for]}:[#{route.app.options[:path].join(', ')}]" } + + expect(routes_with_multiple_paths).to be_empty, "#{failure_routes} have multiple paths" + end + + it "doesn't define or exclude categories on removed actions", :aggregate_failures do + api_endpoints.group_by(&:first).each do |klass, paths| + existing_paths = paths.map(&:last) + used_paths = paths_defined_in_feature_category_config(klass) + non_existing_used_paths = used_paths - existing_paths + + expect(non_existing_used_paths).to be_empty, + "#{klass} used #{non_existing_used_paths} to define feature category, but the route does not exist" + end + end + end + + def paths_defined_in_feature_category_config(klass) + (klass.try(:class_attributes) || {}).fetch(:feature_category_config, {}) + .values + .flatten + .map(&:to_s) + end +end diff --git a/spec/lib/api/helpers_spec.rb b/spec/lib/api/helpers_spec.rb index 8e738af0fa3..be5f0cc9f9a 100644 --- a/spec/lib/api/helpers_spec.rb +++ b/spec/lib/api/helpers_spec.rb @@ -176,10 +176,10 @@ RSpec.describe API::Helpers do end describe '#track_event' do - it "creates a gitlab tracking event" do - expect(Gitlab::Tracking).to receive(:event).with('foo', 'my_event', {}) - + it "creates a gitlab tracking event", :snowplow do subject.track_event('my_event', category: 'foo') + + expect_snowplow_event(category: 'foo', action: 'my_event') end it "logs an exception" do diff --git a/spec/lib/api/validations/validators/email_or_email_list_spec.rb b/spec/lib/api/validations/validators/email_or_email_list_spec.rb new file mode 100644 index 00000000000..ac3111c2319 --- /dev/null +++ b/spec/lib/api/validations/validators/email_or_email_list_spec.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe API::Validations::Validators::EmailOrEmailList do + include ApiValidatorsHelpers + + subject do + described_class.new(['email'], {}, false, scope.new) + end + + context 'with valid email addresses' do + it 'does not raise a validation error' do + expect_no_validation_error('test' => 'test@example.org') + expect_no_validation_error('test' => 'test1@example.com,test2@example.org') + expect_no_validation_error('test' => 'test1@example.com,test2@example.org,test3@example.co.uk') + end + end + + context 'including any invalid email address' do + it 'raises a validation error' do + expect_validation_error('test' => 'not') + expect_validation_error('test' => '@example.com') + expect_validation_error('test' => 'test1@example.com,asdf') + expect_validation_error('test' => 'asdf,testa1@example.com,asdf') + end + end +end |