alexjp-blackbook_csv 1.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES.markdown +44 -0
- data/Manifest.txt +59 -0
- data/README.markdown +72 -0
- data/Rakefile +39 -0
- data/debug_blackbook.rb +10 -0
- data/init.rb +1 -0
- data/lib/blackbook/exporter/base.rb +16 -0
- data/lib/blackbook/exporter/vcf.rb +45 -0
- data/lib/blackbook/exporter/xml.rb +28 -0
- data/lib/blackbook/importer/aol.rb +83 -0
- data/lib/blackbook/importer/base.rb +39 -0
- data/lib/blackbook/importer/csv.rb +87 -0
- data/lib/blackbook/importer/gmail.rb +66 -0
- data/lib/blackbook/importer/hotmail.rb +124 -0
- data/lib/blackbook/importer/page_scraper.rb +86 -0
- data/lib/blackbook/importer/yahoo.rb +61 -0
- data/lib/blackbook.rb +76 -0
- data/test/fixtures/aol_application_page.html +566 -0
- data/test/fixtures/aol_bad_login_response_stage_3.html +565 -0
- data/test/fixtures/aol_contacts.html +90 -0
- data/test/fixtures/aol_login_response_stage_1.html +158 -0
- data/test/fixtures/aol_login_response_stage_2.html +559 -0
- data/test/fixtures/aol_login_response_stage_3.html +61 -0
- data/test/fixtures/aol_login_response_stage_4.html +48 -0
- data/test/fixtures/aol_login_response_stage_5.html +404 -0
- data/test/fixtures/aol_new_contacts.html +431 -0
- data/test/fixtures/gmail.csv +3 -0
- data/test/fixtures/gmail_bad_login_response_stage_2.html +560 -0
- data/test/fixtures/gmail_contacts.html +228 -0
- data/test/fixtures/gmail_login_response_stage_1.html +556 -0
- data/test/fixtures/gmail_login_response_stage_2.html +1 -0
- data/test/fixtures/gmail_login_response_stage_2a.html +1 -0
- data/test/fixtures/gmail_login_response_stage_3.html +249 -0
- data/test/fixtures/hotmail_bad_login_response_stage_2.html +31 -0
- data/test/fixtures/hotmail_contacts.html +191 -0
- data/test/fixtures/hotmail_login_response_stage_1.html +31 -0
- data/test/fixtures/hotmail_login_response_stage_2.html +1 -0
- data/test/fixtures/hotmail_login_response_stage_3.html +380 -0
- data/test/fixtures/yahoo_bad_login_response_stage_2.html +443 -0
- data/test/fixtures/yahoo_contacts.csv +3 -0
- data/test/fixtures/yahoo_contacts_not_logged_in.html +432 -0
- data/test/fixtures/yahoo_contacts_stage_1.html +399 -0
- data/test/fixtures/yahoo_login_response_stage_1.html +433 -0
- data/test/fixtures/yahoo_login_response_stage_2.html +16 -0
- data/test/scripts/live_test.rb +25 -0
- data/test/test_blackbook.rb +60 -0
- data/test/test_blackbook_exporter_base.rb +16 -0
- data/test/test_blackbook_exporter_vcf.rb +52 -0
- data/test/test_blackbook_exporter_xml.rb +16 -0
- data/test/test_blackbook_importer_aol.rb +113 -0
- data/test/test_blackbook_importer_base.rb +24 -0
- data/test/test_blackbook_importer_csv.rb +60 -0
- data/test/test_blackbook_importer_gmail.rb +117 -0
- data/test/test_blackbook_importer_hotmail.rb +147 -0
- data/test/test_blackbook_importer_page_scraper.rb +51 -0
- data/test/test_blackbook_importer_yahoo.rb +97 -0
- data/test/test_helper.rb +69 -0
- data/vendor/plugins/blackbook/lib/autotest/blackbook.rb +27 -0
- data/vendor/plugins/blackbook/lib/autotest/discover.rb +3 -0
- metadata +176 -0
@@ -0,0 +1,113 @@
|
|
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('/31361/aim/en-us/Lite/Today.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_application_page.html').join
|
92
|
+
uri = URI.parse('http://webmail.aol.com/34865/aim/en-us/Lite/Today.aspx')
|
93
|
+
page = WWW::Mechanize::Page.new(uri, response, body, code=nil, mech=nil)
|
94
|
+
@importer.agent.expects(:current_page).times(2).returns(page)
|
95
|
+
|
96
|
+
response = {'content-type' => 'text/html'}
|
97
|
+
body = load_fixture('aol_contacts.html').join
|
98
|
+
page = WWW::Mechanize::Page.new(uri=nil, response, body, code=nil, mech=nil)
|
99
|
+
@importer.agent.expects(:get).with(
|
100
|
+
'http://webmail.aol.com/34865/aim/en-us/Lite/addresslist-print.aspx?command=all&sort=FirstLastNick&sortDir=Ascending&nameFormat=FirstLastNick&user=ULKLrUelQ2'
|
101
|
+
).once.returns(page)
|
102
|
+
|
103
|
+
assert_nothing_raised do
|
104
|
+
contacts = @importer.scrape_contacts
|
105
|
+
assert_equal 2, contacts.size
|
106
|
+
assert contacts.detect{|c| c[:email] == 'joe.user@example.com'}
|
107
|
+
assert contacts.detect{|c| c[:name] == 'Joe User'}
|
108
|
+
assert contacts.detect{|c| c[:email] == 'jane.user@example.com'}
|
109
|
+
assert contacts.detect{|c| c[:name] == 'Jane User'}
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
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,117 @@
|
|
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/mail/').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/mail/').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_login_alternative_uri
|
69
|
+
response = {'content-type' => 'text/html'}
|
70
|
+
|
71
|
+
body = load_fixture('gmail_login_response_stage_1.html').join
|
72
|
+
page = WWW::Mechanize::Page.new(uri=nil, response, body, code=nil, mech=nil)
|
73
|
+
@importer.agent.expects(:get).with('http://mail.google.com/mail/').once.returns(page)
|
74
|
+
|
75
|
+
body = load_fixture('gmail_login_response_stage_2a.html').join
|
76
|
+
page = WWW::Mechanize::Page.new(uri=nil, response, body, code=nil, mech=nil)
|
77
|
+
@importer.agent.expects(:submit).once.returns(page)
|
78
|
+
|
79
|
+
body = load_fixture('gmail_login_response_stage_3.html').join
|
80
|
+
page = WWW::Mechanize::Page.new(uri=nil, response, body, code=nil, mech=nil)
|
81
|
+
@importer.agent.expects(:get).with('http://mail.google.com/mail/?ui=html&zy=l'
|
82
|
+
).once.returns(page)
|
83
|
+
|
84
|
+
assert_nothing_raised do
|
85
|
+
assert @importer.login
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def test_scrape_contacts_raises_badcredentialerror_when_not_logged_in
|
90
|
+
@importer.agent.expects(:cookies).once.returns([])
|
91
|
+
assert_raises(Blackbook::BadCredentialsError) do
|
92
|
+
@importer.scrape_contacts
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def test_scrape_contacts
|
97
|
+
cookie = WWW::Mechanize::Cookie.new('GAUSR', 'mail:user@gmail.com')
|
98
|
+
@importer.agent.expects(:cookies).once.returns([cookie])
|
99
|
+
|
100
|
+
response = {'content-type' => 'text/html'}
|
101
|
+
body = load_fixture('gmail_contacts.html').join
|
102
|
+
page = WWW::Mechanize::Page.new(uri=nil, response, body, code=nil, mech=nil)
|
103
|
+
@importer.agent.expects(:get).with(
|
104
|
+
'http://mail.google.com/mail/h/?v=cl&pnl=a'
|
105
|
+
).once.returns(page)
|
106
|
+
|
107
|
+
assert_nothing_raised do
|
108
|
+
contacts = @importer.scrape_contacts
|
109
|
+
assert_equal 2, contacts.size
|
110
|
+
assert contacts.detect{|c| c[:email] == 'joe.user@example.com'}
|
111
|
+
assert contacts.detect{|c| c[:name] == 'Joe User'}
|
112
|
+
assert contacts.detect{|c| c[:email] == 'jane.user@example.com'}
|
113
|
+
assert contacts.detect{|c| c[:name] == 'Jane User'}
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
end
|
@@ -0,0 +1,147 @@
|
|
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
|
+
|
81
|
+
# Joe doesn't have a name, so we use his display name (which hotmail sets to his email)
|
82
|
+
assert contacts.detect{|c| c[:email] == 'joe.user@example.com'}
|
83
|
+
assert contacts.detect{|c| c[:name] == 'joe.user@example.com'}
|
84
|
+
|
85
|
+
# Jane has both a name and an email
|
86
|
+
assert contacts.detect{|c| c[:email] == 'jane.user@example.com'}
|
87
|
+
assert contacts.detect{|c| c[:name] == 'Jane User'}
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def test_prepare
|
92
|
+
@importer.expects(:login).once
|
93
|
+
@importer.prepare
|
94
|
+
end
|
95
|
+
|
96
|
+
def test_importer_match
|
97
|
+
assert_equal false, @importer =~ nil
|
98
|
+
assert_equal false, @importer =~ {}
|
99
|
+
assert_equal false, @importer =~ {'username' => 'joe@example.com'}
|
100
|
+
assert_equal false, @importer =~ {:username => 'joe@example.com'}
|
101
|
+
assert_equal false, @importer =~ {:username => 'joe'}
|
102
|
+
|
103
|
+
domains = ["compaq.net",
|
104
|
+
"hotmail.co.jp",
|
105
|
+
"hotmail.co.uk",
|
106
|
+
"hotmail.com",
|
107
|
+
"hotmail.de",
|
108
|
+
"hotmail.fr",
|
109
|
+
"hotmail.it",
|
110
|
+
"messengeruser.com",
|
111
|
+
"msn.com",
|
112
|
+
"passport.com",
|
113
|
+
"webtv.net"]
|
114
|
+
domains.each do |domain|
|
115
|
+
assert_equal true, @importer =~ {:username => "joe@#{domain}"}
|
116
|
+
assert_equal true, @importer =~ {:username => "joe@#{domain}".upcase}
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
|
121
|
+
def test_login_url
|
122
|
+
assert_equal "https://login.live.com/ppsecure/post.srf", @importer.login_url
|
123
|
+
@importer.options = {:username => 'user@msn.com', :password => 'password'}
|
124
|
+
assert_equal "https://msnia.login.live.com/ppsecure/post.srf", @importer.login_url
|
125
|
+
end
|
126
|
+
|
127
|
+
def test_current_host
|
128
|
+
response = {'content-type' => 'text/html'}
|
129
|
+
page = WWW::Mechanize::Page.new(URI.parse('http://localhost/'), response, '<html></html>', code=nil, mech=nil)
|
130
|
+
|
131
|
+
importer = Blackbook::Importer::Hotmail.new
|
132
|
+
assert_nil importer.current_host
|
133
|
+
importer.create_agent
|
134
|
+
assert_nil importer.current_host
|
135
|
+
importer.agent.expects(:current_page).times(2).returns(page)
|
136
|
+
assert_equal 'http://localhost', importer.current_host
|
137
|
+
end
|
138
|
+
|
139
|
+
def test_username_domain(username = nil)
|
140
|
+
importer = Blackbook::Importer::Hotmail.new
|
141
|
+
assert_nil importer.username_domain
|
142
|
+
assert_equal 'example.com', importer.username_domain('user@example.com')
|
143
|
+
assert_equal 'example.com', @importer.username_domain('user@example.com')
|
144
|
+
assert_equal 'hotmail.com', @importer.username_domain
|
145
|
+
end
|
146
|
+
|
147
|
+
end
|
@@ -0,0 +1,51 @@
|
|
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 TestBlackbookImporterPageScraper < Test::Unit::TestCase
|
7
|
+
|
8
|
+
include TestHelper
|
9
|
+
|
10
|
+
def setup
|
11
|
+
@scraper = Blackbook::Importer::PageScraper.new
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_mechanize_patch_to_absolute_uri
|
15
|
+
@scraper.create_agent
|
16
|
+
response = {'content-type' => 'text/html'}
|
17
|
+
page = WWW::Mechanize::Page.new(uri=nil, response, '<html></html>', code=nil, mech=nil)
|
18
|
+
url = "http://localhost/?arg=val&jank=AAA%3D"
|
19
|
+
assert_equal URI.parse(url), @scraper.agent.send(:to_absolute_uri, url)
|
20
|
+
# pattern of odd URL created by javascript validator in AOL webmail login
|
21
|
+
url = "http://localhost/?arg=val&jank=AAA%3D%3D"
|
22
|
+
assert_equal URI.parse(url), @scraper.agent.send(:to_absolute_uri, url)
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_create_agent
|
26
|
+
agent = nil
|
27
|
+
assert_nothing_raised do
|
28
|
+
agent = @scraper.create_agent
|
29
|
+
end
|
30
|
+
assert_equal agent, @scraper.agent
|
31
|
+
assert_equal true, agent.is_a?(WWW::Mechanize)
|
32
|
+
assert_equal "Mozilla/4.0 (compatible; Blackbook #{Blackbook::VERSION})", agent.user_agent
|
33
|
+
assert_equal false, agent.keep_alive
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_strip_html
|
37
|
+
assert_equal '', @scraper.strip_html('<foo></foo>')
|
38
|
+
assert_equal 'bar', @scraper.strip_html('<foo>bar</foo>')
|
39
|
+
assert_equal 'bar', @scraper.strip_html('bar</foo>')
|
40
|
+
assert_equal 'bar', @scraper.strip_html('<foo>bar')
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_fetch_contacts!
|
44
|
+
@scraper.expects(:create_agent).once
|
45
|
+
@scraper.expects(:prepare).once
|
46
|
+
@scraper.expects(:scrape_contacts).once
|
47
|
+
assert_nothing_raised do
|
48
|
+
agent = @scraper.fetch_contacts!
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,97 @@
|
|
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 TestBlackbookImporterYahoo < Test::Unit::TestCase
|
7
|
+
|
8
|
+
include TestHelper
|
9
|
+
|
10
|
+
def setup
|
11
|
+
@importer = Blackbook::Importer::Yahoo.new
|
12
|
+
@importer.options = {:username => 'user@yahoo.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@yahoo.com'}
|
28
|
+
assert_equal true, @importer =~ {:username => 'JOE@YAHOO.COM'}
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_login
|
32
|
+
response = {'content-type' => 'text/html'}
|
33
|
+
|
34
|
+
body = load_fixture('yahoo_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('https://login.yahoo.com/config/login_verify2?').once.returns(page)
|
37
|
+
|
38
|
+
body = load_fixture('yahoo_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_nothing_raised do
|
43
|
+
assert @importer.login
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_bad_login
|
48
|
+
response = {'content-type' => 'text/html'}
|
49
|
+
|
50
|
+
body = load_fixture('yahoo_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('https://login.yahoo.com/config/login_verify2?').once.returns(page)
|
53
|
+
|
54
|
+
body = load_fixture('yahoo_bad_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
|
+
assert_raises(Blackbook::BadCredentialsError) do
|
59
|
+
@importer.login
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_scrape_contacts_raises_badcredentialerror_when_not_logged_in
|
64
|
+
response = {'content-type' => 'text/html'}
|
65
|
+
body = load_fixture('yahoo_contacts_not_logged_in.html').join
|
66
|
+
page = WWW::Mechanize::Page.new(uri=nil, response, body, code=nil, mech=nil)
|
67
|
+
@importer.agent.expects(:get).with('http://address.yahoo.com/?1=&VPC=import_export'
|
68
|
+
).once.returns(page)
|
69
|
+
|
70
|
+
assert_raises(Blackbook::BadCredentialsError) do
|
71
|
+
@importer.scrape_contacts
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_scrape_contacts
|
76
|
+
response = {'content-type' => 'text/html'}
|
77
|
+
body = load_fixture('yahoo_contacts_stage_1.html').join
|
78
|
+
page = WWW::Mechanize::Page.new(uri=nil, response, body, code=nil, mech=nil)
|
79
|
+
@importer.agent.expects(:get).with('http://address.yahoo.com/?1=&VPC=import_export'
|
80
|
+
).once.returns(page)
|
81
|
+
|
82
|
+
response = {'content-type' => 'text/csv; charset=UTF-8'}
|
83
|
+
body = load_fixture('yahoo_contacts.csv').join
|
84
|
+
page = WWW::Mechanize::File.new(uri=nil, response, body, code=nil)
|
85
|
+
@importer.agent.expects(:submit).once.returns(page)
|
86
|
+
|
87
|
+
assert_nothing_raised do
|
88
|
+
contacts = @importer.scrape_contacts
|
89
|
+
assert_equal 2, contacts.size
|
90
|
+
assert contacts.detect{|c| c[:email] == 'joe.user@example.com'}
|
91
|
+
assert contacts.detect{|c| c[:name] == 'Joe User'}
|
92
|
+
assert contacts.detect{|c| c[:email] == 'jane.user@example.com'}
|
93
|
+
assert contacts.detect{|c| c[:name] == 'Jane User'}
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
module TestHelper
|
2
|
+
|
3
|
+
def fixture_path(name)
|
4
|
+
"#{File.dirname(__FILE__)}/fixtures/#{name}"
|
5
|
+
end
|
6
|
+
|
7
|
+
def load_fixture(file)
|
8
|
+
IO.readlines(fixture_path(file))
|
9
|
+
end
|
10
|
+
|
11
|
+
def fixture_file(name)
|
12
|
+
File.new fixture_path(name)
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
module Net #:nodoc:
|
18
|
+
class HTTP #:nodoc:
|
19
|
+
|
20
|
+
extend TestHelper
|
21
|
+
|
22
|
+
RESPONSES = {}
|
23
|
+
|
24
|
+
def self.responses=(r)
|
25
|
+
RESPONSES.clear
|
26
|
+
r.each{|k,v| RESPONSES[k] = v}
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
alias :old_net_http_request :request
|
31
|
+
|
32
|
+
def request(req, body = nil, &block)
|
33
|
+
prot = use_ssl ? "https" : "http"
|
34
|
+
uri_cls = use_ssl ? URI::HTTPS : URI::HTTP
|
35
|
+
query = req.path.split('?',2)
|
36
|
+
opts = {:host => self.address,
|
37
|
+
:port => self.port, :path => query[0]}
|
38
|
+
opts[:query] = query[1] if query[1]
|
39
|
+
uri = uri_cls.build(opts)
|
40
|
+
raise ArgumentError.new("#{req.method} method to #{uri} not being handled in testing")
|
41
|
+
end
|
42
|
+
|
43
|
+
def connect
|
44
|
+
raise ArgumentError.new("connect not being handled in testing")
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
plugin_root = "#{ File.dirname(__FILE__) }/.."
|
50
|
+
|
51
|
+
require 'rubygems'
|
52
|
+
require 'mocha'
|
53
|
+
|
54
|
+
module BlackbookExtensions
|
55
|
+
module TestHelper
|
56
|
+
class Credentials
|
57
|
+
attr_accessor :network, :config
|
58
|
+
|
59
|
+
def initialize(network)
|
60
|
+
self.network = network.to_s
|
61
|
+
self.config = YAML.load_file( File.join(File.dirname(__FILE__), "config/credentials.yml") )
|
62
|
+
end
|
63
|
+
|
64
|
+
def get
|
65
|
+
[ self.config[self.network]["username"], self.config[self.network]["password"] ]
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|