From 7fdb5660875bef65bc4bb929a4790c372a820ab4 Mon Sep 17 00:00:00 2001 From: x4d3 Date: Thu, 1 Jun 2017 18:04:50 +0100 Subject: [PATCH] WIP Remote Authenticable --- api/spec/spec_helper.rb | 2 +- core/app/models/mno/base_resource.rb | 11 ++++++++++- core/app/models/mno/user.rb | 2 +- core/app/models/mno_enterprise/user.rb | 2 +- core/lib/devise/models/remote_authenticatable.rb | 2 +- .../lib/devise/model/remote_authenticable_spec.rb | 13 +++++++++++-- core/spec/rails_helper.rb | 7 +++++++ core/spec/spec_helper.rb | 2 ++ 8 files changed, 34 insertions(+), 7 deletions(-) diff --git a/api/spec/spec_helper.rb b/api/spec/spec_helper.rb index ec6fdc53a..77315a3f5 100644 --- a/api/spec/spec_helper.rb +++ b/api/spec/spec_helper.rb @@ -15,7 +15,7 @@ # # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration require "fakeweb" - +# Library for stubbing and setting expectations on HTTP requests in Ruby. require 'webmock/rspec' RSpec.configure do |config| diff --git a/core/app/models/mno/base_resource.rb b/core/app/models/mno/base_resource.rb index 18e738ac9..9ddedeeaf 100644 --- a/core/app/models/mno/base_resource.rb +++ b/core/app/models/mno/base_resource.rb @@ -31,6 +31,7 @@ def self.parse_types(res) end class BaseResource < ::JsonApiClient::Resource + include ActiveModel::Callbacks self.site = URI.join(MnoEnterprise.api_host, MnoEnterprise.mno_api_v2_root_path).to_s self.parser = CustomParser @@ -61,7 +62,15 @@ def update_attribute(name, value) end def save(*args) - super() + run_callbacks :save do + super + end + end + + def update_attributes(*args) + run_callbacks :update do + super + end end def cache_key(*timestamp_names) diff --git a/core/app/models/mno/user.rb b/core/app/models/mno/user.rb index c68debb5f..b35e439e9 100644 --- a/core/app/models/mno/user.rb +++ b/core/app/models/mno/user.rb @@ -5,7 +5,6 @@ class User < BaseResource # include ActiveModel::Model include ActiveModel::Conversion include ActiveModel::AttributeMethods - # extend ActiveModel::Callbacks include ActiveModel::Validations property :id @@ -47,6 +46,7 @@ def validate_each(record,attribute,value) end end + before_update :connard def self.validates_uniqueness_of(*attr_names) validates_with RemoteUniquenessValidator, _merge_attributes(attr_names) diff --git a/core/app/models/mno_enterprise/user.rb b/core/app/models/mno_enterprise/user.rb index 09b89f812..c241d3c1b 100644 --- a/core/app/models/mno_enterprise/user.rb +++ b/core/app/models/mno_enterprise/user.rb @@ -94,7 +94,7 @@ class User < BaseResource # The auth_hash includes an email and password # Return nil in case of failure def self.authenticate(auth_hash) - u = self.post("user_sessions", auth_hash) + u = self.post('user_sessions', auth_hash) if u && u.id u.clear_attribute_changes! diff --git a/core/lib/devise/models/remote_authenticatable.rb b/core/lib/devise/models/remote_authenticatable.rb index ada85e620..feff92b8a 100644 --- a/core/lib/devise/models/remote_authenticatable.rb +++ b/core/lib/devise/models/remote_authenticatable.rb @@ -63,7 +63,7 @@ def serialize_into_session(record) # we won't have access to dirty attributes. # Flag change before the model is saved. def track_password_changed - @password_changed = password_changed? + @password_changed = send_password_change_notification true # before callback needs to return true to continue end diff --git a/core/spec/lib/devise/model/remote_authenticable_spec.rb b/core/spec/lib/devise/model/remote_authenticable_spec.rb index 7c6f5b6ab..b2505bf88 100644 --- a/core/spec/lib/devise/model/remote_authenticable_spec.rb +++ b/core/spec/lib/devise/model/remote_authenticable_spec.rb @@ -1,13 +1,22 @@ require "rails_helper" - +require 'webmock/rspec' RSpec.describe Devise::Models::RemoteAuthenticatable < ActiveSupport::TestCase do let(:user) { build(:user, email: 'test@maestrano.com', password: 'oldpass') } before do - api_stub_for(put: "/users/#{user.id}", response: from_api(user)) + stub_api_v2(:get, '/users', [], [], {filter: {email: 'test@maestrano.com'}, page: {number: 1, size: 1}}) + stub_api_v2(:post, '/users', user) end describe 'Sends an email on password update' do + + before{ + stub_api_v2(:get, '/users', user, [], [], {filter: {Bconfirmation_token: 'test@maestrano.com'}) + stub_request(:get, "https://api-enterprise.maestrano.com/api/mnoe/v2/users?filter%5Bconfirmation_token%5D=1e243fa1180e32f3ec66a648835d1fbca7912223a487eac36be22b095a01b5a5"). + with(headers: {'Accept'=>'application/vnd.api+json', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization'=>'Basic bXlfdGVuYW50X2lkOm15X3RlbmFudF9hY2Nlc3Nfa2V5', 'Content-Type'=>'application/vnd.api+json', 'User-Agent'=>'Faraday v0.12.1'}). + to_return(status: 200, body: "", headers: {}) + } + subject { user.update(updates) } context 'when password change notifications are enabled' do diff --git a/core/spec/rails_helper.rb b/core/spec/rails_helper.rb index 8862019cc..61aed03b7 100644 --- a/core/spec/rails_helper.rb +++ b/core/spec/rails_helper.rb @@ -1,9 +1,16 @@ # This file is copied to spec/ when you run 'rails generate rspec:install' ENV["RAILS_ENV"] ||= 'test' + +# Library for stubbing and setting expectations on HTTP requests in Ruby. +require 'fakeweb' +require 'webmock/rspec' + + require 'spec_helper' require 'her' require 'factory_girl_rails' + # Load the Dummy application require File.expand_path("../../spec/dummy/config/environment.rb", __FILE__) require 'rspec/rails' diff --git a/core/spec/spec_helper.rb b/core/spec/spec_helper.rb index cfb18dcda..0d13fba34 100644 --- a/core/spec/spec_helper.rb +++ b/core/spec/spec_helper.rb @@ -15,6 +15,8 @@ # # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration RSpec.configure do |config| + + # The settings below are suggested to provide a good initial experience # with RSpec, but feel free to customize to your heart's content. =begin