summaryrefslogtreecommitdiff
path: root/users/test/unit/email_test.rb
blob: 060ced52c3aa6a4fcc65c0dd516615fde794685a (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
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)
    @email_string = "valid_alias@#{APP_CONFIG[:domain]}"
    User.find_by_email_or_alias(@email_string).try(:destroy)
  end

  teardown do
    @user.destroy if @user.persisted? # just in case
    @other_user.destroy if @other_user.persisted?
  end

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

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

  test "email needs to be different from email aliases" do
    @user.email_aliases.build :email => @email_string
    @user.save
    assert_invalid_email @email_string
  end

  test "non local emails are invalid" do
    assert_invalid_email "not_valid@mail.me"
  end

  test "local emails are valid" do
    local_email = "valid@#{APP_CONFIG[:domain]}"
    @user.email = local_email
    @user.valid?
    assert_equal Hash.new, @user.errors.messages
  end

  test "find user by email" do
    email = "finding@test.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

  def assert_invalid_email(string)
    @user.email = string
    assert !@user.valid?
    assert @user.errors.keys.include?(:email)
  end

end