Validate uniquness of api_key.

This commit is contained in:
Guo Xiang Tan 2017-07-24 21:45:05 +09:00
parent 57d6a5dc9c
commit 6b17178533
2 changed files with 9 additions and 4 deletions

View File

@ -2,6 +2,7 @@ class ApiKey < ActiveRecord::Base
belongs_to :user
belongs_to :created_by, class_name: User
validates :user_id, uniqueness: true
validates_presence_of :key
def regenerate!(updated_by)

View File

@ -3,14 +3,18 @@ require 'rails_helper'
require_dependency 'api_key'
describe ApiKey do
let(:user) { Fabricate(:user) }
it { is_expected.to belong_to :user }
it { is_expected.to belong_to :created_by }
it { is_expected.to validate_presence_of :key }
skip 'validates uniqueness of user_id' do
Fabricate(:api_key)
is_expected.to validate_uniqueness_of(:user_id)
it 'validates uniqueness of user_id' do
Fabricate(:api_key, user: user)
api_key = Fabricate.build(:api_key, user: user)
expect(api_key.save).to eq(false)
expect(api_key.errors).to include(:user_id)
end
end