summaryrefslogtreecommitdiff
path: root/users/test/unit/email_aliases_test.rb
blob: 88f97f4f4fc145cd641b71e0cb995b1348fcb539 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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)
    @attribs.merge!(:login => "other_user")
    User.find_by_login(@attribs[:login]).try(:destroy)
    @other_user = User.create(@attribs)
    @alias = "valid_alias@#{APP_CONFIG[:domain]}"
    User.find_by_email_or_alias(@alias).try(:destroy)
  end

  test "no email aliases set in the beginning" do
    assert_equal [], @user.email_aliases
  end

  test "adding email alias through params" do
    @user.attributes = {:email_aliases_attributes => {"0" => {:email => @alias}}}
    assert @user.changed?
    assert @user.save
    assert_equal @alias, @user.email_aliases.first.to_s
  end

  test "adding email alias directly" do
    @user.email_aliases.build :email => @alias
    assert @user.save
    assert_equal @alias, @user.reload.email_aliases.first.to_s
  end

  test "duplicated email aliases are invalid" do
    @user.email_aliases.build :email => @alias
    @user.save
    assert_invalid_alias @alias
  end

  test "email alias needs to be different from other peoples email" do
    @other_user.email = @alias
    @other_user.save
    assert_invalid_alias @alias
  end

  test "email needs to be different from other peoples email aliases" do
    @other_user.email_aliases.build :email => @alias
    @other_user.save
    assert_invalid_alias @alias
  end

  test "email is invalid as email alias" do
    @user.email = @alias
    assert_invalid_alias @alias
  end

  test "find user by email alias" do
    @user.email_aliases.build :email => @alias
    assert @user.save
    assert_equal @user, User.find_by_email_or_alias(@alias)
    assert_equal @user, User.find_by_email_alias(@alias)
    assert_nil User.find_by_email(@alias)
  end

  def assert_invalid_alias(string)
    email_alias = @user.email_aliases.build :email => string
    assert !email_alias.valid?
    assert !@user.valid?
  end

end