From a3728cfa4c148581ccde91d28c191efd5ee0deb2 Mon Sep 17 00:00:00 2001 From: Andy Zhao <17884966+Zhao-Andy@users.noreply.github.com> Date: Mon, 2 Mar 2020 12:14:24 -0500 Subject: [PATCH] [deploy] Add EmailAuthorization model (#6335) * Add EmailAuthorization model * Add model file oops * Add comment for uuid type Co-authored-by: Ben Halpern --- app/models/email_authorization.rb | 12 ++++++++++++ app/models/user.rb | 1 + ...0226205549_create_email_authorization_table.rb | 15 +++++++++++++++ ...1_validate_create_email_authorization_table.rb | 5 +++++ db/schema.rb | 11 +++++++++++ spec/factories/email_authorizations.rb | 7 +++++++ spec/models/email_authorization_spec.rb | 12 ++++++++++++ 7 files changed, 63 insertions(+) create mode 100644 app/models/email_authorization.rb create mode 100644 db/migrate/20200226205549_create_email_authorization_table.rb create mode 100644 db/migrate/20200226210111_validate_create_email_authorization_table.rb create mode 100644 spec/factories/email_authorizations.rb create mode 100644 spec/models/email_authorization_spec.rb diff --git a/app/models/email_authorization.rb b/app/models/email_authorization.rb new file mode 100644 index 000000000..00a8678b7 --- /dev/null +++ b/app/models/email_authorization.rb @@ -0,0 +1,12 @@ +class EmailAuthorization < ApplicationRecord + belongs_to :user + + TYPES = %w[merge_request account_lockout uuid_issue].freeze + # uuid_issue is a specific case where a user deletes their old auth account and recreates it, leaving us with the incorrect uuid + + validates :json_data, :type_of, presence: true + validates :type_of, inclusion: { in: TYPES } + validates :user_id, uniqueness: { scope: %i[type_of] } + + alias_attribute :sent_at, :created_at +end diff --git a/app/models/user.rb b/app/models/user.rb index cd0d88fdb..22e24ce06 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -24,6 +24,7 @@ class User < ApplicationRecord has_many :collections, dependent: :destroy has_many :comments, dependent: :destroy has_many :email_messages, class_name: "Ahoy::Message", dependent: :destroy + has_many :email_authorizations, dependent: :delete_all has_many :github_repos, dependent: :destroy has_many :identities, dependent: :destroy has_many :mentions, dependent: :destroy diff --git a/db/migrate/20200226205549_create_email_authorization_table.rb b/db/migrate/20200226205549_create_email_authorization_table.rb new file mode 100644 index 000000000..aedfc33bd --- /dev/null +++ b/db/migrate/20200226205549_create_email_authorization_table.rb @@ -0,0 +1,15 @@ +class CreateEmailAuthorizationTable < ActiveRecord::Migration[5.2] + def change + create_table :email_authorizations do |t| + t.references :user + t.jsonb :json_data, null: false, default: {} + t.string :type_of, null: false + t.timestamp :verified_at + t.timestamps null: false + end + + add_index :email_authorizations, %i[user_id type_of], unique: true + + add_foreign_key :email_authorizations, :users, on_delete: :cascade, validate: false + end +end diff --git a/db/migrate/20200226210111_validate_create_email_authorization_table.rb b/db/migrate/20200226210111_validate_create_email_authorization_table.rb new file mode 100644 index 000000000..6094c0bce --- /dev/null +++ b/db/migrate/20200226210111_validate_create_email_authorization_table.rb @@ -0,0 +1,5 @@ +class ValidateCreateEmailAuthorizationTable < ActiveRecord::Migration[5.2] + def change + validate_foreign_key :email_authorizations, :users + end +end diff --git a/db/schema.rb b/db/schema.rb index dc5ef1acb..75fa7870f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -388,6 +388,16 @@ ActiveRecord::Schema.define(version: 2020_02_27_214321) do t.datetime "updated_at", null: false end + create_table "email_authorizations", force: :cascade do |t| + t.datetime "created_at", null: false + t.jsonb "json_data", default: {}, null: false + t.string "type_of", null: false + t.datetime "updated_at", null: false + t.bigint "user_id" + t.datetime "verified_at" + t.index ["user_id", "type_of"], name: "index_email_authorizations_on_user_id_and_type_of", unique: true + end + create_table "events", force: :cascade do |t| t.string "category" t.string "cover_image" @@ -1215,6 +1225,7 @@ ActiveRecord::Schema.define(version: 2020_02_27_214321) do add_foreign_key "chat_channel_memberships", "chat_channels" add_foreign_key "chat_channel_memberships", "users" add_foreign_key "classified_listings", "users", on_delete: :cascade + add_foreign_key "email_authorizations", "users", on_delete: :cascade add_foreign_key "identities", "users", on_delete: :cascade add_foreign_key "messages", "chat_channels" add_foreign_key "messages", "users" diff --git a/spec/factories/email_authorizations.rb b/spec/factories/email_authorizations.rb new file mode 100644 index 000000000..d71656e69 --- /dev/null +++ b/spec/factories/email_authorizations.rb @@ -0,0 +1,7 @@ +FactoryBot.define do + factory :email_authorization do + user + type_of { "merge_request" } + json_data { { keep_user_id: 1, deleted_user_id: 1 } } + end +end diff --git a/spec/models/email_authorization_spec.rb b/spec/models/email_authorization_spec.rb new file mode 100644 index 000000000..570508601 --- /dev/null +++ b/spec/models/email_authorization_spec.rb @@ -0,0 +1,12 @@ +require "rails_helper" + +RSpec.describe EmailAuthorization, type: :model do + it { is_expected.to validate_inclusion_of(:type_of).in_array(EmailAuthorization::TYPES) } + + describe "#sent_at" do + it "calls #created_at" do + email_auth = create(:email_authorization) + expect(email_auth.sent_at).to eq email_auth.created_at + end + end +end