Merge pull request #1432 from internetee/add-audit-log-to-registrant-verifications

Add audit log to registrant verifications
This commit is contained in:
Timo Võhmar 2019-12-27 16:49:25 +02:00 committed by GitHub
commit 6d876039c5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 377 additions and 199 deletions

View file

@ -57,6 +57,7 @@ class Domain < ApplicationRecord
has_many :legal_documents, as: :documentable
accepts_nested_attributes_for :legal_documents, reject_if: proc { |attrs| attrs[:body].blank? }
has_many :registrant_verifications, dependent: :destroy
after_initialize do
self.pending_json = {} if pending_json.blank?

View file

@ -1,6 +1,8 @@
# Used in Registrant portal to collect registrant verifications
# Registrant postgres user can access this table directly.
class RegistrantVerification < ApplicationRecord
has_paper_trail class_name: 'RegistrantVerificationVersion'
# actions
CONFIRMED = 'confirmed'
REJECTED = 'rejected'

View file

@ -0,0 +1,7 @@
class RegistrantVerificationVersion < PaperTrail::Version
include VersionSession
self.table_name = :log_registrant_verifications
self.sequence_name = :log_registrant_verifications_id_seq
scope :deleted, -> { where(event: 'destroy') }
end

View file

@ -0,0 +1,24 @@
class AddJsonBasedVersionToRegistrantVerifications < ActiveRecord::Migration[5.0]
def change
name = 'registrant_verification'
table_name = "log_#{name.tableize}"
create_table table_name do |t|
t.string :item_type, null: false
t.integer :item_id, null: false
t.string :event, null: false
t.string :whodunnit
t.json :object
t.json :object_changes
t.datetime :created_at
t.string :session
end
add_index table_name, [:item_type, :item_id]
add_index table_name, :whodunnit
add_column name.tableize, :creator_id_tmp, :integer
add_column name.tableize, :updater_id_tmp, :integer
rename_column name.tableize, :creator_id_tmp, :creator_id
rename_column name.tableize, :updater_id_tmp, :updater_id
end
end

View file

@ -1511,6 +1511,42 @@ CREATE SEQUENCE public.log_notifications_id_seq
ALTER SEQUENCE public.log_notifications_id_seq OWNED BY public.log_notifications.id;
--
-- Name: log_registrant_verifications; Type: TABLE; Schema: public; Owner: -; Tablespace:
--
CREATE TABLE public.log_registrant_verifications (
id integer NOT NULL,
item_type character varying NOT NULL,
item_id integer NOT NULL,
event character varying NOT NULL,
whodunnit character varying,
object json,
object_changes json,
created_at timestamp without time zone,
session character varying
);
--
-- Name: log_registrant_verifications_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--
CREATE SEQUENCE public.log_registrant_verifications_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
--
-- Name: log_registrant_verifications_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--
ALTER SEQUENCE public.log_registrant_verifications_id_seq OWNED BY public.log_registrant_verifications.id;
--
-- Name: log_registrars; Type: TABLE; Schema: public; Owner: -; Tablespace:
--
@ -1868,7 +1904,9 @@ CREATE TABLE public.registrant_verifications (
updated_at timestamp without time zone,
action character varying NOT NULL,
domain_id integer NOT NULL,
action_type character varying NOT NULL
action_type character varying NOT NULL,
creator_id integer,
updater_id integer
);
@ -2456,6 +2494,13 @@ ALTER TABLE ONLY public.log_nameservers ALTER COLUMN id SET DEFAULT nextval('pub
ALTER TABLE ONLY public.log_notifications ALTER COLUMN id SET DEFAULT nextval('public.log_notifications_id_seq'::regclass);
--
-- Name: id; Type: DEFAULT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.log_registrant_verifications ALTER COLUMN id SET DEFAULT nextval('public.log_registrant_verifications_id_seq'::regclass);
--
-- Name: id; Type: DEFAULT; Schema: public; Owner: -
--
@ -2854,6 +2899,14 @@ ALTER TABLE ONLY public.log_notifications
ADD CONSTRAINT log_notifications_pkey PRIMARY KEY (id);
--
-- Name: log_registrant_verifications_pkey; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace:
--
ALTER TABLE ONLY public.log_registrant_verifications
ADD CONSTRAINT log_registrant_verifications_pkey PRIMARY KEY (id);
--
-- Name: log_registrars_pkey; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace:
--
@ -3487,6 +3540,20 @@ CREATE INDEX index_log_notifications_on_item_type_and_item_id ON public.log_noti
CREATE INDEX index_log_notifications_on_whodunnit ON public.log_notifications USING btree (whodunnit);
--
-- Name: index_log_registrant_verifications_on_item_type_and_item_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
CREATE INDEX index_log_registrant_verifications_on_item_type_and_item_id ON public.log_registrant_verifications USING btree (item_type, item_id);
--
-- Name: index_log_registrant_verifications_on_whodunnit; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
CREATE INDEX index_log_registrant_verifications_on_whodunnit ON public.log_registrant_verifications USING btree (whodunnit);
--
-- Name: index_log_registrars_on_item_type_and_item_id; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
@ -4263,6 +4330,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20191024153351'),
('20191024160038'),
('20191206183853'),
('20191212133136');
('20191212133136'),
('20191227110904');

View file

@ -0,0 +1,15 @@
namespace :data_migrations do
task delete_orphaned_registrant_verifications: :environment do
orphaned_registrant_verifications = RegistrantVerification.where.not(domain_id: Domain.ids)
orphaned_registrant_verification_count = orphaned_registrant_verifications.count
processed_registrant_verification_count = 0
orphaned_registrant_verifications.each do |registrant_verification|
registrant_verification.destroy!
processed_registrant_verification_count += 1
end
puts "Processed: #{processed_registrant_verification_count} out of" \
" #{orphaned_registrant_verification_count}"
end
end

View file

@ -0,0 +1,6 @@
one:
action: confirmed
action_type: domain_delete
created_at: <%= Time.zone.parse('2010-07-05') %>
domain: shop
verification_token: 1234

View file

@ -0,0 +1,12 @@
require 'test_helper'
class RegistrantVerificationTest < ActiveSupport::TestCase
def test_audit_log
registrant_verification = registrant_verifications(:one)
random_action = "random#{rand(100)}"
assert_difference -> { RegistrantVerificationVersion.count } do
registrant_verification.update_attributes!(action: random_action)
end
end
end

View file

@ -0,0 +1,43 @@
require 'test_helper'
class ArchiveOrphanedRegistrantVerificationsTest < ActiveSupport::TestCase
def test_deletes_orphaned_registrant_verifications
create_orphaned_registrant_verification
assert_difference 'RegistrantVerification.count', -1 do
capture_io do
run_task
end
end
end
def test_keeps_non_orphaned_registrant_verifications_intact
assert_no_difference 'RegistrantVerification.count' do
capture_io do
run_task
end
end
end
def test_output
create_orphaned_registrant_verification
assert_output "Processed: 1 out of 1\n" do
run_task
end
end
private
def create_orphaned_registrant_verification
non_existent_domain_id = 55
assert_not_includes Domain.ids, non_existent_domain_id
RegistrantVerification.connection.disable_referential_integrity do
registrant_verifications(:one).update_columns(domain_id: non_existent_domain_id)
end
end
def run_task
Rake::Task['data_migrations:delete_orphaned_registrant_verifications'].execute end
end