diff options
| -rw-r--r-- | Gemfile.lock | 2 | ||||
| -rw-r--r-- | app/models/user.rb | 22 | ||||
| -rw-r--r-- | config/environments/test.rb | 6 | ||||
| -rw-r--r-- | config/initializers/devise.rb | 2 | ||||
| -rw-r--r-- | spec/models/user_spec.rb | 39 |
5 files changed, 59 insertions, 12 deletions
diff --git a/Gemfile.lock b/Gemfile.lock index 5e76888fc..23d8e4d65 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -423,7 +423,7 @@ GEM ffi spreadsheet (1.0.2) ruby-ole (>= 1.0) - spring (1.3.3) + spring (1.7.2) sprockets (2.12.3) hike (~> 1.2) multi_json (~> 1.0) diff --git a/app/models/user.rb b/app/models/user.rb index e00b6a35a..9e67ba743 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -31,13 +31,21 @@ class User < ActiveRecord::Base def cas_extra_attributes=(extra_attributes) extra_attributes.each do |name, value| - # case name.to_sym - # Extra attributes - # when :fullname - # self.fullname = value - # when :email - # self.email = value - # end + case name.to_sym + when :full_name + self.name = value + when :email + self.email = value + when :username + self.username = value + end + end + self.organisation = self.cas_assign_or_create_organisation extra_attributes[:organisation_name] + end + + def cas_assign_or_create_organisation name + Organisation.find_or_create_by(name: name) do |organisation| + organisation.name = name end end diff --git a/config/environments/test.rb b/config/environments/test.rb index 9db6ad9f3..c239c6c89 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -46,8 +46,12 @@ Rails.application.configure do config.company_contact = "http://www.chouette.mobi/club-utilisateurs/contact-support/" config.accept_user_creation = true + # config.chouette_authentication_settings = { + # type: "database" + # } config.chouette_authentication_settings = { - type: "database" + type: "cas", + cas_server: "http://localhost:3000/sessions" } # file to data for demo diff --git a/config/initializers/devise.rb b/config/initializers/devise.rb index f8d12a0ee..136f50c92 100644 --- a/config/initializers/devise.rb +++ b/config/initializers/devise.rb @@ -302,7 +302,7 @@ Devise.setup do |config| # By default, devise_cas_authenticatable will create users. If you would rather # require user records to already exist locally before they can authenticate via # CAS, uncomment the following line. - config.cas_create_user = false + # config.cas_create_user = false # You can enable Single Sign Out, which by default is disabled. # config.cas_enable_single_sign_out = true diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index e22e53797..ea1170aa1 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -1,14 +1,49 @@ 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: 'xinhui.xu', + email: 'john.doe@af83.com', + organisation_code: '0083', + organisation_name: 'af83' + } + ticket.user = "xinhui.xu" + ticket.success = true + end + end + + 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: 'xinhui.xu') + 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 } + end + + it 'should not create a new organisation if organisation is already present' do + organisation = create :organisation + ticket.extra_attributes[:organisation_name] = organisation.name + expect{User.authenticate_with_cas_ticket(ticket)}.not_to change{ Organisation.count } + 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 |
