vkontakte_api 1.2 → 1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,40 +2,48 @@ require 'spec_helper'
2
2
 
3
3
  describe VkontakteApi::Client do
4
4
  before(:each) do
5
- @user_id = double("User id")
6
- @string_token = double("Access token as a String")
7
- @expires_at = Time.now - 2 * 60 * 60 # 2.hours.ago
8
-
9
- @oauth2_token = double("Access token as an OAuth2::AccessToken")
10
- @oauth2_token.stub(:token).and_return(@string_token)
11
- @oauth2_token.stub(:params).and_return('user_id' => @user_id)
12
- @oauth2_token.stub(:expires_at).and_return(@expires_at)
5
+ @user_id = double("User id")
6
+ end
7
+
8
+ def oauth2_token(expires_at = Time.now)
9
+ token = double("Access token as an OAuth2::AccessToken").tap do |token|
10
+ token.stub(:token).and_return(string_token)
11
+ token.stub(:params).and_return('user_id' => @user_id)
12
+ token.stub(:expires_at).and_return(expires_at)
13
+ end
13
14
  end
14
15
 
16
+ let(:string_token) { double("Access token as a String") }
17
+ let(:expired_token) { oauth2_token(Time.now - 2 * 60 * 60) }
18
+ let(:actual_token) { oauth2_token(Time.now + 2 * 60 * 60) }
19
+
15
20
  describe "#initialize" do
16
21
  context "without arguments" do
22
+ let(:client) { VkontakteApi::Client.new }
23
+
17
24
  it "creates a client with a nil access_token" do
18
- client = VkontakteApi::Client.new
19
- client.token.should be_nil
25
+ expect(client.token).to be_nil
20
26
  end
21
27
  end
22
28
 
23
29
  context "with a token argument" do
24
30
  context "with a string value" do
31
+ let(:client) { VkontakteApi::Client.new(string_token) }
32
+
25
33
  it "creates a client with a given access_token" do
26
- client = VkontakteApi::Client.new(@string_token)
27
- client.token.should == @string_token
34
+ expect(client.token).to eq(string_token)
28
35
  end
29
36
  end
30
37
 
31
38
  context "with an OAuth2::AccessToken value" do
39
+ let(:client) { VkontakteApi::Client.new(oauth2_token) }
40
+
32
41
  it "extracts the string token and uses it" do
33
- client = VkontakteApi::Client.new(@oauth2_token)
34
- client.token.should == @string_token
35
- client.user_id.should == @user_id
42
+ expect(client.token).to eq(string_token)
43
+ expect(client.user_id).to eq(@user_id)
36
44
 
37
- client.expires_at.should be_a(Time)
38
- client.expires_at.should be < Time.now
45
+ expect(client.expires_at).to be_a(Time)
46
+ expect(client.expires_at).to be < Time.now
39
47
  end
40
48
  end
41
49
  end
@@ -43,60 +51,57 @@ describe VkontakteApi::Client do
43
51
 
44
52
  describe "#authorized?" do
45
53
  context "with an unauthorized client" do
54
+ let(:client) { VkontakteApi::Client.new }
55
+
46
56
  it "returns false" do
47
- VkontakteApi::Client.new.should_not be_authorized
57
+ expect(client).not_to be_authorized
48
58
  end
49
59
  end
50
60
 
51
61
  context "with an authorized client" do
62
+ let(:client) { VkontakteApi::Client.new(string_token) }
63
+
52
64
  it "returns true" do
53
- VkontakteApi::Client.new(@string_token).should be_authorized
65
+ expect(client).to be_authorized
54
66
  end
55
67
  end
56
68
  end
57
69
 
58
70
  describe "#expired?" do
59
71
  context "with an expired token" do
60
- before(:each) do
61
- @client = VkontakteApi::Client.new(@oauth2_token)
62
- end
72
+ let(:client) { VkontakteApi::Client.new(expired_token) }
63
73
 
64
74
  it "returns true" do
65
- @client.should be_expired
75
+ expect(client).to be_expired
66
76
  end
67
77
  end
68
78
 
69
79
  context "with an actual token" do
70
- before(:each) do
71
- @oauth2_token.stub(:expires_at).and_return(Time.now + 2 * 24 * 60 * 60) # 2.days.from_now
72
- @client = VkontakteApi::Client.new(@oauth2_token)
73
- end
80
+ let(:client) { VkontakteApi::Client.new(actual_token) }
74
81
 
75
82
  it "returns false" do
76
- @client.should_not be_expired
83
+ expect(client).not_to be_expired
77
84
  end
78
85
  end
79
86
 
80
87
  context "with a String token" do
81
- before(:each) do
82
- @client = VkontakteApi::Client.new(@string_token)
83
- end
88
+ let(:client) { VkontakteApi::Client.new(string_token) }
84
89
 
85
90
  it "returns false" do
86
- @client.should_not be_expired
91
+ expect(client).not_to be_expired
87
92
  end
88
93
  end
89
94
  end
90
95
 
91
96
  describe "#scope" do
92
- before(:each) do
93
- @client = VkontakteApi::Client.new
94
- @client.stub(:get_user_settings).and_return(865310)
97
+ let(:client) do
98
+ VkontakteApi::Client.new.tap do |client|
99
+ client.stub(:get_user_settings).and_return(865310)
100
+ end
95
101
  end
102
+ let(:scope) { client.scope }
96
103
 
97
104
  it "returns an array of access rights" do
98
- scopes_array = @client.scope
99
-
100
105
  [
101
106
  :friends,
102
107
  :photos,
@@ -108,7 +113,38 @@ describe VkontakteApi::Client do
108
113
  :groups,
109
114
  :notifications
110
115
  ].each do |access_scope|
111
- scopes_array.should include(access_scope)
116
+ expect(scope).to include(access_scope)
117
+ end
118
+ end
119
+ end
120
+
121
+ describe "#method_missing" do
122
+ let(:client) do
123
+ token = double("Token")
124
+ VkontakteApi::Client.new(token)
125
+ end
126
+
127
+ context "called with a namespace" do
128
+ it "returns a Namespace instance" do
129
+ expect(client.users).to be_a(VkontakteApi::Namespace)
130
+ end
131
+ end
132
+
133
+ context "called with a method" do
134
+ before(:each) do
135
+ @result = double("Result")
136
+ @method = double("Method", call: @result)
137
+ VkontakteApi::Method.stub(:new).and_return(@method)
138
+ end
139
+
140
+ it "creates a Method instance" do
141
+ expect(VkontakteApi::Method).to receive(:new).with(:get, resolver: client.resolver)
142
+ client.get(id: 1)
143
+ end
144
+
145
+ it "calls Method#call and returns the result" do
146
+ expect(@method).to receive(:call).with(id: 1)
147
+ expect(client.get(id: 1)).to eq(@result)
112
148
  end
113
149
  end
114
150
  end
@@ -7,30 +7,31 @@ end
7
7
  describe VkontakteApi::Configuration do
8
8
  describe "#configure" do
9
9
  it "yields self" do
10
- Configurable.should_receive(:some_method)
10
+ expect(Configurable).to receive(:some_method)
11
11
  Configurable.configure do |config|
12
12
  config.some_method
13
13
  end
14
14
  end
15
15
 
16
16
  it "returns self" do
17
- Configurable.configure.should == Configurable
17
+ expect(Configurable.configure).to eq(Configurable)
18
18
  end
19
19
  end
20
20
 
21
21
  describe "#reset" do
22
22
  it "sets all options to their default values" do
23
23
  Configurable.reset
24
- Configurable.app_id.should be_nil
25
- Configurable.app_secret.should be_nil
26
- Configurable.adapter.should == VkontakteApi::Configuration::DEFAULT_ADAPTER
27
- Configurable.http_verb.should == VkontakteApi::Configuration::DEFAULT_HTTP_VERB
28
- Configurable.faraday_options.should == {}
29
24
 
30
- Configurable.logger.should be_a(Logger)
31
- Configurable.should log_requests
32
- Configurable.should log_errors
33
- Configurable.should_not log_responses
25
+ expect(Configurable.app_id).to be_nil
26
+ expect(Configurable.app_secret).to be_nil
27
+ expect(Configurable.adapter).to eq(VkontakteApi::Configuration::DEFAULT_ADAPTER)
28
+ expect(Configurable.http_verb).to eq(VkontakteApi::Configuration::DEFAULT_HTTP_VERB)
29
+ expect(Configurable.faraday_options).to eq(Hash.new)
30
+
31
+ expect(Configurable.logger).to be_a(Logger)
32
+ expect(Configurable).to log_requests
33
+ expect(Configurable).to log_errors
34
+ expect(Configurable).not_to log_responses
34
35
  end
35
36
  end
36
37
  end
@@ -1,8 +1,8 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe VkontakteApi::Error do
4
- before(:each) do
5
- @error_data = Hashie::Mash.new(
4
+ let(:error_data) do
5
+ Hashie::Mash.new(
6
6
  error_code: 5,
7
7
  error_msg: 'User authorization failed: invalid access_token.',
8
8
  request_params: [
@@ -24,24 +24,22 @@ describe VkontakteApi::Error do
24
24
 
25
25
  describe "#message" do
26
26
  context "without parameters" do
27
- before(:each) do
28
- @e = VkontakteApi::Error.new(@error_data)
29
- end
27
+ let(:error) { VkontakteApi::Error.new(error_data) }
30
28
 
31
29
  it "returns all needed data about an error" do
32
30
  message = 'VKontakte returned an error 5: \'User authorization failed: invalid access_token.\''
33
31
  message << ' after calling method \'unknownMethod\' without parameters.'
34
32
 
35
33
  expect {
36
- raise @e
37
- }.to raise_error(@e.class, message)
34
+ raise error
35
+ }.to raise_error(error.class, message)
38
36
  end
39
37
  end
40
38
 
41
39
  context "with parameters" do
42
- before(:each) do
43
- @error_data[:request_params] << Hashie::Mash.new(key: 'some', value: 'params')
44
- @e = VkontakteApi::Error.new(@error_data)
40
+ let(:error) do
41
+ error_data[:request_params] << Hashie::Mash.new(key: 'some', value: 'params')
42
+ VkontakteApi::Error.new(error_data)
45
43
  end
46
44
 
47
45
  it "returns all needed data about an error" do
@@ -49,8 +47,8 @@ describe VkontakteApi::Error do
49
47
  message << ' after calling method \'unknownMethod\' with parameters {"some"=>"params"}.'
50
48
 
51
49
  expect {
52
- raise @e
53
- }.to raise_error(@e.class, message)
50
+ raise error
51
+ }.to raise_error(error.class, message)
54
52
  end
55
53
  end
56
54
  end
@@ -2,10 +2,18 @@ require 'spec_helper'
2
2
 
3
3
  describe VkontakteApi::Logger do
4
4
  before(:each) do
5
- @success_response = Oj.dump('a' => 1, 'b' => 2)
6
- @fail_response = Oj.dump('error' => 404)
5
+ VkontakteApi.logger = double("Logger").as_null_object
7
6
 
8
- @connection = Faraday.new(url: 'http://example.com') do |builder|
7
+ VkontakteApi.log_requests = false
8
+ VkontakteApi.log_responses = false
9
+ VkontakteApi.log_errors = false
10
+ end
11
+
12
+ let(:success_response) { Oj.dump('a' => 1, 'b' => 2) }
13
+ let(:fail_response) { Oj.dump('error' => 404) }
14
+
15
+ let(:connection) do
16
+ Faraday.new(url: 'http://example.com') do |builder|
9
17
  builder.request :url_encoded
10
18
  builder.response :vk_logger
11
19
  builder.response :mashify
@@ -13,23 +21,18 @@ describe VkontakteApi::Logger do
13
21
 
14
22
  builder.adapter :test do |stub|
15
23
  stub.get('/success') do
16
- [200, {}, @success_response]
24
+ [200, {}, success_response]
17
25
  end
26
+
18
27
  stub.post('/success') do
19
- [200, {}, @success_response]
28
+ [200, {}, success_response]
20
29
  end
30
+
21
31
  stub.get('/fail') do
22
- [200, {}, @fail_response]
32
+ [200, {}, fail_response]
23
33
  end
24
34
  end
25
35
  end
26
-
27
- @logger = double("Logger").as_null_object
28
- VkontakteApi.logger = @logger
29
-
30
- VkontakteApi.log_requests = false
31
- VkontakteApi.log_responses = false
32
- VkontakteApi.log_errors = false
33
36
  end
34
37
 
35
38
  context "with VkontakteApi.log_requests?" do
@@ -38,23 +41,23 @@ describe VkontakteApi::Logger do
38
41
  end
39
42
 
40
43
  it "logs the request URL" do
41
- @logger.should_receive(:debug).with('GET http://example.com/success')
42
- @connection.get('/success')
44
+ expect(VkontakteApi.logger).to receive(:debug).with('GET http://example.com/success')
45
+ connection.get('/success')
43
46
  end
44
47
 
45
48
  context "with a POST request" do
46
49
  it "logs the request URL and the request body" do
47
- @logger.should_receive(:debug).with('POST http://example.com/success')
48
- @logger.should_receive(:debug).with('body: "param=1"')
49
- @connection.post('/success', param: 1)
50
+ expect(VkontakteApi.logger).to receive(:debug).with('POST http://example.com/success')
51
+ expect(VkontakteApi.logger).to receive(:debug).with('body: "param=1"')
52
+ connection.post('/success', param: 1)
50
53
  end
51
54
  end
52
55
  end
53
56
 
54
57
  context "without VkontakteApi.log_requests?" do
55
58
  it "doesn't log the request" do
56
- @logger.should_not_receive(:debug)
57
- @connection.get('/success')
59
+ expect(VkontakteApi.logger).not_to receive(:debug)
60
+ connection.get('/success')
58
61
  end
59
62
  end
60
63
 
@@ -65,15 +68,15 @@ describe VkontakteApi::Logger do
65
68
  end
66
69
 
67
70
  it "logs the response body" do
68
- @logger.should_receive(:debug).with(@success_response)
69
- @connection.get('/success')
71
+ expect(VkontakteApi.logger).to receive(:debug).with(success_response)
72
+ connection.get('/success')
70
73
  end
71
74
  end
72
75
 
73
76
  context "without VkontakteApi.log_responses?" do
74
77
  it "doesn't log the response body" do
75
- @logger.should_not_receive(:debug)
76
- @connection.get('/success')
78
+ expect(VkontakteApi.logger).not_to receive(:debug)
79
+ connection.get('/success')
77
80
  end
78
81
  end
79
82
  end
@@ -85,15 +88,15 @@ describe VkontakteApi::Logger do
85
88
  end
86
89
 
87
90
  it "logs the response body" do
88
- @logger.should_receive(:warn).with(@fail_response)
89
- @connection.get('/fail')
91
+ expect(VkontakteApi.logger).to receive(:warn).with(fail_response)
92
+ connection.get('/fail')
90
93
  end
91
94
  end
92
95
 
93
96
  context "without VkontakteApi.log_errors?" do
94
97
  it "doesn't log the response body" do
95
- @logger.should_not_receive(:warn)
96
- @connection.get('/fail')
98
+ expect(VkontakteApi.logger).not_to receive(:warn)
99
+ connection.get('/fail')
97
100
  end
98
101
  end
99
102
  end
@@ -2,42 +2,45 @@ require 'spec_helper'
2
2
 
3
3
  describe VkontakteApi::Method do
4
4
  describe "#call" do
5
+ let(:full_name) { double("Full method name") }
6
+ let(:args) { double("Method arguments") }
7
+ let(:token) { double("Access token") }
8
+
9
+ let(:method) do
10
+ VkontakteApi::Method.new('some_name').tap do |method|
11
+ method.stub(:full_name).and_return(full_name)
12
+ method.stub(:token).and_return(token)
13
+ end
14
+ end
15
+
5
16
  before(:each) do
6
- @full_name = double("Full method name")
7
- @args = double("Method arguments")
8
- @token = double("Access token")
9
-
10
- @method = VkontakteApi::Method.new('some_name')
11
- @method.stub(:full_name).and_return(@full_name)
12
- @method.stub(:token).and_return(@token)
13
17
  VkontakteApi::Result.stub(:process)
14
18
  end
15
19
 
16
20
  it "calls API.call with full name, args and token" do
17
- VkontakteApi::API.should_receive(:call).with(@full_name, @args, @token)
18
- @method.call(@args)
21
+ expect(VkontakteApi::API).to receive(:call).with(full_name, args, token)
22
+ method.call(args)
19
23
  end
20
24
 
21
25
  it "sends the response to Result.process" do
22
26
  response = double("VK response")
23
27
  VkontakteApi::API.stub(:call).and_return(response)
24
28
  type = double("Type")
25
- @method.stub(:type).and_return(type)
29
+ method.stub(:type).and_return(type)
26
30
 
27
- VkontakteApi::Result.should_receive(:process).with(response, type, nil)
28
- @method.call(@args)
31
+ expect(VkontakteApi::Result).to receive(:process).with(response, type, nil)
32
+ method.call(args)
29
33
  end
30
34
  end
31
35
 
32
36
  describe "#full_name" do
33
- before(:each) do
37
+ let(:method) do
34
38
  resolver = Hashie::Mash.new(name: 'name_space')
35
- @name = 'name'
36
- @method = VkontakteApi::Method.new(@name, resolver: resolver)
39
+ VkontakteApi::Method.new('name', resolver: resolver)
37
40
  end
38
41
 
39
42
  it "sends each part to #camelize" do
40
- @method.send(:full_name).should == 'nameSpace.name'
43
+ expect(method.send(:full_name)).to eq('nameSpace.name')
41
44
  end
42
45
  end
43
46
 
@@ -45,14 +48,14 @@ describe VkontakteApi::Method do
45
48
  context "with a usual name" do
46
49
  it "returns :anything" do
47
50
  method = VkontakteApi::Method.new('get')
48
- method.send(:type).should == :anything
51
+ expect(method.send(:type)).to eq(:anything)
49
52
  end
50
53
  end
51
54
 
52
55
  context "with a predicate name" do
53
56
  it "returns :boolean" do
54
57
  method = VkontakteApi::Method.new('is_app_user?')
55
- method.send(:type).should == :boolean
58
+ expect(method.send(:type)).to eq(:boolean)
56
59
  end
57
60
  end
58
61
  end