2015-11-04 17:27:01 +01:00
|
|
|
require 'spec_helper'
|
2015-10-23 16:19:55 +02:00
|
|
|
|
2015-11-04 17:27:01 +01:00
|
|
|
describe Administrateur, type: :model do
|
2015-11-10 10:23:15 +01:00
|
|
|
describe 'assocations' do
|
2016-05-20 15:39:17 +02:00
|
|
|
it { is_expected.to have_and_belong_to_many(:gestionnaires) }
|
2015-11-10 10:23:15 +01:00
|
|
|
it { is_expected.to have_many(:procedures) }
|
|
|
|
end
|
|
|
|
|
2015-12-14 17:28:36 +01:00
|
|
|
describe 'after_save' do
|
|
|
|
subject { described_class.new(email: 'toto@tps.com', password: 'password') }
|
|
|
|
before do
|
|
|
|
subject.save
|
|
|
|
end
|
|
|
|
it { expect(subject.api_token).not_to be_blank }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'generate_api_token' do
|
|
|
|
let(:token) { 'bullshit' }
|
|
|
|
let(:new_token) { 'pocket_master' }
|
2017-06-12 16:12:03 +02:00
|
|
|
let!(:admin_1) { create(:administrateur, email: 'toto@tps.com', password: 'password', api_token: token) }
|
2015-12-14 17:28:36 +01:00
|
|
|
before do
|
|
|
|
allow(SecureRandom).to receive(:hex).and_return(token, new_token)
|
|
|
|
admin_1.renew_api_token
|
|
|
|
end
|
|
|
|
it 'generate a token who does not already exist' do
|
|
|
|
expect(admin_1.api_token).to eq(new_token)
|
|
|
|
end
|
|
|
|
end
|
2016-12-07 17:24:01 +01:00
|
|
|
|
|
|
|
context 'unified login' do
|
|
|
|
it 'syncs credentials to associated user' do
|
|
|
|
administrateur = create(:administrateur)
|
|
|
|
user = create(:user, email: administrateur.email)
|
|
|
|
|
|
|
|
administrateur.update_attributes(email: 'whoami@plop.com', password: 'super secret')
|
|
|
|
|
|
|
|
user.reload
|
|
|
|
expect(user.email).to eq('whoami@plop.com')
|
|
|
|
expect(user.valid_password?('super secret')).to be(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'syncs credentials to associated administrateur' do
|
|
|
|
administrateur = create(:administrateur)
|
|
|
|
gestionnaire = create(:gestionnaire, email: administrateur.email)
|
|
|
|
|
|
|
|
administrateur.update_attributes(email: 'whoami@plop.com', password: 'super secret')
|
|
|
|
|
|
|
|
gestionnaire.reload
|
|
|
|
expect(gestionnaire.email).to eq('whoami@plop.com')
|
|
|
|
expect(gestionnaire.valid_password?('super secret')).to be(true)
|
|
|
|
end
|
|
|
|
end
|
2015-10-23 16:19:55 +02:00
|
|
|
end
|