twilson63-nifty-generators 0.2.5
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +71 -0
- data/LICENSE +20 -0
- data/Manifest +103 -0
- data/README.rdoc +94 -0
- data/Rakefile +15 -0
- data/TODO +7 -0
- data/lib/nifty_generators.rb +3 -0
- data/nifty-generators.gemspec +32 -0
- data/rails_generators/nifty_authentication/USAGE +46 -0
- data/rails_generators/nifty_authentication/lib/insert_commands.rb +74 -0
- data/rails_generators/nifty_authentication/nifty_authentication_generator.rb +118 -0
- data/rails_generators/nifty_authentication/templates/authentication.rb +36 -0
- data/rails_generators/nifty_authentication/templates/fixtures.yml +12 -0
- data/rails_generators/nifty_authentication/templates/migration.rb +15 -0
- data/rails_generators/nifty_authentication/templates/sessions_controller.rb +22 -0
- data/rails_generators/nifty_authentication/templates/sessions_helper.rb +2 -0
- data/rails_generators/nifty_authentication/templates/tests/rspec/sessions_controller.rb +25 -0
- data/rails_generators/nifty_authentication/templates/tests/rspec/user.rb +81 -0
- data/rails_generators/nifty_authentication/templates/tests/rspec/users_controller.rb +24 -0
- data/rails_generators/nifty_authentication/templates/tests/shoulda/sessions_controller.rb +26 -0
- data/rails_generators/nifty_authentication/templates/tests/shoulda/user.rb +83 -0
- data/rails_generators/nifty_authentication/templates/tests/shoulda/users_controller.rb +25 -0
- data/rails_generators/nifty_authentication/templates/tests/testunit/sessions_controller.rb +22 -0
- data/rails_generators/nifty_authentication/templates/tests/testunit/user.rb +86 -0
- data/rails_generators/nifty_authentication/templates/tests/testunit/users_controller.rb +21 -0
- data/rails_generators/nifty_authentication/templates/user.rb +36 -0
- data/rails_generators/nifty_authentication/templates/users_controller.rb +16 -0
- data/rails_generators/nifty_authentication/templates/users_helper.rb +2 -0
- data/rails_generators/nifty_authentication/templates/views/erb/login.html.erb +15 -0
- data/rails_generators/nifty_authentication/templates/views/erb/signup.html.erb +24 -0
- data/rails_generators/nifty_authentication/templates/views/haml/login.html.haml +15 -0
- data/rails_generators/nifty_authentication/templates/views/haml/signup.html.haml +24 -0
- data/rails_generators/nifty_config/USAGE +23 -0
- data/rails_generators/nifty_config/nifty_config_generator.rb +32 -0
- data/rails_generators/nifty_config/templates/config.yml +8 -0
- data/rails_generators/nifty_config/templates/load_config.rb +2 -0
- data/rails_generators/nifty_layout/USAGE +25 -0
- data/rails_generators/nifty_layout/nifty_layout_generator.rb +44 -0
- data/rails_generators/nifty_layout/templates/helper.rb +23 -0
- data/rails_generators/nifty_layout/templates/layout.html.erb +22 -0
- data/rails_generators/nifty_layout/templates/layout.html.haml +19 -0
- data/rails_generators/nifty_layout/templates/stylesheet.css +81 -0
- data/rails_generators/nifty_layout/templates/stylesheet.sass +67 -0
- data/rails_generators/nifty_scaffold/USAGE +51 -0
- data/rails_generators/nifty_scaffold/nifty_scaffold_generator.rb +227 -0
- data/rails_generators/nifty_scaffold/templates/actions/create.rb +9 -0
- data/rails_generators/nifty_scaffold/templates/actions/destroy.rb +6 -0
- data/rails_generators/nifty_scaffold/templates/actions/edit.rb +3 -0
- data/rails_generators/nifty_scaffold/templates/actions/index.rb +3 -0
- data/rails_generators/nifty_scaffold/templates/actions/new.rb +3 -0
- data/rails_generators/nifty_scaffold/templates/actions/show.rb +3 -0
- data/rails_generators/nifty_scaffold/templates/actions/update.rb +9 -0
- data/rails_generators/nifty_scaffold/templates/controller.rb +3 -0
- data/rails_generators/nifty_scaffold/templates/fixtures.yml +9 -0
- data/rails_generators/nifty_scaffold/templates/helper.rb +2 -0
- data/rails_generators/nifty_scaffold/templates/migration.rb +16 -0
- data/rails_generators/nifty_scaffold/templates/model.rb +2 -0
- data/rails_generators/nifty_scaffold/templates/tests/rspec/actions/create.rb +11 -0
- data/rails_generators/nifty_scaffold/templates/tests/rspec/actions/destroy.rb +6 -0
- data/rails_generators/nifty_scaffold/templates/tests/rspec/actions/edit.rb +4 -0
- data/rails_generators/nifty_scaffold/templates/tests/rspec/actions/index.rb +4 -0
- data/rails_generators/nifty_scaffold/templates/tests/rspec/actions/new.rb +4 -0
- data/rails_generators/nifty_scaffold/templates/tests/rspec/actions/show.rb +4 -0
- data/rails_generators/nifty_scaffold/templates/tests/rspec/actions/update.rb +11 -0
- data/rails_generators/nifty_scaffold/templates/tests/rspec/controller.rb +8 -0
- data/rails_generators/nifty_scaffold/templates/tests/rspec/model.rb +7 -0
- data/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/create.rb +13 -0
- data/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/destroy.rb +8 -0
- data/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/edit.rb +6 -0
- data/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/index.rb +6 -0
- data/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/new.rb +6 -0
- data/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/show.rb +6 -0
- data/rails_generators/nifty_scaffold/templates/tests/shoulda/actions/update.rb +13 -0
- data/rails_generators/nifty_scaffold/templates/tests/shoulda/controller.rb +5 -0
- data/rails_generators/nifty_scaffold/templates/tests/shoulda/model.rb +7 -0
- data/rails_generators/nifty_scaffold/templates/tests/testunit/actions/create.rb +11 -0
- data/rails_generators/nifty_scaffold/templates/tests/testunit/actions/destroy.rb +6 -0
- data/rails_generators/nifty_scaffold/templates/tests/testunit/actions/edit.rb +4 -0
- data/rails_generators/nifty_scaffold/templates/tests/testunit/actions/index.rb +4 -0
- data/rails_generators/nifty_scaffold/templates/tests/testunit/actions/new.rb +4 -0
- data/rails_generators/nifty_scaffold/templates/tests/testunit/actions/show.rb +4 -0
- data/rails_generators/nifty_scaffold/templates/tests/testunit/actions/update.rb +11 -0
- data/rails_generators/nifty_scaffold/templates/tests/testunit/controller.rb +5 -0
- data/rails_generators/nifty_scaffold/templates/tests/testunit/model.rb +7 -0
- data/rails_generators/nifty_scaffold/templates/views/erb/_form.html.erb +10 -0
- data/rails_generators/nifty_scaffold/templates/views/erb/edit.html.erb +14 -0
- data/rails_generators/nifty_scaffold/templates/views/erb/index.html.erb +29 -0
- data/rails_generators/nifty_scaffold/templates/views/erb/new.html.erb +7 -0
- data/rails_generators/nifty_scaffold/templates/views/erb/show.html.erb +20 -0
- data/rails_generators/nifty_scaffold/templates/views/haml/_form.html.haml +11 -0
- data/rails_generators/nifty_scaffold/templates/views/haml/edit.html.haml +1 -0
- data/rails_generators/nifty_scaffold/templates/views/haml/index.html.haml +1 -0
- data/rails_generators/nifty_scaffold/templates/views/haml/new.html.haml +2 -0
- data/rails_generators/nifty_scaffold/templates/views/haml/show.html.haml +6 -0
- data/tasks/deployment.rake +2 -0
- data/test/test_helper.rb +117 -0
- data/test/test_nifty_authentication_generator.rb +160 -0
- data/test/test_nifty_config_generator.rb +37 -0
- data/test/test_nifty_layout_generator.rb +42 -0
- data/test/test_nifty_scaffold_generator.rb +532 -0
- metadata +166 -0
@@ -0,0 +1,118 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + "/lib/insert_commands.rb")
|
2
|
+
class NiftyAuthenticationGenerator < Rails::Generator::Base
|
3
|
+
attr_accessor :user_name, :sessions_name
|
4
|
+
|
5
|
+
def initialize(runtime_args, runtime_options = {})
|
6
|
+
super
|
7
|
+
|
8
|
+
@user_name = @args[0] || 'user'
|
9
|
+
@sessions_name = @args[1] || 'sessions'
|
10
|
+
end
|
11
|
+
|
12
|
+
def manifest
|
13
|
+
record do |m|
|
14
|
+
m.directory "app/models"
|
15
|
+
m.directory "app/controllers"
|
16
|
+
m.directory "app/helpers"
|
17
|
+
m.directory "app/views"
|
18
|
+
m.directory "lib"
|
19
|
+
|
20
|
+
m.directory "app/views/#{user_plural_name}"
|
21
|
+
m.template "user.rb", "app/models/#{user_singular_name}.rb"
|
22
|
+
m.template "users_controller.rb", "app/controllers/#{user_plural_name}_controller.rb"
|
23
|
+
m.template "users_helper.rb", "app/helpers/#{user_plural_name}_helper.rb"
|
24
|
+
m.template "views/#{view_language}/signup.html.#{view_language}", "app/views/#{user_plural_name}/new.html.#{view_language}"
|
25
|
+
|
26
|
+
m.directory "app/views/#{sessions_underscore_name}"
|
27
|
+
m.template "sessions_controller.rb", "app/controllers/#{sessions_underscore_name}_controller.rb"
|
28
|
+
m.template "sessions_helper.rb", "app/helpers/#{sessions_underscore_name}_helper.rb"
|
29
|
+
m.template "views/#{view_language}/login.html.#{view_language}", "app/views/#{sessions_underscore_name}/new.html.#{view_language}"
|
30
|
+
|
31
|
+
m.template "authentication.rb", "lib/authentication.rb"
|
32
|
+
m.migration_template "migration.rb", "db/migrate", :migration_file_name => "create_#{user_plural_name}"
|
33
|
+
|
34
|
+
m.route_resources user_plural_name
|
35
|
+
m.route_resources sessions_underscore_name
|
36
|
+
m.route_name :login, 'login', :controller => sessions_underscore_name, :action => 'new'
|
37
|
+
m.route_name :logout, 'logout', :controller => sessions_underscore_name, :action => 'destroy'
|
38
|
+
m.route_name :signup, 'signup', :controller => user_plural_name, :action => 'new'
|
39
|
+
|
40
|
+
m.insert_into "app/controllers/#{application_controller_name}.rb", 'include Authentication'
|
41
|
+
|
42
|
+
if test_framework == :rspec
|
43
|
+
m.directory "spec"
|
44
|
+
m.directory "spec/fixtures"
|
45
|
+
m.directory "spec/controllers"
|
46
|
+
m.directory "spec/models"
|
47
|
+
m.template "fixtures.yml", "spec/fixtures/#{user_plural_name}.yml"
|
48
|
+
m.template "tests/rspec/user.rb", "spec/models/#{user_singular_name}_spec.rb"
|
49
|
+
m.template "tests/rspec/users_controller.rb", "spec/controllers/#{user_plural_name}_controller_spec.rb"
|
50
|
+
m.template "tests/rspec/sessions_controller.rb", "spec/controllers/#{sessions_underscore_name}_controller_spec.rb"
|
51
|
+
else
|
52
|
+
m.directory "test"
|
53
|
+
m.directory "test/fixtures"
|
54
|
+
m.directory "test/functional"
|
55
|
+
m.directory "test/unit"
|
56
|
+
m.template "fixtures.yml", "test/fixtures/#{user_plural_name}.yml"
|
57
|
+
m.template "tests/#{test_framework}/user.rb", "test/unit/#{user_singular_name}_test.rb"
|
58
|
+
m.template "tests/#{test_framework}/users_controller.rb", "test/functional/#{user_plural_name}_controller_test.rb"
|
59
|
+
m.template "tests/#{test_framework}/sessions_controller.rb", "test/functional/#{sessions_underscore_name}_controller_test.rb"
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def user_singular_name
|
65
|
+
user_name.underscore
|
66
|
+
end
|
67
|
+
|
68
|
+
def user_plural_name
|
69
|
+
user_singular_name.pluralize
|
70
|
+
end
|
71
|
+
|
72
|
+
def user_class_name
|
73
|
+
user_name.camelize
|
74
|
+
end
|
75
|
+
|
76
|
+
def user_plural_class_name
|
77
|
+
user_plural_name.camelize
|
78
|
+
end
|
79
|
+
|
80
|
+
def sessions_underscore_name
|
81
|
+
sessions_name.underscore
|
82
|
+
end
|
83
|
+
|
84
|
+
def sessions_class_name
|
85
|
+
sessions_name.camelize
|
86
|
+
end
|
87
|
+
|
88
|
+
def application_controller_name
|
89
|
+
Rails.version >= '2.3.0' ? 'application_controller' : 'application'
|
90
|
+
end
|
91
|
+
|
92
|
+
protected
|
93
|
+
|
94
|
+
def view_language
|
95
|
+
options[:haml] ? 'haml' : 'erb'
|
96
|
+
end
|
97
|
+
|
98
|
+
def test_framework
|
99
|
+
options[:test_framework] ||= File.exist?(destination_path("spec")) ? :rspec : :testunit
|
100
|
+
end
|
101
|
+
|
102
|
+
def add_options!(opt)
|
103
|
+
opt.separator ''
|
104
|
+
opt.separator 'Options:'
|
105
|
+
opt.on("--testunit", "Use test/unit for test files.") { options[:test_framework] = :testunit }
|
106
|
+
opt.on("--rspec", "Use RSpec for test files.") { options[:test_framework] = :rspec }
|
107
|
+
opt.on("--shoulda", "Use Shoulda for test files.") { options[:test_framework] = :shoulda }
|
108
|
+
opt.on("--haml", "Generate HAML views instead of ERB.") { |v| options[:haml] = true }
|
109
|
+
end
|
110
|
+
|
111
|
+
def banner
|
112
|
+
<<-EOS
|
113
|
+
Creates user model and controllers to handle registration and authentication.
|
114
|
+
|
115
|
+
USAGE: #{$0} #{spec.name} [user_name] [sessions_controller_name]
|
116
|
+
EOS
|
117
|
+
end
|
118
|
+
end
|
@@ -0,0 +1,36 @@
|
|
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.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?
|
20
|
+
end
|
21
|
+
|
22
|
+
def current_<%= user_singular_name %>
|
23
|
+
@current_<%= user_singular_name %> ||= <%= user_class_name %>.find(session[:<%= user_singular_name %>_id]) if session[:<%= user_singular_name %>_id]
|
24
|
+
end
|
25
|
+
|
26
|
+
def logged_in?
|
27
|
+
current_<%= user_singular_name %>
|
28
|
+
end
|
29
|
+
|
30
|
+
def login_required
|
31
|
+
unless logged_in?
|
32
|
+
flash[:error] = "You must first log in or sign up before accessing this page."
|
33
|
+
redirect_to login_url
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# password: "secret"
|
2
|
+
one:
|
3
|
+
username: foo
|
4
|
+
email: foo@example.com
|
5
|
+
password_hash: 3488f5f7efecab14b91eb96169e5e1ee518a569f
|
6
|
+
password_salt: bef65e058905c379436d80d1a32e7374b139e7b0
|
7
|
+
|
8
|
+
two:
|
9
|
+
username: bar
|
10
|
+
email: bar@example.com
|
11
|
+
password_hash: 3488f5f7efecab14b91eb96169e5e1ee518a569f
|
12
|
+
password_salt: bef65e058905c379436d80d1a32e7374b139e7b0
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class Create<%= user_plural_class_name %> < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table :<%= user_plural_name %> do |t|
|
4
|
+
t.column :username, :string
|
5
|
+
t.column :email, :string
|
6
|
+
t.column :password_hash, :string
|
7
|
+
t.column :password_salt, :string
|
8
|
+
t.timestamps
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.down
|
13
|
+
drop_table :<%= user_plural_name %>
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
class <%= sessions_class_name %>Controller < ApplicationController
|
2
|
+
def new
|
3
|
+
end
|
4
|
+
|
5
|
+
def create
|
6
|
+
<%= user_singular_name %> = <%= user_class_name %>.authenticate(params[:login], params[:password])
|
7
|
+
if <%= user_singular_name %>
|
8
|
+
session[:<%= user_singular_name %>_id] = <%= user_singular_name %>.id
|
9
|
+
flash[:notice] = "Logged in successfully."
|
10
|
+
redirect_to root_url
|
11
|
+
else
|
12
|
+
flash.now[:error] = "Invalid login or password."
|
13
|
+
render :action => 'new'
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def destroy
|
18
|
+
session[:<%= user_singular_name %>_id] = nil
|
19
|
+
flash[:notice] = "You have been logged out."
|
20
|
+
redirect_to root_url
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
describe <%= sessions_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 authentication is invalid" do
|
13
|
+
<%= user_class_name %>.stubs(:authenticate).returns(nil)
|
14
|
+
post :create
|
15
|
+
response.should render_template(:new)
|
16
|
+
session['<%= user_singular_name %>_id'].should be_nil
|
17
|
+
end
|
18
|
+
|
19
|
+
it "create action should redirect when authentication is valid" do
|
20
|
+
<%= user_class_name %>.stubs(:authenticate).returns(<%= user_class_name %>.first)
|
21
|
+
post :create
|
22
|
+
response.should redirect_to(root_url)
|
23
|
+
session['<%= user_singular_name %>_id'].should == <%= user_class_name %>.first.id
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
describe <%= user_class_name %> do
|
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_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 username" do
|
21
|
+
new_<%= user_singular_name %>(:username => '').should have(1).error_on(:username)
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should require password" do
|
25
|
+
new_<%= user_singular_name %>(:password => '').should have(1).error_on(:password)
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should require well formed email" do
|
29
|
+
new_<%= user_singular_name %>(:email => 'foo@bar@example.com').should have(1).error_on(:email)
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should validate uniqueness of email" do
|
33
|
+
new_<%= user_singular_name %>(:email => 'bar@example.com').save!
|
34
|
+
new_<%= user_singular_name %>(:email => 'bar@example.com').should have(1).error_on(:email)
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should validate uniqueness of username" do
|
38
|
+
new_<%= user_singular_name %>(:username => 'uniquename').save!
|
39
|
+
new_<%= user_singular_name %>(:username => 'uniquename').should have(1).error_on(:username)
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should not allow odd characters in username" do
|
43
|
+
new_<%= user_singular_name %>(:username => 'odd ^&(@)').should have(1).error_on(:username)
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should validate password is longer than 3 characters" do
|
47
|
+
new_<%= user_singular_name %>(:password => 'bad').should have(1).error_on(:password)
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should require matching password confirmation" do
|
51
|
+
new_<%= user_singular_name %>(:password_confirmation => 'nonmatching').should have(1).error_on(:password)
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should generate password hash and salt on create" do
|
55
|
+
<%= user_singular_name %> = new_<%= user_singular_name %>
|
56
|
+
<%= user_singular_name %>.save!
|
57
|
+
<%= user_singular_name %>.password_hash.should_not be_nil
|
58
|
+
<%= user_singular_name %>.password_salt.should_not be_nil
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should authenticate by username" do
|
62
|
+
<%= user_singular_name %> = new_<%= user_singular_name %>(:username => 'foobar', :password => 'secret')
|
63
|
+
<%= user_singular_name %>.save!
|
64
|
+
<%= user_class_name %>.authenticate('foobar', 'secret').should == <%= user_singular_name %>
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should authenticate by email" do
|
68
|
+
<%= user_singular_name %> = new_<%= user_singular_name %>(:email => 'foo@bar.com', :password => 'secret')
|
69
|
+
<%= user_singular_name %>.save!
|
70
|
+
<%= user_class_name %>.authenticate('foo@bar.com', 'secret').should == <%= user_singular_name %>
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should not authenticate bad username" do
|
74
|
+
<%= user_class_name %>.authenticate('nonexisting', 'secret').should be_nil
|
75
|
+
end
|
76
|
+
|
77
|
+
it "should not authenticate bad password" do
|
78
|
+
new_<%= user_singular_name %>(:username => 'foobar', :password => 'secret').save!
|
79
|
+
<%= user_class_name %>.authenticate('foobar', 'badpassword').should be_nil
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,24 @@
|
|
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
|
+
session['<%= user_singular_name %>_id'].should == assigns['<%= user_singular_name %>'].id
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class <%= sessions_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 authentication is invalid" do
|
13
|
+
<%= user_class_name %>.stubs(:authenticate).returns(nil)
|
14
|
+
post :create
|
15
|
+
assert_template 'new'
|
16
|
+
assert_nil session['<%= user_singular_name %>_id']
|
17
|
+
end
|
18
|
+
|
19
|
+
should "redirect when authentication is valid" do
|
20
|
+
<%= user_class_name %>.stubs(:authenticate).returns(<%= user_class_name %>.first)
|
21
|
+
post :create
|
22
|
+
assert_redirected_to root_url
|
23
|
+
assert_equal <%= user_class_name %>.first.id, session['<%= user_singular_name %>_id']
|
24
|
+
end
|
25
|
+
end
|
26
|
+
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
|