forked from newleaders/thincloud-authentication
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
212 additions
and
0 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
app/controllers/thincloud/authentication/passwords_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
module Thincloud::Authentication | ||
# Public: Handle password reset management | ||
class PasswordsController < ApplicationController | ||
|
||
before_filter :find_identity, only: [:edit, :update] | ||
|
||
layout Thincloud::Authentication.configuration.layout | ||
|
||
def new | ||
render | ||
end | ||
|
||
def create | ||
PasswordResetWorkflow.call(params[:email]) | ||
redirect_to login_url, | ||
notice: "Email sent with password reset instructions." | ||
end | ||
|
||
def edit | ||
render | ||
end | ||
|
||
def update | ||
if UpdateIdentityPassword.call(@identity, identity_params) | ||
login_as @identity.user | ||
redirect_to after_password_update_path | ||
else | ||
render :edit | ||
end | ||
end | ||
|
||
|
||
private | ||
|
||
def find_identity | ||
@identity = Identity.find_by_password_reset_token!(params[:id]) | ||
end | ||
|
||
def identity_params | ||
params.require(:identity).permit(:password, :password_confirmation) | ||
end | ||
|
||
end | ||
end |
35 changes: 35 additions & 0 deletions
35
app/views/thincloud/authentication/passwords/edit.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<%= form_for @identity, url: password_url(id: @identity.password_reset_token), method: :put do |f| %> | ||
<fieldset> | ||
<legend>Password Reset</legend> | ||
|
||
<div class="control-group"> | ||
<%= f.label :password, "Password", class: "control-label" %> | ||
|
||
<div class="controls"> | ||
<div class="input-prepend"> | ||
<span class="add-on"><i class="icon-envelope"></i></span> | ||
<%= f.password_field :password %> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<div class="control-group"> | ||
<%= f.label :password_confirmation, "Password Confirmation", class: "control-label" %> | ||
|
||
<div class="controls"> | ||
<div class="input-prepend"> | ||
<span class="add-on"><i class="icon-envelope"></i></span> | ||
<%= f.password_field :password_confirmation %> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<div class="control-group"> | ||
<div class="controls"> | ||
<%= button_tag type: "submit", class: "btn btn-large btn-primary" do %> | ||
<i class="icon-ok icon-white"></i> Submit | ||
<% end %> | ||
</div> | ||
</div> | ||
</fieldset> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<%= form_tag passwords_url, method: :post do %> | ||
<fieldset> | ||
<legend>Password Reset</legend> | ||
|
||
<div class="control-group"> | ||
<%= label_tag :email, "Email", class: "control-label" %> | ||
|
||
<div class="controls"> | ||
<div class="input-prepend"> | ||
<span class="add-on"><i class="icon-envelope"></i></span> | ||
<%= email_field_tag :email %> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<div class="control-group"> | ||
<div class="controls"> | ||
<%= button_tag type: "submit", class: "btn btn-large btn-primary" do %> | ||
<i class="icon-ok icon-white"></i> Submit | ||
<% end %> | ||
|
||
or | ||
|
||
<%= link_to login_url, class: "btn btn-large" do %> | ||
<i class="icon-user"></i> Login | ||
<% end %> | ||
</div> | ||
</div> | ||
|
||
</fieldset> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
test/controllers/thincloud/authentication/passwords_controller_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
require "minitest_helper" | ||
|
||
module Thincloud::Authentication | ||
describe PasswordsController do | ||
describe "GET new" do | ||
before { get :new } | ||
|
||
it { assert_response :success } | ||
it { assert_template :new } | ||
end | ||
|
||
describe "POST create" do | ||
before do | ||
PasswordResetWorkflow.expects(:call).with("foo@bar.com") | ||
post :create, email: "foo@bar.com" | ||
end | ||
|
||
it { assert_response :redirect } | ||
it { assert_redirected_to login_url } | ||
it { | ||
flash[:notice].must_equal( | ||
"Email sent with password reset instructions." | ||
) | ||
} | ||
end | ||
|
||
describe "GET edit" do | ||
describe "with an invalid id" do | ||
it "raises an exception" do | ||
-> { | ||
get :edit, id: "invalid" | ||
}.must_raise(ActiveRecord::RecordNotFound) | ||
end | ||
end | ||
|
||
describe "with a valid id" do | ||
let(:identity) { Identity.new(password_reset_token: "abc123") } | ||
|
||
before do | ||
Identity.stubs(:find_by_password_reset_token!).with("abc123").returns( | ||
identity | ||
) | ||
get :edit, id: "abc123" | ||
end | ||
|
||
it { assert_response :success } | ||
it { assert_template :edit } | ||
it { assigns[:identity].must_equal identity } | ||
end | ||
end | ||
|
||
describe "PUT update" do | ||
before do | ||
attrs = { | ||
name: "test", email: "foo@bar.com", password: "test123", | ||
password_confirmation: "test123", password_reset_token: "abc123", | ||
password_reset_sent_at: 1.hour.ago, user_id: User.create.id | ||
} | ||
@identity = Identity.create!(attrs) | ||
end | ||
|
||
describe "with invalid identity attributes" do | ||
before do | ||
put :update, id: "abc123", identity: { | ||
password: "xxx1", password_confirmation: "xxx2" | ||
} | ||
end | ||
|
||
it { assert_response :success } | ||
it { assert_template :edit } | ||
it { assigns[:identity].must_equal @identity } | ||
it { assigns[:identity].errors[:password].wont_be_empty } | ||
end | ||
|
||
describe "with valid identity attributes" do | ||
before do | ||
put :update, id: "abc123", identity: { | ||
password: "p@ssw0rd1", password_confirmation: "p@ssw0rd1" | ||
} | ||
end | ||
|
||
it { assert_response :redirect } | ||
it { assert_redirected_to "/" } | ||
end | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters