twimock 0.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.
Files changed (55) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +23 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +5 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE.txt +22 -0
  7. data/README.md +125 -0
  8. data/Rakefile +6 -0
  9. data/db/.gitkeep +0 -0
  10. data/lib/twimock/access_token.rb +31 -0
  11. data/lib/twimock/api/account/verify_credentials.rb +40 -0
  12. data/lib/twimock/api/application.rb +29 -0
  13. data/lib/twimock/api/intent/sessions.rb +60 -0
  14. data/lib/twimock/api/oauth/access_token.rb +65 -0
  15. data/lib/twimock/api/oauth/authenticate.rb +51 -0
  16. data/lib/twimock/api/oauth/request_token.rb +49 -0
  17. data/lib/twimock/api/oauth.rb +83 -0
  18. data/lib/twimock/api.rb +35 -0
  19. data/lib/twimock/application.rb +21 -0
  20. data/lib/twimock/auth_hash.rb +8 -0
  21. data/lib/twimock/config.rb +90 -0
  22. data/lib/twimock/database/table.rb +359 -0
  23. data/lib/twimock/database.rb +133 -0
  24. data/lib/twimock/errors.rb +13 -0
  25. data/lib/twimock/omniauth/strategies/twitter.rb +28 -0
  26. data/lib/twimock/omniauth_twitter.rb +36 -0
  27. data/lib/twimock/request_token.rb +23 -0
  28. data/lib/twimock/user.rb +58 -0
  29. data/lib/twimock/version.rb +3 -0
  30. data/lib/twimock.rb +39 -0
  31. data/spec/spec_helper.rb +18 -0
  32. data/spec/support/api_spec_helper.rb +30 -0
  33. data/spec/support/omniauth_twitter_helper.rb +26 -0
  34. data/spec/support/tables_helper.rb +54 -0
  35. data/spec/support/test_application_helper.rb +9 -0
  36. data/spec/twimock/access_token_spec.rb +128 -0
  37. data/spec/twimock/api/account/verify_credentials_spec.rb +125 -0
  38. data/spec/twimock/api/application_spec.rb +27 -0
  39. data/spec/twimock/api/intent/sessions_spec.rb +184 -0
  40. data/spec/twimock/api/oauth/access_token_spec.rb +185 -0
  41. data/spec/twimock/api/oauth/authenticate_spec.rb +96 -0
  42. data/spec/twimock/api/oauth/request_token_spec.rb +123 -0
  43. data/spec/twimock/api_spec.rb +81 -0
  44. data/spec/twimock/application_spec.rb +120 -0
  45. data/spec/twimock/auth_hash_spec.rb +7 -0
  46. data/spec/twimock/config_spec.rb +192 -0
  47. data/spec/twimock/database/table_spec.rb +769 -0
  48. data/spec/twimock/database_spec.rb +261 -0
  49. data/spec/twimock/omniauth_twitter_spec.rb +129 -0
  50. data/spec/twimock/request_token_spec.rb +140 -0
  51. data/spec/twimock/user_spec.rb +271 -0
  52. data/spec/twimock_spec.rb +76 -0
  53. data/twimock.gemspec +38 -0
  54. data/view/authenticate.html.erb +23 -0
  55. metadata +343 -0
@@ -0,0 +1,184 @@
1
+ require 'spec_helper'
2
+ require 'rack/test'
3
+
4
+ describe Twimock::API::Intent::Sessions do
5
+ include TestApplicationHelper
6
+ include APISpecHelper
7
+ include Rack::Test::Methods
8
+
9
+ let(:method) { 'POST' }
10
+ let(:path) { '/intent/sessions' }
11
+ let(:body) { "" }
12
+ let(:header) { {} }
13
+ let(:test_app) { TestApplicationHelper::TestRackApplication.new }
14
+ let(:app) { Twimock::API::Intent::Sessions.new(test_app) }
15
+
16
+ def query_string_to_hash(query_string)
17
+ ary = URI::decode_www_form(query_string)
18
+ hash = Hash[ary]
19
+ Hashie::Mash.new(hash)
20
+ end
21
+
22
+ describe '::METHOD' do
23
+ subject { Twimock::API::Intent::Sessions::METHOD }
24
+ it { is_expected.to eq method }
25
+ end
26
+
27
+ describe '::PATH' do
28
+ subject { Twimock::API::Intent::Sessions::PATH }
29
+ it { is_expected.to eq path }
30
+ end
31
+
32
+ shared_examples 'API 302 InvalidInputData' do
33
+ it 'should return 302 Redirected /oauth/authenticate' do
34
+ expect(last_response.status).to eq 302
35
+ expect(last_response.header).not_to be_blank
36
+ expect(last_response.header['Content-Length']).to eq last_response.body.bytesize.to_s
37
+ expect(last_response.header['Content-Type']).to eq "application/json; charset=utf-8"
38
+ expect(last_response.header['Location']).not_to be_blank
39
+ location = URI.parse(last_response.header['Location'])
40
+ query = query_string_to_hash(location.query)
41
+ expect(location.path).to eq "/oauth/authenticate"
42
+ expect(query).to be_has_key "oauth_token"
43
+ expect(query["oauth_token"]).to eq @body[:oauth_token]
44
+ expect(last_response.body).not_to be_blank
45
+ parsed_body = JSON.parse(last_response.body)
46
+ expect(parsed_body["error"]["code"]).to match /^Invalid.*/
47
+ end
48
+ end
49
+
50
+ shared_examples 'API 302 Redircted Callback URL' do
51
+ it 'should return 302 Redirected callback url' do
52
+ post path, @body, header
53
+
54
+ expect(last_response.status).to eq 302
55
+ expect(last_response.header).not_to be_blank
56
+ expect(last_response.header['Content-Length']).to eq last_response.body.bytesize.to_s
57
+ query_string = "oauth_token=#{@request_token.string}&oauth_verifier=#{@request_token.verifier}"
58
+ location = Twimock::Config.callback_url + "?" + query_string
59
+ expect(last_response.header['Location']).to eq location
60
+ expect(last_response.body).to be_blank
61
+ user_id = Twimock::RequestToken.find_by_string(@body[:oauth_token]).user_id
62
+ expect(user_id).to eq @user.id
63
+ end
64
+ end
65
+
66
+ describe "POST '/oauth/request_token'" do
67
+ before { stub_const("Twimock::Database::DEFAULT_DB_NAME", db_name) }
68
+ after { database.drop }
69
+
70
+ let(:db_name) { ".test" }
71
+ let(:database) { Twimock::Database.new }
72
+
73
+ context 'without oauth token' do
74
+ before { post path, body, header }
75
+ it_behaves_like 'API 401 UnAuthorized'
76
+ end
77
+
78
+ context 'with invalid oauth token' do
79
+ before do
80
+ request_token = Twimock::RequestToken.new
81
+ @body = { 'session[username_or_email]' => "testuser",
82
+ 'session[password]' => "testpass",
83
+ oauth_token: request_token.string }
84
+ post path, @body, header
85
+ end
86
+ it_behaves_like 'API 401 UnAuthorized'
87
+ end
88
+
89
+ context 'with only valid oauth token' do
90
+ before do
91
+ application = Twimock::Application.new
92
+ application.save!
93
+ request_token = Twimock::RequestToken.new(application_id: application.id)
94
+ request_token.save!
95
+ @body = { oauth_token: request_token.string }
96
+ post path, @body, header
97
+ end
98
+ it_behaves_like 'API 302 InvalidInputData'
99
+ end
100
+
101
+ context 'with only valid oauth token and invalid username' do
102
+ before do
103
+ application = Twimock::Application.new
104
+ application.save!
105
+ request_token = Twimock::RequestToken.new(application_id: application.id)
106
+ request_token.save!
107
+ user = Twimock::User.new(name: "testuser")
108
+ user.save!
109
+ @body = { 'session[username_or_email]' => "invalidusername",
110
+ oauth_token: request_token.string }
111
+ post path, @body, header
112
+ end
113
+ it_behaves_like 'API 302 InvalidInputData'
114
+ end
115
+
116
+ context 'with valid oauth token and username and invalid password' do
117
+ before do
118
+ application = Twimock::Application.new
119
+ application.save!
120
+ request_token = Twimock::RequestToken.new(application_id: application.id)
121
+ request_token.save!
122
+ user = Twimock::User.new(password: "testpass")
123
+ user.save!
124
+ @body = { 'session[username_or_email]' => user.twitter_id,
125
+ 'session[password]' => "invalidpassword",
126
+ oauth_token: request_token.string }
127
+ post path, @body, header
128
+ end
129
+ it_behaves_like 'API 302 InvalidInputData'
130
+ end
131
+
132
+ context 'with valid oauth token and username and password' do
133
+ before do
134
+ application = Twimock::Application.new
135
+ application.save!
136
+ @request_token = Twimock::RequestToken.new(application_id: application.id)
137
+ @request_token.save!
138
+ @user = Twimock::User.new
139
+ @user.save!
140
+ @body = { 'session[username_or_email]' => @user.twitter_id,
141
+ 'session[password]' => @user.password,
142
+ oauth_token: @request_token.string }
143
+ post path, @body, header
144
+ end
145
+ it_behaves_like 'API 302 Redircted Callback URL'
146
+ end
147
+
148
+ context 'with valid oauth token and email and password' do
149
+ before do
150
+ application = Twimock::Application.new
151
+ application.save!
152
+ @request_token = Twimock::RequestToken.new(application_id: application.id)
153
+ @request_token.save!
154
+ @user = Twimock::User.new
155
+ @user.save!
156
+ @body = { 'session[username_or_email]' => @user.email,
157
+ 'session[password]' => @user.password,
158
+ oauth_token: @request_token.string }
159
+ post path, @body, header
160
+ end
161
+ it_behaves_like 'API 302 Redircted Callback URL'
162
+ end
163
+
164
+ context 'raise error that is not catched' do
165
+ before do
166
+ allow_any_instance_of(Twimock::API::Intent::Sessions).to receive(:query_string_to_hash) do
167
+ lambda { raise }
168
+ end
169
+ post path, @body, header
170
+ end
171
+ it_behaves_like 'API 500 InternalServerError'
172
+ end
173
+ end
174
+
175
+ describe "GET '/intent/sessions'" do
176
+ before { get '/intent/sessions' }
177
+ it_behaves_like 'TestRackApplication 200 OK'
178
+ end
179
+
180
+ describe "POST '/oauth/sessions'" do
181
+ before { post '/oauth/sessions' }
182
+ it_behaves_like 'TestRackApplication 200 OK'
183
+ end
184
+ end
@@ -0,0 +1,185 @@
1
+ require 'spec_helper'
2
+ require 'rack/test'
3
+
4
+ describe Twimock::API::OAuth::AccessToken do
5
+ include TestApplicationHelper
6
+ include Rack::Test::Methods
7
+
8
+ let(:method) { 'POST' }
9
+ let(:path) { '/oauth/access_token' }
10
+ let(:authorization_regexp) { Regexp.new('OAuth oauth_body_hash=\"(.*)\", oauth_consumer_key=\"(.*)\", oauth_nonce=\"(.*)\", oauth_signature=\"(.*)\", oauth_signature_method=\"(.*)\", oauth_timestamp=\"(.*)\", oauth_token=\"(.*)\", oauth_verifier=\"(.*)\", oauth_version=\"(.*)\"') }
11
+ let(:body) { "" }
12
+ let(:header) { {} }
13
+ let(:test_app) { TestApplicationHelper::TestRackApplication.new }
14
+ let(:app) { Twimock::API::OAuth::AccessToken.new(test_app) }
15
+
16
+ def create_authorization_header(consumer_key, token)
17
+ params = {
18
+ body_hash: "2jmj7l5rSw0yVb%2FvlWAYkK%2FYBwk%3D",
19
+ consumer_key: consumer_key,
20
+ nonce: "IowIhqA1ckGHxbDL3pRVU3Td7BHfo2CWx7a6BArMveE",
21
+ signature: "FfuyevfGWuVC5ZBUta0J4TmFFfQ%3D",
22
+ signature_method: "HMAC-SHA1",
23
+ timestamp: "1422273884",
24
+ token: token,
25
+ verifier: "Mk8kPU3Del5IrhQuxdYAVVJIAHeetQ4M",
26
+ version: "1.0" }
27
+ string = params.inject([]){|a, (k,v)| a << "oauth_#{k}=\"#{v}\"" }.join(", ")
28
+ [ "OAuth #{string}" ]
29
+ end
30
+
31
+ describe '::METHOD' do
32
+ subject { Twimock::API::OAuth::AccessToken::METHOD }
33
+ it { is_expected.to eq method }
34
+ end
35
+
36
+ describe '::PATH' do
37
+ subject { Twimock::API::OAuth::AccessToken::PATH }
38
+ it { is_expected.to eq path }
39
+ end
40
+
41
+ describe '::AUTHORIZATION_REGEXP' do
42
+ subject { Twimock::API::OAuth::AccessToken::AUTHORIZATION_REGEXP }
43
+ it { is_expected.to eq authorization_regexp }
44
+ end
45
+
46
+ shared_examples "Get Access Token" do
47
+ it 'should return 200 Created' do
48
+ post path, body, header
49
+
50
+ expect(last_response.status).to eq 200
51
+ expect(last_response.header).not_to be_blank
52
+ expect(last_response.header['Content-Length']).to eq last_response.body.bytesize.to_s
53
+ expect(last_response.body).not_to be_blank
54
+
55
+ index = last_response.body =~ /^oauth_token=(.*)&oauth_token_secret=(.*)&user_id=(.*)&screen_name=(.*)$/
56
+ expect(index).to eq 0
57
+ oauth_token = $1
58
+ oauth_token_secret = $2
59
+ user_id = $3.to_i
60
+ screen_name = $4
61
+
62
+ access_token = Twimock::AccessToken.find_by_string(oauth_token)
63
+ expect(access_token).not_to be_nil
64
+ expect(access_token.secret).to eq oauth_token_secret
65
+ expect(access_token.user_id).to eq user_id
66
+ user = Twimock::User.find_by_id(user_id)
67
+ expect(user).not_to be_nil
68
+ expect(user.twitter_id).to eq screen_name
69
+ end
70
+ end
71
+
72
+ describe "POST '/oauth/access_token'" do
73
+ context 'with authorization header' do
74
+ before { stub_const("Twimock::Database::DEFAULT_DB_NAME", db_name) }
75
+ after { database.drop }
76
+
77
+ let(:db_name) { ".test" }
78
+ let(:database) { Twimock::Database.new }
79
+
80
+ let(:header) { { "authorization" => @authorization } }
81
+
82
+ context 'that is correct' do
83
+ before do
84
+ app = Twimock::Application.new
85
+ app.save!
86
+ user = Twimock::User.new
87
+ user.save!
88
+ request_token = Twimock::RequestToken.new(application_id: app.id, user_id: user.id)
89
+ request_token.save!
90
+ @authorization = create_authorization_header(app.api_key, request_token.string)
91
+ end
92
+ it_behaves_like "Get Access Token"
93
+
94
+ context 'authorization header is string' do
95
+ before do
96
+ app = Twimock::Application.new
97
+ app.save!
98
+ user = Twimock::User.new
99
+ user.save!
100
+ request_token = Twimock::RequestToken.new(application_id: app.id, user_id: user.id)
101
+ request_token.save!
102
+ @authorization = create_authorization_header(app.api_key, request_token.string).first
103
+ end
104
+ it_behaves_like "Get Access Token"
105
+ end
106
+
107
+ context 'raise error that is not catched' do
108
+ before do
109
+ allow(Twimock::RequestToken).to receive(:find_by_string){ raise }
110
+ post path, body, header
111
+ end
112
+ it_behaves_like 'API 500 InternalServerError'
113
+ end
114
+ end
115
+
116
+ context 'that is incorrect format' do
117
+ before do
118
+ @authorization = ["OAuth consumer_key=\"test_consumer_key\, oauth_token=\"test_token\""]
119
+ post path, body, header
120
+ end
121
+ it_behaves_like "API 401 UnAuthorized"
122
+ end
123
+
124
+ context 'but consumer_key is invalid' do
125
+ before do
126
+ app = Twimock::Application.new
127
+ request_token = Twimock::RequestToken.new(application_id: app.id)
128
+ @authorization = create_authorization_header(app.api_key, request_token.string)
129
+ post path, body, header
130
+ end
131
+ it_behaves_like "API 401 UnAuthorized"
132
+ end
133
+
134
+ context 'but oauth_token is invalid' do
135
+ before do
136
+ app = Twimock::Application.new
137
+ app.save!
138
+ request_token = Twimock::RequestToken.new(application_id: app.id)
139
+ @authorization = create_authorization_header(app.api_key, request_token.string)
140
+ post path, body, header
141
+ end
142
+ it_behaves_like "API 401 UnAuthorized"
143
+ end
144
+
145
+ context 'but oauth_token does not belong to user' do
146
+ before do
147
+ app = Twimock::Application.new
148
+ app.save!
149
+ request_token = Twimock::RequestToken.new(application_id: app.id)
150
+ request_token.save!
151
+ @authorization = create_authorization_header(app.api_key, request_token.string)
152
+ post path, body, header
153
+ end
154
+ it_behaves_like "API 401 UnAuthorized"
155
+ end
156
+
157
+ context 'but oauth_token does not belong to application' do
158
+ before do
159
+ app = Twimock::Application.new
160
+ app.save!
161
+ request_token = Twimock::RequestToken.new(application_id: app.id)
162
+ request_token.save!
163
+ @authorization = create_authorization_header(app.api_key, request_token.string)
164
+ post path, body, header
165
+ end
166
+ it_behaves_like "API 401 UnAuthorized"
167
+ end
168
+
169
+ context 'without authorization header' do
170
+ before { post path, body, header }
171
+ it_behaves_like "API 401 UnAuthorized"
172
+ end
173
+ end
174
+ end
175
+
176
+ describe "GET '/test'" do
177
+ before { post '/test' }
178
+ it_behaves_like 'TestRackApplication 200 OK'
179
+ end
180
+
181
+ describe "GET '/oauth/access_token'" do
182
+ before { get '/oauth/access_token' }
183
+ it_behaves_like 'TestRackApplication 200 OK'
184
+ end
185
+ end
@@ -0,0 +1,96 @@
1
+ require 'spec_helper'
2
+ require 'rack/test'
3
+
4
+ describe Twimock::API::OAuth::Authenticate do
5
+ include TestApplicationHelper
6
+ include APISpecHelper
7
+ include Rack::Test::Methods
8
+
9
+ let(:method) { 'GET' }
10
+ let(:path) { '/oauth/authenticate' }
11
+ let(:body) { "" }
12
+ let(:header) { {} }
13
+ let(:test_app) { TestApplicationHelper::TestRackApplication.new }
14
+ let(:app) { Twimock::API::OAuth::Authenticate.new(test_app) }
15
+
16
+ describe '::METHOD' do
17
+ subject { Twimock::API::OAuth::Authenticate::METHOD }
18
+ it { is_expected.to eq method }
19
+ end
20
+
21
+ describe '::PATH' do
22
+ subject { Twimock::API::OAuth::Authenticate::PATH }
23
+ it { is_expected.to eq path }
24
+ end
25
+
26
+ describe "GET '/oauth/authenticate'" do
27
+ before { stub_const("Twimock::Database::DEFAULT_DB_NAME", db_name) }
28
+ after { database.drop }
29
+
30
+ let(:db_name) { ".test" }
31
+ let(:database) { Twimock::Database.new }
32
+
33
+ context 'without oauth token' do
34
+ before { get path, body, header }
35
+ it_behaves_like 'API 401 UnAuthorized'
36
+ end
37
+
38
+ context 'with invalid oauth token' do
39
+ before do
40
+ request_token = Twimock::RequestToken.new
41
+ query_string = "request_token=#{request_token.string}"
42
+ get path + "?" + query_string , body, header
43
+ end
44
+ it_behaves_like 'API 401 UnAuthorized'
45
+ end
46
+
47
+ context 'with valid oauth token' do
48
+ before do
49
+ application = Twimock::Application.new
50
+ application.save!
51
+ @request_token = Twimock::RequestToken.new(application_id: application.id)
52
+ @request_token.save!
53
+ @path = path + "?oauth_token=#{@request_token.string}"
54
+ get @path, body, header
55
+ end
56
+
57
+ it 'should return 200 OK' do
58
+ view = Twimock::API::OAuth::Authenticate.view(@request_token.string)
59
+ expect(last_response.status).to eq 200
60
+ expect(last_response.header).not_to be_blank
61
+ expect(last_response.header['Content-Length']).to eq last_response.body.bytesize.to_s
62
+ expect(last_response.body).to eq view
63
+ expect(last_response.body).to be_include(@request_token.string)
64
+ expect(last_response.body).to be_include(Twimock::API::Intent::Sessions::PATH)
65
+ end
66
+ end
67
+
68
+ context 'raise error that is not catched' do
69
+ before do
70
+ allow(Twimock::API::OAuth::Authenticate).to receive(:view){ raise }
71
+ application = Twimock::Application.new
72
+ application.save!
73
+ @request_token = Twimock::RequestToken.new(application_id: application.id)
74
+ @request_token.save!
75
+ @path = path + "?oauth_token=#{@request_token.string}"
76
+ get @path, body, header
77
+ end
78
+ it_behaves_like 'API 500 InternalServerError'
79
+ end
80
+ end
81
+
82
+ describe "GET '/test'" do
83
+ before { get '/test' }
84
+ it_behaves_like 'TestRackApplication 200 OK'
85
+ end
86
+
87
+ describe "POST '/oauth/authenticate'" do
88
+ before { post '/oauth/authenticate' }
89
+ it_behaves_like 'TestRackApplication 200 OK'
90
+ end
91
+
92
+ describe "GET '/oauth/authentication'" do
93
+ before { get '/oauth/authentication' }
94
+ it_behaves_like 'TestRackApplication 200 OK'
95
+ end
96
+ end
@@ -0,0 +1,123 @@
1
+ require 'spec_helper'
2
+ require 'rack/test'
3
+
4
+ describe Twimock::API::OAuth::RequestToken do
5
+ include TestApplicationHelper
6
+ include Rack::Test::Methods
7
+
8
+ let(:method) { 'POST' }
9
+ let(:path) { '/oauth/request_token' }
10
+ let(:authorization_regexp) { Regexp.new('OAuth oauth_callback=\"(.*)\", oauth_consumer_key=\"(.*)\", oauth_nonce=\"(.*)\", oauth_signature=\"(.*)\", oauth_signature_method=\"(.*)\", oauth_timestamp=\"(.*)\", oauth_version=\"(.*)\".*') }
11
+ let(:body) { "" }
12
+ let(:header) { {} }
13
+ let(:test_app) { TestApplicationHelper::TestRackApplication.new }
14
+ let(:app) { Twimock::API::OAuth::RequestToken.new(test_app) }
15
+
16
+ describe '::METHOD' do
17
+ subject { Twimock::API::OAuth::RequestToken::METHOD }
18
+ it { is_expected.to eq method }
19
+ end
20
+
21
+ describe '::PATH' do
22
+ subject { Twimock::API::OAuth::RequestToken::PATH }
23
+ it { is_expected.to eq path }
24
+ end
25
+
26
+ describe '::AUTHORIZATION_REGEXP' do
27
+ subject { Twimock::API::OAuth::RequestToken::AUTHORIZATION_REGEXP }
28
+ it { is_expected.to eq authorization_regexp }
29
+ end
30
+
31
+ describe "POST '/oauth/request_token'" do
32
+ context 'with authorization header' do
33
+ before { stub_const("Twimock::Database::DEFAULT_DB_NAME", db_name) }
34
+ after { database.drop }
35
+
36
+ let(:db_name) { ".test" }
37
+ let(:database) { Twimock::Database.new }
38
+
39
+ let(:header) { { "authorization" => @authorization } }
40
+
41
+ context 'that is correct' do
42
+ before do
43
+ app = Twimock::Application.new
44
+ app.save!
45
+ @authorization = ["OAuth oauth_callback=\"http%3A%2F%2Fhiddeste.local.jp%3A3456%2Fusers%2Fauth%2Ftwitter%2Fcallback\", oauth_consumer_key=\"#{app.api_key}\", oauth_nonce=\"gop2czKq1IebHEvEIo2qE64Hwp5SRWxLgilYAKqrWE\", oauth_signature=\"FVn4chN1TbLPDDsLb%2FqG%2FU99biA%3D\", oauth_signature_method=\"HMAC-SHA1\", oauth_timestamp=\"1422273831\", oauth_version=\"1.0\""]
46
+ end
47
+
48
+ it 'should return 200 OK' do
49
+ post path, body, header
50
+ expect(last_response.status).to eq 200
51
+ expect(last_response.header).not_to be_blank
52
+ expect(last_response.header['Content-Length']).to eq last_response.body.bytesize.to_s
53
+ expect(last_response.body).not_to be_blank
54
+
55
+ index = last_response.body =~ /^oauth_token=(.*)&oauth_token_secret=(.*)&oauth_callback_confirmed=(.*)$/
56
+ expect(index).to eq 0
57
+ oauth_token = $1
58
+ oauth_secret = $2
59
+ oauth_callback_confirmed = $3
60
+
61
+ token = Twimock::RequestToken.find_by_string(oauth_token)
62
+ expect(token).not_to be_nil
63
+ expect(token.secret).to eq oauth_secret
64
+ expect(oauth_callback_confirmed).to eq true.to_s
65
+ end
66
+ end
67
+
68
+ context 'that is incorrect format' do
69
+ before do
70
+ @authorization = ["OAuth consumer_key=\"test_consumer_key\""]
71
+ post path, body, header
72
+ end
73
+ it_behaves_like "API 401 UnAuthorized"
74
+ end
75
+
76
+ context 'but consumer_key is invalid' do
77
+ before do
78
+ app = Twimock::Application.new
79
+ @authorization = ["OAuth oauth_callback=\"http%3A%2F%2Fhiddeste.local.jp%3A3456%2Fusers%2Fauth%2Ftwitter%2Fcallback\", oauth_consumer_key=\"#{app.api_key}\", oauth_nonce=\"gop2czKq1IebHEvEIo2qE64Hwp5SRWxLgilYAKqrWE\", oauth_signature=\"FVn4chN1TbLPDDsLb%2FqG%2FU99biA%3D\", oauth_signature_method=\"HMAC-SHA1\", oauth_timestamp=\"1422273831\", oauth_version=\"1.0\""]
80
+ post path, body, header
81
+ end
82
+ it_behaves_like "API 401 UnAuthorized"
83
+ end
84
+
85
+ context 'raise error that is not catched' do
86
+ before do
87
+ allow(Twimock::Application).to receive(:find_by_api_key){ raise }
88
+ app = Twimock::Application.new
89
+ app.save!
90
+ @authorization = ["OAuth oauth_callback=\"http%3A%2F%2Fhiddeste.local.jp%3A3456%2Fusers%2Fauth%2Ftwitter%2Fcallback\", oauth_consumer_key=\"#{app.api_key}\", oauth_nonce=\"gop2czKq1IebHEvEIo2qE64Hwp5SRWxLgilYAKqrWE\", oauth_signature=\"FVn4chN1TbLPDDsLb%2FqG%2FU99biA%3D\", oauth_signature_method=\"HMAC-SHA1\", oauth_timestamp=\"1422273831\", oauth_version=\"1.0\""]
91
+ post path, body, header
92
+ end
93
+ it_behaves_like 'API 500 InternalServerError'
94
+ end
95
+
96
+ context 'without authorization header'do
97
+ before { post path, body, header }
98
+ it_behaves_like "API 401 UnAuthorized"
99
+ end
100
+ end
101
+
102
+ end
103
+
104
+ describe "POST '/test'" do
105
+ it 'should return 200 OK' do
106
+ post '/test'
107
+
108
+ expect(last_response.status).to eq 200
109
+ expect(last_response.header).to be_blank
110
+ expect(last_response.body).to be_blank
111
+ end
112
+ end
113
+
114
+ describe "GET '/oauth/request_token'" do
115
+ it 'should return 200 OK' do
116
+ get '/oauth/request_token'
117
+
118
+ expect(last_response.status).to eq 200
119
+ expect(last_response.header).to be_blank
120
+ expect(last_response.body).to be_blank
121
+ end
122
+ end
123
+ end
@@ -0,0 +1,81 @@
1
+ require 'spec_helper'
2
+
3
+ describe Twimock::API do
4
+ let(:hostname) { "api.twitter.com" }
5
+ let(:port) { 443 }
6
+ let(:middlewares) { [ Twimock::API::OAuth::AccessToken,
7
+ Twimock::API::OAuth::RequestToken,
8
+ Twimock::API::Account::VerifyCredentials ] }
9
+
10
+
11
+ describe '::HOSTNAME' do
12
+ subject { Twimock::API::HOSTNAME }
13
+ it { is_expected.to eq hostname }
14
+ end
15
+
16
+ describe '::PORT' do
17
+ subject { Twimock::API::PORT }
18
+ it { is_expected.to eq port }
19
+ end
20
+
21
+ describe '::MIDDLEWARES' do
22
+ subject { Twimock::API::MIDDLEWARES }
23
+ it { is_expected.to eq middlewares }
24
+ end
25
+
26
+ describe '.on?' do
27
+ context 'when api mock is on' do
28
+ before { expect(ShamRack).to receive(:application_for).with(hostname, port) { Object.new } }
29
+ subject { Twimock::API.on? }
30
+ it { is_expected.to eq true }
31
+ end
32
+
33
+ context 'when api mock is off' do
34
+ before { expect(ShamRack).to receive(:application_for).with(hostname, port) { nil } }
35
+ subject { Twimock::API.on? }
36
+ it { is_expected.to eq false }
37
+ end
38
+ end
39
+
40
+ describe '.on' do
41
+ context 'when api mock is on' do
42
+ before do
43
+ expect(Twimock::API).to receive(:on?) { false }
44
+ expect(ShamRack).to receive(:at)
45
+ end
46
+ subject { Twimock::API.on }
47
+ it { is_expected.to eq true }
48
+ end
49
+
50
+ context 'when api mock is off' do
51
+ before do
52
+ expect(Twimock::API).to receive(:on?) { true }
53
+ end
54
+ subject { Twimock::API.on }
55
+ it { is_expected.to eq true }
56
+ end
57
+ end
58
+
59
+ describe '.off' do
60
+ before { expect(ShamRack).to receive(:unmount_all) }
61
+ subject { Twimock::API.off }
62
+ it { is_expected.to eq true }
63
+ end
64
+
65
+ describe '.app' do
66
+ subject { Twimock::API.app }
67
+ it { is_expected.to be_instance_of middlewares.last }
68
+
69
+ it 'should have middlewares and application as instance variable "app"' do
70
+ mid3 = Twimock::API.app
71
+ mid2 = mid3.instance_variable_get(:@app)
72
+ mid1 = mid2.instance_variable_get(:@app)
73
+ app = mid1.instance_variable_get(:@app)
74
+
75
+ expect(app.class).to eq Twimock::API::Application
76
+ expect(middlewares).to be_include mid1.class
77
+ expect(middlewares).to be_include mid2.class
78
+ expect(middlewares).to be_include mid3.class
79
+ end
80
+ end
81
+ end