iain-pizza-generators 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +67 -0
- data/LICENSE +20 -0
- data/README.rdoc +238 -0
- data/Rakefile +16 -0
- data/TODO +7 -0
- data/lib/pizza_generators.rb +3 -0
- data/pizza-generators.gemspec +53 -0
- data/rails_generators/pizza_authentication/USAGE +46 -0
- data/rails_generators/pizza_authentication/lib/insert_commands.rb +74 -0
- data/rails_generators/pizza_authentication/pizza_authentication_generator.rb +123 -0
- data/rails_generators/pizza_authentication/templates/authentication.rb +43 -0
- data/rails_generators/pizza_authentication/templates/fixtures.yml +26 -0
- data/rails_generators/pizza_authentication/templates/migration.rb +33 -0
- data/rails_generators/pizza_authentication/templates/session.rb +3 -0
- data/rails_generators/pizza_authentication/templates/sessions_controller.rb +24 -0
- data/rails_generators/pizza_authentication/templates/sessions_helper.rb +2 -0
- data/rails_generators/pizza_authentication/templates/tests/rspec/sessions_controller.rb +45 -0
- data/rails_generators/pizza_authentication/templates/tests/rspec/user.rb +45 -0
- data/rails_generators/pizza_authentication/templates/tests/rspec/users_controller.rb +33 -0
- data/rails_generators/pizza_authentication/templates/tests/shoulda/sessions_controller.rb +44 -0
- data/rails_generators/pizza_authentication/templates/tests/shoulda/user.rb +83 -0
- data/rails_generators/pizza_authentication/templates/tests/shoulda/users_controller.rb +25 -0
- data/rails_generators/pizza_authentication/templates/tests/testunit/sessions_controller.rb +32 -0
- data/rails_generators/pizza_authentication/templates/tests/testunit/user.rb +86 -0
- data/rails_generators/pizza_authentication/templates/tests/testunit/users_controller.rb +21 -0
- data/rails_generators/pizza_authentication/templates/user.rb +3 -0
- data/rails_generators/pizza_authentication/templates/users_controller.rb +13 -0
- data/rails_generators/pizza_authentication/templates/users_helper.rb +2 -0
- data/rails_generators/pizza_authentication/templates/views/erb/login.html.erb +15 -0
- data/rails_generators/pizza_authentication/templates/views/erb/signup.html.erb +24 -0
- data/rails_generators/pizza_authentication/templates/views/haml/login.html.haml +15 -0
- data/rails_generators/pizza_authentication/templates/views/haml/signup.html.haml +19 -0
- data/rails_generators/pizza_config/USAGE +23 -0
- data/rails_generators/pizza_config/pizza_config_generator.rb +32 -0
- data/rails_generators/pizza_config/templates/config.yml +8 -0
- data/rails_generators/pizza_config/templates/load_config.rb +2 -0
- data/rails_generators/pizza_layout/USAGE +25 -0
- data/rails_generators/pizza_layout/pizza_layout_generator.rb +55 -0
- data/rails_generators/pizza_layout/templates/_menu.html.haml +4 -0
- data/rails_generators/pizza_layout/templates/application.sass +5 -0
- data/rails_generators/pizza_layout/templates/definition_lists.sass +8 -0
- data/rails_generators/pizza_layout/templates/forms.sass +79 -0
- data/rails_generators/pizza_layout/templates/helper.rb +41 -0
- data/rails_generators/pizza_layout/templates/layout.html.erb +22 -0
- data/rails_generators/pizza_layout/templates/layout.html.haml +25 -0
- data/rails_generators/pizza_layout/templates/layout.sass +121 -0
- data/rails_generators/pizza_layout/templates/required.png +0 -0
- data/rails_generators/pizza_layout/templates/reset.sass +39 -0
- data/rails_generators/pizza_layout/templates/stylesheet.css +81 -0
- data/rails_generators/pizza_layout/templates/tables.sass +23 -0
- data/rails_generators/pizza_layout/templates/will_paginate.rb +9 -0
- data/rails_generators/pizza_scaffold/USAGE +51 -0
- data/rails_generators/pizza_scaffold/pizza_scaffold_generator.rb +233 -0
- data/rails_generators/pizza_scaffold/templates/actions/create.rb +4 -0
- data/rails_generators/pizza_scaffold/templates/actions/destroy.rb +4 -0
- data/rails_generators/pizza_scaffold/templates/actions/edit.rb +0 -0
- data/rails_generators/pizza_scaffold/templates/actions/index.rb +3 -0
- data/rails_generators/pizza_scaffold/templates/actions/new.rb +0 -0
- data/rails_generators/pizza_scaffold/templates/actions/show.rb +0 -0
- data/rails_generators/pizza_scaffold/templates/actions/update.rb +4 -0
- data/rails_generators/pizza_scaffold/templates/controller.rb +11 -0
- data/rails_generators/pizza_scaffold/templates/fixtures.yml +9 -0
- data/rails_generators/pizza_scaffold/templates/helper.rb +2 -0
- data/rails_generators/pizza_scaffold/templates/migration.rb +16 -0
- data/rails_generators/pizza_scaffold/templates/model.rb +21 -0
- data/rails_generators/pizza_scaffold/templates/tests/rspec/actions/create.rb +15 -0
- data/rails_generators/pizza_scaffold/templates/tests/rspec/actions/destroy.rb +10 -0
- data/rails_generators/pizza_scaffold/templates/tests/rspec/actions/edit.rb +8 -0
- data/rails_generators/pizza_scaffold/templates/tests/rspec/actions/index.rb +8 -0
- data/rails_generators/pizza_scaffold/templates/tests/rspec/actions/new.rb +8 -0
- data/rails_generators/pizza_scaffold/templates/tests/rspec/actions/show.rb +8 -0
- data/rails_generators/pizza_scaffold/templates/tests/rspec/actions/update.rb +15 -0
- data/rails_generators/pizza_scaffold/templates/tests/rspec/controller.rb +8 -0
- data/rails_generators/pizza_scaffold/templates/tests/rspec/model.rb +7 -0
- data/rails_generators/pizza_scaffold/templates/tests/shoulda/actions/create.rb +13 -0
- data/rails_generators/pizza_scaffold/templates/tests/shoulda/actions/destroy.rb +8 -0
- data/rails_generators/pizza_scaffold/templates/tests/shoulda/actions/edit.rb +6 -0
- data/rails_generators/pizza_scaffold/templates/tests/shoulda/actions/index.rb +6 -0
- data/rails_generators/pizza_scaffold/templates/tests/shoulda/actions/new.rb +6 -0
- data/rails_generators/pizza_scaffold/templates/tests/shoulda/actions/show.rb +6 -0
- data/rails_generators/pizza_scaffold/templates/tests/shoulda/actions/update.rb +13 -0
- data/rails_generators/pizza_scaffold/templates/tests/shoulda/controller.rb +5 -0
- data/rails_generators/pizza_scaffold/templates/tests/shoulda/model.rb +7 -0
- data/rails_generators/pizza_scaffold/templates/tests/testunit/actions/create.rb +11 -0
- data/rails_generators/pizza_scaffold/templates/tests/testunit/actions/destroy.rb +6 -0
- data/rails_generators/pizza_scaffold/templates/tests/testunit/actions/edit.rb +4 -0
- data/rails_generators/pizza_scaffold/templates/tests/testunit/actions/index.rb +4 -0
- data/rails_generators/pizza_scaffold/templates/tests/testunit/actions/new.rb +4 -0
- data/rails_generators/pizza_scaffold/templates/tests/testunit/actions/show.rb +4 -0
- data/rails_generators/pizza_scaffold/templates/tests/testunit/actions/update.rb +11 -0
- data/rails_generators/pizza_scaffold/templates/tests/testunit/controller.rb +5 -0
- data/rails_generators/pizza_scaffold/templates/tests/testunit/model.rb +7 -0
- data/rails_generators/pizza_scaffold/templates/views/erb/_form.html.erb +10 -0
- data/rails_generators/pizza_scaffold/templates/views/erb/edit.html.erb +14 -0
- data/rails_generators/pizza_scaffold/templates/views/erb/index.html.erb +29 -0
- data/rails_generators/pizza_scaffold/templates/views/erb/new.html.erb +7 -0
- data/rails_generators/pizza_scaffold/templates/views/erb/show.html.erb +20 -0
- data/rails_generators/pizza_scaffold/templates/views/haml/_form.html.haml +10 -0
- data/rails_generators/pizza_scaffold/templates/views/haml/edit.html.haml +13 -0
- data/rails_generators/pizza_scaffold/templates/views/haml/index.html.haml +31 -0
- data/rails_generators/pizza_scaffold/templates/views/haml/new.html.haml +8 -0
- data/rails_generators/pizza_scaffold/templates/views/haml/show.html.haml +17 -0
- data/tasks/deployment.rake +5 -0
- data/test/test_helper.rb +117 -0
- data/test/test_pizza_authentication_generator.rb +158 -0
- data/test/test_pizza_config_generator.rb +37 -0
- data/test/test_pizza_layout_generator.rb +42 -0
- data/test/test_pizza_scaffold_generator.rb +532 -0
- metadata +237 -0
@@ -0,0 +1,43 @@
|
|
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 <%%= current_<%= user_singular_name %>.login %>! 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
|
+
|
19
|
+
def self.included(controller)
|
20
|
+
controller.send :helper_method, :current_<%= user_singular_name %>, :logged_in?, :current_<%= user_session_singular_name %>
|
21
|
+
end
|
22
|
+
|
23
|
+
def current_<%= user_singular_name %>
|
24
|
+
return @current_<%= user_singular_name %> if defined?(@current_<%= user_singular_name %>)
|
25
|
+
@current_<%= user_singular_name %> = current_<%= user_session_singular_name %> && current_<%= user_session_singular_name %>.user
|
26
|
+
end
|
27
|
+
|
28
|
+
def current_<%= user_session_singular_name %>
|
29
|
+
return @current_<%= user_session_singular_name %> if defined?(@current_<%= user_session_singular_name %>)
|
30
|
+
@current_<%= user_session_singular_name %> = <%= user_session_class_name %>.find
|
31
|
+
end
|
32
|
+
|
33
|
+
def logged_in?
|
34
|
+
current_<%= user_singular_name %>
|
35
|
+
end
|
36
|
+
|
37
|
+
def login_required
|
38
|
+
unless logged_in?
|
39
|
+
flash[:error] = I18n.t("<%= user_session_plural_name %>.login_required")
|
40
|
+
redirect_to login_url
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# password: "secret"
|
2
|
+
one:
|
3
|
+
login: foo
|
4
|
+
password_salt: <%%= salt = User.unique_token %>
|
5
|
+
crypted_password: <%%= Authlogic::CryptoProviders::Sha512.encrypt("secret" + salt) %>
|
6
|
+
persistence_token: 6cde0674657a8a313ce952df979de2830309aa4c11ca65805dd00bfdc65dbcc2f5e36718660a1d2e68c1a08c276d996763985d2f06fd3d076eb7bc4d97b1e317
|
7
|
+
login_count: 10
|
8
|
+
last_request_at: <%%= 10.seconds.ago %>
|
9
|
+
current_logint_at: <%%= 1.minute.ago %>
|
10
|
+
last_login_at: <%%= 1.day.ago %>
|
11
|
+
current_login_ip: "127.0.0.1"
|
12
|
+
last_login_ip: "127.0.0.7"
|
13
|
+
active: true
|
14
|
+
|
15
|
+
two:
|
16
|
+
login: bar
|
17
|
+
password_salt: <%%= salt = User.unique_token %>
|
18
|
+
crypted_password: <%%= Authlogic::CryptoProviders::Sha512.encrypt("secret" + salt) %>
|
19
|
+
persistence_token: ""
|
20
|
+
login_count: 10
|
21
|
+
last_request_at: <%%= 10.seconds.ago %>
|
22
|
+
current_logint_at: <%%= 1.minute.ago %>
|
23
|
+
last_login_at: <%%= 1.day.ago %>
|
24
|
+
current_login_ip: "127.0.0.1"
|
25
|
+
last_login_ip: "127.0.0.7"
|
26
|
+
active: false
|
@@ -0,0 +1,33 @@
|
|
1
|
+
class Create<%= user_plural_class_name %> < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table :<%= user_plural_name %> do |t|
|
4
|
+
|
5
|
+
# Required database fields
|
6
|
+
t.string :login, :null => false
|
7
|
+
t.string :crypted_password, :null => false
|
8
|
+
t.string :password_salt, :null => false
|
9
|
+
t.string :persistence_token, :null => false
|
10
|
+
|
11
|
+
# Optional tokens, see Authlogic README for more details.
|
12
|
+
# t.string :single_access_token, :null => false # or: rss_feed_token
|
13
|
+
# t.string :perishable_token, :null => false # or: pw_reset_token, activation_token
|
14
|
+
|
15
|
+
# Magic columns
|
16
|
+
t.integer :login_count, :null => false, :default => 0
|
17
|
+
t.datetime :last_request_at
|
18
|
+
t.datetime :current_logint_at
|
19
|
+
t.datetime :last_login_at
|
20
|
+
t.string :current_login_ip
|
21
|
+
t.string :last_login_ip
|
22
|
+
|
23
|
+
# For activating an account, optional
|
24
|
+
t.boolean :active, :null => false, :default => true
|
25
|
+
|
26
|
+
t.timestamps
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.down
|
31
|
+
drop_table :<%= user_plural_name %>
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
class <%= user_session_plural_class_name %>Controller < ResourceController::Singleton
|
2
|
+
|
3
|
+
actions :new, :create, :destroy
|
4
|
+
|
5
|
+
create do
|
6
|
+
success do
|
7
|
+
wants.html { redirect_to root_url }
|
8
|
+
flash { t(:success) }
|
9
|
+
end
|
10
|
+
failure.flash_now { t(:failure) }
|
11
|
+
end
|
12
|
+
|
13
|
+
destroy.success do
|
14
|
+
flash { t(:success) }
|
15
|
+
wants.html { redirect_to root_url }
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def object
|
21
|
+
@object ||= <%= user_session_class_name %>.find
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
require 'authlogic/testing/test_unit_helpers' # put this in spec/spec_helper.rb
|
3
|
+
|
4
|
+
describe <%= user_session_plural_class_name %>Controller do
|
5
|
+
fixtures :all
|
6
|
+
integrate_views
|
7
|
+
|
8
|
+
describe "new action" do
|
9
|
+
|
10
|
+
it "should render new template" do
|
11
|
+
get :new
|
12
|
+
response.should render_template(:new)
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
describe "create action" do
|
18
|
+
|
19
|
+
it "should render new template when authentication is invalid" do
|
20
|
+
post :create, :<%= user_session_singular_name %> => { :login => "foo", :password => "wrong" }
|
21
|
+
response.should render_template(:new)
|
22
|
+
<%= user_session_class_name %>.find.should be_nil
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should redirect when authentication is valid" do
|
26
|
+
post :create, :<%= user_session_singular_name %> => { :login => "foo", :password => "secret" }
|
27
|
+
response.should redirect_to(root_url)
|
28
|
+
<%= user_session_class_name %>.find.should_not be_nil
|
29
|
+
<%= user_session_class_name %>.find.user.should == <%= user_plural_name %>(:one)
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "destroy action" do
|
35
|
+
|
36
|
+
it "should destroy user session" do
|
37
|
+
set_session_for <%= user_plural_name %>(:one)
|
38
|
+
delete :destroy
|
39
|
+
<%= user_session_class_name %>.find.should be_nil
|
40
|
+
response.should redirect_to(root_url)
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
describe <%= user_class_name %> do
|
4
|
+
|
5
|
+
def new_<%= user_singular_name %>(attributes = {})
|
6
|
+
attributes[:login] ||= 'foo'
|
7
|
+
attributes[:password] ||= 'abc123'
|
8
|
+
attributes[:password_confirmation] ||= attributes[:password]
|
9
|
+
<%= user_class_name %>.new(attributes)
|
10
|
+
end
|
11
|
+
|
12
|
+
before(:each) do
|
13
|
+
<%= user_class_name %>.delete_all
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should be valid" do
|
17
|
+
new_<%= user_singular_name %>.should be_valid
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should require login" do
|
21
|
+
new_<%= user_singular_name %>(:login => '').should have_at_least(1).error_on(:login)
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should require password" do
|
25
|
+
new_<%= user_singular_name %>(:password => '').should have_at_least(1).error_on(:password)
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should validate uniqueness of login" do
|
29
|
+
new_<%= user_singular_name %>(:login => 'uniquename').save!
|
30
|
+
new_<%= user_singular_name %>(:login => 'uniquename').should have_at_least(1).error_on(:login)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should not allow odd characters in login" do
|
34
|
+
new_<%= user_singular_name %>(:login => 'odd ^&(@)').should have_at_least(1).error_on(:login)
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should validate password is longer than 3 characters" do
|
38
|
+
new_<%= user_singular_name %>(:password => 'bad').should have_at_least(1).error_on(:password)
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should require matching password confirmation" do
|
42
|
+
new_<%= user_singular_name %>(:password_confirmation => 'nonmatching').should have_at_least(1).error_on(:password)
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
describe <%= user_plural_class_name %>Controller do
|
4
|
+
fixtures :all
|
5
|
+
integrate_views
|
6
|
+
|
7
|
+
describe "new action" do
|
8
|
+
|
9
|
+
it "should render new template" do
|
10
|
+
get :new
|
11
|
+
response.should render_template(:new)
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "create action" do
|
17
|
+
|
18
|
+
it "should render new template when model is invalid" do
|
19
|
+
<%= user_class_name %>.any_instance.stubs(:valid?).returns(false)
|
20
|
+
post :create
|
21
|
+
response.should render_template(:new)
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should redirect when model is valid" do
|
25
|
+
<%= user_class_name %>.any_instance.stubs(:valid?).returns(true)
|
26
|
+
<%= user_class_name %>.stubs(:new).returns(<%= user_plural_name %>(:one))
|
27
|
+
post :create
|
28
|
+
response.should redirect_to(root_url)
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'authlogic/testing/test_unit_helpers' # put this in test/test_helper.rb
|
3
|
+
|
4
|
+
class <%= user_session_plural_class_name %>ControllerTest < ActionController::TestCase
|
5
|
+
|
6
|
+
context "new action" do
|
7
|
+
|
8
|
+
should "render new template" do
|
9
|
+
get :new
|
10
|
+
assert_template 'new'
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
|
15
|
+
context "create action" do
|
16
|
+
|
17
|
+
should "create user session when succesful login" do
|
18
|
+
post :create, :<%= user_session_singular_name %> => { :login => "foo", :password => "secret" }
|
19
|
+
assert <%= user_session_singular_name %> = <%= user_session_class_name %>.find
|
20
|
+
assert_equal <%= user_plural_name %>(:foo), <%= user_session_singular_name %>.user
|
21
|
+
assert_redirected_to root_url
|
22
|
+
end
|
23
|
+
|
24
|
+
should "render new template when failed login" do
|
25
|
+
post :create, :<%= user_session_singular_name %> => { :login => "foo", :password => "wrong" }
|
26
|
+
assert_template 'new'
|
27
|
+
assert_nil <%= user_session_class_name %>.find
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
context "destroy action" do
|
33
|
+
|
34
|
+
should "destroy user session" do
|
35
|
+
set_session_for <%= user_plural_name %>(:foo)
|
36
|
+
assert <%= user_session_class_name %>.find
|
37
|
+
delete :destroy
|
38
|
+
assert_nil <%= user_session_class_name %>.find
|
39
|
+
assert_redirected_to login_url
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class <%= user_class_name %>Test < ActiveSupport::TestCase
|
4
|
+
def new_<%= user_singular_name %>(attributes = {})
|
5
|
+
attributes[:username] ||= 'foo'
|
6
|
+
attributes[:email] ||= 'foo@example.com'
|
7
|
+
attributes[:password] ||= 'abc123'
|
8
|
+
attributes[:password_confirmation] ||= attributes[:password]
|
9
|
+
<%= user_singular_name %> = <%= user_class_name %>.new(attributes)
|
10
|
+
<%= user_singular_name %>.valid? # run validations
|
11
|
+
<%= user_singular_name %>
|
12
|
+
end
|
13
|
+
|
14
|
+
def setup
|
15
|
+
<%= user_class_name %>.delete_all
|
16
|
+
end
|
17
|
+
|
18
|
+
should "be valid" do
|
19
|
+
assert new_<%= user_singular_name %>.valid?
|
20
|
+
end
|
21
|
+
|
22
|
+
should "require username" do
|
23
|
+
assert new_<%= user_singular_name %>(:username => '').errors.on(:username)
|
24
|
+
end
|
25
|
+
|
26
|
+
should "require password" do
|
27
|
+
assert new_<%= user_singular_name %>(:password => '').errors.on(:password)
|
28
|
+
end
|
29
|
+
|
30
|
+
should "require well formed email" do
|
31
|
+
assert new_<%= user_singular_name %>(:email => 'foo@bar@example.com').errors.on(:email)
|
32
|
+
end
|
33
|
+
|
34
|
+
should "validate uniqueness of email" do
|
35
|
+
new_<%= user_singular_name %>(:email => 'bar@example.com').save!
|
36
|
+
assert new_<%= user_singular_name %>(:email => 'bar@example.com').errors.on(:email)
|
37
|
+
end
|
38
|
+
|
39
|
+
should "validate uniqueness of username" do
|
40
|
+
new_<%= user_singular_name %>(:username => 'uniquename').save!
|
41
|
+
assert new_<%= user_singular_name %>(:username => 'uniquename').errors.on(:username)
|
42
|
+
end
|
43
|
+
|
44
|
+
should "not allow odd characters in username" do
|
45
|
+
assert new_<%= user_singular_name %>(:username => 'odd ^&(@)').errors.on(:username)
|
46
|
+
end
|
47
|
+
|
48
|
+
should "validate password is longer than 3 characters" do
|
49
|
+
assert new_<%= user_singular_name %>(:password => 'bad').errors.on(:password)
|
50
|
+
end
|
51
|
+
|
52
|
+
should "require matching password confirmation" do
|
53
|
+
assert new_<%= user_singular_name %>(:password_confirmation => 'nonmatching').errors.on(:password)
|
54
|
+
end
|
55
|
+
|
56
|
+
should "generate password hash and salt on create" do
|
57
|
+
<%= user_singular_name %> = new_<%= user_singular_name %>
|
58
|
+
<%= user_singular_name %>.save!
|
59
|
+
assert <%= user_singular_name %>.password_hash
|
60
|
+
assert <%= user_singular_name %>.password_salt
|
61
|
+
end
|
62
|
+
|
63
|
+
should "authenticate by username" do
|
64
|
+
<%= user_singular_name %> = new_<%= user_singular_name %>(:username => 'foobar', :password => 'secret')
|
65
|
+
<%= user_singular_name %>.save!
|
66
|
+
assert_equal <%= user_singular_name %>, <%= user_class_name %>.authenticate('foobar', 'secret')
|
67
|
+
end
|
68
|
+
|
69
|
+
should "authenticate by email" do
|
70
|
+
<%= user_singular_name %> = new_<%= user_singular_name %>(:email => 'foo@bar.com', :password => 'secret')
|
71
|
+
<%= user_singular_name %>.save!
|
72
|
+
assert_equal <%= user_singular_name %>, <%= user_class_name %>.authenticate('foo@bar.com', 'secret')
|
73
|
+
end
|
74
|
+
|
75
|
+
should "not authenticate bad username" do
|
76
|
+
assert_nil <%= user_class_name %>.authenticate('nonexisting', 'secret')
|
77
|
+
end
|
78
|
+
|
79
|
+
should "not authenticate bad password" do
|
80
|
+
new_<%= user_singular_name %>(:username => 'foobar', :password => 'secret').save!
|
81
|
+
assert_nil <%= user_class_name %>.authenticate('foobar', 'badpassword')
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,25 @@
|
|
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
|
+
assert_equal assigns['<%= user_singular_name %>'].id, session['<%= user_singular_name %>_id']
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'authlogic/testing/test_unit_helpers' # put this in test/test_helper.rb
|
3
|
+
|
4
|
+
class <%= user_session_plural_class_name %>ControllerTest < ActionController::TestCase
|
5
|
+
|
6
|
+
test "new action should render new template" do
|
7
|
+
get :new
|
8
|
+
assert_template 'new'
|
9
|
+
end
|
10
|
+
|
11
|
+
test "create action should create user session when succesful login" do
|
12
|
+
post :create, :<%= user_session_singular_name %> => { :login => "foo", :password => "secret" }
|
13
|
+
assert <%= user_session_singular_name %> = <%= user_session_class_name %>.find
|
14
|
+
assert_equal <%= user_plural_name %>(:foo), <%= user_session_singular_name %>.user
|
15
|
+
assert_redirected_to root_url
|
16
|
+
end
|
17
|
+
|
18
|
+
test "create action should render new template when failed login" do
|
19
|
+
post :create, :<%= user_session_singular_name %> => { :login => "foo", :password => "wrong" }
|
20
|
+
assert_template 'new'
|
21
|
+
assert_nil <%= user_session_class_name %>.find
|
22
|
+
end
|
23
|
+
|
24
|
+
test "destroy action should destroy user session" do
|
25
|
+
set_session_for <%= user_plural_name %>(:foo)
|
26
|
+
assert <%= user_session_class_name %>.find
|
27
|
+
delete :destroy
|
28
|
+
assert_nil <%= user_session_class_name %>.find
|
29
|
+
assert_redirected_to login_url
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class <%= user_class_name %>Test < ActiveSupport::TestCase
|
4
|
+
def new_<%= user_singular_name %>(attributes = {})
|
5
|
+
attributes[:username] ||= 'foo'
|
6
|
+
attributes[:email] ||= 'foo@example.com'
|
7
|
+
attributes[:password] ||= 'abc123'
|
8
|
+
attributes[:password_confirmation] ||= attributes[:password]
|
9
|
+
<%= user_singular_name %> = <%= user_class_name %>.new(attributes)
|
10
|
+
<%= user_singular_name %>.valid? # run validations
|
11
|
+
<%= user_singular_name %>
|
12
|
+
end
|
13
|
+
|
14
|
+
def setup
|
15
|
+
<%= user_class_name %>.delete_all
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_valid
|
19
|
+
assert new_<%= user_singular_name %>.valid?
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_require_username
|
23
|
+
assert new_<%= user_singular_name %>(:username => '').errors.on(:username)
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_require_password
|
27
|
+
assert new_<%= user_singular_name %>(:password => '').errors.on(:password)
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_require_well_formed_email
|
31
|
+
assert new_<%= user_singular_name %>(:email => 'foo@bar@example.com').errors.on(:email)
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_validate_uniqueness_of_email
|
35
|
+
new_<%= user_singular_name %>(:email => 'bar@example.com').save!
|
36
|
+
assert new_<%= user_singular_name %>(:email => 'bar@example.com').errors.on(:email)
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_validate_uniqueness_of_username
|
40
|
+
new_<%= user_singular_name %>(:username => 'uniquename').save!
|
41
|
+
assert new_<%= user_singular_name %>(:username => 'uniquename').errors.on(:username)
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_validate_odd_characters_in_username
|
45
|
+
assert new_<%= user_singular_name %>(:username => 'odd ^&(@)').errors.on(:username)
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_validate_password_length
|
49
|
+
assert new_<%= user_singular_name %>(:password => 'bad').errors.on(:password)
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_require_matching_password_confirmation
|
53
|
+
assert new_<%= user_singular_name %>(:password_confirmation => 'nonmatching').errors.on(:password)
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_generate_password_hash_and_salt_on_create
|
57
|
+
<%= user_singular_name %> = new_<%= user_singular_name %>
|
58
|
+
<%= user_singular_name %>.save!
|
59
|
+
assert <%= user_singular_name %>.password_hash
|
60
|
+
assert <%= user_singular_name %>.password_salt
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_authenticate_by_username
|
64
|
+
<%= user_class_name %>.delete_all
|
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
|
+
def test_authenticate_by_email
|
71
|
+
<%= user_class_name %>.delete_all
|
72
|
+
<%= user_singular_name %> = new_<%= user_singular_name %>(:email => 'foo@bar.com', :password => 'secret')
|
73
|
+
<%= user_singular_name %>.save!
|
74
|
+
assert_equal <%= user_singular_name %>, <%= user_class_name %>.authenticate('foo@bar.com', 'secret')
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_authenticate_bad_username
|
78
|
+
assert_nil <%= user_class_name %>.authenticate('nonexisting', 'secret')
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_authenticate_bad_password
|
82
|
+
<%= user_class_name %>.delete_all
|
83
|
+
new_<%= user_singular_name %>(:username => 'foobar', :password => 'secret').save!
|
84
|
+
assert_nil <%= user_class_name %>.authenticate('foobar', 'badpassword')
|
85
|
+
end
|
86
|
+
end
|
@@ -0,0 +1,21 @@
|
|
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
|
+
assert_equal assigns['<%= user_singular_name %>'].id, session['<%= user_singular_name %>_id']
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
<%% title "Log in" %>
|
2
|
+
|
3
|
+
<p>Don't have an account? <%%= link_to "Sign up!", signup_path %></p>
|
4
|
+
|
5
|
+
<%% form_tag <%= user_session_plural_name %>_path do %>
|
6
|
+
<p>
|
7
|
+
<%%= label_tag :login, "Username or Email Address" %><br />
|
8
|
+
<%%= text_field_tag :login, params[:login] %>
|
9
|
+
</p>
|
10
|
+
<p>
|
11
|
+
<%%= label_tag :password %><br />
|
12
|
+
<%%= password_field_tag :password %>
|
13
|
+
</p>
|
14
|
+
<p><%%= submit_tag "Log in" %></p>
|
15
|
+
<%% end %>
|
@@ -0,0 +1,24 @@
|
|
1
|
+
<%% title "Sign up" %>
|
2
|
+
|
3
|
+
<p>Already have an account? <%%= link_to "Log in", login_path %>.</p>
|
4
|
+
|
5
|
+
<%% form_for @<%= user_singular_name %> do |f| %>
|
6
|
+
<%%= f.error_messages %>
|
7
|
+
<p>
|
8
|
+
<%%= f.label :username %><br />
|
9
|
+
<%%= f.text_field :username %>
|
10
|
+
</p>
|
11
|
+
<p>
|
12
|
+
<%%= f.label :email, "Email Address" %><br />
|
13
|
+
<%%= f.text_field :email %>
|
14
|
+
</p>
|
15
|
+
<p>
|
16
|
+
<%%= f.label :password %><br />
|
17
|
+
<%%= f.password_field :password %>
|
18
|
+
</p>
|
19
|
+
<p>
|
20
|
+
<%%= f.label :password_confirmation, "Confirm Password" %><br />
|
21
|
+
<%%= f.password_field :password_confirmation %>
|
22
|
+
</p>
|
23
|
+
<p><%%= f.submit "Sign up" %></p>
|
24
|
+
<%% end %>
|
@@ -0,0 +1,15 @@
|
|
1
|
+
- title t(:title)
|
2
|
+
|
3
|
+
- form_for @<%= user_session_singular_name %>, :url => object_path do |f|
|
4
|
+
= f.error_messages
|
5
|
+
- f.fieldset do
|
6
|
+
- f.field :login
|
7
|
+
- f.field do
|
8
|
+
= f.required :password
|
9
|
+
= f.password_field :password
|
10
|
+
- f.field do
|
11
|
+
%label
|
12
|
+
= f.submit t(:submit)
|
13
|
+
|
14
|
+
%ol.actions
|
15
|
+
%li.signup= link_to t(:signup), signup_path
|
@@ -0,0 +1,19 @@
|
|
1
|
+
- title t(:title)
|
2
|
+
|
3
|
+
- form_for @<%= user_singular_name %>, :url => collection_path do |f|
|
4
|
+
= f.error_messages
|
5
|
+
- f.fieldset do
|
6
|
+
- f.field :login
|
7
|
+
- f.field do
|
8
|
+
= f.required :password
|
9
|
+
= f.password_field :password
|
10
|
+
- f.field do
|
11
|
+
= f.required :password_confirmation
|
12
|
+
= f.password_field :password_confirmation
|
13
|
+
- f.field do
|
14
|
+
%label
|
15
|
+
= f.submit t(:submit)
|
16
|
+
= cancel_link
|
17
|
+
|
18
|
+
%ol.actions
|
19
|
+
%li.login= link_to t(:login), login_path
|