social_stream-oauth2_server 2.0.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (69) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +10 -0
  3. data/Gemfile +5 -0
  4. data/README.rdoc +3 -0
  5. data/Rakefile +26 -0
  6. data/app/assets/images/logos/small/site.png +0 -0
  7. data/app/assets/javascripts/social_stream-oauth2_server.js +6 -0
  8. data/app/assets/javascripts/social_stream/site_client.js +15 -0
  9. data/app/assets/stylesheets/social_stream-oauth2_server.css.sass +24 -0
  10. data/app/assets/stylesheets/social_stream/oauth2_server/applications/_applications-oauth2server.css.sass +45 -0
  11. data/app/assets/stylesheets/social_stream/oauth2_server/applications/layout/_applications-oauth2server.css.sass +51 -0
  12. data/app/assets/stylesheets/social_stream/oauth2_server/applications/responsive/_responsive-1200px-min.css.sass +7 -0
  13. data/app/assets/stylesheets/social_stream/oauth2_server/applications/responsive/_responsive-767px-max.css.sass +35 -0
  14. data/app/assets/stylesheets/social_stream/oauth2_server/applications/responsive/_responsive-768px-979px.css.sass +8 -0
  15. data/app/assets/stylesheets/social_stream/oauth2_server/create/_create-oauth2server.css.sass +11 -0
  16. data/app/assets/stylesheets/social_stream/oauth2_server/create/_error_create-oauth2server.css.sass +13 -0
  17. data/app/assets/stylesheets/social_stream/oauth2_server/create/layout/_create-oauth2server.css.sass +19 -0
  18. data/app/assets/stylesheets/social_stream/oauth2_server/create/responsive/_responsive-1200px-min.css.sass +7 -0
  19. data/app/assets/stylesheets/social_stream/oauth2_server/create/responsive/_responsive-767px-max.css.sass +16 -0
  20. data/app/assets/stylesheets/social_stream/oauth2_server/create/responsive/_responsive-768px-979px.css.sass +8 -0
  21. data/app/assets/stylesheets/social_stream/oauth2_server/show/_show-oauth2server.css.sass +42 -0
  22. data/app/assets/stylesheets/social_stream/oauth2_server/show/layout/_show-oauth2server.css.sass +71 -0
  23. data/app/assets/stylesheets/social_stream/oauth2_server/show/responsive/_responsive-1200px-min.css.sass +7 -0
  24. data/app/assets/stylesheets/social_stream/oauth2_server/show/responsive/_responsive-767px-max.css.sass +16 -0
  25. data/app/assets/stylesheets/social_stream/oauth2_server/show/responsive/_responsive-768px-979px.css.sass +8 -0
  26. data/app/controllers/authorizations_controller.rb +67 -0
  27. data/app/controllers/site/clients_controller.rb +39 -0
  28. data/app/decorators/social_stream/base/actor_decorator.rb +3 -0
  29. data/app/decorators/social_stream/base/user_decorator.rb +3 -0
  30. data/app/helpers/site_client_helper.rb +17 -0
  31. data/app/models/oauth2_token.rb +33 -0
  32. data/app/models/oauth2_token/access_token.rb +32 -0
  33. data/app/models/oauth2_token/authorization_code.rb +5 -0
  34. data/app/models/oauth2_token/refresh_token.rb +7 -0
  35. data/app/models/relation/admin.rb +9 -0
  36. data/app/models/relation/auth.rb +8 -0
  37. data/app/models/site/client.rb +38 -0
  38. data/app/overrides/frontpage/_presentation/client_site_presentation.html.erb.deface +8 -0
  39. data/app/overrides/layouts/_header_dropdown_menu/applications_entry.html.erb.deface +4 -0
  40. data/app/views/authorizations/error.html.erb +4 -0
  41. data/app/views/authorizations/new.html.erb +27 -0
  42. data/app/views/site/clients/_form.html.erb +39 -0
  43. data/app/views/site/clients/_new.modal.html.erb +9 -0
  44. data/app/views/site/clients/_presentation.html.erb +13 -0
  45. data/app/views/site/clients/index.html.erb +42 -0
  46. data/app/views/site/clients/new.html.erb +10 -0
  47. data/app/views/site/clients/show.html.erb +44 -0
  48. data/config/locales/en.yml +29 -0
  49. data/config/locales/es.yml +29 -0
  50. data/config/routes.rb +10 -0
  51. data/db/migrate/20130115102300_create_social_stream_oauth2_server.rb +25 -0
  52. data/lib/generators/social_stream/oauth2_server/install_generator.rb +23 -0
  53. data/lib/social_stream-oauth2_server.rb +19 -0
  54. data/lib/social_stream/migrations/oauth2_server.rb +8 -0
  55. data/lib/social_stream/oauth2_server.rb +5 -0
  56. data/lib/social_stream/oauth2_server/controllers/helpers.rb +41 -0
  57. data/lib/social_stream/oauth2_server/engine.rb +21 -0
  58. data/lib/social_stream/oauth2_server/models/actor.rb +11 -0
  59. data/lib/social_stream/oauth2_server/models/user.rb +35 -0
  60. data/lib/social_stream/oauth2_server/token_endpoint.rb +41 -0
  61. data/lib/social_stream/oauth2_server/version.rb +5 -0
  62. data/lib/tasks/db/populate.rake +37 -0
  63. data/social_stream-oauth2_server.gemspec +19 -0
  64. data/spec/controllers/authorizations_controller_spec.rb +179 -0
  65. data/spec/controllers/dummy_controller_spec.rb +43 -0
  66. data/spec/factories/site_client.rb +6 -0
  67. data/spec/models/relation/auth_spec.rb +15 -0
  68. data/spec/models/site/client_spec.rb +22 -0
  69. metadata +156 -0
@@ -0,0 +1,10 @@
1
+ <section id="new_site_client">
2
+ <%= render partial: 'toolbar/home' %>
3
+
4
+ <section class="new_site_client">
5
+ <h1><%= t 'site.client.new.title' %></h1>
6
+
7
+ <%= render partial: 'form' %>
8
+ </section>
9
+ </section>
10
+
@@ -0,0 +1,44 @@
1
+ <%= render partial: 'toolbar/home' %>
2
+
3
+ <section id="oauth2_server">
4
+ <h1>
5
+ <%= @client.name %>
6
+ </h1>
7
+
8
+ <hr class="soften">
9
+ <div class="content">
10
+ <a class="pull-left" href="#">
11
+ <img class="media-object" alt="application image" style="width: 100px; height: 100px;" src="assets/user.png">
12
+ </a>
13
+ <article class="media box">
14
+ <div class="media-body">
15
+ <h4>
16
+ <%= Site::Client.human_attribute_name :url %>
17
+ </h4>
18
+ <div class="result">
19
+ <%= link_to @client.url, @client.url%>
20
+ </div>
21
+ <h4>
22
+ <%= Site::Client.human_attribute_name :callback_url %>
23
+ </h4>
24
+ <div class="result">
25
+ <%= @client.callback_url %>
26
+ </div>
27
+ <h4>
28
+ <%= Site::Client.human_attribute_name :id %>
29
+ </h4>
30
+ <div class="result">
31
+ <%= @client.id %>
32
+ </div>
33
+ <h4>
34
+ <%= Site::Client.human_attribute_name :secret %>
35
+ </h4>
36
+ <div class="result client_secret">
37
+ <p>
38
+ <%= @client.secret %>
39
+ </p>
40
+ </div>
41
+ </div>
42
+ </article>
43
+ </div>
44
+ </section>
@@ -0,0 +1,29 @@
1
+ en:
2
+ account:
3
+ applications: "Applications"
4
+ activerecord:
5
+ attributes:
6
+ site/client:
7
+ callback_url: "Callback URL"
8
+ id: "Client ID"
9
+ name: "Name"
10
+ secret: "Client Secret"
11
+ url: "URL"
12
+ authorization:
13
+ form:
14
+ accept: "Accept"
15
+ cancel: "Cancel"
16
+ new:
17
+ title: "Authorize %{client}?"
18
+ permission:
19
+ title: "The site %{client} will be able to:"
20
+ permission:
21
+ public_info: "Read your public information"
22
+ site:
23
+ client:
24
+ added: "Added sites"
25
+ index:
26
+ title: "Client sites"
27
+ new:
28
+ link: "Add site"
29
+ title: "Register new client site"
@@ -0,0 +1,29 @@
1
+ es:
2
+ account:
3
+ applications: "Aplicaciones"
4
+ activerecord:
5
+ attributes:
6
+ site/client:
7
+ callback_url: "Callback URL"
8
+ id: "Client ID"
9
+ name: "Nombre"
10
+ secret: "Client Secret"
11
+ url: "URL"
12
+ authorizations:
13
+ form:
14
+ accept: "Aceptar"
15
+ cancel: "Cancelar"
16
+ new:
17
+ title: "¿Autorizar %{client}?"
18
+ permission:
19
+ title: "El sitio %{client} será capaz de:"
20
+ permission:
21
+ public_info: "Acceder a tu información pública"
22
+ site:
23
+ client:
24
+ added: "Sitios añadidos"
25
+ index:
26
+ title: "Sitios"
27
+ new:
28
+ link: "Añadir sitio"
29
+ title: "Registrar nuevo sitio"
@@ -0,0 +1,10 @@
1
+ Rails.application.routes.draw do
2
+ match 'oauth2/authorize', to: 'authorizations#new'
3
+ post 'oauth2/token', to: proc { |env| SocialStream::Oauth2Server::TokenEndpoint.new.call(env) }
4
+
5
+ resources :authorizations, only: :create
6
+
7
+ namespace "site" do
8
+ resources :clients
9
+ end
10
+ end
@@ -0,0 +1,25 @@
1
+ class CreateSocialStreamOauth2Server < ActiveRecord::Migration
2
+ def change
3
+ create_table :oauth2_tokens do |t|
4
+ t.string :type
5
+
6
+ t.integer :user_id
7
+ t.integer :site_id
8
+ t.string :token
9
+
10
+ t.string :redirect_uri
11
+ t.integer :refresh_token_id
12
+
13
+ t.timestamps
14
+ t.datetime :expires_at
15
+ end
16
+
17
+ add_index "oauth2_tokens", :user_id, :name => "index_oauth2_tokens_on_user_id"
18
+ add_index "oauth2_tokens", :site_id, :name => "index_oauth2_tokens_on_site_id"
19
+ add_index "oauth2_tokens", :token, :name => "index_oauth2_tokens_on_token"
20
+ add_index "oauth2_tokens", :refresh_token_id, :name => "index_oauth2_tokens_on_refresh_token_id"
21
+
22
+ add_foreign_key "oauth2_tokens", "sites", :name => "index_oauth2_tokens_on_site_id"
23
+ add_foreign_key "oauth2_tokens", "users", :name => "index_oauth2_tokens_on_user_id"
24
+ end
25
+ end
@@ -0,0 +1,23 @@
1
+ class SocialStream::Oauth2Server::InstallGenerator < Rails::Generators::Base
2
+ include Rails::Generators::Migration
3
+
4
+ source_root File.expand_path('../templates', __FILE__)
5
+
6
+ def create_migration_file
7
+ require 'rake'
8
+ Rails.application.load_tasks
9
+ Rake::Task['railties:install:migrations'].reenable
10
+ Rake::Task['social_stream_oauth2_server_engine:install:migrations'].invoke
11
+ end
12
+
13
+ def require_javascripts
14
+ inject_into_file 'app/assets/javascripts/application.js',
15
+ "//= require social_stream-oauth2_server\n",
16
+ :before => '//= require_tree .'
17
+ end
18
+
19
+ def require_stylesheets
20
+ append_file 'app/assets/stylesheets/social_stream.css.sass',
21
+ "@import social_stream-oauth2_server\n"
22
+ end
23
+ end
@@ -0,0 +1,19 @@
1
+ require 'social_stream-base'
2
+
3
+ require 'rack/oauth2'
4
+
5
+ module SocialStream
6
+ module Oauth2Server
7
+ module Controllers
8
+ autoload :Helpers, 'social_stream/oauth2_server/controllers/helpers'
9
+ end
10
+
11
+ module Models
12
+ autoload :Actor, 'social_stream/oauth2_server/models/actor'
13
+ autoload :User, 'social_stream/oauth2_server/models/user'
14
+ end
15
+ autoload :TokenEndpoint, 'social_stream/oauth2_server/token_endpoint'
16
+ end
17
+ end
18
+
19
+ require 'social_stream/oauth2_server/engine'
@@ -0,0 +1,8 @@
1
+ require 'social_stream/migrations/components'
2
+
3
+ module SocialStream
4
+ module Migrations
5
+ class Oauth2Server < Components
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,5 @@
1
+ # Bundler 1.2 tries to load this file instead of the gem name,
2
+ # so we delegate to it
3
+ #
4
+ # We will probably change this in the future
5
+ require 'social_stream-oauth2_server'
@@ -0,0 +1,41 @@
1
+ module SocialStream
2
+ module Oauth2Server
3
+ module Controllers
4
+ # Common methods added to ApplicationController
5
+ module Helpers
6
+ extend ActiveSupport::Concern
7
+
8
+ def authenticate_user!(opts = {})
9
+ oauth2_token? || super
10
+ end
11
+
12
+ def current_subject
13
+ super ||
14
+ @current_subject ||=
15
+ current_from_oauth_token(:client)
16
+ end
17
+
18
+ def current_user
19
+ super ||
20
+ @current_user ||=
21
+ current_from_oauth_token(:user)
22
+ end
23
+
24
+ def current_from_oauth_token(type)
25
+ return unless oauth2_token?
26
+
27
+ oauth2_token.__send__(type)
28
+ end
29
+
30
+ def oauth2_token
31
+ @oauth2_token ||=
32
+ request.env[Rack::OAuth2::Server::Resource::ACCESS_TOKEN]
33
+ end
34
+
35
+ def oauth2_token?
36
+ oauth2_token.present?
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,21 @@
1
+ module SocialStream
2
+ module Oauth2Server
3
+ class Engine < Rails::Engine
4
+ config.app_middleware.use Rack::OAuth2::Server::Resource::Bearer, 'Social Stream OAuth2' do |req|
5
+ Oauth2Token::AccessToken.valid.find_by_token(req.access_token) || req.invalid_token!
6
+ end
7
+
8
+ initializer "social_stream-oauth2_server.controller.helpers",
9
+ after: "social_stream-base.controller.helpers" do
10
+ ActiveSupport.on_load(:action_controller) do
11
+ include SocialStream::Oauth2Server::Controllers::Helpers
12
+ end
13
+ end
14
+
15
+ initializer "social_stream-oauth2_server.add_filters" do |app|
16
+ app.config.filter_parameters += [:secret]
17
+ app.config.filter_parameters.uniq
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,11 @@
1
+ module SocialStream
2
+ module Oauth2Server
3
+ module Models
4
+ module Actor
5
+ def developer_site_clients
6
+ Site::Client.administered_by(self)
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,35 @@
1
+ module SocialStream
2
+ module Oauth2Server
3
+ module Models
4
+ module User
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ has_many :oauth2_tokens,
9
+ dependent: :destroy
10
+
11
+ has_many :authorization_codes,
12
+ class_name: 'Oauth2Token::AuthorizationCode'
13
+
14
+ has_many :access_tokens,
15
+ class_name: 'Oauth2Token::AccessToken'
16
+
17
+ has_many :refresh_tokens,
18
+ class_name: 'Oauth2Token::RefreshToken'
19
+ end
20
+
21
+ # Is {#client} authorized by this {User}
22
+ def client_authorized?(client)
23
+ contact_to!(client).relation_ids.include? Relation::Auth.instance.id
24
+ end
25
+
26
+ # Create a new tie to {Site::Client}
27
+ def client_authorize!(client)
28
+ unless contact_to!(client).relation_ids.include?(Relation::Auth.instance.id)
29
+ contact_to!(client).relation_ids += [ Relation::Auth.instance.id ]
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,41 @@
1
+ module SocialStream
2
+ module Oauth2Server
3
+ class TokenEndpoint
4
+ def call(env)
5
+ authenticator.call(env)
6
+ end
7
+
8
+ private
9
+
10
+ def authenticator
11
+ Rack::OAuth2::Server::Token.new do |req, res|
12
+ client = Site::Client.find(req.client_id) || req.invalid_client!
13
+ client.secret == req.client_secret || req.invalid_client!
14
+
15
+ case req.grant_type
16
+ when :authorization_code
17
+ code = Oauth2Token::AuthorizationCode.valid.find_by_token(req.code)
18
+ req.invalid_grant! if code.blank? || code.redirect_uri != req.redirect_uri
19
+
20
+ res.access_token = code.access_token.to_bearer_token(:with_refresh_token)
21
+ when :password
22
+ # TODO
23
+ account = Account.find_by_username_and_password(req.username, req.password) || req.invalid_grant!
24
+ res.access_token = account.access_tokens.create(:client => client).to_bearer_token(:with_refresh_token)
25
+ when :client_credentials
26
+ # NOTE: client is already authenticated here.
27
+ res.access_token = client.access_tokens.create!.to_bearer_token
28
+ when :refresh_token
29
+ refresh_token = client.refresh_tokens.valid.find_by_token(req.refresh_token)
30
+ req.invalid_grant! unless refresh_token
31
+ res.access_token = refresh_token.access_tokens.create!.to_bearer_token
32
+ else
33
+ # NOTE: extended assertion grant_types are not supported yet.
34
+ req.unsupported_grant_type!
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+
@@ -0,0 +1,5 @@
1
+ module SocialStream
2
+ module Oauth2Server
3
+ VERSION = "2.0.0.beta1".freeze
4
+ end
5
+ end
@@ -0,0 +1,37 @@
1
+ namespace :db do
2
+ namespace :populate do
3
+ desc "Create populate data with client sites"
4
+ task create: 'create:site_clients'
5
+
6
+ namespace :create do
7
+ desc "Add client sites populate data"
8
+ task site_clients: :read_environment do
9
+ puts 'Site Client population (Dummy and 9 clients more)'
10
+
11
+ start = Time.now
12
+
13
+ # Create dummy site
14
+ aid = Actor.find_by_slug('demo').id
15
+
16
+ s = Site::Client.create! name: 'Dummy',
17
+ description: "Social Stream's spec/dummy application",
18
+ url: 'http://localhost:3000',
19
+ callback_url: 'http://localhost:3000/users/auth/socialstream/callback',
20
+ author_id: aid
21
+
22
+ s.update_attributes! secret: "f9974ce87c455544f61cc960b58cf833eb039875ef27029449408857879a1e87283c86558e46fa431d37a3c5590ba92612c51dfd0872ccff35cbecf3910eaa02"
23
+
24
+ 9.times do
25
+ domain = Forgery::Internet.domain_name
26
+ Site::Client.create! name: Forgery::Name.company_name,
27
+ description: Forgery::LoremIpsum.sentence(random: true),
28
+ url: "https://#{ domain }",
29
+ callback_url: "https://#{ domain }/callback",
30
+ author: User.all[rand(User.all.size)]
31
+ end
32
+
33
+ puts " -> #{ (Time.now - start).round(4) }s"
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,19 @@
1
+ # encoding: UTF-8
2
+ require File.join(File.dirname(__FILE__), 'lib', 'social_stream', 'oauth2_server', 'version')
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "social_stream-oauth2_server"
6
+ s.version = SocialStream::Oauth2Server::VERSION.dup
7
+ s.authors = ["Antonio Tapiador", "GING - DIT - UPM"]
8
+ s.summary = "OAuth2 server support for Social Stream, the framework for building social network websites"
9
+ s.description = "Social Stream is a Ruby on Rails engine providing your application with social networking features and activity streams.\n\nThis gem supplies with OAuth2 server support"
10
+ s.email = "social-stream@dit.upm.es"
11
+ s.homepage = "http://github.com/ging/social_stream-oauth2_server"
12
+ s.files = `git ls-files`.split("\n")
13
+
14
+ # Gem dependencies
15
+ s.add_runtime_dependency('social_stream-base', '~> 2.0.0.beta1')
16
+ s.add_runtime_dependency('rack-oauth2', '~> 1.0.0')
17
+
18
+ s.add_development_dependency('rspec-rails', '~> 2.8.0')
19
+ end
@@ -0,0 +1,179 @@
1
+ require 'spec_helper'
2
+
3
+ describe AuthorizationsController do
4
+ let(:redirect_uri) { "https://test.host/callback" }
5
+ let(:token) { "token" }
6
+
7
+ let(:authorization_code) { double :authorization_code, token: token }
8
+ let(:user) { double :user, language: :en }
9
+ let(:client) { double :client, callback_url: redirect_uri }
10
+
11
+ context "#new" do
12
+ context "without authentication" do
13
+ it "should redirect to login" do
14
+ get :new
15
+
16
+ response.should redirect_to(:new_user_session)
17
+ end
18
+ end
19
+
20
+ context "with authentication" do
21
+ before do
22
+ controller.stub(:authenticate_user!)
23
+ controller.stub(:current_user) { user }
24
+ end
25
+
26
+ context "without client_id" do
27
+ it "should respond with bad request" do
28
+ get :new
29
+
30
+ response.should be_bad_request
31
+ assigns(:error).to_s.should eq('bad_request')
32
+ end
33
+ end
34
+
35
+ context "with client_id" do
36
+ before :each do
37
+ @params = { client_id: 7 }
38
+ end
39
+
40
+ it "should return bad request" do
41
+ get :new, @params
42
+
43
+ response.should be_bad_request
44
+ assigns(:error).to_s.should eq("invalid_request :: 'response_type' required.")
45
+ end
46
+
47
+ context "with response code" do
48
+ before :each do
49
+ @params.merge! response_type: 'code'
50
+ end
51
+
52
+ it "should raise RecordNotFound" do
53
+ lambda { get :new, @params }.should raise_error(ActiveRecord::RecordNotFound)
54
+ end
55
+
56
+ context "with valid client" do
57
+ before do
58
+ Site::Client.should_receive(:find).with("7") { client }
59
+ end
60
+
61
+ context "not authorized" do
62
+ before do
63
+ user.stub(:client_authorized?) { false }
64
+ end
65
+
66
+ it "should render new" do
67
+ get :new, @params
68
+
69
+ response.should be_success
70
+ response.should render_template('new')
71
+ end
72
+ end
73
+
74
+ context "authorized" do
75
+ before do
76
+ user.stub(:client_authorized?) { true }
77
+
78
+ codes = double(:codes)
79
+
80
+ user.stub(:authorization_codes) { codes }
81
+
82
+ codes.should_receive(:create!).with(client: client, redirect_uri: redirect_uri) { authorization_code }
83
+ end
84
+
85
+ it "should render new" do
86
+ get :new, @params
87
+
88
+ response.should redirect_to("#{ redirect_uri }?code=#{ token }")
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
96
+
97
+ describe "#create" do
98
+ context "without authentication" do
99
+ it "should redirect to login" do
100
+ post :create
101
+
102
+ response.should redirect_to(:new_user_session)
103
+ end
104
+ end
105
+
106
+ describe "with authentication" do
107
+ before do
108
+ controller.stub(:authenticate_user!)
109
+ controller.stub(:current_user) { user }
110
+ end
111
+
112
+ context "without client_id" do
113
+ it "should respond with bad request" do
114
+ post :create
115
+
116
+ response.should be_bad_request
117
+ assigns(:error).to_s.should eq('bad_request')
118
+ end
119
+ end
120
+
121
+ context "with client_id" do
122
+ before :each do
123
+ @params = { client_id: 7 }
124
+ end
125
+
126
+ it "should return bad request" do
127
+ post :create, @params
128
+
129
+ response.should be_bad_request
130
+ assigns(:error).to_s.should eq("invalid_request :: 'response_type' required.")
131
+ end
132
+
133
+ context "with response code" do
134
+ before :each do
135
+ @params.merge! response_type: 'code'
136
+ end
137
+
138
+ it "should raise RecordNotFound" do
139
+ lambda { post :create, @params }.should raise_error(ActiveRecord::RecordNotFound)
140
+ end
141
+
142
+ context "with valid client" do
143
+ before do
144
+ Site::Client.should_receive(:find).with("7") { client }
145
+ end
146
+
147
+ context "not accepted" do
148
+ it "should redirect" do
149
+ post :create, @params
150
+
151
+ response.should redirect_to("#{ redirect_uri }?error=access_denied&error_description=The+end-user+or+authorization+server+denied+the+request.")
152
+ end
153
+ end
154
+
155
+ context "accepted" do
156
+ before do
157
+ @params.merge!(accept: "true")
158
+
159
+ user.should_receive(:client_authorize!).with(client)
160
+
161
+ codes = double(:codes)
162
+
163
+ user.stub(:authorization_codes) { codes }
164
+
165
+ codes.should_receive(:create!).with(client: client, redirect_uri: redirect_uri) { authorization_code }
166
+ end
167
+
168
+ it "should respond with test" do
169
+ post :create, @params
170
+
171
+ response.should redirect_to("#{ redirect_uri }?code=#{ token }")
172
+ end
173
+ end
174
+ end
175
+ end
176
+ end
177
+ end
178
+ end
179
+ end