summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/application_context_spec.rb
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-01-03 15:08:33 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2020-01-03 15:08:33 +0000
commit511e761b41b81484c85e3d125f45873ce38e9201 (patch)
tree6bb98a6356de6e1d736951d2eef6ec83e6aa3dd2 /spec/lib/gitlab/application_context_spec.rb
parent4247e67be1faa9d52691757dad954a7fa63e8bfe (diff)
downloadgitlab-ce-511e761b41b81484c85e3d125f45873ce38e9201.tar.gz
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/lib/gitlab/application_context_spec.rb')
-rw-r--r--spec/lib/gitlab/application_context_spec.rb82
1 files changed, 82 insertions, 0 deletions
diff --git a/spec/lib/gitlab/application_context_spec.rb b/spec/lib/gitlab/application_context_spec.rb
new file mode 100644
index 00000000000..1c8fcb8d737
--- /dev/null
+++ b/spec/lib/gitlab/application_context_spec.rb
@@ -0,0 +1,82 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+describe Gitlab::ApplicationContext do
+ describe '.with_context' do
+ it 'yields the block' do
+ expect { |b| described_class.with_context({}, &b) }.to yield_control
+ end
+
+ it 'passes the expected context on to labkit' do
+ fake_proc = duck_type(:call)
+ expected_context = hash_including(user: fake_proc, project: fake_proc, root_namespace: fake_proc)
+
+ expect(Labkit::Context).to receive(:with_context).with(expected_context)
+
+ described_class.with_context(
+ user: build(:user),
+ project: build(:project),
+ namespace: build(:namespace)) {}
+ end
+
+ it 'raises an error when passing invalid options' do
+ expect { described_class.with_context(no: 'option') {} }.to raise_error(ArgumentError)
+ end
+ end
+
+ describe '.push' do
+ it 'passes the expected context on to labkit' do
+ fake_proc = duck_type(:call)
+ expected_context = hash_including(user: fake_proc, project: fake_proc, root_namespace: fake_proc)
+
+ expect(Labkit::Context).to receive(:push).with(expected_context)
+
+ described_class.push(user: build(:user))
+ end
+
+ it 'raises an error when passing invalid options' do
+ expect { described_class.push(no: 'option')}.to raise_error(ArgumentError)
+ end
+ end
+
+ describe '#to_lazy_hash' do
+ let(:user) { build(:user) }
+ let(:project) { build(:project) }
+ let(:namespace) { build(:group) }
+ let(:subgroup) { build(:group, parent: namespace) }
+
+ def result(context)
+ context.to_lazy_hash.transform_values { |v| v.call }
+ end
+
+ it 'does not call the attributes until needed' do
+ fake_proc = double('Proc')
+
+ expect(fake_proc).not_to receive(:call)
+
+ described_class.new(user: fake_proc, project: fake_proc, namespace: fake_proc).to_lazy_hash
+ end
+
+ it 'correctly loads the expected values when they are wrapped in a block' do
+ context = described_class.new(user: -> { user }, project: -> { project }, namespace: -> { subgroup })
+
+ expect(result(context))
+ .to include(user: user.username, project: project.full_path, root_namespace: namespace.full_path)
+ end
+
+ it 'correctly loads the expected values when passed directly' do
+ context = described_class.new(user: user, project: project, namespace: subgroup)
+
+ expect(result(context))
+ .to include(user: user.username, project: project.full_path, root_namespace: namespace.full_path)
+ end
+
+ it 'falls back to a projects namespace when a project is passed but no namespace' do
+ context = described_class.new(project: project)
+
+ expect(result(context))
+ .to include(project: project.full_path, root_namespace: project.full_path_components.first)
+ end
+ end
+end