thimbl 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,22 @@
1
+ {
2
+ "following": [
3
+ {
4
+ "address": "mike@mikepearce.net",
5
+ "nick": "mike"
6
+ },
7
+ {
8
+ "address": "rw@telekommunisten.org",
9
+ "nick": "rico"
10
+ }
11
+ ],
12
+ "messages": [
13
+ {
14
+ "text": "messages_1 a",
15
+ "time": "20101105124412"
16
+ },
17
+ {
18
+ "text": "messages_1 b",
19
+ "time": "20100707125120"
20
+ }
21
+ ]
22
+ }
@@ -0,0 +1,12 @@
1
+ {
2
+ "messages": [
3
+ {
4
+ "text": "messages_2 a",
5
+ "time": "20101105124416"
6
+ },
7
+ {
8
+ "text": "messages_2 b",
9
+ "time": "20100707125126"
10
+ }
11
+ ]
12
+ }
@@ -0,0 +1,12 @@
1
+ {
2
+ "messages": [
3
+ {
4
+ "text": "messages_3 a",
5
+ "time": "20101105124406"
6
+ },
7
+ {
8
+ "text": "messages_3 b",
9
+ "time": "20100707125136"
10
+ }
11
+ ]
12
+ }
@@ -0,0 +1,69 @@
1
+ {
2
+ "bio": "Venture Communist",
3
+ "following": [
4
+ {
5
+ "address": "mike@mikepearce.net",
6
+ "nick": "mike"
7
+ },
8
+ {
9
+ "address": "rw@telekommunisten.org",
10
+ "nick": "rico"
11
+ },
12
+ {
13
+ "address": "ashull@telekommunisten.org",
14
+ "nick": "ashull"
15
+ }
16
+ ],
17
+ "messages": [
18
+ {
19
+ "text": "Hey @mark, this is the first update sent with Thimbl-CLI",
20
+ "time": "20101105124412"
21
+ },
22
+ {
23
+ "text": "mike needs to stop sending messages from the future!",
24
+ "time": "20100707125120"
25
+ },
26
+ {
27
+ "text": "#thimbl is rolling!",
28
+ "time": "20100707122140"
29
+ },
30
+ {
31
+ "text": "I love thimbl! #thimbl",
32
+ "time": "20100622170914"
33
+ }
34
+ ],
35
+ "name": "Dmytri Kleiner",
36
+ "properties": {
37
+ "email": "dk@telekommunisten.net",
38
+ "mobile": "+491632866163",
39
+ "website": "http://www.telekommunisten.net"
40
+ },
41
+ "replies": {
42
+ "ashull@telekommunisten.org": [
43
+ {
44
+ "text": "@ashull, the feed is awesome, works great!",
45
+ "time": "20100707122315"
46
+ },
47
+ {
48
+ "text": "@ashull, here is a reply",
49
+ "time": "20100705163145"
50
+ }
51
+ ],
52
+ "mike@mikepearce.net": [
53
+ {
54
+ "text": "@mike, the baboon would win. No contest",
55
+ "time": "20100707122100"
56
+ },
57
+ {
58
+ "text": "@mike, do you like this json format?",
59
+ "time": "20100622170025"
60
+ }
61
+ ],
62
+ "rw@telekommunisten.org": [
63
+ {
64
+ "text": "@rico, I'll call you later",
65
+ "time": "20100622162340"
66
+ }
67
+ ]
68
+ }
69
+ }
@@ -0,0 +1,6 @@
1
+ 2010-07-07 12:51:20 user@thimbl.net > messages_1 b
2
+ 2010-07-07 12:51:26 mike@mikepearce.net > messages_2 b
3
+ 2010-07-07 12:51:36 rw@telekommunisten.org > messages_3 b
4
+ 2010-11-05 12:44:06 rw@telekommunisten.org > messages_3 a
5
+ 2010-11-05 12:44:12 user@thimbl.net > messages_1 a
6
+ 2010-11-05 12:44:16 mike@mikepearce.net > messages_2 a
@@ -2,4 +2,6 @@ require "#{File.dirname(__FILE__)}/../lib/thimbl"
2
2
  require 'test/unit'
3
3
  require 'mocha'
4
4
  require 'ruby-debug'
5
- require 'delorean'
5
+ require 'delorean'
6
+
7
+ FIXTURES_PATH = "#{File.dirname(__FILE__)}/fixtures"
@@ -25,161 +25,178 @@ class ThimblBaseTest < Test::Unit::TestCase
25
25
  end
26
26
 
27
27
  def test_new_without_options
28
- thimbl = Thimbl::Base.new
29
-
30
- assert_equal( 'address', thimbl.me )
31
-
32
- data = thimbl.data['plans'][thimbl.me]
33
- assert_equal( 'name', data['name'] )
34
- assert_equal( [], data['messages'] )
35
- assert_equal( 'bio', data['bio'] )
36
- assert_equal( {}, data['replies'] )
37
- assert_equal( [], data['following'] )
38
- assert_equal( 'mobile', data['properties']['mobile'] )
39
- assert_equal( 'website', data['properties']['website'] )
40
- assert_equal( 'email', data['properties']['email'] )
28
+ thimbl = Thimbl::Base.new 'user@thimbl.net'
29
+
30
+ assert_equal 'user@thimbl.net', thimbl.address
31
+ assert_equal nil , thimbl.properties.name
32
+ assert_equal [] , thimbl.messages
33
+ assert_equal nil , thimbl.properties.bio
34
+ assert_equal [] , thimbl.following
35
+ assert_equal nil , thimbl.properties.mobile
36
+ assert_equal nil , thimbl.properties.website
37
+ assert_equal nil , thimbl.properties.email
41
38
  end
42
39
 
43
40
  def test_new_with_options
44
41
  thimbl =
45
42
  Thimbl::Base.new(
46
- 'bio' => 'my bio',
47
- 'website' => 'my website',
48
- 'mobile' => 'my mobile',
49
- 'email' => 'my email',
50
- 'address' => 'my address',
51
- 'name' => 'my name'
43
+ 'user@thimbl.net',
44
+ {
45
+ :bio => 'my bio',
46
+ :website => 'my website',
47
+ :mobile => 'my mobile',
48
+ :email => 'my email',
49
+ :name => 'my name'
50
+ }
52
51
  )
53
52
 
54
- assert_equal( 'my address', thimbl.me )
55
-
56
- data = thimbl.data['plans'][thimbl.me]
57
- assert_equal( 'my name', data['name'] )
58
- assert_equal( 'my bio', data['bio'] )
59
- assert_equal( 'my mobile', data['properties']['mobile'] )
60
- assert_equal( 'my website', data['properties']['website'] )
61
- assert_equal( 'my email', data['properties']['email'] )
53
+ assert_equal 'user@thimbl.net' , thimbl.address
54
+ assert_equal 'my name' , thimbl.properties.name
55
+ assert_equal 'my bio' , thimbl.properties.bio
56
+ assert_equal 'my mobile' , thimbl.properties.mobile
57
+ assert_equal 'my website' , thimbl.properties.website
58
+ assert_equal 'my email' , thimbl.properties.email
62
59
  end
63
60
 
64
61
  def test_post
65
- thimbl = Thimbl::Base.new
62
+ thimbl = Thimbl::Base.new 'user@thimbl.net'
66
63
 
67
64
  Delorean.time_travel_to("2011-02-03 04:05:06") do
68
65
  thimbl.post( "wadus wadus" )
69
66
  end
70
67
 
71
- message = thimbl.data['plans'][thimbl.me]['messages'].last
72
- assert_equal( '20110203040506', message['time'] )
73
- assert_equal( 'wadus wadus', message['text'] )
68
+ message = thimbl.messages.last
69
+ assert_equal '20110203040506', message.time.strftime('%Y%m%d%H%M%S')
70
+ assert_equal 'wadus wadus', message.text
71
+ end
72
+
73
+ def test_post!
74
+ thimbl = Thimbl::Base.new 'user@thimbl.net'
75
+ thimbl.expects( :post ).with( 'wadus wadus' )
76
+ thimbl.expects( :push ).with( 'password' )
77
+
78
+ thimbl.post! 'wadus wadus', 'password'
74
79
  end
75
80
 
76
81
  def test_follow
77
- thimbl = Thimbl::Base.new
82
+ thimbl = Thimbl::Base.new 'user@thimbl.net'
78
83
 
79
84
  thimbl.follow( 'nick', 'address' )
80
85
 
81
- following = thimbl.data['plans'][thimbl.me]['following'].last
82
- assert_equal( 'nick', following['nick'] )
83
- assert_equal( 'address', following['address'] )
86
+ followed = thimbl.following.last
87
+ assert_equal( 'nick', followed.nick )
88
+ assert_equal( 'address', followed.address )
89
+ end
90
+
91
+ def test_follow!
92
+ thimbl = Thimbl::Base.new 'user@thimbl.net'
93
+ thimbl.expects( :follow ).with( 'nick', 'new@thimbl.net' )
94
+ thimbl.expects( :push ).with( 'password' )
95
+
96
+ thimbl.follow! 'nick', 'new@thimbl.net', 'password'
97
+ end
98
+
99
+ def test_unfollow
100
+ thimbl = Thimbl::Base.new 'user@thimbl.net'
101
+ thimbl.data = JSON.load File.read "#{FIXTURES_PATH}/following.json"
102
+
103
+ assert_equal 2, thimbl.following.size
104
+
105
+ thimbl.unfollow 'rw@telekommunisten.org'
106
+
107
+ assert_equal 1, thimbl.following.size
108
+ end
109
+
110
+ def test_unfollow!
111
+ thimbl = Thimbl::Base.new 'user@thimbl.net'
112
+ thimbl.expects( :unfollow ).with( 'new@thimbl.net' )
113
+ thimbl.expects( :push ).with( 'password' )
114
+
115
+ thimbl.unfollow! 'new@thimbl.net', 'password'
84
116
  end
85
117
 
86
118
  def test_fetch
87
- finger_fixture = File.read "#{File.dirname(__FILE__)}/fixtures/finger_dk_telekommunisten_org.txt"
88
- finger_sequence = sequence('finger_sequence')
89
-
90
- Thimbl::Finger.
91
- expects(:run).
92
- with( 'wadus1@telekommunisten.org' ).
93
- returns( finger_fixture ).
94
- in_sequence( finger_sequence )
95
-
96
- Thimbl::Finger.
97
- expects(:run).
98
- with( 'wadus2@telekommunisten.org' ).
99
- returns( finger_fixture ).
100
- in_sequence( finger_sequence )
101
-
102
- Thimbl::Finger.
103
- expects(:run).
104
- with( 'me@thimbl.net' ).
105
- returns( finger_fixture ).
106
- in_sequence( finger_sequence )
119
+ finger_fixture = File.read "#{FIXTURES_PATH}/finger_dk_telekommunisten_org.txt"
120
+ Thimbl::Finger.expects(:run).with( 'me@thimbl.net' ).returns( finger_fixture )
107
121
 
108
- thimbl = Thimbl::Base.new( 'address' => 'me@thimbl.net' )
109
- thimbl.expects(:following).returns( [
110
- {'nick' => 'wadus1', 'address' => 'wadus1@telekommunisten.org' },
111
- {'nick' => 'wadus2', 'address' => 'wadus2@telekommunisten.org' },
112
- ] )
113
-
122
+ thimbl = Thimbl::Base.new 'me@thimbl.net'
114
123
  thimbl.fetch
115
124
 
116
- assert_equal( 21, thimbl.data['plans']['wadus1@telekommunisten.org']['messages'].count )
117
- assert_equal( 21, thimbl.data['plans']['wadus2@telekommunisten.org']['messages'].count )
125
+ assert_equal( 21, thimbl.messages.size )
118
126
  end
119
127
 
120
128
  def test_fetch_with_plan_with_two_break_lines
121
- finger_fixture = File.read "#{File.dirname(__FILE__)}/fixtures/finger_dk_telekommunisten_org_two_break_lines.txt"
129
+ finger_fixture = File.read "#{FIXTURES_PATH}/finger_dk_telekommunisten_org_two_break_lines.txt"
122
130
  Thimbl::Finger.expects(:run).with( 'me@thimbl.net' ).returns( finger_fixture )
123
131
 
124
- thimbl = Thimbl::Base.new( 'address' => 'me@thimbl.net' )
132
+ thimbl = Thimbl::Base.new 'me@thimbl.net'
125
133
  thimbl.fetch
126
134
 
127
- assert_equal( 22, thimbl.data['plans']['me@thimbl.net']['messages'].count )
135
+ assert_equal( 22, thimbl.messages.size )
136
+ end
137
+
138
+ def test_fetch_with_not_plan
139
+ Thimbl::Finger.expects(:run).with( 'me@thimbl.net' ).returns( 'no plan' )
140
+
141
+ thimbl = Thimbl::Base.new 'me@thimbl.net'
142
+
143
+ assert_raise(Thimbl::NoPlanException) do
144
+ thimbl.fetch
145
+ end
128
146
  end
129
147
 
130
148
  def test_push
131
- thimbl = Thimbl::Base.new( 'address' => 'user@domain.com' )
149
+ thimbl = Thimbl::Base.new 'user@domain.com'
132
150
  Net::SCP.expects(:start).with( 'domain.com', 'user', :password => 'my password' )
133
151
  thimbl.push 'my password'
134
152
  end
135
153
 
136
154
  def test_messages
137
- thimbl = Thimbl::Base.new
138
- thimbl.data = JSON.load File.read "#{File.dirname(__FILE__)}/fixtures/cache.json"
155
+ thimbl = Thimbl::Base.new 'user@thimbl.net'
156
+ thimbl.data = JSON.load File.read "#{FIXTURES_PATH}/messages_1.json"
139
157
 
140
- assert_equal( 24, thimbl.messages.size )
158
+ assert_equal( 2, thimbl.messages.size )
141
159
 
142
160
  first = thimbl.messages.first
143
161
  last = thimbl.messages.last
144
162
 
145
- assert_equal 'dk@telekommunisten.org', first['address']
146
- assert_equal '@bernd, would be cool to inegrate thimbl in http://bau-ha.us\\!', first['text']
147
- assert_equal Time.utc( 2010, 11, 29, 6, 3, 35 ), first['time']
163
+ assert_equal 'user@thimbl.net', first.address
164
+ assert_equal 'messages_1 b', first.text
165
+ assert_equal '20100707125120', first.time.strftime('%Y%m%d%H%M%S')
148
166
 
149
- assert_equal 'fguillen@telekommunisten.org', last['address']
150
- assert_equal 'testing 3', last['text']
151
- assert_equal Time.utc( 2011, 2, 5, 15, 22, 47 ), last['time']
167
+ assert_equal 'user@thimbl.net', last.address
168
+ assert_equal 'messages_1 a', last.text
169
+ assert_equal '20101105124412', last.time.strftime('%Y%m%d%H%M%S')
152
170
  end
153
171
 
154
- def test_messages_with_nil_error
155
- thimbl = Thimbl::Base.new
156
- thimbl.data = JSON.load File.read "#{File.dirname(__FILE__)}/fixtures/cache_error_nil.json"
172
+ def test_messages_when_plan_has_not_messages
173
+ thimbl = Thimbl::Base.new 'user@thimbl.net'
174
+ thimbl.data = JSON.load File.read "#{FIXTURES_PATH}/no_messages.json"
175
+
176
+ assert_equal [], thimbl.messages
177
+ end
178
+
179
+ def test_following
180
+ thimbl = Thimbl::Base.new 'user@thimbl.net'
181
+ thimbl.data = JSON.load File.read "#{FIXTURES_PATH}/following.json"
157
182
 
158
- assert_not_nil thimbl.messages
159
- end
160
-
161
- def test_print
162
- messages = [
163
- {
164
- "address" => "fguillen@telekommunisten.org",
165
- "text" => "Here I am",
166
- "time" => "20110131002202"
167
- },
168
- {
169
- "address" => "fguillen@telekommunisten.org",
170
- "text" => "testing :)",
171
- "time" => "20110205150637"
172
- }
173
- ]
183
+ assert_equal( 2, thimbl.following.size )
174
184
 
175
- thimbl = Thimbl::Base.new
176
- thimbl.expects( :messages ).returns( messages )
185
+ first = thimbl.following.first
186
+ last = thimbl.following.last
177
187
 
178
- print = thimbl.print
179
-
180
- assert_equal( 2, print.lines.to_a.size )
181
- assert_equal( "2011-01-31 00:22:02 fguillen@telekommunisten.org > Here I am\n", print.lines.to_a[0] )
182
- assert_equal( "2011-02-05 15:06:37 fguillen@telekommunisten.org > testing :)\n", print.lines.to_a[1] )
188
+ assert_equal 'mike@mikepearce.net', first.address
189
+ assert_equal 'mike', first.nick
190
+
191
+ assert_equal 'rw@telekommunisten.org', last.address
192
+ assert_equal 'rico', last.nick
193
+ end
194
+
195
+ def test_following_when_plan_has_not_following
196
+ thimbl = Thimbl::Base.new 'user@thimbl.net'
197
+ thimbl.data = JSON.load File.read "#{FIXTURES_PATH}/no_following.json"
198
+
199
+ assert_equal [], thimbl.following
183
200
  end
184
201
 
185
202
  def test_parse_time
@@ -187,7 +204,7 @@ class ThimblBaseTest < Test::Unit::TestCase
187
204
  end
188
205
 
189
206
  def test_not_adding_a_following_if_already_there
190
- thimbl = Thimbl::Base.new
207
+ thimbl = Thimbl::Base.new 'user@thimbl.net'
191
208
  thimbl.follow 'wadus', 'wadus@thimbl.net'
192
209
  thimbl.follow 'wadus', 'wadus@thimbl.net'
193
210
 
@@ -7,6 +7,37 @@ class CommandTest < Test::Unit::TestCase
7
7
  def teardown
8
8
  end
9
9
 
10
+ def test_get_actual
11
+ temp_folder = Dir.tmpdir
12
+
13
+ File.open( "#{temp_folder}/actual", 'w' ) { |f| f.write 'user@thimbl.net' }
14
+
15
+ Thimbl::Command.stubs( :thimbl_folder ).returns( temp_folder )
16
+ Thimbl::Base.any_instance.expects( :fetch )
17
+
18
+ thimbl = Thimbl::Command.get_actual
19
+
20
+ assert_equal 'user@thimbl.net', thimbl.address
21
+ end
22
+
23
+ def test_get_actual_with_not_existing_file
24
+ Thimbl::Command.stubs( :thimbl_folder ).returns( '/not_exists' )
25
+
26
+ assert_raise( ArgumentError ) do
27
+ Thimbl::Command.get_actual
28
+ end
29
+ end
30
+
31
+ def test_save_actual
32
+ temp_folder = "#{Dir.tmpdir}/.thimbl"
33
+ FileUtils.mkdir_p temp_folder
34
+ Thimbl::Command.stubs( :thimbl_folder ).returns( temp_folder )
35
+
36
+ Thimbl::Command.save_actual 'user@thimbl.net'
37
+
38
+ assert_equal 'user@thimbl.net', File.read( "#{temp_folder}/actual" )
39
+ end
40
+
10
41
  def test_setup_should_exit_if_parameters_not_correct
11
42
  assert_raise( ArgumentError ) do
12
43
  Thimbl::Command.setup
@@ -14,30 +45,25 @@ class CommandTest < Test::Unit::TestCase
14
45
  end
15
46
 
16
47
  def test_setup
17
- temp_file = Tempfile.new('cache.json')
18
- Thimbl::Command.stubs( :cache_path ).returns( temp_file.path )
48
+ temp_folder = "#{Dir.tmpdir}/.thimbl"
49
+ Thimbl::Command.expects( :thimbl_folder ).returns( temp_folder )
50
+ Thimbl::Command.expects( :save_actual ).with( 'me@thimbl.net' )
51
+
19
52
  Thimbl::Command.setup( 'me@thimbl.net' )
20
53
 
21
- cache_written = JSON.load temp_file.read
22
- assert_equal 'me@thimbl.net', cache_written['me']
23
-
24
- temp_file.unlink
54
+ assert File.directory? temp_folder
25
55
  end
26
56
 
27
57
  def test_print
28
- thimbl = Thimbl::Base.new
29
- Thimbl::Command.expects( :load ).returns( thimbl )
30
- thimbl.expects( :print )
58
+ thimbl = Thimbl::Base.new 'user@thimbl.net'
59
+ Thimbl::Command.expects( :get_actual ).returns( thimbl )
60
+ Thimbl::Base.any_instance.expects( :fetch_plan ).times( 3 ).returns(
61
+ File.read( "#{FIXTURES_PATH}/messages_1.json" ), # the user
62
+ File.read( "#{FIXTURES_PATH}/messages_2.json" ), # following 1
63
+ File.read( "#{FIXTURES_PATH}/messages_3.json" ) # following 2
64
+ )
31
65
 
32
- Thimbl::Command.print
33
- end
34
-
35
- def test_fetch
36
- thimbl = Thimbl::Base.new
37
- Thimbl::Command.expects( :load ).returns( thimbl )
38
- thimbl.expects( :fetch )
39
-
40
- Thimbl::Command.fetch
66
+ assert_equal File.read( "#{FIXTURES_PATH}/print.txt" ), Thimbl::Command.print
41
67
  end
42
68
 
43
69
  def test_post_should_raise_error_if_wrong_parameters
@@ -46,22 +72,20 @@ class CommandTest < Test::Unit::TestCase
46
72
  end
47
73
  end
48
74
 
49
- def test_post
50
- thimbl = Thimbl::Base.new
51
- Thimbl::Command.expects( :load ).returns( thimbl )
52
- thimbl.expects( :post ).with( 'wadus' )
53
-
54
- Thimbl::Command.post 'wadus'
75
+ def test_post_should_raise_error_if_not_password
76
+ assert_raise( ArgumentError ) do
77
+ Thimbl::Command.post "message"
78
+ end
55
79
  end
56
80
 
57
- def test_push
58
- thimbl = Thimbl::Base.new
59
- Thimbl::Command.expects( :load ).returns( thimbl )
60
- thimbl.expects( :push ).with( 'pass' )
81
+ def test_post
82
+ thimbl = Thimbl::Base.new 'user@thimbl.net'
83
+ Thimbl::Command.expects( :get_actual ).returns( thimbl )
84
+ thimbl.expects( :post! ).with( 'wadus', 'password' )
61
85
 
62
- Thimbl::Command.push 'pass'
86
+ Thimbl::Command.post 'wadus', 'password'
63
87
  end
64
-
88
+
65
89
  def test_follow_should_raise_error_if_wrong_parameters
66
90
  assert_raise( ArgumentError ) do
67
91
  Thimbl::Command.follow
@@ -69,32 +93,10 @@ class CommandTest < Test::Unit::TestCase
69
93
  end
70
94
 
71
95
  def test_follow
72
- thimbl = Thimbl::Base.new
73
- Thimbl::Command.expects( :load ).returns( thimbl )
74
- thimbl.expects( :follow ).with( 'wadus', 'wadus@domain.com' )
96
+ thimbl = Thimbl::Base.new 'user@thimbl.net'
97
+ Thimbl::Command.expects( :get_actual ).returns( thimbl )
98
+ thimbl.expects( :follow! ).with( 'wadus', 'wadus@domain.com', 'password' )
75
99
 
76
- Thimbl::Command.follow 'wadus', 'wadus@domain.com'
77
- end
78
-
79
- def test_load_should_raise_error_if_not_config_file
80
- Thimbl::Command.stubs( :cache_path ).returns( '/tmp/not_exists_file' )
81
-
82
- assert_raise( ArgumentError ) do
83
- Thimbl::Command.load
84
- end
85
- end
86
-
87
- def test_load
88
- temp_file = Tempfile.new 'cache.json'
89
- temp_file.write "{ \"a\" : 1 }"
90
- temp_file.close
91
-
92
- Thimbl::Command.stubs( :cache_path ).returns( temp_file.path )
93
-
94
- thimbl = Thimbl::Command.load
95
-
96
- assert_equal 1, thimbl.data['a']
97
-
98
- temp_file.unlink
100
+ Thimbl::Command.follow 'wadus', 'wadus@domain.com', 'password'
99
101
  end
100
102
  end