Commit 8cd9e28bfd163496129ebaf2d67e2660a431ad3f
1 parent
c2c835ac
Exists in
theme-brasil-digital-from-staging
and in
9 other branches
api: fix user registration
Showing
3 changed files
with
8 additions
and
12 deletions
Show diff stats
app/models/user.rb
lib/noosfero/api/session.rb
... | ... | @@ -45,14 +45,16 @@ module Noosfero |
45 | 45 | # test_captcha will render_api_error! and exit in case of any problem |
46 | 46 | # this return is just to improve the clarity of the execution path |
47 | 47 | return unless test_captcha(remote_ip, params, environment) |
48 | + | |
48 | 49 | name = params[:name].present? ? params[:name] : attrs[:email] |
49 | 50 | user = User.new(attrs.merge(:name => name)) |
50 | - if user.signup! | |
51 | + | |
52 | + begin | |
53 | + user.signup! | |
51 | 54 | user.generate_private_token! if user.activated? |
52 | 55 | present user, :with => Entities::UserLogin |
53 | - else | |
54 | - user.destroy | |
55 | - message = user.errors.as_json.merge(user.person.errors.as_json).to_json | |
56 | + rescue ActiveRecord::RecordInvalid | |
57 | + message = user.errors.as_json.merge((user.person.present? ? user.person.errors : {}).as_json).to_json | |
56 | 58 | render_api_error!(message, 400) |
57 | 59 | end |
58 | 60 | end | ... | ... |
test/unit/api/session_test.rb
... | ... | @@ -27,8 +27,8 @@ class SessionTest < ActiveSupport::TestCase |
27 | 27 | assert_equal 201, last_response.status |
28 | 28 | json = JSON.parse(last_response.body) |
29 | 29 | assert User['newuserapi'].activated? |
30 | - assert json['user']['activated'] | |
31 | - assert json['user']['private_token'].present? | |
30 | + assert json['activated'] | |
31 | + assert json['private_token'].present? | |
32 | 32 | end |
33 | 33 | |
34 | 34 | should 'register a user with name' do | ... | ... |