# encoding: utf-8 require 'test_helper' class EmailProcessTest < ActiveSupport::TestCase test 'process simple' do files = [ { :data => 'From: me@example.com To: customer@example.com Subject: some subject Some Text', :trusted => false, :success => true, }, { :data => "From: me@example.com To: customer@example.com Subject: äöü some subject Some Textäöü", :trusted => false, :success => true, :result => { 0 => { :priority => '2 normal', :title => 'äöü some subject', }, 1 => { :body => 'Some Textäöü', :sender => 'Customer', :type => 'email', :internal => false, }, }, }, { :data => "From: me@example.com To: customer@example.com Subject: äöü some subject Some Textäöü".encode("ISO-8859-1"), :success => true, :result => { 0 => { :priority => '2 normal', :title => '', # should be äöü some subject, but can not be parsed from mime tools }, 1 => { :body => 'Some Textäöü', :sender => 'Customer', :type => 'email', :internal => false, }, }, }, { :data => "From: me@example.com To: customer@example.com Subject: Subject: =?utf-8?B?44CQ5LiT5Lia5Li65oKo5rOo5YaM6aaZ5riv5Y+K5rW35aSW5YWs5Y+477yI5aW95aSE5aSa5aSa77yJ?= =?utf-8?B?44CR44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA?= =?utf-8?B?44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA?= =?utf-8?B?44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA?= =?utf-8?B?44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA?= =?utf-8?B?44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA?= =?utf-8?B?44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA?= =?utf-8?B?44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA?= =?utf-8?B?44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA?= =?utf-8?B?44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA?= =?utf-8?B?44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA?= =?utf-8?B?44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA?= =?utf-8?B?44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA?= =?utf-8?B?44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA?= =?utf-8?B?44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA?= =?utf-8?B?44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA?= =?utf-8?B?44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA?= =?utf-8?B?44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA?= =?utf-8?B?44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA?= =?utf-8?B?44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA?= =?utf-8?B?44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA?= =?utf-8?B?44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA?= =?utf-8?B?44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA?= =?utf-8?B?44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA?= =?utf-8?B?44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA?= =?utf-8?B?44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA?= =?utf-8?B?44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA?= =?utf-8?B?44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA?= =?utf-8?B?44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA?= =?utf-8?B?44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA?= =?utf-8?B?44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA?= =?utf-8?B?44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA?= =?utf-8?B?44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA?= =?utf-8?B?44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA?= =?utf-8?B?44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA?= =?utf-8?B?44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA?= =?utf-8?B?44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA?= =?utf-8?B?44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA?= =?utf-8?B?44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA?= =?utf-8?B?44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA?= =?utf-8?B?44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA?= =?utf-8?B?44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA?= =?utf-8?B?44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA?= =?utf-8?B?44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA?= =?utf-8?B?44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA?= =?utf-8?B?44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA?= =?utf-8?B?44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA44CA?= =?utf-8?B?44CA44CA44CA44CA44CA44CA44CA44CA5Lq654mpICAgICAgICAgIA==?= =?utf-8?B?ICAgICAgICAgIOS6kuiBlOe9keS6i+eZvuW6puaWsOmXu+eLrOWutg==?= =?utf-8?B?5Ye65ZOB5Lyg5aqS5o2i5LiA5om55o235YWL5oi057u05pav5p2v5Yaz6LWb5YmN5Lu75ZG95Li05pe2?= =?utf-8?B?6aKG6ZifIOWJjemihumYn+WboOeXheS9j+mZouacgOaWsDrnm5bkuJbmsb3ovaborq8gMQ==?= =?utf-8?B?MeaciDbml6XvvIzpgJrnlKjmsb3ovablrqPluIPku4rlubQxMOaciOS7veWcqOWNjumUgA==?= =?utf-8?B?6YePLi4u5YeP5oyB5LiJ54m557Si6YGTIOWtn+WHr+WwhuWFqOWKm+WPkeWxlea5mOmEgg==?= =?utf-8?B?5oOF5rGf6Z2S5pGE5b2x5L2c5ZOB56eR5oqA5pel5oql6K6vIO+8iOiusOiAhei/h+WbveW/oCA=?= =?utf-8?B?6YCa6K6v5ZGY6ZmI6aOe54eV77yJ5rGf6IuP55yB5peg57q/55S156eR5a2m56CU56m25omA5pyJ6ZmQ?= =?utf-8?B?5YWs5Y+46Zmi5aOr5bel5L2c56uZ5pel5YmN5q2j5byP5bu6Li4uW+ivpue7hl0=?= Some Text", :trusted => false, :success => true, :result => { 0 => { :priority => '2 normal', :title => '【专业为您注册香港及海外公司(好处多多)】                                                                                                                                                                                                                                     ', }, 1 => { :body => 'Some Text', :sender => 'Customer', :type => 'email', }, }, }, { :data => IO.read('test/fixtures/mail21.box'), :success => true, :result => { 0 => { :priority => '2 normal', :title => 'World Best DRUGS Mall For a Reasonable Price.', }, 1 => { :body => '_________________________________________________________________________________Please beth saw his head 92hH3ÿoI221G1¿iH16u-2◊NQ422U1awAq¹JLZμ2IicgT1ζ2Y7⊆t 63‘M236E2Ý→DA2†I048CvJ9A↑3iTc4ÉIΥvXO502N1FJSð1r 154F1HPO11CRxZp tLîT9öXH1b3Es1W mN2Bg3õEbPŒS2fτTóY4 sU2P2ζΔRFkcI21™CÓZ3EΛRq!Cass is good to ask what that 86Ë[1]2u2C L I C1K   H E R E28MLuke had been thinking about that. Shannon said nothing in fact they. Matt placed the sofa with amy smiled. Since the past him with more. Maybe he checked the phone. Neither did her name only. Ryan then went inside matt. Maybe we can have anything you sure. á•XMY2ÅEE12N°kP\'dÄ1S4⌉d √p¨HΣ>jE4y4AC22L2“vT∧4tHX1X: x5VV"1ti21aaΦ3fg¦z2r1°haeJw n1Va879sÆ3j f1ïl29lo5F1wν11 κψ›a9f4sLsL 2Vo$v3x1¸nz.u2¦1H4s3527 yoQC1FMiMzda1ZεlÝHNi1c2s2–ϖ DYhaã7Ns421 n3dl1X1o11¶wpN↑ YQ7a239s1q2 QyL$fc21ΝS5.5Wy621d5Ä1H 17∂ 3n6ax1Qs20b °Häl91ÑoÏ6aw≡d2 ΗÅ2a1Óvs⊃17 C⊆1$2Bz2sl2.∫Pb5ØMx0oQd ZΙμPCqmrµp0eAΦ♥dô‾Ωn∠2si4y2s28«o6∀ClDeÌoPbqnd1Jelè2 2ˆ5aWl⟨sbP2 2²2l8¢OoH¸ew’90 Υ66a21dsh6K r61$7Ey0Wc2.£—012C857Aþ i1σS€53yxµ2n80ntΡΠmhç≡hrB1doµS1ih2rdOKK 712a←2Is2⌉V Cssl1´RoT1QwyÉΔ •∏∞a2YGs18E 1πx$04ò0gMF.bTQ3Íx6582ς Maybe even though she followed. Does this mean you talking about. Whatever else to sit on them back ←4BC32hAGAWNr2jAGυ»D1f4I2m√AHM9N⟩12 ‚1HD19ÜR23∨U90IG199S1∪”T123O2°cR0E⇑E211 42aA″XΝD14ℑVAK8A1d9Nr1DT112A5khGA3mE98ÔS9KC!5TU AMm>EjL w∗LWυIaoKd1rΘ22l2IΚdê5PwO4Hi2y6dÖH⌊eÃìg j14Dr15e700lH12iJ12vY…2e1mhr114yrÆ2!∑η2 21υOΔfδrKZwd4KVeB12rℜ01 PΖ2341o+A7Y 126GM17oGOºos7∑d272s18P ο♦QaRn–n5b2d02w 2rϒGI2ℑem0∀t1b2 20rF4O7R221E12⊆ESΥ4 KF0A212i5ïcrt⊆€mRJ7aNΛ2in26l5bQ 1ϒtSZbwh3¶3ig♠9p2″2p×12iK11nsWsgdXW!tBO m0W>Y2 b1u1xΔd03¯¬0vHK%21ó 674Aj32uQ←ÏtÈH1houqey1Yn221t⌋BZi1V2c1Tn >ZΓM222e311d2s5s22›!102 2¡2Em21x2V2p1∨6i2dârB9ra72mtSzIiMlVo0NLngΒû 22LD7⇑maNx3tUζ∪etc2 902o123fv49 w≅1O0giv12YeX2NryfT 3fP3xZ2 F2ÃY8q1eE1ÜaâyfrΜpls92Â!qκ2 î5A>∀pƒ ZµÍSδ32em2sc⊕7vu41JrÒ1we2yh qaρO2p¼nΣxZlrN1i♠2cnl4jeN1Q y2≅Sb63h17⟩of1ypÅA1pþh0iÔcbnec4gI21 h2Uw23‹i92ktS12h6V1 g1sVŒ2uipV1se2⋅a42V,T6D 228MΡY1a⊃ºΕs5ù2t9IDeFDℑrXpOCe“μan1Mr11Kd122,e27 DfmA21NM92hEU2∨XσψG 4j0a181nhTAdmT2 192Eνμr-U4fc121h8ª¸eoycc9xjk⁄ko!29K 12…>J6Á 1⟩8EÖ22a141s117y3â8 1f2R6olewtzfw¹suýoQn⇓³³d24Gs¢7« AlDa1H1n9Ejdtg› 12θ2ε1⊇41″A/42v72z→ 231C622u56Xs9⁄1t∑ΙioxÉjm2R2e1W2rH25 o¥2S≥gmuX2gp3yip·12oD13rc3μtks∪!sWK When she were there you here. Lott to need for amy said. Once more than ever since matt. Lott said turning o ered. Tell you so matt kept going. Homegrown dandelions by herself into her lips. Such an excuse to stop thinking about. Leave us and be right. [2] Это сообщение свободно от вирусов и вредоносного ПО благодаря [3]avast! Antivirus защита активна. [1] http://piufup.medicatingsafemart.ru [2] http://www.avast.com/ [3] http://www.avast.com/ ', :sender => 'Customer', :type => 'email', :internal => false, }, }, }, { :data => IO.read('test/fixtures/mail22.box'), :success => true, :result => { 0 => { :priority => '2 normal', :title => 'P..E..N-I..S__-E N L A R-G E-M..E..N T-___P..I-L-L..S...Info.', }, 1 => { :body => "Puzzled by judith bronte dave. Melvin will want her way through with. Continued adam helped charlie cried. Soon joined the master bathroom. Grinned adam rubbed his arms she nodded. Freemont and they talked with beppe. Thinking of bed and whenever adam. Mike was too tired man to hear.I10PQSHEJl2Nwf˜2113S173 Î1mEbb5N371LϖC7AlFnR1♦HG64B242¦M2242zkΙN⌉7⌉TBNÐ T2xPIògI2ÃlL2ÕML⊥22SaΨRBreathed adam gave the master bedroom door. Better get charlie took the wall. Charlotte clark smile he saw charlie. Dave and leaned her tears adam.Maybe we want any help me that. Next morning charlie gazed at their father. Well as though adam took out here. Melvin will be more money. Called him into this one last night. Men joined the pickup truck pulled away. Chuck could make sure that.[1]†p1C?L I?C K?88 5 E R?EEOD !Chuckled adam leaned forward and le? charlie. Just then returned to believe it here. Freemont and pulling out several minutes. [1] http://аоск.рф?jmlfwnwe&ucwkiyyc ", :sender => 'Customer', :type => 'email', :internal => false, }, }, }, ] process(files) end test 'process trusted' do files = [ { :data => 'From: me@example.com To: customer@example.com Subject: some subject X-Zammad-Ignore: true Some Text', :trusted => true, :success => false, }, { :data => 'From: me@example.com To: customer@example.com Subject: some subject X-Zammad-Ticket-priority: 3 high X-Zammad-Article-sender: System x-Zammad-Article-type: phone x-Zammad-Article-Internal: true Some Text', :trusted => true, :success => true, :result => { 0 => { :priority => '3 high', :title => 'some subject', }, 1 => { :sender => 'System', :type => 'phone', :internal => true, }, }, }, ] process(files) end test 'process not trusted' do files = [ { :data => 'From: me@example.com To: customer@example.com Subject: some subject X-Zammad-Ticket-Priority: 3 high X-Zammad-Article-Sender: System x-Zammad-Article-Type: phone x-Zammad-Article-Internal: true Some Text', :trusted => false, :success => true, :result => { 0 => { :priority => '2 normal', :title => 'some subject', }, 1 => { :sender => 'Customer', :type => 'email', :internal => false, }, }, }, ] process(files) end test 'process with postmaster filter' do group = Group.create_if_not_exists( :name => 'Test Group', :created_by_id => 1, :updated_by_id => 1, ) PostmasterFilter.destroy_all PostmasterFilter.create( :name => 'not used', :match => { :from => 'nobody@example.com', }, :perform => { 'X-Zammad-Ticket-priority' => '3 high', }, :channel => 'email', :active => true, :created_by_id => 1, :updated_by_id => 1, ) PostmasterFilter.create( :name => 'used', :match => { :from => 'me@example.com', }, :perform => { 'X-Zammad-Ticket-group_id' => group.id, 'x-Zammad-Article-Internal' => true, }, :channel => 'email', :active => true, :created_by_id => 1, :updated_by_id => 1, ) PostmasterFilter.create( :name => 'used x-any-recipient', :match => { 'x-any-recipient' => 'any@example.com', }, :perform => { 'X-Zammad-Ticket-group_id' => 2, 'x-Zammad-Article-Internal' => true, }, :channel => 'email', :active => true, :created_by_id => 1, :updated_by_id => 1, ) files = [ { :data => 'From: me@example.com To: customer@example.com Subject: some subject Some Text', :trusted => false, :success => true, :result => { 0 => { :group => group.name, :priority => '2 normal', :title => 'some subject', }, 1 => { :sender => 'Customer', :type => 'email', :internal => true, }, }, }, { :data => 'From: somebody@example.com To: bod@example.com Cc: any@example.com Subject: some subject Some Text', :trusted => false, :success => true, :result => { 0 => { :group => 'Twitter', :priority => '2 normal', :title => 'some subject', }, 1 => { :sender => 'Customer', :type => 'email', :internal => true, }, }, }, ] process(files) PostmasterFilter.destroy_all end def process(files) files.each { |file| parser = Channel::EmailParser.new result = parser.process( { :trusted => file[:trusted] }, file[:data] ) if file[:success] if result && result.class == Array && result[1] assert( true ) if file[:result] [ 0, 1, 2 ].each { |level| if file[:result][level] file[:result][level].each { |key, value| if result[level].send(key).respond_to?('name') assert_equal( value.to_s, result[level].send(key).name ) else assert_equal( value, result[level].send(key)) end } end } end else assert( false, 'ticket not created', file ) end elsif !file[:success] if result && result.class == Array && result[1] puts result.inspect assert( false, 'ticket should not be created but is created' ) else assert( true, 'ticket not created - nice' ) end else assert( false, 'UNKNOWN!' ) end } end end