diff options
| author | Xinhui | 2016-08-05 11:23:03 +0200 |
|---|---|---|
| committer | Xinhui | 2016-08-05 11:23:03 +0200 |
| commit | 7cfd4faefab17e556ac66dd0e52c5942b289ec62 (patch) | |
| tree | c5630a1a3112f24a089b80ff13556892fef527f5 /spec | |
| parent | 9a818eeb8991bf0e07f2c9a9837930d153a0492f (diff) | |
| download | chouette-core-7cfd4faefab17e556ac66dd0e52c5942b289ec62.tar.bz2 | |
Rspec task organisations:sync
Diffstat (limited to 'spec')
| -rw-r--r-- | spec/fixtures/organizations.json | 98 | ||||
| -rw-r--r-- | spec/spec_helper.rb | 4 | ||||
| -rw-r--r-- | spec/task/organisations_rake_spec.rb | 46 |
3 files changed, 146 insertions, 2 deletions
diff --git a/spec/fixtures/organizations.json b/spec/fixtures/organizations.json new file mode 100644 index 000000000..b70f99520 --- /dev/null +++ b/spec/fixtures/organizations.json @@ -0,0 +1,98 @@ +[{ + "id": 1, + "name": "STIF", + "code": "STIF", + "created_at": "2016-05-17T15:33:45.951Z", + "updated_at": "2016-05-17T15:33:45.951Z", + "organization_users": { + "users_count": 9 + }, + "suborganizations": { + "suborganizations_count": 4, + "suborganizations": [{ + "id": 6, + "name": "ALBATRANS", + "code": "ALBATRANS" + }, { + "id": 5, + "name": "OPTILE", + "code": "OPTILE" + }, { + "id": 2, + "name": "RATP", + "code": "RATP" + }, { + "id": 3, + "name": "SNCF", + "code": "SNCF" + }] + } +}, { + "id": 3, + "name": "SNCF", + "code": "SNCF", + "created_at": "2016-05-17T16:04:27.047Z", + "updated_at": "2016-05-17T16:04:27.047Z", + "organization_users": { + "users_count": 2 + }, + "suborganizations": { + "suborganizations_count": 0, + "suborganizations": [] + }, + "organization_parent": { + "parent_id": 1, + "parent_name": "STIF" + } +}, { + "id": 5, + "name": "OPTILE", + "code": "OPTILE", + "created_at": "2016-05-17T16:04:27.655Z", + "updated_at": "2016-05-17T16:04:27.655Z", + "organization_users": { + "users_count": 0 + }, + "suborganizations": { + "suborganizations_count": 0, + "suborganizations": [] + }, + "organization_parent": { + "parent_id": 1, + "parent_name": "STIF" + } +}, { + "id": 6, + "name": "ALBATRANS", + "code": "ALBATRANS", + "created_at": "2016-05-17T16:04:27.955Z", + "updated_at": "2016-05-17T16:04:27.955Z", + "organization_users": { + "users_count": 0 + }, + "suborganizations": { + "suborganizations_count": 0, + "suborganizations": [] + }, + "organization_parent": { + "parent_id": 1, + "parent_name": "STIF" + } +}, { + "id": 2, + "name": "RATP", + "code": "RATP", + "created_at": "2016-05-17T16:04:26.646Z", + "updated_at": "2016-05-31T10:06:39.349Z", + "organization_users": { + "users_count": 8 + }, + "suborganizations": { + "suborganizations_count": 0, + "suborganizations": [] + }, + "organization_parent": { + "parent_id": 1, + "parent_name": "STIF" + } +}] diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 47cefaedf..93b148496 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -11,7 +11,7 @@ require 'capybara/poltergeist' require 'georuby-ext' require 'will_paginate/array' require 'fakeweb' - +require 'webmock/rspec' require 'simplecov' if ENV['JOB_NAME'] @@ -49,7 +49,7 @@ RSpec.configure do |config| config.filter_run_excluding :js => true config.run_all_when_everything_filtered = true config.include TokenInputHelper, :type => :feature - + # ## Mock Framework # # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line: diff --git a/spec/task/organisations_rake_spec.rb b/spec/task/organisations_rake_spec.rb new file mode 100644 index 000000000..9718533e9 --- /dev/null +++ b/spec/task/organisations_rake_spec.rb @@ -0,0 +1,46 @@ +require 'rails_helper' +require 'rake' + +describe 'organisations:sync rake task' do + before :all do + Rake.application.rake_require "tasks/organisations" + Rake::Task.define_task(:environment) + end + + describe 'organisations:sync' do + let(:conf) { Rails.application.config.stif_portail_api } + let :run_rake_task do + Rake::Task["organisations:sync"].reenable + Rake.application.invoke_task "organisations:sync" + end + + 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 create new organisations' do + run_rake_task + expect(WebMock).to have_requested(:get, "#{conf[:url]}/api/v1/organizations"). + with(headers: { 'Authorization' => "Token token=\"#{conf[:key]}\"" }) + expect(Organisation.count).to eq(6) + end + + it 'should update existing organisations' do + create :organisation, name: 'dummy_name', code:'RATP', updated_at: 10.days.ago + run_rake_task + organisation = Organisation.find_by(code: 'RATP') + + expect(organisation.name).to eq('RATP') + expect(organisation.updated_at.utc).to be_within(1.second).of Time.now + expect(organisation.synced_at.utc).to be_within(1.second).of Time.now + end + + it 'should not create organisation if code is already present' do + create :organisation, code:'RATP' + run_rake_task + expect(Organisation.count).to eq(6) + end + end +end |
