blackbook 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. data/History.txt +18 -0
  2. data/Manifest.txt +55 -0
  3. data/README.txt +71 -0
  4. data/Rakefile +38 -0
  5. data/init.rb +1 -0
  6. data/lib/blackbook.rb +80 -0
  7. data/lib/blackbook/exporter/base.rb +16 -0
  8. data/lib/blackbook/exporter/vcf.rb +45 -0
  9. data/lib/blackbook/exporter/xml.rb +28 -0
  10. data/lib/blackbook/importer/aol.rb +74 -0
  11. data/lib/blackbook/importer/base.rb +39 -0
  12. data/lib/blackbook/importer/csv.rb +61 -0
  13. data/lib/blackbook/importer/gmail.rb +59 -0
  14. data/lib/blackbook/importer/hotmail.rb +125 -0
  15. data/lib/blackbook/importer/page_scraper.rb +86 -0
  16. data/lib/blackbook/importer/yahoo.rb +61 -0
  17. data/test/fixtures/aol_bad_login_response_stage_3.html +565 -0
  18. data/test/fixtures/aol_contacts.html +90 -0
  19. data/test/fixtures/aol_login_response_stage_1.html +158 -0
  20. data/test/fixtures/aol_login_response_stage_2.html +559 -0
  21. data/test/fixtures/aol_login_response_stage_3.html +61 -0
  22. data/test/fixtures/aol_login_response_stage_4.html +48 -0
  23. data/test/fixtures/aol_login_response_stage_5.html +404 -0
  24. data/test/fixtures/gmail.csv +3 -0
  25. data/test/fixtures/gmail_bad_login_response_stage_2.html +560 -0
  26. data/test/fixtures/gmail_contacts.html +228 -0
  27. data/test/fixtures/gmail_login_response_stage_1.html +556 -0
  28. data/test/fixtures/gmail_login_response_stage_2.html +1 -0
  29. data/test/fixtures/gmail_login_response_stage_3.html +249 -0
  30. data/test/fixtures/hotmail_bad_login_response_stage_2.html +31 -0
  31. data/test/fixtures/hotmail_contacts.html +132 -0
  32. data/test/fixtures/hotmail_login_response_stage_1.html +31 -0
  33. data/test/fixtures/hotmail_login_response_stage_2.html +1 -0
  34. data/test/fixtures/hotmail_login_response_stage_3.html +380 -0
  35. data/test/fixtures/yahoo_bad_login_response_stage_2.html +443 -0
  36. data/test/fixtures/yahoo_contacts.csv +3 -0
  37. data/test/fixtures/yahoo_contacts_not_logged_in.html +432 -0
  38. data/test/fixtures/yahoo_contacts_stage_1.html +399 -0
  39. data/test/fixtures/yahoo_login_response_stage_1.html +433 -0
  40. data/test/fixtures/yahoo_login_response_stage_2.html +16 -0
  41. data/test/scripts/live_test.rb +25 -0
  42. data/test/test_blackbook.rb +60 -0
  43. data/test/test_blackbook_exporter_base.rb +16 -0
  44. data/test/test_blackbook_exporter_vcf.rb +52 -0
  45. data/test/test_blackbook_exporter_xml.rb +16 -0
  46. data/test/test_blackbook_importer_aol.rb +107 -0
  47. data/test/test_blackbook_importer_base.rb +24 -0
  48. data/test/test_blackbook_importer_csv.rb +60 -0
  49. data/test/test_blackbook_importer_gmail.rb +96 -0
  50. data/test/test_blackbook_importer_hotmail.rb +143 -0
  51. data/test/test_blackbook_importer_page_scraper.rb +51 -0
  52. data/test/test_blackbook_importer_yahoo.rb +97 -0
  53. data/test/test_helper.rb +47 -0
  54. data/vendor/plugins/blackbook/lib/autotest/blackbook.rb +27 -0
  55. data/vendor/plugins/blackbook/lib/autotest/discover.rb +3 -0
  56. metadata +147 -0
@@ -0,0 +1,60 @@
1
+ require File.join( File.dirname(__FILE__), '../lib/blackbook.rb' )
2
+ require File.join( File.dirname(__FILE__), 'test_helper.rb' )
3
+ require 'test/unit'
4
+ require 'mocha'
5
+
6
+ class TestBlackbook < Test::Unit::TestCase
7
+
8
+ include TestHelper
9
+
10
+ def test_class_get
11
+ Blackbook.any_instance.expects(:get)
12
+ Blackbook.get
13
+ end
14
+
15
+ def test_class_instance
16
+ assert Blackbook.instance.is_a?(Blackbook)
17
+ end
18
+
19
+ def test_class_register
20
+ Blackbook.register(:base, Blackbook::Exporter::Base)
21
+ assert Blackbook.instance.exporters[:base].is_a?(Blackbook::Exporter::Base)
22
+
23
+ Blackbook.register(:base, Blackbook::Importer::Base)
24
+ assert Blackbook.instance.importers[:base].is_a?(Blackbook::Importer::Base)
25
+ end
26
+
27
+ def test_class_register_raises_on_bad_register
28
+ assert_raises(ArgumentError) do
29
+ Blackbook.register(:bad, String)
30
+ end
31
+ end
32
+
33
+ def test_export
34
+ importer = Blackbook::Importer::Base.new
35
+ exporter = Blackbook::Exporter::Base.new
36
+ importer.expects(:import)
37
+ exporter.expects(:export)
38
+ Blackbook.instance.export( importer, exporter, {} )
39
+ end
40
+
41
+ def test_find_importer
42
+ base = Blackbook::Importer::Base.new
43
+ base.stubs(:=~).returns(true)
44
+ Blackbook.any_instance.stubs(:importers).returns({:basic => base})
45
+ assert_equal base, Blackbook.instance.find_importer(:as => :basic)
46
+
47
+ base.stubs(:=~).returns(false)
48
+ assert_nil Blackbook.instance.find_importer(:as => :basic)
49
+ end
50
+
51
+ def test_get
52
+ cards = Blackbook.instance.get( :csv, :file => fixture_file('gmail.csv') )
53
+ assert_equal 2, cards.size
54
+ assert cards.detect{|card| card[:name] == "Joe User"}
55
+ assert cards.detect{|card| card[:name] == "Some Guy"}
56
+ assert cards.detect{|card| card[:email] == "joeuser@example.com"}
57
+ assert cards.detect{|card| card[:email] == "someguy@example.com"}
58
+ end
59
+
60
+ end
@@ -0,0 +1,16 @@
1
+ require File.join( File.dirname(__FILE__), '../lib/blackbook.rb' )
2
+ require File.join( File.dirname(__FILE__), 'test_helper.rb' )
3
+ require 'test/unit'
4
+ require 'mocha'
5
+
6
+ class TestBlackbookExporterBase < Test::Unit::TestCase
7
+
8
+ include TestHelper
9
+
10
+ def test_export
11
+ exporter = Blackbook::Exporter::Base.new
12
+ contacts = [{ :name => 'Test', :email => 'test@domain.com' }]
13
+ assert_equal contacts, exporter.export(contacts)
14
+ end
15
+
16
+ end
@@ -0,0 +1,52 @@
1
+ require File.join( File.dirname(__FILE__), '../lib/blackbook.rb' )
2
+ require File.join( File.dirname(__FILE__), 'test_helper.rb' )
3
+ require 'test/unit'
4
+ require 'mocha'
5
+
6
+ class TestBlackbookExporterVcf < Test::Unit::TestCase
7
+
8
+ include TestHelper
9
+
10
+ def setup
11
+ @card = Blackbook::Exporter::Vcf::Vcard.new(
12
+ {'first' => 'joe', 'last' => 'user',
13
+ 'email' => 'joe.user@example.com'})
14
+ @exporter = Blackbook::Exporter::Vcf.new
15
+ end
16
+
17
+ def test_vcard
18
+ assert_equal 'joe', @card.first
19
+ assert_equal 'user', @card.last
20
+ assert_equal 'joe.user@example.com', @card.email
21
+ end
22
+
23
+ def test_vcard_to_s
24
+ card = %q!BEGIN:VCARD
25
+ N:user;joe
26
+ EMAIL:joe.user@example.com
27
+ END:VCARD
28
+ !
29
+ assert_equal card, @card.to_s
30
+ assert_equal 'user', @card.last
31
+ assert_equal 'joe.user@example.com', @card.email
32
+ end
33
+
34
+ def test_export
35
+ contacts = [{ :name => 'Test', :email => 'test@example.com' }]
36
+ expected = <<VCF
37
+ BEGIN:VCARD
38
+ N:;Test
39
+ EMAIL:test@example.com
40
+ END:VCARD
41
+ VCF
42
+ exported = @exporter.export(contacts)
43
+ assert_equal 1, exported.size
44
+ assert_equal expected, exported.first.to_s
45
+ end
46
+
47
+ def test_export_when_contacts_blank
48
+ assert_nil @exporter.export()
49
+ assert_nil @exporter.export([])
50
+ end
51
+
52
+ end
@@ -0,0 +1,16 @@
1
+ require File.join( File.dirname(__FILE__), '../lib/blackbook.rb' )
2
+ require File.join( File.dirname(__FILE__), 'test_helper.rb' )
3
+ require 'test/unit'
4
+ require 'mocha'
5
+
6
+ class TestBlackbookExporterXml < Test::Unit::TestCase
7
+
8
+ include TestHelper
9
+
10
+ def test_export
11
+ exporter = Blackbook::Exporter::Xml.new
12
+ contacts = [{ :name => 'Test', :email => 'test@domain.com' }]
13
+ assert_equal "<?xml version='1.0'?><contacts><contact><name>Test</name><email>test@domain.com</email></contact></contacts>", exporter.export(contacts)
14
+ end
15
+
16
+ end
@@ -0,0 +1,107 @@
1
+ require File.join( File.dirname(__FILE__), '../lib/blackbook.rb' )
2
+ require File.join( File.dirname(__FILE__), 'test_helper.rb' )
3
+ require 'test/unit'
4
+ require 'mocha'
5
+ require 'mechanize'
6
+
7
+
8
+ class TestBlackbookImporterAol < Test::Unit::TestCase
9
+
10
+ include TestHelper
11
+
12
+ def setup
13
+ @importer = Blackbook::Importer::Aol.new
14
+ @importer.options = {:username => 'user@aol.com', :password => 'password'}
15
+ @importer.create_agent
16
+ end
17
+
18
+ def test_prepare
19
+ @importer.expects(:login).once
20
+ @importer.prepare
21
+ end
22
+
23
+ def test_importer_match
24
+ assert_equal false, @importer =~ nil
25
+ assert_equal false, @importer =~ {}
26
+ assert_equal false, @importer =~ {'username' => 'joe@example.com'}
27
+ assert_equal false, @importer =~ {:username => 'joe@example.com'}
28
+ assert_equal false, @importer =~ {:username => 'joe'}
29
+ assert_equal true, @importer =~ {:username => 'joe@aol.com'}
30
+ assert_equal true, @importer =~ {:username => 'joe@aim.com'}
31
+ assert_equal true, @importer =~ {:username => 'JOE@AOL.COM'}
32
+ assert_equal true, @importer =~ {:username => 'JOE@AIM.COM'}
33
+ end
34
+
35
+ def test_login
36
+ response = {'content-type' => 'text/html'}
37
+
38
+ body = load_fixture('aol_login_response_stage_2.html').join
39
+ page = WWW::Mechanize::Page.new(uri=nil, response, body, code=nil, mech=nil)
40
+ @importer.agent.expects(:get).with('http://webmail.aol.com/').once.returns(page)
41
+
42
+ body = load_fixture('aol_login_response_stage_3.html').join
43
+ page = WWW::Mechanize::Page.new(uri=nil, response, body, code=nil, mech=nil)
44
+ @importer.agent.expects(:submit).once.returns(page)
45
+
46
+ body = load_fixture('aol_login_response_stage_4.html').join
47
+ page = WWW::Mechanize::Page.new(uri=nil, response, body, code=nil, mech=nil)
48
+ @importer.agent.expects(:get).with(
49
+ 'http://webmail.aol.com/_cqr/LoginSuccess.aspx?sitedomain=sns.webmail.aol.com&authLev=2&siteState=ver%3A2%7Cac%3AWS%7Cat%3ASNS%7Cld%3Awebmail.aol.com%7Cuv%3AAOL%7Clc%3Aen-us&lang=en&locale=us&uitype=std&mcAuth=%2Fblackbookauthtest%3D%3D'
50
+ ).once.returns(page)
51
+
52
+ body = load_fixture('aol_login_response_stage_5.html').join
53
+ page = WWW::Mechanize::Page.new(uri=nil, response, body, code=nil, mech=nil)
54
+ @importer.agent.expects(:get).with('http://webmail.aol.com/31361/aol/en-us/Suite.aspx').once.returns(page)
55
+
56
+ assert_nothing_raised do
57
+ assert @importer.login
58
+ end
59
+ end
60
+
61
+ def test_login_bad_credentials
62
+ response = {'content-type' => 'text/html'}
63
+
64
+ body = load_fixture('aol_login_response_stage_2.html').join
65
+ page = WWW::Mechanize::Page.new(uri=nil, response, body, code=nil, mech=nil)
66
+ @importer.agent.expects(:get).with('http://webmail.aol.com/').once.returns(page)
67
+
68
+ body = load_fixture('aol_bad_login_response_stage_3.html').join
69
+ page = WWW::Mechanize::Page.new(uri=nil, response, body, code=nil, mech=nil)
70
+ @importer.agent.expects(:submit).once.returns(page)
71
+
72
+ assert_raises(Blackbook::BadCredentialsError) do
73
+ @importer.login
74
+ end
75
+ end
76
+
77
+ def test_scrape_contacts_raises_badcredentialerror_when_not_logged_in
78
+ @importer.agent.expects(:cookies).once.returns([])
79
+ assert_raises(Blackbook::BadCredentialsError) do
80
+ @importer.scrape_contacts
81
+ end
82
+ end
83
+
84
+ def test_scrape_contacts
85
+ cookie = WWW::Mechanize::Cookie.new('Auth',
86
+ 'ver:7&uas:user%2cuser%2caim.com%2c0%2c0%2c0%2cAIM%2cen-us%2c2%2c633308135438559961%2c0%2c1%3b&un:user&at:SNS&sn:user&wim:djEgMVM%253D-Qg0R3W9DSOpFNfKRog8SxBigVbPg%252BDTtGsaBctTczECZruX82XONmIMGYsY%253D&sty:0&ud:aim.com&uid:blackbookuid&ss:633308135438559961&sst:1195234657&la:633308135438559961&lrid:25156&act:M&br:0&mbt:T&uv:AIM&lc:en-us&acd:315532800&pix:0&prmc:135925&relm:im&mah:&sah:&snh:ZDdhMTQ5YjlmZjQ5NTE5ZTFkYWI5OTU0ZDU1NWNlYTM%3d&miu:True&sit:sns.webmail.aol.com&ckd:.webmail.aol.com&ckp:%2f&ha:ABrytqN65h7Czwu0%2bDXlQGuc%2fQY%3d&')
87
+ cookie.domain = 'localhost'
88
+ @importer.agent.expects(:cookies).once.returns([cookie])
89
+
90
+ response = {'content-type' => 'text/html'}
91
+ body = load_fixture('aol_contacts.html').join
92
+ page = WWW::Mechanize::Page.new(uri=nil, response, body, code=nil, mech=nil)
93
+ @importer.agent.expects(:get).with(
94
+ 'http://webmail.aol.com/aim/en-us/Lite/addresslist-print.aspx?command=all&sort=FirstLastNick&sortDir=Ascending&nameFormat=FirstLastNick&user=blackbookuid'
95
+ ).once.returns(page)
96
+
97
+ assert_nothing_raised do
98
+ contacts = @importer.scrape_contacts
99
+ assert_equal 2, contacts.size
100
+ assert contacts.detect{|c| c[:email] == 'joe.user@example.com'}
101
+ assert contacts.detect{|c| c[:name] == 'Joe User'}
102
+ assert contacts.detect{|c| c[:email] == 'jane.user@example.com'}
103
+ assert contacts.detect{|c| c[:name] == 'Jane User'}
104
+ end
105
+ end
106
+
107
+ end
@@ -0,0 +1,24 @@
1
+ require File.join( File.dirname(__FILE__), '../lib/blackbook.rb' )
2
+ require File.join( File.dirname(__FILE__), 'test_helper.rb' )
3
+ require 'test/unit'
4
+ require 'mocha'
5
+
6
+ class TestBlackbookImporterBase < Test::Unit::TestCase
7
+
8
+ include TestHelper
9
+
10
+ def test_service_name
11
+ assert_equal "Base", Blackbook::Importer::Base.new.service_name
12
+ end
13
+
14
+ def test_import
15
+ importer = Blackbook::Importer::Base.new
16
+ importer.expects(:fetch_contacts!)
17
+ options = {:foo => 'bar'}
18
+ assert_nothing_raised do
19
+ importer.import(options)
20
+ end
21
+ assert_equal options, importer.options
22
+ end
23
+
24
+ end
@@ -0,0 +1,60 @@
1
+ require File.join( File.dirname(__FILE__), '../lib/blackbook.rb' )
2
+ require File.join( File.dirname(__FILE__), 'test_helper.rb' )
3
+ require 'tempfile'
4
+ require 'test/unit'
5
+ require 'mocha'
6
+
7
+ class TestBlackbookImporterCsv < Test::Unit::TestCase
8
+
9
+ include TestHelper
10
+
11
+ def setup
12
+ @importer = Blackbook::Importer::Csv.new
13
+ end
14
+
15
+ def test_importer_match
16
+ csv = Tempfile.new('test.csv')
17
+ assert_equal false, @importer =~ nil
18
+ assert_equal false, @importer =~ {}
19
+ assert_equal false, @importer =~ {:file => nil}
20
+ assert_equal true, @importer =~ {:file => csv}
21
+ end
22
+
23
+ def test_to_hash
24
+ cols = [:name, :email, :misc]
25
+ assert_equal({:name => 'joe', :email => 'joe@example.com', :misc => ''},
26
+ @importer.to_hash(cols,['joe', 'joe@example.com']))
27
+ assert_equal({:name => 'joe', :email => 'joe@example.com', :misc => 'foo'},
28
+ @importer.to_hash(cols,['joe', 'joe@example.com', 'foo']))
29
+ assert_equal({:name => 'joe', :email => 'joe@example.com', :misc => 'foo,bar'},
30
+ @importer.to_hash(cols,['joe', 'joe@example.com', 'foo', 'bar']))
31
+ end
32
+
33
+ def test_to_columns
34
+ assert_equal [:name,:email,:misc], @importer.to_columns('name,email,misc')
35
+ assert_equal [:name,:email,:misc], @importer.to_columns('Name,email,misc')
36
+ assert_equal [:name,:email,:misc], @importer.to_columns('Name,Email,misc')
37
+ assert_equal [:name,:email,:misc], @importer.to_columns('Name,E-mail,misc')
38
+ end
39
+
40
+ def test_fetch_contacts_with_no_column_names
41
+ file = mock(:path => '/tmp/test.csv')
42
+ options = {:file => file}
43
+ @importer.instance_variable_set(:@options, options)
44
+ IO.expects(:readlines).with('/tmp/test.csv').once.returns(['joe,joe@example.com','fred,fred@example.com'])
45
+ expected = [{:name => 'joe', :email => 'joe@example.com', :misc => ''},
46
+ {:name => 'fred', :email => 'fred@example.com', :misc => ''}]
47
+ assert_equal expected, @importer.fetch_contacts!
48
+ end
49
+
50
+ def test_fetch_contacts_with_column_names
51
+ file = mock(:path => '/tmp/test.csv')
52
+ options = {:file => file}
53
+ @importer.instance_variable_set(:@options, options)
54
+ IO.expects(:readlines).with('/tmp/test.csv').once.returns(['name,email,misc','joe,joe@example.com','fred,fred@example.com'])
55
+ expected = [{:name => 'joe', :email => 'joe@example.com', :misc => ''},
56
+ {:name => 'fred', :email => 'fred@example.com', :misc => ''}]
57
+ assert_equal expected, @importer.fetch_contacts!
58
+ end
59
+
60
+ end
@@ -0,0 +1,96 @@
1
+ require File.join( File.dirname(__FILE__), '../lib/blackbook.rb' )
2
+ require File.join( File.dirname(__FILE__), 'test_helper.rb' )
3
+ require 'test/unit'
4
+ require 'mocha'
5
+
6
+ class TestBlackbookImporterGmail < Test::Unit::TestCase
7
+
8
+ include TestHelper
9
+
10
+ def setup
11
+ @importer = Blackbook::Importer::Gmail.new
12
+ @importer.options = {:username => 'user@gmail.com', :password => 'password'}
13
+ @importer.create_agent
14
+ end
15
+
16
+ def test_prepare
17
+ @importer.expects(:login).once
18
+ @importer.prepare
19
+ end
20
+
21
+ def test_importer_match
22
+ assert_equal false, @importer =~ nil
23
+ assert_equal false, @importer =~ {}
24
+ assert_equal false, @importer =~ {'username' => 'joe@example.com'}
25
+ assert_equal false, @importer =~ {:username => 'joe@example.com'}
26
+ assert_equal false, @importer =~ {:username => 'joe'}
27
+ assert_equal true, @importer =~ {:username => 'joe@gmail.com'}
28
+ assert_equal true, @importer =~ {:username => 'JOE@GMAIL.COM'}
29
+ end
30
+
31
+ def test_bad_login
32
+ response = {'content-type' => 'text/html'}
33
+
34
+ body = load_fixture('gmail_login_response_stage_1.html').join
35
+ page = WWW::Mechanize::Page.new(uri=nil, response, body, code=nil, mech=nil)
36
+ @importer.agent.expects(:get).with('http://mail.google.com/').once.returns(page)
37
+
38
+ body = load_fixture('gmail_bad_login_response_stage_2.html').join
39
+ page = WWW::Mechanize::Page.new(uri=nil, response, body, code=nil, mech=nil)
40
+ @importer.agent.expects(:submit).once.returns(page)
41
+
42
+ assert_raises(Blackbook::BadCredentialsError) do
43
+ @importer.login
44
+ end
45
+ end
46
+
47
+ def test_login
48
+ response = {'content-type' => 'text/html'}
49
+
50
+ body = load_fixture('gmail_login_response_stage_1.html').join
51
+ page = WWW::Mechanize::Page.new(uri=nil, response, body, code=nil, mech=nil)
52
+ @importer.agent.expects(:get).with('http://mail.google.com/').once.returns(page)
53
+
54
+ body = load_fixture('gmail_login_response_stage_2.html').join
55
+ page = WWW::Mechanize::Page.new(uri=nil, response, body, code=nil, mech=nil)
56
+ @importer.agent.expects(:submit).once.returns(page)
57
+
58
+ body = load_fixture('gmail_login_response_stage_3.html').join
59
+ page = WWW::Mechanize::Page.new(uri=nil, response, body, code=nil, mech=nil)
60
+ @importer.agent.expects(:get).with('http://mail.google.com/mail/?ui=html&zy=l'
61
+ ).once.returns(page)
62
+
63
+ assert_nothing_raised do
64
+ assert @importer.login
65
+ end
66
+ end
67
+
68
+ def test_scrape_contacts_raises_badcredentialerror_when_not_logged_in
69
+ @importer.agent.expects(:cookies).once.returns([])
70
+ assert_raises(Blackbook::BadCredentialsError) do
71
+ @importer.scrape_contacts
72
+ end
73
+ end
74
+
75
+ def test_scrape_contacts
76
+ cookie = WWW::Mechanize::Cookie.new('GAUSR', 'mail:user@gmail.com')
77
+ @importer.agent.expects(:cookies).once.returns([cookie])
78
+
79
+ response = {'content-type' => 'text/html'}
80
+ body = load_fixture('gmail_contacts.html').join
81
+ page = WWW::Mechanize::Page.new(uri=nil, response, body, code=nil, mech=nil)
82
+ @importer.agent.expects(:get).with(
83
+ 'http://mail.google.com/mail/h/?v=cl&pnl=a'
84
+ ).once.returns(page)
85
+
86
+ assert_nothing_raised do
87
+ contacts = @importer.scrape_contacts
88
+ assert_equal 2, contacts.size
89
+ assert contacts.detect{|c| c[:email] == 'joe.user@example.com'}
90
+ assert contacts.detect{|c| c[:name] == 'Joe User'}
91
+ assert contacts.detect{|c| c[:email] == 'jane.user@example.com'}
92
+ assert contacts.detect{|c| c[:name] == 'Jane User'}
93
+ end
94
+ end
95
+
96
+ end
@@ -0,0 +1,143 @@
1
+ require File.join( File.dirname(__FILE__), '../lib/blackbook.rb' )
2
+ require File.join( File.dirname(__FILE__), 'test_helper.rb' )
3
+ require 'test/unit'
4
+ require 'mocha'
5
+ require 'mechanize'
6
+
7
+ class TestBlackbookImporterHotmail < Test::Unit::TestCase
8
+
9
+ include TestHelper
10
+
11
+
12
+ def setup
13
+ @importer = Blackbook::Importer::Hotmail.new
14
+ @importer.options = {:username => 'user@hotmail.com', :password => 'password'}
15
+ @importer.create_agent
16
+ end
17
+
18
+ def test_bad_login
19
+ response = {'content-type' => 'text/html'}
20
+
21
+ body = load_fixture('hotmail_login_response_stage_1.html').join
22
+ page = WWW::Mechanize::Page.new(uri=nil, response, body, code=nil, mech=nil)
23
+ @importer.agent.expects(:get).with('http://login.live.com/login.srf?id=2'
24
+ ).once.returns(page)
25
+
26
+ body = load_fixture('hotmail_bad_login_response_stage_2.html').join
27
+ page = WWW::Mechanize::Page.new(uri=nil, response, body, code=nil, mech=nil)
28
+ @importer.agent.expects(:submit).once.returns(page)
29
+
30
+ assert_raises(Blackbook::BadCredentialsError) do
31
+ @importer.login
32
+ end
33
+
34
+ end
35
+
36
+ def test_login
37
+ response = {'content-type' => 'text/html'}
38
+
39
+ body = load_fixture('hotmail_login_response_stage_1.html').join
40
+ page = WWW::Mechanize::Page.new(uri=nil, response, body, code=nil, mech=nil)
41
+ @importer.agent.expects(:get).with('http://login.live.com/login.srf?id=2'
42
+ ).once.returns(page)
43
+
44
+ body = load_fixture('hotmail_login_response_stage_2.html').join
45
+ page = WWW::Mechanize::Page.new(uri=nil, response, body, code=nil, mech=nil)
46
+ @importer.agent.expects(:submit).once.returns(page)
47
+
48
+ body = load_fixture('hotmail_login_response_stage_3.html').join
49
+ page = WWW::Mechanize::Page.new(uri=nil, response, body, code=nil, mech=nil)
50
+ @importer.agent.expects(:get).with(
51
+ 'http://www.hotmail.msn.com/cgi-bin/sbox?t=9m9!oYK!qAKigHv8qDw1X1CMCqcbK10gIMWFQjyEK5dofUz!Aneq2zVs58p30sL*Vn8LAwVDD7952o!s1iny8uLHnZxso8YLLoQo4Z3CxhDx11tpPojydwUvwNF9zLFas7&p=9ZiSOOa!3CzsA5AbkqScGrB6w0zuXf!S*UaqQfk0fTM54toklVx51mUYcS3O8JAxjQmsQiwdEBIueipsTADcMs!2RRit547E5MtOMhA6NdiUNkoRvaGsSXK*j1iKAm7Z5jpdKpvW8oSv5yNpN5mWoULuD6lUEqC7i0KHR22e3NJ95C8uGbIGfgxkCjluo7Ye0eJcGraNIAbG0$&lc=1033&id=2'
52
+ ).once.returns(page)
53
+
54
+ assert_nothing_raised do
55
+ @importer.login
56
+ end
57
+ end
58
+
59
+ def test_scrape_contacts_not_logged_in
60
+ @importer.agent.expects(:cookies).once.returns([])
61
+ assert_raises(Blackbook::BadCredentialsError) do
62
+ @importer.scrape_contacts
63
+ end
64
+ end
65
+
66
+ def test_scrape_contacts
67
+ cookie = WWW::Mechanize::Cookie.new('MSPPre', 'user@hotmail.com')
68
+ cookie.domain = 'localhost'
69
+ @importer.agent.expects(:cookies).once.returns([cookie])
70
+
71
+ response = {'content-type' => 'text/html'}
72
+ body = load_fixture('hotmail_contacts.html').join
73
+ uri = URI.parse('http://by135w.bay135.mail.live.com/mail/')
74
+ page = WWW::Mechanize::Page.new(uri, response, body, code=nil, mech=nil)
75
+ @importer.agent.expects(:get).with('PrintShell.aspx?type=contact').once.returns(page)
76
+
77
+ assert_nothing_raised do
78
+ contacts = @importer.scrape_contacts
79
+ assert_equal 2, contacts.size
80
+ assert contacts.detect{|c| c[:email] == 'joe.user@example.com'}
81
+ assert contacts.detect{|c| c[:name] == 'Joe User'}
82
+ assert contacts.detect{|c| c[:email] == 'jane.user@example.com'}
83
+ assert contacts.detect{|c| c[:name] == 'Jane User'}
84
+ end
85
+ end
86
+
87
+ def test_prepare
88
+ @importer.expects(:login).once
89
+ @importer.prepare
90
+ end
91
+
92
+ def test_importer_match
93
+ assert_equal false, @importer =~ nil
94
+ assert_equal false, @importer =~ {}
95
+ assert_equal false, @importer =~ {'username' => 'joe@example.com'}
96
+ assert_equal false, @importer =~ {:username => 'joe@example.com'}
97
+ assert_equal false, @importer =~ {:username => 'joe'}
98
+
99
+ domains = ["compaq.net",
100
+ "hotmail.co.jp",
101
+ "hotmail.co.uk",
102
+ "hotmail.com",
103
+ "hotmail.de",
104
+ "hotmail.fr",
105
+ "hotmail.it",
106
+ "messengeruser.com",
107
+ "msn.com",
108
+ "passport.com",
109
+ "webtv.net"]
110
+ domains.each do |domain|
111
+ assert_equal true, @importer =~ {:username => "joe@#{domain}"}
112
+ assert_equal true, @importer =~ {:username => "joe@#{domain}".upcase}
113
+ end
114
+ end
115
+
116
+
117
+ def test_login_url
118
+ assert_equal "https://login.live.com/ppsecure/post.srf", @importer.login_url
119
+ @importer.options = {:username => 'user@msn.com', :password => 'password'}
120
+ assert_equal "https://msnia.login.live.com/ppsecure/post.srf", @importer.login_url
121
+ end
122
+
123
+ def test_current_host
124
+ response = {'content-type' => 'text/html'}
125
+ page = WWW::Mechanize::Page.new(URI.parse('http://localhost/'), response, '<html></html>', code=nil, mech=nil)
126
+
127
+ importer = Blackbook::Importer::Hotmail.new
128
+ assert_nil importer.current_host
129
+ importer.create_agent
130
+ assert_nil importer.current_host
131
+ importer.agent.expects(:current_page).times(2).returns(page)
132
+ assert_equal 'http://localhost', importer.current_host
133
+ end
134
+
135
+ def test_username_domain(username = nil)
136
+ importer = Blackbook::Importer::Hotmail.new
137
+ assert_nil importer.username_domain
138
+ assert_equal 'example.com', importer.username_domain('user@example.com')
139
+ assert_equal 'example.com', @importer.username_domain('user@example.com')
140
+ assert_equal 'hotmail.com', @importer.username_domain
141
+ end
142
+
143
+ end