cti_caller_id_test.rb 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. # encoding: utf-8
  2. require 'test_helper'
  3. class CtiCallerIdTest < ActiveSupport::TestCase
  4. test '1 parse possible phone numbers in text' do
  5. text = "some text\ntest 123"
  6. result = []
  7. assert_equal(result, Cti::CallerId.parse_text(text))
  8. text = '0049 1234 123456789'
  9. result = ['491234123456789']
  10. assert_equal(result, Cti::CallerId.parse_text(text))
  11. text = '022 1234567'
  12. result = ['49221234567']
  13. assert_equal(result, Cti::CallerId.parse_text(text))
  14. text = '0271233211'
  15. result = ['49271233211']
  16. assert_equal(result, Cti::CallerId.parse_text(text))
  17. text = '021-233-9123'
  18. result = ['49212339123']
  19. assert_equal(result, Cti::CallerId.parse_text(text))
  20. text = '09 123 32112'
  21. result = ['49912332112']
  22. assert_equal(result, Cti::CallerId.parse_text(text))
  23. text = '021 2331231'
  24. result = ['49212331231']
  25. assert_equal(result, Cti::CallerId.parse_text(text))
  26. text = '021 321123123'
  27. result = ['4921321123123']
  28. assert_equal(result, Cti::CallerId.parse_text(text))
  29. text = '622 32281'
  30. result = ['4962232281']
  31. assert_equal(result, Cti::CallerId.parse_text(text))
  32. text = '5754321'
  33. result = ['495754321']
  34. assert_equal(result, Cti::CallerId.parse_text(text))
  35. text = '092213212'
  36. result = ['4992213212']
  37. assert_equal(result, Cti::CallerId.parse_text(text))
  38. text = '(09)1234321'
  39. result = ['4991234321']
  40. assert_equal(result, Cti::CallerId.parse_text(text))
  41. text = '+49 30 53 00 00 000'
  42. result = ['4930530000000']
  43. assert_equal(result, Cti::CallerId.parse_text(text))
  44. text = '+49 160 0000000'
  45. result = ['491600000000']
  46. assert_equal(result, Cti::CallerId.parse_text(text))
  47. text = '+49 (0) 30 60 00 00 00-0'
  48. result = ['4930600000000']
  49. assert_equal(result, Cti::CallerId.parse_text(text))
  50. text = '0043 (0) 30 60 00 00 00-0'
  51. result = ['4330600000000']
  52. assert_equal(result, Cti::CallerId.parse_text(text))
  53. text = '0043 30 60 00 00 00-0'
  54. result = ['4330600000000']
  55. assert_equal(result, Cti::CallerId.parse_text(text))
  56. text = '1-888-407-4747'
  57. result = ['18884074747']
  58. assert_equal(result, Cti::CallerId.parse_text(text))
  59. text = 'Lorem ipsum dolor sit amet, consectetuer +49 (0) 30 60 00 00 00-0 adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel.'
  60. result = ['4930600000000']
  61. assert_equal(result, Cti::CallerId.parse_text(text))
  62. text = "GS Oberalteich\nTelefon 09422 1000 Telefax 09422 805000\nE-Mail: "
  63. result = %w(4994221000 499422805000)
  64. assert_equal(result, Cti::CallerId.parse_text(text))
  65. text = 'Tel +41 81 288 63 93 / +41 76 346 72 14 ...'
  66. result = %w(41812886393 41763467214)
  67. assert_equal(result, Cti::CallerId.parse_text(text))
  68. text = "P: +1 (949) 431 0000\nF: +1 (949) 431 0001\nW: http://znuny"
  69. result = %w(19494310000 19494310001)
  70. assert_equal(result, Cti::CallerId.parse_text(text))
  71. end
  72. test '2 lookups' do
  73. Ticket.destroy_all
  74. Cti::CallerId.destroy_all
  75. agent1 = User.create_or_update(
  76. login: 'ticket-caller_id-agent1@example.com',
  77. firstname: 'CallerId',
  78. lastname: 'Agent1',
  79. email: 'ticket-caller_id-agent1@example.com',
  80. password: 'agentpw',
  81. active: true,
  82. phone: '+49 1111 222222',
  83. fax: '+49 1111 222223',
  84. mobile: '+49 1111 222223',
  85. note: 'Phone at home: +49 1111 222224',
  86. updated_by_id: 1,
  87. created_by_id: 1,
  88. )
  89. agent2 = User.create_or_update(
  90. login: 'ticket-caller_id-agent2@example.com',
  91. firstname: 'CallerId',
  92. lastname: 'Agent2',
  93. email: 'ticket-caller_id-agent2@example.com',
  94. password: 'agentpw',
  95. phone: '+49 2222 222222',
  96. note: 'Phone at home: <b>+49 2222 222224</b>',
  97. active: true,
  98. updated_by_id: 1,
  99. created_by_id: 1,
  100. )
  101. agent3 = User.create_or_update(
  102. login: 'ticket-caller_id-agent3@example.com',
  103. firstname: 'CallerId',
  104. lastname: 'Agent3',
  105. email: 'ticket-caller_id-agent3@example.com',
  106. password: 'agentpw',
  107. phone: '+49 2222 222222',
  108. active: true,
  109. updated_by_id: 1,
  110. created_by_id: 1,
  111. )
  112. customer1 = User.create_or_update(
  113. login: 'ticket-caller_id-customer1@example.com',
  114. firstname: 'CallerId',
  115. lastname: 'Customer1',
  116. email: 'ticket-caller_id-customer1@example.com',
  117. password: 'customerpw',
  118. active: true,
  119. updated_by_id: 1,
  120. created_by_id: 1,
  121. )
  122. Cti::CallerId.rebuild
  123. caller_ids = Cti::CallerId.lookup('491111222277')
  124. assert_equal(0, caller_ids.length)
  125. caller_ids = Cti::CallerId.lookup('491111222223')
  126. assert_equal(1, caller_ids.length)
  127. assert_equal(agent1.id, caller_ids[0].user_id)
  128. assert_equal('known', caller_ids[0].level)
  129. caller_ids = Cti::CallerId.lookup('492222222222')
  130. assert_equal(2, caller_ids.length)
  131. assert_equal(agent3.id, caller_ids[0].user_id)
  132. assert_equal('known', caller_ids[0].level)
  133. assert_equal(agent2.id, caller_ids[1].user_id)
  134. assert_equal('known', caller_ids[1].level)
  135. # create ticket in group
  136. ticket1 = Ticket.create(
  137. title: 'some caller id test 1',
  138. group: Group.lookup(name: 'Users'),
  139. customer: customer1,
  140. state: Ticket::State.lookup(name: 'new'),
  141. priority: Ticket::Priority.lookup(name: '2 normal'),
  142. updated_by_id: agent1.id,
  143. created_by_id: agent1.id,
  144. )
  145. article1 = Ticket::Article.create(
  146. ticket_id: ticket1.id,
  147. from: 'some_sender@example.com',
  148. to: 'some_recipient@example.com',
  149. subject: 'some subject',
  150. message_id: 'some@id',
  151. body: "some message\nFon (GEL): +49 111 366-1111 Mi-Fr
  152. Fon (LIN): +49 222 6112222 Mo-Di
  153. Mob: +49 333 8362222",
  154. internal: false,
  155. sender: Ticket::Article::Sender.where(name: 'Customer').first,
  156. type: Ticket::Article::Type.where(name: 'email').first,
  157. updated_by_id: customer1.id,
  158. created_by_id: customer1.id,
  159. )
  160. assert(ticket1)
  161. # create ticket in group
  162. ticket2 = Ticket.create(
  163. title: 'some caller id test 2',
  164. group: Group.lookup(name: 'Users'),
  165. customer: customer1,
  166. state: Ticket::State.lookup(name: 'new'),
  167. priority: Ticket::Priority.lookup(name: '2 normal'),
  168. updated_by_id: agent1.id,
  169. created_by_id: agent1.id,
  170. )
  171. article2 = Ticket::Article.create(
  172. ticket_id: ticket2.id,
  173. from: 'some_sender@example.com',
  174. to: 'some_recipient@example.com',
  175. subject: 'some subject',
  176. message_id: 'some@id',
  177. body: "some message\nFon (GEL): +49 111 111-1111 Mi-Fr
  178. Fon (LIN): +49 222 1112222 Mo-Di
  179. Mob: +49 333 1112222",
  180. internal: false,
  181. sender: Ticket::Article::Sender.where(name: 'Agent').first,
  182. type: Ticket::Article::Type.where(name: 'email').first,
  183. updated_by_id: agent1.id,
  184. created_by_id: agent1.id,
  185. )
  186. assert(ticket2)
  187. Cti::CallerId.rebuild
  188. caller_ids = Cti::CallerId.lookup('491111222277')
  189. assert_equal(0, caller_ids.length)
  190. caller_ids = Cti::CallerId.lookup('491111222223')
  191. assert_equal(1, caller_ids.length)
  192. assert_equal(agent1.id, caller_ids[0].user_id)
  193. assert_equal('known', caller_ids[0].level)
  194. caller_ids = Cti::CallerId.lookup('492222222222')
  195. assert_equal(2, caller_ids.length)
  196. assert_equal(agent3.id, caller_ids[0].user_id)
  197. assert_equal('known', caller_ids[0].level)
  198. assert_equal(agent2.id, caller_ids[1].user_id)
  199. assert_equal('known', caller_ids[1].level)
  200. caller_ids = Cti::CallerId.lookup('492226112222')
  201. assert_equal(1, caller_ids.length)
  202. assert_equal(customer1.id, caller_ids[0].user_id)
  203. assert_equal('maybe', caller_ids[0].level)
  204. caller_ids = Cti::CallerId.lookup('492221112222')
  205. assert_equal(0, caller_ids.length)
  206. end
  207. test '3 lookups' do
  208. Cti::CallerId.destroy_all
  209. Cti::CallerId.maybe_add(
  210. caller_id: '4999999999',
  211. level: 'maybe',
  212. user_id: 2,
  213. object: 'Ticket',
  214. o_id: 2,
  215. )
  216. Cti::CallerId.maybe_add(
  217. caller_id: '4912345678901',
  218. comment: 'Hairdresser Bob Smith, San Francisco',
  219. level: 'public',
  220. user_id: 2,
  221. object: 'GoYello',
  222. o_id: 1,
  223. )
  224. caller_ids = Cti::CallerId.lookup('4912345678901')
  225. assert_equal(1, caller_ids.length)
  226. assert_equal('public', caller_ids[0].level)
  227. assert_equal(2, caller_ids[0].user_id)
  228. assert_equal('Hairdresser Bob Smith, San Francisco', caller_ids[0].comment)
  229. Cti::CallerId.maybe_add(
  230. caller_id: '4912345678901',
  231. level: 'maybe',
  232. user_id: 2,
  233. object: 'Ticket',
  234. o_id: 2,
  235. )
  236. caller_ids = Cti::CallerId.lookup('4912345678901')
  237. assert_equal(1, caller_ids.length)
  238. assert_equal('maybe', caller_ids[0].level)
  239. assert_equal(2, caller_ids[0].user_id)
  240. assert_equal(nil, caller_ids[0].comment)
  241. Cti::CallerId.maybe_add(
  242. caller_id: '4912345678901',
  243. level: 'maybe',
  244. user_id: 2,
  245. object: 'Ticket',
  246. o_id: 2,
  247. )
  248. caller_ids = Cti::CallerId.lookup('4912345678901')
  249. assert_equal(1, caller_ids.length)
  250. assert_equal('maybe', caller_ids[0].level)
  251. assert_equal(2, caller_ids[0].user_id)
  252. assert_equal(nil, caller_ids[0].comment)
  253. Cti::CallerId.maybe_add(
  254. caller_id: '4912345678901',
  255. level: 'maybe',
  256. user_id: 3,
  257. object: 'Ticket',
  258. o_id: 2,
  259. )
  260. caller_ids = Cti::CallerId.lookup('4912345678901')
  261. assert_equal(2, caller_ids.length)
  262. assert_equal('maybe', caller_ids[0].level)
  263. assert_equal(3, caller_ids[0].user_id)
  264. assert_equal(nil, caller_ids[0].comment)
  265. assert_equal('maybe', caller_ids[1].level)
  266. assert_equal(2, caller_ids[1].user_id)
  267. assert_equal(nil, caller_ids[1].comment)
  268. Cti::CallerId.maybe_add(
  269. caller_id: '4912345678901',
  270. level: 'known',
  271. user_id: 3,
  272. object: 'User',
  273. o_id: 2,
  274. )
  275. caller_ids = Cti::CallerId.lookup('4912345678901')
  276. assert_equal(1, caller_ids.length)
  277. assert_equal('known', caller_ids[0].level)
  278. assert_equal(3, caller_ids[0].user_id)
  279. assert_equal(nil, caller_ids[0].comment)
  280. end
  281. end