Rename column

This commit is contained in:
Urban Rotnik 2020-06-29 10:42:19 +02:00
parent 88f54b16d7
commit de8aca9dbd
4 changed files with 8 additions and 8 deletions

View file

@ -286,8 +286,8 @@ class User < ApplicationRecord
foreign_key: :resource_owner_id,
dependent: :delete_all
before_save :ensure_2fa_token, if: ->(user) { user.changed.include?('twofa_enabled') }
before_create :generate_2fa_token
before_save :ensure_2fa_token, if: ->(user) { user.changed.include?('two_factor_auth_enabled') }
before_create :assign_2fa_token
before_destroy :destroy_notifications
def name
@ -661,11 +661,11 @@ class User < ApplicationRecord
Rails.cache.delete_matched(%r{^views\/users\/#{id}-})
end
def generate_2fa_token
def assign_2fa_token
self.otp_secret = ROTP::Base32.random
end
def ensure_2fa_token
generate_2fa_token unless otp_secret
assign_2fa_token unless otp_secret
end
end

View file

@ -3,7 +3,7 @@
class Add2faToUsers < ActiveRecord::Migration[6.0]
def change
change_table :users, bulk: true do |t|
t.boolean :twofa_enabled, default: false, null: false
t.boolean :two_factor_auth_enabled, default: false, null: false
t.string :otp_secret
end
end

View file

@ -2648,7 +2648,7 @@ CREATE TABLE public.users (
authentication_token character varying(30),
settings jsonb DEFAULT '{}'::jsonb NOT NULL,
variables jsonb DEFAULT '{}'::jsonb NOT NULL,
twofa_enabled boolean DEFAULT false NOT NULL,
two_factor_auth_enabled boolean DEFAULT false NOT NULL,
otp_secret character varying
);

View file

@ -349,13 +349,13 @@ describe User, type: :model do
it 'sets token before save' do
user.update_column(:otp_secret, nil)
expect { user.update(twofa_enabled: true) }.to(change { user.otp_secret })
expect { user.update(two_factor_auth_enabled: true) }.to(change { user.otp_secret })
end
end
context 'when user does have otp_secret' do
it 'does not set new token before save' do
expect { user.update(twofa_enabled: true) }.not_to(change { user.otp_secret })
expect { user.update(two_factor_auth_enabled: true) }.not_to(change { user.otp_secret })
end
end
end