Commit 0f42462f2a4a8c521818d6b18c929e39b4227561

Authored by Evandro Junior
2 parents fa53fb3f 3215dbd8

Merge branch 'api' into production

* api:
  Register user with and without name
  Needs to be compatible with older versions
app/models/user.rb
... ... @@ -94,6 +94,12 @@ class User < ActiveRecord::Base
94 94 end
95 95 end
96 96  
  97 + def signup
  98 + User.transaction do
  99 + self.person.save if self.save
  100 + end
  101 + end
  102 +
97 103 has_one :person, :dependent => :destroy
98 104 belongs_to :environment
99 105  
... ...
lib/noosfero/api/entities.rb
... ... @@ -112,6 +112,7 @@ module Noosfero
112 112 root 'users', 'user'
113 113 expose :id
114 114 expose :login
  115 + expose :email
115 116 expose :person, :using => Profile
116 117 expose :activated?, as: :activated
117 118 expose :permissions do |user, options|
... ...
lib/noosfero/api/session.rb
... ... @@ -42,12 +42,27 @@ module Noosfero
42 42 # this return is just to improve the clarity of the execution path
43 43 return unless test_captcha(remote_ip, params, environment)
44 44 user = User.new(attrs)
45   - if user.save
46   - user.generate_private_token! if user.activated?
47   - present user, :with => Entities::UserLogin
  45 + if params[:name].present?
  46 + #adds user and person
  47 + person = Person.new(name: params[:name], identifier: user.login)
  48 + user.person = person
  49 + if user.signup
  50 + user.generate_private_token! if user.activated?
  51 + present user, :with => Entities::UserLogin
  52 + else
  53 + user.destroy
  54 + message = user.errors.as_json.merge(person.errors.as_json).to_json
  55 + render_api_error!(message, 400)
  56 + end
48 57 else
49   - message = user.errors.to_json
50   - render_api_error!(message, 400)
  58 + #adds user only
  59 + if user.save
  60 + user.generate_private_token! if user.activated?
  61 + present user, :with => Entities::UserLogin
  62 + else
  63 + message = user.errors.to_json
  64 + render_api_error!(message, 400)
  65 + end
51 66 end
52 67 end
53 68  
... ...
test/unit/api/session_test.rb
... ... @@ -30,6 +30,17 @@ class SessionTest < ActiveSupport::TestCase
30 30 assert json['private_token'].present?
31 31 end
32 32  
  33 + should 'register a user with name' do
  34 + Environment.default.enable('skip_new_user_email_confirmation')
  35 + params = {:login => "newuserapi", :password => "newuserapi", :password_confirmation => "newuserapi", :email => "newuserapi@email.com", :name => "Little John" }
  36 + post "/api/v1/register?#{params.to_query}"
  37 + assert_equal 201, last_response.status
  38 + json = JSON.parse(last_response.body)
  39 + ap json
  40 + assert json['activated']
  41 + assert json['private_token'].present?
  42 + end
  43 +
33 44 should 'register an inactive user' do
34 45 params = {:login => "newuserapi", :password => "newuserapi", :password_confirmation => "newuserapi", :email => "newuserapi@email.com" }
35 46 post "/api/v1/register?#{params.to_query}"
... ...