Add field User.refresh_interval

This commit is contained in:
Will Roden 2017-01-02 09:31:48 -06:00
Родитель 0cd4be589d
Коммит 7f54042359
4 изменённых файлов: 47 добавлений и 12 удалений

Просмотреть файл

@ -2,17 +2,25 @@
class User < ApplicationRecord
has_many :notifications, dependent: :delete_all
validates :github_id, presence: true, uniqueness: true
validates :access_token, presence: true, uniqueness: true
validates :github_login, presence: true
validate :personal_access_token_validator
ERRORS = {
invalid_token: [:personal_access_token, 'is not a valid token for this github user'],
missing_scope: [:personal_access_token, 'does not include the notifications scope'],
disallowed_tokens: [:personal_access_token, 'is not allowed in this instance']
disallowed_tokens: [:personal_access_token, 'is not allowed in this instance'],
refresh_interval_size: [:refresh_interval, 'must be less than 1 day']
}.freeze
validates :github_id, presence: true, uniqueness: true
validates :access_token, presence: true, uniqueness: true
validates :github_login, presence: true
validates :refresh_interval, numericality: {
only_integer: true,
allow_blank: false,
greater_than_or_equal_to: 0,
less_than_or_equal_to: 86_400_000,
message: ERRORS[:refresh_interval_size][1]
}
validate :personal_access_token_validator
def self.find_by_auth_hash(auth_hash)
User.find_by(github_id: auth_hash['uid'])
end

Просмотреть файл

@ -0,0 +1,5 @@
class AddRefreshIntervalToUsers < ActiveRecord::Migration[5.0]
def change
add_column :users, :refresh_interval, :integer, null: false, default: 0
end
end

Просмотреть файл

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20161228162823) do
ActiveRecord::Schema.define(version: 20170102152008) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -37,13 +37,14 @@ ActiveRecord::Schema.define(version: 20161228162823) do
end
create_table "users", force: :cascade do |t|
t.integer "github_id", null: false
t.string "access_token", null: false
t.string "github_login", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "github_id", null: false
t.string "access_token", null: false
t.string "github_login", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "last_synced_at"
t.string "personal_access_token"
t.integer "refresh_interval", default: 0, null: false
t.index ["access_token"], name: "index_users_on_access_token", unique: true, using: :btree
t.index ["github_id"], name: "index_users_on_github_id", unique: true, using: :btree
end

Просмотреть файл

@ -123,4 +123,25 @@ class UserTest < ActiveSupport::TestCase
user.personal_access_token = 'abcdefghijklmnopqrstuvwxyz'
assert_equal user.masked_personal_access_token, '********************************stuvwxyz'
end
test 'rejects refresh_interval over a day' do
user = users(:andrew)
user.refresh_interval = 90_000_000
refute user.valid?
assert_error_present(user, User::ERRORS[:refresh_interval_size])
end
test 'rejects negative refresh_interval' do
user = users(:andrew)
user.refresh_interval = -90_000
refute user.valid?
assert_error_present(user, User::ERRORS[:refresh_interval_size])
end
test 'sets refresh interval' do
user = users(:andrew)
user.refresh_interval = 60_000
user.save
assert_equal 60_000, users(:andrew).refresh_interval
end
end