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
| @@ -102,12 +102,6 @@ class User < ActiveRecord::Base | @@ -102,12 +102,6 @@ class User < ActiveRecord::Base | ||
| 102 | end | 102 | end |
| 103 | end | 103 | end |
| 104 | 104 | ||
| 105 | - def signup | ||
| 106 | - User.transaction do | ||
| 107 | - self.person.save if self.save | ||
| 108 | - end | ||
| 109 | - end | ||
| 110 | - | ||
| 111 | has_one :person, :dependent => :destroy | 105 | has_one :person, :dependent => :destroy |
| 112 | belongs_to :environment | 106 | belongs_to :environment |
| 113 | 107 |
lib/noosfero/api/session.rb
| @@ -45,14 +45,16 @@ module Noosfero | @@ -45,14 +45,16 @@ module Noosfero | ||
| 45 | # test_captcha will render_api_error! and exit in case of any problem | 45 | # test_captcha will render_api_error! and exit in case of any problem |
| 46 | # this return is just to improve the clarity of the execution path | 46 | # this return is just to improve the clarity of the execution path |
| 47 | return unless test_captcha(remote_ip, params, environment) | 47 | return unless test_captcha(remote_ip, params, environment) |
| 48 | + | ||
| 48 | name = params[:name].present? ? params[:name] : attrs[:email] | 49 | name = params[:name].present? ? params[:name] : attrs[:email] |
| 49 | user = User.new(attrs.merge(:name => name)) | 50 | user = User.new(attrs.merge(:name => name)) |
| 50 | - if user.signup! | 51 | + |
| 52 | + begin | ||
| 53 | + user.signup! | ||
| 51 | user.generate_private_token! if user.activated? | 54 | user.generate_private_token! if user.activated? |
| 52 | present user, :with => Entities::UserLogin | 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 | render_api_error!(message, 400) | 58 | render_api_error!(message, 400) |
| 57 | end | 59 | end |
| 58 | end | 60 | end |
test/unit/api/session_test.rb
| @@ -27,8 +27,8 @@ class SessionTest < ActiveSupport::TestCase | @@ -27,8 +27,8 @@ class SessionTest < ActiveSupport::TestCase | ||
| 27 | assert_equal 201, last_response.status | 27 | assert_equal 201, last_response.status |
| 28 | json = JSON.parse(last_response.body) | 28 | json = JSON.parse(last_response.body) |
| 29 | assert User['newuserapi'].activated? | 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 | end | 32 | end |
| 33 | 33 | ||
| 34 | should 'register a user with name' do | 34 | should 'register a user with name' do |