thoughtbot-clearance 0.3.3 → 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -3,14 +3,16 @@ require 'rake/testtask'
3
3
  require 'date'
4
4
 
5
5
  test_files_pattern = 'test/rails_root/test/{unit,functional,other}/**/*_test.rb'
6
- Rake::TestTask.new do |t|
7
- t.libs << 'lib'
8
- t.pattern = test_files_pattern
9
- t.verbose = false
6
+ namespace :test do
7
+ Rake::TestTask.new(:all => 'generator:tests') do |t|
8
+ t.libs << 'lib'
9
+ t.pattern = test_files_pattern
10
+ t.verbose = false
11
+ end
10
12
  end
11
13
 
12
14
  desc "Run the test suite"
13
- task :default => :test
15
+ task :default => 'test:all'
14
16
 
15
17
  spec = Gem::Specification.new do |s|
16
18
  s.name = "clearance"
@@ -23,28 +25,10 @@ spec = Gem::Specification.new do |s|
23
25
  end
24
26
 
25
27
  namespace :generator do
26
- task :templates do
27
- app_files = FileList["test/rails_root/app/{controllers,models,views}/**/*"]
28
- app_files.reject! { |file| file.include?("test/rails_root/app/views/layouts") }
29
- test_files = FileList["test/rails_root/test/{functional,unit}/**/*"]
30
- test_files += ["test/rails_root/test/factories.rb"]
31
- files = test_files + app_files
32
- templates_path = "generators/clearance/templates"
33
- system `rm -rf #{templates_path}`
34
- system `mkdir #{templates_path}`
35
- ["app", "app/controllers", "app/models", "app/views",
36
- "test", "test/functional", "test/unit"].each do |directory|
37
- system `mkdir #{templates_path}/#{directory}`
38
- end
39
- files.each do |file|
40
- template = "generators/clearance/templates/#{file.gsub("test/rails_root/", "")}"
41
- if File.directory?(file)
42
- system `rm -rf #{template}`
43
- system `mkdir #{template}`
44
- else
45
- system `rm #{template}` if File.exists?(template)
46
- system `cp #{file} #{template}`
47
- end
48
- end
28
+ desc "Run the generator on the tests"
29
+ task :tests do
30
+ system "mkdir -p test/rails_root/vendor/plugins/clearance"
31
+ system "cp -R generators test/rails_root/vendor/plugins/clearance"
32
+ system "cd test/rails_root; ./script/generate clearance"
49
33
  end
50
34
  end
@@ -12,7 +12,7 @@ class ClearanceGenerator < Rails::Generator::Base
12
12
 
13
13
  m.directory File.join("app", "models")
14
14
  ["app/models/user.rb",
15
- "app/models/user_mailer.rb"].each do |file|
15
+ "app/models/clearance_mailer.rb"].each do |file|
16
16
  m.file file, file
17
17
  end
18
18
 
@@ -34,8 +34,8 @@ class ClearanceGenerator < Rails::Generator::Base
34
34
  end
35
35
 
36
36
  m.directory File.join("app", "views", "user_mailer")
37
- ["app/views/user_mailer/change_password.html.erb",
38
- "app/views/user_mailer/confirmation.html.erb"].each do |file|
37
+ ["app/views/clearance_mailer/change_password.html.erb",
38
+ "app/views/clearance_mailer/confirmation.html.erb"].each do |file|
39
39
  m.file file, file
40
40
  end
41
41
 
@@ -55,7 +55,7 @@ class ClearanceGenerator < Rails::Generator::Base
55
55
  end
56
56
 
57
57
  m.directory File.join("test", "unit")
58
- ["test/unit/user_mailer_test.rb",
58
+ ["test/unit/clearance_mailer_test.rb",
59
59
  "test/unit/user_test.rb"].each do |file|
60
60
  m.file file, file
61
61
  end
data/lib/clearance.rb CHANGED
@@ -4,12 +4,12 @@ require 'clearance/app/controllers/passwords_controller'
4
4
  require 'clearance/app/controllers/sessions_controller'
5
5
  require 'clearance/app/controllers/users_controller'
6
6
  require 'clearance/app/models/user'
7
- require 'clearance/app/models/user_mailer'
7
+ require 'clearance/app/models/clearance_mailer'
8
8
  require 'clearance/test/functional/confirmations_controller_test'
9
9
  require 'clearance/test/functional/sessions_controller_test'
10
10
  require 'clearance/test/functional/users_controller_test'
11
11
  require 'clearance/test/functional/passwords_controller_test'
12
12
  require 'clearance/test/test_helper'
13
13
  require 'clearance/test/unit/user_test'
14
- require 'clearance/test/unit/user_mailer_test'
14
+ require 'clearance/test/unit/clearance_mailer_test'
15
15
  require 'clearance/version'
@@ -27,7 +27,7 @@ module Clearance
27
27
 
28
28
  module ProtectedInstanceMethods
29
29
  def authenticate
30
- deny_access unless self.current_user
30
+ deny_access unless logged_in?
31
31
  end
32
32
 
33
33
  def user_from_session
@@ -39,13 +39,15 @@ module Clearance
39
39
  user && user.remember_token? ? user : nil
40
40
  end
41
41
 
42
- def login(user)
43
- create_session_for(user)
44
- @current_user = user
42
+ # Level of indirection so you can easily override this method
43
+ # but also call #login .
44
+ def log_user_in(user)
45
+ login(user)
45
46
  end
46
47
 
47
- def create_session_for(user)
48
+ def login(user)
48
49
  session[:user_id] = user.id if user
50
+ @current_user = user
49
51
  end
50
52
 
51
53
  def redirect_back_or(default)
@@ -62,7 +64,7 @@ module Clearance
62
64
  end
63
65
 
64
66
  def deny_access(flash_message = nil, opts = {})
65
- opts[:redirect] ||= root_url
67
+ opts[:redirect] ||= new_session_path
66
68
  store_location
67
69
  flash[:error] = flash_message if flash_message
68
70
  redirect_to opts[:redirect]
@@ -71,6 +73,10 @@ module Clearance
71
73
  def user_model
72
74
  User
73
75
  end
76
+
77
+ def mailer_model
78
+ ClearanceMailer
79
+ end
74
80
  end
75
81
  end
76
82
  end
@@ -25,7 +25,7 @@ module Clearance
25
25
  flash.now[:warning] = 'Unknown email'
26
26
  render :action => :new
27
27
  else
28
- UserMailer.deliver_change_password user
28
+ ClearanceMailer.deliver_change_password user
29
29
  redirect_to url_after_create
30
30
  end
31
31
  end
@@ -18,8 +18,20 @@ module Clearance
18
18
 
19
19
  module InstanceMethods
20
20
  def create
21
- remember_me = params[:session][:remember_me] if params[:session]
22
- login_via_password(params[:session][:email], params[:session][:password], remember_me)
21
+ @user = user_model.authenticate(params[:session][:email], params[:session][:password])
22
+ if @user.nil?
23
+ login_failure
24
+ else
25
+ if @user.confirmed?
26
+ remember_me = params[:session][:remember_me] if params[:session]
27
+ remember(@user) if remember_me == '1'
28
+ log_user_in(@user)
29
+ login_successful
30
+ else
31
+ mailer_model.deliver_confirmation(@user)
32
+ deny_access('Account not confirmed. Confirmation email sent.')
33
+ end
34
+ end
23
35
  end
24
36
 
25
37
  def destroy
@@ -31,17 +43,6 @@ module Clearance
31
43
  end
32
44
 
33
45
  module PrivateInstanceMethods
34
- def login_via_password(email, password, remember_me)
35
- user = user_model.authenticate(email, password)
36
- if login(user)
37
- create_session_for(user)
38
- remember(user) if remember_me == '1'
39
- login_successful
40
- else
41
- login_failure
42
- end
43
- end
44
-
45
46
  def login_successful
46
47
  flash[:notice] = 'Logged in successfully'
47
48
  redirect_back_or url_after_create
@@ -24,7 +24,7 @@ module Clearance
24
24
  def create
25
25
  @user = user_model.new params[:user]
26
26
  if @user.save
27
- UserMailer.deliver_confirmation @user
27
+ ClearanceMailer.deliver_confirmation @user
28
28
  flash[:notice] = "You will receive an email within the next few minutes. It contains instructions for you to confirm your account."
29
29
  redirect_to url_after_create
30
30
  else
@@ -5,22 +5,45 @@ module Clearance
5
5
 
6
6
  def self.included(base)
7
7
  base.class_eval do
8
- context "Given a user" do
9
- setup { @user = Factory :user }
8
+ should_filter_params :password
9
+
10
+ context "on GET to /sessions/new" do
11
+ setup { get :new }
12
+
13
+ should_respond_with :success
14
+ should_render_template :new
15
+ should_not_set_the_flash
16
+ should_have_form :action => "session_path",
17
+ :fields => { "session[email]" => :text,
18
+ "session[password]" => :password,
19
+ "session[remember_me]" => :checkbox }
20
+ end
10
21
 
11
- should_filter_params :password
22
+ context "Given an unconfirmed user" do
23
+ setup do
24
+ @user = Factory(:user, :confirmed => false)
25
+ end
12
26
 
13
- context "on GET to /sessions/new" do
14
- setup { get :new }
15
-
16
- should_respond_with :success
17
- should_render_template :new
18
- should_not_set_the_flash
19
- should_have_form :action => "session_path",
20
- :fields => { "session[email]" => :text,
21
- "session[password]" => :password,
22
- "session[remember_me]" => :checkbox }
27
+ context "a POST to #create with good credentials" do
28
+ setup do
29
+ ActionMailer::Base.deliveries.clear
30
+ post :create, :session => {
31
+ :email => @user.email,
32
+ :password => @user.password
33
+ }
34
+ end
35
+
36
+ should_deny_access(:flash => /confirm/i)
37
+
38
+ should "send the confirmation email" do
39
+ assert_not_nil email = ActionMailer::Base.deliveries[0]
40
+ assert_match /account confirmation/i, email.subject
41
+ end
23
42
  end
43
+ end
44
+
45
+ context "Given a confirmed user" do
46
+ setup { @user = Factory(:user, :confirmed => true) }
24
47
 
25
48
  context "a POST to #create with good credentials" do
26
49
  setup do
@@ -49,15 +49,20 @@ module Clearance
49
49
  end
50
50
 
51
51
  logged_in_user_context do
52
+ context "GET to new" do
53
+ setup { get :new }
54
+ should_redirect_to 'root_url'
55
+ end
52
56
 
53
- should_deny_access_on "get :new"
54
- should_deny_access_on "post :create, :user => {}"
55
- should_filter_params :password
57
+ context "POST to create" do
58
+ setup { post :create, :user => {} }
59
+ should_redirect_to 'root_url'
60
+ end
56
61
 
62
+ should_filter_params :password
57
63
  end
58
64
  end
59
65
  end
60
-
61
66
  end
62
67
  end
63
68
  end
@@ -23,16 +23,20 @@ module Clearance
23
23
 
24
24
  module ClassMethods
25
25
  def should_deny_access_on(command, opts = {})
26
- opts[:redirect] ||= "root_url"
27
26
 
28
27
  context "on #{command}" do
29
28
  setup { eval command }
30
- should_redirect_to opts[:redirect]
31
- if opts[:flash]
32
- should_set_the_flash_to opts[:flash]
33
- else
34
- should_not_set_the_flash
35
- end
29
+ should_deny_access(opts)
30
+ end
31
+ end
32
+
33
+ def should_deny_access(opts = {})
34
+ opts[:redirect] ||= "new_session_path"
35
+ should_redirect_to opts[:redirect]
36
+ if opts[:flash]
37
+ should_set_the_flash_to opts[:flash]
38
+ else
39
+ should_not_set_the_flash
36
40
  end
37
41
  end
38
42
 
@@ -2,12 +2,15 @@ ActionController::Routing::Routes.draw do |map|
2
2
 
3
3
  map.resources :users
4
4
  map.resource :session
5
+ map.resource :session_mailer_override
5
6
 
6
7
  map.resources :users, :has_one => :password
7
8
  map.resources :users, :has_one => :confirmation
8
9
 
9
10
  map.resources :passwords
11
+
12
+ map.resource :account
10
13
 
11
- map.root :controller => 'sessions', :action => 'new'
14
+ map.root :controller => 'users', :action => 'new'
12
15
 
13
- end
16
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: thoughtbot-clearance
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - thoughtbot, inc.
@@ -1,7 +0,0 @@
1
- class UserMailer < ActionMailer::Base
2
-
3
- default_url_options[:host] = HOST
4
-
5
- include Clearance::App::Models::UserMailer
6
-
7
- end
@@ -1,6 +0,0 @@
1
- Someone, hopefully you, has requested that we send you a link to change your password.
2
-
3
- Here's the link:
4
- <%= edit_user_password_url @user, :email => @user.email, :password => @user.crypted_password %>
5
-
6
- If you didn't request this, no need to freak out, your password hasn't been changed. You can just ignore this email.
@@ -1 +0,0 @@
1
- <%= new_user_confirmation_url :user_id => @user, :salt => @user.salt %>
@@ -1,6 +0,0 @@
1
- require File.dirname(__FILE__) + '/../test_helper'
2
-
3
- class UserMailerTest < ActionMailer::TestCase
4
- tests UserMailer
5
- include Clearance::Test::Unit::UserMailerTest
6
- end
@@ -1,33 +0,0 @@
1
- module Clearance
2
- module App
3
- module Models
4
- module UserMailer
5
-
6
- def self.included(base)
7
- base.class_eval do
8
-
9
- include InstanceMethods
10
-
11
- end
12
- end
13
-
14
- module InstanceMethods
15
- def change_password(user)
16
- from DO_NOT_REPLY
17
- recipients user.email
18
- subject "[#{PROJECT_NAME.humanize}] Change your password"
19
- body :user => user
20
- end
21
-
22
- def confirmation(user)
23
- recipients user.email
24
- from DO_NOT_REPLY
25
- subject "[#{PROJECT_NAME.humanize}] Email confirmation"
26
- body :user => user
27
- end
28
- end
29
-
30
- end
31
- end
32
- end
33
- end
@@ -1,63 +0,0 @@
1
- module Clearance
2
- module Test
3
- module Unit
4
- module UserMailerTest
5
-
6
- def self.included(base)
7
- base.class_eval do
8
- context "A change password email" do
9
- setup do
10
- @user = Factory :user
11
- @email = UserMailer.create_change_password @user
12
- end
13
-
14
- should "set its from address to DO_NOT_REPLY" do
15
- assert_equal DO_NOT_REPLY, @email.from[0]
16
- end
17
-
18
- should "contain a link to edit the user's password" do
19
- host = ActionMailer::Base.default_url_options[:host]
20
- regexp = %r{http://#{host}/users/#{@user.id}/password/edit\?email=#{@user.email.gsub("@", "%40")}&amp;password=#{@user.crypted_password}}
21
- assert_match regexp, @email.body
22
- end
23
-
24
- should "be sent to the user" do
25
- assert_equal [@user.email], @email.to
26
- end
27
-
28
- should "have a subject of '[PROJECT_NAME] Change your password'" do
29
- assert_equal @email.subject, "[#{PROJECT_NAME.humanize}] Change your password"
30
- end
31
- end
32
-
33
- context "A confirmation email" do
34
- setup do
35
- @user = Factory :user
36
- @email = UserMailer.create_confirmation @user
37
- end
38
-
39
- should 'set its recipient to the given user' do
40
- assert_equal @user.email, @email.to[0]
41
- end
42
-
43
- should 'set its subject' do
44
- assert_equal "[#{PROJECT_NAME.humanize}] Email confirmation", @email.subject
45
- end
46
-
47
- should 'set its from address to DO_NOT_REPLY' do
48
- assert_equal DO_NOT_REPLY, @email.from[0]
49
- end
50
-
51
- should "contain a link to confirm the user's account" do
52
- host = ActionMailer::Base.default_url_options[:host]
53
- regexp = %r{http://#{host}/users/#{@user.id}/confirmation/new\?salt=#{@user.salt}}
54
- assert_match regexp, @email.body
55
- end
56
- end
57
- end
58
- end
59
-
60
- end
61
- end
62
- end
63
- end
@@ -1,7 +0,0 @@
1
- class UserMailer < ActionMailer::Base
2
-
3
- default_url_options[:host] = HOST
4
-
5
- include Clearance::App::Models::UserMailer
6
-
7
- end
@@ -1,6 +0,0 @@
1
- Someone, hopefully you, has requested that we send you a link to change your password.
2
-
3
- Here's the link:
4
- <%= edit_user_password_url @user, :email => @user.email, :password => @user.crypted_password %>
5
-
6
- If you didn't request this, no need to freak out, your password hasn't been changed. You can just ignore this email.
@@ -1 +0,0 @@
1
- <%= new_user_confirmation_url :user_id => @user, :salt => @user.salt %>
@@ -1,6 +0,0 @@
1
- require File.dirname(__FILE__) + '/../test_helper'
2
-
3
- class UserMailerTest < ActionMailer::TestCase
4
- tests UserMailer
5
- include Clearance::Test::Unit::UserMailerTest
6
- end