Commit 9816856d authored by Alexis Reigel's avatar Alexis Reigel

perform signature update in sidekiq worker

parent 9d30a80d
...@@ -28,7 +28,7 @@ class GpgKey < ActiveRecord::Base ...@@ -28,7 +28,7 @@ class GpgKey < ActiveRecord::Base
unless: -> { errors.has_key?(:key) } unless: -> { errors.has_key?(:key) }
before_validation :extract_fingerprint, :extract_primary_keyid before_validation :extract_fingerprint, :extract_primary_keyid
after_create :update_invalid_gpg_signatures after_create :update_invalid_gpg_signatures_after_create
after_create :notify_user after_create :notify_user
def key=(value) def key=(value)
...@@ -54,7 +54,7 @@ class GpgKey < ActiveRecord::Base ...@@ -54,7 +54,7 @@ class GpgKey < ActiveRecord::Base
end end
def update_invalid_gpg_signatures def update_invalid_gpg_signatures
Gitlab::Gpg::InvalidGpgSignatureUpdater.new(self).run InvalidGpgSignatureUpdateWorker.perform_async(self.id)
end end
private private
...@@ -74,4 +74,8 @@ class GpgKey < ActiveRecord::Base ...@@ -74,4 +74,8 @@ class GpgKey < ActiveRecord::Base
def notify_user def notify_user
run_after_commit { NotificationService.new.new_gpg_key(self) } run_after_commit { NotificationService.new.new_gpg_key(self) }
end end
def update_invalid_gpg_signatures_after_create
run_after_commit { update_invalid_gpg_signatures }
end
end end
...@@ -13,6 +13,7 @@ class User < ActiveRecord::Base ...@@ -13,6 +13,7 @@ class User < ActiveRecord::Base
include IgnorableColumn include IgnorableColumn
include FeatureGate include FeatureGate
include CreatedAtFilterable include CreatedAtFilterable
include AfterCommitQueue
DEFAULT_NOTIFICATION_LEVEL = :participating DEFAULT_NOTIFICATION_LEVEL = :participating
...@@ -515,7 +516,7 @@ class User < ActiveRecord::Base ...@@ -515,7 +516,7 @@ class User < ActiveRecord::Base
end end
def update_invalid_gpg_signatures def update_invalid_gpg_signatures
gpg_keys.each(&:update_invalid_gpg_signatures) run_after_commit { gpg_keys.each(&:update_invalid_gpg_signatures) }
end end
# Returns the groups a user has access to # Returns the groups a user has access to
......
class InvalidGpgSignatureUpdateWorker
include Sidekiq::Worker
include DedicatedSidekiqQueue
def perform(gpg_key_id)
if gpg_key = GpgKey.find_by(id: gpg_key_id)
Gitlab::Gpg::InvalidGpgSignatureUpdater.new(gpg_key).run
else
Rails.logger.error("InvalidGpgSignatureUpdateWorker: couldn't find gpg_key with ID=#{gpg_key_id}, skipping job")
end
end
end
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
- [email_receiver, 2] - [email_receiver, 2]
- [emails_on_push, 2] - [emails_on_push, 2]
- [mailers, 2] - [mailers, 2]
- [invalid_gpg_signature_update, 2]
- [upload_checksum, 1] - [upload_checksum, 1]
- [use_key, 1] - [use_key, 1]
- [repository_fork, 1] - [repository_fork, 1]
......
...@@ -223,7 +223,9 @@ describe 'Commits' do ...@@ -223,7 +223,9 @@ describe 'Commits' do
user = create :user, email: 'unrelated.user@example.org' user = create :user, email: 'unrelated.user@example.org'
project.team << [user, :master] project.team << [user, :master]
Sidekiq::Testing.inline! do
create :gpg_key, key: GpgHelpers::User1.public_key, user: user create :gpg_key, key: GpgHelpers::User1.public_key, user: user
end
login_with(user) login_with(user)
...@@ -235,8 +237,10 @@ describe 'Commits' do ...@@ -235,8 +237,10 @@ describe 'Commits' do
end end
# user changes his email which makes the gpg key verified # user changes his email which makes the gpg key verified
Sidekiq::Testing.inline! do
user.skip_reconfirmation! user.skip_reconfirmation!
user.update_attributes!(email: GpgHelpers::User1.emails.first) user.update_attributes!(email: GpgHelpers::User1.emails.first)
end
visit namespace_project_commits_path(project.namespace, project, :master) visit namespace_project_commits_path(project.namespace, project, :master)
...@@ -260,7 +264,9 @@ describe 'Commits' do ...@@ -260,7 +264,9 @@ describe 'Commits' do
end end
# user adds the gpg key which makes the signature valid # user adds the gpg key which makes the signature valid
Sidekiq::Testing.inline! do
create :gpg_key, key: GpgHelpers::User1.public_key, user: user create :gpg_key, key: GpgHelpers::User1.public_key, user: user
end
visit namespace_project_commits_path(project.namespace, project, :master) visit namespace_project_commits_path(project.namespace, project, :master)
......
require 'spec_helper'
describe InvalidGpgSignatureUpdateWorker do
context 'when GpgKey is found' do
it 'calls NotificationService.new.run' do
gpg_key = create(:gpg_key)
invalid_signature_updater = double(:invalid_signature_updater)
expect(Gitlab::Gpg::InvalidGpgSignatureUpdater).to receive(:new).with(gpg_key).and_return(invalid_signature_updater)
expect(invalid_signature_updater).to receive(:run)
described_class.new.perform(gpg_key.id)
end
end
context 'when GpgKey is not found' do
let(:nonexisting_gpg_key_id) { -1 }
it 'logs InvalidGpgSignatureUpdateWorker process skipping' do
expect(Rails.logger).to receive(:error)
.with("InvalidGpgSignatureUpdateWorker: couldn't find gpg_key with ID=-1, skipping job")
described_class.new.perform(nonexisting_gpg_key_id)
end
it 'does not raise errors' do
expect { described_class.new.perform(nonexisting_gpg_key_id) }.not_to raise_error
end
it 'does not call NotificationService.new.run' do
expect(Gitlab::Gpg::InvalidGpgSignatureUpdater).not_to receive(:new)
described_class.new.perform(nonexisting_gpg_key_id)
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment