summaryrefslogtreecommitdiff
path: root/users/test
diff options
context:
space:
mode:
Diffstat (limited to 'users/test')
-rw-r--r--users/test/functional/application_controller_test.rb2
-rw-r--r--users/test/functional/users_controller_test.rb14
-rw-r--r--users/test/support/auth_test_helper.rb7
-rw-r--r--users/test/support/stub_record_helper.rb4
-rw-r--r--users/test/unit/email_aliases_test.rb57
-rw-r--r--users/test/unit/email_test.rb39
-rw-r--r--users/test/unit/user_test.rb18
7 files changed, 133 insertions, 8 deletions
diff --git a/users/test/functional/application_controller_test.rb b/users/test/functional/application_controller_test.rb
index 857bae5..94b77bd 100644
--- a/users/test/functional/application_controller_test.rb
+++ b/users/test/functional/application_controller_test.rb
@@ -9,7 +9,7 @@ class ApplicationControllerTest < ActionController::TestCase
def test_authorize_redirect
@controller.send(:authorize)
- assert_access_denied
+ assert_access_denied(true, false)
end
def test_authorized
diff --git a/users/test/functional/users_controller_test.rb b/users/test/functional/users_controller_test.rb
index 1840a72..ce17500 100644
--- a/users/test/functional/users_controller_test.rb
+++ b/users/test/functional/users_controller_test.rb
@@ -1,7 +1,6 @@
require 'test_helper'
class UsersControllerTest < ActionController::TestCase
- include StubRecordHelper
test "should get new" do
get :new
@@ -35,7 +34,10 @@ class UsersControllerTest < ActionController::TestCase
end
test "should get edit view" do
- user = find_record User, :email => nil, :email_forward => nil
+ user = find_record User,
+ :email => nil,
+ :email_forward => nil,
+ :email_aliases => []
login user
get :edit, :id => user.id
@@ -45,7 +47,9 @@ class UsersControllerTest < ActionController::TestCase
test "should process updated params" do
user = find_record User
- user.expects(:update_attributes).with(user.params).returns(true)
+ user.expects(:attributes=).with(user.params)
+ user.expects(:changed?).returns(true)
+ user.expects(:save).returns(true)
login user
put :update, :user => user.params, :id => user.id, :format => :json
@@ -57,7 +61,9 @@ class UsersControllerTest < ActionController::TestCase
test "admin can update user" do
user = find_record User
- user.expects(:update_attributes).with(user.params).returns(true)
+ user.expects(:attributes=).with(user.params)
+ user.expects(:changed?).returns(true)
+ user.expects(:save).returns(true)
login :is_admin? => true
put :update, :user => user.params, :id => user.id, :format => :json
diff --git a/users/test/support/auth_test_helper.rb b/users/test/support/auth_test_helper.rb
index f3506ae..c9f5612 100644
--- a/users/test/support/auth_test_helper.rb
+++ b/users/test/support/auth_test_helper.rb
@@ -1,5 +1,4 @@
module AuthTestHelper
- include StubRecordHelper
extend ActiveSupport::Concern
# Controller will fetch current user from warden.
@@ -19,10 +18,12 @@ module AuthTestHelper
return @current_user
end
- def assert_access_denied(denied = true)
+ def assert_access_denied(denied = true, logged_in = true)
if denied
assert_equal({:alert => "Not authorized"}, flash.to_hash)
- assert_redirected_to login_path
+ # todo: eventually probably eliminate separate conditions
+ assert_redirected_to login_path if !logged_in
+ assert_redirected_to root_path if logged_in
else
assert flash[:alert].blank?
end
diff --git a/users/test/support/stub_record_helper.rb b/users/test/support/stub_record_helper.rb
index 2e1a533..1be419a 100644
--- a/users/test/support/stub_record_helper.rb
+++ b/users/test/support/stub_record_helper.rb
@@ -39,3 +39,7 @@ module StubRecordHelper
end
end
+
+class ActionController::TestCase
+ include StubRecordHelper
+end
diff --git a/users/test/unit/email_aliases_test.rb b/users/test/unit/email_aliases_test.rb
new file mode 100644
index 0000000..762aaea
--- /dev/null
+++ b/users/test/unit/email_aliases_test.rb
@@ -0,0 +1,57 @@
+require 'test_helper'
+
+class EmailAliasTest < ActiveSupport::TestCase
+
+ setup do
+ @attribs = User.valid_attributes_hash
+ User.find_by_login(@attribs[:login]).try(:destroy)
+ @user = User.new(@attribs)
+ end
+
+ test "no email aliases set in the beginning" do
+ assert_equal [], @user.email_aliases
+ end
+
+ test "adding email alias through params" do
+ email_alias = "valid_alias@domain.net"
+ @user.attributes = {:email_aliases_attributes => {"0" => {:email => email_alias}}}
+ assert @user.changed?
+ assert @user.save
+ assert_equal email_alias, @user.email_aliases.first.to_s
+ end
+
+ test "adding email alias directly" do
+ email_alias = "valid_alias@domain.net"
+ @user.add_email_alias(email_alias)
+ assert @user.changed?
+ assert @user.save
+ assert_equal email_alias, @user.reload.email_aliases.first.to_s
+ end
+
+ test "duplicated email aliases are invalid" do
+ email_alias = "valid_alias@domain.net"
+ @user.add_email_alias(email_alias)
+ @user.save
+ # add again
+ @user.add_email_alias(email_alias)
+ assert @user.changed?
+ assert !@user.valid?
+ end
+
+ test "email is invalid as email alias" do
+ email_alias = "valid_alias@domain.net"
+ @user.email = email_alias
+ @user.add_email_alias(email_alias)
+ assert @user.changed?
+ assert !@user.valid?
+ end
+
+ test "find user by email alias" do
+ email_alias = "valid_alias@domain.net"
+ @user.add_email_alias(email_alias)
+ assert @user.save
+ assert_equal @user, User.find_by_email_or_alias(email_alias)
+ assert_equal @user, User.find_by_email_alias(email_alias)
+ assert_nil User.find_by_email(email_alias)
+ end
+end
diff --git a/users/test/unit/email_test.rb b/users/test/unit/email_test.rb
new file mode 100644
index 0000000..1e216d6
--- /dev/null
+++ b/users/test/unit/email_test.rb
@@ -0,0 +1,39 @@
+require 'test_helper'
+
+class EmailTest < ActiveSupport::TestCase
+
+ setup do
+ # TODO build helper for this ... make_record(User)
+ @attribs = User.valid_attributes_hash
+ User.find_by_login(@attribs[:login]).try(:destroy)
+ @user = User.new(@attribs)
+ @attribs.merge!(:login => "other_user")
+ User.find_by_login(@attribs[:login]).try(:destroy)
+ @other_user = User.create(@attribs)
+ end
+
+ teardown do
+ @user.destroy if @user.persisted? # just in case
+ @other_user.destroy
+ end
+
+
+ test "email aliases need to be unique" do
+ email_alias = "valid_alias@domain.net"
+ @other_user.add_email_alias email_alias
+ @other_user.save
+ @user.add_email_alias email_alias
+ assert @user.changed?
+ assert !@user.save
+ # TODO handle errors
+ end
+
+ test "email aliases may not conflict with emails" do
+ email_alias = "valid_alias@domain.net"
+ @other_user.email = email_alias
+ @other_user.save
+ @user.add_email_alias email_alias
+ assert @user.changed?
+ assert !@user.save
+ end
+end
diff --git a/users/test/unit/user_test.rb b/users/test/unit/user_test.rb
index cce11c2..5d2a7ea 100644
--- a/users/test/unit/user_test.rb
+++ b/users/test/unit/user_test.rb
@@ -49,4 +49,22 @@ class UserTest < ActiveSupport::TestCase
assert_equal client_rnd, srp_session.aa
end
+ test 'is user an admin' do
+ admin_login = APP_CONFIG['admins'].first
+ attribs = User.valid_attributes_hash
+ attribs[:login] = admin_login
+ admin_user = User.new(attribs)
+ assert admin_user.is_admin?
+ assert !@user.is_admin?
+
+ end
+
+ test "find user by email" do
+ email = "tryto@find.me"
+ @user.email = email
+ @user.save
+ assert_equal @user, User.find_by_email(email)
+ assert_equal @user, User.find_by_email_or_alias(email)
+ assert_nil User.find_by_email_alias(email)
+ end
end