zoo-generators 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +198 -0
- data/Gemfile +2 -0
- data/LICENSE +20 -0
- data/README.rdoc +96 -0
- data/Rakefile +10 -0
- data/features/step_definitions/common_steps.rb +62 -0
- data/features/step_definitions/rails_setup_steps.rb +6 -0
- data/features/support/env.rb +6 -0
- data/features/support/matchers.rb +7 -0
- data/features/zoo_authentication.feature +83 -0
- data/features/zoo_config.feature +17 -0
- data/features/zoo_layout.feature +13 -0
- data/features/zoo_scaffold.feature +84 -0
- data/lib/generators/zoo.rb +28 -0
- data/lib/generators/zoo/authentication/USAGE +46 -0
- data/lib/generators/zoo/authentication/authentication_generator.rb +151 -0
- data/lib/generators/zoo/authentication/templates/controller_authentication.rb +48 -0
- data/lib/generators/zoo/authentication/templates/factory.rb +6 -0
- data/lib/generators/zoo/authentication/templates/fixtures.yml +12 -0
- data/lib/generators/zoo/authentication/templates/migration.rb +15 -0
- data/lib/generators/zoo/authentication/templates/sessions_controller.rb +20 -0
- data/lib/generators/zoo/authentication/templates/sessions_helper.rb +2 -0
- data/lib/generators/zoo/authentication/templates/tests/rspec/sessions_controller.rb +26 -0
- data/lib/generators/zoo/authentication/templates/tests/rspec/user.rb +81 -0
- data/lib/generators/zoo/authentication/templates/tests/rspec/users_controller.rb +54 -0
- data/lib/generators/zoo/authentication/templates/tests/shoulda/sessions_controller.rb +26 -0
- data/lib/generators/zoo/authentication/templates/tests/shoulda/user.rb +83 -0
- data/lib/generators/zoo/authentication/templates/tests/shoulda/users_controller.rb +59 -0
- data/lib/generators/zoo/authentication/templates/tests/testunit/sessions_controller.rb +22 -0
- data/lib/generators/zoo/authentication/templates/tests/testunit/user.rb +87 -0
- data/lib/generators/zoo/authentication/templates/tests/testunit/users_controller.rb +51 -0
- data/lib/generators/zoo/authentication/templates/user.rb +35 -0
- data/lib/generators/zoo/authentication/templates/users_controller.rb +30 -0
- data/lib/generators/zoo/authentication/templates/users_helper.rb +2 -0
- data/lib/generators/zoo/authentication/templates/views/haml/_form.html.haml +16 -0
- data/lib/generators/zoo/authentication/templates/views/haml/edit.html.haml +3 -0
- data/lib/generators/zoo/authentication/templates/views/haml/login.html.haml +13 -0
- data/lib/generators/zoo/authentication/templates/views/haml/signup.html.haml +5 -0
- data/lib/generators/zoo/config/USAGE +23 -0
- data/lib/generators/zoo/config/config_generator.rb +24 -0
- data/lib/generators/zoo/config/templates/config.yml +8 -0
- data/lib/generators/zoo/config/templates/load_config.rb +2 -0
- data/lib/generators/zoo/layout/USAGE +25 -0
- data/lib/generators/zoo/layout/layout_generator.rb +26 -0
- data/lib/generators/zoo/layout/templates/error_messages_helper.rb +23 -0
- data/lib/generators/zoo/layout/templates/layout.html.erb +19 -0
- data/lib/generators/zoo/layout/templates/layout.html.haml +21 -0
- data/lib/generators/zoo/layout/templates/layout_helper.rb +22 -0
- data/lib/generators/zoo/layout/templates/stylesheet.css +83 -0
- data/lib/generators/zoo/layout/templates/stylesheet.sass +73 -0
- data/lib/generators/zoo/scaffold/USAGE +51 -0
- data/lib/generators/zoo/scaffold/scaffold_generator.rb +316 -0
- data/lib/generators/zoo/scaffold/templates/actions/create.rb +8 -0
- data/lib/generators/zoo/scaffold/templates/actions/destroy.rb +5 -0
- data/lib/generators/zoo/scaffold/templates/actions/edit.rb +3 -0
- data/lib/generators/zoo/scaffold/templates/actions/index.rb +3 -0
- data/lib/generators/zoo/scaffold/templates/actions/new.rb +3 -0
- data/lib/generators/zoo/scaffold/templates/actions/show.rb +3 -0
- data/lib/generators/zoo/scaffold/templates/actions/update.rb +8 -0
- data/lib/generators/zoo/scaffold/templates/controller.rb +3 -0
- data/lib/generators/zoo/scaffold/templates/factory.rb +5 -0
- data/lib/generators/zoo/scaffold/templates/fixtures.yml +9 -0
- data/lib/generators/zoo/scaffold/templates/helper.rb +2 -0
- data/lib/generators/zoo/scaffold/templates/migration.rb +16 -0
- data/lib/generators/zoo/scaffold/templates/model.rb +4 -0
- data/lib/generators/zoo/scaffold/templates/tests/rspec/actions/create.rb +11 -0
- data/lib/generators/zoo/scaffold/templates/tests/rspec/actions/destroy.rb +6 -0
- data/lib/generators/zoo/scaffold/templates/tests/rspec/actions/edit.rb +4 -0
- data/lib/generators/zoo/scaffold/templates/tests/rspec/actions/index.rb +4 -0
- data/lib/generators/zoo/scaffold/templates/tests/rspec/actions/new.rb +4 -0
- data/lib/generators/zoo/scaffold/templates/tests/rspec/actions/show.rb +4 -0
- data/lib/generators/zoo/scaffold/templates/tests/rspec/actions/update.rb +11 -0
- data/lib/generators/zoo/scaffold/templates/tests/rspec/controller.rb +8 -0
- data/lib/generators/zoo/scaffold/templates/tests/rspec/model.rb +7 -0
- data/lib/generators/zoo/scaffold/templates/tests/shoulda/actions/create.rb +13 -0
- data/lib/generators/zoo/scaffold/templates/tests/shoulda/actions/destroy.rb +8 -0
- data/lib/generators/zoo/scaffold/templates/tests/shoulda/actions/edit.rb +6 -0
- data/lib/generators/zoo/scaffold/templates/tests/shoulda/actions/index.rb +6 -0
- data/lib/generators/zoo/scaffold/templates/tests/shoulda/actions/new.rb +6 -0
- data/lib/generators/zoo/scaffold/templates/tests/shoulda/actions/show.rb +6 -0
- data/lib/generators/zoo/scaffold/templates/tests/shoulda/actions/update.rb +13 -0
- data/lib/generators/zoo/scaffold/templates/tests/shoulda/controller.rb +5 -0
- data/lib/generators/zoo/scaffold/templates/tests/shoulda/model.rb +7 -0
- data/lib/generators/zoo/scaffold/templates/tests/testunit/actions/create.rb +11 -0
- data/lib/generators/zoo/scaffold/templates/tests/testunit/actions/destroy.rb +6 -0
- data/lib/generators/zoo/scaffold/templates/tests/testunit/actions/edit.rb +4 -0
- data/lib/generators/zoo/scaffold/templates/tests/testunit/actions/index.rb +4 -0
- data/lib/generators/zoo/scaffold/templates/tests/testunit/actions/new.rb +4 -0
- data/lib/generators/zoo/scaffold/templates/tests/testunit/actions/show.rb +4 -0
- data/lib/generators/zoo/scaffold/templates/tests/testunit/actions/update.rb +11 -0
- data/lib/generators/zoo/scaffold/templates/tests/testunit/controller.rb +5 -0
- data/lib/generators/zoo/scaffold/templates/tests/testunit/model.rb +7 -0
- data/lib/generators/zoo/scaffold/templates/views/haml/_form.html.haml +9 -0
- data/lib/generators/zoo/scaffold/templates/views/haml/edit.html.haml +14 -0
- data/lib/generators/zoo/scaffold/templates/views/haml/index.html.haml +25 -0
- data/lib/generators/zoo/scaffold/templates/views/haml/new.html.haml +7 -0
- data/lib/generators/zoo/scaffold/templates/views/haml/show.html.haml +20 -0
- metadata +243 -0
@@ -0,0 +1,26 @@
|
|
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
|
+
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
|
+
|
26
|
+
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,54 @@
|
|
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(root_url)
|
22
|
+
session['<%= user_singular_name %>_id'].should == assigns['<%= user_singular_name %>'].id
|
23
|
+
end
|
24
|
+
|
25
|
+
it "edit action should redirect when not logged in" do
|
26
|
+
get :edit, :id => "ignored"
|
27
|
+
response.should redirect_to(login_url)
|
28
|
+
end
|
29
|
+
|
30
|
+
it "edit action should render edit template" do
|
31
|
+
@controller.stubs(:current_<%= user_singular_name %>).returns(<%= user_class_name %>.first)
|
32
|
+
get :edit, :id => "ignored"
|
33
|
+
response.should render_template(:edit)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "update action should redirect when not logged in" do
|
37
|
+
put :update, :id => "ignored"
|
38
|
+
response.should redirect_to(login_url)
|
39
|
+
end
|
40
|
+
|
41
|
+
it "update action should render edit template when <%= user_singular_name %> is invalid" do
|
42
|
+
@controller.stubs(:current_<%= user_singular_name %>).returns(<%= user_class_name %>.first)
|
43
|
+
<%= user_class_name %>.any_instance.stubs(:valid?).returns(false)
|
44
|
+
put :update, :id => "ignored"
|
45
|
+
response.should render_template(:edit)
|
46
|
+
end
|
47
|
+
|
48
|
+
it "update action should redirect when <%= user_singular_name %> is valid" do
|
49
|
+
@controller.stubs(:current_<%= user_singular_name %>).returns(<%= user_class_name %>.first)
|
50
|
+
<%= user_class_name %>.any_instance.stubs(:valid?).returns(true)
|
51
|
+
put :update, :id => "ignored"
|
52
|
+
response.should redirect_to(root_url)
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,26 @@
|
|
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
|
+
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_equal ["can't be blank"], new_<%= user_singular_name %>(:username => '').errors[:username]
|
24
|
+
end
|
25
|
+
|
26
|
+
should "require password" do
|
27
|
+
assert_equal ["can't be blank"], new_<%= user_singular_name %>(:password => '').errors[:password]
|
28
|
+
end
|
29
|
+
|
30
|
+
should "require well formed email" do
|
31
|
+
assert_equal ["is invalid"], new_<%= user_singular_name %>(:email => 'foo@bar@example.com').errors[:email]
|
32
|
+
end
|
33
|
+
|
34
|
+
should "validate uniqueness of email" do
|
35
|
+
new_<%= user_singular_name %>(:email => 'bar@example.com').save!
|
36
|
+
assert_equal ["has already been taken"], new_<%= user_singular_name %>(:email => 'bar@example.com').errors[:email]
|
37
|
+
end
|
38
|
+
|
39
|
+
should "validate uniqueness of username" do
|
40
|
+
new_<%= user_singular_name %>(:username => 'uniquename').save!
|
41
|
+
assert_equal ["has already been taken"], new_<%= user_singular_name %>(:username => 'uniquename').errors[:username]
|
42
|
+
end
|
43
|
+
|
44
|
+
should "not allow odd characters in username" do
|
45
|
+
assert_equal ["should only contain letters, numbers, or .-_@"], new_<%= user_singular_name %>(:username => 'odd ^&(@)').errors[:username]
|
46
|
+
end
|
47
|
+
|
48
|
+
should "validate password is longer than 3 characters" do
|
49
|
+
assert_equal ["is too short (minimum is 4 characters)"], new_<%= user_singular_name %>(:password => 'bad').errors[:password]
|
50
|
+
end
|
51
|
+
|
52
|
+
should "require matching password confirmation" do
|
53
|
+
assert_equal ["doesn't match confirmation"], new_<%= user_singular_name %>(:password_confirmation => 'nonmatching').errors[: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,59 @@
|
|
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 <%= user_singular_name %> 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 <%= user_singular_name %> 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
|
+
|
26
|
+
context "edit action" do
|
27
|
+
should "redirect when not logged in" do
|
28
|
+
get :edit, :id => "ignored"
|
29
|
+
assert_redirected_to login_url
|
30
|
+
end
|
31
|
+
|
32
|
+
should "render edit template" do
|
33
|
+
@controller.stubs(:current_<%= user_singular_name %>).returns(<%= user_class_name %>.first)
|
34
|
+
get :edit, :id => "ignored"
|
35
|
+
assert_template 'edit'
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
context "update action" do
|
40
|
+
should "redirect when not logged in" do
|
41
|
+
put :update, :id => "ignored"
|
42
|
+
assert_redirected_to login_url
|
43
|
+
end
|
44
|
+
|
45
|
+
should "render edit template when <%= user_singular_name %> is invalid" do
|
46
|
+
@controller.stubs(:current_<%= user_singular_name %>).returns(<%= user_class_name %>.first)
|
47
|
+
<%= user_class_name %>.any_instance.stubs(:valid?).returns(false)
|
48
|
+
put :update, :id => "ignored"
|
49
|
+
assert_template 'edit'
|
50
|
+
end
|
51
|
+
|
52
|
+
should "redirect when <%= user_singular_name %> is valid" do
|
53
|
+
@controller.stubs(:current_<%= user_singular_name %>).returns(<%= user_class_name %>.first)
|
54
|
+
<%= user_class_name %>.any_instance.stubs(:valid?).returns(true)
|
55
|
+
put :update, :id => "ignored"
|
56
|
+
assert_redirected_to root_url
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,22 @@
|
|
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
|
+
def test_create_invalid
|
10
|
+
<%= user_class_name %>.stubs(:authenticate).returns(nil)
|
11
|
+
post :create
|
12
|
+
assert_template 'new'
|
13
|
+
assert_nil session['<%= user_singular_name %>_id']
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_create_valid
|
17
|
+
<%= user_class_name %>.stubs(:authenticate).returns(<%= user_class_name %>.first)
|
18
|
+
post :create
|
19
|
+
assert_redirected_to root_url
|
20
|
+
assert_equal <%= user_class_name %>.first.id, session['<%= user_singular_name %>_id']
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class <%= user_class_name %>Test < ActiveSupport::TestCase
|
4
|
+
|
5
|
+
def new_<%= user_singular_name %>(attributes = {})
|
6
|
+
attributes[:username] ||= 'foo'
|
7
|
+
attributes[:email] ||= 'foo@example.com'
|
8
|
+
attributes[:password] ||= 'abc123'
|
9
|
+
attributes[:password_confirmation] ||= attributes[:password]
|
10
|
+
<%= user_singular_name %> = <%= user_class_name %>.new(attributes)
|
11
|
+
<%= user_singular_name %>.valid? # run validations
|
12
|
+
<%= user_singular_name %>
|
13
|
+
end
|
14
|
+
|
15
|
+
def setup
|
16
|
+
<%= user_class_name %>.delete_all
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_valid
|
20
|
+
assert new_<%= user_singular_name %>.valid?
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_require_username
|
24
|
+
assert_equal ["can't be blank"], new_<%= user_singular_name %>(:username => '').errors[:username]
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_require_password
|
28
|
+
assert_equal ["can't be blank"], new_<%= user_singular_name %>(:password => '').errors[:password]
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_require_well_formed_email
|
32
|
+
assert_equal ["is invalid"], new_<%= user_singular_name %>(:email => 'foo@bar@example.com').errors[:email]
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_validate_uniqueness_of_email
|
36
|
+
new_<%= user_singular_name %>(:email => 'bar@example.com').save!
|
37
|
+
assert_equal ["has already been taken"], new_<%= user_singular_name %>(:email => 'bar@example.com').errors[:email]
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_validate_uniqueness_of_username
|
41
|
+
new_<%= user_singular_name %>(:username => 'uniquename').save!
|
42
|
+
assert_equal ["has already been taken"], new_<%= user_singular_name %>(:username => 'uniquename').errors[:username]
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_validate_odd_characters_in_username
|
46
|
+
assert_equal ["should only contain letters, numbers, or .-_@"], new_<%= user_singular_name %>(:username => 'odd ^&(@)').errors[:username]
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_validate_password_length
|
50
|
+
assert_equal ["is too short (minimum is 4 characters)"], new_<%= user_singular_name %>(:password => 'bad').errors[:password]
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_require_matching_password_confirmation
|
54
|
+
assert_equal ["doesn't match confirmation"], new_<%= user_singular_name %>(:password_confirmation => 'nonmatching').errors[:password]
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_generate_password_hash_and_salt_on_create
|
58
|
+
<%= user_singular_name %> = new_<%= user_singular_name %>
|
59
|
+
<%= user_singular_name %>.save!
|
60
|
+
assert <%= user_singular_name %>.password_hash
|
61
|
+
assert <%= user_singular_name %>.password_salt
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_authenticate_by_username
|
65
|
+
<%= user_class_name %>.delete_all
|
66
|
+
<%= user_singular_name %> = new_<%= user_singular_name %>(:username => 'foobar', :password => 'secret')
|
67
|
+
<%= user_singular_name %>.save!
|
68
|
+
assert_equal <%= user_singular_name %>, <%= user_class_name %>.authenticate('foobar', 'secret')
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_authenticate_by_email
|
72
|
+
<%= user_class_name %>.delete_all
|
73
|
+
<%= user_singular_name %> = new_<%= user_singular_name %>(:email => 'foo@bar.com', :password => 'secret')
|
74
|
+
<%= user_singular_name %>.save!
|
75
|
+
assert_equal <%= user_singular_name %>, <%= user_class_name %>.authenticate('foo@bar.com', 'secret')
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_authenticate_bad_username
|
79
|
+
assert_nil <%= user_class_name %>.authenticate('nonexisting', 'secret')
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_authenticate_bad_password
|
83
|
+
<%= user_class_name %>.delete_all
|
84
|
+
new_<%= user_singular_name %>(:username => 'foobar', :password => 'secret').save!
|
85
|
+
assert_nil <%= user_class_name %>.authenticate('foobar', 'badpassword')
|
86
|
+
end
|
87
|
+
end
|