diff options
Diffstat (limited to 'users')
-rw-r--r-- | users/app/controllers/email_alias_controller.rb | 36 | ||||
-rw-r--r-- | users/test/unit/email_alias_test.rb | 50 |
2 files changed, 86 insertions, 0 deletions
diff --git a/users/app/controllers/email_alias_controller.rb b/users/app/controllers/email_alias_controller.rb new file mode 100644 index 0000000..979c8ad --- /dev/null +++ b/users/app/controllers/email_alias_controller.rb @@ -0,0 +1,36 @@ +class EmailAliasesController < ApplicationController + + before_filter :fetch_user + + # get a list of email aliases for the given user? + def index + @aliases = @user.email_aliases + respond_with @aliases + end + + def create + @alias = @user.add_email_alias(params[:email_alias]) + flash[:notice] = t(:email_alias_created_successfully) unless @alias.errors + respond_with @alias + end + + def update + @alias = @user.get_email_alias(params[:id]) + @alias.set_email(params[:email_alias]) + flash[:notice] = t(:email_alias_updated_successfully) unless @alias.errors + respond_with @alias + end + + def destroy + @alias = @user.get_email_alias(params[:id]) + flash[:notice] = t(:email_alias_destroyed_successfully) + @alias.destroy + end + + protected + + def fetch_user + @user = User.find_by_param(params[:user_id]) + access_denied unless admin? or (@user == current_user) + end +end diff --git a/users/test/unit/email_alias_test.rb b/users/test/unit/email_alias_test.rb new file mode 100644 index 0000000..86cb10f --- /dev/null +++ b/users/test/unit/email_alias_test.rb @@ -0,0 +1,50 @@ +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" do + email_alias = "valid_alias@domain.net" + @user.add_email_alias(email_alias) + assert_equal [email_alias], @user.email_aliases + end + + test "email aliases need to be unique" do + # todo build helper for this ... make_record(User) + email_alias = "valid_alias@domain.net" + attribs = User.valid_attributes_hash.merge(:login => "other_user") + User.find_by_login(attribs[:login]).try(:destroy) + other_user = User.new(attribs) + other_user.add_email_alias(email_alias) + @user.add_email_alias(email_alias) + # todo: how do we handle errors? Should email_alias become an ActiveModel? + assert_equal [], @user.email_aliases + end + + test "email aliases may not conflict with emails" do + # todo build helper for this ... make_record(User) + email_alias = "valid_alias@domain.net" + attribs = User.valid_attributes_hash.merge(:login => "other_user", :email => email_alias) + User.find_by_login(attribs[:login]).try(:destroy) + other_user = User.new(attribs) + @user.add_email_alias(email_alias) + # todo: how do we handle errors? Should email_alias become an ActiveModel? + assert_equal [], @user.email_aliases + end + + test "can retrieve user by email alias" do + email_alias = "valid_alias@domain.net" + @user.add_email_alias(email_alias) + assert_equal @user, User.find_by_email_alias(email_alias) + assert_equal @user, User.find_by_email(email_alias) + end +end |