ardekantur / taskomaly

a ruby api for tasko @ taskodone.com

This URL has Read+Write access

taskomaly / spec / api_spec.rb
100644 182 lines (147 sloc) 6.503 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
require File.dirname(__FILE__) + '/base'
 
describe "Taskomaly::API" do
  
  TEST_KEY = '0b9ca1660ec4cedffd9b5b163ce7a577aa31b'
 
  before do
    opts = { 'user' => API_CONFIG_USER, 'key' => API_CONFIG_KEY }
    File.open( API_CONFIG_LOCATION, 'w' ) do |out|
        YAML.dump( opts, out )
      end
  end
 
  it "requires both a user ID and an API key, or the location of a configuration file" do
    lambda { bad_t = Taskomaly::API.new }.should raise_error( ArgumentError )
    lambda { bad_t = Taskomaly::API.new :user => 0 }.should raise_error( ArgumentError )
    lambda { bad_t = Taskomaly::API.new :key => TEST_KEY }.should raise_error( ArgumentError )
    
    t = Taskomaly::API.new :user => 9999, :key => TEST_KEY
    t.user.should == 9999
    t.key.should == TEST_KEY
    
    lambda { config_t = Taskomaly::API.new :config => API_CONFIG_LOCATION }.should_not raise_error( ArgumentError )
  end
  
  it "should reject a negative user ID" do
    lambda { bad_t = Taskomaly::API.new :user => -3, :key => TEST_KEY }.should raise_error( ArgumentError )
  end
  
  it "should reject an invalid API key" do
    lambda { bad_t = Taskomaly::API.new :user => 345, :key => 'mumbo-jumbo' }.should raise_error( ArgumentError )
    lambda { bad_t = Taskomaly::API.new :user => 345, :key => '' }.should raise_error( ArgumentError )
  end
 
  it "can load a user ID and API key from a configuration file" do
    t = Taskomaly::API.new :config => API_CONFIG_LOCATION
    t.user.should == API_CONFIG_USER
    t.key.should == API_CONFIG_KEY
  end
    
  it "should raise an error if a specified configured file does not exist" do
    lambda { bad_t = Taskomaly::API.new :config => 'i_do_not_exist.yml' }.should raise_error( ArgumentError )
  end
  
  it "should work with shortcut methods" do
    t = Taskomaly::From API_CONFIG_LOCATION
    t.user.should == API_CONFIG_USER
    t.key.should == API_CONFIG_KEY
    
    t = Taskomaly::With :user => 9889, :key => TEST_KEY
    t.user.should == 9889
    t.key.should == TEST_KEY
  end
  
  it "returns papers with itself as an API" do
    xml = <<-XML
<?xml version='1.0' encoding='UTF-8'?>
<methodResponse>
<params>
<param>
<value><string>#{TEST_PAPER_ONE}</string></value>
</param>
</params>
</methodResponse>
XML
 
    t = Taskomaly::From API_CONFIG_LOCATION
    request = mock('RestClient::Resource')
    request.stubs(:send).with( :post, t.send( :base_payload, 'paper', 'Test Paper One' ) ).returns(xml)
    t.stubs(:request_generator).returns(request)
 
    p = t.request :paper, 'Test Paper One'
    p.name.should == 'Test Paper One'
    p.body.should == TEST_PAPER_ONE
 
    p.send(:api).should == t
  end
    
  it "needs a place to store a response from the server" do
    t = Taskomaly::API.new :config => API_CONFIG_LOCATION
    t.respond_to?( :response, true ).should == true
  end
  
  it "should format a base payload correctly" do
    xml = <<-XML
<methodCall><methodName>test_method</methodName>
<params>
<param><value><int>#{API_CONFIG_USER}</int></value></param>
<param><value><string>#{API_CONFIG_KEY}</string></value></param>
</params></methodCall>
XML
    t = Taskomaly::API.new :config => API_CONFIG_LOCATION
    t.send( :base_payload, 'test_method' ).gsub(/[ \n]/, '').should == xml.strip.gsub(/[ \n]/, '')
  end
  
  it "should format any parameters into a payload correctly" do
    xml = <<-XML
<methodCall><methodName>test_method</methodName>
<params>
<param><value><int>#{API_CONFIG_USER}</int></value></param>
<param><value><string>#{API_CONFIG_KEY}</string></value></param>
<param><value><string>this is a test</string></value></param>
<param><value><integer>37</integer></value></param>
<param><value><string>signals</string></value></param>
</params></methodCall>
XML
    t = Taskomaly::API.new :config => API_CONFIG_LOCATION
    t.send( :base_payload, 'test_method', [ 'this is a test', 37, 'signals' ] ).gsub(/[ \n]/, '').should == xml.strip.gsub(/[ \n]/, '')
  end
  
  it "only supports the actions specified by the API" do
    pending "Not done yet"
  end
  
  it "must raise an error if the server is unavailable" do
    t = Taskomaly::API.new :config => API_CONFIG_LOCATION
 
    request = mock('RestClient::Resource')
    request.expects(:send).with( :post, t.send( :base_payload, 'papers' ) ).raises(SocketError)
    
    t.stubs(:request_generator).returns(request)
    lambda { t.request :papers }.should raise_error(RuntimeError, 'the site appears to be unavailable')
  end
 
  it "can retrieve a response from the server" do
    xml = <<-XML
<?xml version='1.0' encoding='UTF-8'?>
<methodResponse>
<params><param><value><array><data>
<value><string>Paper One</string></value>
<value><string>Paper Two</string></value>
</data></array></value></param></params>
</methodResponse>
XML
    
    t = Taskomaly::API.new :config => API_CONFIG_LOCATION
    
    request = mock('RestClient::Resource')
    request.expects(:send).with( :post, t.send( :base_payload, 'papers') ).returns(xml)
    
    t.stubs(:request_generator).returns(request)
    t.request :papers
    t.response.should_not == nil
  end
 
  it "can retrieve a list of papers from the server" do
    xml = <<-XML
<?xml version='1.0' encoding='UTF-8'?>
<methodResponse>
<params><param><value><array><data>
<value><string>Paper One</string></value>
<value><string>Paper Two</string></value>
</data></array></value></param></params>
</methodResponse>
XML
    
    t = Taskomaly::API.new :config => API_CONFIG_LOCATION
    
    request = mock('RestClient::Resource')
    request.expects(:send).with( :post, t.send( :base_payload, 'papers') ).returns(xml)
    
    t.stubs(:request_generator).returns(request)
    papers = t.request :papers
    papers.sort.should == ['Paper One', 'Paper Two']
    papers.size.should == 2
  end
 
  it "can synchronize a local file with the server copy" do
    pending "Not done yet"
    #BODY = "Project Awesome:\n- This is task number one @home\n- This is task number two @done"
  end
  
 
  it "can load a configuration file specified relative to ~" do
    file_loc = '~/.test_tasks.yml'
    Taskomaly::API.any_instance.stubs(:parse_config).with(File.expand_path(file_loc)).returns({'user' => API_CONFIG_USER, 'key' => API_CONFIG_KEY})
    t = Taskomaly::API.new :config => file_loc
    t.user.should == API_CONFIG_USER
    t.key.should == API_CONFIG_KEY
  end
 
end