Commit f8be4d992cf08c988fc1b43f309f11db93af8dd7
Committed by
Macartur Sousa
1 parent
12ab2e97
Exists in
web_steps_improvements
and in
7 other branches
Add method to resend activation code
Signed-off-by: Alexandre Barbosa <alexandreab@live.com> Signed-off-by: Macartur Sousa <macartur.sc@gmail.com> Signed-off-by: Marcos Ronaldo <marcos.rpj2@gmail.com>
Showing
4 changed files
with
70 additions
and
0 deletions
Show diff stats
app/models/user.rb
| ... | ... | @@ -417,6 +417,12 @@ class User < ActiveRecord::Base |
| 417 | 417 | @is_password_required = false |
| 418 | 418 | end |
| 419 | 419 | |
| 420 | + def resend_activation_code | |
| 421 | + return if self.activated? | |
| 422 | + update_attribute(:activation_code, make_activation_code) | |
| 423 | + self.deliver_activation_code | |
| 424 | + end | |
| 425 | + | |
| 420 | 426 | protected |
| 421 | 427 | |
| 422 | 428 | def normalize_email | ... | ... |
lib/noosfero/api/session.rb
| ... | ... | @@ -113,6 +113,22 @@ module Noosfero |
| 113 | 113 | end |
| 114 | 114 | end |
| 115 | 115 | |
| 116 | + # Resend activation code. | |
| 117 | + # | |
| 118 | + # Parameters: | |
| 119 | + # value (required) - Email or login | |
| 120 | + # Example Request: | |
| 121 | + # POST /resend_activation_code?value=some@mail.com | |
| 122 | + post "/resend_activation_code" do | |
| 123 | + requestors = fetch_requestors(params[:value]) | |
| 124 | + not_found! if requestors.blank? | |
| 125 | + remote_ip = (request.respond_to?(:remote_ip) && request.remote_ip) || (env && env['REMOTE_ADDR']) | |
| 126 | + requestors.each do |requestor| | |
| 127 | + requestor.user.resend_activation_code | |
| 128 | + end | |
| 129 | + present requestors.map(&:user), :with => Entities::UserLogin | |
| 130 | + end | |
| 131 | + | |
| 116 | 132 | params do |
| 117 | 133 | requires :code, type: String, desc: _("Forgot password code") |
| 118 | 134 | end | ... | ... |
test/unit/api/session_test.rb
| ... | ... | @@ -192,4 +192,30 @@ class SessionTest < ActiveSupport::TestCase |
| 192 | 192 | assert !json['user']['private_token'].present? |
| 193 | 193 | end |
| 194 | 194 | |
| 195 | + should 'resend activation code for an inactive user' do | |
| 196 | + user = create_user | |
| 197 | + params = {:value => user.login} | |
| 198 | + Delayed::Job.destroy_all | |
| 199 | + assert_difference 'ActionMailer::Base.deliveries.size' do | |
| 200 | + post "/api/v1/resend_activation_code?#{params.to_query}" | |
| 201 | + process_delayed_job_queue | |
| 202 | + end | |
| 203 | + json = JSON.parse(last_response.body) | |
| 204 | + refute json['users'].first['private_token'] | |
| 205 | + assert_equal user.email, ActionMailer::Base.deliveries.last['to'].to_s | |
| 206 | + end | |
| 207 | + | |
| 208 | + should 'not resend activation code for an active user' do | |
| 209 | + user = create_user | |
| 210 | + params = {:value => user.login} | |
| 211 | + user.activate | |
| 212 | + Delayed::Job.destroy_all | |
| 213 | + assert_no_difference 'ActionMailer::Base.deliveries.size' do | |
| 214 | + post "/api/v1/resend_activation_code?#{params.to_query}" | |
| 215 | + process_delayed_job_queue | |
| 216 | + end | |
| 217 | + json = JSON.parse(last_response.body) | |
| 218 | + assert json['users'].first['private_token'] | |
| 219 | + end | |
| 220 | + | |
| 195 | 221 | end | ... | ... |
test/unit/user_test.rb
| ... | ... | @@ -737,6 +737,28 @@ class UserTest < ActiveSupport::TestCase |
| 737 | 737 | assert user.private_token, 'token' |
| 738 | 738 | end |
| 739 | 739 | |
| 740 | + should 'deliver e-mail with activation code when resend was requested and user was not activated' do | |
| 741 | + user = new_user :email => 'pending@activation.com' | |
| 742 | + activation_code = user.activation_code | |
| 743 | + Delayed::Job.destroy_all | |
| 744 | + assert_difference 'ActionMailer::Base.deliveries.size', 1 do | |
| 745 | + user.resend_activation_code | |
| 746 | + process_delayed_job_queue | |
| 747 | + end | |
| 748 | + assert_not_equal activation_code, user.reload.activation_code | |
| 749 | + assert_equal 'pending@activation.com', ActionMailer::Base.deliveries.last['to'].to_s | |
| 750 | + end | |
| 751 | + | |
| 752 | + should 'not deliver e-mail with activation code when resend was requested and user was activated' do | |
| 753 | + user = new_user :email => 'pending@activation.com' | |
| 754 | + user.activate | |
| 755 | + Delayed::Job.destroy_all | |
| 756 | + assert_no_difference 'ActionMailer::Base.deliveries.size' do | |
| 757 | + user.resend_activation_code | |
| 758 | + process_delayed_job_queue | |
| 759 | + end | |
| 760 | + end | |
| 761 | + | |
| 740 | 762 | protected |
| 741 | 763 | def new_user(options = {}) |
| 742 | 764 | user = User.new({ :login => 'quire', :email => 'quire@example.com', :password => 'quire', :password_confirmation => 'quire' }.merge(options)) | ... | ... |