cirandas.net

ref: master

test/unit/domain_test.rb


  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
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
require_relative "../test_helper"

class DomainTest < ActiveSupport::TestCase
  fixtures :domains, :environments, :profiles, :users

  def setup
    Domain.clear_cache
  end

  should 'not allow domains without name' do
    domain = Domain.new
    domain.valid?
    assert domain.errors[:name].present?
  end

  should 'not allow domain without dot' do
    domain = build(Domain, :name => 'test')
    domain.valid?
    assert domain.errors[:name].present?
  end

  should 'allow domains with dot' do
    domain = build(Domain, :name => 'test.org')
    domain.valid?
    refute domain.errors[:name].present?
  end

  should 'not allow domains with upper cased letters' do
    domain = build(Domain, :name => 'tEst.org')
    domain.valid?
    assert domain.errors[:name].present?
  end

  should 'allow domains with hyphen' do
    domain = build(Domain, :name => 'test-domain.org')
    domain.valid?
    refute domain.errors[:name].present?
  end

  should 'allow domains with underscore' do
    domain = build(Domain, :name => 'test_domain.org')
    domain.valid?
    refute domain.errors[:name].present?
  end

  should 'return protocol' do
    http = Domain.new :name => 'http_domain.org'
    https = Domain.new :name => 'https_domain.org', :ssl => true
    assert_equal http.protocol, 'http'
    assert_equal https.protocol, 'https'
  end

  def test_owner
    d = build(Domain, :name => 'example.com')
    d.owner = build(Environment, :name => 'Example')
    assert d.save
    assert_kind_of Environment, d.owner
  end

  def test_get_domain_name
    assert_equal 'example.net', Domain.extract_domain_name('www.example.net')
    assert_equal 'example.net', Domain.extract_domain_name('WWW.EXAMPLE.NET')
  end

  def test_name_cannot_have_www
    d = Domain.new
    d.name = 'www.example.net'
    d.valid?
    assert d.errors[:name].present?, "Name should not accept www."

    d.name = 'example.net'
    d.valid?
    refute d.errors[:name].present?
  end

  def test_find_by_name
    Domain.delete_all
    fast_create(Domain, :name => 'example.net')
    d1 = Domain.by_name('example.net')
    d2 =  Domain.by_name('www.example.net')
    refute d1.nil?
    refute d2.nil?
    assert d1 == d2
  end

  def test_unique_name
    Domain.delete_all
    assert create(Domain, :name => 'example.net')

    d = build(Domain, :name => 'example.net')
    refute d.valid?
    assert d.errors[:name].present?
  end

  def test_environment
    # domain directly linked to Environment
    domain = Domain.by_name('colivre.net')
    assert_kind_of Environment, domain.owner
    assert_kind_of Environment, domain.environment

    # domain linked to Profile
    domain = Domain.by_name('johndoe.net')
    assert_kind_of Profile, domain.owner
    assert_kind_of Environment, domain.environment
  end

  def test_profile
    # domain linked to profile
    assert_not_nil Domain.by_name('johndoe.net').profile
    # domain linked to Environment
    assert_nil Domain.by_name('colivre.net').profile
  end

  def test_hosted_domain
    assert_equal false, Domain.hosting_profile_at('example.com')

    profile = create_user('hosted_user').person
    create(Domain, :name => 'example.com', :owner => profile)
    assert_equal true, Domain.hosting_profile_at('example.com')
  end

  def test_not_report_profile_hosted_for_environment_domains
    create(Domain, :name => 'example.com', :owner => Environment.default)
    assert_equal false, Domain.hosting_profile_at('example.com')
  end

  should 'not crash if key is not defined' do
    domain = fast_create(Domain, :name => 'domain-without-key')
    assert_nil domain.google_maps_key
  end

  should 'return key if defined' do
    domain = fast_create(Domain, :name => 'domain-with-key', :google_maps_key => 'DOMAIN_KEY')
    assert_equal 'DOMAIN_KEY', domain.google_maps_key
  end

  should 'return default domain' do
    d1 = fast_create(Domain, :name => 'example1.net')
    d2 = fast_create(Domain, :name => 'example2.net', is_default: true)
    d3 = fast_create(Domain, :name => 'example3.net')
    assert_equal d2, Domain.default
  end

  should 'return by_context' do
    d1 = fast_create(Domain, :name => 'example1.net')
    d2 = fast_create(Domain, :name => 'example2.net', is_default: true)
    d3 = fast_create(Domain, :name => 'example3.net')
    assert_equal d3, Domain.by_context('example3.net')
  end

  should 'return default when passing a unknow domain' do
    d1 = fast_create(Domain, :name => 'example1.net')
    d2 = fast_create(Domain, :name => 'example2.net', is_default: true)
    d3 = fast_create(Domain, :name => 'example3.net')
    assert_equal d2, Domain.by_context('unknown.net')
  end

  should 'return a domain with the default environment when no domains exists' do
    assert_equal Environment.default, Domain.by_context('unknown.net').owner
  end
end