Skip to content

Commit

Permalink
WIP Remote Authenticable
Browse files Browse the repository at this point in the history
  • Loading branch information
x4d3 committed Jun 1, 2017
1 parent a434d70 commit 7fdb566
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 7 deletions.
2 changes: 1 addition & 1 deletion api/spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
11 changes: 10 additions & 1 deletion core/app/models/mno/base_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion core/app/models/mno/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ class User < BaseResource
# include ActiveModel::Model
include ActiveModel::Conversion
include ActiveModel::AttributeMethods
# extend ActiveModel::Callbacks
include ActiveModel::Validations

property :id
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion core/app/models/mno_enterprise/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down
2 changes: 1 addition & 1 deletion core/lib/devise/models/remote_authenticatable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 11 additions & 2 deletions core/spec/lib/devise/model/remote_authenticable_spec.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
require "rails_helper"

require 'webmock/rspec'
RSpec.describe Devise::Models::RemoteAuthenticatable < ActiveSupport::TestCase do
let(:user) { build(:user, email: '[email protected]', password: 'oldpass') }

before do
api_stub_for(put: "/users/#{user.id}", response: from_api(user))
stub_api_v2(:get, '/users', [], [], {filter: {email: '[email protected]'}, 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: '[email protected]'})
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
Expand Down
7 changes: 7 additions & 0 deletions core/spec/rails_helper.rb
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
2 changes: 2 additions & 0 deletions core/spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7fdb566

Please sign in to comment.