diff options
Diffstat (limited to 'spec/models/organisation_spec.rb')
| -rw-r--r-- | spec/models/organisation_spec.rb | 37 |
1 files changed, 36 insertions, 1 deletions
diff --git a/spec/models/organisation_spec.rb b/spec/models/organisation_spec.rb index fb9d3629b..9d9749b18 100644 --- a/spec/models/organisation_spec.rb +++ b/spec/models/organisation_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe Organisation do +describe Organisation, :type => :model do it { should validate_presence_of(:name) } it { should validate_uniqueness_of(:name) } @@ -13,4 +13,39 @@ describe Organisation do organisation = create(:organisation) organisation.rule_parameter_sets.size.should == 1 end + + describe "Portail sync" do + let(:conf) { Rails.application.config.stif_portail_api } + before :each do + stub_request(:get, "#{conf[:url]}/api/v1/organizations"). + with(headers: { 'Authorization' => "Token token=\"#{conf[:key]}\"" }). + to_return(body: File.open(File.join(Rails.root, 'spec', 'fixtures', 'organizations.json')), status: 200) + end + + it 'should retrieve data from portail api' do + expect(Organisation.portail_api_request).to be_truthy + expect(WebMock).to have_requested(:get, "#{conf[:url]}/api/v1/organizations"). + with(headers: { 'Authorization' => "Token token=\"#{conf[:key]}\"" }) + end + + it 'should create new organisations' do + expect{Organisation.portail_sync}.to change{ Organisation.count }.by(5) + expect(Organisation.all.map(&:name)).to include 'ALBATRANS', 'OPTILE', 'SNCF', 'STIF' + end + + it 'should update existing organisations' do + create :organisation, name: 'dummy_name', code:'RATP', updated_at: 10.days.ago + Organisation.portail_sync + Organisation.find_by(code: 'RATP').tap do |org| + expect(org.name).to eq('RATP') + expect(org.updated_at.utc).to be_within(1.second).of Time.now + expect(org.synced_at.utc).to be_within(1.second).of Time.now + end + end + + it 'should not create organisation if code is already present' do + create :organisation, code:'RATP' + expect{Organisation.portail_sync}.to change{ Organisation.count }.by(4) + end + end end |
