2019-04-11 12:17:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-02-20 10:53:15 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 15:08:50 +00:00
|
|
|
RSpec.describe SystemHooksService do
|
2017-03-27 21:14:01 +00:00
|
|
|
let(:user) { create(:user) }
|
2017-08-02 19:55:11 +00:00
|
|
|
let(:project) { create(:project) }
|
2017-03-27 21:14:01 +00:00
|
|
|
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) }
|
2013-02-20 10:53:15 +00:00
|
|
|
|
2013-10-08 08:06:38 +00:00
|
|
|
context 'event data' do
|
2016-01-07 17:54:35 +00:00
|
|
|
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) }
|
2015-05-05 15:13:11 +00:00
|
|
|
it { expect(event_data(project, :create)).to include(:event_name, :name, :created_at, :updated_at, :path, :project_id, :owner_name, :owner_email, :project_visibility) }
|
2016-08-08 13:31:35 +00:00
|
|
|
it { expect(event_data(project, :update)).to include(:event_name, :name, :created_at, :updated_at, :path, :project_id, :owner_name, :owner_email, :project_visibility) }
|
2015-05-05 15:13:11 +00:00
|
|
|
it { expect(event_data(project, :destroy)).to include(:event_name, :name, :created_at, :updated_at, :path, :project_id, :owner_name, :owner_email, :project_visibility) }
|
2016-01-13 09:12:30 +00:00
|
|
|
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) }
|
2019-08-28 23:27:18 +00:00
|
|
|
it { expect(event_data(project_member, :update)).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-02-12 18:17:35 +00:00
|
|
|
it { expect(event_data(key, :create)).to include(:username, :key, :id) }
|
|
|
|
it { expect(event_data(key, :destroy)).to include(:username, :key, :id) }
|
2017-03-16 13:28:34 +00:00
|
|
|
it { expect(event_data(deploy_key, :create)).to include(:key, :id) }
|
|
|
|
it { expect(event_data(deploy_key, :destroy)).to include(:key, :id) }
|
2014-08-21 10:53:32 +00:00
|
|
|
|
2015-05-05 15:13:11 +00:00
|
|
|
it do
|
|
|
|
project.old_path_with_namespace = 'renamed_from_path'
|
|
|
|
expect(event_data(project, :rename)).to include(
|
2016-01-13 09:12:30 +00:00
|
|
|
:event_name, :name, :created_at, :updated_at, :path, :project_id,
|
|
|
|
:owner_name, :owner_email, :project_visibility,
|
2015-05-05 15:13:11 +00:00
|
|
|
:old_path_with_namespace
|
2016-01-13 09:12:30 +00:00
|
|
|
)
|
2015-05-05 15:13:11 +00:00
|
|
|
end
|
2018-03-05 13:41:54 +00:00
|
|
|
|
2015-05-05 15:13:11 +00:00
|
|
|
it do
|
2018-11-15 10:13:50 +00:00
|
|
|
project.old_path_with_namespace = 'transferred_from_path'
|
2015-05-05 15:13:11 +00:00
|
|
|
expect(event_data(project, :transfer)).to include(
|
2016-01-13 09:12:30 +00:00
|
|
|
:event_name, :name, :created_at, :updated_at, :path, :project_id,
|
|
|
|
:owner_name, :owner_email, :project_visibility,
|
2015-05-05 15:13:11 +00:00
|
|
|
:old_path_with_namespace
|
2016-01-13 09:12:30 +00:00
|
|
|
)
|
2015-05-05 15:13:11 +00:00
|
|
|
end
|
|
|
|
|
2014-08-21 10:53:32 +00:00
|
|
|
it do
|
2015-02-12 18:17:35 +00:00
|
|
|
expect(event_data(group, :create)).to include(
|
2016-01-13 09:12:30 +00:00
|
|
|
:event_name, :name, :created_at, :updated_at, :path, :group_id,
|
2015-05-05 15:13:11 +00:00
|
|
|
:owner_name, :owner_email
|
2014-08-21 10:53:32 +00:00
|
|
|
)
|
|
|
|
end
|
2018-03-05 13:41:54 +00:00
|
|
|
|
2014-08-21 10:53:32 +00:00
|
|
|
it do
|
2015-02-12 18:17:35 +00:00
|
|
|
expect(event_data(group, :destroy)).to include(
|
2016-01-13 09:12:30 +00:00
|
|
|
:event_name, :name, :created_at, :updated_at, :path, :group_id,
|
2015-05-05 15:13:11 +00:00
|
|
|
:owner_name, :owner_email
|
2014-08-21 10:53:32 +00:00
|
|
|
)
|
|
|
|
end
|
2018-03-05 13:41:54 +00:00
|
|
|
|
2014-08-21 10:53:32 +00:00
|
|
|
it do
|
2015-02-12 18:17:35 +00:00
|
|
|
expect(event_data(group_member, :create)).to include(
|
2016-01-13 09:12:30 +00:00
|
|
|
:event_name, :created_at, :updated_at, :group_name, :group_path,
|
2016-01-07 17:54:54 +00:00
|
|
|
:group_id, :user_id, :user_username, :user_name, :user_email, :group_access
|
2014-08-21 10:53:32 +00:00
|
|
|
)
|
|
|
|
end
|
2018-03-05 13:41:54 +00:00
|
|
|
|
2014-08-21 10:53:32 +00:00
|
|
|
it do
|
2015-02-12 18:17:35 +00:00
|
|
|
expect(event_data(group_member, :destroy)).to include(
|
2016-01-13 09:12:30 +00:00
|
|
|
:event_name, :created_at, :updated_at, :group_name, :group_path,
|
2016-01-07 17:54:54 +00:00
|
|
|
:group_id, :user_id, :user_username, :user_name, :user_email, :group_access
|
2014-08-21 10:53:32 +00:00
|
|
|
)
|
|
|
|
end
|
2017-10-27 14:09:11 +00:00
|
|
|
|
2019-08-28 23:27:18 +00:00
|
|
|
it do
|
|
|
|
expect(event_data(group_member, :update)).to include(
|
|
|
|
:event_name, :created_at, :updated_at, :group_name, :group_path,
|
|
|
|
:group_id, :user_id, :user_username, :user_name, :user_email, :group_access
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2017-10-27 14:09:11 +00:00
|
|
|
it 'includes the correct project visibility level' do
|
|
|
|
data = event_data(project, :create)
|
|
|
|
|
|
|
|
expect(data[:project_visibility]).to eq('private')
|
|
|
|
end
|
2017-11-03 11:26:52 +00:00
|
|
|
|
2018-03-05 21:37:37 +00:00
|
|
|
it 'handles nil datetime columns' do
|
2020-10-21 15:10:28 +00:00
|
|
|
user.update!(created_at: nil, updated_at: nil)
|
2018-03-05 21:37:37 +00:00
|
|
|
data = event_data(user, :destroy)
|
|
|
|
|
|
|
|
expect(data[:created_at]).to be(nil)
|
|
|
|
expect(data[:updated_at]).to be(nil)
|
|
|
|
end
|
|
|
|
|
2017-11-03 11:26:52 +00:00
|
|
|
context 'group_rename' do
|
|
|
|
it 'contains old and new path' do
|
2019-04-23 09:30:18 +00:00
|
|
|
allow(group).to receive(:path_before_last_save).and_return('old-path')
|
2017-11-03 11:26:52 +00:00
|
|
|
|
|
|
|
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-04-23 09:30:18 +00:00
|
|
|
expect(data[:old_path]).to eq(group.path_before_last_save)
|
|
|
|
expect(data[:old_full_path]).to eq(group.path_before_last_save)
|
2017-11-03 11:26:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'contains old and new full_path for subgroup' do
|
|
|
|
subgroup = create(:group, parent: group)
|
2019-04-23 09:30:18 +00:00
|
|
|
allow(subgroup).to receive(:path_before_last_save).and_return('old-path')
|
2017-11-03 11:26:52 +00:00
|
|
|
|
|
|
|
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-04-23 09:30:18 +00:00
|
|
|
allow(user).to receive(:username_before_last_save).and_return('old-username')
|
2017-11-03 11:26:52 +00:00
|
|
|
|
|
|
|
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-04-23 09:30:18 +00:00
|
|
|
expect(data[:old_username]).to eq(user.username_before_last_save)
|
2017-11-03 11:26:52 +00:00
|
|
|
end
|
|
|
|
end
|
2018-01-15 05:10:48 +00:00
|
|
|
|
|
|
|
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
|
2013-02-20 10:53:15 +00:00
|
|
|
end
|
|
|
|
|
2013-10-08 08:06:38 +00:00
|
|
|
context 'event names' do
|
2015-02-12 18:17:35 +00:00
|
|
|
it { expect(event_name(user, :create)).to eq "user_create" }
|
|
|
|
it { expect(event_name(user, :destroy)).to eq "user_destroy" }
|
2017-11-03 11:26:52 +00:00
|
|
|
it { expect(event_name(user, :rename)).to eq 'user_rename' }
|
2018-01-15 05:10:48 +00:00
|
|
|
it { expect(event_name(user, :failed_login)).to eq 'user_failed_login' }
|
2015-02-12 18:17:35 +00:00
|
|
|
it { expect(event_name(project, :create)).to eq "project_create" }
|
|
|
|
it { expect(event_name(project, :destroy)).to eq "project_destroy" }
|
2015-05-05 15:13:11 +00:00
|
|
|
it { expect(event_name(project, :rename)).to eq "project_rename" }
|
|
|
|
it { expect(event_name(project, :transfer)).to eq "project_transfer" }
|
2016-08-08 13:31:35 +00:00
|
|
|
it { expect(event_name(project, :update)).to eq "project_update" }
|
2015-02-12 18:17:35 +00:00
|
|
|
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" }
|
2019-08-28 23:27:18 +00:00
|
|
|
it { expect(event_name(project_member, :update)).to eq "user_update_for_team" }
|
2015-02-12 18:17:35 +00:00
|
|
|
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' }
|
2017-11-03 11:26:52 +00:00
|
|
|
it { expect(event_name(group, :rename)).to eq 'group_rename' }
|
2013-10-08 08:06:38 +00:00
|
|
|
end
|
2013-02-20 10:53:15 +00:00
|
|
|
|
2013-10-08 08:06:38 +00:00
|
|
|
def event_data(*args)
|
2014-01-15 12:26:05 +00:00
|
|
|
SystemHooksService.new.send :build_event_data, *args
|
2013-10-08 08:06:38 +00:00
|
|
|
end
|
2013-02-20 10:53:15 +00:00
|
|
|
|
2013-10-08 08:06:38 +00:00
|
|
|
def event_name(*args)
|
2014-01-15 12:26:05 +00:00
|
|
|
SystemHooksService.new.send :build_event_name, *args
|
2013-02-20 10:53:15 +00:00
|
|
|
end
|
|
|
|
end
|