flexi_generators 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 (137) hide show
  1. data/README.textile +71 -0
  2. data/Rakefile +38 -0
  3. data/VERSION +1 -0
  4. data/flexi_generators.gemspec +176 -0
  5. data/flexi_template.rb +28 -0
  6. data/generators/flexi_authentication/USAGE +50 -0
  7. data/generators/flexi_authentication/flexi_authentication_generator.rb +127 -0
  8. data/generators/flexi_authentication/lib/insert_commands.rb +74 -0
  9. data/generators/flexi_authentication/templates/admin_users_controller.rb +60 -0
  10. data/generators/flexi_authentication/templates/authentication.rb +61 -0
  11. data/generators/flexi_authentication/templates/authlogic_session.rb +2 -0
  12. data/generators/flexi_authentication/templates/fixtures.yml +24 -0
  13. data/generators/flexi_authentication/templates/migration.rb +30 -0
  14. data/generators/flexi_authentication/templates/sessions_controller.rb +52 -0
  15. data/generators/flexi_authentication/templates/sessions_helper.rb +2 -0
  16. data/generators/flexi_authentication/templates/tests/rspec/sessions_controller.rb +39 -0
  17. data/generators/flexi_authentication/templates/tests/rspec/user.rb +83 -0
  18. data/generators/flexi_authentication/templates/tests/rspec/users_controller.rb +26 -0
  19. data/generators/flexi_authentication/templates/tests/shoulda/sessions_controller.rb +40 -0
  20. data/generators/flexi_authentication/templates/tests/shoulda/user.rb +85 -0
  21. data/generators/flexi_authentication/templates/tests/shoulda/users_controller.rb +27 -0
  22. data/generators/flexi_authentication/templates/tests/testunit/sessions_controller.rb +36 -0
  23. data/generators/flexi_authentication/templates/tests/testunit/user.rb +88 -0
  24. data/generators/flexi_authentication/templates/tests/testunit/users_controller.rb +23 -0
  25. data/generators/flexi_authentication/templates/user.rb +43 -0
  26. data/generators/flexi_authentication/templates/users_helper.rb +2 -0
  27. data/generators/flexi_authentication/templates/views/erb/admin_user_edit.html.erb +23 -0
  28. data/generators/flexi_authentication/templates/views/erb/admin_user_index.html.erb +33 -0
  29. data/generators/flexi_authentication/templates/views/erb/admin_user_new.html.erb +23 -0
  30. data/generators/flexi_authentication/templates/views/erb/login.html.erb +38 -0
  31. data/generators/flexi_layout/USAGE +18 -0
  32. data/generators/flexi_layout/flexi_layout_generator.rb +51 -0
  33. data/generators/flexi_layout/lib/insert_commands.rb +28 -0
  34. data/generators/flexi_layout/templates/add.png +0 -0
  35. data/generators/flexi_layout/templates/admin_home_controller.rb +6 -0
  36. data/generators/flexi_layout/templates/admin_home_index.html.erb +16 -0
  37. data/generators/flexi_layout/templates/bg.png +0 -0
  38. data/generators/flexi_layout/templates/custom_field_error.rb +12 -0
  39. data/generators/flexi_layout/templates/helper.rb +19 -0
  40. data/generators/flexi_layout/templates/home_controller.rb +9 -0
  41. data/generators/flexi_layout/templates/home_helper.rb +2 -0
  42. data/generators/flexi_layout/templates/home_index.html.erb +17 -0
  43. data/generators/flexi_layout/templates/inflector_portuguese.rb +62 -0
  44. data/generators/flexi_layout/templates/layout.html.erb +47 -0
  45. data/generators/flexi_layout/templates/layout_login.html.erb +27 -0
  46. data/generators/flexi_layout/templates/layout_site.html.erb +22 -0
  47. data/generators/flexi_layout/templates/logo.png +0 -0
  48. data/generators/flexi_layout/templates/pt-BR.yml +143 -0
  49. data/generators/flexi_layout/templates/stylesheet.css +132 -0
  50. data/generators/flexi_layout/templates/stylesheet_login.css +73 -0
  51. data/generators/flexi_layout/templates/stylesheet_site.css +63 -0
  52. data/generators/flexi_layout/templates/users.png +0 -0
  53. data/generators/flexi_resource_scaffold/USAGE +29 -0
  54. data/generators/flexi_resource_scaffold/flexi_resource_scaffold_generator.rb +179 -0
  55. data/generators/flexi_resource_scaffold/templates/controller.rb +29 -0
  56. data/generators/flexi_resource_scaffold/templates/fixtures.yml +10 -0
  57. data/generators/flexi_resource_scaffold/templates/functional_test.rb +57 -0
  58. data/generators/flexi_resource_scaffold/templates/helper.rb +2 -0
  59. data/generators/flexi_resource_scaffold/templates/migration.rb +15 -0
  60. data/generators/flexi_resource_scaffold/templates/model.rb +2 -0
  61. data/generators/flexi_resource_scaffold/templates/old_migration.rb +13 -0
  62. data/generators/flexi_resource_scaffold/templates/rspec/functional_spec.rb +255 -0
  63. data/generators/flexi_resource_scaffold/templates/rspec/helper_spec.rb +11 -0
  64. data/generators/flexi_resource_scaffold/templates/rspec/routing_spec.rb +61 -0
  65. data/generators/flexi_resource_scaffold/templates/rspec/unit_spec.rb +11 -0
  66. data/generators/flexi_resource_scaffold/templates/rspec/views/edit_spec.rb +28 -0
  67. data/generators/flexi_resource_scaffold/templates/rspec/views/index_spec.rb +26 -0
  68. data/generators/flexi_resource_scaffold/templates/rspec/views/new_spec.rb +30 -0
  69. data/generators/flexi_resource_scaffold/templates/rspec/views/show_spec.rb +25 -0
  70. data/generators/flexi_resource_scaffold/templates/shoulda_functional_test.rb +19 -0
  71. data/generators/flexi_resource_scaffold/templates/unit_test.rb +7 -0
  72. data/generators/flexi_resource_scaffold/templates/view__form.erb +6 -0
  73. data/generators/flexi_resource_scaffold/templates/view__form.haml +5 -0
  74. data/generators/flexi_resource_scaffold/templates/view_edit.erb +8 -0
  75. data/generators/flexi_resource_scaffold/templates/view_edit.haml +11 -0
  76. data/generators/flexi_resource_scaffold/templates/view_index.erb +27 -0
  77. data/generators/flexi_resource_scaffold/templates/view_index.haml +19 -0
  78. data/generators/flexi_resource_scaffold/templates/view_new.erb +9 -0
  79. data/generators/flexi_resource_scaffold/templates/view_new.haml +9 -0
  80. data/generators/flexi_resource_scaffold/templates/view_show.erb +10 -0
  81. data/generators/flexi_resource_scaffold/templates/view_show.haml +9 -0
  82. data/generators/flexi_scaffold/USAGE +51 -0
  83. data/generators/flexi_scaffold/flexi_scaffold_generator.rb +232 -0
  84. data/generators/flexi_scaffold/templates/actions/create.rb +9 -0
  85. data/generators/flexi_scaffold/templates/actions/destroy.rb +6 -0
  86. data/generators/flexi_scaffold/templates/actions/edit.rb +3 -0
  87. data/generators/flexi_scaffold/templates/actions/index.rb +4 -0
  88. data/generators/flexi_scaffold/templates/actions/new.rb +3 -0
  89. data/generators/flexi_scaffold/templates/actions/show.rb +3 -0
  90. data/generators/flexi_scaffold/templates/actions/update.rb +9 -0
  91. data/generators/flexi_scaffold/templates/controller.rb +5 -0
  92. data/generators/flexi_scaffold/templates/fixtures.yml +9 -0
  93. data/generators/flexi_scaffold/templates/helper.rb +2 -0
  94. data/generators/flexi_scaffold/templates/migration.rb +16 -0
  95. data/generators/flexi_scaffold/templates/model.rb +3 -0
  96. data/generators/flexi_scaffold/templates/tests/rspec/actions/create.rb +11 -0
  97. data/generators/flexi_scaffold/templates/tests/rspec/actions/destroy.rb +6 -0
  98. data/generators/flexi_scaffold/templates/tests/rspec/actions/edit.rb +4 -0
  99. data/generators/flexi_scaffold/templates/tests/rspec/actions/index.rb +4 -0
  100. data/generators/flexi_scaffold/templates/tests/rspec/actions/new.rb +4 -0
  101. data/generators/flexi_scaffold/templates/tests/rspec/actions/show.rb +4 -0
  102. data/generators/flexi_scaffold/templates/tests/rspec/actions/update.rb +11 -0
  103. data/generators/flexi_scaffold/templates/tests/rspec/controller.rb +8 -0
  104. data/generators/flexi_scaffold/templates/tests/rspec/model.rb +7 -0
  105. data/generators/flexi_scaffold/templates/tests/shoulda/actions/create.rb +13 -0
  106. data/generators/flexi_scaffold/templates/tests/shoulda/actions/destroy.rb +8 -0
  107. data/generators/flexi_scaffold/templates/tests/shoulda/actions/edit.rb +6 -0
  108. data/generators/flexi_scaffold/templates/tests/shoulda/actions/index.rb +6 -0
  109. data/generators/flexi_scaffold/templates/tests/shoulda/actions/new.rb +6 -0
  110. data/generators/flexi_scaffold/templates/tests/shoulda/actions/show.rb +6 -0
  111. data/generators/flexi_scaffold/templates/tests/shoulda/actions/update.rb +13 -0
  112. data/generators/flexi_scaffold/templates/tests/shoulda/controller.rb +5 -0
  113. data/generators/flexi_scaffold/templates/tests/shoulda/model.rb +7 -0
  114. data/generators/flexi_scaffold/templates/tests/testunit/actions/create.rb +11 -0
  115. data/generators/flexi_scaffold/templates/tests/testunit/actions/destroy.rb +6 -0
  116. data/generators/flexi_scaffold/templates/tests/testunit/actions/edit.rb +4 -0
  117. data/generators/flexi_scaffold/templates/tests/testunit/actions/index.rb +4 -0
  118. data/generators/flexi_scaffold/templates/tests/testunit/actions/new.rb +4 -0
  119. data/generators/flexi_scaffold/templates/tests/testunit/actions/show.rb +4 -0
  120. data/generators/flexi_scaffold/templates/tests/testunit/actions/update.rb +11 -0
  121. data/generators/flexi_scaffold/templates/tests/testunit/controller.rb +5 -0
  122. data/generators/flexi_scaffold/templates/tests/testunit/model.rb +7 -0
  123. data/generators/flexi_scaffold/templates/views/erb/_form.html.erb +6 -0
  124. data/generators/flexi_scaffold/templates/views/erb/edit.html.erb +8 -0
  125. data/generators/flexi_scaffold/templates/views/erb/index.html.erb +41 -0
  126. data/generators/flexi_scaffold/templates/views/erb/new.html.erb +10 -0
  127. data/generators/flexi_scaffold/templates/views/erb/show.html.erb +19 -0
  128. data/generators/flexi_scaffold/templates/views/haml/_form.html.haml +10 -0
  129. data/generators/flexi_scaffold/templates/views/haml/edit.html.haml +14 -0
  130. data/generators/flexi_scaffold/templates/views/haml/index.html.haml +25 -0
  131. data/generators/flexi_scaffold/templates/views/haml/new.html.haml +7 -0
  132. data/generators/flexi_scaffold/templates/views/haml/show.html.haml +20 -0
  133. data/init.rb +2 -0
  134. data/lib/flexi_generators.rb +4 -0
  135. data/test/flexi_generators_test.rb +8 -0
  136. data/test/test_helper.rb +3 -0
  137. metadata +203 -0
@@ -0,0 +1,61 @@
1
+ # This module is included in your application controller which makes
2
+ # several methods available to all controllers and views. Here's a
3
+ # common example you might add to your application layout file.
4
+ #
5
+ # <%% if logged_in? %>
6
+ # Welcome <%%=h current_<%= user_singular_name %>.username %>! Not you?
7
+ # <%%= link_to "Log out", logout_path %>
8
+ # <%% else %>
9
+ # <%%= link_to "Sign up", signup_path %> or
10
+ # <%%= link_to "log in", login_path %>.
11
+ # <%% end %>
12
+ #
13
+ # You can also restrict unregistered users from accessing a controller using
14
+ # a before filter. For example.
15
+ #
16
+ # before_filter :login_required, :except => [:index, :show]
17
+ module Authentication
18
+ def self.included(controller)
19
+ controller.send :helper_method, :current_<%= user_singular_name %>, :logged_in?, :redirect_to_target_or_default
20
+ controller.filter_parameter_logging :password
21
+ end
22
+
23
+ <%- if options[:authlogic] -%>
24
+ def current_<%= session_singular_name %>
25
+ return @current_<%= session_singular_name %> if defined?(@current_<%= session_singular_name %>)
26
+ @current_<%= session_singular_name %> = <%= session_class_name %>.find
27
+ end
28
+
29
+ def current_<%= user_singular_name %>
30
+ return @current_<%= user_singular_name %> if defined?(@current_<%= user_singular_name %>)
31
+ @current_<%= user_singular_name %> = current_<%= session_singular_name %> && current_<%= session_singular_name %>.record
32
+ end
33
+ <%- else -%>
34
+ def current_<%= user_singular_name %>
35
+ @current_<%= user_singular_name %> ||= <%= user_class_name %>.find(session[:<%= user_singular_name %>_id]) if session[:<%= user_singular_name %>_id]
36
+ end
37
+ <%- end -%>
38
+
39
+ def logged_in?
40
+ current_<%= user_singular_name %>
41
+ end
42
+
43
+ def login_required
44
+ unless logged_in?
45
+ flash[:error] = "É necessária a autenticação para ter acesso a esta página."
46
+ store_target_location
47
+ redirect_to login_url
48
+ end
49
+ end
50
+
51
+ def redirect_to_target_or_default(default)
52
+ redirect_to(session[:return_to] || default)
53
+ session[:return_to] = nil
54
+ end
55
+
56
+ private
57
+
58
+ def store_target_location
59
+ session[:return_to] = request.request_uri
60
+ end
61
+ end
@@ -0,0 +1,2 @@
1
+ class <%= session_class_name %> < Authlogic::Session::Base
2
+ end
@@ -0,0 +1,24 @@
1
+ # password: "secret"
2
+ foo:
3
+ username: foo
4
+ email: foo@example.com
5
+ <%- if options[:authlogic] -%>
6
+ persistence_token: d5ddba13ed4408ea2b0a12ab18ed2d2eda086279736bdc121ca726a11f1e4b99217d9c534c2cc4ebb22729349c8c5fdbe1529e1f2c3c5859c62ef4dd9feea25c
7
+ crypted_password: 3d16c326648cccafe3d4b4cb024475c381dda92f430dfedf6f933e1f61203bacb6bae2437849bdb43b06be335e23790e4aa03902b3c28c3bbbbe27d501e521f3
8
+ password_salt: n6z_wtpWoIsHgQb5IcFd
9
+ <%- else -%>
10
+ password_hash: 3488f5f7efecab14b91eb96169e5e1ee518a569f
11
+ password_salt: bef65e058905c379436d80d1a32e7374b139e7b0
12
+ <%- end -%>
13
+
14
+ bar:
15
+ username: bar
16
+ email: bar@example.com
17
+ <%- if options[:authlogic] -%>
18
+ persistence_token: 19e074bd7cb506ab3e7e53e41f24f0ab3221c8cb68111f4c1aa43965114ad734233979a50a9463537487cdca18c279ac91c4bc83693d589625d446493322394c
19
+ crypted_password: 3bc9f4113ca645a186765df3d31a9352d0067bf2304ba0cdd6b08a7f3d58c6668ab1762fa3e76aef466ea2ff188399d8e6c40244fa59312bb4112292dac9f7f0
20
+ password_salt: UiAh9ejabnKRxqsiK0xO
21
+ <%- else -%>
22
+ password_hash: 3488f5f7efecab14b91eb96169e5e1ee518a569f
23
+ password_salt: bef65e058905c379436d80d1a32e7374b139e7b0
24
+ <%- end -%>
@@ -0,0 +1,30 @@
1
+ class Create<%= user_plural_class_name %> < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :<%= user_plural_name %> do |t|
4
+ t.string :username
5
+ t.string :email
6
+ <%- if options[:authlogic] -%>
7
+ t.string :persistence_token
8
+ t.string :crypted_password
9
+ <%- else -%>
10
+ t.string :password_hash
11
+ <%- end -%>
12
+ t.string :password_salt
13
+ t.timestamps
14
+ end
15
+
16
+ <%- if options[:authlogic] -%>
17
+ # ...
18
+ <%- else -%>
19
+ execute "INSERT INTO <%= user_plural_name %> (username,email,password_hash,password_salt) VALUES ('admin','admin@admin.com','3dad6da7754a5de4c3d46fc3901adf1a1ae9ce18','4207a4c0006e0b01da91693066d91a5e34fa7fb9')"
20
+ puts ""
21
+ puts "Username: admin"
22
+ puts "Password: admin123"
23
+ puts ""
24
+ <%- end -%>
25
+ end
26
+
27
+ def self.down
28
+ drop_table :<%= user_plural_name %>
29
+ end
30
+ end
@@ -0,0 +1,52 @@
1
+ class <%= session_plural_class_name %>Controller < ApplicationController
2
+ <%- if options[:authlogic] -%>
3
+ skip_before_filter :login_required
4
+
5
+ layout 'login'
6
+
7
+ def new
8
+ @<%= session_singular_name %> = <%= session_class_name %>.new
9
+ end
10
+
11
+ def create
12
+ @<%= session_singular_name %> = <%= session_class_name %>.new(params[:<%= session_singular_name %>])
13
+ if @<%= session_singular_name %>.save
14
+ flash[:notice] = "Bem-vindo!"
15
+ redirect_to_target_or_default(root_url)
16
+ else
17
+ flash.now[:error] = "Usuário/E-mail ou senha inválidos."
18
+ render :action => 'new'
19
+ end
20
+ end
21
+
22
+ def destroy
23
+ @<%= session_singular_name %> = <%= session_class_name %>.find
24
+ @<%= session_singular_name %>.destroy
25
+ redirect_to root_url
26
+ end
27
+ <%- else -%>
28
+ skip_before_filter :login_required
29
+
30
+ layout 'login'
31
+
32
+ def new
33
+ end
34
+
35
+ def create
36
+ <%= user_singular_name %> = <%= user_class_name %>.authenticate(params[:login], params[:password])
37
+ if <%= user_singular_name %>
38
+ session[:<%= user_singular_name %>_id] = <%= user_singular_name %>.id
39
+ flash[:notice] = "Bem-vindo!"
40
+ redirect_to admin_home_url
41
+ else
42
+ flash.now[:error] = "Usuário/E-mail ou senha inválidos."
43
+ render :action => 'new'
44
+ end
45
+ end
46
+
47
+ def destroy
48
+ session[:<%= user_singular_name %>_id] = nil
49
+ redirect_to root_url
50
+ end
51
+ <%- end -%>
52
+ end
@@ -0,0 +1,2 @@
1
+ module <%= session_plural_class_name %>Helper
2
+ end
@@ -0,0 +1,39 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe <%= session_plural_class_name %>Controller do
4
+ fixtures :all
5
+ integrate_views
6
+
7
+ it "new action should render new template" do
8
+ get :new
9
+ response.should render_template(:new)
10
+ end
11
+
12
+ <%- if options[:authlogic] -%>
13
+ it "create action should render new template when authentication is invalid" do
14
+ post :create, :<%= session_singular_name %> => { :username => "foo", :password => "badpassword" }
15
+ response.should render_template(:new)
16
+ <%= session_class_name %>.find.should be_nil
17
+ end
18
+
19
+ it "create action should redirect when authentication is valid" do
20
+ post :create, :<%= session_singular_name %> => { :username => "foo", :password => "secret" }
21
+ response.should redirect_to(root_url)
22
+ <%= session_class_name %>.find.<%= user_singular_name %>.should == <%= user_plural_name %>(:foo)
23
+ end
24
+ <%- else -%>
25
+ it "create action should render new template when authentication is invalid" do
26
+ <%= user_class_name %>.stubs(:authenticate).returns(nil)
27
+ post :create
28
+ response.should render_template(:new)
29
+ session['<%= user_singular_name %>_id'].should be_nil
30
+ end
31
+
32
+ it "create action should redirect when authentication is valid" do
33
+ <%= user_class_name %>.stubs(:authenticate).returns(<%= user_class_name %>.first)
34
+ post :create
35
+ response.should redirect_to(root_url)
36
+ session['<%= user_singular_name %>_id'].should == <%= user_class_name %>.first.id
37
+ end
38
+ <%- end -%>
39
+ end
@@ -0,0 +1,83 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe <%= user_class_name %> do
4
+ <%- unless options[:authlogic] -%>
5
+ def new_<%= user_singular_name %>(attributes = {})
6
+ attributes[:username] ||= 'foo'
7
+ attributes[:email] ||= 'foo@example.com'
8
+ attributes[:password] ||= 'abc123'
9
+ attributes[:password_confirmation] ||= attributes[:password]
10
+ <%= user_class_name %>.new(attributes)
11
+ end
12
+
13
+ before(:each) do
14
+ <%= user_class_name %>.delete_all
15
+ end
16
+
17
+ it "should be valid" do
18
+ new_<%= user_singular_name %>.should be_valid
19
+ end
20
+
21
+ it "should require username" do
22
+ new_<%= user_singular_name %>(:username => '').should have(1).error_on(:username)
23
+ end
24
+
25
+ it "should require password" do
26
+ new_<%= user_singular_name %>(:password => '').should have(1).error_on(:password)
27
+ end
28
+
29
+ it "should require well formed email" do
30
+ new_<%= user_singular_name %>(:email => 'foo@bar@example.com').should have(1).error_on(:email)
31
+ end
32
+
33
+ it "should validate uniqueness of email" do
34
+ new_<%= user_singular_name %>(:email => 'bar@example.com').save!
35
+ new_<%= user_singular_name %>(:email => 'bar@example.com').should have(1).error_on(:email)
36
+ end
37
+
38
+ it "should validate uniqueness of username" do
39
+ new_<%= user_singular_name %>(:username => 'uniquename').save!
40
+ new_<%= user_singular_name %>(:username => 'uniquename').should have(1).error_on(:username)
41
+ end
42
+
43
+ it "should not allow odd characters in username" do
44
+ new_<%= user_singular_name %>(:username => 'odd ^&(@)').should have(1).error_on(:username)
45
+ end
46
+
47
+ it "should validate password is longer than 3 characters" do
48
+ new_<%= user_singular_name %>(:password => 'bad').should have(1).error_on(:password)
49
+ end
50
+
51
+ it "should require matching password confirmation" do
52
+ new_<%= user_singular_name %>(:password_confirmation => 'nonmatching').should have(1).error_on(:password)
53
+ end
54
+
55
+ it "should generate password hash and salt on create" do
56
+ <%= user_singular_name %> = new_<%= user_singular_name %>
57
+ <%= user_singular_name %>.save!
58
+ <%= user_singular_name %>.password_hash.should_not be_nil
59
+ <%= user_singular_name %>.password_salt.should_not be_nil
60
+ end
61
+
62
+ it "should authenticate by username" do
63
+ <%= user_singular_name %> = new_<%= user_singular_name %>(:username => 'foobar', :password => 'secret')
64
+ <%= user_singular_name %>.save!
65
+ <%= user_class_name %>.authenticate('foobar', 'secret').should == <%= user_singular_name %>
66
+ end
67
+
68
+ it "should authenticate by email" do
69
+ <%= user_singular_name %> = new_<%= user_singular_name %>(:email => 'foo@bar.com', :password => 'secret')
70
+ <%= user_singular_name %>.save!
71
+ <%= user_class_name %>.authenticate('foo@bar.com', 'secret').should == <%= user_singular_name %>
72
+ end
73
+
74
+ it "should not authenticate bad username" do
75
+ <%= user_class_name %>.authenticate('nonexisting', 'secret').should be_nil
76
+ end
77
+
78
+ it "should not authenticate bad password" do
79
+ new_<%= user_singular_name %>(:username => 'foobar', :password => 'secret').save!
80
+ <%= user_class_name %>.authenticate('foobar', 'badpassword').should be_nil
81
+ end
82
+ <%- end -%>
83
+ end
@@ -0,0 +1,26 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe <%= user_plural_class_name %>Controller do
4
+ fixtures :all
5
+ integrate_views
6
+
7
+ it "new action should render new template" do
8
+ get :new
9
+ response.should render_template(:new)
10
+ end
11
+
12
+ it "create action should render new template when model is invalid" do
13
+ <%= user_class_name %>.any_instance.stubs(:valid?).returns(false)
14
+ post :create
15
+ response.should render_template(:new)
16
+ end
17
+
18
+ it "create action should redirect when model is valid" do
19
+ <%= user_class_name %>.any_instance.stubs(:valid?).returns(true)
20
+ post :create
21
+ response.should redirect_to(root_url)
22
+ <%- unless options[:authlogic] -%>
23
+ session['<%= user_singular_name %>_id'].should == assigns['<%= user_singular_name %>'].id
24
+ <%- end -%>
25
+ end
26
+ end
@@ -0,0 +1,40 @@
1
+ require 'test_helper'
2
+
3
+ class <%= session_plural_class_name %>ControllerTest < ActionController::TestCase
4
+ context "new action" do
5
+ should "render new template" do
6
+ get :new
7
+ assert_template 'new'
8
+ end
9
+ end
10
+
11
+ context "create action" do
12
+ <%- if options[:authlogic] -%>
13
+ should "render new template when authentication is invalid" do
14
+ post :create, :<%= session_singular_name %> => { :username => "foo", :password => "badpassword" }
15
+ assert_template 'new'
16
+ assert_nil <%= session_class_name %>.find
17
+ end
18
+
19
+ should "redirect when authentication is valid" do
20
+ post :create, :<%= session_singular_name %> => { :username => "foo", :password => "secret" }
21
+ assert_redirected_to root_url
22
+ assert_equal <%= user_plural_name %>(:foo), <%= session_class_name %>.find.<%= user_singular_name %>
23
+ end
24
+ <%- else -%>
25
+ should "render new template when authentication is invalid" do
26
+ <%= user_class_name %>.stubs(:authenticate).returns(nil)
27
+ post :create
28
+ assert_template 'new'
29
+ assert_nil session['<%= user_singular_name %>_id']
30
+ end
31
+
32
+ should "redirect when authentication is valid" do
33
+ <%= user_class_name %>.stubs(:authenticate).returns(<%= user_class_name %>.first)
34
+ post :create
35
+ assert_redirected_to root_url
36
+ assert_equal <%= user_class_name %>.first.id, session['<%= user_singular_name %>_id']
37
+ end
38
+ <%- end -%>
39
+ end
40
+ end
@@ -0,0 +1,85 @@
1
+ require 'test_helper'
2
+
3
+ class <%= user_class_name %>Test < ActiveSupport::TestCase
4
+ <%- unless options[:authlogic] -%>
5
+ def new_<%= user_singular_name %>(attributes = {})
6
+ attributes[:username] ||= 'foo'
7
+ attributes[:email] ||= 'foo@example.com'
8
+ attributes[:password] ||= 'abc123'
9
+ attributes[:password_confirmation] ||= attributes[:password]
10
+ <%= user_singular_name %> = <%= user_class_name %>.new(attributes)
11
+ <%= user_singular_name %>.valid? # run validations
12
+ <%= user_singular_name %>
13
+ end
14
+
15
+ def setup
16
+ <%= user_class_name %>.delete_all
17
+ end
18
+
19
+ should "be valid" do
20
+ assert new_<%= user_singular_name %>.valid?
21
+ end
22
+
23
+ should "require username" do
24
+ assert new_<%= user_singular_name %>(:username => '').errors.on(:username)
25
+ end
26
+
27
+ should "require password" do
28
+ assert new_<%= user_singular_name %>(:password => '').errors.on(:password)
29
+ end
30
+
31
+ should "require well formed email" do
32
+ assert new_<%= user_singular_name %>(:email => 'foo@bar@example.com').errors.on(:email)
33
+ end
34
+
35
+ should "validate uniqueness of email" do
36
+ new_<%= user_singular_name %>(:email => 'bar@example.com').save!
37
+ assert new_<%= user_singular_name %>(:email => 'bar@example.com').errors.on(:email)
38
+ end
39
+
40
+ should "validate uniqueness of username" do
41
+ new_<%= user_singular_name %>(:username => 'uniquename').save!
42
+ assert new_<%= user_singular_name %>(:username => 'uniquename').errors.on(:username)
43
+ end
44
+
45
+ should "not allow odd characters in username" do
46
+ assert new_<%= user_singular_name %>(:username => 'odd ^&(@)').errors.on(:username)
47
+ end
48
+
49
+ should "validate password is longer than 3 characters" do
50
+ assert new_<%= user_singular_name %>(:password => 'bad').errors.on(:password)
51
+ end
52
+
53
+ should "require matching password confirmation" do
54
+ assert new_<%= user_singular_name %>(:password_confirmation => 'nonmatching').errors.on(:password)
55
+ end
56
+
57
+ should "generate password hash and salt on create" do
58
+ <%= user_singular_name %> = new_<%= user_singular_name %>
59
+ <%= user_singular_name %>.save!
60
+ assert <%= user_singular_name %>.password_hash
61
+ assert <%= user_singular_name %>.password_salt
62
+ end
63
+
64
+ should "authenticate by username" do
65
+ <%= user_singular_name %> = new_<%= user_singular_name %>(:username => 'foobar', :password => 'secret')
66
+ <%= user_singular_name %>.save!
67
+ assert_equal <%= user_singular_name %>, <%= user_class_name %>.authenticate('foobar', 'secret')
68
+ end
69
+
70
+ should "authenticate by email" do
71
+ <%= user_singular_name %> = new_<%= user_singular_name %>(:email => 'foo@bar.com', :password => 'secret')
72
+ <%= user_singular_name %>.save!
73
+ assert_equal <%= user_singular_name %>, <%= user_class_name %>.authenticate('foo@bar.com', 'secret')
74
+ end
75
+
76
+ should "not authenticate bad username" do
77
+ assert_nil <%= user_class_name %>.authenticate('nonexisting', 'secret')
78
+ end
79
+
80
+ should "not authenticate bad password" do
81
+ new_<%= user_singular_name %>(:username => 'foobar', :password => 'secret').save!
82
+ assert_nil <%= user_class_name %>.authenticate('foobar', 'badpassword')
83
+ end
84
+ <%- end -%>
85
+ end
@@ -0,0 +1,27 @@
1
+ require 'test_helper'
2
+
3
+ class <%= user_plural_class_name %>ControllerTest < ActionController::TestCase
4
+ context "new action" do
5
+ should "render new template" do
6
+ get :new
7
+ assert_template 'new'
8
+ end
9
+ end
10
+
11
+ context "create action" do
12
+ should "render new template when model is invalid" do
13
+ <%= user_class_name %>.any_instance.stubs(:valid?).returns(false)
14
+ post :create
15
+ assert_template 'new'
16
+ end
17
+
18
+ should "redirect when model is valid" do
19
+ <%= user_class_name %>.any_instance.stubs(:valid?).returns(true)
20
+ post :create
21
+ assert_redirected_to root_url
22
+ <%- unless options[:authlogic] -%>
23
+ assert_equal assigns['<%= user_singular_name %>'].id, session['<%= user_singular_name %>_id']
24
+ <%- end -%>
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,36 @@
1
+ require 'test_helper'
2
+
3
+ class <%= session_plural_class_name %>ControllerTest < ActionController::TestCase
4
+ def test_new
5
+ get :new
6
+ assert_template 'new'
7
+ end
8
+
9
+ <%- if options[:authlogic] -%>
10
+ def test_create_invalid
11
+ post :create, :<%= session_singular_name %> => { :username => "foo", :password => "badpassword" }
12
+ assert_template 'new'
13
+ assert_nil <%= session_class_name %>.find
14
+ end
15
+
16
+ def test_create_valid
17
+ post :create, :<%= session_singular_name %> => { :username => "foo", :password => "secret" }
18
+ assert_redirected_to root_url
19
+ assert_equal <%= user_plural_name %>(:foo), <%= session_class_name %>.find.<%= user_singular_name %>
20
+ end
21
+ <%- else -%>
22
+ def test_create_invalid
23
+ <%= user_class_name %>.stubs(:authenticate).returns(nil)
24
+ post :create
25
+ assert_template 'new'
26
+ assert_nil session['<%= user_singular_name %>_id']
27
+ end
28
+
29
+ def test_create_valid
30
+ <%= user_class_name %>.stubs(:authenticate).returns(<%= user_class_name %>.first)
31
+ post :create
32
+ assert_redirected_to root_url
33
+ assert_equal <%= user_class_name %>.first.id, session['<%= user_singular_name %>_id']
34
+ end
35
+ <%- end -%>
36
+ end
@@ -0,0 +1,88 @@
1
+ require 'test_helper'
2
+
3
+ class <%= user_class_name %>Test < ActiveSupport::TestCase
4
+ <%- unless options[:authlogic] -%>
5
+ def new_<%= user_singular_name %>(attributes = {})
6
+ attributes[:username] ||= 'foo'
7
+ attributes[:email] ||= 'foo@example.com'
8
+ attributes[:password] ||= 'abc123'
9
+ attributes[:password_confirmation] ||= attributes[:password]
10
+ <%= user_singular_name %> = <%= user_class_name %>.new(attributes)
11
+ <%= user_singular_name %>.valid? # run validations
12
+ <%= user_singular_name %>
13
+ end
14
+
15
+ def setup
16
+ <%= user_class_name %>.delete_all
17
+ end
18
+
19
+ def test_valid
20
+ assert new_<%= user_singular_name %>.valid?
21
+ end
22
+
23
+ def test_require_username
24
+ assert new_<%= user_singular_name %>(:username => '').errors.on(:username)
25
+ end
26
+
27
+ def test_require_password
28
+ assert new_<%= user_singular_name %>(:password => '').errors.on(:password)
29
+ end
30
+
31
+ def test_require_well_formed_email
32
+ assert new_<%= user_singular_name %>(:email => 'foo@bar@example.com').errors.on(:email)
33
+ end
34
+
35
+ def test_validate_uniqueness_of_email
36
+ new_<%= user_singular_name %>(:email => 'bar@example.com').save!
37
+ assert new_<%= user_singular_name %>(:email => 'bar@example.com').errors.on(:email)
38
+ end
39
+
40
+ def test_validate_uniqueness_of_username
41
+ new_<%= user_singular_name %>(:username => 'uniquename').save!
42
+ assert new_<%= user_singular_name %>(:username => 'uniquename').errors.on(:username)
43
+ end
44
+
45
+ def test_validate_odd_characters_in_username
46
+ assert new_<%= user_singular_name %>(:username => 'odd ^&(@)').errors.on(:username)
47
+ end
48
+
49
+ def test_validate_password_length
50
+ assert new_<%= user_singular_name %>(:password => 'bad').errors.on(:password)
51
+ end
52
+
53
+ def test_require_matching_password_confirmation
54
+ assert new_<%= user_singular_name %>(:password_confirmation => 'nonmatching').errors.on(:password)
55
+ end
56
+
57
+ def test_generate_password_hash_and_salt_on_create
58
+ <%= user_singular_name %> = new_<%= user_singular_name %>
59
+ <%= user_singular_name %>.save!
60
+ assert <%= user_singular_name %>.password_hash
61
+ assert <%= user_singular_name %>.password_salt
62
+ end
63
+
64
+ def test_authenticate_by_username
65
+ <%= user_class_name %>.delete_all
66
+ <%= user_singular_name %> = new_<%= user_singular_name %>(:username => 'foobar', :password => 'secret')
67
+ <%= user_singular_name %>.save!
68
+ assert_equal <%= user_singular_name %>, <%= user_class_name %>.authenticate('foobar', 'secret')
69
+ end
70
+
71
+ def test_authenticate_by_email
72
+ <%= user_class_name %>.delete_all
73
+ <%= user_singular_name %> = new_<%= user_singular_name %>(:email => 'foo@bar.com', :password => 'secret')
74
+ <%= user_singular_name %>.save!
75
+ assert_equal <%= user_singular_name %>, <%= user_class_name %>.authenticate('foo@bar.com', 'secret')
76
+ end
77
+
78
+ def test_authenticate_bad_username
79
+ assert_nil <%= user_class_name %>.authenticate('nonexisting', 'secret')
80
+ end
81
+
82
+ def test_authenticate_bad_password
83
+ <%= user_class_name %>.delete_all
84
+ new_<%= user_singular_name %>(:username => 'foobar', :password => 'secret').save!
85
+ assert_nil <%= user_class_name %>.authenticate('foobar', 'badpassword')
86
+ end
87
+ <%- end -%>
88
+ end
@@ -0,0 +1,23 @@
1
+ require 'test_helper'
2
+
3
+ class <%= user_plural_class_name %>ControllerTest < ActionController::TestCase
4
+ def test_new
5
+ get :new
6
+ assert_template 'new'
7
+ end
8
+
9
+ def test_create_invalid
10
+ <%= user_class_name %>.any_instance.stubs(:valid?).returns(false)
11
+ post :create
12
+ assert_template 'new'
13
+ end
14
+
15
+ def test_create_valid
16
+ <%= user_class_name %>.any_instance.stubs(:valid?).returns(true)
17
+ post :create
18
+ assert_redirected_to root_url
19
+ <%- unless options[:authlogic] -%>
20
+ assert_equal assigns['<%= user_singular_name %>'].id, session['<%= user_singular_name %>_id']
21
+ <%- end -%>
22
+ end
23
+ end
@@ -0,0 +1,43 @@
1
+ class <%= user_class_name %> < ActiveRecord::Base
2
+ <%- if options[:authlogic] -%>
3
+ acts_as_authentic
4
+ <%- else -%>
5
+ # new columns need to be added here to be writable through mass assignment
6
+ attr_accessible :username, :email, :password, :password_confirmation
7
+
8
+ attr_accessor :password
9
+ before_save :prepare_password
10
+
11
+ validates_presence_of :username
12
+ validates_uniqueness_of :username, :email, :allow_blank => true
13
+ validates_format_of :username, :with => /^[-\w\._@]+$/i, :allow_blank => true, :message => "should only contain letters, numbers, or .-_@"
14
+ validates_format_of :email, :with => /^[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i
15
+ validates_presence_of :password, :on => :create
16
+ validates_presence_of :password_confirmation, :on => :create
17
+ validates_confirmation_of :password
18
+ validates_length_of :password, :minimum => 4, :allow_blank => true
19
+
20
+ # login can be either username or email address
21
+ def self.authenticate(login, pass)
22
+ <%= user_singular_name %> = find_by_username(login) || find_by_email(login)
23
+ return <%= user_singular_name %> if <%= user_singular_name %> && <%= user_singular_name %>.matching_password?(pass)
24
+ end
25
+
26
+ def matching_password?(pass)
27
+ self.password_hash == encrypt_password(pass)
28
+ end
29
+
30
+ private
31
+
32
+ def prepare_password
33
+ unless password.blank?
34
+ self.password_salt = Digest::SHA1.hexdigest([Time.now, rand].join)
35
+ self.password_hash = encrypt_password(password)
36
+ end
37
+ end
38
+
39
+ def encrypt_password(pass)
40
+ Digest::SHA1.hexdigest([pass, password_salt].join)
41
+ end
42
+ <%- end -%>
43
+ end