Implement Key.authenticate
This commit is contained in:
parent
78f79c4c34
commit
0f1f7106b2
@ -4,4 +4,10 @@ class Key < ActiveRecord::Base
|
||||
has_secure_token :token
|
||||
|
||||
validates :name, presence: true
|
||||
|
||||
class << self
|
||||
def authenticate token
|
||||
find_by_token token
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -9,4 +9,20 @@ describe Key do
|
||||
key.save
|
||||
expect(key.token).to match /\A[\w\d]{24,}\z/
|
||||
end
|
||||
|
||||
describe '.authenticate' do
|
||||
context 'when given token belong to existing key' do
|
||||
before { key.save }
|
||||
|
||||
it 'returns the key' do
|
||||
expect(described_class.authenticate key.token).to eq key
|
||||
end
|
||||
end
|
||||
|
||||
context 'when given token is unknown' do
|
||||
it 'returns nil' do
|
||||
expect(described_class.authenticate key.token).to be nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Loading…
x
Reference in New Issue
Block a user