xing_backend_token_auth 0.1.31
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 +7 -0
- data/LICENSE +13 -0
- data/README.md +679 -0
- data/Rakefile +34 -0
- data/app/controllers/devise_token_auth/application_controller.rb +22 -0
- data/app/controllers/devise_token_auth/concerns/set_user_by_token.rb +110 -0
- data/app/controllers/devise_token_auth/confirmations_controller.rb +31 -0
- data/app/controllers/devise_token_auth/omniauth_callbacks_controller.rb +169 -0
- data/app/controllers/devise_token_auth/passwords_controller.rb +107 -0
- data/app/controllers/devise_token_auth/registrations_controller.rb +99 -0
- data/app/controllers/devise_token_auth/sessions_controller.rb +50 -0
- data/app/controllers/devise_token_auth/token_validations_controller.rb +22 -0
- data/app/serializers/devise_token_auth/error_messages_serializer.rb +16 -0
- data/app/serializers/devise_token_auth/resource_errors_serializer.rb +24 -0
- data/app/serializers/devise_token_auth/resource_serializer.rb +17 -0
- data/app/serializers/devise_token_auth/success_message_serializer.rb +15 -0
- data/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
- data/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
- data/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
- data/app/views/devise_token_auth/omniauth_failure.html.erb +2 -0
- data/app/views/devise_token_auth/omniauth_success.html.erb +8 -0
- data/app/views/layouts/omniauth_response.html.erb +31 -0
- data/config/initializers/devise.rb +207 -0
- data/config/initializers/token_auth_failure_app.rb +7 -0
- data/config/locales/devise.en.yml +59 -0
- data/config/routes.rb +5 -0
- data/lib/devise_token_auth.rb +9 -0
- data/lib/devise_token_auth/controllers/helpers.rb +129 -0
- data/lib/devise_token_auth/controllers/url_helpers.rb +8 -0
- data/lib/devise_token_auth/engine.rb +32 -0
- data/lib/devise_token_auth/models/token_authenticatable.rb +195 -0
- data/lib/devise_token_auth/rails/routes.rb +65 -0
- data/lib/generators/devise_token_auth/USAGE +31 -0
- data/lib/generators/devise_token_auth/install_generator.rb +100 -0
- data/lib/generators/devise_token_auth/install_views_generator.rb +16 -0
- data/lib/generators/devise_token_auth/templates/devise_token_auth.rb +22 -0
- data/lib/generators/devise_token_auth/templates/devise_token_auth_add_token_info_to_users.rb.erb +14 -0
- data/lib/tasks/devise_token_auth_tasks.rake +4 -0
- data/lib/xing_backend_token_auth.rb +1 -0
- data/test/controllers/demo_group_controller_test.rb +126 -0
- data/test/controllers/demo_mang_controller_test.rb +263 -0
- data/test/controllers/demo_user_controller_test.rb +262 -0
- data/test/controllers/devise_token_auth/confirmations_controller_test.rb +107 -0
- data/test/controllers/devise_token_auth/omniauth_callbacks_controller_test.rb +144 -0
- data/test/controllers/devise_token_auth/passwords_controller_test.rb +275 -0
- data/test/controllers/devise_token_auth/registrations_controller_test.rb +405 -0
- data/test/controllers/devise_token_auth/registrations_controller_test.rb.orig +494 -0
- data/test/controllers/devise_token_auth/sessions_controller_test.rb +169 -0
- data/test/controllers/overrides/confirmations_controller_test.rb +44 -0
- data/test/controllers/overrides/omniauth_callbacks_controller_test.rb +44 -0
- data/test/controllers/overrides/passwords_controller_test.rb +64 -0
- data/test/controllers/overrides/registrations_controller_test.rb +42 -0
- data/test/controllers/overrides/sessions_controller_test.rb +35 -0
- data/test/controllers/overrides/token_validations_controller_test.rb +38 -0
- data/test/dummy/README.rdoc +28 -0
- data/test/dummy/Rakefile +6 -0
- data/test/dummy/app/assets/images/logo.jpg +0 -0
- data/test/dummy/app/assets/images/omniauth-provider-settings.png +0 -0
- data/test/dummy/app/assets/javascripts/application.js +13 -0
- data/test/dummy/app/assets/stylesheets/application.css +15 -0
- data/test/dummy/app/controllers/application_controller.rb +16 -0
- data/test/dummy/app/controllers/demo_group_controller.rb +13 -0
- data/test/dummy/app/controllers/demo_mang_controller.rb +12 -0
- data/test/dummy/app/controllers/demo_user_controller.rb +12 -0
- data/test/dummy/app/controllers/overrides/confirmations_controller.rb +32 -0
- data/test/dummy/app/controllers/overrides/omniauth_callbacks_controller.rb +14 -0
- data/test/dummy/app/controllers/overrides/passwords_controller.rb +39 -0
- data/test/dummy/app/controllers/overrides/registrations_controller.rb +27 -0
- data/test/dummy/app/controllers/overrides/sessions_controller.rb +26 -0
- data/test/dummy/app/controllers/overrides/token_validations_controller.rb +23 -0
- data/test/dummy/app/controllers/registrations_controller.rb +2 -0
- data/test/dummy/app/helpers/application_helper.rb +1065 -0
- data/test/dummy/app/models/evil_user.rb +5 -0
- data/test/dummy/app/models/mang.rb +5 -0
- data/test/dummy/app/models/user.rb +20 -0
- data/test/dummy/app/views/layouts/application.html.erb +14 -0
- data/test/dummy/bin/bundle +3 -0
- data/test/dummy/bin/rails +8 -0
- data/test/dummy/bin/rake +8 -0
- data/test/dummy/bin/spring +18 -0
- data/test/dummy/config.ru +16 -0
- data/test/dummy/config/application.rb +23 -0
- data/test/dummy/config/boot.rb +5 -0
- data/test/dummy/config/database.yml +31 -0
- data/test/dummy/config/environment.rb +5 -0
- data/test/dummy/config/environments/development.rb +44 -0
- data/test/dummy/config/environments/production.rb +82 -0
- data/test/dummy/config/environments/test.rb +40 -0
- data/test/dummy/config/initializers/assets.rb +8 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
- data/test/dummy/config/initializers/devise_token_auth.rb +22 -0
- data/test/dummy/config/initializers/figaro.rb +1 -0
- data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/test/dummy/config/initializers/inflections.rb +16 -0
- data/test/dummy/config/initializers/mime_types.rb +4 -0
- data/test/dummy/config/initializers/omniauth.rb +8 -0
- data/test/dummy/config/initializers/session_store.rb +3 -0
- data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/test/dummy/config/locales/en.yml +23 -0
- data/test/dummy/config/routes.rb +32 -0
- data/test/dummy/config/secrets.yml +22 -0
- data/test/dummy/config/spring.rb +1 -0
- data/test/dummy/db/migrate/20140715061447_devise_token_auth_create_users.rb +56 -0
- data/test/dummy/db/migrate/20140715061805_devise_token_auth_create_mangs.rb +56 -0
- data/test/dummy/db/migrate/20140829044006_add_operating_thetan_to_user.rb +6 -0
- data/test/dummy/db/migrate/20140916224624_add_favorite_color_to_mangs.rb +5 -0
- data/test/dummy/db/migrate/20140928231203_devise_token_auth_create_evil_users.rb +57 -0
- data/test/dummy/db/schema.rb +111 -0
- data/test/dummy/public/404.html +67 -0
- data/test/dummy/public/422.html +67 -0
- data/test/dummy/public/500.html +66 -0
- data/test/dummy/public/favicon.ico +0 -0
- data/test/fixtures/evil_users.yml +29 -0
- data/test/fixtures/mangs.yml +29 -0
- data/test/fixtures/users.yml +29 -0
- data/test/integration/navigation_test.rb +10 -0
- data/test/lib/generators/devise_token_auth/install_generator_test.rb +131 -0
- data/test/lib/generators/devise_token_auth/install_views_generator_test.rb +23 -0
- data/test/models/user_test.rb +81 -0
- data/test/test_helper.rb +60 -0
- metadata +320 -0
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
module ActionDispatch::Routing
|
|
2
|
+
class Mapper
|
|
3
|
+
def mount_devise_token_auth_for(resource, opts)
|
|
4
|
+
# ensure objects exist to simplify attr checks
|
|
5
|
+
opts[:controllers] ||= {}
|
|
6
|
+
opts[:skip] ||= []
|
|
7
|
+
|
|
8
|
+
# check for ctrl overrides, fall back to defaults
|
|
9
|
+
sessions_ctrl = opts[:controllers][:sessions] || "devise_token_auth/sessions"
|
|
10
|
+
registrations_ctrl = opts[:controllers][:registrations] || "devise_token_auth/registrations"
|
|
11
|
+
passwords_ctrl = opts[:controllers][:passwords] || "devise_token_auth/passwords"
|
|
12
|
+
confirmations_ctrl = opts[:controllers][:confirmations] || "devise_token_auth/confirmations"
|
|
13
|
+
token_validations_ctrl = opts[:controllers][:token_validations] || "devise_token_auth/token_validations"
|
|
14
|
+
omniauth_ctrl = opts[:controllers][:omniauth_callbacks] || "devise_token_auth/omniauth_callbacks"
|
|
15
|
+
|
|
16
|
+
# define devise controller mappings
|
|
17
|
+
controllers = {:sessions => sessions_ctrl,
|
|
18
|
+
:registrations => registrations_ctrl,
|
|
19
|
+
:passwords => passwords_ctrl,
|
|
20
|
+
:confirmations => confirmations_ctrl,
|
|
21
|
+
:omniauth_callbacks => omniauth_ctrl}
|
|
22
|
+
|
|
23
|
+
# remove any unwanted devise modules
|
|
24
|
+
opts[:skip].each{|item| controllers.delete(item)}
|
|
25
|
+
|
|
26
|
+
scope opts[:at] do
|
|
27
|
+
devise_for resource.pluralize.underscore.to_sym,
|
|
28
|
+
:class_name => resource,
|
|
29
|
+
:module => :devise,
|
|
30
|
+
:path => "",
|
|
31
|
+
:controllers => controllers
|
|
32
|
+
|
|
33
|
+
devise_scope resource.underscore.to_sym do
|
|
34
|
+
# path to verify token validity
|
|
35
|
+
get "validate_token", to: "#{token_validations_ctrl}#validate_token"
|
|
36
|
+
|
|
37
|
+
# omniauth routes. only define if omniauth is installed and not skipped.
|
|
38
|
+
if defined?(::OmniAuth) and not opts[:skip].include?(:omniauth_callbacks)
|
|
39
|
+
get "failure", to: "#{omniauth_ctrl}#omniauth_failure"
|
|
40
|
+
get ":provider/callback", to: "#{omniauth_ctrl}#omniauth_success"
|
|
41
|
+
|
|
42
|
+
# preserve the resource class thru oauth authentication by setting name of
|
|
43
|
+
# resource as "resource_class" param
|
|
44
|
+
match ":provider", to: redirect{|params, request|
|
|
45
|
+
# get the current querystring
|
|
46
|
+
qs = CGI::parse(request.env["QUERY_STRING"])
|
|
47
|
+
|
|
48
|
+
# append name of current resource
|
|
49
|
+
qs["resource_class"] = [resource]
|
|
50
|
+
|
|
51
|
+
# re-construct the path for omniauth
|
|
52
|
+
"#{::OmniAuth::config.path_prefix}/#{params[:provider]}?#{{}.tap {|hash| qs.each{|k, v| hash[k] = v.first}}.to_param}"
|
|
53
|
+
}, via: [:get]
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# ignore error about omniauth/multiple model support
|
|
60
|
+
def set_omniauth_path_prefix!(path_prefix)
|
|
61
|
+
::OmniAuth.config.path_prefix = path_prefix
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
end
|
|
65
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
Description:
|
|
2
|
+
This generator will install all the necessary configuration and migration
|
|
3
|
+
files for the devies_token_auth gem. See
|
|
4
|
+
https://github.com/lynndylanhurley/devise_token_auth for more information.
|
|
5
|
+
|
|
6
|
+
Arguments:
|
|
7
|
+
USER_CLASS # The name of the class to use for user authentication. Default is
|
|
8
|
+
# 'User'
|
|
9
|
+
MOUNT_PATH # The path at which to mount the authentication routes. Default is
|
|
10
|
+
# 'auth'. More detail documentation is here:
|
|
11
|
+
# https://github.com/lynndylanhurley/devise_token_auth#usage
|
|
12
|
+
|
|
13
|
+
Example:
|
|
14
|
+
rails generate devise_token_auth:install User auth
|
|
15
|
+
|
|
16
|
+
This will create:
|
|
17
|
+
config/initializers/devise_token_auth.rb
|
|
18
|
+
db/migrate/<%= Time.now.utc.strftime("%Y%m%d%H%M%S") %>_create_devise_token_auth_create_users.rb
|
|
19
|
+
app/models/user.rb
|
|
20
|
+
|
|
21
|
+
If 'app/models/user.rb' already exists, the following line will be inserted
|
|
22
|
+
after the class definition:
|
|
23
|
+
include DeviseTokenAuth::Concerns::User
|
|
24
|
+
|
|
25
|
+
The following line will be inserted into your application controller at
|
|
26
|
+
app/controllers/application_controller.rb:
|
|
27
|
+
include DeviseTokenAuth::Concerns::SetUserByToken
|
|
28
|
+
|
|
29
|
+
The following line will be inserted at the top of 'config/routes.rb' if it
|
|
30
|
+
does not already exist:
|
|
31
|
+
mount_devise_token_auth_for "User", at: '/auth'
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
module DeviseTokenAuth
|
|
2
|
+
class InstallGenerator < Rails::Generators::Base
|
|
3
|
+
include Rails::Generators::Migration
|
|
4
|
+
|
|
5
|
+
source_root File.expand_path('../templates', __FILE__)
|
|
6
|
+
|
|
7
|
+
argument :user_class, type: :string, default: "User"
|
|
8
|
+
argument :mount_path, type: :string, default: '/auth'
|
|
9
|
+
|
|
10
|
+
def create_initializer_file
|
|
11
|
+
copy_file("devise_token_auth.rb", "config/initializers/devise_token_auth.rb")
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def copy_migrations
|
|
15
|
+
if self.class.migration_exists?("db/migrate", "devise_token_auth_create_#{ user_class.underscore }")
|
|
16
|
+
say_status("skipped", "Migration 'devise_token_auth_create_#{ user_class.underscore }' already exists")
|
|
17
|
+
else
|
|
18
|
+
migration_template(
|
|
19
|
+
"devise_token_auth_add_token_info_to_users.rb.erb",
|
|
20
|
+
"db/migrate/devise_token_auth_add_token_info_to_#{ user_class.pluralize.underscore }.rb"
|
|
21
|
+
)
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def include_controller_concerns
|
|
26
|
+
fname = "app/controllers/application_controller.rb"
|
|
27
|
+
line = "include DeviseTokenAuth::Concerns::SetUserByToken"
|
|
28
|
+
|
|
29
|
+
if File.exist?(File.join(destination_root, fname))
|
|
30
|
+
if parse_file_for_line(fname, line)
|
|
31
|
+
say_status("skipped", "Concern is already included in the application controller.")
|
|
32
|
+
else
|
|
33
|
+
inject_into_file fname, after: "class ApplicationController < ActionController::Base\n" do <<-'RUBY'
|
|
34
|
+
include DeviseTokenAuth::Concerns::SetUserByToken
|
|
35
|
+
RUBY
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
else
|
|
39
|
+
say_status("skipped", "app/controllers/application_controller.rb not found. Add 'include DeviseTokenAuth::Concerns::SetUserByToken' to any controllers that require authentication.")
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def add_route_mount
|
|
44
|
+
f = "config/routes.rb"
|
|
45
|
+
str = "mount_devise_token_auth_for '#{user_class}', at: '#{mount_path}'"
|
|
46
|
+
|
|
47
|
+
if File.exist?(File.join(destination_root, f))
|
|
48
|
+
line = parse_file_for_line(f, "mount_devise_token_auth_for")
|
|
49
|
+
|
|
50
|
+
unless line
|
|
51
|
+
line = "Rails.application.routes.draw do"
|
|
52
|
+
existing_user_class = false
|
|
53
|
+
else
|
|
54
|
+
existing_user_class = true
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
if parse_file_for_line(f, str)
|
|
58
|
+
say_status("skipped", "Routes already exist for #{user_class} at #{mount_path}")
|
|
59
|
+
else
|
|
60
|
+
insert_after_line(f, line, str)
|
|
61
|
+
|
|
62
|
+
if existing_user_class
|
|
63
|
+
scoped_routes = ""+
|
|
64
|
+
"as :#{user_class.underscore} do\n"+
|
|
65
|
+
" # Define routes for #{user_class} within this block.\n"+
|
|
66
|
+
" end\n"
|
|
67
|
+
insert_after_line(f, str, scoped_routes)
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
else
|
|
71
|
+
say_status("skipped", "config/routes.rb not found. Add \"mount_devise_token_auth_for '#{user_class}', at: '#{mount_path}'\" to your routes file.")
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
private
|
|
76
|
+
|
|
77
|
+
def self.next_migration_number(path)
|
|
78
|
+
Time.now.utc.strftime("%Y%m%d%H%M%S")
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def insert_after_line(filename, line, str)
|
|
82
|
+
gsub_file filename, /(#{Regexp.escape(line)})/mi do |match|
|
|
83
|
+
"#{match}\n #{str}"
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def parse_file_for_line(filename, str)
|
|
88
|
+
match = false
|
|
89
|
+
|
|
90
|
+
File.open(File.join(destination_root, filename)) do |f|
|
|
91
|
+
f.each_line do |line|
|
|
92
|
+
if line =~ /(#{Regexp.escape(str)})/mi
|
|
93
|
+
match = line
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
match
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
module DeviseTokenAuth
|
|
2
|
+
class InstallViewsGenerator < Rails::Generators::Base
|
|
3
|
+
source_root File.expand_path('../../../../app/views/devise/mailer', __FILE__)
|
|
4
|
+
|
|
5
|
+
def copy_mailer_templates
|
|
6
|
+
copy_file(
|
|
7
|
+
"confirmation_instructions.html.erb",
|
|
8
|
+
"app/views/devise/mailer/confirmation_instructions.html.erb"
|
|
9
|
+
)
|
|
10
|
+
copy_file(
|
|
11
|
+
"reset_password_instructions.html.erb",
|
|
12
|
+
"app/views/devise/mailer/reset_password_instructions.html.erb"
|
|
13
|
+
)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
DeviseTokenAuth.setup do |config|
|
|
2
|
+
# By default the authorization headers will change after each request. The
|
|
3
|
+
# client is responsible for keeping track of the changing tokens. Change
|
|
4
|
+
# this to false to prevent the Authorization header from changing after
|
|
5
|
+
# each request.
|
|
6
|
+
#config.change_headers_on_each_request = true
|
|
7
|
+
|
|
8
|
+
# By default, users will need to re-authenticate after 2 weeks. This setting
|
|
9
|
+
# determines how long tokens will remain valid after they are issued.
|
|
10
|
+
#config.token_lifespan = 2.weeks
|
|
11
|
+
|
|
12
|
+
# Sometimes it's necessary to make several requests to the API at the same
|
|
13
|
+
# time. In this case, each request in the batch will need to share the same
|
|
14
|
+
# auth token. This setting determines how far apart the requests can be while
|
|
15
|
+
# still using the same auth token.
|
|
16
|
+
#config.batch_request_buffer_throttle = 5.seconds
|
|
17
|
+
|
|
18
|
+
# This route will be the prefix for all oauth2 redirect callbacks. For
|
|
19
|
+
# example, using the default '/omniauth', the github oauth2 provider will
|
|
20
|
+
# redirect successful authentications to '/omniauth/github/callback'
|
|
21
|
+
#config.omniauth_prefix = "/omniauth"
|
|
22
|
+
end
|
data/lib/generators/devise_token_auth/templates/devise_token_auth_add_token_info_to_users.rb.erb
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
class DeviseTokenAuthAddTokenInfoTo<%= user_class.pluralize %> < ActiveRecord::Migration
|
|
2
|
+
def change
|
|
3
|
+
change_table(:<%= user_class.pluralize.underscore %>) do |t|
|
|
4
|
+
## unique oauth id
|
|
5
|
+
t.string :provider
|
|
6
|
+
t.string :uid, :null => false, :default => ""
|
|
7
|
+
|
|
8
|
+
## Tokens
|
|
9
|
+
t.text :tokens
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
add_index :<%= user_class.pluralize.underscore %>, :uid, :unique => true
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require 'devise_token_auth'
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
require 'test_helper'
|
|
2
|
+
|
|
3
|
+
# was the web request successful?
|
|
4
|
+
# was the user redirected to the right page?
|
|
5
|
+
# was the user successfully authenticated?
|
|
6
|
+
# was the correct object stored in the response?
|
|
7
|
+
# was the appropriate message delivered in the json payload?
|
|
8
|
+
|
|
9
|
+
class DemoGroupControllerTest < ActionDispatch::IntegrationTest
|
|
10
|
+
describe DemoGroupController do
|
|
11
|
+
describe "Token access" do
|
|
12
|
+
before do
|
|
13
|
+
# user
|
|
14
|
+
@user = users(:confirmed_email_user)
|
|
15
|
+
@user.skip_confirmation!
|
|
16
|
+
@user.save!
|
|
17
|
+
|
|
18
|
+
@user_auth_headers = @user.create_new_auth_token
|
|
19
|
+
|
|
20
|
+
@user_token = @user_auth_headers['access-token']
|
|
21
|
+
@user_client_id = @user_auth_headers['client']
|
|
22
|
+
@user_expiry = @user_auth_headers['expiry']
|
|
23
|
+
|
|
24
|
+
# mang
|
|
25
|
+
@mang = mangs(:confirmed_email_user)
|
|
26
|
+
@mang.skip_confirmation!
|
|
27
|
+
@mang.save!
|
|
28
|
+
|
|
29
|
+
@mang_auth_headers = @mang.create_new_auth_token
|
|
30
|
+
|
|
31
|
+
@mang_token = @mang_auth_headers['access-token']
|
|
32
|
+
@mang_client_id = @mang_auth_headers['client']
|
|
33
|
+
@mang_expiry = @mang_auth_headers['expiry']
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
describe 'user access' do
|
|
37
|
+
before do
|
|
38
|
+
# ensure that request is not treated as batch request
|
|
39
|
+
age_token(@user, @user_client_id)
|
|
40
|
+
|
|
41
|
+
get '/demo/members_only_group', {}, @user_auth_headers
|
|
42
|
+
|
|
43
|
+
@resp_token = response.headers['access-token']
|
|
44
|
+
@resp_client_id = response.headers['client']
|
|
45
|
+
@resp_expiry = response.headers['expiry']
|
|
46
|
+
@resp_uid = response.headers['uid']
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
test 'request is successful' do
|
|
50
|
+
assert_equal 200, response.status
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
describe 'devise mappings' do
|
|
54
|
+
it 'should define current_user' do
|
|
55
|
+
assert_equal @user, @controller.current_user
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
it 'should define user_signed_in?' do
|
|
59
|
+
assert @controller.user_signed_in?
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
it 'should not define current_mang' do
|
|
63
|
+
refute_equal @user, @controller.current_mang
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
it 'should define current_member' do
|
|
67
|
+
assert_equal @user, @controller.current_member
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
it 'should define current_members' do
|
|
71
|
+
assert @controller.current_members.include? @user
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
it 'should define member_signed_in?' do
|
|
75
|
+
assert @controller.current_members.include? @user
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
describe 'mang access' do
|
|
81
|
+
before do
|
|
82
|
+
# ensure that request is not treated as batch request
|
|
83
|
+
age_token(@mang, @mang_client_id)
|
|
84
|
+
|
|
85
|
+
get '/demo/members_only_group', {}, @mang_auth_headers
|
|
86
|
+
|
|
87
|
+
@resp_token = response.headers['access-token']
|
|
88
|
+
@resp_client_id = response.headers['client']
|
|
89
|
+
@resp_expiry = response.headers['expiry']
|
|
90
|
+
@resp_uid = response.headers['uid']
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
test 'request is successful' do
|
|
94
|
+
assert_equal 200, response.status
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
describe 'devise mappings' do
|
|
98
|
+
it 'should define current_mang' do
|
|
99
|
+
assert_equal @mang, @controller.current_mang
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
it 'should define mang_signed_in?' do
|
|
103
|
+
assert @controller.mang_signed_in?
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
it 'should not define current_mang' do
|
|
107
|
+
refute_equal @mang, @controller.current_user
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
it 'should define current_member' do
|
|
111
|
+
assert_equal @mang, @controller.current_member
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
it 'should define current_members' do
|
|
115
|
+
assert @controller.current_members.include? @mang
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
it 'should define member_signed_in?' do
|
|
119
|
+
assert @controller.current_members.include? @mang
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
require 'test_helper'
|
|
2
|
+
|
|
3
|
+
# was the web request successful?
|
|
4
|
+
# was the user redirected to the right page?
|
|
5
|
+
# was the user successfully authenticated?
|
|
6
|
+
# was the correct object stored in the response?
|
|
7
|
+
# was the appropriate message delivered in the json payload?
|
|
8
|
+
|
|
9
|
+
class DemoMangControllerTest < ActionDispatch::IntegrationTest
|
|
10
|
+
describe DemoMangController do
|
|
11
|
+
describe "Token access" do
|
|
12
|
+
before do
|
|
13
|
+
@user = mangs(:confirmed_email_user)
|
|
14
|
+
@user.skip_confirmation!
|
|
15
|
+
@user.save!
|
|
16
|
+
|
|
17
|
+
@auth_headers = @user.create_new_auth_token
|
|
18
|
+
|
|
19
|
+
@token = @auth_headers['access-token']
|
|
20
|
+
@client_id = @auth_headers['client']
|
|
21
|
+
@expiry = @auth_headers['expiry']
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
describe 'successful request' do
|
|
25
|
+
before do
|
|
26
|
+
# ensure that request is not treated as batch request
|
|
27
|
+
age_token(@user, @client_id)
|
|
28
|
+
|
|
29
|
+
get '/demo/members_only_mang', {}, @auth_headers
|
|
30
|
+
|
|
31
|
+
@resp_token = response.headers['access-token']
|
|
32
|
+
@resp_client_id = response.headers['client']
|
|
33
|
+
@resp_expiry = response.headers['expiry']
|
|
34
|
+
@resp_uid = response.headers['uid']
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
describe 'devise mappings' do
|
|
38
|
+
it 'should define current_mang' do
|
|
39
|
+
assert_equal @user, @controller.current_mang
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
it 'should define mang_signed_in?' do
|
|
43
|
+
assert @controller.mang_signed_in?
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
it 'should not define current_user' do
|
|
47
|
+
refute_equal @user, @controller.current_user
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
it 'should return success status' do
|
|
52
|
+
assert_equal 200, response.status
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
it 'should receive new token after successful request' do
|
|
56
|
+
refute_equal @token, @resp_token
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
it 'should preserve the client id from the first request' do
|
|
60
|
+
assert_equal @client_id, @resp_client_id
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
it "should return the user's uid in the auth header" do
|
|
64
|
+
assert_equal @user.uid, @resp_uid
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
it 'should not treat this request as a batch request' do
|
|
68
|
+
refute assigns(:is_batch_request)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
describe 'subsequent requests' do
|
|
72
|
+
before do
|
|
73
|
+
@user.reload
|
|
74
|
+
# ensure that request is not treated as batch request
|
|
75
|
+
age_token(@user, @client_id)
|
|
76
|
+
|
|
77
|
+
get '/demo/members_only_mang', {}, @auth_headers.merge({'access-token' => @resp_token})
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
it 'should not treat this request as a batch request' do
|
|
81
|
+
refute assigns(:is_batch_request)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
it "should allow a new request to be made using new token" do
|
|
85
|
+
assert_equal 200, response.status
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
describe 'failed request' do
|
|
91
|
+
before do
|
|
92
|
+
get '/demo/members_only_mang', {}, @auth_headers.merge({'access-token' => "bogus"})
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
it 'should not return any auth headers' do
|
|
96
|
+
refute response.headers['access-token']
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
it 'should return error: unauthorized status' do
|
|
100
|
+
assert_equal 401, response.status
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
describe 'disable change_headers_on_each_request' do
|
|
105
|
+
before do
|
|
106
|
+
DeviseTokenAuth.change_headers_on_each_request = false
|
|
107
|
+
@user.reload
|
|
108
|
+
age_token(@user, @client_id)
|
|
109
|
+
|
|
110
|
+
get '/demo/members_only_mang', {}, @auth_headers
|
|
111
|
+
|
|
112
|
+
@first_is_batch_request = assigns(:is_batch_request)
|
|
113
|
+
@first_user = assigns(:user).dup
|
|
114
|
+
@first_access_token = response.headers['access-token']
|
|
115
|
+
@first_response_status = response.status
|
|
116
|
+
|
|
117
|
+
@user.reload
|
|
118
|
+
age_token(@user, @client_id)
|
|
119
|
+
|
|
120
|
+
# use expired auth header
|
|
121
|
+
get '/demo/members_only_mang', {}, @auth_headers
|
|
122
|
+
|
|
123
|
+
@second_is_batch_request = assigns(:is_batch_request)
|
|
124
|
+
@second_user = assigns(:user).dup
|
|
125
|
+
@second_access_token = response.headers['access-token']
|
|
126
|
+
@second_response_status = response.status
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
after do
|
|
130
|
+
DeviseTokenAuth.change_headers_on_each_request = true
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
it 'should allow the first request through' do
|
|
134
|
+
assert_equal 200, @first_response_status
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
it 'should allow the second request through' do
|
|
138
|
+
assert_equal 200, @second_response_status
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
it 'should return auth headers from the first request' do
|
|
142
|
+
assert @first_access_token
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
it 'should not treat either requests as batch requests' do
|
|
146
|
+
refute @first_is_batch_request
|
|
147
|
+
refute @second_is_batch_request
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
it 'should return auth headers from the second request' do
|
|
151
|
+
assert @second_access_token
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
it 'should define user during first request' do
|
|
155
|
+
assert @first_user
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
it 'should define user during second request' do
|
|
159
|
+
assert @second_user
|
|
160
|
+
end
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
describe 'batch requests' do
|
|
164
|
+
describe 'success' do
|
|
165
|
+
before do
|
|
166
|
+
age_token(@user, @client_id)
|
|
167
|
+
#request.headers.merge!(@auth_headers)
|
|
168
|
+
|
|
169
|
+
get '/demo/members_only_mang', {}, @auth_headers
|
|
170
|
+
|
|
171
|
+
@first_is_batch_request = assigns(:is_batch_request)
|
|
172
|
+
@first_user = assigns(:user)
|
|
173
|
+
@first_access_token = response.headers['access-token']
|
|
174
|
+
|
|
175
|
+
get '/demo/members_only_mang', {}, @auth_headers
|
|
176
|
+
|
|
177
|
+
@second_is_batch_request = assigns(:is_batch_request)
|
|
178
|
+
@second_user = assigns(:user)
|
|
179
|
+
@second_access_token = response.headers['access-token']
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
it 'should allow both requests through' do
|
|
183
|
+
assert_equal 200, response.status
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
it 'should not treat the first request as a batch request' do
|
|
187
|
+
refute @first_is_batch_request
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
it 'should treat the second request as a batch request' do
|
|
191
|
+
assert @second_is_batch_request
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
it 'should return access token for first (non-batch) request' do
|
|
195
|
+
assert @first_access_token
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
it 'should also return auth headers for second (batched) requests' do
|
|
199
|
+
assert @second_access_token
|
|
200
|
+
end
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
describe 'time out' do
|
|
204
|
+
before do
|
|
205
|
+
@user.reload
|
|
206
|
+
age_token(@user, @client_id)
|
|
207
|
+
|
|
208
|
+
get '/demo/members_only_mang', {}, @auth_headers
|
|
209
|
+
|
|
210
|
+
@first_is_batch_request = assigns(:is_batch_request)
|
|
211
|
+
@first_user = assigns(:user).dup
|
|
212
|
+
@first_access_token = response.headers['access-token']
|
|
213
|
+
@first_response_status = response.status
|
|
214
|
+
|
|
215
|
+
@user.reload
|
|
216
|
+
age_token(@user, @client_id)
|
|
217
|
+
|
|
218
|
+
# use expired auth header
|
|
219
|
+
get '/demo/members_only_mang', {}, @auth_headers
|
|
220
|
+
|
|
221
|
+
@second_is_batch_request = assigns(:is_batch_request)
|
|
222
|
+
@second_user = assigns(:user)
|
|
223
|
+
@second_access_token = response.headers['access-token']
|
|
224
|
+
@second_response_status = response.status
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
it 'should allow the first request through' do
|
|
228
|
+
assert_equal 200, @first_response_status
|
|
229
|
+
end
|
|
230
|
+
|
|
231
|
+
it 'should not allow the second request through' do
|
|
232
|
+
assert_equal 401, @second_response_status
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
it 'should not treat first request as batch request' do
|
|
236
|
+
refute @secord_is_batch_request
|
|
237
|
+
end
|
|
238
|
+
|
|
239
|
+
it 'should return auth headers from the first request' do
|
|
240
|
+
assert @first_access_token
|
|
241
|
+
end
|
|
242
|
+
|
|
243
|
+
it 'should not treat second request as batch request' do
|
|
244
|
+
refute @secord_is_batch_request
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
it 'should not return auth headers from the second request' do
|
|
248
|
+
refute @second_access_token
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
it 'should define user during first request' do
|
|
252
|
+
assert @first_user
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
it 'should not define user during second request' do
|
|
256
|
+
refute @second_user
|
|
257
|
+
end
|
|
258
|
+
end
|
|
259
|
+
end
|
|
260
|
+
end
|
|
261
|
+
end
|
|
262
|
+
end
|
|
263
|
+
|