twimock 0.0.1 → 0.0.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 98d87b06d405b5e1b62b1e556b0364db89fd791f
4
- data.tar.gz: 51a1cc2ac6c7bf7bc616330947265a52f3736462
3
+ metadata.gz: 5309fdefb9500a7240260529d4059c8582224540
4
+ data.tar.gz: 9317dc0fe012b4fa78217a63bd57d387de84553c
5
5
  SHA512:
6
- metadata.gz: 1bada1bcbe144100f5d8358303b6e9214cface5cd33052e5f5f4f0f8154a06ac45b24b606b658dc09f2bc38748ef4f808b186d5b6c8242e37a11e7b562568d29
7
- data.tar.gz: 46af02913e36302f7eb310e1eda72711fa7857bedd0dc7cce99f72e5dc9e3a2c0038ac101d96975272ea588f562b17910645393a5da4871d14533eb813904fd6
6
+ metadata.gz: bbe6f91f69f875e7ccb370972f09954043a1336cf86248ad25ff3ccc8ad4f7771b9fd00a431d4b70a18d577d37ed5ea596ba1ffde53506e50d39b72d2b30b3a3
7
+ data.tar.gz: a7d786545527bf373a9742bab18d6cca9e0cbdf84dd6bb11777e33627e34d928a79ab096cad952fdf596100827350e8e4e1a6a5dc0a8d78a5d170a4e3406fdc5
@@ -24,6 +24,8 @@ module Twimock
24
24
 
25
25
  if !validate_request_token(@oauth_token)
26
26
  raise Twimock::Errors::InvalidRequestToken.new
27
+ elsif body.cancel
28
+ raise Twimock::Errors::OAuthCancelled.new
27
29
  elsif !(user = Twimock::User.find_by_tiwtter_id_or_email(@username_or_email))
28
30
  raise Twimock::Errors::InvalidUsernameOrEmail.new
29
31
  elsif @password.blank? || @password != user.password
@@ -43,6 +45,12 @@ module Twimock
43
45
  header = { "Content-Length" => body.bytesize.to_s,
44
46
  "Location" => callback_url }
45
47
  [ status, header, [ body ] ]
48
+ rescue Twimock::Errors::OAuthCancelled
49
+ status = 303
50
+ body = ""
51
+ header = { "Content-Length" => body.bytesize.to_s,
52
+ "Location" => "/oauth/authorize?oauth_token=#{@oauth_token}&cancel=true" }
53
+ [ status, header, [ body ] ]
46
54
  rescue Twimock::Errors::InvalidUsernameOrEmail, Twimock::Errors::InvalidPassword => @error
47
55
  response = unauthorized
48
56
  response[0] = 302
@@ -1,6 +1,8 @@
1
1
  require 'twimock/api/oauth/access_token'
2
2
  require 'twimock/api/oauth/request_token'
3
3
  require 'twimock/api/oauth/authenticate'
4
+ require 'twimock/api/oauth/authorize'
5
+ require 'twimock/api/oauth/cancelled'
4
6
  require 'twimock/api/intent/sessions'
5
7
  require 'twimock/api/account/verify_credentials'
6
8
  require 'twimock/errors'
@@ -0,0 +1,50 @@
1
+ require 'uri'
2
+ require 'erb'
3
+
4
+ module Twimock
5
+ module API
6
+ # 認証キャンセル後の画面を返すAPI
7
+ # POST http://api.twimock.com/oauth/authorize
8
+ class OAuth
9
+ class Authorize < OAuth
10
+ METHOD = "GET"
11
+ PATH = "/oauth/authorize"
12
+
13
+ def call(env)
14
+ return super unless called?(env)
15
+ begin
16
+ request = Rack::Request.new(env)
17
+ @oauth_token = request.params["oauth_token"]
18
+ @cancel = request.params["cancel"]
19
+
20
+ if !validate_request_token(@oauth_token)
21
+ raise Twimock::Errors::InvalidRequestToken.new
22
+ elsif @cancel == "true"
23
+ raise Twimock::Errors::OAuthCancelled.new
24
+ end
25
+
26
+ status = 200
27
+ body = ""
28
+ header = { "Content-Length" => body.bytesize.to_s }
29
+ [ status, header, [ body ] ]
30
+ rescue Twimock::Errors::InvalidRequestToken => @error
31
+ unauthorized
32
+ rescue Twimock::Errors::OAuthCancelled => @error
33
+ oauth_cancelled
34
+ rescue => @error
35
+ internal_server_error
36
+ end
37
+ end
38
+
39
+ private
40
+
41
+ def oauth_cancelled
42
+ status = 200
43
+ body = Twimock::API::OAuth::Cancelled.view(@oauth_token)
44
+ header = { "Content-Length" => body.bytesize.to_s }
45
+ [ status, header, [ body ] ]
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,24 @@
1
+ module Twimock
2
+ module API
3
+ # OAuthでブラウザ認証するAPI
4
+ # GET http://api.twimock.com/authenticate?oauth_token=xxx
5
+ class OAuth
6
+ class Cancelled
7
+ VIEW_DIRECTORY = File.expand_path("../../../../../view", __FILE__)
8
+ VIEW_FILE_NAME = "oauth_cancelled.html.erb"
9
+
10
+ def self.view(oauth_token)
11
+ @oauth_token = oauth_token
12
+ erb = ERB.new(File.read(filepath))
13
+ erb.result(binding)
14
+ end
15
+
16
+ private
17
+
18
+ def self.filepath
19
+ File.join(VIEW_DIRECTORY, VIEW_FILE_NAME)
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -9,5 +9,7 @@ module Twimock
9
9
  class InvalidUsernameOrEmail < Error; end
10
10
  class InvalidPassword < Error; end
11
11
  class ApplicationNotFound < Error; end
12
+ class OAuthCancelled < Error; end
13
+ class InternalServerError < Error; end
12
14
  end
13
15
  end
@@ -1,3 +1,3 @@
1
1
  module Twimock
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -161,6 +161,24 @@ describe Twimock::API::Intent::Sessions do
161
161
  it_behaves_like 'API 302 Redircted Callback URL'
162
162
  end
163
163
 
164
+ context 'with authenticate canceled' do
165
+ before do
166
+ application = Twimock::Application.new
167
+ application.save!
168
+ @request_token = Twimock::RequestToken.new(application_id: application.id)
169
+ @request_token.save!
170
+ @body = { oauth_token: @request_token.string, cancel: 'true' }
171
+ post path, @body, header
172
+ end
173
+
174
+ it 'should return 303 Temporary Redirect /oauth/authorize' do
175
+ expect(last_response.status).to eq 303
176
+ location = Twimock::API::OAuth::Authorize::PATH + "?oauth_token=#{@request_token.string}&cancel=true"
177
+ expect(last_response.header['Location']).to eq location
178
+ expect(last_response.body).to be_blank
179
+ end
180
+ end
181
+
164
182
  context 'raise error that is not catched' do
165
183
  before do
166
184
  allow_any_instance_of(Twimock::API::Intent::Sessions).to receive(:query_string_to_hash) do
@@ -0,0 +1,101 @@
1
+ require 'spec_helper'
2
+ require 'rack/test'
3
+
4
+ describe Twimock::API::OAuth::Authorize do
5
+ include TestApplicationHelper
6
+ include APISpecHelper
7
+ include Rack::Test::Methods
8
+
9
+ let(:method) { 'GET' }
10
+ let(:path) { '/oauth/authorize' }
11
+ let(:body) { "" }
12
+ let(:header) { {} }
13
+ let(:test_app) { TestApplicationHelper::TestRackApplication.new }
14
+ let(:app) { Twimock::API::OAuth::Authorize.new(test_app) }
15
+
16
+ describe '::METHOD' do
17
+ subject { Twimock::API::OAuth::Authorize::METHOD }
18
+ it { is_expected.to eq method }
19
+ end
20
+
21
+ describe '::PATH' do
22
+ subject { Twimock::API::OAuth::Authorize::PATH }
23
+ it { is_expected.to eq path }
24
+ end
25
+
26
+ describe "POST '/oauth/authorize'" 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 'with invalid oauth token' do
34
+ before do
35
+ @request_token = Twimock::RequestToken.new
36
+ get path + "?oauth_token=#{@request_token.string}", body, header
37
+ end
38
+ it_behaves_like 'API 401 UnAuthorized'
39
+ end
40
+
41
+ context 'with only valid oauth token' do
42
+ before do
43
+ application = Twimock::Application.new
44
+ application.save!
45
+ @request_token = Twimock::RequestToken.new(application_id: application.id)
46
+ @request_token.save!
47
+ get path + "?oauth_token=#{@request_token.string}", body, header
48
+ end
49
+
50
+ it 'should return 200 OK' do
51
+ expect(last_response.status).to eq 200
52
+ expect(last_response.header['Content-Length']).to eq last_response.body.bytesize.to_s
53
+ expect(last_response.body).to be_blank
54
+ end
55
+ end
56
+
57
+ context 'with valid oauth token and cancel' do
58
+ before do
59
+ application = Twimock::Application.new
60
+ application.save!
61
+ @request_token = Twimock::RequestToken.new(application_id: application.id)
62
+ @request_token.save!
63
+ get path + "?oauth_token=#{@request_token.string}&cancel=true", body, header
64
+ end
65
+
66
+ it 'should return 200 OK with Cancelled view' do
67
+ view = Twimock::API::OAuth::Cancelled.view(@request_token.string)
68
+ expect(last_response.status).to eq 200
69
+ expect(last_response.header['Content-Length']).to eq last_response.body.bytesize.to_s
70
+ expect(last_response.body).to eq view
71
+ end
72
+ end
73
+
74
+ context 'when raise unexpected error anywhere' do
75
+ before do
76
+ allow_any_instance_of(Rack::Request).to receive(:params) { raise }
77
+ application = Twimock::Application.new
78
+ application.save!
79
+ @request_token = Twimock::RequestToken.new(application_id: application.id)
80
+ @request_token.save!
81
+ get path + "?oauth_token=#{@request_token.string}", body, header
82
+ end
83
+ it_behaves_like 'API 500 InternalServerError'
84
+ end
85
+ end
86
+
87
+ describe "get '/test'" do
88
+ before { get '/test' }
89
+ it_behaves_like 'TestRackApplication 200 OK'
90
+ end
91
+
92
+ describe "POST '/oauth/authorize'" do
93
+ before { post '/oauth/authorize' }
94
+ it_behaves_like 'TestRackApplication 200 OK'
95
+ end
96
+
97
+ describe "POST '/oauth/authorization'" do
98
+ before { get '/oauth/authorization' }
99
+ it_behaves_like 'TestRackApplication 200 OK'
100
+ end
101
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe Twimock::API::OAuth::Cancelled do
4
+ let(:view_directory) { File.expand_path("../../../../../view", __FILE__) }
5
+ let(:view_file_name) { "oauth_cancelled.html.erb" }
6
+
7
+ describe '::VIEW_DIRECTORY' do
8
+ subject { Twimock::API::OAuth::Cancelled::VIEW_DIRECTORY }
9
+ it { is_expected.to eq view_directory }
10
+ end
11
+
12
+ describe '::VIEW_FILE_NAME' do
13
+ subject { Twimock::API::OAuth::Cancelled::VIEW_FILE_NAME }
14
+ it { is_expected.to eq view_file_name }
15
+ end
16
+
17
+ describe '.view' do
18
+ context 'without oauth_token' do
19
+ subject { lambda { Twimock::API::OAuth::Cancelled.view } }
20
+ it { is_expected.to raise_error ArgumentError }
21
+ end
22
+
23
+ context 'with oauth token' do
24
+ before { @oauth_token = Twimock::RequestToken.new.string }
25
+ subject { Twimock::API::OAuth::Cancelled.view(@oauth_token) }
26
+ it { is_expected.to be_include "<!DOCTYPE html>" }
27
+ it { is_expected.to be_include 'body class="oauth cancelled' }
28
+ it { is_expected.to be_include @oauth_token }
29
+ end
30
+ end
31
+ end
data/spec/twimock_spec.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Twimock do
4
- let(:version) { '0.0.1' }
4
+ let(:version) { '0.0.2' }
5
5
  let(:db_name) { '.test' }
6
6
  let(:provider) { 'twitter' }
7
7
 
@@ -16,6 +16,7 @@
16
16
  <input type="hidden" name="remember_me" value="1">
17
17
  <input type="hidden" name="oauth_token" value="<%= @oauth_token %>">
18
18
  <input type="submit" value="login" class="submit button selected" id="allow">
19
+ <input type="submit" value="cancel" class="submit button" id="cancel" name="cancel">
19
20
  </form>
20
21
  </div>
21
22
  </div>
@@ -0,0 +1,18 @@
1
+ <!DOCTYPE html>
2
+ <html lang="ja" dir="ltr" class="">
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <title>Authorize Twimock Application</title>
6
+ </head>
7
+
8
+ <body class="oauth cancelled tfw ja logged-out noloki">
9
+ <div id="bd" role="main">
10
+ <div class="auth">
11
+ <h1>You have not signed in</h1>
12
+ <p class="cancel-callback">
13
+ <a href="<%= Twimock::Config.callback_url %>?denied=<%= @oauth_token %>">Return to App</a>
14
+ </p>
15
+ </div>
16
+ </div>
17
+ </body>
18
+ </html>
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: twimock
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - ogawatti
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-20 00:00:00.000000000 Z
11
+ date: 2015-03-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sqlite3
@@ -258,6 +258,8 @@ files:
258
258
  - lib/twimock/api/oauth.rb
259
259
  - lib/twimock/api/oauth/access_token.rb
260
260
  - lib/twimock/api/oauth/authenticate.rb
261
+ - lib/twimock/api/oauth/authorize.rb
262
+ - lib/twimock/api/oauth/cancelled.rb
261
263
  - lib/twimock/api/oauth/request_token.rb
262
264
  - lib/twimock/application.rb
263
265
  - lib/twimock/auth_hash.rb
@@ -281,6 +283,8 @@ files:
281
283
  - spec/twimock/api/intent/sessions_spec.rb
282
284
  - spec/twimock/api/oauth/access_token_spec.rb
283
285
  - spec/twimock/api/oauth/authenticate_spec.rb
286
+ - spec/twimock/api/oauth/authorize_spec.rb
287
+ - spec/twimock/api/oauth/cancelled_spec.rb
284
288
  - spec/twimock/api/oauth/request_token_spec.rb
285
289
  - spec/twimock/api_spec.rb
286
290
  - spec/twimock/application_spec.rb
@@ -294,6 +298,7 @@ files:
294
298
  - spec/twimock_spec.rb
295
299
  - twimock.gemspec
296
300
  - view/authenticate.html.erb
301
+ - view/oauth_cancelled.html.erb
297
302
  homepage: https://github.com/ogawatti/twimock
298
303
  licenses:
299
304
  - MIT
@@ -330,6 +335,8 @@ test_files:
330
335
  - spec/twimock/api/intent/sessions_spec.rb
331
336
  - spec/twimock/api/oauth/access_token_spec.rb
332
337
  - spec/twimock/api/oauth/authenticate_spec.rb
338
+ - spec/twimock/api/oauth/authorize_spec.rb
339
+ - spec/twimock/api/oauth/cancelled_spec.rb
333
340
  - spec/twimock/api/oauth/request_token_spec.rb
334
341
  - spec/twimock/api_spec.rb
335
342
  - spec/twimock/application_spec.rb