Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(registration and update): Ensure UID is updated alongside Email, and case-sensitivity is honored #71

Merged
merged 3 commits into from
Nov 15, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ class RegistrationsController < DeviseTokenAuth::ApplicationController

def create
@resource = resource_class.new(sign_up_params)
@resource.uid = sign_up_params[:email]
@resource.provider = "email"

# honor devise configuration for case_insensitive_keys
if resource_class.case_insensitive_keys.include?(:email)
@resource.email = sign_up_params[:email].downcase
else
@resource.email = sign_up_params[:email]
end

# success redirect url is required
unless params[:confirm_success_url]
return render json: {
Expand Down Expand Up @@ -70,6 +76,7 @@ def create

def update
if @resource

if @resource.update_attributes(account_update_params)
render json: {
status: 'success',
Expand Down
8 changes: 8 additions & 0 deletions app/models/devise_token_auth/concerns/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ module DeviseTokenAuth::Concerns::User
after_save :set_empty_token_hash
after_initialize :set_empty_token_hash

# keep uid in sync with email
before_save :sync_uid

# get rid of dead tokens
before_save :destroy_expired_tokens

Expand Down Expand Up @@ -212,10 +215,15 @@ def set_empty_token_hash
self.tokens ||= {} if has_attribute?(:tokens)
end

def sync_uid
self.uid = email if provider == 'email'
end

def destroy_expired_tokens
self.tokens.delete_if{|cid,v|
expiry = v[:expiry] || v["expiry"]
DateTime.strptime(expiry.to_s, '%s') < Time.now
}
end

end
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,36 @@ class DeviseTokenAuth::RegistrationsControllerTest < ActionDispatch::Integration
end
end

describe "case-insensitive email" do

before do
@resource_class = User
@request_params = {
email: "AlternatingCase@example.com",
password: "secret123",
password_confirmation: "secret123",
confirm_success_url: Faker::Internet.url
}
end

test "success should downcase uid if configured" do
@resource_class.case_insensitive_keys = [:email]
post '/auth', @request_params
assert_equal 200, response.status
@data = JSON.parse(response.body)
assert_equal "alternatingcase@example.com", @data['data']['uid']
end

test "request should not downcase uid if not configured" do
@resource_class.case_insensitive_keys = []
post '/auth', @request_params
assert_equal 200, response.status
@data = JSON.parse(response.body)
assert_equal "AlternatingCase@example.com", @data['data']['uid']
end

end

describe "Adding extra params" do
before do
@redirect_url = Faker::Internet.url
Expand Down Expand Up @@ -205,22 +235,38 @@ class DeviseTokenAuth::RegistrationsControllerTest < ActionDispatch::Integration
describe "success" do
before do
# test valid update param
@resource_class = User
@new_operating_thetan = 1000000

put "/auth", {
operating_thetan: @new_operating_thetan
}, @auth_headers

@data = JSON.parse(response.body)
@existing_user.reload
@email = "AlternatingCase2@example.com"
@request_params = {
operating_thetan: @new_operating_thetan,
email: @email
}
end

test "Request was successful" do
put "/auth", @request_params, @auth_headers
assert_equal 200, response.status
end

test "User attribute was updated" do
test "Case sensitive attributes update" do
@resource_class.case_insensitive_keys = []
put "/auth", @request_params, @auth_headers
@data = JSON.parse(response.body)
@existing_user.reload
assert_equal @new_operating_thetan, @existing_user.operating_thetan
assert_equal @email, @existing_user.email
assert_equal @email, @existing_user.uid
end

test "Case insensitive attributes update" do
@resource_class.case_insensitive_keys = [:email]
put "/auth", @request_params, @auth_headers
@data = JSON.parse(response.body)
@existing_user.reload
assert_equal @new_operating_thetan, @existing_user.operating_thetan
assert_equal @email.downcase, @existing_user.email
assert_equal @email.downcase, @existing_user.uid
end
end

Expand Down