diff options
| author | Xinhui | 2016-08-05 15:25:39 +0200 |
|---|---|---|
| committer | Xinhui | 2016-08-05 15:25:43 +0200 |
| commit | 96b1abb62d9ae34c9ee7b334c374476b78f82b9b (patch) | |
| tree | 5d732f165743511ca27e4049db8961232ae655af | |
| parent | 6ad42c2e2aa43b18ec2d6d94b998c7e27e55ea78 (diff) | |
| download | chouette-core-96b1abb62d9ae34c9ee7b334c374476b78f82b9b.tar.bz2 | |
Refactoring - Rake task users:sync
| -rw-r--r-- | app/models/organisation.rb | 8 | ||||
| -rw-r--r-- | app/models/user.rb | 6 | ||||
| -rw-r--r-- | config/environments/development.rb | 12 | ||||
| -rw-r--r-- | db/seeds.rb | 2 | ||||
| -rw-r--r-- | lib/tasks/organisations.rake | 13 | ||||
| -rw-r--r-- | lib/tasks/users.rake | 39 | ||||
| -rw-r--r-- | spec/fixtures/users.json | 133 | ||||
| -rw-r--r-- | spec/task/organisations_rake_spec.rb | 17 | ||||
| -rw-r--r-- | spec/task/users_rake_spec.rb | 48 |
9 files changed, 247 insertions, 31 deletions
diff --git a/app/models/organisation.rb b/app/models/organisation.rb index c219bcbc9..9d6e92825 100644 --- a/app/models/organisation.rb +++ b/app/models/organisation.rb @@ -21,12 +21,4 @@ class Organisation < ActiveRecord::Base def add_rule_parameter_set RuleParameterSet.default_for_all_modes( self).save end - - def self.sync_or_create code:, name: - find_or_create_by(code: code) do |org| - org.name = name - org.code = code - org.synced_at = Time.now - end - end end diff --git a/app/models/user.rb b/app/models/user.rb index a8b8a01a0..3233b5efb 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -32,7 +32,11 @@ class User < ActiveRecord::Base extra = extra_attributes.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo} self.name = extra[:full_name] self.email = extra[:email] - self.organisation = Organisation.sync_or_create code: extra[:organisation_code], name: extra[:organisation_name] + + self.organisation = Organisation.find_or_create_by(code: extra[:organisation_code]).tap do |org| + org.name = extra[:organisation_name] + org.synced_at = Time.now + end end private diff --git a/config/environments/development.rb b/config/environments/development.rb index b895fa9a3..4a6844a3c 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -56,17 +56,17 @@ Rails.application.configure do config.company_contact = "http://www.chouette.mobi/club-utilisateurs/contact-support/" config.accept_user_creation = false - config.chouette_authentication_settings = { - type: "database" - } # config.chouette_authentication_settings = { - # type: "cas", - # cas_server: "http://stif-portail-dev.af83.priv/sessions" + # type: "database" # } + config.chouette_authentication_settings = { + type: "cas", + cas_server: "http://stif-portail-dev.af83.priv/sessions" + } config.stif_portail_api = { key: "411e6b8d259bc9900c0caf9db6072013", - url: "http://localhost:3000" + url: "http://stif-portail-dev.af83.priv" } # file to data for demo diff --git a/db/seeds.rb b/db/seeds.rb index cee1e9f9a..11ca7e9c1 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -7,7 +7,7 @@ # cities = City.create([{ :name => 'Chicago' }, { :name => 'Copenhagen' }]) # Mayor.create(:name => 'Emanuel', :city => cities.first) -stif = Organisation.find_or_create_by(name: "STIF") +stif = Organisation.find_or_create_by(name: "STIF", code: "STIF") stif.users.find_or_create_by!(username: "admin") do |user| user.email = 'stif-boiv@af83.com' diff --git a/lib/tasks/organisations.rake b/lib/tasks/organisations.rake index 06e1d3da7..24b02b630 100644 --- a/lib/tasks/organisations.rake +++ b/lib/tasks/organisations.rake @@ -5,7 +5,6 @@ namespace :organisations do conn = Faraday.new(:url => conf[:url]) do |c| c.headers['Authorization'] = "Token token=\"#{conf[:key]}\"" - c.request :url_encoded c.adapter Faraday.default_adapter end @@ -14,12 +13,12 @@ namespace :organisations do end def sync_organisations data - data.each do |org| - Organisation.sync_or_create(code: org['code'], name: org['name']).tap do |organisation| - organisation.name = org['name'] - organisation.synced_at = Time.now - organisation.save if organisation.changed? - puts "✓ Organisation #{organisation.name} has been updated" unless Rails.env.test? + data.each do |el| + Organisation.find_or_create_by(code: el['code']).tap do |org| + org.name = el['name'] + org.synced_at = Time.now + org.save if org.changed? + puts "✓ Organisation #{org.name} has been updated" unless Rails.env.test? end end end diff --git a/lib/tasks/users.rake b/lib/tasks/users.rake new file mode 100644 index 000000000..ca2264381 --- /dev/null +++ b/lib/tasks/users.rake @@ -0,0 +1,39 @@ +namespace :users do + def api_retrieve_user + 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]}\"" + c.adapter Faraday.default_adapter + end + + resp = conn.get '/api/v1/users' + JSON.parse resp.body if resp.status == 200 + end + + def sync_users data + data.each do |el| + User.find_or_create_by(username: el['username']).tap do |user| + user.name = "#{el['firstname']} #{el['lastname']}" + user.email = el['email'] + user.synced_at = Time.now + + # Set organisation + user.organisation = Organisation.find_or_create_by(code: el['organization_code']).tap do |org| + org.name = el['organization_name'] + org.synced_at = Time.now + end + + user.save if user.changed? + puts "✓ user #{user.name} has been updated" unless Rails.env.test? + end + end + end + + desc "Sync users from stif portail" + task sync: :environment do + data = api_retrieve_user + sync_users(data) if data + end +end diff --git a/spec/fixtures/users.json b/spec/fixtures/users.json new file mode 100644 index 000000000..f78e1c445 --- /dev/null +++ b/spec/fixtures/users.json @@ -0,0 +1,133 @@ +[{ + "id": 1, + "username": "admin", + "email": "stif-portail@af83.com", + "firstname": "admin", + "lastname": "stif", + "phone_number": null, + "created_at": "2016-08-05T12:33:45.152Z", + "updated_at": "2016-08-05T12:33:45.152Z", + "profile": "Administrateur", + "organization_name": "STIF", + "organization_code": "STIF" +}, { + "id": 2, + "username": "luc.donnet", + "email": "luc.donnet@af83.com", + "firstname": "Luc", + "lastname": "Donnet", + "phone_number": "+336 69 25 15 71", + "created_at": "2016-08-05T12:34:03.049Z", + "updated_at": "2016-08-05T12:34:03.049Z", + "profile": "Administrateur", + "organization_name": "STIF", + "organization_code": "STIF" +}, { + "id": 3, + "username": "alban.peignier", + "email": "alban.peignier@af83.com", + "firstname": "Alban", + "lastname": "Peignier", + "phone_number": "0697099622", + "created_at": "2016-08-05T12:34:03.161Z", + "updated_at": "2016-08-05T12:34:03.161Z", + "profile": "Administrateur", + "organization_name": "STIF", + "organization_code": "STIF" +}, { + "id": 4, + "username": "pierre.vabre", + "email": "pierre.vabre@af83.com", + "firstname": "Pierre", + "lastname": "Vabre", + "phone_number": "0033778300069", + "created_at": "2016-08-05T12:34:03.256Z", + "updated_at": "2016-08-05T12:34:03.256Z", + "profile": "Administrateur", + "organization_name": "STIF", + "organization_code": "STIF" +}, { + "id": 5, + "username": "laure.dubuc", + "email": "laure.dubuc@af83.com", + "firstname": "Laure", + "lastname": "Dubuc", + "phone_number": "0302098171", + "created_at": "2016-08-05T12:34:03.360Z", + "updated_at": "2016-08-05T12:34:03.360Z", + "profile": "Administrateur", + "organization_name": "STIF", + "organization_code": "STIF" +}, { + "id": 6, + "username": "thomas.haddad", + "email": "thomas.haddad@af83.com", + "firstname": "Thomas", + "lastname": "Haddad", + "phone_number": "0033763987210", + "created_at": "2016-08-05T12:34:03.457Z", + "updated_at": "2016-08-05T12:34:03.457Z", + "profile": "Administrateur", + "organization_name": "STIF", + "organization_code": "STIF" +}, { + "id": 7, + "username": "jean-paul.lescouzeres", + "email": "jean-paul.lescouzeres@af83.com", + "firstname": "Jean-Paul", + "lastname": "Lescouzères", + "phone_number": "0033790632621", + "created_at": "2016-08-05T12:34:03.553Z", + "updated_at": "2016-08-05T12:34:03.553Z", + "profile": "Administrateur", + "organization_name": "STIF", + "organization_code": "STIF" +}, { + "id": 8, + "username": "xinhui.xu", + "email": "xinhui.xu@af83.com", + "firstname": "Xinhui", + "lastname": "Xu", + "phone_number": "+33799062141", + "created_at": "2016-08-05T12:34:03.649Z", + "updated_at": "2016-08-05T12:34:03.649Z", + "profile": "Administrateur", + "organization_name": "STIF", + "organization_code": "STIF" +}, { + "id": 9, + "username": "edouard.maffert", + "email": "edouard.maffert@af83.com", + "firstname": "Edouard", + "lastname": "Maffert", + "phone_number": "+33614563995", + "created_at": "2016-08-05T12:34:03.756Z", + "updated_at": "2016-08-05T12:34:03.756Z", + "profile": "Administrateur", + "organization_name": "STIF", + "organization_code": "STIF" +}, { + "id": 10, + "username": "john.doe", + "email": "john.doe@af83.com", + "firstname": "John", + "lastname": "Doe", + "phone_number": "00337 67 87 93 02", + "created_at": "2016-08-05T12:34:03.894Z", + "updated_at": "2016-08-05T12:34:03.894Z", + "profile": "Référent IV Transporteur", + "organization_name": "RATP", + "organization_code": "RATP" +}, { + "id": 11, + "username": "jane.doe", + "email": "jane.doe@af83.com", + "firstname": "Jane", + "lastname": "Doe", + "phone_number": "+337 07 54 55 73", + "created_at": "2016-08-05T12:34:03.995Z", + "updated_at": "2016-08-05T12:34:03.995Z", + "profile": "Référent IV Transporteur", + "organization_name": "RATP", + "organization_code": "RATP" +}] diff --git a/spec/task/organisations_rake_spec.rb b/spec/task/organisations_rake_spec.rb index 9718533e9..92696810b 100644 --- a/spec/task/organisations_rake_spec.rb +++ b/spec/task/organisations_rake_spec.rb @@ -21,26 +21,27 @@ describe 'organisations:sync rake task' do end it 'should create new organisations' do - run_rake_task + expect{run_rake_task}.to change{ Organisation.count }.by(5) expect(WebMock).to have_requested(:get, "#{conf[:url]}/api/v1/organizations"). with(headers: { 'Authorization' => "Token token=\"#{conf[:key]}\"" }) - expect(Organisation.count).to eq(6) + + 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 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 + 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' - run_rake_task - expect(Organisation.count).to eq(6) + expect{run_rake_task}.to change{ Organisation.count }.by(4) end end end diff --git a/spec/task/users_rake_spec.rb b/spec/task/users_rake_spec.rb new file mode 100644 index 000000000..d7c2ed8c9 --- /dev/null +++ b/spec/task/users_rake_spec.rb @@ -0,0 +1,48 @@ +require 'rails_helper' +require 'rake' + +describe 'users:sync rake task' do + before :all do + Rake.application.rake_require "tasks/users" + Rake::Task.define_task(:environment) + end + + describe 'users:sync' do + let(:conf) { Rails.application.config.stif_portail_api } + let :run_rake_task do + Rake::Task["users:sync"].reenable + Rake.application.invoke_task "users:sync" + end + + before :each do + stub_request(:get, "#{conf[:url]}/api/v1/users"). + with(headers: { 'Authorization' => "Token token=\"#{conf[:key]}\"" }). + to_return(body: File.open(File.join(Rails.root, 'spec', 'fixtures', 'users.json')), status: 200) + end + + it 'should create new users' do + run_rake_task + expect(WebMock).to have_requested(:get, "#{conf[:url]}/api/v1/users"). + with(headers: { 'Authorization' => "Token token=\"#{conf[:key]}\"" }) + expect(User.count).to eq(11) + expect(Organisation.count).to eq(3) + end + + it 'should update existing users' do + create :user, username: 'alban.peignier', email:'dummy@example.com', updated_at: 10.days.ago + run_rake_task + user = User.find_by(username: 'alban.peignier') + + expect(user.name).to eq('Alban Peignier') + expect(user.email).to eq('alban.peignier@af83.com') + expect(user.updated_at.utc).to be_within(1.second).of Time.now + expect(user.synced_at.utc).to be_within(1.second).of Time.now + end + + it 'should not create new user if username is already present' do + create :user, username: 'alban.peignier' + run_rake_task + expect(User.count).to eq(11) + end + end +end |
