Remove ExpiringToken

improve-embed-js-serving
Marcin Kulik 7 years ago
parent ce08316ff5
commit e03e35ad6a

@ -1,28 +0,0 @@
class ExpiringToken < ActiveRecord::Base
belongs_to :user
validates :user, :token, :expires_at, presence: true
scope :active, -> { where(used_at: nil).where('expires_at > ?', Time.now) }
def self.create_for_user(user)
create!(
user: user,
token: SecureRandom.urlsafe_base64,
expires_at: 15.minutes.from_now
)
end
def self.active_for_token(token)
active.where(token: token).first
end
def use!
raise "token #{token} already used at #{used_at}" if used_at
self.used_at = Time.now
save!
end
end

@ -6,7 +6,6 @@ class User < ActiveRecord::Base
has_many :api_tokens, :dependent => :destroy
has_many :asciicasts, :dependent => :destroy
has_many :expiring_tokens, dependent: :destroy
validates :email, presence: true, on: :update
validates :email, format: { with: /.+@.+\..+/i }, uniqueness: true, if: :email
@ -102,10 +101,6 @@ class User < ActiveRecord::Base
CFG.admin_ids.include?(id)
end
def first_login?
expiring_tokens.count == 1
end
def new_asciicast_private?
asciicasts_private_by_default?
end

@ -0,0 +1,5 @@
class DropExpiringTokens < ActiveRecord::Migration
def change
drop_table :expiring_tokens
end
end

@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20170721130539) do
ActiveRecord::Schema.define(version: 20170728221839) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -65,17 +65,6 @@ ActiveRecord::Schema.define(version: 20170721130539) do
add_index "asciicasts", ["user_id"], name: "index_asciicasts_on_user_id", using: :btree
add_index "asciicasts", ["views_count"], name: "index_asciicasts_on_views_count", using: :btree
create_table "expiring_tokens", force: true do |t|
t.integer "user_id", null: false
t.string "token", null: false
t.datetime "expires_at", null: false
t.datetime "created_at"
t.datetime "used_at"
end
add_index "expiring_tokens", ["used_at", "expires_at", "token"], name: "index_expiring_tokens_on_used_at_and_expires_at_and_token", using: :btree
add_index "expiring_tokens", ["user_id"], name: "index_expiring_tokens_on_user_id", using: :btree
create_table "users", force: true do |t|
t.string "provider"
t.string "uid"
@ -101,6 +90,4 @@ ActiveRecord::Schema.define(version: 20170721130539) do
add_foreign_key "asciicasts", "users", name: "asciicasts_user_id_fk"
add_foreign_key "expiring_tokens", "users", name: "expiring_tokens_user_id_fk"
end

@ -194,8 +194,6 @@ defmodule Asciinema.Users do
Repo.update_all(asciicasts_q, set: [user_id: dst_user.id, updated_at: Timex.now])
api_tokens_q = from(assoc(src_user, :api_tokens))
Repo.update_all(api_tokens_q, set: [user_id: dst_user.id, updated_at: Timex.now])
expiring_tokens_q = from(assoc(src_user, :expiring_tokens))
Repo.delete_all(expiring_tokens_q)
Repo.delete!(src_user)
dst_user
end)

@ -1,17 +0,0 @@
# Read about factories at http://github.com/thoughtbot/factory_girl
FactoryGirl.define do
factory :expiring_token do
association :user
sequence(:token) { |n| "token-#{n}" }
expires_at { 10.minutes.from_now }
end
factory :used_expiring_token, parent: :expiring_token do
used_at { 1.minute.ago }
end
factory :expired_expiring_token, parent: :expiring_token do
expires_at { 1.minute.ago }
end
end

@ -1,47 +0,0 @@
require 'rails_helper'
RSpec.describe ExpiringToken, :type => :model do
it { should validate_presence_of(:user) }
it { should validate_presence_of(:token) }
it { should validate_presence_of(:expires_at) }
describe '.create_for_user' do
it 'creates expiring token with generated token and expiration time in the future' do
user = create(:user)
expiring_token = ExpiringToken.create_for_user(user)
expect(expiring_token.user).to eq(user)
expect(expiring_token.token.size).to eq(22)
expect(expiring_token.expires_at).to be > Time.now
end
end
describe '.active_for_token' do
it 'returns not used and not expired expiring token matching given token' do
used_expiring_token = create(:used_expiring_token)
expired_expiring_token = create(:expired_expiring_token)
good_expiring_token = create(:expiring_token)
expect(ExpiringToken.active_for_token(used_expiring_token.token)).to be(nil)
expect(ExpiringToken.active_for_token(expired_expiring_token.token)).to be(nil)
expect(ExpiringToken.active_for_token(good_expiring_token.token)).to eq(good_expiring_token)
end
end
describe '#use!' do
it 'sets used_at to the current time and saves the record' do
expiring_token = create(:expiring_token)
now = Time.now
Timecop.freeze(now) do
expiring_token.use!
end
expect(expiring_token.used_at).to eq(now)
expect(expiring_token).to_not be_changed
end
end
end

@ -1,7 +0,0 @@
defmodule Asciinema.ExpiringToken do
use Asciinema.Web, :model
schema "expiring_tokens" do
belongs_to :user, Asciinema.User
end
end

@ -18,7 +18,6 @@ defmodule Asciinema.User do
has_many :asciicasts, Asciinema.Asciicast
has_many :api_tokens, Asciinema.ApiToken
has_many :expiring_tokens, Asciinema.ExpiringToken
end
def changeset(struct, params \\ %{}) do

Loading…
Cancel
Save