very_nifty_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.
- data/LICENSE +20 -0
- data/README.rdoc +17 -0
- data/lib/generators/nifty/authentication/authentication_generator.rb +152 -0
- data/lib/generators/nifty/authentication/templates/authentication.rb +61 -0
- data/lib/generators/nifty/authentication/templates/authlogic_session.rb +2 -0
- data/lib/generators/nifty/authentication/templates/migration.rb +20 -0
- data/lib/generators/nifty/authentication/templates/sessions_controller.rb +45 -0
- data/lib/generators/nifty/authentication/templates/sessions_helper.rb +2 -0
- data/lib/generators/nifty/authentication/templates/tests/rspec/sessions_controller.rb +39 -0
- data/lib/generators/nifty/authentication/templates/tests/rspec/user.rb +83 -0
- data/lib/generators/nifty/authentication/templates/tests/rspec/users_controller.rb +26 -0
- data/lib/generators/nifty/authentication/templates/tests/shoulda/sessions_controller.rb +40 -0
- data/lib/generators/nifty/authentication/templates/tests/shoulda/user.rb +85 -0
- data/lib/generators/nifty/authentication/templates/tests/shoulda/users_controller.rb +27 -0
- data/lib/generators/nifty/authentication/templates/tests/testunit/sessions_controller.rb +36 -0
- data/lib/generators/nifty/authentication/templates/tests/testunit/user.rb +88 -0
- data/lib/generators/nifty/authentication/templates/tests/testunit/users_controller.rb +23 -0
- data/lib/generators/nifty/authentication/templates/user.rb +42 -0
- data/lib/generators/nifty/authentication/templates/users_controller.rb +18 -0
- data/lib/generators/nifty/authentication/templates/users_helper.rb +2 -0
- data/lib/generators/nifty/config/config_generator.rb +29 -0
- data/lib/generators/nifty/layout/layout_generator.rb +28 -0
- data/lib/generators/nifty/layout/templates/helper.rb +22 -0
- data/lib/generators/nifty/scaffold/scaffold_generator.rb +247 -0
- data/lib/generators/nifty/scaffold/templates/actions/create.rb +9 -0
- data/lib/generators/nifty/scaffold/templates/actions/destroy.rb +6 -0
- data/lib/generators/nifty/scaffold/templates/actions/edit.rb +3 -0
- data/lib/generators/nifty/scaffold/templates/actions/index.rb +3 -0
- data/lib/generators/nifty/scaffold/templates/actions/new.rb +3 -0
- data/lib/generators/nifty/scaffold/templates/actions/show.rb +3 -0
- data/lib/generators/nifty/scaffold/templates/actions/update.rb +9 -0
- data/lib/generators/nifty/scaffold/templates/controller.rb +3 -0
- data/lib/generators/nifty/scaffold/templates/helper.rb +2 -0
- data/lib/generators/nifty/scaffold/templates/migration.rb +16 -0
- data/lib/generators/nifty/scaffold/templates/model.rb +3 -0
- data/lib/generators/nifty/scaffold/templates/tests/rspec/actions/create.rb +11 -0
- data/lib/generators/nifty/scaffold/templates/tests/rspec/actions/destroy.rb +6 -0
- data/lib/generators/nifty/scaffold/templates/tests/rspec/actions/edit.rb +4 -0
- data/lib/generators/nifty/scaffold/templates/tests/rspec/actions/index.rb +4 -0
- data/lib/generators/nifty/scaffold/templates/tests/rspec/actions/new.rb +4 -0
- data/lib/generators/nifty/scaffold/templates/tests/rspec/actions/show.rb +4 -0
- data/lib/generators/nifty/scaffold/templates/tests/rspec/actions/update.rb +11 -0
- data/lib/generators/nifty/scaffold/templates/tests/rspec/controller.rb +8 -0
- data/lib/generators/nifty/scaffold/templates/tests/rspec/model.rb +7 -0
- data/lib/generators/nifty/scaffold/templates/tests/shoulda/actions/create.rb +13 -0
- data/lib/generators/nifty/scaffold/templates/tests/shoulda/actions/destroy.rb +8 -0
- data/lib/generators/nifty/scaffold/templates/tests/shoulda/actions/edit.rb +6 -0
- data/lib/generators/nifty/scaffold/templates/tests/shoulda/actions/index.rb +6 -0
- data/lib/generators/nifty/scaffold/templates/tests/shoulda/actions/new.rb +6 -0
- data/lib/generators/nifty/scaffold/templates/tests/shoulda/actions/show.rb +6 -0
- data/lib/generators/nifty/scaffold/templates/tests/shoulda/actions/update.rb +13 -0
- data/lib/generators/nifty/scaffold/templates/tests/shoulda/controller.rb +5 -0
- data/lib/generators/nifty/scaffold/templates/tests/shoulda/model.rb +7 -0
- data/lib/generators/nifty/scaffold/templates/tests/testunit/actions/create.rb +11 -0
- data/lib/generators/nifty/scaffold/templates/tests/testunit/actions/destroy.rb +6 -0
- data/lib/generators/nifty/scaffold/templates/tests/testunit/actions/edit.rb +4 -0
- data/lib/generators/nifty/scaffold/templates/tests/testunit/actions/index.rb +4 -0
- data/lib/generators/nifty/scaffold/templates/tests/testunit/actions/new.rb +4 -0
- data/lib/generators/nifty/scaffold/templates/tests/testunit/actions/show.rb +4 -0
- data/lib/generators/nifty/scaffold/templates/tests/testunit/actions/update.rb +11 -0
- data/lib/generators/nifty/scaffold/templates/tests/testunit/controller.rb +5 -0
- data/lib/generators/nifty/scaffold/templates/tests/testunit/model.rb +7 -0
- data/lib/generators/nifty.rb +15 -0
- data/spec/spec_helper.rb +9 -0
- data/spec/very_nifty_generators_spec.rb +7 -0
- metadata +128 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Kristian Mandrup
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
= very_nifty_generators
|
2
|
+
|
3
|
+
Description goes here.
|
4
|
+
|
5
|
+
== Note on Patches/Pull Requests
|
6
|
+
|
7
|
+
* Fork the project.
|
8
|
+
* Make your feature addition or bug fix.
|
9
|
+
* Add tests for it. This is important so I don't break it in a
|
10
|
+
future version unintentionally.
|
11
|
+
* Commit, do not mess with rakefile, version, or history.
|
12
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
13
|
+
* Send me a pull request. Bonus points for topic branches.
|
14
|
+
|
15
|
+
== Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2010 Kristian Mandrup. See LICENSE for details.
|
@@ -0,0 +1,152 @@
|
|
1
|
+
require 'generators/nifty'
|
2
|
+
require 'rails/generators/migration'
|
3
|
+
|
4
|
+
module Nifty
|
5
|
+
module Generators
|
6
|
+
class AuthenticationGenerator < Base
|
7
|
+
include Rails::Generators::Migration
|
8
|
+
|
9
|
+
argument :user_name, :type => :string, :default => 'user', :banner => 'user_name'
|
10
|
+
argument :session_name, :type => :string, :default => '[[DEFAULT]]', :banner => 'sessions_controller_name'
|
11
|
+
|
12
|
+
class_option :testunit, :desc => 'Use test/unit for test files.', :group => 'Test framework',
|
13
|
+
:type => :boolean
|
14
|
+
class_option :rspec, :desc => 'Use RSpec for test files.', :group => 'Test framework',
|
15
|
+
:type => :boolean
|
16
|
+
class_option :shoulda, :desc => 'Use shoulda for test files.', :group => 'Test framework',
|
17
|
+
:type => :boolean
|
18
|
+
|
19
|
+
class_option :haml, :desc => 'Generate HAML views instead of ERB.', :type => :boolean
|
20
|
+
class_option :authlogic, :desc => 'Use Authlogic for authentication.', :type => :boolean
|
21
|
+
|
22
|
+
def create_model_files
|
23
|
+
template 'user.rb', "app/models/#{user_singular_name}.rb"
|
24
|
+
template 'authlogic_session.rb', "app/models/#{user_singular_name}_session.rb" if options.authlogic?
|
25
|
+
end
|
26
|
+
|
27
|
+
def create_controller_files
|
28
|
+
template 'users_controller.rb', "app/controllers/#{user_plural_name}_controller.rb"
|
29
|
+
template 'sessions_controller.rb', "app/controllers/#{session_plural_name}_controller.rb"
|
30
|
+
end
|
31
|
+
|
32
|
+
def crete_helper_files
|
33
|
+
template 'users_helper.rb', "app/helpers/#{user_plural_name}_helper.rb"
|
34
|
+
template 'sessions_helper.rb', "app/helpers/#{session_plural_name}_helper.rb"
|
35
|
+
end
|
36
|
+
|
37
|
+
def create_view_files
|
38
|
+
template "views/#{view_language}/signup.html.#{view_language}", "app/views/#{user_plural_name}/new.html.#{view_language}"
|
39
|
+
template "views/#{view_language}/login.html.#{view_language}", "app/views/#{session_plural_name}/new.html.#{view_language}"
|
40
|
+
end
|
41
|
+
|
42
|
+
def create_lib_files
|
43
|
+
template 'authentication.rb', 'lib/authentication.rb'
|
44
|
+
end
|
45
|
+
|
46
|
+
def create_routes
|
47
|
+
route "resources #{user_plural_name.to_sym.inspect}"
|
48
|
+
route "resources #{session_plural_name.to_sym.inspect}"
|
49
|
+
route "match 'login' => '#{session_plural_name}#new', :as => :login"
|
50
|
+
route "match 'logout' => '#{session_plural_name}#destroy', :as => :logout"
|
51
|
+
route "match 'signup' => '#{user_plural_name}#new', :as => :signup"
|
52
|
+
end
|
53
|
+
|
54
|
+
def create_migration
|
55
|
+
migration_template 'migration.rb', "db/migrate/create_#{user_plural_name}.rb"
|
56
|
+
end
|
57
|
+
|
58
|
+
def create_include_line
|
59
|
+
inject_into_class 'app/controllers/application_controller.rb', 'ApplicationController', ' include Authentication'
|
60
|
+
end
|
61
|
+
|
62
|
+
def create_test_files
|
63
|
+
if test_framework == :rspec
|
64
|
+
template 'fixtures.yml', "spec/fixtures/#{user_plural_name}.yml"
|
65
|
+
template 'tests/rspec/user.rb', "spec/models/#{user_singular_name}_spec.rb"
|
66
|
+
template 'tests/rspec/users_controller.rb', "spec/controllers/#{user_plural_name}_controller_spec.rb"
|
67
|
+
template 'tests/rspec/sessions_controller.rb', "spec/controllers/#{session_plural_name}_controller_spec.rb"
|
68
|
+
else
|
69
|
+
template 'fixtures.yml', "test/fixtures/#{user_plural_name}.yml"
|
70
|
+
template "tests/#{test_framework}/user.rb", "test/unit/#{user_singular_name}_test.rb"
|
71
|
+
template "tests/#{test_framework}/users_controller.rb", "test/functional/#{user_plural_name}_controller_test.rb"
|
72
|
+
template "tests/#{test_framework}/sessions_controller.rb", "test/functional/#{session_plural_name}_controller_test.rb"
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
no_tasks do
|
77
|
+
def session_name
|
78
|
+
@_session_name ||= @session_name == '[[DEFAULT]]' ?
|
79
|
+
(options.authlogic? ? user_name + '_session' : 'session') :
|
80
|
+
@session_name
|
81
|
+
end
|
82
|
+
|
83
|
+
def user_singular_name
|
84
|
+
user_name.underscore
|
85
|
+
end
|
86
|
+
|
87
|
+
def user_plural_name
|
88
|
+
user_singular_name.pluralize
|
89
|
+
end
|
90
|
+
|
91
|
+
def user_class_name
|
92
|
+
user_name.camelize
|
93
|
+
end
|
94
|
+
|
95
|
+
def user_plural_class_name
|
96
|
+
user_plural_name.camelize
|
97
|
+
end
|
98
|
+
|
99
|
+
def session_singular_name
|
100
|
+
session_name.underscore
|
101
|
+
end
|
102
|
+
|
103
|
+
def session_plural_name
|
104
|
+
session_singular_name.pluralize
|
105
|
+
end
|
106
|
+
|
107
|
+
def session_class_name
|
108
|
+
session_name.camelize
|
109
|
+
end
|
110
|
+
|
111
|
+
def session_plural_class_name
|
112
|
+
session_plural_name.camelize
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
protected
|
117
|
+
|
118
|
+
no_tasks do
|
119
|
+
def view_language
|
120
|
+
options.haml? ? 'haml' : 'erb'
|
121
|
+
end
|
122
|
+
|
123
|
+
def test_framework
|
124
|
+
return @test_framework if defined?(@test_framework)
|
125
|
+
if options.testunit?
|
126
|
+
return @test_framework = :testunit
|
127
|
+
elsif options.rspec?
|
128
|
+
return @test_framework = :rspec
|
129
|
+
elsif options.shoulda?
|
130
|
+
return @test_framework = :shoulda
|
131
|
+
else
|
132
|
+
return @test_framework = File.exist?(destination_path('spec')) ? :rspec : :testunit
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
def destination_path(path)
|
137
|
+
File.join(destination_root, path)
|
138
|
+
end
|
139
|
+
|
140
|
+
# FIXME: Should be proxied to ActiveRecord::Generators::Base
|
141
|
+
# Implement the required interface for Rails::Generators::Migration.
|
142
|
+
def next_migration_number(dirname) #:nodoc:
|
143
|
+
if ActiveRecord::Base.timestamped_migrations
|
144
|
+
Time.now.utc.strftime("%Y%m%d%H%M%S")
|
145
|
+
else
|
146
|
+
"%.3d" % (current_migration_number(dirname) + 1)
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
@@ -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] = "You must first log in or sign up before accessing this page."
|
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,20 @@
|
|
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
|
+
end
|
16
|
+
|
17
|
+
def self.down
|
18
|
+
drop_table :<%= user_plural_name %>
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
class <%= session_plural_class_name %>Controller < ApplicationController
|
2
|
+
<%- if options.authlogic? -%>
|
3
|
+
def new
|
4
|
+
@<%= session_singular_name %> = <%= session_class_name %>.new
|
5
|
+
end
|
6
|
+
|
7
|
+
def create
|
8
|
+
@<%= session_singular_name %> = <%= session_class_name %>.new(params[:<%= session_singular_name %>])
|
9
|
+
if @<%= session_singular_name %>.save
|
10
|
+
flash[:notice] = "Logged in successfully."
|
11
|
+
redirect_to_target_or_default(root_url)
|
12
|
+
else
|
13
|
+
render :action => 'new'
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def destroy
|
18
|
+
@<%= session_singular_name %> = <%= session_class_name %>.find
|
19
|
+
@<%= session_singular_name %>.destroy
|
20
|
+
flash[:notice] = "You have been logged out."
|
21
|
+
redirect_to root_url
|
22
|
+
end
|
23
|
+
<%- else -%>
|
24
|
+
def new
|
25
|
+
end
|
26
|
+
|
27
|
+
def create
|
28
|
+
<%= user_singular_name %> = <%= user_class_name %>.authenticate(params[:login], params[:password])
|
29
|
+
if <%= user_singular_name %>
|
30
|
+
session[:<%= user_singular_name %>_id] = <%= user_singular_name %>.id
|
31
|
+
flash[:notice] = "Logged in successfully."
|
32
|
+
redirect_to_target_or_default(root_url)
|
33
|
+
else
|
34
|
+
flash.now[:error] = "Invalid login or password."
|
35
|
+
render :action => 'new'
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def destroy
|
40
|
+
session[:<%= user_singular_name %>_id] = nil
|
41
|
+
flash[:notice] = "You have been logged out."
|
42
|
+
redirect_to root_url
|
43
|
+
end
|
44
|
+
<%- end -%>
|
45
|
+
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
|