merbful_authentication 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. data/LICENSE +25 -0
  2. data/README +69 -0
  3. data/Rakefile +35 -0
  4. data/TODO +5 -0
  5. data/activerecord_generators/merbful_authentication_model/merbful_authentication_model_generator.rb +65 -0
  6. data/activerecord_generators/merbful_authentication_model/templates/authenticated_system_orm_map.rb +34 -0
  7. data/activerecord_generators/merbful_authentication_model/templates/migration.rb +20 -0
  8. data/activerecord_generators/merbful_authentication_model/templates/model.rb +63 -0
  9. data/datamapper_generators/merbful_authentication_model/merbful_authentication_model_generator.rb +60 -0
  10. data/datamapper_generators/merbful_authentication_model/templates/authenticated_system_orm_map.rb +34 -0
  11. data/datamapper_generators/merbful_authentication_model/templates/model.rb +78 -0
  12. data/lib/merbful_authentication.rb +10 -0
  13. data/lib/merbful_authentication/merbtasks.rb +6 -0
  14. data/merb_generators/authentication/USAGE +5 -0
  15. data/merb_generators/authentication/authentication_generator.rb +256 -0
  16. data/merb_generators/authentication/templates/activation.html.erb +1 -0
  17. data/merb_generators/authentication/templates/activation.text.erb +1 -0
  18. data/merb_generators/authentication/templates/authenticated_system_controller.rb +132 -0
  19. data/merb_generators/authentication/templates/authenticated_system_model.rb +97 -0
  20. data/merb_generators/authentication/templates/login.html.erb +14 -0
  21. data/merb_generators/authentication/templates/mail_controller.rb +13 -0
  22. data/merb_generators/authentication/templates/model_controller.rb +33 -0
  23. data/merb_generators/authentication/templates/new_model.html.erb +18 -0
  24. data/merb_generators/authentication/templates/session_controller.rb +33 -0
  25. data/merb_generators/authentication/templates/signup.html.erb +8 -0
  26. data/merb_generators/authentication/templates/signup.text.erb +8 -0
  27. data/rspec_generators/merbful_authentication_tests/merbful_authentication_tests_generator.rb +83 -0
  28. data/rspec_generators/merbful_authentication_tests/templates/authenticated_system_spec_helper.rb +22 -0
  29. data/rspec_generators/merbful_authentication_tests/templates/model_controller_spec.rb +78 -0
  30. data/rspec_generators/merbful_authentication_tests/templates/model_spec.rb +357 -0
  31. data/rspec_generators/merbful_authentication_tests/templates/model_spec_helper.rb +8 -0
  32. data/rspec_generators/merbful_authentication_tests/templates/session_controller_spec.rb +101 -0
  33. data/rspec_generators/merbful_authentication_tests/templates/user_mailer_spec.rb +70 -0
  34. data/test_unit_generators/merbful_authentication_tests/USAGE +5 -0
  35. data/test_unit_generators/merbful_authentication_tests/merbful_authentication_tests_generator.rb +84 -0
  36. data/test_unit_generators/merbful_authentication_tests/templates/authenticated_system_test_helper.rb +50 -0
  37. data/test_unit_generators/merbful_authentication_tests/templates/functional_test.rb +92 -0
  38. data/test_unit_generators/merbful_authentication_tests/templates/mailer_test.rb +66 -0
  39. data/test_unit_generators/merbful_authentication_tests/templates/model_functional_test.rb +92 -0
  40. data/test_unit_generators/merbful_authentication_tests/templates/model_test_helper.rb +8 -0
  41. data/test_unit_generators/merbful_authentication_tests/templates/unit_test.rb +142 -0
  42. metadata +114 -0
@@ -0,0 +1,8 @@
1
+ module <%= class_name %>SpecHelper
2
+ def valid_<%= singular_name %>_hash
3
+ { :login => "daniel",
4
+ :email => "daniel@example.com",
5
+ :password => "sekret",
6
+ :password_confirmation => "sekret"}
7
+ end
8
+ end
@@ -0,0 +1,101 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')
2
+ require File.join( File.dirname(__FILE__), "..", "<%= singular_name %>_spec_helper")
3
+ require File.join( File.dirname(__FILE__), "..", "authenticated_system_spec_helper")
4
+ require 'cgi'
5
+
6
+ describe "<%= controller_class_name %> Controller", "index action" do
7
+ include <%= class_name %>SpecHelper
8
+
9
+ before(:each) do
10
+ <%= class_name %>.clear_database_table
11
+ @quentin = <%= class_name %>.create(valid_<%= singular_name %>_hash.with(:login => "quentin", :password => "test", :password_confirmation => "test"))
12
+ @controller = <%= controller_class_name %>.build(fake_request)
13
+ <% if include_activation -%>
14
+ @quentin.activate
15
+ <% end -%>
16
+ end
17
+
18
+ it "should have a route to <%= controller_class_name %>#new from '/login'" do
19
+ with_route("/login") do |params|
20
+ params[:controller].should == "<%= controller_class_name %>"
21
+ params[:action].should == "create"
22
+ end
23
+ end
24
+
25
+ it "should route to <%= controller_class_name %>#create from '/login' via post" do
26
+ with_route("/login", "POST") do |params|
27
+ params[:controller].should == "<%= controller_class_name %>"
28
+ params[:action].should == "create"
29
+ end
30
+ end
31
+
32
+ it "should have a named route :login" do
33
+ controller.url(:login).should == "/login"
34
+ end
35
+
36
+ it "should have route to <%= controller_class_name %>#destroy from '/logout' via delete" do
37
+ with_route("/logout", "DELETE") do |params|
38
+ params[:controller].should == "<%= controller_class_name %>"
39
+ params[:action].should == "destroy"
40
+ end
41
+ end
42
+
43
+ it "should route to <%= controller_class_name %>#destroy from '/logout' via get" do
44
+ with_route("/logout", "GET") do |params|
45
+ params[:controller].should == "<%= controller_class_name %>"
46
+ params[:action].should == "destroy"
47
+ end
48
+ end
49
+
50
+ it 'logins and redirects' do
51
+ post "/login", :login => 'quentin', :password => 'test'
52
+ session[:<%= singular_name %>].should_not be_nil
53
+ session[:<%= singular_name %>].should == @quentin.id
54
+ controller.should redirect_to("/")
55
+ end
56
+
57
+ it 'fails login and does not redirect' do
58
+ post "/login", :login => 'quentin', :password => 'bad password'
59
+ session[:<%= singular_name %>].should be_nil
60
+ controller.template.should match(/^new\./)
61
+ controller.should be_success
62
+ end
63
+
64
+ it 'logs out' do
65
+ get("/logout"){|response| response.stub!(:current_<%= singular_name %>).and_return(@quentin) }
66
+ session[:<%= singular_name %>].should be_nil
67
+ controller.should redirect
68
+ end
69
+
70
+ it 'remembers me' do
71
+ post "/login", :login => 'quentin', :password => 'test', :remember_me => "1"
72
+ cookies["auth_token"].should_not be_nil
73
+ end
74
+
75
+ it 'does not remember me' do
76
+ post "login", :login => 'quentin', :password => 'test', :remember_me => "0"
77
+ cookies["auth_token"].should be_nil
78
+ end
79
+
80
+ it 'deletes token on logout' do
81
+ get("/logout") {|request| request.stub!(:current_<%= singular_name %>).and_return(@quentin) }
82
+ cookies["auth_token"].should == nil
83
+ end
84
+
85
+
86
+ it 'logs in with cookie' do
87
+ @quentin.remember_me
88
+ get "/login" do |request|
89
+ request.env[Merb::Const::HTTP_COOKIE] = "auth_token=#{@quentin.remember_token}"
90
+ end
91
+ controller.should be_logged_in
92
+ end
93
+
94
+ def auth_token(token)
95
+ CGI::Cookie.new('name' => 'auth_token', 'value' => token)
96
+ end
97
+
98
+ def cookie_for(<%= singular_name %>)
99
+ auth_token <%= singular_name %>.remember_token
100
+ end
101
+ end
@@ -0,0 +1,70 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')
2
+ require File.join( File.dirname(__FILE__), "..", "<%= singular_name %>_spec_helper")
3
+ require File.join( File.dirname(__FILE__), "..", "authenticated_system_spec_helper")
4
+
5
+ describe <%= class_name %>Mailer do
6
+
7
+ def deliver(action, mail_opts= {},opts = {})
8
+ <%= class_name %>Mailer.dispatch_and_deliver action, mail_opts, opts
9
+ @delivery = Merb::Mailer.deliveries.last
10
+ end
11
+
12
+ before(:each) do
13
+ @u = <%= class_name %>.new(:email => "homer@simpsons.com", :login => "homer", :activation_code => "12345")
14
+ @mailer_params = { :from => "info@mysite.com",
15
+ :to => @u.email,
16
+ :subject => "Welcome to MySite.com" }
17
+ end
18
+
19
+ after(:each) do
20
+ Merb::Mailer.deliveries.clear
21
+ end
22
+
23
+ it "should send mail to homer@simpsons.com for the signup email" do
24
+ deliver(:signup_notification, @mailer_params, :<%= singular_name %> => @u)
25
+ @delivery.assigns(:headers).should include("to: homer@simpsons.com")
26
+ end
27
+
28
+ it "should send the mail from 'info@mysite.com' for the signup email" do
29
+ deliver(:signup_notification, @mailer_params, :<%= singular_name %> => @u)
30
+ @delivery.assigns(:headers).should include("from: info@mysite.com")
31
+ end
32
+
33
+ it "should mention the <%= plural_name %> login in the text signup mail" do
34
+ deliver(:signup_notification, @mailer_params, :<%= singular_name %> => @u)
35
+ @delivery.text.should include(@u.login)
36
+ end
37
+
38
+ it "should mention the <%= plural_name %> login in the HTML signup mail" do
39
+ deliver(:signup_notification, @mailer_params, :<%= singular_name %> => @u)
40
+ @delivery.html.should include(@u.login)
41
+ end
42
+
43
+ it "should mention the activation link in the signup emails" do
44
+ deliver(:signup_notification, @mailer_params, :<%= singular_name %> => @u)
45
+ the_url = <%= class_name %>Mailer.new.url(:<%= singular_name %>_activation, :activation_code => @u.activation_code)
46
+ the_url.should_not be_nil
47
+ @delivery.text.should include( the_url )
48
+ @delivery.html.should include( the_url )
49
+ end
50
+
51
+ it "should send mail to homer@simpson.com for the activation email" do
52
+ deliver(:activation_notification, @mailer_params, :<%= singular_name %> => @u)
53
+ @delivery.assigns(:headers).should include("to: homer@simpsons.com")
54
+ end
55
+
56
+ it "should send the mail from 'info@mysite.com' for the activation email" do
57
+ deliver(:activation_notification, @mailer_params, :<%= singular_name %> => @u)
58
+ @delivery.assigns(:headers).should include("from: info@mysite.com")
59
+ end
60
+
61
+ it "should mention ther <%= plural_name %> login in the text activation mail" do
62
+ deliver(:activation_notification, @mailer_params, :<%= singular_name %> => @u)
63
+ @delivery.text.should include(@u.login)
64
+ end
65
+
66
+ it "should mention the suers login in the html activation mail" do
67
+ deliver(:activation_notification, @mailer_params, :<%= singular_name %> => @u)
68
+ @delivery.html.should include(@u.login)
69
+ end
70
+ end
@@ -0,0 +1,84 @@
1
+ class MerbfulAuthenticationTestsGenerator < RubiGen::Base
2
+
3
+ attr_reader :name,
4
+ :class_name,
5
+ :class_path,
6
+ :file_name,
7
+ :class_nesting,
8
+ :class_nesting_depth,
9
+ :plural_name,
10
+ :singular_name,
11
+ :controller_name,
12
+ :controller_class_path,
13
+ :controller_file_path,
14
+ :controller_class_nesting,
15
+ :controller_class_nesting_depth,
16
+ :controller_class_name,
17
+ :controller_singular_name,
18
+ :controller_plural_name,
19
+ :model_controller_name,
20
+ :model_controller_class_path,
21
+ :model_controller_file_path,
22
+ :model_controller_class_nesting,
23
+ :model_controller_class_nesting_depth,
24
+ :model_controller_class_name,
25
+ :model_controller_singular_name,
26
+ :model_controller_plural_name,
27
+ :include_activation
28
+
29
+ def initialize(runtime_args, runtime_options = {})
30
+ super
31
+ usage if args.empty?
32
+ @name = args.shift
33
+ extract_options
34
+ runtime_options.each{ |k,v| self.instance_variable_set("@#{k}", v) }
35
+ end
36
+
37
+ def manifest
38
+ record do |m|
39
+ # Ensure appropriate folder(s) exists
40
+ m.directory "test"
41
+ m.directory "test/unit"
42
+ m.directory "test/functional"
43
+
44
+ m.directory "test/mailers" if include_activation
45
+ # Create stubs
46
+ m.template "model_test_helper.rb", File.join("test", "#{file_name}_test_helper.rb")
47
+ m.template "authenticated_system_test_helper.rb", File.join("test", "authenticated_system_test_helper.rb")
48
+ m.template "model_functional_test.rb", File.join("test", "functional", "#{model_controller_file_path}_test.rb")
49
+ m.template "functional_test.rb", File.join("test", "functional", "#{controller_file_path}_test.rb")
50
+ m.template "unit_test.rb", File.join("test", "unit", "#{singular_name}_test.rb")
51
+
52
+ if include_activation
53
+ m.template "mailer_test.rb", File.join("test/mailers", "#{singular_name}_mailer_test.rb")
54
+ end
55
+ end
56
+ end
57
+
58
+ protected
59
+ def banner
60
+ <<-EOS
61
+ Creates a ...
62
+
63
+ USAGE: #{$0} #{spec.name} name"
64
+ EOS
65
+ end
66
+
67
+ def add_options!(opts)
68
+ # opts.separator ''
69
+ # opts.separator 'Options:'
70
+ # For each option below, place the default
71
+ # at the top of the file next to "default_options"
72
+ # opts.on("-a", "--author=\"Your Name\"", String,
73
+ # "Some comment about this option",
74
+ # "Default: none") { |options[:author]| }
75
+ # opts.on("-v", "--version", "Show the #{File.basename($0)} version number and quit.")
76
+ end
77
+
78
+ def extract_options
79
+ # for each option, extract it into a local variable (and create an "attr_reader :author" at the top)
80
+ # Templates can access these value via the attr_reader-generated methods, but not the
81
+ # raw instance variable value.
82
+ # @author = options[:author]
83
+ end
84
+ end
@@ -0,0 +1,50 @@
1
+ class Merb::Controller
2
+ require "merb/session/memory_session"
3
+ Merb::MemorySessionContainer.setup
4
+ include ::Merb::SessionMixin
5
+ self.session_secret_key = "foo to the bar to the baz"
6
+ end
7
+
8
+ class Merb::Mailer
9
+ self.delivery_method = :test_send
10
+ end
11
+
12
+ class Hash
13
+
14
+ def with( opts )
15
+ self.merge(opts)
16
+ end
17
+
18
+ def without(*args)
19
+ self.dup.delete_if{ |k,v| args.include?(k)}
20
+ end
21
+
22
+ end
23
+
24
+ # Assert difference(s) methods were taken from RubyOnRails to support the port of restful_authentication
25
+
26
+ def assert_difference(expressions, difference = 1, message = nil, &block)
27
+ expression_evaluations = Array(expressions).collect{ |expression| lambda { eval(expression, block.send(:binding)) } }
28
+
29
+ original_values = expression_evaluations.inject([]) { |memo, expression| memo << expression.call }
30
+ yield
31
+ expression_evaluations.each_with_index do |expression, i|
32
+ assert_equal original_values[i] + difference, expression.call, message
33
+ end
34
+ end
35
+
36
+ def assert_no_difference(expressions, message = nil, &block)
37
+ assert_difference expressions, 0, message, &block
38
+ end
39
+
40
+
41
+ def assert_response(code = :success)
42
+ case code
43
+ when :success
44
+ assert_equal 200, controller.status
45
+ when :redirect
46
+ assert_equal 302, controller.status
47
+ end
48
+ end
49
+
50
+
@@ -0,0 +1,92 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+ require (File.dirname(__FILE__) / '../authenticated_system_test_helper')
3
+ require (File.dirname(__FILE__) / ".." / "<%= singular_name %>_test_helper")
4
+ include <%= class_name %>TestHelper
5
+
6
+ # Re-raise errors caught by the controller.
7
+ class <%= controller_class_name %>; def rescue_action(e) raise e end; end
8
+
9
+ class <%= controller_class_name %>ControllerTest < Test::Unit::TestCase
10
+
11
+ def setup
12
+ <%= class_name %>.clear_database_table
13
+ @controller = <%= controller_class_name %>.build(fake_request)
14
+ @request = @controller.request
15
+ @response = @controller.response
16
+
17
+ @<%= singular_name %> = <%= class_name %>.new(valid_<%= singular_name %>_hash.with(:login => 'quentin', :password => 'test', :password_confirmation => 'test'))
18
+ @<%= singular_name %>.save
19
+ <% if options[:include_activation] -%>
20
+ @<%= singular_name %>.activate
21
+ <% end -%>
22
+ end
23
+
24
+ def test_should_login_and_redirect
25
+ controller.params.merge!(:login => 'quentin', :password => 'test')
26
+ controller.dispatch(:create)
27
+ assert controller.session[:<%= singular_name %>]
28
+ assert_response :redirect
29
+ end
30
+
31
+ def test_should_fail_login_and_not_redirect
32
+ controller.params.merge!(:login => 'quentin', :password => 'bad password')
33
+ controller.dispatch(:create)
34
+ assert_nil controller.session[:<%= file_name %>]
35
+ assert_response :success
36
+ end
37
+
38
+ def test_should_logout
39
+ login_as :quentin
40
+ controller.dispatch(:destroy)
41
+ assert_nil controller.session[:<%= file_name %>]
42
+ assert_response :redirect
43
+ end
44
+
45
+ def test_should_remember_me
46
+ controller.params.merge!(:login => 'quentin', :password => 'test', :remember_me => '1')
47
+ controller.dispatch(:create)
48
+ assert_not_nil controller.cookies["auth_token"]
49
+ end
50
+
51
+ def test_should_not_remember_me
52
+ controller.params.merge!(:login => 'quentin', :password => 'test', :remember_me => '0')
53
+ controller.dispatch(:create)
54
+ assert_nil controller.cookies["auth_token"]
55
+ end
56
+
57
+ def test_should_delete_token_on_logout
58
+ login_as :quentin
59
+ controller.dispatch(:destroy)
60
+ assert_nil controller.cookies["auth_token"]
61
+ end
62
+
63
+ def test_should_login_with_cookie
64
+ @user.remember_me
65
+ controller.cookies["auth_token"] = @<%= singular_name %>.remember_token
66
+ controller.dispatch(:new)
67
+ assert controller.send(:logged_in?)
68
+ end
69
+
70
+ def test_should_fail_expired_cookie_login
71
+ @<%= singular_name %>.remember_me
72
+ @<%= singular_name %>.remember_token_expires_at = (Time.now - (5 * 60))
73
+ @<%= singular_name %>.save
74
+ controller.cookies["auth_token"] = @<%= singular_name %>.remember_token
75
+ controller.dispatch(:new)
76
+ assert !(controller.send(:logged_in?))
77
+ end
78
+
79
+ def test_should_fail_cookie_login
80
+ @<%= singular_name %>.remember_me
81
+ controller.cookies["auth_token"] = 'invalid_auth_token'
82
+ controller.dispatch(:new)
83
+ assert !controller.send(:logged_in?)
84
+ end
85
+
86
+ protected
87
+
88
+ def login_as(login = :quentin)
89
+ <%= singular_name %> = <%= class_name %>.find_with_conditions(:login => login.to_s)
90
+ @controller.session[:<%= singular_name %>] = <%= singular_name %> ? <%= singular_name %>.id : nil
91
+ end
92
+ end
@@ -0,0 +1,66 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+ require (File.dirname(__FILE__) / '../authenticated_system_test_helper')
3
+ require (File.dirname(__FILE__) / ".." / "<%= singular_name %>_test_helper")
4
+ include <%= class_name %>TestHelper
5
+
6
+ class <%= class_name %>MailerTest < Test::Unit::TestCase
7
+
8
+ def setup
9
+ @<%= singular_name %> = <%= class_name %>.new(:email => "homer@simpsons.com", :login => "homer", :activation_code => "12345")
10
+ @mailer_params = { :from => "info@mysite.com",
11
+ :to => @<%= singular_name %>.email,
12
+ :subject => "Welcome to MySite.com" }
13
+ end
14
+
15
+ def tear_down
16
+ Merb::Mailer.deliveries.clear
17
+ end
18
+
19
+ def test_signup_email_goes_to_the_right_place
20
+ deliver(:signup_notification, @mailer_params, :<%= singular_name %> => @<%= singular_name %>)
21
+ assert @delivery.assigns(:headers).any? {|v| v == "to: homer@simpsons.com"}
22
+ end
23
+
24
+ def test_signup_email_should_come_from_the_right_place
25
+ deliver(:signup_notification, @mailer_params, :<%= singular_name %> => @<%= singular_name %>)
26
+ assert @delivery.assigns(:headers).any? {|v| v == "from: info@mysite.com"}
27
+ end
28
+
29
+ def test_signup_email_should_mention_the_login_name
30
+ deliver(:signup_notification, @mailer_params, :<%= singular_name %> => @<%= singular_name %>)
31
+ assert_match %r(#{@<%= singular_name %>.login}), @delivery.text
32
+ end
33
+
34
+ def test_signup_html_email_should_mention_the_login_name
35
+ deliver(:signup_notification, @mailer_params, :<%= singular_name %> => @<%= singular_name %>)
36
+ assert_match %r(#{@<%= singular_name %>.login}), @delivery.html
37
+ end
38
+
39
+ def test_signup_email_should_mention_the_activation_link
40
+ deliver(:signup_notification, @mailer_params, :<%= singular_name %> => @<%= singular_name %>)
41
+ assert_match %r(#{@<%= singular_name %>.activation_code}), @delivery.text
42
+ assert_match %r(#{@<%= singular_name %>.activation_code}), @delivery.html
43
+ end
44
+
45
+ def test_activation_email_should_go_to_the_right_place
46
+ deliver(:activation_notification, @mailer_params, :<%= singular_name %> => @<%= singular_name %>)
47
+ assert @delivery.assigns(:headers).any?{|v| v == "to: homer@simpsons.com"}
48
+ end
49
+
50
+ def test_activation_email_should_come_from_the_right_place
51
+ deliver(:activation_notification, @mailer_params, :<%= singular_name %> => @<%= singular_name %>)
52
+ assert @delivery.assigns(:headers).any?{|v| v == "from: info@mysite.com"}
53
+ end
54
+
55
+ def test_should_mention_the_login_in_the_activation_email
56
+ deliver(:activation_notification, @mailer_params, :<%= singular_name %> => @<%= singular_name %>)
57
+ assert_match %r(#{@<%= singular_name %>.login}), @delivery.text
58
+ assert_match %r(#{@<%= singular_name %>.login}), @delivery.html
59
+ end
60
+ private
61
+ def deliver(action, mail_opts= {},opts = {})
62
+ <%= class_name %>Mailer.dispatch_and_deliver action, mail_opts, opts
63
+ @delivery = Merb::Mailer.deliveries.last
64
+ end
65
+
66
+ end