summaryrefslogtreecommitdiff
path: root/spec/lib/api/helpers
diff options
context:
space:
mode:
Diffstat (limited to 'spec/lib/api/helpers')
-rw-r--r--spec/lib/api/helpers/authentication_spec.rb15
-rw-r--r--spec/lib/api/helpers/caching_spec.rb139
-rw-r--r--spec/lib/api/helpers/packages/dependency_proxy_helpers_spec.rb10
-rw-r--r--spec/lib/api/helpers/packages_manager_clients_helpers_spec.rb3
-rw-r--r--spec/lib/api/helpers/variables_helpers_spec.rb43
5 files changed, 207 insertions, 3 deletions
diff --git a/spec/lib/api/helpers/authentication_spec.rb b/spec/lib/api/helpers/authentication_spec.rb
index 461b0d2f6f9..eea5c10d4f8 100644
--- a/spec/lib/api/helpers/authentication_spec.rb
+++ b/spec/lib/api/helpers/authentication_spec.rb
@@ -7,6 +7,7 @@ RSpec.describe API::Helpers::Authentication do
let_it_be(:project, reload: true) { create(:project, :public) }
let_it_be(:personal_access_token) { create(:personal_access_token, user: user) }
let_it_be(:deploy_token) { create(:deploy_token, read_package_registry: true, write_package_registry: true) }
+ let_it_be(:ci_build) { create(:ci_build, :running, user: user) }
describe 'class methods' do
subject { Class.new.include(described_class::ClassMethods).new }
@@ -176,6 +177,20 @@ RSpec.describe API::Helpers::Authentication do
end
end
+ describe '#ci_build_from_namespace_inheritable' do
+ subject { object.ci_build_from_namespace_inheritable }
+
+ it 'returns #token_from_namespace_inheritable if it is a ci build' do
+ expect(object).to receive(:token_from_namespace_inheritable).and_return(ci_build)
+ expect(subject).to be(ci_build)
+ end
+
+ it 'returns nil if #token_from_namespace_inheritable is not a ci build' do
+ expect(object).to receive(:token_from_namespace_inheritable).and_return(personal_access_token)
+ expect(subject).to eq(nil)
+ end
+ end
+
describe '#user_from_namespace_inheritable' do
subject { object.user_from_namespace_inheritable }
diff --git a/spec/lib/api/helpers/caching_spec.rb b/spec/lib/api/helpers/caching_spec.rb
new file mode 100644
index 00000000000..a8cd061e123
--- /dev/null
+++ b/spec/lib/api/helpers/caching_spec.rb
@@ -0,0 +1,139 @@
+# frozen_string_literal: true
+
+require "spec_helper"
+
+RSpec.describe API::Helpers::Caching do
+ subject(:instance) { Class.new.include(described_class).new }
+
+ describe "#present_cached" do
+ let_it_be(:project) { create(:project) }
+ let_it_be(:user) { create(:user) }
+
+ let(:presenter) { API::Entities::Todo }
+
+ let(:kwargs) do
+ {
+ with: presenter,
+ project: project
+ }
+ end
+
+ subject do
+ instance.present_cached(presentable, **kwargs)
+ end
+
+ before do
+ # We have to stub #body as it's a Grape method
+ # unavailable in the module by itself
+ expect(instance).to receive(:body) do |data|
+ data
+ end
+
+ allow(instance).to receive(:current_user) { user }
+ end
+
+ context "single object" do
+ let_it_be(:presentable) { create(:todo, project: project) }
+
+ it { is_expected.to be_a(Gitlab::Json::PrecompiledJson) }
+
+ it "uses the presenter" do
+ expect(presenter).to receive(:represent).with(presentable, project: project)
+
+ subject
+ end
+
+ it "is valid JSON" do
+ parsed = Gitlab::Json.parse(subject.to_s)
+
+ expect(parsed).to be_a(Hash)
+ expect(parsed["id"]).to eq(presentable.id)
+ end
+
+ it "fetches from the cache" do
+ expect(instance.cache).to receive(:fetch).with("#{presentable.cache_key}:#{user.cache_key}", expires_in: described_class::DEFAULT_EXPIRY).once
+
+ subject
+ end
+
+ context "when a cache context is supplied" do
+ before do
+ kwargs[:cache_context] = -> (todo) { todo.project.cache_key }
+ end
+
+ it "uses the context to augment the cache key" do
+ expect(instance.cache).to receive(:fetch).with("#{presentable.cache_key}:#{project.cache_key}", expires_in: described_class::DEFAULT_EXPIRY).once
+
+ subject
+ end
+ end
+
+ context "when expires_in is supplied" do
+ it "sets the expiry when accessing the cache" do
+ kwargs[:expires_in] = 7.days
+
+ expect(instance.cache).to receive(:fetch).with("#{presentable.cache_key}:#{user.cache_key}", expires_in: 7.days).once
+
+ subject
+ end
+ end
+ end
+
+ context "for a collection of objects" do
+ let_it_be(:presentable) { Array.new(5).map { create(:todo, project: project) } }
+
+ it { is_expected.to be_an(Gitlab::Json::PrecompiledJson) }
+
+ it "uses the presenter" do
+ presentable.each do |todo|
+ expect(presenter).to receive(:represent).with(todo, project: project)
+ end
+
+ subject
+ end
+
+ it "is valid JSON" do
+ parsed = Gitlab::Json.parse(subject.to_s)
+
+ expect(parsed).to be_an(Array)
+
+ presentable.each_with_index do |todo, i|
+ expect(parsed[i]["id"]).to eq(todo.id)
+ end
+ end
+
+ it "fetches from the cache" do
+ keys = presentable.map { |todo| "#{todo.cache_key}:#{user.cache_key}" }
+
+ expect(instance.cache).to receive(:fetch_multi).with(*keys, expires_in: described_class::DEFAULT_EXPIRY).once.and_call_original
+
+ subject
+ end
+
+ context "when a cache context is supplied" do
+ before do
+ kwargs[:cache_context] = -> (todo) { todo.project.cache_key }
+ end
+
+ it "uses the context to augment the cache key" do
+ keys = presentable.map { |todo| "#{todo.cache_key}:#{project.cache_key}" }
+
+ expect(instance.cache).to receive(:fetch_multi).with(*keys, expires_in: described_class::DEFAULT_EXPIRY).once.and_call_original
+
+ subject
+ end
+ end
+
+ context "expires_in is supplied" do
+ it "sets the expiry when accessing the cache" do
+ keys = presentable.map { |todo| "#{todo.cache_key}:#{user.cache_key}" }
+ kwargs[:expires_in] = 7.days
+
+ expect(instance.cache).to receive(:fetch_multi).with(*keys, expires_in: 7.days).once.and_call_original
+
+ subject
+ end
+ end
+ end
+ end
+end
diff --git a/spec/lib/api/helpers/packages/dependency_proxy_helpers_spec.rb b/spec/lib/api/helpers/packages/dependency_proxy_helpers_spec.rb
index 6d06fc3618d..99b52236771 100644
--- a/spec/lib/api/helpers/packages/dependency_proxy_helpers_spec.rb
+++ b/spec/lib/api/helpers/packages/dependency_proxy_helpers_spec.rb
@@ -12,6 +12,10 @@ RSpec.describe API::Helpers::Packages::DependencyProxyHelpers do
subject { helper.redirect_registry_request(forward_to_registry, package_type, options) { helper.fallback } }
+ before do
+ allow(helper).to receive(:options).and_return(for: API::NpmInstancePackages)
+ end
+
shared_examples 'executing fallback' do
it 'redirects to package registry' do
expect(helper).to receive(:registry_url).never
@@ -23,13 +27,14 @@ RSpec.describe API::Helpers::Packages::DependencyProxyHelpers do
end
shared_examples 'executing redirect' do
- it 'redirects to package registry' do
- expect(helper).to receive(:track_event).with('npm_request_forward').once
+ it 'redirects to package registry', :snowplow do
expect(helper).to receive(:registry_url).once
expect(helper).to receive(:redirect).once
expect(helper).to receive(:fallback).never
subject
+
+ expect_snowplow_event(category: 'API::NpmInstancePackages', action: 'npm_request_forward')
end
end
@@ -64,7 +69,6 @@ RSpec.describe API::Helpers::Packages::DependencyProxyHelpers do
let(:package_type) { pkg_type }
it 'raises an error' do
- allow(helper).to receive(:track_event)
expect { subject }.to raise_error(ArgumentError, "Can't build registry_url for package_type #{package_type}")
end
end
diff --git a/spec/lib/api/helpers/packages_manager_clients_helpers_spec.rb b/spec/lib/api/helpers/packages_manager_clients_helpers_spec.rb
index 3c40859da21..e4c5002aa68 100644
--- a/spec/lib/api/helpers/packages_manager_clients_helpers_spec.rb
+++ b/spec/lib/api/helpers/packages_manager_clients_helpers_spec.rb
@@ -8,6 +8,7 @@ RSpec.describe API::Helpers::PackagesManagerClientsHelpers do
let_it_be(:personal_access_token) { create(:personal_access_token) }
let_it_be(:username) { personal_access_token.user.username }
let_it_be(:helper) { Class.new.include(described_class).new }
+
let(:password) { personal_access_token.token }
let(:env) do
@@ -50,6 +51,7 @@ RSpec.describe API::Helpers::PackagesManagerClientsHelpers do
describe '#find_job_from_http_basic_auth' do
let_it_be(:user) { personal_access_token.user }
+
let(:job) { create(:ci_build, user: user, status: :running) }
let(:password) { job.token }
@@ -74,6 +76,7 @@ RSpec.describe API::Helpers::PackagesManagerClientsHelpers do
describe '#find_deploy_token_from_http_basic_auth' do
let_it_be(:deploy_token) { create(:deploy_token) }
+
let(:token) { deploy_token.token }
let(:username) { deploy_token.username }
let(:password) { token }
diff --git a/spec/lib/api/helpers/variables_helpers_spec.rb b/spec/lib/api/helpers/variables_helpers_spec.rb
new file mode 100644
index 00000000000..de6bebaa827
--- /dev/null
+++ b/spec/lib/api/helpers/variables_helpers_spec.rb
@@ -0,0 +1,43 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.describe API::Helpers::VariablesHelpers do
+ let(:helper) { Class.new.include(described_class).new }
+
+ describe '#filter_variable_parameters' do
+ let(:project) { double }
+ let(:params) { double }
+
+ subject { helper.filter_variable_parameters(project, params) }
+
+ it 'returns unmodified params (overridden in EE)' do
+ expect(subject).to eq(params)
+ end
+ end
+
+ describe '#find_variable' do
+ let(:owner) { double }
+ let(:params) { double }
+ let(:variables) { [double] }
+
+ subject { helper.find_variable(owner, params) }
+
+ before do
+ expect(Ci::VariablesFinder).to receive(:new).with(owner, params)
+ .and_return(double(execute: variables))
+ end
+
+ it { is_expected.to eq(variables.first) }
+
+ context 'there are multiple variables with the supplied key' do
+ let(:variables) { [double, double] }
+
+ it 'raises a conflict!' do
+ expect(helper).to receive(:conflict!).with(/There are multiple variables with provided parameters/)
+
+ subject
+ end
+ end
+ end
+end