dust-generators 0.0.1.pre2 → 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. data/Rakefile +11 -10
  2. data/lib/generators/dust/authentication/USAGE +50 -0
  3. data/lib/generators/dust/authentication/authentication_generator.rb +154 -0
  4. data/lib/generators/dust/authentication/templates/authlogic_session.rb +2 -0
  5. data/lib/generators/dust/authentication/templates/controller_authentication.rb +61 -0
  6. data/lib/generators/dust/authentication/templates/fixtures.yml +24 -0
  7. data/lib/generators/dust/authentication/templates/migration.rb +20 -0
  8. data/lib/generators/dust/authentication/templates/sessions_controller.rb +45 -0
  9. data/lib/generators/dust/authentication/templates/sessions_helper.rb +2 -0
  10. data/lib/generators/dust/authentication/templates/tests/rspec/sessions_controller.rb +39 -0
  11. data/lib/generators/dust/authentication/templates/tests/rspec/user.rb +83 -0
  12. data/lib/generators/dust/authentication/templates/tests/rspec/users_controller.rb +56 -0
  13. data/lib/generators/dust/authentication/templates/tests/shoulda/sessions_controller.rb +40 -0
  14. data/lib/generators/dust/authentication/templates/tests/shoulda/user.rb +85 -0
  15. data/lib/generators/dust/authentication/templates/tests/shoulda/users_controller.rb +61 -0
  16. data/lib/generators/dust/authentication/templates/tests/testunit/sessions_controller.rb +36 -0
  17. data/lib/generators/dust/authentication/templates/tests/testunit/user.rb +88 -0
  18. data/lib/generators/dust/authentication/templates/tests/testunit/users_controller.rb +53 -0
  19. data/lib/generators/dust/authentication/templates/user.rb +42 -0
  20. data/lib/generators/dust/authentication/templates/users_controller.rb +34 -0
  21. data/lib/generators/dust/authentication/templates/users_helper.rb +2 -0
  22. data/lib/generators/dust/authentication/templates/views/erb/_form.html.erb +20 -0
  23. data/lib/generators/dust/authentication/templates/views/erb/edit.html.erb +3 -0
  24. data/lib/generators/dust/authentication/templates/views/erb/login.html.erb +30 -0
  25. data/lib/generators/dust/authentication/templates/views/erb/signup.html.erb +5 -0
  26. data/lib/generators/dust/authentication/templates/views/haml/_form.html.haml +20 -0
  27. data/lib/generators/dust/authentication/templates/views/haml/edit.html.haml +3 -0
  28. data/lib/generators/dust/authentication/templates/views/haml/login.html.haml +30 -0
  29. data/lib/generators/dust/authentication/templates/views/haml/signup.html.haml +5 -0
  30. data/lib/generators/dust/config/config_generator.rb +1 -1
  31. data/lib/generators/dust/layout/USAGE +25 -0
  32. data/lib/generators/dust/layout/layout_generator.rb +29 -0
  33. data/lib/generators/dust/layout/templates/error_messages_helper.rb +23 -0
  34. data/lib/generators/dust/layout/templates/layout.html.erb +19 -0
  35. data/lib/generators/dust/layout/templates/layout.html.haml +21 -0
  36. data/lib/generators/dust/layout/templates/layout_helper.rb +22 -0
  37. data/lib/generators/dust/layout/templates/stylesheet.css +75 -0
  38. data/lib/generators/dust/layout/templates/stylesheet.sass +66 -0
  39. data/lib/generators/dust/scaffold/scaffold_generator.rb +1 -3
  40. data/lib/generators/dust/scaffold/templates/actions/index.rb +1 -1
  41. data/lib/generators/dust/scaffold/templates/model.rb +0 -12
  42. data/lib/generators/dust/scaffold/templates/views/erb/_form.html.erb +4 -10
  43. data/lib/generators/dust/scaffold/templates/views/erb/edit.html.erb +12 -2
  44. data/lib/generators/dust/scaffold/templates/views/erb/index.html.erb +26 -21
  45. data/lib/generators/dust/scaffold/templates/views/erb/new.html.erb +3 -1
  46. data/lib/generators/dust/scaffold/templates/views/erb/show.html.erb +18 -20
  47. metadata +43 -12
  48. data/lib/dust/version.rb +0 -10
  49. data/lib/generators/dust/scaffold/templates/views/erb/_search.html.erb +0 -6
data/Rakefile CHANGED
@@ -1,21 +1,19 @@
1
1
  require 'rubygems'
2
2
  require 'bundler'
3
- require './lib/dust/version.rb'
4
-
5
3
  begin
6
4
  Bundler.setup(:default, :development)
7
- rescue Bundler::BundlerError => e
8
- $stderr.puts e.message
9
- $stderr.puts "Run `bundle install` to install missing gems"
10
- exit e.status_code
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
11
9
  end
12
-
13
10
  require 'rake'
14
- require 'jeweler'
15
11
 
12
+ require 'jeweler'
16
13
  Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
15
  gem.name = "dust-generators"
18
- gem.version = Dust::Version::STRING
16
+ gem.version = "0.0.1"
19
17
  gem.homepage = "http://github.com/rossnelson/dust-generators"
20
18
  gem.license = "MIT"
21
19
  gem.summary = %Q{Generators for DustCMS}
@@ -23,8 +21,11 @@ Jeweler::Tasks.new do |gem|
23
21
  gem.email = "axcess1@me.com"
24
22
  gem.authors = ["rossnelson"]
25
23
  gem.files = FileList["[A-Z]*", "{lib}/**/*"]
24
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
25
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
26
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
27
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
26
28
  end
27
-
28
29
  Jeweler::RubygemsDotOrgTasks.new
29
30
 
30
31
  require 'rake/testtask'
@@ -0,0 +1,50 @@
1
+ Description:
2
+ Generates a user model, users controller, and sessions controller. The
3
+ users controller handles the registration and the sessions controller
4
+ handles authentication. This is similar to restful_authentication, but
5
+ simpler.
6
+
7
+ IMPORTANT: This generator uses the "title" helper method which is generated
8
+ by the dust_layout generator. You may want to run that generator first.
9
+
10
+ Usage:
11
+ If you do not pass any arguments, the model name will default to "user", and
12
+ the authentication controller will default to "session". You can override
13
+ each of these respectively by passing one or two arguments. Either name can
14
+ be CamelCased or under_scored.
15
+
16
+ Make sure to setup the authlogic gem if you are using that option.
17
+
18
+ gem "authlogic" # in Gemfile
19
+
20
+ Examples:
21
+ rails generate dust:authentication
22
+
23
+ Creates user model, users_controller, and sessions_controller.
24
+
25
+ rails generate dust:authentication account
26
+
27
+ Creates account model, accounts_controller, and sessions_controller.
28
+
29
+ rails generate dust:authentication Account UserSession
30
+
31
+ Creates account model, accounts_controller, and user_sessions_controller.
32
+
33
+ Methods:
34
+ There are several methods generated which you can use in your application.
35
+ Here's a common example of what you might add to your layout.
36
+
37
+ <% if logged_in? %>
38
+ Welcome <%= current_user.username %>! Not you?
39
+ <%= link_to "Log out", logout_path %>
40
+ <% else %>
41
+ <%= link_to "Sign up", signup_path %> or
42
+ <%= link_to "log in", login_path %>.
43
+ <% end %>
44
+
45
+ You can also restrict unregistered users from accessing a controller using
46
+ a before filter. For example.
47
+
48
+ before_filter :login_required, :except => [:index, :show]
49
+
50
+ See the generated file lib/authentication.rb for details.
@@ -0,0 +1,154 @@
1
+ require 'generators/dust'
2
+ require 'rails/generators/migration'
3
+
4
+ module dust
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', :type => :boolean
13
+ class_option :rspec, :desc => 'Use RSpec for test files.', :group => 'Test framework', :type => :boolean
14
+ class_option :shoulda, :desc => 'Use shoulda for test files.', :group => 'Test framework', :type => :boolean
15
+
16
+ class_option :haml, :desc => 'Generate HAML views instead of ERB.', :type => :boolean
17
+ class_option :authlogic, :desc => 'Use Authlogic for authentication.', :type => :boolean
18
+
19
+ def add_gems
20
+ gem "bcrypt-ruby", :require => "bcrypt" unless File.read(destination_path("Gemfile")).include? "bcrypt"
21
+ gem "mocha", :group => :test unless File.read(destination_path("Gemfile")).include? "mocha"
22
+ end
23
+
24
+ def create_model_files
25
+ template 'user.rb', "app/models/#{user_singular_name}.rb"
26
+ template 'authlogic_session.rb', "app/models/#{user_singular_name}_session.rb" if options.authlogic?
27
+ end
28
+
29
+ def create_controller_files
30
+ template 'users_controller.rb', "app/controllers/#{user_plural_name}_controller.rb"
31
+ template 'sessions_controller.rb', "app/controllers/#{session_plural_name}_controller.rb"
32
+ end
33
+
34
+ def create_helper_files
35
+ template 'users_helper.rb', "app/helpers/#{user_plural_name}_helper.rb"
36
+ template 'sessions_helper.rb', "app/helpers/#{session_plural_name}_helper.rb"
37
+ end
38
+
39
+ def create_view_files
40
+ template "views/#{view_language}/signup.html.#{view_language}", "app/views/#{user_plural_name}/new.html.#{view_language}"
41
+ template "views/#{view_language}/edit.html.#{view_language}", "app/views/#{user_plural_name}/edit.html.#{view_language}"
42
+ template "views/#{view_language}/_form.html.#{view_language}", "app/views/#{user_plural_name}/_form.html.#{view_language}"
43
+ template "views/#{view_language}/login.html.#{view_language}", "app/views/#{session_plural_name}/new.html.#{view_language}"
44
+ end
45
+
46
+ def create_lib_files
47
+ template 'controller_authentication.rb', 'lib/controller_authentication.rb'
48
+ end
49
+
50
+ def create_routes
51
+ route "resources #{user_plural_name.to_sym.inspect}"
52
+ route "resources #{session_plural_name.to_sym.inspect}"
53
+ route "match 'login' => '#{session_plural_name}#new', :as => :login"
54
+ route "match 'logout' => '#{session_plural_name}#destroy', :as => :logout"
55
+ route "match 'signup' => '#{user_plural_name}#new', :as => :signup"
56
+ route "match '#{user_singular_name}/edit' => '#{user_plural_name}#edit', :as => :edit_#{user_singular_name}"
57
+ end
58
+
59
+ def create_migration
60
+ migration_template 'migration.rb', "db/migrate/create_#{user_plural_name}.rb"
61
+ end
62
+
63
+ def load_and_include_authentication
64
+ inject_into_class "config/application.rb", "Application", " config.autoload_paths << \"\#{config.root}/lib\""
65
+ inject_into_class "app/controllers/application_controller.rb", "ApplicationController", " include ControllerAuthentication\n"
66
+ end
67
+
68
+ def create_test_files
69
+ if test_framework == :rspec
70
+ template 'fixtures.yml', "spec/fixtures/#{user_plural_name}.yml"
71
+ template 'tests/rspec/user.rb', "spec/models/#{user_singular_name}_spec.rb"
72
+ template 'tests/rspec/users_controller.rb', "spec/controllers/#{user_plural_name}_controller_spec.rb"
73
+ template 'tests/rspec/sessions_controller.rb', "spec/controllers/#{session_plural_name}_controller_spec.rb"
74
+ else
75
+ template 'fixtures.yml', "test/fixtures/#{user_plural_name}.yml"
76
+ template "tests/#{test_framework}/user.rb", "test/unit/#{user_singular_name}_test.rb"
77
+ template "tests/#{test_framework}/users_controller.rb", "test/functional/#{user_plural_name}_controller_test.rb"
78
+ template "tests/#{test_framework}/sessions_controller.rb", "test/functional/#{session_plural_name}_controller_test.rb"
79
+ end
80
+ end
81
+
82
+ private
83
+
84
+ def session_name
85
+ @_session_name ||= @session_name == '[[DEFAULT]]' ?
86
+ (options.authlogic? ? user_name + '_session' : 'session') :
87
+ @session_name
88
+ end
89
+
90
+ def user_singular_name
91
+ user_name.underscore
92
+ end
93
+
94
+ def user_plural_name
95
+ user_singular_name.pluralize
96
+ end
97
+
98
+ def user_class_name
99
+ user_name.camelize
100
+ end
101
+
102
+ def user_plural_class_name
103
+ user_plural_name.camelize
104
+ end
105
+
106
+ def session_singular_name
107
+ session_name.underscore
108
+ end
109
+
110
+ def session_plural_name
111
+ session_singular_name.pluralize
112
+ end
113
+
114
+ def session_class_name
115
+ session_name.camelize
116
+ end
117
+
118
+ def session_plural_class_name
119
+ session_plural_name.camelize
120
+ end
121
+
122
+ def view_language
123
+ options.haml? ? 'haml' : 'erb'
124
+ end
125
+
126
+ def test_framework
127
+ return @test_framework if defined?(@test_framework)
128
+ if options.testunit?
129
+ return @test_framework = :testunit
130
+ elsif options.rspec?
131
+ return @test_framework = :rspec
132
+ elsif options.shoulda?
133
+ return @test_framework = :shoulda
134
+ else
135
+ return @test_framework = File.exist?(destination_path('spec')) ? :rspec : :testunit
136
+ end
137
+ end
138
+
139
+ def destination_path(path)
140
+ File.join(destination_root, path)
141
+ end
142
+
143
+ # FIXME: Should be proxied to ActiveRecord::Generators::Base
144
+ # Implement the required interface for Rails::Generators::Migration.
145
+ def self.next_migration_number(dirname) #:nodoc:
146
+ if ActiveRecord::Base.timestamped_migrations
147
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
148
+ else
149
+ "%.3d" % (current_migration_number(dirname) + 1)
150
+ end
151
+ end
152
+ end
153
+ end
154
+ end
@@ -0,0 +1,2 @@
1
+ class <%= session_class_name %> < Authlogic::Session::Base
2
+ 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 <%%= current_<%= user_singular_name %>.username %>.
7
+ # <%%= link_to "Edit profile", edit_<%= user_singular_name %>_path %> or
8
+ # <%%= link_to "Log out", logout_path %>
9
+ # <%% else %>
10
+ # <%%= link_to "Sign up", signup_path %> or
11
+ # <%%= link_to "log in", login_path %>.
12
+ # <%% end %>
13
+ #
14
+ # You can also restrict unregistered users from accessing a controller using
15
+ # a before filter. For example.
16
+ #
17
+ # before_filter :login_required, :except => [:index, :show]
18
+ module ControllerAuthentication
19
+ def self.included(controller)
20
+ controller.send :helper_method, :current_<%= user_singular_name %>, :logged_in?, :redirect_to_target_or_default
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.url
60
+ end
61
+ 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,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("/")
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 "/"
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("/")
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 "/"
43
+ end
44
+ <%- end -%>
45
+ 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
+ render_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("/")
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("/")
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,56 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe <%= user_plural_class_name %>Controller do
4
+ fixtures :all
5
+ render_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("/")
22
+ <%- unless options[:authlogic] -%>
23
+ session['<%= user_singular_name %>_id'].should == assigns['<%= user_singular_name %>'].id
24
+ <%- end -%>
25
+ end
26
+
27
+ it "edit action should redirect when not logged in" do
28
+ get :edit, :id => "ignored"
29
+ response.should redirect_to(login_url)
30
+ end
31
+
32
+ it "edit action should render edit template" do
33
+ @controller.stubs(:current_<%= user_singular_name %>).returns(<%= user_class_name %>.first)
34
+ get :edit, :id => "ignored"
35
+ response.should render_template(:edit)
36
+ end
37
+
38
+ it "update action should redirect when not logged in" do
39
+ put :update, :id => "ignored"
40
+ response.should redirect_to(login_url)
41
+ end
42
+
43
+ it "update action should render edit template when <%= user_singular_name %> is invalid" do
44
+ @controller.stubs(:current_<%= user_singular_name %>).returns(<%= user_class_name %>.first)
45
+ <%= user_class_name %>.any_instance.stubs(:valid?).returns(false)
46
+ put :update, :id => "ignored"
47
+ response.should render_template(:edit)
48
+ end
49
+
50
+ it "update action should redirect when <%= user_singular_name %> is valid" do
51
+ @controller.stubs(:current_<%= user_singular_name %>).returns(<%= user_class_name %>.first)
52
+ <%= user_class_name %>.any_instance.stubs(:valid?).returns(true)
53
+ put :update, :id => "ignored"
54
+ response.should redirect_to("/")
55
+ end
56
+ 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 "/"
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 "/"
36
+ assert_equal <%= user_class_name %>.first.id, session['<%= user_singular_name %>_id']
37
+ end
38
+ <%- end -%>
39
+ end
40
+ end