Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorRémy Coutable <remy@rymai.me>2017-10-05 20:02:50 +0300
committerRémy Coutable <remy@rymai.me>2017-10-09 17:44:47 +0300
commitf070265a6ddd0173c8924bfcd7791ecafa15ab7e (patch)
treecf364687889aa546f39b607aa57db839d7059c97 /spec
parent075d6516047d899746d22b5323d3b74558e200d0 (diff)
Introduce new hook data builders for Issue and MergeRequest
Signed-off-by: Rémy Coutable <remy@rymai.me>
Diffstat (limited to 'spec')
-rw-r--r--spec/lib/gitlab/hook_data/issuable_builder_spec.rb18
-rw-r--r--spec/lib/gitlab/hook_data/issue_builder_spec.rb46
-rw-r--r--spec/lib/gitlab/hook_data/merge_request_builder_spec.rb62
-rw-r--r--spec/models/concerns/issuable_spec.rb57
-rw-r--r--spec/models/issue_spec.rb44
-rw-r--r--spec/models/merge_request_spec.rb59
-rw-r--r--spec/support/shared_examples/models/issuable_hook_data_shared_examples.rb57
7 files changed, 230 insertions, 113 deletions
diff --git a/spec/lib/gitlab/hook_data/issuable_builder_spec.rb b/spec/lib/gitlab/hook_data/issuable_builder_spec.rb
index 31cc5eaea88..30da56bec16 100644
--- a/spec/lib/gitlab/hook_data/issuable_builder_spec.rb
+++ b/spec/lib/gitlab/hook_data/issuable_builder_spec.rb
@@ -37,15 +37,23 @@ describe Gitlab::HookData::IssuableBuilder do
lock_version: %w[foo bar],
merge_jid: %w[foo bar],
title: ['A title', 'Hello World'],
- title_html: %w[foo bar]
+ title_html: %w[foo bar],
+ labels: [
+ [{ id: 1, title: 'foo' }],
+ [{ id: 1, title: 'foo' }, { id: 2, title: 'bar' }]
+ ]
}
end
let(:data) { builder.build(user: user, changes: changes) }
it 'populates the :changes hash' do
expect(data[:changes]).to match(hash_including({
- title: ['A title', 'Hello World'],
- description: ['A description', 'A cool description']
+ title: { previous: 'A title', current: 'Hello World' },
+ description: { previous: 'A description', current: 'A cool description' },
+ labels: {
+ previous: [{ id: 1, title: 'foo' }],
+ current: [{ id: 1, title: 'foo' }, { id: 2, title: 'bar' }]
+ }
}))
end
@@ -73,7 +81,7 @@ describe Gitlab::HookData::IssuableBuilder do
end
context 'issue is assigned' do
- let(:issue) { create(:issue).tap { |i| i.assignees << user } }
+ let(:issue) { create(:issue, assignees: [user]) }
let(:data) { described_class.new(issue).build(user: user) }
it 'returns correct hook data' do
@@ -84,7 +92,7 @@ describe Gitlab::HookData::IssuableBuilder do
end
context 'merge_request is assigned' do
- let(:merge_request) { create(:merge_request).tap { |mr| mr.update(assignee: user) } }
+ let(:merge_request) { create(:merge_request, assignee: user) }
let(:data) { described_class.new(merge_request).build(user: user) }
it 'returns correct hook data' do
diff --git a/spec/lib/gitlab/hook_data/issue_builder_spec.rb b/spec/lib/gitlab/hook_data/issue_builder_spec.rb
new file mode 100644
index 00000000000..6c529cdd051
--- /dev/null
+++ b/spec/lib/gitlab/hook_data/issue_builder_spec.rb
@@ -0,0 +1,46 @@
+require 'spec_helper'
+
+describe Gitlab::HookData::IssueBuilder do
+ set(:issue) { create(:issue) }
+ let(:builder) { described_class.new(issue) }
+
+ describe '#build' do
+ let(:data) { builder.build }
+
+ it 'includes safe attribute' do
+ %w[
+ assignee_id
+ author_id
+ branch_name
+ closed_at
+ confidential
+ created_at
+ deleted_at
+ description
+ due_date
+ id
+ iid
+ last_edited_at
+ last_edited_by_id
+ milestone_id
+ moved_to_id
+ project_id
+ relative_position
+ state
+ time_estimate
+ title
+ updated_at
+ updated_by_id
+ ].each do |key|
+ expect(data).to include(key)
+ end
+ end
+
+ it 'includes additional attrs' do
+ expect(data).to include(:total_time_spent)
+ expect(data).to include(:human_time_estimate)
+ expect(data).to include(:human_total_time_spent)
+ expect(data).to include(:assignee_ids)
+ end
+ end
+end
diff --git a/spec/lib/gitlab/hook_data/merge_request_builder_spec.rb b/spec/lib/gitlab/hook_data/merge_request_builder_spec.rb
new file mode 100644
index 00000000000..92bf87bbad4
--- /dev/null
+++ b/spec/lib/gitlab/hook_data/merge_request_builder_spec.rb
@@ -0,0 +1,62 @@
+require 'spec_helper'
+
+describe Gitlab::HookData::MergeRequestBuilder do
+ set(:merge_request) { create(:merge_request) }
+ let(:builder) { described_class.new(merge_request) }
+
+ describe '#build' do
+ let(:data) { builder.build }
+
+ it 'includes safe attribute' do
+ %w[
+ assignee_id
+ author_id
+ created_at
+ deleted_at
+ description
+ head_pipeline_id
+ id
+ iid
+ last_edited_at
+ last_edited_by_id
+ merge_commit_sha
+ merge_error
+ merge_params
+ merge_status
+ merge_user_id
+ merge_when_pipeline_succeeds
+ milestone_id
+ ref_fetched
+ source_branch
+ source_project_id
+ state
+ target_branch
+ target_project_id
+ time_estimate
+ title
+ updated_at
+ updated_by_id
+ ].each do |key|
+ expect(data).to include(key)
+ end
+ end
+
+ %i[source target].each do |key|
+ describe "#{key} key" do
+ include_examples 'project hook data', project_key: key do
+ let(:project) { merge_request.public_send("#{key}_project") }
+ end
+ end
+ end
+
+ it 'includes additional attrs' do
+ expect(data).to include(:source)
+ expect(data).to include(:target)
+ expect(data).to include(:last_commit)
+ expect(data).to include(:work_in_progress)
+ expect(data).to include(:total_time_spent)
+ expect(data).to include(:human_time_estimate)
+ expect(data).to include(:human_total_time_spent)
+ end
+ end
+end
diff --git a/spec/models/concerns/issuable_spec.rb b/spec/models/concerns/issuable_spec.rb
index 1f8541a3262..ba57301a3c9 100644
--- a/spec/models/concerns/issuable_spec.rb
+++ b/spec/models/concerns/issuable_spec.rb
@@ -267,53 +267,70 @@ describe Issuable do
describe '#to_hook_data' do
context 'labels are updated' do
let(:labels) { create_list(:label, 2) }
- let(:data) { issue.to_hook_data(user, old_labels: [labels[0]]) }
before do
issue.update(labels: [labels[1]])
end
- it 'includes labels in the hook data' do
- expect(data[:labels]).to eq([labels[1].hook_attrs])
- expect(data[:changes]).to match(hash_including({
- labels: [[labels[0].hook_attrs], [labels[1].hook_attrs]]
- }))
+ it 'delegates to Gitlab::HookData::IssuableBuilder#build' do
+ builder = double
+
+ expect(Gitlab::HookData::IssuableBuilder)
+ .to receive(:new).with(issue).and_return(builder)
+ expect(builder).to receive(:build).with(
+ user: user,
+ changes: hash_including(
+ 'labels' => [[labels[0].hook_attrs], [labels[1].hook_attrs]]
+ ))
+
+ issue.to_hook_data(user, old_labels: [labels[0]])
end
end
context 'issue is assigned' do
let(:user2) { create(:user) }
- let(:data) { issue.to_hook_data(user, old_assignees: [user]) }
before do
issue.assignees << user << user2
end
- it 'returns correct hook data' do
- expect(data[:assignees]).to eq([user.hook_attrs, user2.hook_attrs])
- expect(data[:changes]).to match(hash_including({
- assignees: [[user.hook_attrs], [user.hook_attrs, user2.hook_attrs]]
- }))
+ it 'delegates to Gitlab::HookData::IssuableBuilder#build' do
+ builder = double
+
+ expect(Gitlab::HookData::IssuableBuilder)
+ .to receive(:new).with(issue).and_return(builder)
+ expect(builder).to receive(:build).with(
+ user: user,
+ changes: hash_including(
+ 'assignees' => [[user.hook_attrs], [user.hook_attrs, user2.hook_attrs]]
+ ))
+
+ issue.to_hook_data(user, old_assignees: [user])
end
end
context 'merge_request is assigned' do
let(:merge_request) { create(:merge_request) }
let(:user2) { create(:user) }
- let(:data) { merge_request.to_hook_data(user, old_assignees: [user]) }
before do
merge_request.update(assignee: user)
merge_request.update(assignee: user2)
end
- it 'returns correct hook data' do
- expect(data[:object_attributes]['assignee_id']).to eq(user2.id)
- expect(data[:assignee]).to eq(user2.hook_attrs)
- expect(data[:changes]).to match(hash_including({
- assignee_id: [user.id, user2.id],
- assignee: [user.hook_attrs, user2.hook_attrs]
- }))
+ it 'delegates to Gitlab::HookData::IssuableBuilder#build' do
+ builder = double
+
+ expect(Gitlab::HookData::IssuableBuilder)
+ .to receive(:new).with(merge_request).and_return(builder)
+ expect(builder).to receive(:build).with(
+ user: user,
+ changes: hash_including(
+ 'assignee_id' => [user.id, user2.id],
+ 'assignee' => [user.hook_attrs, user2.hook_attrs]
+ ))
+
+ merge_request.to_hook_data(user, old_assignees: [user])
end
end
end
diff --git a/spec/models/issue_spec.rb b/spec/models/issue_spec.rb
index bd1ae3c4945..bb5033c1628 100644
--- a/spec/models/issue_spec.rb
+++ b/spec/models/issue_spec.rb
@@ -700,42 +700,14 @@ describe Issue do
end
describe '#hook_attrs' do
- let(:attrs_hash) { subject.hook_attrs }
-
- it 'includes safe attribute' do
- %w[
- assignee_id
- author_id
- branch_name
- closed_at
- confidential
- created_at
- deleted_at
- description
- due_date
- id
- iid
- last_edited_at
- last_edited_by_id
- milestone_id
- moved_to_id
- project_id
- relative_position
- state
- time_estimate
- title
- updated_at
- updated_by_id
- ].each do |key|
- expect(attrs_hash).to include(key)
- end
- end
-
- it 'includes additional attrs' do
- expect(attrs_hash).to include(:total_time_spent)
- expect(attrs_hash).to include(:human_time_estimate)
- expect(attrs_hash).to include(:human_total_time_spent)
- expect(attrs_hash).to include(:assignee_ids)
+ it 'delegates to Gitlab::HookData::IssueBuilder#build' do
+ builder = double
+
+ expect(Gitlab::HookData::IssueBuilder)
+ .to receive(:new).with(subject).and_return(builder)
+ expect(builder).to receive(:build)
+
+ subject.hook_attrs
end
end
diff --git a/spec/models/merge_request_spec.rb b/spec/models/merge_request_spec.rb
index 844ada57e22..17c9f15b021 100644
--- a/spec/models/merge_request_spec.rb
+++ b/spec/models/merge_request_spec.rb
@@ -661,59 +661,14 @@ describe MergeRequest do
end
describe '#hook_attrs' do
- let(:attrs_hash) { subject.hook_attrs }
-
- it 'includes safe attribute' do
- %w[
- assignee_id
- author_id
- created_at
- deleted_at
- description
- head_pipeline_id
- id
- iid
- last_edited_at
- last_edited_by_id
- merge_commit_sha
- merge_error
- merge_params
- merge_status
- merge_user_id
- merge_when_pipeline_succeeds
- milestone_id
- ref_fetched
- source_branch
- source_project_id
- state
- target_branch
- target_project_id
- time_estimate
- title
- updated_at
- updated_by_id
- ].each do |key|
- expect(attrs_hash).to include(key)
- end
- end
-
- %i[source target].each do |key|
- describe "#{key} key" do
- include_examples 'project hook data', project_key: key do
- let(:data) { attrs_hash }
- let(:project) { subject.public_send("#{key}_project") }
- end
- end
- end
+ it 'delegates to Gitlab::HookData::MergeRequestBuilder#build' do
+ builder = double
+
+ expect(Gitlab::HookData::MergeRequestBuilder)
+ .to receive(:new).with(subject).and_return(builder)
+ expect(builder).to receive(:build)
- it 'includes additional attrs' do
- expect(attrs_hash).to include(:source)
- expect(attrs_hash).to include(:target)
- expect(attrs_hash).to include(:last_commit)
- expect(attrs_hash).to include(:work_in_progress)
- expect(attrs_hash).to include(:total_time_spent)
- expect(attrs_hash).to include(:human_time_estimate)
- expect(attrs_hash).to include(:human_total_time_spent)
+ subject.hook_attrs
end
end
diff --git a/spec/support/shared_examples/models/issuable_hook_data_shared_examples.rb b/spec/support/shared_examples/models/issuable_hook_data_shared_examples.rb
new file mode 100644
index 00000000000..a4762b68858
--- /dev/null
+++ b/spec/support/shared_examples/models/issuable_hook_data_shared_examples.rb
@@ -0,0 +1,57 @@
+# This shared example requires a `builder` and `user` variable
+shared_examples 'issuable hook data' do |kind|
+ let(:data) { builder.build(user: user) }
+
+ include_examples 'project hook data' do
+ let(:project) { builder.issuable.project }
+ end
+ include_examples 'deprecated repository hook data'
+
+ context "with a #{kind}" do
+ it 'contains issuable data' do
+ expect(data[:object_kind]).to eq(kind)
+ expect(data[:user]).to eq(user.hook_attrs)
+ expect(data[:project]).to eq(builder.issuable.project.hook_attrs)
+ expect(data[:object_attributes]).to eq(builder.issuable.hook_attrs)
+ expect(data[:changes]).to eq({})
+ expect(data[:repository]).to eq(builder.issuable.project.hook_attrs.slice(:name, :url, :description, :homepage))
+ end
+
+ it 'does not contain certain keys' do
+ expect(data).not_to have_key(:assignees)
+ expect(data).not_to have_key(:assignee)
+ end
+
+ describe 'changes are given' do
+ let(:changes) do
+ {
+ cached_markdown_version: %w[foo bar],
+ description: ['A description', 'A cool description'],
+ description_html: %w[foo bar],
+ in_progress_merge_commit_sha: %w[foo bar],
+ lock_version: %w[foo bar],
+ merge_jid: %w[foo bar],
+ title: ['A title', 'Hello World'],
+ title_html: %w[foo bar]
+ }
+ end
+ let(:data) { builder.build(user: user, changes: changes) }
+
+ it 'populates the :changes hash' do
+ expect(data[:changes]).to match(hash_including({
+ title: { previous: 'A title', current: 'Hello World' },
+ description: { previous: 'A description', current: 'A cool description' }
+ }))
+ end
+
+ it 'does not contain certain keys' do
+ expect(data[:changes]).not_to have_key('cached_markdown_version')
+ expect(data[:changes]).not_to have_key('description_html')
+ expect(data[:changes]).not_to have_key('lock_version')
+ expect(data[:changes]).not_to have_key('title_html')
+ expect(data[:changes]).not_to have_key('in_progress_merge_commit_sha')
+ expect(data[:changes]).not_to have_key('merge_jid')
+ end
+ end
+ end
+end