voxcast_api 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,11 +1,15 @@
1
+ gem 'url_maker'
1
2
  require 'time'
2
3
  require 'set'
3
4
  require 'digest/md5'
4
5
  require 'forwardable'
5
6
  require 'hpricot'
7
+ require 'active_support/core_ext/hash'
8
+ require 'url_maker'
6
9
 
7
10
  class VoxcastApi::Base < VoxcastApi::Http
8
11
  extend Forwardable
12
+ include UrlMaker
9
13
 
10
14
  def_delegators :@entry, :device_id
11
15
  attr_accessor :key, :secret_key
@@ -1,5 +1,4 @@
1
1
  class VoxcastApi::Http
2
- include UrlMaker
3
2
  attr_accessor :url, :user, :password
4
3
 
5
4
  def initialize(entry = nil)
@@ -0,0 +1,174 @@
1
+ require 'cgi'
2
+ require 'net/http'
3
+ require 'digest/md5'
4
+ require 'voxcast_api'
5
+
6
+ describe VoxcastApi::Base do
7
+ class MockHTTP
8
+ attr_accessor :requests, :gets, :posts, :get_response, :auth,
9
+ :post_response, :request_response, :read_timeout, :use_ssl, :ssl_args
10
+
11
+ def initialize
12
+ @requests = []
13
+ @gets = []
14
+ @posts = []
15
+ @get_response = nil
16
+ @post_response = nil
17
+ @request_response = nil
18
+ @read_timeout = nil
19
+ @use_ssl = nil
20
+ @ssl_args = nil
21
+ # used to work with HTTPi expectations as well
22
+ @auth = MockAuth.new
23
+ end
24
+
25
+ def start
26
+ yield self
27
+ end
28
+
29
+ def ssl_client_auth(ssl_args)
30
+ @ssl_args = ssl_args
31
+ end
32
+
33
+ def request(req)
34
+ @requests << req
35
+ @request_response
36
+ end
37
+
38
+ def get(uri)
39
+ @gets << uri
40
+ @get_response
41
+ end
42
+
43
+ def post(uri, data)
44
+ @posts << [uri, data]
45
+ @post_response
46
+ end
47
+ end
48
+
49
+ class MockAuth
50
+ attr_accessor :user, :password, :verify_mode, :ssl
51
+
52
+ def initialize(init_ssl = true)
53
+ @user = @password = @verify_mode = nil
54
+ @ssl = MockAuth.new(false) if init_ssl
55
+ end
56
+
57
+ def basic(user, pass)
58
+ @user = user
59
+ @password = pass
60
+ end
61
+ end
62
+
63
+ def mock_http(set = true)
64
+ http = MockHTTP.new
65
+ Net::HTTP.stub!(:new).and_return(http) if set
66
+ http
67
+ end
68
+
69
+ def setup_http(code = '200', body = '')
70
+ @mock_http = mock_http
71
+ @mock_resp = mock('response')
72
+ @mock_resp.stub!(:code).and_return code
73
+ @mock_resp.stub!(:body).and_return body
74
+ @mock_http.get_response = @mock_resp
75
+ @mock_http.post_response = @mock_resp
76
+ @mock_http.request_response = @mock_resp
77
+ end
78
+
79
+ before(:each) do
80
+ @mock_entry = mock("Voxcast", :purge_url => 'http://purge.com', :purge_user => 'user',
81
+ :purge_password => 'pass', :purge_key => 'key', :purge_secret_key => 'secret',
82
+ :http_domain => 'domain', :logger => nil)
83
+ @api = VoxcastApi::Base.new(@mock_entry)
84
+ base_dir = File.expand_path('../../../xml', __FILE__)
85
+ @log_list = File.join(base_dir, 'log_list.xml')
86
+ @purge_ret = File.join(base_dir, 'purge.xml')
87
+ end
88
+
89
+ it "should check auth_key? properly" do
90
+ params = {}
91
+ @api.auth_key?(params).should == false
92
+ params = {'method' => 'something'}
93
+ @api.auth_key?(params).should == false
94
+ params = {'method' => 'voxel.hapi.authkeys.read'}
95
+ @api.auth_key?(params).should == true
96
+ end
97
+
98
+ it "should execute_method properly" do
99
+ params = {'a' => 'b'}
100
+ looked = 'found'
101
+ @api.should_receive(:execute).with(nil, params.merge('method' => looked))
102
+ @api.should_receive(:lookup_method).with(:purge_file).and_return looked
103
+ @api.execute_method(:purge_file, params)
104
+ end
105
+
106
+ it "should execute_method for auth_key" do
107
+ @api.should_receive(:execute).with('https://purge.com', {'method' => 'voxel.hapi.authkeys.read'})
108
+ @api.execute_method(:auth_key)
109
+ end
110
+
111
+ it "should generate api_sig properly" do
112
+ params = {'a' => 'b',
113
+ 'c' => 'd'
114
+ }
115
+ expected = Digest::MD5.hexdigest('secret keyabcd')
116
+ sig = @api.api_sig(params, 'secret key')
117
+ sig.should == expected
118
+ end
119
+
120
+ it "should generate api_sig if using sym" do
121
+ params = {:c => 'd',
122
+ 'a' => 'b'
123
+ }
124
+ expected = Digest::MD5.hexdigest('secret keyabcd')
125
+ sig = @api.api_sig(params, 'secret key')
126
+ sig.should == expected
127
+ end
128
+
129
+ it "should process_response properly" do
130
+ resp = mock('response')
131
+ body = File.read(@purge_ret)
132
+ resp.stub!(:code).and_return 200
133
+ resp.stub!(:body).and_return body
134
+ ret = @api.process_response(resp)
135
+ ret[:status].should == true
136
+ id = '048c7546-b4f8-42f4-89da-3544cbd13c4c'
137
+ ret[:detail]['transaction']['id'].should == id
138
+ end
139
+
140
+ it "should get_data properly" do
141
+ @api.should_receive(:timestamp).and_return 'timestamp'
142
+ @api.should_receive(:api_sig).and_return 'sig'
143
+ CGI.parse(@api.get_data).should == {'timestamp' => ['timestamp'], 'key' => ['key'], 'api_sig' => ['sig']}
144
+ end
145
+
146
+ it "should handle log_list which return error" do
147
+ @api.should_receive(:execute_method).with(:log_list, {:complex_parser => true}).and_return({:status => false})
148
+ @api.log_list.should == []
149
+ end
150
+
151
+ it "should handle log_list properly" do
152
+ setup_http
153
+ body = File.read(@log_list)
154
+ @mock_resp.stub!(:body).and_return body
155
+ ret = @api.log_list
156
+ # there should be 4 log files
157
+ ret.size.should == 4
158
+ end
159
+
160
+ it "should handle log_download properly" do
161
+ setup_http
162
+ @mock_resp.stub!(:body).and_return 'blah'
163
+ dst = File.join('/tmp', 'dst_file')
164
+ begin
165
+ @api.should_not_receive(:process_response)
166
+ @api.log_download('myfile', dst).should be_true
167
+ File.exists?(dst).should be_true
168
+ File.read(dst).should == 'blah'
169
+ ensure
170
+ File.delete(dst) if File.exists?(dst)
171
+ end
172
+ end
173
+
174
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 0
8
- - 0
9
- version: 1.0.0
8
+ - 1
9
+ version: 1.0.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Bruce Wang
@@ -34,6 +34,7 @@ files:
34
34
  - lib/voxcast_api/base.rb
35
35
  - lib/voxcast_api/http.rb
36
36
  - lib/voxcast_api/purge.rb
37
+ - spec/lib/voxcast_api_spec.rb
37
38
  has_rdoc: true
38
39
  homepage: http://rubygems.org/gems/voxcast_api
39
40
  licenses: []