graybook 1.0.22

Sign up to get free protection for your applications and to get access to all the features.
Files changed (78) hide show
  1. data/.gitignore +1 -0
  2. data/CHANGES.md +49 -0
  3. data/Manifest.txt +59 -0
  4. data/README.md +66 -0
  5. data/Rakefile +40 -0
  6. data/VERSION +1 -0
  7. data/VERSION.yml +4 -0
  8. data/VERSION_NAME +1 -0
  9. data/debug_graybook.rb +10 -0
  10. data/init.rb +1 -0
  11. data/lib/graybook/exporter/base.rb +16 -0
  12. data/lib/graybook/exporter/vcf.rb +45 -0
  13. data/lib/graybook/exporter/xml.rb +28 -0
  14. data/lib/graybook/importer/aol.rb +87 -0
  15. data/lib/graybook/importer/base.rb +39 -0
  16. data/lib/graybook/importer/csv.rb +74 -0
  17. data/lib/graybook/importer/freenet.rb +62 -0
  18. data/lib/graybook/importer/gmail.rb +84 -0
  19. data/lib/graybook/importer/gmx.rb +68 -0
  20. data/lib/graybook/importer/hotmail.rb +135 -0
  21. data/lib/graybook/importer/page_scraper.rb +86 -0
  22. data/lib/graybook/importer/web.de.rb +67 -0
  23. data/lib/graybook/importer/yahoo.rb +63 -0
  24. data/lib/graybook.rb +83 -0
  25. data/test/config/credentials.yml.example +9 -0
  26. data/test/fixtures/aol_application_page.html +566 -0
  27. data/test/fixtures/aol_bad_login_response_stage_3.html +565 -0
  28. data/test/fixtures/aol_contacts.html +102 -0
  29. data/test/fixtures/aol_login_response_stage_1.html +158 -0
  30. data/test/fixtures/aol_login_response_stage_2.html +559 -0
  31. data/test/fixtures/aol_login_response_stage_3.html +48 -0
  32. data/test/fixtures/aol_login_response_stage_4.html +404 -0
  33. data/test/fixtures/aol_login_response_stage_5.html +404 -0
  34. data/test/fixtures/aol_new_contacts.html +431 -0
  35. data/test/fixtures/gmail.csv +3 -0
  36. data/test/fixtures/gmail_bad_login_response_stage_2.html +560 -0
  37. data/test/fixtures/gmail_contacts.html +228 -0
  38. data/test/fixtures/gmail_login_response_stage_1.html +556 -0
  39. data/test/fixtures/gmail_login_response_stage_2.html +1 -0
  40. data/test/fixtures/gmail_login_response_stage_2a.html +1 -0
  41. data/test/fixtures/gmail_login_response_stage_3.html +249 -0
  42. data/test/fixtures/gmail_redirect_body.html +10 -0
  43. data/test/fixtures/hotmail_bad_login_response_stage_2.html +31 -0
  44. data/test/fixtures/hotmail_contacts.html +262 -0
  45. data/test/fixtures/hotmail_login_response_stage_1.html +31 -0
  46. data/test/fixtures/hotmail_login_response_stage_2.html +1 -0
  47. data/test/fixtures/hotmail_login_response_stage_3.html +519 -0
  48. data/test/fixtures/hotmail_scrape_first_page.html +77 -0
  49. data/test/fixtures/hotmail_scrape_response_stage_1.html +90 -0
  50. data/test/fixtures/hotmail_scrape_response_stage_2.html +77 -0
  51. data/test/fixtures/hotmail_scrape_response_stage_3.html +0 -0
  52. data/test/fixtures/yahoo_bad_login_response_stage_2.html +443 -0
  53. data/test/fixtures/yahoo_contacts.csv +3 -0
  54. data/test/fixtures/yahoo_contacts_not_logged_in.html +432 -0
  55. data/test/fixtures/yahoo_contacts_stage_1.html +399 -0
  56. data/test/fixtures/yahoo_login_response_stage_1.html +433 -0
  57. data/test/fixtures/yahoo_login_response_stage_2.html +16 -0
  58. data/test/fixtures/yahoo_no_user_response_stage_2.html +574 -0
  59. data/test/freenet_importer_test.rb +53 -0
  60. data/test/gmx_importer_test.rb +53 -0
  61. data/test/scripts/live_test.rb +25 -0
  62. data/test/test_graybook.rb +60 -0
  63. data/test/test_graybook_exporter_base.rb +16 -0
  64. data/test/test_graybook_exporter_vcf.rb +52 -0
  65. data/test/test_graybook_exporter_xml.rb +16 -0
  66. data/test/test_graybook_importer_aol.rb +108 -0
  67. data/test/test_graybook_importer_base.rb +24 -0
  68. data/test/test_graybook_importer_csv.rb +60 -0
  69. data/test/test_graybook_importer_gmail.rb +116 -0
  70. data/test/test_graybook_importer_hotmail.rb +165 -0
  71. data/test/test_graybook_importer_page_scraper.rb +51 -0
  72. data/test/test_graybook_importer_yahoo.rb +137 -0
  73. data/test/test_helper.rb +71 -0
  74. data/test/web.de_importer_test.rb +53 -0
  75. data/updater.rb +15 -0
  76. data/vendor/plugins/graybook/lib/autotest/discover.rb +3 -0
  77. data/vendor/plugins/graybook/lib/autotest/graybook.rb +27 -0
  78. metadata +185 -0
@@ -0,0 +1,53 @@
1
+ require File.dirname(__FILE__) + '/../test_helper.rb'
2
+
3
+ context "The GMX importer" do
4
+
5
+ setup do
6
+ @username, @password = GraybookExtensions::TestHelper::Credentials.new(:gmx).get
7
+
8
+ @importer = Graybook::Importer::GMX.new
9
+ @importer.create_agent
10
+ end
11
+
12
+ specify "should match emails" do
13
+ @importer.=~(nil).should.not.be
14
+ @importer.=~({}).should.not.be
15
+ @importer.=~({ 'username' => "john@foo.com" }).should.not.be
16
+ @importer.=~({ :username => "john@foo.com" }).should.not.be
17
+ @importer.=~({ :username => "john" }).should.not.be
18
+ @importer.=~({ :username => "john@gmx.de" }).should.be
19
+ @importer.=~({ :username => "JOHN@GMX.DE" }).should.be
20
+ end
21
+
22
+ specify "should be able to login with correct credentials" do
23
+ should.not.raise Exception do
24
+ login(@username, @password)
25
+ end
26
+ end
27
+
28
+ specify "should raise BadCredentialsError on login with wrong credentials" do
29
+ should.raise Graybook::BadCredentialsError do
30
+ login
31
+ end
32
+ end
33
+
34
+ specify "should raise BadCredentialsError on login with correct username and wrong password" do
35
+ should.raise Graybook::BadCredentialsError do
36
+ login(@username)
37
+ end
38
+ end
39
+
40
+ specify "should able to get contacts" do
41
+ login(@username, @password)
42
+
43
+ contacts = @importer.scrape_contacts
44
+ contacts.should.not.be.empty
45
+ contacts.should.be.all { |contact| contact.is_a? Hash }
46
+ end
47
+
48
+ private
49
+ def login(username = "foo", password = "bar")
50
+ @importer.options = { :username => username, :password => password }
51
+ @importer.login
52
+ end
53
+ end
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env ruby
2
+ require File.join( File.dirname(__FILE__), '../../lib/graybook.rb' )
3
+ require 'optparse'
4
+
5
+ options = {}
6
+ importer = :auto
7
+
8
+ opts = OptionParser.new do |opts|
9
+ opts.on("-u USERNAME") do |username|
10
+ options[:username] = username
11
+ end
12
+ opts.on("-p PASSWORD") do |password|
13
+ options[:password] = password
14
+ end
15
+ opts.on("-i [IMPORTER]") do |importer_name|
16
+ importer = importer_name.to_sym
17
+ end
18
+ opts.on("-x [EXPORTER]") do |exporter_name|
19
+ options[:as] = exporter_name.to_sym
20
+ end
21
+ end
22
+
23
+ opts.parse!(ARGV)
24
+
25
+ puts contacts = Graybook.get( importer, options )
@@ -0,0 +1,60 @@
1
+ require File.join( File.dirname(__FILE__), '../lib/graybook.rb' )
2
+ require File.join( File.dirname(__FILE__), 'test_helper.rb' )
3
+ require 'test/unit'
4
+ require 'mocha'
5
+
6
+ class TestGraybook < Test::Unit::TestCase
7
+
8
+ include TestHelper
9
+
10
+ def test_class_get
11
+ Graybook.any_instance.expects(:get)
12
+ Graybook.get
13
+ end
14
+
15
+ def test_class_instance
16
+ assert Graybook.instance.is_a?(Graybook)
17
+ end
18
+
19
+ def test_class_register
20
+ Graybook.register(:base, Graybook::Exporter::Base)
21
+ assert Graybook.instance.exporters[:base].is_a?(Graybook::Exporter::Base)
22
+
23
+ Graybook.register(:base, Graybook::Importer::Base)
24
+ assert Graybook.instance.importers[:base].is_a?(Graybook::Importer::Base)
25
+ end
26
+
27
+ def test_class_register_raises_on_bad_register
28
+ assert_raises(ArgumentError) do
29
+ Graybook.register(:bad, String)
30
+ end
31
+ end
32
+
33
+ def test_export
34
+ importer = Graybook::Importer::Base.new
35
+ exporter = Graybook::Exporter::Base.new
36
+ importer.expects(:import)
37
+ exporter.expects(:export)
38
+ Graybook.instance.export( importer, exporter, {} )
39
+ end
40
+
41
+ def test_find_importer
42
+ base = Graybook::Importer::Base.new
43
+ base.stubs(:=~).returns(true)
44
+ Graybook.any_instance.stubs(:importers).returns({:basic => base})
45
+ assert_equal base, Graybook.instance.find_importer(:as => :basic)
46
+
47
+ base.stubs(:=~).returns(false)
48
+ assert_nil Graybook.instance.find_importer(:as => :basic)
49
+ end
50
+
51
+ def test_get
52
+ cards = Graybook.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/graybook.rb' )
2
+ require File.join( File.dirname(__FILE__), 'test_helper.rb' )
3
+ require 'test/unit'
4
+ require 'mocha'
5
+
6
+ class TestGraybookExporterBase < Test::Unit::TestCase
7
+
8
+ include TestHelper
9
+
10
+ def test_export
11
+ exporter = Graybook::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/graybook.rb' )
2
+ require File.join( File.dirname(__FILE__), 'test_helper.rb' )
3
+ require 'test/unit'
4
+ require 'mocha'
5
+
6
+ class TestGraybookExporterVcf < Test::Unit::TestCase
7
+
8
+ include TestHelper
9
+
10
+ def setup
11
+ @card = Graybook::Exporter::Vcf::Vcard.new(
12
+ {'first' => 'joe', 'last' => 'user',
13
+ 'email' => 'joe.user@example.com'})
14
+ @exporter = Graybook::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/graybook.rb' )
2
+ require File.join( File.dirname(__FILE__), 'test_helper.rb' )
3
+ require 'test/unit'
4
+ require 'mocha'
5
+
6
+ class TestGraybookExporterXml < Test::Unit::TestCase
7
+
8
+ include TestHelper
9
+
10
+ def test_export
11
+ exporter = Graybook::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,108 @@
1
+ require File.join( File.dirname(__FILE__), '../lib/graybook.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 TestGraybookImporterAol < Test::Unit::TestCase
9
+
10
+ include TestHelper
11
+
12
+ def setup
13
+ @importer = Graybook::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('/31361/aim/en-us/Lite/Today.aspx').once.returns(page)
49
+
50
+ assert_nothing_raised do
51
+ assert @importer.login
52
+ end
53
+ end
54
+
55
+ def test_login_bad_credentials
56
+ response = {'content-type' => 'text/html'}
57
+
58
+ body = load_fixture('aol_login_response_stage_2.html').join
59
+ page = WWW::Mechanize::Page.new(uri=nil, response, body, code=nil, mech=nil)
60
+ @importer.agent.expects(:get).with('http://webmail.aol.com/').once.returns(page)
61
+
62
+ body = load_fixture('aol_bad_login_response_stage_3.html').join
63
+ page = WWW::Mechanize::Page.new(uri=nil, response, body, code=nil, mech=nil)
64
+ @importer.agent.expects(:submit).once.returns(page)
65
+
66
+ assert_raises(Graybook::BadCredentialsError) do
67
+ @importer.login
68
+ end
69
+ end
70
+
71
+ def test_scrape_contacts_raises_badcredentialerror_when_not_logged_in
72
+ @importer.agent.expects(:cookies).once.returns([])
73
+ assert_raises(Graybook::BadCredentialsError) do
74
+ @importer.scrape_contacts
75
+ end
76
+ end
77
+
78
+ def test_scrape_contacts
79
+ cookie = WWW::Mechanize::Cookie.new('Auth',
80
+ '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:graybookuid&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&')
81
+ cookie.domain = 'localhost'
82
+ @importer.agent.expects(:cookies).once.returns([cookie])
83
+
84
+ response = {'content-type' => 'text/html'}
85
+ body = load_fixture('aol_application_page.html').join
86
+ uri = URI.parse('http://webmail.aol.com/34865/aim/en-us/Lite/Today.aspx')
87
+ page = WWW::Mechanize::Page.new(uri, response, body, code=nil, mech=nil)
88
+ @importer.agent.expects(:current_page).times(2).returns(page)
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/34865/aim/en-us/Lite/addresslist-print.aspx?command=all&sort=FirstLastNick&sortDir=Ascending&nameFormat=FirstLastNick&user=ULKLrUelQ2'
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
+ assert contacts.detect{|c| c[:name] == 'NoEmail Dude'} == nil
105
+ end
106
+ end
107
+
108
+ end
@@ -0,0 +1,24 @@
1
+ require File.join( File.dirname(__FILE__), '../lib/graybook.rb' )
2
+ require File.join( File.dirname(__FILE__), 'test_helper.rb' )
3
+ require 'test/unit'
4
+ require 'mocha'
5
+
6
+ class TestGraybookImporterBase < Test::Unit::TestCase
7
+
8
+ include TestHelper
9
+
10
+ def test_service_name
11
+ assert_equal "Base", Graybook::Importer::Base.new.service_name
12
+ end
13
+
14
+ def test_import
15
+ importer = Graybook::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/graybook.rb' )
2
+ require File.join( File.dirname(__FILE__), 'test_helper.rb' )
3
+ require 'tempfile'
4
+ require 'test/unit'
5
+ require 'mocha'
6
+
7
+ class TestGraybookImporterCsv < Test::Unit::TestCase
8
+
9
+ include TestHelper
10
+
11
+ def setup
12
+ @importer = Graybook::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,116 @@
1
+ require File.join( File.dirname(__FILE__), '../lib/graybook.rb' )
2
+ require File.join( File.dirname(__FILE__), 'test_helper.rb' )
3
+ require 'test/unit'
4
+ require 'mocha'
5
+
6
+ class TestGraybookImporterGmail < Test::Unit::TestCase
7
+
8
+ include TestHelper
9
+
10
+ def setup
11
+ @importer = Graybook::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(Graybook::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').once.returns(page)
61
+
62
+ assert_nothing_raised do
63
+ # assert @importer.login
64
+ @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').once.returns(page)
82
+
83
+ assert_nothing_raised do
84
+ # assert @importer.login
85
+ @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(Graybook::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').once.returns(page)
105
+
106
+ assert_nothing_raised do
107
+ contacts = @importer.scrape_contacts
108
+ assert_equal 2, contacts.size
109
+ assert contacts.detect{|c| c[:email] == 'joe.user@example.com'}
110
+ assert contacts.detect{|c| c[:name] == 'Joe User'}
111
+ assert contacts.detect{|c| c[:email] == 'jane.user@example.com'}
112
+ assert contacts.detect{|c| c[:name] == 'Jane User'}
113
+ end
114
+ end
115
+
116
+ end