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 | |
| parent | 9a818eeb8991bf0e07f2c9a9837930d153a0492f (diff) | |
| download | chouette-core-7cfd4faefab17e556ac66dd0e52c5942b289ec62.tar.bz2 | |
Rspec task organisations:sync
| -rw-r--r-- | Gemfile | 1 | ||||
| -rw-r--r-- | Gemfile.lock | 9 | ||||
| -rw-r--r-- | config/environments/test.rb | 6 | ||||
| -rw-r--r-- | lib/tasks/organisations.rake | 10 | ||||
| -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 |
7 files changed, 167 insertions, 7 deletions
@@ -133,6 +133,7 @@ group :test, :development do gem 'pry-rails' gem 'rspec-rails', '~> 3.1.0' gem 'fakeweb' + gem 'webmock' gem 'capybara', '~> 2.4.0' gem 'poltergeist' gem 'launchy' diff --git a/Gemfile.lock b/Gemfile.lock index d543d272d..c9ad505a2 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -95,6 +95,8 @@ GEM coffee-script-source execjs coffee-script-source (1.9.1) + crack (0.4.3) + safe_yaml (~> 1.0.0) daemons (1.1.9) database_cleaner (1.4.1) dbf (3.0.3) @@ -186,6 +188,7 @@ GEM has_scope (0.6.0) actionpack (>= 3.2, < 5) activesupport (>= 3.2, < 5) + hashdiff (0.3.0) highline (1.7.1) hike (1.2.3) hitimes (1.2.2) @@ -386,6 +389,7 @@ GEM rubycas-client (2.3.9) activesupport rubyzip (1.1.7) + safe_yaml (1.0.4) sass (3.2.19) sass-rails (4.0.5) railties (>= 4.0.0, < 5.0) @@ -469,6 +473,10 @@ GEM json (>= 1.8.0) warden (1.2.3) rack (>= 1.0) + webmock (2.1.0) + addressable (>= 2.3.6) + crack (>= 0.3.2) + hashdiff websocket-driver (0.5.3) websocket-extensions (>= 0.1.0) websocket-driver (0.5.3-java) @@ -580,6 +588,7 @@ DEPENDENCIES transpec turbolinks uglifier (~> 2.7.2) + webmock will_paginate (~> 3.0.7) will_paginate-bootstrap (~> 1.0.1) diff --git a/config/environments/test.rb b/config/environments/test.rb index 3e0c486e6..512afab4f 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -54,6 +54,12 @@ Rails.application.configure do cas_server: "http://stif-portail-dev.af83.priv/sessions" } + config.stif_portail_api = + { + key: "411e6b8d259bc9900c0caf9db6072013", + url: "http://localhost:3000" + } + # file to data for demo config.demo_data = "tmp/demo.zip" diff --git a/lib/tasks/organisations.rake b/lib/tasks/organisations.rake index a887cd277..06e1d3da7 100644 --- a/lib/tasks/organisations.rake +++ b/lib/tasks/organisations.rake @@ -1,7 +1,7 @@ namespace :organisations do def api_retrieve_organisation - last_sync = User.minimum(:synced_at) - conf = Rails.application.config.stif_portail_api + conf = Rails.application.config.try(:stif_portail_api) + raise 'Rails.application.config.stif_portail_api settings is not defined' unless conf conn = Faraday.new(:url => conf[:url]) do |c| c.headers['Authorization'] = "Token token=\"#{conf[:key]}\"" @@ -9,8 +9,8 @@ namespace :organisations do c.adapter Faraday.default_adapter end - response = conn.get '/api/v1/organizations' - JSON.parse response.body if response.status == 200 + resp = conn.get '/api/v1/organizations' + JSON.parse resp.body if resp.status == 200 end def sync_organisations data @@ -19,7 +19,7 @@ namespace :organisations do organisation.name = org['name'] organisation.synced_at = Time.now organisation.save if organisation.changed? - puts "Organisation #{organisation.name} has been updated" + puts "✓ Organisation #{organisation.name} has been updated" unless Rails.env.test? end end end 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 |
