aboutsummaryrefslogtreecommitdiffstats
path: root/spec/models/user_spec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/models/user_spec.rb')
-rw-r--r--spec/models/user_spec.rb51
1 files changed, 51 insertions, 0 deletions
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)}