Skip to content

Commit

Permalink
Adapt RegistrationSpec
Browse files Browse the repository at this point in the history
  • Loading branch information
x4d3 committed Jun 1, 2017
1 parent e9d3832 commit a434d70
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 45 deletions.
31 changes: 9 additions & 22 deletions api/spec/requests/devise/registration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,17 @@ module MnoEnterprise
let(:confirmation_token) { 'wky763pGjtzWR7dP44PD' }
let(:user) { build(:user, :unconfirmed, confirmation_token: confirmation_token) }
let(:email_uniq_resp) { [] }
let(:signup_attrs) { {name: "John", surname: "Doe", email: '[email protected]', password: 'securepassword'} }
let(:signup_attrs) { {name: 'John', surname: 'Doe', email: '[email protected]', password: 'securepassword'} }

# Stub user calls
before { api_stub_for(post: '/users', response: from_api(user)) }
before { api_stub_for(get: "/users/#{user.id}", response: from_api(user)) }
before { api_stub_for(put: "/users/#{user.id}", response: from_api(user)) }

# Stub user retrieval using confirmation token
before { api_stub_for(
get: '/users',
params: {filter: {confirmation_token: '**'}, limit: 1},
response: from_api([])
) }

# Stub user email uniqueness check
before { api_stub_for(
get: '/users',
params: {filter: {email: '**'}, limit: 1},
response: -> { from_api(email_uniq_resp) }
) }

# Stub org_invites retrieval
before { api_stub_for(get: '/org_invites', response: from_api([])) }
before {
stub_api_v2(:post, '/users', user)
stub_api_v2(:get, "/users/#{user.id}", user, %i(deletion_requests organizations orga_relations dashboards))
stub_api_v2(:patch, "/users/#{user.id}", user)
stub_api_v2(:get, '/orga_invites', [], [], {filter: {user_email: signup_attrs[:email]}})

stub_api_v2(:get, '/users', email_uniq_resp, [], {filter: {email: signup_attrs[:email]}, page: {number: 1, size: 1}})
}

describe 'signup' do
subject { post '/mnoe/auth/users', user: signup_attrs }
Expand All @@ -51,7 +38,7 @@ module MnoEnterprise
end

describe 'failure' do
let(:email_uniq_resp) { [from_api(user)] }
let(:email_uniq_resp) { [user] }
before { subject }

it 'does not log the user in' do
Expand Down
29 changes: 24 additions & 5 deletions core/app/models/mno/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ class User < BaseResource
# extend ActiveModel::Callbacks
include ActiveModel::Validations

property :id
property :created_at, type: :time
property :updated_at, type: :time
property :email, type: :string
property :name, type: :string
property :surname, type: :string
property :company, type: :string
property :phone, type: :string

property :password
property :api_secret, type: :string
property :api_key, type: :string
property :phone_country_code, type: :string
Expand All @@ -32,18 +33,36 @@ class User < BaseResource
define_model_callbacks :update #required by Devise
define_model_callbacks :create #required by Devise
define_model_callbacks :save #required by Devise


class RemoteUniquenessValidator < ::ActiveModel::EachValidator
def validate_each(record,attribute,value)
list = record.class.where({ attribute => value }).paginate(page: 1, per_page: 1).to_a

if list.reject { |e| e.id == record.id }.any?
error_options = options.except(:case_sensitive, :scope, :conditions)
error_options[:value] = value
record.errors.add(attribute, :taken, error_options)
end
end
end


def self.validates_uniqueness_of(*attr_names)
validates_with RemoteUniquenessValidator, _merge_attributes(attr_names)
end

#:validatable, :confirmable
devise :remote_authenticatable, :registerable, :recoverable, :rememberable, :confirmable,
:trackable, :lockable, :timeoutable, :password_expirable,
:trackable, :validatable, :lockable, :timeoutable, :password_expirable,
:omniauthable, omniauth_providers: Devise.omniauth_providers

def initialize(params = {})
attributes
super
end

def validates_uniqueness_of
#TODO implement
end


#================================
# Validation
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module MnoEnterprise::Concerns::Controllers::Auth::RegistrationsController
extend ActiveSupport::Concern

#==================================================================
# Included methods
#==================================================================
Expand All @@ -9,22 +9,22 @@ module MnoEnterprise::Concerns::Controllers::Auth::RegistrationsController
included do
before_filter :configure_sign_up_params, only: [:create]
# before_filter :configure_account_update_params, only: [:update]

protected
def configure_sign_up_params
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(
:email,
:password,
:password_confirmation,
:name,
:email,
:password,
:password_confirmation,
:name,
:surname,
:company,
:phone,
:phone_country_code
)}
)}
end
end

#==================================================================
# Class methods
#==================================================================
Expand All @@ -33,7 +33,7 @@ module ClassMethods
# 'some text'
# end
end

#==================================================================
# Instance methods
#==================================================================
Expand All @@ -46,9 +46,9 @@ module ClassMethods
def create
build_resource(sign_up_params)
resource.password ||= Devise.friendly_token

resource_saved = resource.save

if resource_saved
if resource.active_for_authentication?
set_flash_message :notice, :signed_up if is_flashing_format?
Expand Down Expand Up @@ -97,7 +97,7 @@ def create
# end

protected

# You can put the params you want to permit in the empty array.
# def configure_account_update_params
# devise_parameter_sanitizer.for(:account_update) << :attribute
Expand All @@ -112,25 +112,24 @@ def after_sign_up_path_for(resource)
# def after_inactive_sign_up_path_for(resource)
# super(resource)
# end

def sign_up_params
attrs = super
attrs.merge(orga_on_create: create_orga_on_user_creation(attrs))
end

# Check whether we should create an organization for the user
def create_orga_on_user_creation(user_attrs)
return false unless user_attrs['email']

# First check previous url to see if the user
# was trying to accept an orga
orga_invites = []
if !session[:previous_url].blank? && (r = session[:previous_url].match(/\/orga_invites\/(\d+)\?token=(\w+)/))
invite_params = { id: r.captures[0].to_i, token: r.captures[1] }
return false if MnoEnterprise::OrgInvite.where(invite_params).any?
return false if Mno::OrgaInvite.where(invite_params).any?
end

# Get remaining invites via email address
return MnoEnterprise::OrgInvite.where(user_email: user_attrs['email']).empty?
return Mno::OrgaInvite.where(user_email: user_attrs['email']).empty?
end
end
end

0 comments on commit a434d70

Please sign in to comment.