debian-mirror-gitlab/spec/services/system_hooks_service_spec.rb

165 lines
7.1 KiB
Ruby
Raw Normal View History

2019-07-31 22:56:46 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
require 'spec_helper'
2017-09-10 17:25:29 +05:30
describe SystemHooksService do
2017-08-17 22:00:37 +05:30
let(:user) { create(:user) }
2017-09-10 17:25:29 +05:30
let(:project) { create(:project) }
2017-08-17 22:00:37 +05:30
let(:project_member) { create(:project_member) }
let(:key) { create(:key, user: user) }
let(:deploy_key) { create(:key) }
let(:group) { create(:group) }
let(:group_member) { create(:group_member) }
2014-09-02 18:07:02 +05:30
context 'event data' do
it { expect(event_data(user, :create)).to include(:event_name, :name, :created_at, :updated_at, :email, :user_id, :username) }
it { expect(event_data(user, :destroy)).to include(:event_name, :name, :created_at, :updated_at, :email, :user_id, :username) }
it { expect(event_data(project, :create)).to include(:event_name, :name, :created_at, :updated_at, :path, :project_id, :owner_name, :owner_email, :project_visibility) }
2017-08-17 22:00:37 +05:30
it { expect(event_data(project, :update)).to include(:event_name, :name, :created_at, :updated_at, :path, :project_id, :owner_name, :owner_email, :project_visibility) }
it { expect(event_data(project, :destroy)).to include(:event_name, :name, :created_at, :updated_at, :path, :project_id, :owner_name, :owner_email, :project_visibility) }
it { expect(event_data(project_member, :create)).to include(:event_name, :created_at, :updated_at, :project_name, :project_path, :project_path_with_namespace, :project_id, :user_name, :user_username, :user_email, :user_id, :access_level, :project_visibility) }
it { expect(event_data(project_member, :destroy)).to include(:event_name, :created_at, :updated_at, :project_name, :project_path, :project_path_with_namespace, :project_id, :user_name, :user_username, :user_email, :user_id, :access_level, :project_visibility) }
2015-04-26 12:48:37 +05:30
it { expect(event_data(key, :create)).to include(:username, :key, :id) }
it { expect(event_data(key, :destroy)).to include(:username, :key, :id) }
2017-08-17 22:00:37 +05:30
it { expect(event_data(deploy_key, :create)).to include(:key, :id) }
it { expect(event_data(deploy_key, :destroy)).to include(:key, :id) }
2015-04-26 12:48:37 +05:30
it do
project.old_path_with_namespace = 'renamed_from_path'
expect(event_data(project, :rename)).to include(
:event_name, :name, :created_at, :updated_at, :path, :project_id,
:owner_name, :owner_email, :project_visibility,
:old_path_with_namespace
)
end
2018-03-27 19:54:05 +05:30
it do
2019-02-15 15:39:39 +05:30
project.old_path_with_namespace = 'transferred_from_path'
expect(event_data(project, :transfer)).to include(
:event_name, :name, :created_at, :updated_at, :path, :project_id,
:owner_name, :owner_email, :project_visibility,
:old_path_with_namespace
)
end
2015-04-26 12:48:37 +05:30
it do
expect(event_data(group, :create)).to include(
:event_name, :name, :created_at, :updated_at, :path, :group_id,
:owner_name, :owner_email
2015-04-26 12:48:37 +05:30
)
end
2018-03-27 19:54:05 +05:30
2015-04-26 12:48:37 +05:30
it do
expect(event_data(group, :destroy)).to include(
:event_name, :name, :created_at, :updated_at, :path, :group_id,
:owner_name, :owner_email
2015-04-26 12:48:37 +05:30
)
end
2018-03-27 19:54:05 +05:30
2015-04-26 12:48:37 +05:30
it do
expect(event_data(group_member, :create)).to include(
:event_name, :created_at, :updated_at, :group_name, :group_path,
:group_id, :user_id, :user_username, :user_name, :user_email, :group_access
2015-04-26 12:48:37 +05:30
)
end
2018-03-27 19:54:05 +05:30
2015-04-26 12:48:37 +05:30
it do
expect(event_data(group_member, :destroy)).to include(
:event_name, :created_at, :updated_at, :group_name, :group_path,
:group_id, :user_id, :user_username, :user_name, :user_email, :group_access
2015-04-26 12:48:37 +05:30
)
end
2018-03-17 18:26:18 +05:30
it 'includes the correct project visibility level' do
data = event_data(project, :create)
expect(data[:project_visibility]).to eq('private')
end
2018-03-27 19:54:05 +05:30
it 'handles nil datetime columns' do
2018-11-18 11:00:15 +05:30
user.update(created_at: nil, updated_at: nil)
2018-03-27 19:54:05 +05:30
data = event_data(user, :destroy)
expect(data[:created_at]).to be(nil)
expect(data[:updated_at]).to be(nil)
end
2018-03-17 18:26:18 +05:30
context 'group_rename' do
it 'contains old and new path' do
2019-07-31 22:56:46 +05:30
allow(group).to receive(:path_before_last_save).and_return('old-path')
2018-03-17 18:26:18 +05:30
data = event_data(group, :rename)
expect(data).to include(:event_name, :name, :created_at, :updated_at, :full_path, :path, :group_id, :old_path, :old_full_path)
expect(data[:path]).to eq(group.path)
expect(data[:full_path]).to eq(group.path)
2019-07-31 22:56:46 +05:30
expect(data[:old_path]).to eq(group.path_before_last_save)
expect(data[:old_full_path]).to eq(group.path_before_last_save)
2018-03-17 18:26:18 +05:30
end
it 'contains old and new full_path for subgroup' do
subgroup = create(:group, parent: group)
2019-07-31 22:56:46 +05:30
allow(subgroup).to receive(:path_before_last_save).and_return('old-path')
2018-03-17 18:26:18 +05:30
data = event_data(subgroup, :rename)
expect(data[:full_path]).to eq(subgroup.full_path)
expect(data[:old_path]).to eq('old-path')
end
end
context 'user_rename' do
it 'contains old and new username' do
2019-07-31 22:56:46 +05:30
allow(user).to receive(:username_before_last_save).and_return('old-username')
2018-03-17 18:26:18 +05:30
data = event_data(user, :rename)
expect(data).to include(:event_name, :name, :created_at, :updated_at, :email, :user_id, :username, :old_username)
expect(data[:username]).to eq(user.username)
2019-07-31 22:56:46 +05:30
expect(data[:old_username]).to eq(user.username_before_last_save)
2018-03-17 18:26:18 +05:30
end
end
context 'user_failed_login' do
it 'contains state of user' do
user.ldap_block!
data = event_data(user, :failed_login)
expect(data).to include(:event_name, :name, :created_at, :updated_at, :email, :user_id, :username, :state)
expect(data[:username]).to eq(user.username)
expect(data[:state]).to eq('ldap_blocked')
end
end
2014-09-02 18:07:02 +05:30
end
context 'event names' do
2015-04-26 12:48:37 +05:30
it { expect(event_name(user, :create)).to eq "user_create" }
it { expect(event_name(user, :destroy)).to eq "user_destroy" }
2018-03-17 18:26:18 +05:30
it { expect(event_name(user, :rename)).to eq 'user_rename' }
it { expect(event_name(user, :failed_login)).to eq 'user_failed_login' }
2015-04-26 12:48:37 +05:30
it { expect(event_name(project, :create)).to eq "project_create" }
it { expect(event_name(project, :destroy)).to eq "project_destroy" }
it { expect(event_name(project, :rename)).to eq "project_rename" }
it { expect(event_name(project, :transfer)).to eq "project_transfer" }
2017-08-17 22:00:37 +05:30
it { expect(event_name(project, :update)).to eq "project_update" }
2015-04-26 12:48:37 +05:30
it { expect(event_name(project_member, :create)).to eq "user_add_to_team" }
it { expect(event_name(project_member, :destroy)).to eq "user_remove_from_team" }
it { expect(event_name(key, :create)).to eq 'key_create' }
it { expect(event_name(key, :destroy)).to eq 'key_destroy' }
it { expect(event_name(group, :create)).to eq 'group_create' }
it { expect(event_name(group, :destroy)).to eq 'group_destroy' }
2018-03-17 18:26:18 +05:30
it { expect(event_name(group, :rename)).to eq 'group_rename' }
2015-04-26 12:48:37 +05:30
it { expect(event_name(group_member, :create)).to eq 'user_add_to_group' }
it { expect(event_name(group_member, :destroy)).to eq 'user_remove_from_group' }
2014-09-02 18:07:02 +05:30
end
def event_data(*args)
SystemHooksService.new.send :build_event_data, *args
end
def event_name(*args)
SystemHooksService.new.send :build_event_name, *args
end
end