diff options
| author | Xinhui | 2016-08-10 11:07:30 +0200 |
|---|---|---|
| committer | Xinhui | 2016-08-10 11:26:48 +0200 |
| commit | b39899e26ec2ab3cba7d8f30fd02ee3d06ba01e2 (patch) | |
| tree | 04d85c043faeba69e2d8f88c5e4833e5c527f519 | |
| parent | 53526748c12eb790647b656254b009a08278b70f (diff) | |
| download | chouette-core-b39899e26ec2ab3cba7d8f30fd02ee3d06ba01e2.tar.bz2 | |
Refactoring rspec portail sync code into model spec
| -rw-r--r-- | spec/models/organisation_spec.rb | 37 | ||||
| -rw-r--r-- | spec/models/user_spec.rb | 51 | ||||
| -rw-r--r-- | spec/task/organisations_rake_spec.rb | 47 | ||||
| -rw-r--r-- | spec/task/users_rake_spec.rb | 61 |
4 files changed, 87 insertions, 109 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 diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 5d88fb209..16fbed27a 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -50,6 +50,57 @@ describe User, :type => :model do end end + describe "Portail sync" do + let(:conf) { Rails.application.config.stif_portail_api } + 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 retrieve data from portail api' do + expect(User.portail_api_request).to be_truthy + expect(WebMock).to have_requested(:get, "#{conf[:url]}/api/v1/users"). + with(headers: { 'Authorization' => "Token token=\"#{conf[:key]}\"" }) + end + + it 'should create new users' do + User.portail_sync + 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 + User.portail_sync + 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 update organisation assignement' do + create :user, username: 'alban.peignier', organisation: create(:organisation) + User.portail_sync + expect(User.find_by(username: 'alban.peignier').organisation.name).to eq("STIF") + end + + it 'should update locked_at attribute' do + create :user, username: 'alban.peignier', locked_at: Time.now + User.portail_sync + expect(User.find_by(username: 'alban.peignier').locked_at).to be_nil + expect(User.find_by(username: 'jane.doe').locked_at).to eq("2016-08-05T12:34:03.995Z") + end + + it 'should not create new user if username is already present' do + create :user, username: 'alban.peignier' + User.portail_sync + expect(User.count).to eq(11) + end + end + describe "#destroy" do let!(:organisation){create(:organisation)} let!(:user){create(:user, :organisation => organisation)} diff --git a/spec/task/organisations_rake_spec.rb b/spec/task/organisations_rake_spec.rb deleted file mode 100644 index 92696810b..000000000 --- a/spec/task/organisations_rake_spec.rb +++ /dev/null @@ -1,47 +0,0 @@ -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 - 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.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.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{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 deleted file mode 100644 index 8b90dffb2..000000000 --- a/spec/task/users_rake_spec.rb +++ /dev/null @@ -1,61 +0,0 @@ -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 update organisation assignement' do - create :user, username: 'alban.peignier', organisation: create(:organisation) - run_rake_task - expect(User.find_by(username: 'alban.peignier').organisation.name).to eq("STIF") - end - - it 'should update locked_at attribute' do - create :user, username: 'alban.peignier', locked_at: Time.now - run_rake_task - expect(User.find_by(username: 'alban.peignier').locked_at).to be_nil - expect(User.find_by(username: 'jane.doe').locked_at).to eq("2016-08-05T12:34:03.995Z") - 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 |
