diff options
Diffstat (limited to 'spec')
| -rw-r--r-- | spec/factories/chouette_2_factories.rb | 1 | ||||
| -rw-r--r-- | spec/features/users/user_edit_spec.rb | 2 | ||||
| -rw-r--r-- | spec/features/users/user_index_spec.rb | 1 | ||||
| -rw-r--r-- | spec/features/users/user_show_spec.rb | 3 | ||||
| -rw-r--r-- | spec/fixtures/organizations.json | 98 | ||||
| -rw-r--r-- | spec/models/user_spec.rb | 52 | ||||
| -rw-r--r-- | spec/spec_helper.rb | 4 | ||||
| -rw-r--r-- | spec/support/devise.rb | 2 | ||||
| -rw-r--r-- | spec/task/organisations_rake_spec.rb | 46 |
9 files changed, 197 insertions, 12 deletions
diff --git a/spec/factories/chouette_2_factories.rb b/spec/factories/chouette_2_factories.rb index 6234f6540..b88b0d773 100644 --- a/spec/factories/chouette_2_factories.rb +++ b/spec/factories/chouette_2_factories.rb @@ -2,6 +2,7 @@ FactoryGirl.define do factory :organisation do sequence(:name) { |n| "Organisation #{n}" } + sequence(:code) { |n| "000#{n}" } end factory :referential do diff --git a/spec/features/users/user_edit_spec.rb b/spec/features/users/user_edit_spec.rb index c7c742416..23a58ecae 100644 --- a/spec/features/users/user_edit_spec.rb +++ b/spec/features/users/user_edit_spec.rb @@ -36,9 +36,7 @@ feature 'User edit', :devise do # Then I see my own 'edit profile' page scenario "user cannot cannot edit another user's profile", :me do me = FactoryGirl.create(:user) - me.confirm! other = FactoryGirl.create(:user, email: 'other@example.com') - other.confirm! login_as(me, :scope => :user) visit edit_user_registration_path(other) expect(page).to have_content 'Votre Profil' diff --git a/spec/features/users/user_index_spec.rb b/spec/features/users/user_index_spec.rb index 2f6ec4426..2a9199da3 100644 --- a/spec/features/users/user_index_spec.rb +++ b/spec/features/users/user_index_spec.rb @@ -19,7 +19,6 @@ feature 'User index page', :devise do # Then I see my own email address scenario 'user sees own email address' do user = create(:user) - user.confirm! login_as(user, scope: :user) visit organisation_path expect(page).to have_content user.name.truncate(15) diff --git a/spec/features/users/user_show_spec.rb b/spec/features/users/user_show_spec.rb index 4a56e3255..d840d752c 100644 --- a/spec/features/users/user_show_spec.rb +++ b/spec/features/users/user_show_spec.rb @@ -19,7 +19,6 @@ feature 'User profile page', :devise do # Then I see my own email address scenario 'user sees own profile' do user = FactoryGirl.create(:user) - user.confirm! login_as(user, :scope => :user) visit organisation_user_path(user) # FIXME ref #819 @@ -33,9 +32,7 @@ feature 'User profile page', :devise do # Then I see an 'access denied' message scenario "user cannot see another user's profile" do me = FactoryGirl.create(:user) - me.confirm! other = FactoryGirl.create(:user, email: 'other@example.com', :organisation => me.organisation) - other.confirm! login_as(me, :scope => :user) Capybara.current_session.driver.header 'Referer', authenticated_root_path visit organisation_user_path(other) 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/models/user_spec.rb b/spec/models/user_spec.rb index e22e53797..5d88fb209 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -1,14 +1,62 @@ require 'spec_helper' describe User, :type => :model do - #it { should validate_uniqueness_of :email } - #it { should validate_presence_of :name } + # it { should validate_uniqueness_of :email } + # it { should validate_presence_of :name } + + describe "SSO" do + let(:ticket) do + CASClient::ServiceTicket.new("ST-test", nil).tap do |ticket| + ticket.extra_attributes = { + :full_name => 'john doe', + :username => 'john.doe', + :email => 'john.doe@af83.com', + :organisation_code => '0083', + :organisation_name => 'af83' + } + ticket.user = "john.doe" + ticket.success = true + end + end + + context 'First time sign on' do + it 'should create a new user if user is not registered' do + expect{User.authenticate_with_cas_ticket(ticket)}.to change{ User.count } + user = User.find_by(username: 'john.doe') + expect(user.email).to eq(ticket.extra_attributes[:email]) + expect(user.name).to eq(ticket.extra_attributes[:full_name]) + end + + it 'should create a new organisation if organisation is not present' do + expect{User.authenticate_with_cas_ticket(ticket)}.to change{ Organisation.count } + expect(Organisation.find_by(code: ticket.extra_attributes[:organisation_code])).to be_truthy + end + + it 'should not create a new organisation if organisation is already present' do + ticket.extra_attributes[:organisation_code] = create(:organisation).code + expect{User.authenticate_with_cas_ticket(ticket)}.not_to change{ Organisation.count } + end + end + + context 'Update attributes on sign on' do + let!(:organisation) { create(:organisation) } + let!(:user) { create(:user, username: 'john.doe', name:'fake name' , email: 'test@example.com', :organisation => organisation) } + + it 'should update user attributes on sign on' do + User.authenticate_with_cas_ticket(ticket) + expect(user.reload.email).to eq(ticket.extra_attributes[:email]) + expect(user.reload.name).to eq(ticket.extra_attributes[:full_name]) + end + end + end describe "#destroy" do let!(:organisation){create(:organisation)} let!(:user){create(:user, :organisation => organisation)} + context "user's organisation contains many user" do let!(:other_user){create(:user, :organisation => organisation)} + it "should destoy also user's organisation" do user.destroy expect(Organisation.where(:name => organisation.name).exists?).to be_truthy 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/support/devise.rb b/spec/support/devise.rb index cd5782f36..d33812e6f 100644 --- a/spec/support/devise.rb +++ b/spec/support/devise.rb @@ -4,7 +4,6 @@ module DeviseRequestHelper def login_user organisation = Organisation.where(:name => "first").first_or_create(attributes_for(:organisation)) @user ||= create(:user, :organisation => organisation) - @user.confirm! login_as @user, :scope => :user # post_via_redirect user_session_path, 'user[email]' => @user.email, 'user[password]' => @user.password end @@ -36,7 +35,6 @@ module DeviseControllerHelper @request.env["devise.mapping"] = Devise.mappings[:user] organisation = Organisation.where(:name => "first").first_or_create(attributes_for(:organisation)) user = create(:user, :organisation => organisation) - user.confirm! sign_in user end end 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 |
