diff --git a/.rubocop.yml b/.rubocop.yml index acd3db74..ed74d1c9 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -134,3 +134,6 @@ Style/EndlessMethod: Style/TrailingCommaInHashLiteral: EnforcedStyleForMultiline: consistent_comma + +Style/TrailingCommaInArguments: + EnforcedStyleForMultiline: consistent_comma diff --git a/app/models/user.rb b/app/models/user.rb index 92427a5d..2b2241e0 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -85,8 +85,14 @@ class User < ApplicationRecord # rubocop:disable Metrics/ClassLength end end + after_destroy do + Retrospring::Metrics::USERS_DESTROYED.increment + end + after_create do Profile.create(user_id: id) if Profile.where(user_id: id).count.zero? + + Retrospring::Metrics::USERS_CREATED.increment end # use the screen name as parameter for url helpers diff --git a/lib/retrospring/metrics.rb b/lib/retrospring/metrics.rb index 7c52dcb6..3c715e5c 100644 --- a/lib/retrospring/metrics.rb +++ b/lib/retrospring/metrics.rb @@ -43,6 +43,16 @@ module Retrospring docstring: "How many comments got created" ) + USERS_CREATED = counter( + :retrospring_users_created_total, + docstring: "How many users got created" + ) + + USERS_DESTROYED = counter( + :retrospring_users_destroyed_total, + docstring: "How many users deleted their accounts" + ) + # metrics from Sidekiq::Stats.new SIDEKIQ = { processed: gauge( diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 2981df53..b3fe0836 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -33,6 +33,45 @@ RSpec.describe User, type: :model do end end + describe "callbacks" do + describe "before_destroy" do + it "marks reports about this user as deleted" do + other_user = FactoryBot.create(:user) + other_user.report me, "va tutto benissimo" + + expect { me.destroy } + .to change { Reports::User.find_by(target_id: me.id).deleted? } + .from(false) + .to(true) + end + end + + describe "after_destroy" do + it "increments the users_destroyed metric" do + expect { me.destroy }.to change { Retrospring::Metrics::USERS_DESTROYED.values.values.sum }.by(1) + end + end + + describe "after_create" do + subject :user do + User.create!( + screen_name: "konqi", + email: "konqi@example.rrerr.net", + password: "dragonsRQt5", + ) + end + + it "creates a profile for the user" do + expect { user }.to change { Profile.count }.by(1) + expect(Profile.find_by(user:).user).to eq(user) + end + + it "increments the users_created metric" do + expect { user }.to change { Retrospring::Metrics::USERS_CREATED.values.values.sum }.by(1) + end + end + end + describe "custom sharing url validation" do subject do FactoryBot.build(:user, sharing_custom_url: url).tap(&:validate).errors[:sharing_custom_url]