summaryrefslogtreecommitdiff
path: root/users/test/unit/identity_test.rb
blob: 4ebd72e92054cfdec84c567aba6cff9d617c8f47 (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
require 'test_helper'

class IdentityTest < ActiveSupport::TestCase

  setup do
    @user = FactoryGirl.create(:user)
  end

  teardown do
    @user.destroy
  end

  test "initial identity for a user" do
    id = @user.build_identity
    assert_equal @user.email_address, id.address
    assert_equal @user.email_address, id.destination
    assert_equal @user, id.user
  end

  test "add alias" do
    id = @user.build_identity address: alias_name
    assert_equal LocalEmail.new(alias_name), id.address
    assert_equal @user.email_address, id.destination
    assert_equal @user, id.user
  end

  test "add forward" do
    id = @user.build_identity destination: forward_address
    assert_equal @user.email_address, id.address
    assert_equal Email.new(forward_address), id.destination
    assert_equal @user, id.user
  end

  test "forward alias" do
    id = @user.build_identity address: alias_name, destination: forward_address
    assert_equal LocalEmail.new(alias_name), id.address
    assert_equal Email.new(forward_address), id.destination
    assert_equal @user, id.user
    id.save
  end

  test "prevents duplicates" do
    id = @user.create_identity address: alias_name, destination: forward_address
    dup = @user.build_identity address: alias_name, destination: forward_address
    assert !dup.valid?
    assert_equal ["This alias already exists"], dup.errors[:base]
  end

  test "validates availability" do
    other_user = FactoryGirl.create(:user)
    id = @user.create_identity address: alias_name, destination: forward_address
    taken = other_user.build_identity address: alias_name
    assert !taken.valid?
    assert_equal ["This email has already been taken"], taken.errors[:base]
    other_user.destroy
  end



  def alias_name
    @alias_name ||= Faker::Internet.user_name
  end

  def forward_address
    @forward_address ||= Faker::Internet.email
  end
end