[deploy] Add EmailAuthorization model (#6335)

* Add EmailAuthorization model

* Add model file oops

* Add comment for uuid type

Co-authored-by: Ben Halpern <bendhalpern@gmail.com>
This commit is contained in:
Andy Zhao 2020-03-02 12:14:24 -05:00 committed by GitHub
parent 8510b50d66
commit a3728cfa4c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 63 additions and 0 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -0,0 +1,5 @@
class ValidateCreateEmailAuthorizationTable < ActiveRecord::Migration[5.2]
def change
validate_foreign_key :email_authorizations, :users
end
end

View file

@ -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"

View file

@ -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

View file

@ -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