summaryrefslogtreecommitdiff
path: root/lib/generators/nifty/authentication/templates/tests/testunit
diff options
context:
space:
mode:
Diffstat (limited to 'lib/generators/nifty/authentication/templates/tests/testunit')
-rw-r--r--lib/generators/nifty/authentication/templates/tests/testunit/sessions_controller.rb36
-rw-r--r--lib/generators/nifty/authentication/templates/tests/testunit/user.rb88
-rw-r--r--lib/generators/nifty/authentication/templates/tests/testunit/users_controller.rb53
3 files changed, 177 insertions, 0 deletions
diff --git a/lib/generators/nifty/authentication/templates/tests/testunit/sessions_controller.rb b/lib/generators/nifty/authentication/templates/tests/testunit/sessions_controller.rb
new file mode 100644
index 0000000..fe2a65b
--- /dev/null
+++ b/lib/generators/nifty/authentication/templates/tests/testunit/sessions_controller.rb
@@ -0,0 +1,36 @@
+require 'test_helper'
+
+class <%= session_plural_class_name %>ControllerTest < ActionController::TestCase
+ def test_new
+ get :new
+ assert_template 'new'
+ end
+
+<%- if options[:authlogic] -%>
+ def test_create_invalid
+ post :create, :<%= session_singular_name %> => { :username => "foo", :password => "badpassword" }
+ assert_template 'new'
+ assert_nil <%= session_class_name %>.find
+ end
+
+ def test_create_valid
+ post :create, :<%= session_singular_name %> => { :username => "foo", :password => "secret" }
+ assert_redirected_to root_url
+ assert_equal <%= user_plural_name %>(:foo), <%= session_class_name %>.find.<%= user_singular_name %>
+ end
+<%- else -%>
+ def test_create_invalid
+ <%= user_class_name %>.stubs(:authenticate).returns(nil)
+ post :create
+ assert_template 'new'
+ assert_nil session['<%= user_singular_name %>_id']
+ end
+
+ def test_create_valid
+ <%= user_class_name %>.stubs(:authenticate).returns(<%= user_class_name %>.first)
+ post :create
+ assert_redirected_to root_url
+ assert_equal <%= user_class_name %>.first.id, session['<%= user_singular_name %>_id']
+ end
+<%- end -%>
+end
diff --git a/lib/generators/nifty/authentication/templates/tests/testunit/user.rb b/lib/generators/nifty/authentication/templates/tests/testunit/user.rb
new file mode 100644
index 0000000..c036cf1
--- /dev/null
+++ b/lib/generators/nifty/authentication/templates/tests/testunit/user.rb
@@ -0,0 +1,88 @@
+require 'test_helper'
+
+class <%= user_class_name %>Test < ActiveSupport::TestCase
+<%- unless options[:authlogic] -%>
+ def new_<%= user_singular_name %>(attributes = {})
+ attributes[:username] ||= 'foo'
+ attributes[:email] ||= 'foo@example.com'
+ attributes[:password] ||= 'abc123'
+ attributes[:password_confirmation] ||= attributes[:password]
+ <%= user_singular_name %> = <%= user_class_name %>.new(attributes)
+ <%= user_singular_name %>.valid? # run validations
+ <%= user_singular_name %>
+ end
+
+ def setup
+ <%= user_class_name %>.delete_all
+ end
+
+ def test_valid
+ assert new_<%= user_singular_name %>.valid?
+ end
+
+ def test_require_username
+ assert_equal ["can't be blank"], new_<%= user_singular_name %>(:username => '').errors[:username]
+ end
+
+ def test_require_password
+ assert_equal ["can't be blank"], new_<%= user_singular_name %>(:password => '').errors[:password]
+ end
+
+ def test_require_well_formed_email
+ assert_equal ["is invalid"], new_<%= user_singular_name %>(:email => 'foo@bar@example.com').errors[:email]
+ end
+
+ def test_validate_uniqueness_of_email
+ new_<%= user_singular_name %>(:email => 'bar@example.com').save!
+ assert_equal ["has already been taken"], new_<%= user_singular_name %>(:email => 'bar@example.com').errors[:email]
+ end
+
+ def test_validate_uniqueness_of_username
+ new_<%= user_singular_name %>(:username => 'uniquename').save!
+ assert_equal ["has already been taken"], new_<%= user_singular_name %>(:username => 'uniquename').errors[:username]
+ end
+
+ def test_validate_odd_characters_in_username
+ assert_equal ["should only contain letters, numbers, or .-_@"], new_<%= user_singular_name %>(:username => 'odd ^&(@)').errors[:username]
+ end
+
+ def test_validate_password_length
+ assert_equal ["is too short (minimum is 4 characters)"], new_<%= user_singular_name %>(:password => 'bad').errors[:password]
+ end
+
+ def test_require_matching_password_confirmation
+ assert_equal ["doesn't match confirmation"], new_<%= user_singular_name %>(:password_confirmation => 'nonmatching').errors[:password]
+ end
+
+ def test_generate_password_hash_and_salt_on_create
+ <%= user_singular_name %> = new_<%= user_singular_name %>
+ <%= user_singular_name %>.save!
+ assert <%= user_singular_name %>.password_hash
+ assert <%= user_singular_name %>.password_salt
+ end
+
+ def test_authenticate_by_username
+ <%= user_class_name %>.delete_all
+ <%= user_singular_name %> = new_<%= user_singular_name %>(:username => 'foobar', :password => 'secret')
+ <%= user_singular_name %>.save!
+ assert_equal <%= user_singular_name %>, <%= user_class_name %>.authenticate('foobar', 'secret')
+ end
+
+ def test_authenticate_by_email
+ <%= user_class_name %>.delete_all
+ <%= user_singular_name %> = new_<%= user_singular_name %>(:email => 'foo@bar.com', :password => 'secret')
+ <%= user_singular_name %>.save!
+ assert_equal <%= user_singular_name %>, <%= user_class_name %>.authenticate('foo@bar.com', 'secret')
+ end
+
+ def test_authenticate_bad_username
+ assert_nil <%= user_class_name %>.authenticate('nonexisting', 'secret')
+ end
+
+ def test_authenticate_bad_password
+ <%= user_class_name %>.delete_all
+ new_<%= user_singular_name %>(:username => 'foobar', :password => 'secret').save!
+ assert_nil <%= user_class_name %>.authenticate('foobar', 'badpassword')
+ end
+<%- end -%>
+end
diff --git a/lib/generators/nifty/authentication/templates/tests/testunit/users_controller.rb b/lib/generators/nifty/authentication/templates/tests/testunit/users_controller.rb
new file mode 100644
index 0000000..ef8a3f7
--- /dev/null
+++ b/lib/generators/nifty/authentication/templates/tests/testunit/users_controller.rb
@@ -0,0 +1,53 @@
+require 'test_helper'
+
+class <%= user_plural_class_name %>ControllerTest < ActionController::TestCase
+ def test_new
+ get :new
+ assert_template 'new'
+ end
+
+ def test_create_invalid
+ <%= user_class_name %>.any_instance.stubs(:valid?).returns(false)
+ post :create
+ assert_template 'new'
+ end
+
+ def test_create_valid
+ <%= user_class_name %>.any_instance.stubs(:valid?).returns(true)
+ post :create
+ assert_redirected_to root_url
+ <%- unless options[:authlogic] -%>
+ assert_equal assigns['<%= user_singular_name %>'].id, session['<%= user_singular_name %>_id']
+ <%- end -%>
+ end
+
+ def test_edit_without_user
+ get :edit, :id => "ignored"
+ assert_redirected_to login_url
+ end
+
+ def test_edit
+ @controller.stubs(:current_<%= user_singular_name %>).returns(<%= user_class_name %>.first)
+ get :edit, :id => "ignored"
+ assert_template 'edit'
+ end
+
+ def test_update_without_user
+ put :update, :id => "ignored"
+ assert_redirected_to login_url
+ end
+
+ def test_update_invalid
+ @controller.stubs(:current_<%= user_singular_name %>).returns(<%= user_class_name %>.first)
+ <%= user_class_name %>.any_instance.stubs(:valid?).returns(false)
+ put :update, :id => "ignored"
+ assert_template 'edit'
+ end
+
+ def test_update_valid
+ @controller.stubs(:current_<%= user_singular_name %>).returns(<%= user_class_name %>.first)
+ <%= user_class_name %>.any_instance.stubs(:valid?).returns(true)
+ put :update, :id => "ignored"
+ assert_redirected_to root_url
+ end
+end