Commit 56fdade9 authored by Qingyu Zhao's avatar Qingyu Zhao

Create table user_credit_card_validations

parent 2c4abf28
......@@ -205,6 +205,7 @@ class User < ApplicationRecord
has_one :user_detail
has_one :user_highest_role
has_one :user_canonical_email
has_one :credit_card_validation, class_name: '::Users::CreditCardValidation'
has_one :atlassian_identity, class_name: 'Atlassian::Identity'
has_many :reviews, foreign_key: :author_id, inverse_of: :author
......@@ -317,6 +318,7 @@ class User < ApplicationRecord
accepts_nested_attributes_for :user_preference, update_only: true
accepts_nested_attributes_for :user_detail, update_only: true
accepts_nested_attributes_for :credit_card_validation, update_only: true
state_machine :state, initial: :active do
event :block do
......@@ -1223,6 +1225,10 @@ class User < ApplicationRecord
user_highest_role&.highest_access_level || Gitlab::Access::NO_ACCESS
end
def credit_card_validated_at
credit_card_validation&.credit_card_validated_at
end
def accessible_deploy_keys
DeployKey.from_union([
DeployKey.where(id: project_deploy_keys.select(:deploy_key_id)),
......
# frozen_string_literal: true
module Users
class CreditCardValidation < ApplicationRecord
self.table_name = 'user_credit_card_validations'
belongs_to :user
end
end
---
title: Create table user_credit_card_validations
merge_request: 60626
author:
type: changed
# frozen_string_literal: true
class CreateUserCreditCardValidations < ActiveRecord::Migration[6.0]
include Gitlab::Database::MigrationHelpers
disable_ddl_transaction!
def up
with_lock_retries do
create_table :user_credit_card_validations, id: false do |t|
t.references :user, foreign_key: { on_delete: :cascade }, index: false, primary_key: true, default: nil
t.datetime_with_timezone :credit_card_validated_at, null: false
end
end
end
def down
with_lock_retries do
drop_table :user_credit_card_validations
end
end
end
68ac54fa7b4e4ef99e58c31d8f960b6f986fd679c11ead235704c7a75b4617ac
\ No newline at end of file
......@@ -18272,6 +18272,11 @@ CREATE SEQUENCE user_canonical_emails_id_seq
ALTER SEQUENCE user_canonical_emails_id_seq OWNED BY user_canonical_emails.id;
CREATE TABLE user_credit_card_validations (
user_id bigint NOT NULL,
credit_card_validated_at timestamp with time zone NOT NULL
);
CREATE TABLE user_custom_attributes (
id integer NOT NULL,
created_at timestamp without time zone NOT NULL,
......@@ -21642,6 +21647,9 @@ ALTER TABLE ONLY user_callouts
ALTER TABLE ONLY user_canonical_emails
ADD CONSTRAINT user_canonical_emails_pkey PRIMARY KEY (id);
ALTER TABLE ONLY user_credit_card_validations
ADD CONSTRAINT user_credit_card_validations_pkey PRIMARY KEY (user_id);
ALTER TABLE ONLY user_custom_attributes
ADD CONSTRAINT user_custom_attributes_pkey PRIMARY KEY (id);
......@@ -25923,6 +25931,9 @@ ALTER TABLE ONLY lfs_file_locks
ALTER TABLE ONLY project_alerting_settings
ADD CONSTRAINT fk_rails_27a84b407d FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE;
ALTER TABLE ONLY user_credit_card_validations
ADD CONSTRAINT fk_rails_27ebc03cbf FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE;
ALTER TABLE ONLY dast_site_validations
ADD CONSTRAINT fk_rails_285c617324 FOREIGN KEY (dast_site_token_id) REFERENCES dast_site_tokens(id) ON DELETE CASCADE;
......@@ -80,6 +80,12 @@ FactoryBot.define do
last_sign_in_ip { '127.0.0.1' }
end
trait :with_credit_card_validation do
after :create do |user|
create :credit_card_validation, user: user
end
end
trait :two_factor_via_otp do
before(:create) do |user|
user.otp_required_for_login = true
......
# frozen_string_literal: true
FactoryBot.define do
factory :credit_card_validation, class: 'Users::CreditCardValidation' do
user
credit_card_validated_at { Time.current }
end
end
......@@ -83,6 +83,7 @@ RSpec.describe User do
it { is_expected.to have_one(:user_detail) }
it { is_expected.to have_one(:atlassian_identity) }
it { is_expected.to have_one(:user_highest_role) }
it { is_expected.to have_one(:credit_card_validation) }
it { is_expected.to have_many(:snippets).dependent(:destroy) }
it { is_expected.to have_many(:members) }
it { is_expected.to have_many(:project_members) }
......@@ -1387,6 +1388,26 @@ RSpec.describe User do
end
end
describe '#credit_card_validated_at' do
let_it_be(:user) { create(:user) }
context 'when credit_card_validation does not exist' do
it 'returns nil' do
expect(user.credit_card_validated_at).to be nil
end
end
context 'when credit_card_validation exists' do
it 'returns the credit card validated time' do
credit_card_validated_time = Time.current - 1.day
create(:credit_card_validation, credit_card_validated_at: credit_card_validated_time, user: user)
expect(user.credit_card_validated_at).to eq(credit_card_validated_time)
end
end
end
describe '#update_tracked_fields!', :clean_gitlab_redis_shared_state do
let(:request) { OpenStruct.new(remote_ip: "127.0.0.1") }
let(:user) { create(:user) }
......@@ -5290,6 +5311,26 @@ RSpec.describe User do
end
end
describe 'user credit card validation' do
context 'when user is initialized' do
let(:user) { build(:user) }
it { expect(user.credit_card_validation).not_to be_present }
end
context 'when create user without credit card validation' do
let(:user) { create(:user) }
it { expect(user.credit_card_validation).not_to be_present }
end
context 'when user credit card validation exists' do
let(:user) { create(:user, :with_credit_card_validation) }
it { expect(user.credit_card_validation).to be_persisted }
end
end
describe 'user detail' do
context 'when user is initialized' do
let(:user) { build(:user) }
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Users::CreditCardValidation do
it { is_expected.to belong_to(:user) }
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