oauth-provider 0.5.0rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (139) hide show
  1. data/.gitignore +12 -0
  2. data/CHANGELOG +180 -0
  3. data/Gemfile +27 -0
  4. data/Guardfile +8 -0
  5. data/MIT-LICENSE +20 -0
  6. data/README.rdoc +393 -0
  7. data/Rakefile +2 -0
  8. data/UPGRADE.rdoc +21 -0
  9. data/generators/oauth_consumer/USAGE +10 -0
  10. data/generators/oauth_consumer/oauth_consumer_generator.rb +50 -0
  11. data/generators/oauth_consumer/templates/consumer_token.rb +11 -0
  12. data/generators/oauth_consumer/templates/controller.rb +27 -0
  13. data/generators/oauth_consumer/templates/index.html.erb +29 -0
  14. data/generators/oauth_consumer/templates/index.html.haml +18 -0
  15. data/generators/oauth_consumer/templates/migration.rb +20 -0
  16. data/generators/oauth_consumer/templates/oauth_config.rb +73 -0
  17. data/generators/oauth_consumer/templates/show.html.erb +7 -0
  18. data/generators/oauth_consumer/templates/show.html.haml +8 -0
  19. data/generators/oauth_provider/USAGE +20 -0
  20. data/generators/oauth_provider/lib/insert_routes.rb +67 -0
  21. data/generators/oauth_provider/oauth_provider_generator.rb +127 -0
  22. data/generators/oauth_provider/templates/_form.html.erb +17 -0
  23. data/generators/oauth_provider/templates/_form.html.haml +21 -0
  24. data/generators/oauth_provider/templates/access_token.rb +16 -0
  25. data/generators/oauth_provider/templates/authorize.html.erb +14 -0
  26. data/generators/oauth_provider/templates/authorize.html.haml +16 -0
  27. data/generators/oauth_provider/templates/authorize_failure.html.erb +1 -0
  28. data/generators/oauth_provider/templates/authorize_failure.html.haml +1 -0
  29. data/generators/oauth_provider/templates/authorize_success.html.erb +1 -0
  30. data/generators/oauth_provider/templates/authorize_success.html.haml +1 -0
  31. data/generators/oauth_provider/templates/client_application.rb +57 -0
  32. data/generators/oauth_provider/templates/client_application_spec.rb +29 -0
  33. data/generators/oauth_provider/templates/client_application_test.rb +42 -0
  34. data/generators/oauth_provider/templates/client_applications.yml +23 -0
  35. data/generators/oauth_provider/templates/clients_controller.rb +52 -0
  36. data/generators/oauth_provider/templates/clients_controller_spec.rb +176 -0
  37. data/generators/oauth_provider/templates/clients_controller_test.rb +280 -0
  38. data/generators/oauth_provider/templates/controller.rb +23 -0
  39. data/generators/oauth_provider/templates/edit.html.erb +7 -0
  40. data/generators/oauth_provider/templates/edit.html.haml +4 -0
  41. data/generators/oauth_provider/templates/index.html.erb +43 -0
  42. data/generators/oauth_provider/templates/index.html.haml +39 -0
  43. data/generators/oauth_provider/templates/migration.rb +47 -0
  44. data/generators/oauth_provider/templates/new.html.erb +5 -0
  45. data/generators/oauth_provider/templates/new.html.haml +5 -0
  46. data/generators/oauth_provider/templates/oauth2_authorize.html.erb +16 -0
  47. data/generators/oauth_provider/templates/oauth2_authorize.html.haml +17 -0
  48. data/generators/oauth_provider/templates/oauth2_token.rb +20 -0
  49. data/generators/oauth_provider/templates/oauth2_token_spec.rb +52 -0
  50. data/generators/oauth_provider/templates/oauth2_verifier.rb +35 -0
  51. data/generators/oauth_provider/templates/oauth2_verifier_spec.rb +44 -0
  52. data/generators/oauth_provider/templates/oauth_nonce.rb +13 -0
  53. data/generators/oauth_provider/templates/oauth_nonce_spec.rb +24 -0
  54. data/generators/oauth_provider/templates/oauth_nonce_test.rb +26 -0
  55. data/generators/oauth_provider/templates/oauth_nonces.yml +13 -0
  56. data/generators/oauth_provider/templates/oauth_token.rb +30 -0
  57. data/generators/oauth_provider/templates/oauth_token_spec.rb +309 -0
  58. data/generators/oauth_provider/templates/oauth_token_test.rb +57 -0
  59. data/generators/oauth_provider/templates/oauth_tokens.yml +17 -0
  60. data/generators/oauth_provider/templates/request_token.rb +40 -0
  61. data/generators/oauth_provider/templates/show.html.erb +27 -0
  62. data/generators/oauth_provider/templates/show.html.haml +30 -0
  63. data/init.rb +1 -0
  64. data/install.rb +2 -0
  65. data/lib/generators/active_record/oauth_provider_generator.rb +39 -0
  66. data/lib/generators/active_record/oauth_provider_templates/access_token.rb +16 -0
  67. data/lib/generators/active_record/oauth_provider_templates/client_application.rb +57 -0
  68. data/lib/generators/active_record/oauth_provider_templates/migration.rb +47 -0
  69. data/lib/generators/active_record/oauth_provider_templates/oauth2_token.rb +20 -0
  70. data/lib/generators/active_record/oauth_provider_templates/oauth2_verifier.rb +35 -0
  71. data/lib/generators/active_record/oauth_provider_templates/oauth_nonce.rb +13 -0
  72. data/lib/generators/active_record/oauth_provider_templates/oauth_token.rb +30 -0
  73. data/lib/generators/active_record/oauth_provider_templates/request_token.rb +40 -0
  74. data/lib/generators/erb/oauth_provider_generator.rb +23 -0
  75. data/lib/generators/erb/oauth_provider_templates/_form.html.erb +17 -0
  76. data/lib/generators/erb/oauth_provider_templates/authorize.html.erb +14 -0
  77. data/lib/generators/erb/oauth_provider_templates/authorize_failure.html.erb +1 -0
  78. data/lib/generators/erb/oauth_provider_templates/authorize_success.html.erb +1 -0
  79. data/lib/generators/erb/oauth_provider_templates/edit.html.erb +7 -0
  80. data/lib/generators/erb/oauth_provider_templates/index.html.erb +43 -0
  81. data/lib/generators/erb/oauth_provider_templates/new.html.erb +5 -0
  82. data/lib/generators/erb/oauth_provider_templates/oauth2_authorize.html.erb +16 -0
  83. data/lib/generators/erb/oauth_provider_templates/show.html.erb +27 -0
  84. data/lib/generators/haml/oauth_provider_generator.rb +28 -0
  85. data/lib/generators/haml/oauth_provider_templates/_form.html.haml +21 -0
  86. data/lib/generators/haml/oauth_provider_templates/authorize.html.haml +16 -0
  87. data/lib/generators/haml/oauth_provider_templates/authorize_failure.html.haml +1 -0
  88. data/lib/generators/haml/oauth_provider_templates/authorize_success.html.haml +1 -0
  89. data/lib/generators/haml/oauth_provider_templates/edit.html.haml +4 -0
  90. data/lib/generators/haml/oauth_provider_templates/index.html.haml +39 -0
  91. data/lib/generators/haml/oauth_provider_templates/new.html.haml +5 -0
  92. data/lib/generators/haml/oauth_provider_templates/oauth2_authorize.html.haml +17 -0
  93. data/lib/generators/haml/oauth_provider_templates/show.html.haml +30 -0
  94. data/lib/generators/mongoid/oauth_provider_generator.rb +21 -0
  95. data/lib/generators/mongoid/oauth_provider_templates/access_token.rb +16 -0
  96. data/lib/generators/mongoid/oauth_provider_templates/client_application.rb +71 -0
  97. data/lib/generators/mongoid/oauth_provider_templates/oauth2_token.rb +20 -0
  98. data/lib/generators/mongoid/oauth_provider_templates/oauth2_verifier.rb +35 -0
  99. data/lib/generators/mongoid/oauth_provider_templates/oauth_nonce.rb +24 -0
  100. data/lib/generators/mongoid/oauth_provider_templates/oauth_token.rb +44 -0
  101. data/lib/generators/mongoid/oauth_provider_templates/request_token.rb +36 -0
  102. data/lib/generators/oauth_inflections.rb +6 -0
  103. data/lib/generators/oauth_provider/USAGE +18 -0
  104. data/lib/generators/oauth_provider/oauth_provider_generator.rb +37 -0
  105. data/lib/generators/oauth_provider/templates/clients_controller.rb +52 -0
  106. data/lib/generators/oauth_provider/templates/controller.rb +23 -0
  107. data/lib/generators/rspec/oauth_provider_generator.rb +35 -0
  108. data/lib/generators/rspec/templates/client_application_spec.rb +29 -0
  109. data/lib/generators/rspec/templates/client_applications.yml +23 -0
  110. data/lib/generators/rspec/templates/clients_controller_spec.rb +176 -0
  111. data/lib/generators/rspec/templates/oauth2_token_spec.rb +52 -0
  112. data/lib/generators/rspec/templates/oauth2_verifier_spec.rb +44 -0
  113. data/lib/generators/rspec/templates/oauth_nonce_spec.rb +24 -0
  114. data/lib/generators/rspec/templates/oauth_nonces.yml +13 -0
  115. data/lib/generators/rspec/templates/oauth_token_spec.rb +309 -0
  116. data/lib/generators/rspec/templates/oauth_tokens.yml +17 -0
  117. data/lib/generators/test_unit/oauth_provider_generator.rb +33 -0
  118. data/lib/generators/test_unit/templates/client_application_test.rb +42 -0
  119. data/lib/generators/test_unit/templates/client_applications.yml +23 -0
  120. data/lib/generators/test_unit/templates/clients_controller_test.rb +280 -0
  121. data/lib/generators/test_unit/templates/oauth_nonce_test.rb +26 -0
  122. data/lib/generators/test_unit/templates/oauth_nonces.yml +13 -0
  123. data/lib/generators/test_unit/templates/oauth_token_test.rb +57 -0
  124. data/lib/generators/test_unit/templates/oauth_tokens.yml +17 -0
  125. data/lib/oauth-provider.rb +24 -0
  126. data/lib/oauth-provider/version.rb +5 -0
  127. data/lib/oauth/controllers/application_controller_methods.rb +136 -0
  128. data/lib/oauth/controllers/provider_controller.rb +181 -0
  129. data/lib/oauth/provider/authorizer.rb +83 -0
  130. data/lib/oauth/rack/oauth_filter.rb +93 -0
  131. data/oauth-provider.gemspec +37 -0
  132. data/rails/init.rb +1 -0
  133. data/spec/dummy_provider_models.rb +53 -0
  134. data/spec/oauth/provider/authorizer_spec.rb +202 -0
  135. data/spec/rack/oauth_filter_spec.rb +244 -0
  136. data/spec/spec_helper.rb +3 -0
  137. data/tasks/oauth_tasks.rake +4 -0
  138. data/uninstall.rb +1 -0
  139. metadata +286 -0
@@ -0,0 +1,20 @@
1
+ class Oauth2Token < AccessToken
2
+ attr_accessor :state
3
+ def as_json(options={})
4
+ d = {:access_token=>token, :token_type => 'bearer'}
5
+ d[:expires_in] = expires_in if expires_at
6
+ d
7
+ end
8
+
9
+ def to_query
10
+ q = "access_token=#{token}&token_type=bearer"
11
+ q << "&state=#{URI.escape(state)}" if @state
12
+ q << "&expires_in=#{expires_in}" if expires_at
13
+ q << "&scope=#{URI.escape(scope)}" if scope
14
+ q
15
+ end
16
+
17
+ def expires_in
18
+ expires_at.to_i - Time.now.to_i
19
+ end
20
+ end
@@ -0,0 +1,35 @@
1
+ class Oauth2Verifier < OauthToken
2
+ validates_presence_of :user
3
+ attr_accessor :state
4
+
5
+ def exchange!(params={})
6
+ OauthToken.transaction do
7
+ token = Oauth2Token.create! :user=>user,:client_application=>client_application, :scope => scope
8
+ invalidate!
9
+ token
10
+ end
11
+ end
12
+
13
+ def code
14
+ token
15
+ end
16
+
17
+ def redirect_url
18
+ callback_url
19
+ end
20
+
21
+ def to_query
22
+ q = "code=#{token}"
23
+ q << "&state=#{URI.escape(state)}" if @state
24
+ q
25
+ end
26
+
27
+ protected
28
+
29
+ def generate_keys
30
+ self.token = OAuth::Helper.generate_key(20)[0,20]
31
+ self.expires_at = 10.minutes.from_now
32
+ self.authorized_at = Time.now
33
+ end
34
+
35
+ end
@@ -0,0 +1,24 @@
1
+ # Simple store of nonces. The OAuth Spec requires that any given pair of nonce and timestamps are unique.
2
+ # Thus you can use the same nonce with a different timestamp and viceversa.
3
+ class OauthNonce
4
+ include Mongoid::Document
5
+ include Mongoid::Timestamps
6
+
7
+ field :nonce, :type => String
8
+ field :timestamp, :type => Integer
9
+
10
+ index [
11
+ [:nonce, Mongo::ASCENDING],
12
+ [:timestamp, Mongo::ASCENDING]
13
+ ], :unique => true
14
+
15
+ validates_presence_of :nonce, :timestamp
16
+ validates_uniqueness_of :nonce, :scope => :timestamp
17
+
18
+ # Remembers a nonce and it's associated timestamp. It returns false if it has already been used
19
+ def self.remember(nonce, timestamp)
20
+ oauth_nonce = OauthNonce.create(:nonce => nonce, :timestamp => timestamp)
21
+ return false if oauth_nonce.new_record?
22
+ oauth_nonce
23
+ end
24
+ end
@@ -0,0 +1,44 @@
1
+ class OauthToken
2
+ include Mongoid::Document
3
+ include Mongoid::Timestamps
4
+
5
+ field :token, :type => String
6
+ field :secret, :type => String
7
+ field :callback_url, :type => String
8
+ field :verifier, :type => String
9
+ field :scope, :type => String
10
+ field :authorized_at, :type => Time
11
+ field :invalidated_at, :type => Time
12
+ field :expires_at, :type => Time
13
+
14
+ index :token, :unique => true
15
+
16
+ referenced_in :user
17
+ referenced_in :client_application
18
+
19
+ validates_uniqueness_of :token
20
+ validates_presence_of :client_application, :token
21
+ before_validation :generate_keys, :on => :create
22
+
23
+ def invalidated?
24
+ !invalidated_at.nil?
25
+ end
26
+
27
+ def invalidate!
28
+ update_attribute(:invalidated_at, Time.now)
29
+ end
30
+
31
+ def authorized?
32
+ !authorized_at.nil? && !invalidated?
33
+ end
34
+
35
+ def to_query
36
+ "oauth_token=#{token}&oauth_token_secret=#{secret}"
37
+ end
38
+
39
+ protected
40
+ def generate_keys
41
+ self.token = OAuth::Helper.generate_key(40)[0,40]
42
+ self.secret = OAuth::Helper.generate_key(40)[0,40]
43
+ end
44
+ end
@@ -0,0 +1,36 @@
1
+ class RequestToken < OauthToken
2
+ attr_accessor :provided_oauth_verifier
3
+
4
+ def authorize!(user)
5
+ return false if authorized?
6
+ self.user = user
7
+ self.authorized_at = Time.now
8
+ self.verifier = OAuth::Helper.generate_key(20)[0,20] unless oauth10?
9
+ self.save
10
+ end
11
+
12
+ def exchange!
13
+ return false unless authorized?
14
+ return false unless oauth10? || verifier == provided_oauth_verifier
15
+
16
+ AccessToken.create(:user => user, :client_application => client_application).tap do
17
+ invalidate!
18
+ end
19
+ end
20
+
21
+ def to_query
22
+ if oauth10?
23
+ super
24
+ else
25
+ "#{super}&oauth_callback_confirmed=true"
26
+ end
27
+ end
28
+
29
+ def oob?
30
+ callback_url.nil? || callback_url.downcase == 'oob'
31
+ end
32
+
33
+ def oauth10?
34
+ (defined? OAUTH_10_SUPPORT) && OAUTH_10_SUPPORT && self.callback_url.blank?
35
+ end
36
+ end
@@ -0,0 +1,6 @@
1
+ require 'active_support'
2
+ require 'active_support/inflector'
3
+
4
+ ActiveSupport::Inflector.inflections do |inflect|
5
+ inflect.uncountable %w(oauth)
6
+ end
@@ -0,0 +1,18 @@
1
+ This creates an OAuth Provider controller as well as the requisite models.
2
+
3
+ It requires an authentication framework such as acts_as_authenticated, restful_authentication or restful_open_id_authentication.
4
+
5
+ If you generated the migration file (true by default), make sure you run
6
+
7
+ rake db:migrate
8
+
9
+ include the following in your user.rb
10
+
11
+ has_many :client_applications
12
+ has_many :tokens, :class_name=>"OauthToken",:order=>"authorized_at desc",:include=>[:client_application]
13
+
14
+ For legacy OAUTH 1.0 support add the following constant in your environment.rb
15
+
16
+ OAUTH_10_SUPPORT = true
17
+
18
+ Note, you should only do this if you really positively require to support old OAuth1.0 clients. There is a serious security issue with this.
@@ -0,0 +1,37 @@
1
+ require 'generators/oauth_inflections'
2
+
3
+ class OauthProviderGenerator < Rails::Generators::NamedBase
4
+ source_root File.expand_path("../templates", __FILE__)
5
+
6
+ argument :name, :type => :string, :default => 'Oauth'
7
+
8
+ desc 'This creates an OAuth Provider controller as well as the requisite models.'
9
+
10
+ hook_for :orm
11
+
12
+ def check_class_collisions
13
+ # Check for class naming collisions.
14
+ class_collisions class_path, "#{class_name}Controller", # Oauth Controller
15
+ "#{class_name}Helper",
16
+ "#{class_name}ClientsController",
17
+ "#{class_name}ClientsHelper"
18
+ end
19
+
20
+ def copy_controller_files
21
+ template 'controller.rb', File.join('app/controllers', class_path, "#{file_name}_controller.rb")
22
+ template 'clients_controller.rb', File.join('app/controllers', class_path, "#{file_name}_clients_controller.rb")
23
+ end
24
+
25
+ hook_for :test_framework, :template_engine
26
+
27
+ def add_routes
28
+ route "match '/oauth', :to => 'oauth#index', :as => :oauth"
29
+ route "match '/oauth/authorize', :to => 'oauth#authorize', :as => :authorize"
30
+ route "match '/oauth/request_token', :to => 'oauth#request_token', :as => :request_token"
31
+ route "match '/oauth/access_token', :to => 'oauth#access_token', :as => :access_token"
32
+ route "match '/oauth/token', :to => 'oauth#token', :as => :token"
33
+ route "match '/oauth/test_request', :to => 'oauth#test_request', :as => :test_request"
34
+
35
+ route "resources :#{file_name}_clients"
36
+ end
37
+ end
@@ -0,0 +1,52 @@
1
+ class OauthClientsController < ApplicationController
2
+ before_filter :login_required
3
+ before_filter :get_client_application, :only => [:show, :edit, :update, :destroy]
4
+
5
+ def index
6
+ @client_applications = current_user.client_applications
7
+ @tokens = current_user.tokens.find :all, :conditions => 'oauth_tokens.invalidated_at is null and oauth_tokens.authorized_at is not null'
8
+ end
9
+
10
+ def new
11
+ @client_application = ClientApplication.new
12
+ end
13
+
14
+ def create
15
+ @client_application = current_user.client_applications.build(params[:client_application])
16
+ if @client_application.save
17
+ flash[:notice] = "Registered the information successfully"
18
+ redirect_to :action => "show", :id => @client_application.id
19
+ else
20
+ render :action => "new"
21
+ end
22
+ end
23
+
24
+ def show
25
+ end
26
+
27
+ def edit
28
+ end
29
+
30
+ def update
31
+ if @client_application.update_attributes(params[:client_application])
32
+ flash[:notice] = "Updated the client information successfully"
33
+ redirect_to :action => "show", :id => @client_application.id
34
+ else
35
+ render :action => "edit"
36
+ end
37
+ end
38
+
39
+ def destroy
40
+ @client_application.destroy
41
+ flash[:notice] = "Destroyed the client application registration"
42
+ redirect_to :action => "index"
43
+ end
44
+
45
+ private
46
+ def get_client_application
47
+ unless @client_application = current_user.client_applications.find(params[:id])
48
+ flash.now[:error] = "Wrong application id"
49
+ raise ActiveRecord::RecordNotFound
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,23 @@
1
+ require 'oauth/controllers/provider_controller'
2
+ class OauthController < ApplicationController
3
+ include OAuth::Controllers::ProviderController
4
+
5
+ protected
6
+ # Override this to match your authorization page form
7
+ # It currently expects a checkbox called authorize
8
+ # def user_authorizes_token?
9
+ # params[:authorize] == '1'
10
+ # end
11
+
12
+ # should authenticate and return a user if valid password.
13
+ # This example should work with most Authlogic or Devise. Uncomment it
14
+ # def authenticate_user(username,password)
15
+ # user = User.find_by_email params[:username]
16
+ # if user && user.valid_password?(params[:password])
17
+ # user
18
+ # else
19
+ # nil
20
+ # end
21
+ # end
22
+
23
+ end
@@ -0,0 +1,35 @@
1
+ require 'generators/rspec'
2
+
3
+ module Rspec
4
+ module Generators
5
+ class OauthProviderGenerator < Rails::Generators::NamedBase
6
+ source_root File.expand_path('../templates', __FILE__)
7
+
8
+ argument :name, :type => :string, :default => 'Oauth'
9
+ class_option :fixture, :type => :boolean
10
+
11
+ def copy_controller_spec_files
12
+ template 'clients_controller_spec.rb',
13
+ File.join('spec/controllers', class_path, "#{file_name}_clients_controller_spec.rb")
14
+ end
15
+
16
+ def copy_models_spec_files
17
+ template 'client_application_spec.rb', File.join('spec/models', 'client_application_spec.rb')
18
+ template 'oauth_token_spec.rb', File.join('spec/models', 'oauth_token_spec.rb')
19
+ template 'oauth2_token_spec.rb', File.join('spec/models', 'oauth2_token_spec.rb')
20
+ template 'oauth2_verifier_spec.rb', File.join('spec/models', 'oauth2_verifier_spec.rb')
21
+ template 'oauth_nonce_spec.rb', File.join('spec/models', 'oauth_nonce_spec.rb')
22
+ end
23
+
24
+ hook_for :fixture_replacement
25
+
26
+ def create_fixture_file
27
+ if options[:fixtures] && options[:fixture_replacement].nil?
28
+ template 'client_applications.yml', File.join('test/fixtures', 'client_applications.yml')
29
+ template 'oauth_tokens.yml', File.join('test/fixtures', 'oauth_tokens.yml')
30
+ template 'oauth_nonces.yml', File.join('test/fixtures', 'oauth_nonces.yml')
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,29 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+ describe ClientApplication do
3
+ fixtures :users, :client_applications, :oauth_tokens
4
+ before(:each) do
5
+ @application = ClientApplication.create :name => "Agree2", :url => "http://agree2.com", :user => users(:quentin)
6
+ end
7
+
8
+ it "should be valid" do
9
+ @application.should be_valid
10
+ end
11
+
12
+
13
+ it "should not have errors" do
14
+ @application.errors.full_messages.should == []
15
+ end
16
+
17
+ it "should have key and secret" do
18
+ @application.key.should_not be_nil
19
+ @application.secret.should_not be_nil
20
+ end
21
+
22
+ it "should have credentials" do
23
+ @application.credentials.should_not be_nil
24
+ @application.credentials.key.should == @application.key
25
+ @application.credentials.secret.should == @application.secret
26
+ end
27
+
28
+ end
29
+
@@ -0,0 +1,23 @@
1
+ # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
2
+ one:
3
+ id: 1
4
+ name: MyString
5
+ url: http://test.com
6
+ support_url: http://test.com/support
7
+ callback_url: http://test.com/callback
8
+ key: one_key
9
+ secret: MyString
10
+ user_id: 1
11
+ created_at: 2007-11-17 16:56:51
12
+ updated_at: 2007-11-17 16:56:51
13
+ two:
14
+ id: 2
15
+ name: MyString
16
+ url: http://test.com
17
+ support_url: http://test.com/support
18
+ callback_url: http://test.com/callback
19
+ key: two_key
20
+ secret: MyString
21
+ user_id: 1
22
+ created_at: 2007-11-17 16:56:51
23
+ updated_at: 2007-11-17 16:56:51
@@ -0,0 +1,176 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+ require File.dirname(__FILE__) + '/oauth_controller_spec_helper'
3
+ require 'oauth/client/action_controller_request'
4
+
5
+ describe OauthClientsController do
6
+ if defined?(Devise)
7
+ include Devise::TestHelpers
8
+ end
9
+ include OAuthControllerSpecHelper
10
+ fixtures :client_applications, :oauth_tokens, :users
11
+ before(:each) do
12
+ login_as_application_owner
13
+ end
14
+
15
+ describe "index" do
16
+ before do
17
+ @client_applications = @user.client_applications
18
+ end
19
+
20
+ def do_get
21
+ get :index
22
+ end
23
+
24
+ it "should be successful" do
25
+ do_get
26
+ response.should be_success
27
+ end
28
+
29
+ it "should assign client_applications" do
30
+ do_get
31
+ assigns[:client_applications].should==@client_applications
32
+ end
33
+
34
+ it "should render index template" do
35
+ do_get
36
+ response.should render_template('index')
37
+ end
38
+ end
39
+
40
+ describe "show" do
41
+
42
+ def do_get
43
+ get :show, :id => '1'
44
+ end
45
+
46
+ it "should be successful" do
47
+ do_get
48
+ response.should be_success
49
+ end
50
+
51
+ it "should assign client_applications" do
52
+ do_get
53
+ assigns[:client_application].should==current_client_application
54
+ end
55
+
56
+ it "should render show template" do
57
+ do_get
58
+ response.should render_template('show')
59
+ end
60
+
61
+ end
62
+
63
+ describe "new" do
64
+
65
+ def do_get
66
+ get :new
67
+ end
68
+
69
+ it "should be successful" do
70
+ do_get
71
+ response.should be_success
72
+ end
73
+
74
+ it "should assign client_applications" do
75
+ do_get
76
+ assigns[:client_application].class.should==ClientApplication
77
+ end
78
+
79
+ it "should render show template" do
80
+ do_get
81
+ response.should render_template('new')
82
+ end
83
+
84
+ end
85
+
86
+ describe "edit" do
87
+ def do_get
88
+ get :edit, :id => '1'
89
+ end
90
+
91
+ it "should be successful" do
92
+ do_get
93
+ response.should be_success
94
+ end
95
+
96
+ it "should assign client_applications" do
97
+ do_get
98
+ assigns[:client_application].should==current_client_application
99
+ end
100
+
101
+ it "should render edit template" do
102
+ do_get
103
+ response.should render_template('edit')
104
+ end
105
+
106
+ end
107
+
108
+ describe "create" do
109
+
110
+ def do_valid_post
111
+ post :create, 'client_application'=>{'name' => 'my site', :url=>"http://test.com"}
112
+ @client_application = ClientApplication.last
113
+ end
114
+
115
+ def do_invalid_post
116
+ post :create
117
+ end
118
+
119
+ it "should redirect to new client_application" do
120
+ do_valid_post
121
+ response.should be_redirect
122
+ response.should redirect_to(:action => "show", :id => @client_application.id)
123
+ end
124
+
125
+ it "should render show template" do
126
+ do_invalid_post
127
+ response.should render_template('new')
128
+ end
129
+ end
130
+
131
+ describe "destroy" do
132
+
133
+ def do_delete
134
+ delete :destroy, :id => '1'
135
+ end
136
+
137
+ it "should destroy client applications" do
138
+ do_delete
139
+ ClientApplication.should_not be_exists(1)
140
+ end
141
+
142
+ it "should redirect to list" do
143
+ do_delete
144
+ response.should be_redirect
145
+ response.should redirect_to(:action => 'index')
146
+ end
147
+
148
+ end
149
+
150
+ describe "update" do
151
+
152
+ def do_valid_update
153
+ put :update, :id => '1', 'client_application'=>{'name' => 'updated site'}
154
+ end
155
+
156
+ def do_invalid_update
157
+ put :update, :id => '1', 'client_application'=>{'name' => nil}
158
+ end
159
+
160
+ it "should redirect to show client_application" do
161
+ do_valid_update
162
+ response.should be_redirect
163
+ response.should redirect_to(:action => "show", :id => 1)
164
+ end
165
+
166
+ it "should assign client_applications" do
167
+ do_invalid_update
168
+ assigns[:client_application].should == ClientApplication.find(1)
169
+ end
170
+
171
+ it "should render show template" do
172
+ do_invalid_update
173
+ response.should render_template('edit')
174
+ end
175
+ end
176
+ end