api_key_spec.rb revision 248ce7a27b1f8b0f557b2d7f05967084753c3bbb
1ffc4fc0f3b09d35eb82399725ff3ace12969bfcEugen Kuksarequire 'spec_helper'
1ffc4fc0f3b09d35eb82399725ff3ace12969bfcEugen Kuksa
1ffc4fc0f3b09d35eb82399725ff3ace12969bfcEugen Kuksadescribe ApiKey do
7c7a19761235efff584ee65a1c6dc4aa1735ff64Eugen Kuksa let(:api_key) { create :api_key }
7c7a19761235efff584ee65a1c6dc4aa1735ff64Eugen Kuksa
1ffc4fc0f3b09d35eb82399725ff3ace12969bfcEugen Kuksa it 'should have a valid factory' do
7c7a19761235efff584ee65a1c6dc4aa1735ff64Eugen Kuksa expect(api_key).to be_valid
1ffc4fc0f3b09d35eb82399725ff3ace12969bfcEugen Kuksa end
1ffc4fc0f3b09d35eb82399725ff3ace12969bfcEugen Kuksa
7c7a19761235efff584ee65a1c6dc4aa1735ff64Eugen Kuksa context '.create_new_key!' do
7c7a19761235efff584ee65a1c6dc4aa1735ff64Eugen Kuksa let(:old_key) { create :api_key }
7c7a19761235efff584ee65a1c6dc4aa1735ff64Eugen Kuksa let!(:key) { described_class.create_new_key!(old_key.user) }
1ffc4fc0f3b09d35eb82399725ff3ace12969bfcEugen Kuksa
7c7a19761235efff584ee65a1c6dc4aa1735ff64Eugen Kuksa it 'should only have the new key as valid' do
1ffc4fc0f3b09d35eb82399725ff3ace12969bfcEugen Kuksa expect(described_class.where(status: 'valid').to_a).
1ffc4fc0f3b09d35eb82399725ff3ace12969bfcEugen Kuksa to eq([key])
7c7a19761235efff584ee65a1c6dc4aa1735ff64Eugen Kuksa end
7c7a19761235efff584ee65a1c6dc4aa1735ff64Eugen Kuksa
1ffc4fc0f3b09d35eb82399725ff3ace12969bfcEugen Kuksa it 'should have invalidated the old key' do
7c7a19761235efff584ee65a1c6dc4aa1735ff64Eugen Kuksa old_key.reload
1ffc4fc0f3b09d35eb82399725ff3ace12969bfcEugen Kuksa expect(old_key.status).to eq('invalid')
1ffc4fc0f3b09d35eb82399725ff3ace12969bfcEugen Kuksa end
1ffc4fc0f3b09d35eb82399725ff3ace12969bfcEugen Kuksa end
7c7a19761235efff584ee65a1c6dc4aa1735ff64Eugen Kuksa
1ffc4fc0f3b09d35eb82399725ff3ace12969bfcEugen Kuksa context 'invalid on' do
1ffc4fc0f3b09d35eb82399725ff3ace12969bfcEugen Kuksa it 'non-unique keys' do
1ffc4fc0f3b09d35eb82399725ff3ace12969bfcEugen Kuksa invalid_key = build(:api_key, key: api_key.key)
1ffc4fc0f3b09d35eb82399725ff3ace12969bfcEugen Kuksa expect(invalid_key).to_not be_valid
1ffc4fc0f3b09d35eb82399725ff3ace12969bfcEugen Kuksa end
1ffc4fc0f3b09d35eb82399725ff3ace12969bfcEugen Kuksa
1ffc4fc0f3b09d35eb82399725ff3ace12969bfcEugen Kuksa it 'non-present user' do
23305f217b86a2ebf90a848af9036908e0070542Eugen Kuksa invalid_key = build(:api_key, user: nil)
1ffc4fc0f3b09d35eb82399725ff3ace12969bfcEugen Kuksa expect(invalid_key).to_not be_valid
1ffc4fc0f3b09d35eb82399725ff3ace12969bfcEugen Kuksa end
7ca38d75d045fade73b81bff6fdf34f4755d75fbEugen Kuksa
7ca38d75d045fade73b81bff6fdf34f4755d75fbEugen Kuksa it 'unacceptable status' do
7ca38d75d045fade73b81bff6fdf34f4755d75fbEugen Kuksa invalid_key = build(:api_key, status: 'nice status')
7ca38d75d045fade73b81bff6fdf34f4755d75fbEugen Kuksa expect(invalid_key).to_not be_valid
1ffc4fc0f3b09d35eb82399725ff3ace12969bfcEugen Kuksa end
1ffc4fc0f3b09d35eb82399725ff3ace12969bfcEugen Kuksa end
1ffc4fc0f3b09d35eb82399725ff3ace12969bfcEugen Kuksaend
23305f217b86a2ebf90a848af9036908e0070542Eugen Kuksa