This repository has been archived by the owner on Nov 26, 2020. It is now read-only.
forked from dodeja/ruby-lastfm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
lastfm_spec.rb
149 lines (128 loc) · 5.27 KB
/
lastfm_spec.rb
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
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
describe "Lastfm" do
before do
@lastfm = Lastfm.new('xxx', 'yyy')
end
it 'should have base_uri' do
Lastfm.base_uri.should eql('http://ws.audioscrobbler.com/2.0')
end
describe '.new' do
it 'should instantiate' do
@lastfm.should be_an_instance_of(Lastfm)
end
end
describe '#request' do
it 'should post' do
mock_response = mock(HTTParty::Response)
@lastfm.class.should_receive(:post).with('/', :body => {
:foo => 'bar',
:method => 'xxx.yyy',
:api_key => 'xxx',
:format => 'json'
}).and_return(mock_response)
mock_response.should_receive(:body).and_return('{ "bar": "baz" }')
@lastfm.request('xxx.yyy', { :foo => 'bar' }, :post, false, false)['bar'].should eql('baz')
end
it 'should post with signature' do
mock_response = mock(HTTParty::Response)
@lastfm.class.should_receive(:post).with('/', :body => {
:foo => 'bar',
:method => 'xxx.yyy',
:api_key => 'xxx',
:api_sig => Digest::MD5.hexdigest('api_keyxxxfoobarmethodxxx.yyyyyy'),
:format => 'json'
}).and_return(mock_response)
mock_response.should_receive(:body).and_return('{ "bar": "baz" }')
@lastfm.request('xxx.yyy', { :foo => 'bar' }, :post, true, false)['bar'].should eql('baz')
end
it 'should post with signature and session (request with authentication)' do
mock_response = mock(HTTParty::Response)
@lastfm.session = 'abcdef'
@lastfm.class.should_receive(:post).with('/', :body => {
:foo => 'bar',
:method => 'xxx.yyy',
:api_key => 'xxx',
:api_sig => Digest::MD5.hexdigest('api_keyxxxfoobarmethodxxx.yyyskabcdefyyy'),
:sk => 'abcdef',
:format => 'json'
}).and_return(mock_response)
mock_response.should_receive(:body).and_return('{ "bar": "baz" }')
@lastfm.request('xxx.yyy', { :foo => 'bar' }, :post, true, true)['bar'].should eql('baz')
end
it 'should get' do
mock_response = mock(HTTParty::Response)
@lastfm.class.should_receive(:get).with('/', :query => {
:foo => 'bar',
:method => 'xxx.yyy',
:api_key => 'xxx',
:format => 'json'
}).and_return(mock_response)
mock_response.should_receive(:body).and_return('{ "bar": "baz" }')
@lastfm.request('xxx.yyy', { :foo => 'bar' }, :get, false, false)['bar'].should eql('baz')
end
it 'should get with signature (request for authentication)' do
mock_response = mock(HTTParty::Response)
@lastfm.class.should_receive(:get).with('/', :query => {
:foo => 'bar',
:method => 'xxx.yyy',
:api_key => 'xxx',
:api_sig => Digest::MD5.hexdigest('api_keyxxxfoobarmethodxxx.yyyyyy'),
:format => 'json'
}).and_return(mock_response)
mock_response.should_receive(:body).and_return('{ "bar": "baz" }')
@lastfm.request('xxx.yyy', { :foo => 'bar' }, :get, true, false)['bar'].should eql('baz')
end
it 'should get with signature and session' do
mock_response = mock(HTTParty::Response)
@lastfm.session = 'abcdef'
@lastfm.class.should_receive(:get).with('/', :query => {
:foo => 'bar',
:method => 'xxx.yyy',
:api_key => 'xxx',
:api_sig => Digest::MD5.hexdigest('api_keyxxxfoobarmethodxxx.yyyskabcdefyyy'),
:sk => 'abcdef',
:format => 'json'
}).and_return(mock_response)
mock_response.should_receive(:body).and_return('{ "bar": "baz" }')
@lastfm.request('xxx.yyy', { :foo => 'bar' }, :get, true, true)['bar'].should eql('baz')
end
it 'should raise an error if an api error is ocuured' do
mock_response = mock(HTTParty::Response)
mock_response.should_receive(:body).and_return('{"message": "Invalid Method - No method with that name in this package", "error": 3}')
@lastfm.class.should_receive(:post).and_return(mock_response)
lambda {
@lastfm.request('xxx.yyy', { :foo => 'bar' }, :post)
}.should raise_error(Lastfm::ApiError, 'Invalid Method - No method with that name in this package')
end
end
describe '#auth' do
it 'should return an instance of Lastfm::Auth' do
@lastfm.auth.should be_an_instance_of(Lastfm::Auth)
end
it 'should get token' do
@lastfm.should_receive(:request).
with('auth.getToken', {}, :get, true).
and_return({ 'token' => 'xxxyyyzzz' })
@lastfm.auth.get_token.should eql('xxxyyyzzz')
end
it 'should get session' do
@lastfm.should_receive(:request).
with('auth.getSession', { :token => 'xxxyyyzzz' }, :get, true).
and_return({ 'session' => { 'key' => 'zzzyyyxxx' }})
@lastfm.auth.get_session('xxxyyyzzz').should eql('zzzyyyxxx')
end
end
describe '#track' do
it 'should return an instance of Lastfm::Track' do
@lastfm.track.should be_an_instance_of(Lastfm::Track)
end
it 'should love' do
@lastfm.session = 'abcdef'
@lastfm.should_receive(:request).with('track.love', {
:artist => 'foo artist',
:track => 'foo track',
}, :post, true, true).and_return({})
@lastfm.track.love('foo artist', 'foo track')
end
end
end