summaryrefslogtreecommitdiff
path: root/test/unit/phone_number_test.rb
blob: 3fd92d6c953a36e998506845514469425b151145 (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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# ruby coding: utf-8

require 'test_helper'

class PhoneNumberTest < ActiveSupport::TestCase
  
  test "should have valid factory" do
    assert FactoryGirl.build(:phone_number).valid?
  end
  
  def test_that_the_initial_state_should_be_active
    @phone_number = FactoryGirl.create(:phone_number)
    assert_equal 'active', @phone_number.state
    assert @phone_number.active?
  end
  
  test "that the value_of_to_s field is filled" do
    phone_number = FactoryGirl.create(:phone_number)
    assert_equal phone_number.value_of_to_s, phone_number.to_s
  end
  
  {
    '+492612000' => {
      :country_code => "49",
      :area_code => "261",
      :central_office_code => nil,
      :subscriber_number => "2000",
      :extension => nil,
    },
    '+49 261 2000' => {
      :country_code => "49",
      :area_code => "261",
      :central_office_code => nil,
      :subscriber_number => "2000",
      :extension => nil,
    },
    '+49-261-2000' => {
      :country_code => "49",
      :area_code => "261",
      :central_office_code => nil,
      :subscriber_number => "2000",
      :extension => nil,
    },
    '492612000' => {
      :country_code => "49",
      :area_code => "261",
      :central_office_code => nil,
      :subscriber_number => "2000",
      :extension => nil,
    },
    '49888888882000' => nil,  # unknown area code
    '552612000' => nil,  # unknown country code
    '15551234567' => {
      :country_code => "1",
      :area_code => "555",
      :central_office_code => "123",
      :subscriber_number => "4567",
      :extension => nil,
    },
    '2612000' => nil,   # not an international number
    '02612000' => nil,  # not an international number
    '00492612000' => nil,  # invalid format
    '2000' => nil,
    '' => nil,
    nil => nil,
    '++++' => nil,
    '###' => nil,
    'äöü' => nil,
    false => nil,
    true => nil,  # true.to_s == "true"  # invalid number
  }.each_pair do |number, expected|
    test "should parse number #{number.inspect} correctly" do
      # load some country codes:
      usa     = Country.create(:name => "United States of America", :country_code => "1",  :international_call_prefix => "011", :trunk_prefix => "1" )
      germany = Country.create(:name => "Germany", :country_code => "49",  :international_call_prefix => "00", :trunk_prefix => "0" )
      cuba    = Country.create(:name => "Cuba", :country_code => "53",  :international_call_prefix => "119", :trunk_prefix => "" )
      # load some area codes:
      AreaCode.create(:country => germany, :name => "Koblenz am Rhein", :area_code => "261")
      AreaCode.create(:country => germany, :name => "Neuwied", :area_code => "2631")
      AreaCode.create(:country => germany, :name => "Berlin", :area_code => "30")
      AreaCode.create(:country => germany, :name => "Hamburg", :area_code => "40")
      AreaCode.create(:country => germany, :name => "Hohenmocker", :area_code => "39993")
      
      assert_equal expected, PhoneNumber.parse_international_number( number )
    end
  end
  
  {
    '+492612000' => {
      :country_code => "49",
      :area_code => "261",
      :central_office_code => nil,
      :subscriber_number => "2000",
      :extension => nil,
    },
    '+49 261 2000' => {
      :country_code => "49",
      :area_code => "261",
      :central_office_code => nil,
      :subscriber_number => "2000",
      :extension => nil,
    },
    '+49-261-2000' => {
      :country_code => "49",
      :area_code => "261",
      :central_office_code => nil,
      :subscriber_number => "2000",
      :extension => nil,
    },
    '110' => {
      :country_code => nil,
      :area_code => nil,
      :central_office_code => nil,
      :subscriber_number => "110",
      :extension => nil,
    },
    '11833' => {
      :country_code => nil,
      :area_code => nil,
      :central_office_code => nil,
      :subscriber_number => "11833",
      :extension => nil,
    },
    '15551234567' => {
      :country_code => nil,
      :area_code => nil,
      :central_office_code => nil,
      :subscriber_number => "15551234567",
      :extension => nil,
    },
    '0015551234567' => {
      :country_code => "1",
      :area_code => "555",
      :central_office_code => "123",
      :subscriber_number => "4567",
      :extension => nil,
    },
    '+15551234567' => {
      :country_code => "1",
      :area_code => "555",
      :central_office_code => "123",
      :subscriber_number => "4567",
      :extension => nil,
    },
    '02612000' => {
      :country_code => "49",
      :area_code => "261",
      :central_office_code => nil,
      :subscriber_number => "2000",
      :extension => nil,
    },
    '00492612000' => {
      :country_code => "49",
      :area_code => "261",
      :central_office_code => nil,
      :subscriber_number => "2000",
      :extension => nil,
    },
    '2000' => {
      :country_code => nil,
      :area_code => nil,
      :central_office_code => nil,
      :subscriber_number => nil,
      :extension => "2000",
    },
    '99' => {
      :country_code => nil,
      :area_code => nil,
      :central_office_code => nil,
      :subscriber_number => nil,
      :extension => "99",
    },
    '5' => {
      :country_code => nil,
      :area_code => nil,
      :central_office_code => nil,
      :subscriber_number => nil,
      :extension => "5",
    },
    '' => nil,
    nil => nil,
    '++++' => nil,
    '###' => nil,
    'äöü' => nil,
    false => nil,
    true => nil,  # true.to_s == "true"  # invalid number
  }.each_pair do |number, expected|
    test "should parse number #{number.inspect} correctly for a specific tenant" do
      # load some country codes:
      usa     = Country.create(:name => "United States of America", :country_code => "1",  :international_call_prefix => "011", :trunk_prefix => "1" )
      germany = Country.create(:name => "Germany", :country_code => "49",  :international_call_prefix => "00", :trunk_prefix => "0" )
      cuba    = Country.create(:name => "Cuba", :country_code => "53",  :international_call_prefix => "119", :trunk_prefix => "" )
      # load some area codes:
      AreaCode.create(:country => germany, :name => "Koblenz am Rhein", :area_code => "261")
      AreaCode.create(:country => germany, :name => "Neuwied", :area_code => "2631")
      AreaCode.create(:country => germany, :name => "Berlin", :area_code => "30")
      AreaCode.create(:country => germany, :name => "Hamburg", :area_code => "40")
      AreaCode.create(:country => germany, :name => "Hohenmocker", :area_code => "39993")
      
      # create a tenant
      tenant = FactoryGirl.create(:tenant, :country_id => germany.id)
      # create some extensions
      internal_extension_range = tenant.phone_number_ranges.create(:name => INTERNAL_EXTENSIONS)
      ['2000', '2001', '2003', '2004', '2005', '2006', '2007', '2008', '2009', '2010', '5', '99'].each do |extension|
        internal_extension_range.phone_numbers.create(:name => "Extension #{extension}", :number => extension)
      end
      
      assert_equal expected, PhoneNumber.parse( number, tenant )
    end
  end
  
  # TODO: Test uniqueness of a phone_number when creating it.
  
  # test "has to be unique per sip_account" do
  #   germany = Country.create(:name => "Germany", :country_code => "49",  :international_call_prefix => "00", :trunk_prefix => "0" )
  #   Language.create(:name => 'Deutsch', :code => 'de')
  #   AreaCode.create(:country => germany, :name => "Bendorf", :area_code => "2622")

  #   @sip_domain = FactoryGirl.create(:sip_domain)

  #   @tenant = @sip_domain.tenants.build(:name => 'AMOOMA GmbH')
  #   @tenant.country  = Country.first
  #   @tenant.language = Language.first
  #   @tenant.internal_extension_ranges = '10-20'
  #   @tenant.save

  #   @tenant.generate_internal_extensions

  #   sip_account = @tenant.sip_accounts.build(FactoryGirl.create(:sip_account).attributes)
  #   phone_number = sip_account.phone_numbers.create(FactoryGirl.build(:phone_number, :number => '10').attributes)
  #   phone_number_evil = sip_account.phone_numbers.build(phone_number.attributes)
    
  #   assert phone_number.valid?
  #   assert !phone_number_evil.valid?
  # end
    
  # test "has to be unique per SIP domain even for different tenants" do
  #   provider_sip_domain = FactoryGirl.create(:sip_domain)
  #   tenants      = []
  #   sip_accounts = []
  #   2.times { |i|
  #     tenants[i] = provider_sip_domain.tenants.create(FactoryGirl.build(:tenant, :internal_extension_ranges => '10-20').attributes)
  #     tenants[i].generate_internal_extensions

  #     sip_accounts[i] = tenants[i].sip_accounts.build(FactoryGirl.build(:sip_account, :tenant_id => tenants[i].id).attributes)
  #     sip_accounts[i].phone_numbers.build(:number => '10')
  #   }
  #   sip_accounts[0].save
    
  #   assert   sip_accounts[0].valid?, 'Should be valid.'
  #   assert ! sip_accounts[1].valid?,
  #     "Shouldn't be possible to use the same phone number more than once per SIP domain."
      
  #   # Lets change the second phone_number for a positiv test:
  #   #
  #   sip_accounts[1].phone_numbers.first.number = '11'
  #   assert sip_accounts[1].valid?    
  # end  
  
end