KnockRails3 2.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (98) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/Rakefile +39 -0
  4. data/app/controllers/knock_rails3/application_controller.rb +11 -0
  5. data/app/controllers/knock_rails3/auth_token_controller.rb +47 -0
  6. data/app/model/knock_rails3/auth_token.rb +78 -0
  7. data/config/routes.rb +3 -0
  8. data/lib/KnockRails3.rb +31 -0
  9. data/lib/generators/knock_rails3/install_generator.rb +13 -0
  10. data/lib/generators/knock_rails3/token_controller_generator.rb +27 -0
  11. data/lib/generators/templates/entity_token_controller.rb.erb +2 -0
  12. data/lib/generators/templates/knock_rails3.rb +59 -0
  13. data/lib/knock_rails3/authenticable.rb +60 -0
  14. data/lib/knock_rails3/engine.rb +6 -0
  15. data/lib/knock_rails3/version.rb +3 -0
  16. data/lib/tasks/KnockRails3_tasks.rake +4 -0
  17. data/test/dummy/README.rdoc +28 -0
  18. data/test/dummy/Rakefile +6 -0
  19. data/test/dummy/app/assets/javascripts/application.js +13 -0
  20. data/test/dummy/app/assets/stylesheets/application.css +15 -0
  21. data/test/dummy/app/controllers/admin_protected_controller.rb +7 -0
  22. data/test/dummy/app/controllers/admin_token_controller.rb +2 -0
  23. data/test/dummy/app/controllers/application_controller.rb +7 -0
  24. data/test/dummy/app/controllers/composite_name_entity_protected_controller.rb +7 -0
  25. data/test/dummy/app/controllers/current_users_controller.rb +9 -0
  26. data/test/dummy/app/controllers/custom_unauthorized_entity_controller.rb +13 -0
  27. data/test/dummy/app/controllers/guest_protected_controller.rb +7 -0
  28. data/test/dummy/app/controllers/protected_resources_controller.rb +7 -0
  29. data/test/dummy/app/controllers/v1/test_namespaced_controller.rb +17 -0
  30. data/test/dummy/app/controllers/vendor_protected_controller.rb +11 -0
  31. data/test/dummy/app/controllers/vendor_token_controller.rb +2 -0
  32. data/test/dummy/app/helpers/application_helper.rb +2 -0
  33. data/test/dummy/app/models/admin.rb +16 -0
  34. data/test/dummy/app/models/composite_name_entity.rb +3 -0
  35. data/test/dummy/app/models/guest.rb +7 -0
  36. data/test/dummy/app/models/user.rb +3 -0
  37. data/test/dummy/app/models/v1/user.rb +5 -0
  38. data/test/dummy/app/models/vendor.rb +3 -0
  39. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  40. data/test/dummy/bin/bundle +3 -0
  41. data/test/dummy/bin/rails +4 -0
  42. data/test/dummy/bin/rake +4 -0
  43. data/test/dummy/bin/setup +29 -0
  44. data/test/dummy/config.ru +4 -0
  45. data/test/dummy/config/application.rb +28 -0
  46. data/test/dummy/config/boot.rb +5 -0
  47. data/test/dummy/config/database.yml +25 -0
  48. data/test/dummy/config/environment.rb +5 -0
  49. data/test/dummy/config/environments/development.rb +41 -0
  50. data/test/dummy/config/environments/production.rb +79 -0
  51. data/test/dummy/config/environments/test.rb +47 -0
  52. data/test/dummy/config/initializers/assets.rb +11 -0
  53. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  54. data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
  55. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  56. data/test/dummy/config/initializers/inflections.rb +16 -0
  57. data/test/dummy/config/initializers/knock.rb +8 -0
  58. data/test/dummy/config/initializers/mime_types.rb +4 -0
  59. data/test/dummy/config/initializers/session_store.rb +3 -0
  60. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  61. data/test/dummy/config/locales/en.yml +23 -0
  62. data/test/dummy/config/routes.rb +17 -0
  63. data/test/dummy/config/secrets.yml +22 -0
  64. data/test/dummy/db/migrate/20150713101607_create_users.rb +10 -0
  65. data/test/dummy/db/migrate/20160519075733_create_admins.rb +10 -0
  66. data/test/dummy/db/migrate/20160522051816_create_vendors.rb +10 -0
  67. data/test/dummy/db/migrate/20160522181712_create_composite_name_entities.rb +10 -0
  68. data/test/dummy/db/migrate/20161127203222_create_v1_users.rb +12 -0
  69. data/test/dummy/db/schema.rb +50 -0
  70. data/test/dummy/public/404.html +67 -0
  71. data/test/dummy/public/422.html +67 -0
  72. data/test/dummy/public/500.html +66 -0
  73. data/test/dummy/public/favicon.ico +0 -0
  74. data/test/dummy/test/controllers/admin_protected_controller_test.rb +49 -0
  75. data/test/dummy/test/controllers/admin_token_controller_test.rb +22 -0
  76. data/test/dummy/test/controllers/composite_name_entity_protected_controller_test.rb +49 -0
  77. data/test/dummy/test/controllers/current_users_controller_test.rb +31 -0
  78. data/test/dummy/test/controllers/custom_unauthorized_entity_controller_test.rb +42 -0
  79. data/test/dummy/test/controllers/guest_protected_controller_test.rb +22 -0
  80. data/test/dummy/test/controllers/protected_resources_controller_test.rb +62 -0
  81. data/test/dummy/test/controllers/v1/test_namespaced_controller_test.rb +19 -0
  82. data/test/dummy/test/controllers/vendor_protected_controller_test.rb +55 -0
  83. data/test/dummy/test/controllers/vendor_token_controller_test.rb +22 -0
  84. data/test/dummy/test/models/admin_test.rb +7 -0
  85. data/test/dummy/test/models/user_test.rb +4 -0
  86. data/test/dummy/test/models/vendor_test.rb +7 -0
  87. data/test/fixtures/admins.yml +5 -0
  88. data/test/fixtures/composite_name_entities.yml +5 -0
  89. data/test/fixtures/users.yml +9 -0
  90. data/test/fixtures/v1_users.yml +6 -0
  91. data/test/fixtures/vendors.yml +5 -0
  92. data/test/generators/install_generator_test.rb +12 -0
  93. data/test/generators/token_controller_generator_test.rb +31 -0
  94. data/test/knock_test.rb +9 -0
  95. data/test/model/knock/auth_token_test.rb +123 -0
  96. data/test/support/generators_test_helper.rb +9 -0
  97. data/test/test_helper.rb +45 -0
  98. metadata +292 -0
@@ -0,0 +1,66 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>We're sorry, but something went wrong (500)</title>
5
+ <meta name="viewport" content="width=device-width,initial-scale=1">
6
+ <style>
7
+ body {
8
+ background-color: #EFEFEF;
9
+ color: #2E2F30;
10
+ text-align: center;
11
+ font-family: arial, sans-serif;
12
+ margin: 0;
13
+ }
14
+
15
+ div.dialog {
16
+ width: 95%;
17
+ max-width: 33em;
18
+ margin: 4em auto 0;
19
+ }
20
+
21
+ div.dialog > div {
22
+ border: 1px solid #CCC;
23
+ border-right-color: #999;
24
+ border-left-color: #999;
25
+ border-bottom-color: #BBB;
26
+ border-top: #B00100 solid 4px;
27
+ border-top-left-radius: 9px;
28
+ border-top-right-radius: 9px;
29
+ background-color: white;
30
+ padding: 7px 12% 0;
31
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
32
+ }
33
+
34
+ h1 {
35
+ font-size: 100%;
36
+ color: #730E15;
37
+ line-height: 1.5em;
38
+ }
39
+
40
+ div.dialog > p {
41
+ margin: 0 0 1em;
42
+ padding: 1em;
43
+ background-color: #F7F7F7;
44
+ border: 1px solid #CCC;
45
+ border-right-color: #999;
46
+ border-left-color: #999;
47
+ border-bottom-color: #999;
48
+ border-bottom-left-radius: 4px;
49
+ border-bottom-right-radius: 4px;
50
+ border-top-color: #DADADA;
51
+ color: #666;
52
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
53
+ }
54
+ </style>
55
+ </head>
56
+
57
+ <body>
58
+ <!-- This file lives in public/500.html -->
59
+ <div class="dialog">
60
+ <div>
61
+ <h1>We're sorry, but something went wrong.</h1>
62
+ </div>
63
+ <p>If you are the application owner check the logs for more information.</p>
64
+ </div>
65
+ </body>
66
+ </html>
File without changes
@@ -0,0 +1,49 @@
1
+ require 'test_helper'
2
+
3
+ class AdminProtectedControllerTest < ActionController::TestCase
4
+ def valid_auth
5
+ @admin = admins(:one)
6
+ @token = KnockRails3::AuthToken.new(payload: { sub: @admin.id }).token
7
+ @request.env['HTTP_AUTHORIZATION'] = "Bearer #{@token}"
8
+ end
9
+
10
+ def invalid_token_auth
11
+ @token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9'
12
+ @request.env['HTTP_AUTHORIZATION'] = "Bearer #{@token}"
13
+ end
14
+
15
+ def invalid_entity_auth
16
+ @token = KnockRails3::AuthToken.new(payload: { sub: 0 }).token
17
+ @request.env['HTTP_AUTHORIZATION'] = "Bearer #{@token}"
18
+ end
19
+
20
+ test "responds with unauthorized" do
21
+ get :index
22
+ assert_response :unauthorized
23
+ end
24
+
25
+ test "responds with unauthorized to invalid token" do
26
+ invalid_token_auth
27
+ get :index
28
+ assert_response :unauthorized
29
+ end
30
+
31
+ test "responds with unauthorized to invalid entity" do
32
+ invalid_entity_auth
33
+ get :index
34
+ assert_response :unauthorized
35
+ end
36
+
37
+ test "responds with success if authenticated" do
38
+ valid_auth
39
+ get :index
40
+ assert_response :success
41
+ end
42
+
43
+ test "has a current_admin after authentication" do
44
+ valid_auth
45
+ get :index
46
+ assert_response :success
47
+ assert @controller.current_admin.id == @admin.id
48
+ end
49
+ end
@@ -0,0 +1,22 @@
1
+ require 'test_helper'
2
+
3
+ class AdminTokenControllerTest < ActionController::TestCase
4
+ def setup
5
+ @admin = admins(:one)
6
+ end
7
+
8
+ test "responds with 404 if user does not exist" do
9
+ post :create, params: {auth: { email: 'wrong@example.net', password: '' }}
10
+ assert_response :not_found
11
+ end
12
+
13
+ test "responds with 404 if password is invalid" do
14
+ post :create, params: {auth: { email: @admin.email, password: 'wrong' }}
15
+ assert_response :not_found
16
+ end
17
+
18
+ test "responds with 201" do
19
+ post :create, params: {auth: { email: @admin.email, password: 'secret' }}
20
+ assert_response :created
21
+ end
22
+ end
@@ -0,0 +1,49 @@
1
+ require 'test_helper'
2
+
3
+ class CompositeNameEntityProtectedControllerTest < ActionController::TestCase
4
+ def valid_auth
5
+ @composite_name_entity = composite_name_entities(:one)
6
+ @token = KnockRails3::AuthToken.new(payload: { sub: @composite_name_entity.id }).token
7
+ @request.env['HTTP_AUTHORIZATION'] = "Bearer #{@token}"
8
+ end
9
+
10
+ def invalid_token_auth
11
+ @token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9'
12
+ @request.env['HTTP_AUTHORIZATION'] = "Bearer #{@token}"
13
+ end
14
+
15
+ def invalid_entity_auth
16
+ @token = KnockRails3::AuthToken.new(payload: { sub: 0 }).token
17
+ @request.env['HTTP_AUTHORIZATION'] = "Bearer #{@token}"
18
+ end
19
+
20
+ test "responds with unauthorized" do
21
+ get :index
22
+ assert_response :unauthorized
23
+ end
24
+
25
+ test "responds with unauthorized to invalid token" do
26
+ invalid_token_auth
27
+ get :index
28
+ assert_response :unauthorized
29
+ end
30
+
31
+ test "responds with unauthorized to invalid entity" do
32
+ invalid_entity_auth
33
+ get :index
34
+ assert_response :unauthorized
35
+ end
36
+
37
+ test "responds with success if authenticated" do
38
+ valid_auth
39
+ get :index
40
+ assert_response :success
41
+ end
42
+
43
+ test "has a current_composite_name_entity after authentication" do
44
+ valid_auth
45
+ get :index
46
+ assert_response :success
47
+ assert @controller.current_composite_name_entity.id == @composite_name_entity.id
48
+ end
49
+ end
@@ -0,0 +1,31 @@
1
+ require 'test_helper'
2
+
3
+ class CurrentUsersControllerTest < ActionController::TestCase
4
+ setup do
5
+ @user = users(:one)
6
+ @token = KnockRails3::AuthToken.new(payload: { sub: @user.id }).token
7
+ end
8
+
9
+ def authenticate token: @token
10
+ @request.env['HTTP_AUTHORIZATION'] = "Bearer #{token}"
11
+ end
12
+
13
+ test "responds with 404 if user is not logged in" do
14
+ get :show
15
+ assert_response :not_found
16
+ end
17
+
18
+ test "responds with 200" do
19
+ authenticate
20
+ get :show
21
+ assert_response :success
22
+ end
23
+
24
+ # Run this test twice to validate that it still works
25
+ # when the getter method has already been defined.
26
+ test "responds with 200 #2" do
27
+ authenticate
28
+ get :show
29
+ assert_response :success
30
+ end
31
+ end
@@ -0,0 +1,42 @@
1
+ require 'test_helper'
2
+
3
+ class CustomUnauthorizedEntityControllerTest < ActionController::TestCase
4
+ def valid_auth
5
+ @user = users(:one)
6
+ @token = KnockRails3::AuthToken.new(payload: { sub: @user.id }).token
7
+ @request.env['HTTP_AUTHORIZATION'] = "Bearer #{@token}"
8
+ end
9
+
10
+ def invalid_token_auth
11
+ @token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9'
12
+ @request.env['HTTP_AUTHORIZATION'] = "Bearer #{@token}"
13
+ end
14
+
15
+ def invalid_entity_auth
16
+ @token = KnockRails3::AuthToken.new(payload: { sub: 0 }).token
17
+ @request.env['HTTP_AUTHORIZATION'] = "Bearer #{@token}"
18
+ end
19
+
20
+ test "responds with not found" do
21
+ get :index
22
+ assert_response :not_found
23
+ end
24
+
25
+ test "responds with not found to invalid token" do
26
+ invalid_token_auth
27
+ get :index
28
+ assert_response :not_found
29
+ end
30
+
31
+ test "responds with not found to invalid entity" do
32
+ invalid_entity_auth
33
+ get :index
34
+ assert_response :not_found
35
+ end
36
+
37
+ test "responds with success if authenticated" do
38
+ valid_auth
39
+ get :index
40
+ assert_response :success
41
+ end
42
+ end
@@ -0,0 +1,22 @@
1
+ require 'test_helper'
2
+
3
+ class GuestProtectedControllerTest < ActionController::TestCase
4
+ def setup
5
+ @token = KnockRails3::AuthToken.new(payload: { sub: "1" }).token
6
+ end
7
+
8
+ def authenticate token: @token
9
+ @request.env['HTTP_AUTHORIZATION'] = "Bearer #{token}"
10
+ end
11
+
12
+ test "responds with unauthorized when no token is provided" do
13
+ get :index
14
+ assert_response :unauthorized
15
+ end
16
+
17
+ test "responds with success with a valid token in the header" do
18
+ authenticate
19
+ get :index
20
+ assert_response :success
21
+ end
22
+ end
@@ -0,0 +1,62 @@
1
+ require 'test_helper'
2
+
3
+ class ProtectedResourcesControllerTest < ActionController::TestCase
4
+ def setup
5
+ @user = users(:one)
6
+ @token = KnockRails3::AuthToken.new(payload: { sub: @user.id }).token
7
+ end
8
+
9
+ def authenticate token: @token
10
+ @request.env['HTTP_AUTHORIZATION'] = "Bearer #{token}"
11
+ end
12
+
13
+ test "responds with unauthorized" do
14
+ get :index
15
+ assert_response :unauthorized
16
+ end
17
+
18
+ test "responds with success with valid token in header" do
19
+ authenticate
20
+ get :index
21
+ assert_response :success
22
+ end
23
+
24
+ test "responds with unauthorized with invalid token in header" do
25
+ authenticate token: "invalid"
26
+ get :index
27
+ assert_response :unauthorized
28
+ end
29
+
30
+ test "responds with success with token in url" do
31
+ get :index, params: {token: @token}
32
+ assert_response :success
33
+ end
34
+
35
+ test "responds with unauthorized with invalid token in url" do
36
+ get :index, params: {token: "invalid"}
37
+ assert_response :unauthorized
38
+ end
39
+
40
+ test "has a current_user after authentication" do
41
+ authenticate
42
+ get :index
43
+ assert_response :success
44
+ assert @controller.current_user.id == @user.id
45
+ end
46
+
47
+ test "accepts any prefix in the authorization header" do
48
+ @request.env['HTTP_AUTHORIZATION'] = "Other #{@token}"
49
+
50
+ get :index
51
+
52
+ assert_response :success
53
+ end
54
+
55
+ test "accepts authorization header without prefix" do
56
+ @request.env['HTTP_AUTHORIZATION'] = "#{@token}"
57
+
58
+ get :index
59
+
60
+ assert_response :success
61
+ end
62
+ end
@@ -0,0 +1,19 @@
1
+ require 'test_helper'
2
+ # require 'timecop'
3
+
4
+ module KnockRails3
5
+ class TestNamespacedControllerTest < ActionDispatch::IntegrationTest
6
+
7
+ setup do
8
+ @user = V1::User.first
9
+ end
10
+
11
+ test "allow namespaced models" do
12
+ token = KnockRails3::AuthToken.new(payload: { sub: @user.id }).token
13
+ get v1_test_namespaced_index_url, headers: {'Authorization': "Bearer #{token}"}
14
+ assert_response :ok
15
+ assert_equal @user, @controller.current_v1_user
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,55 @@
1
+ require 'test_helper'
2
+
3
+ class VendorProtectedControllerTest < ActionController::TestCase
4
+ def valid_auth
5
+ @vendor = vendors(:one)
6
+ @token = KnockRails3::AuthToken.new(payload: { sub: @vendor.id }).token
7
+ @request.env['HTTP_AUTHORIZATION'] = "Bearer #{@token}"
8
+ end
9
+
10
+ def invalid_token_auth
11
+ @token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9'
12
+ @request.env['HTTP_AUTHORIZATION'] = "Bearer #{@token}"
13
+ end
14
+
15
+ def invalid_entity_auth
16
+ @token = KnockRails3::AuthToken.new(payload: { sub: 0 }).token
17
+ @request.env['HTTP_AUTHORIZATION'] = "Bearer #{@token}"
18
+ end
19
+
20
+ test "responds with unauthorized" do
21
+ get :index
22
+ assert_response :unauthorized
23
+ end
24
+
25
+ test "responds with unauthorized to invalid token" do
26
+ invalid_token_auth
27
+ get :index
28
+ assert_response :unauthorized
29
+ end
30
+
31
+ test "responds with unauthorized to invalid entity" do
32
+ invalid_entity_auth
33
+ get :index
34
+ assert_response :unauthorized
35
+ end
36
+
37
+ test "responds with success if authenticated" do
38
+ valid_auth
39
+ get :index
40
+ assert_response :success
41
+ end
42
+
43
+ test "has a current_vendor after authentication" do
44
+ valid_auth
45
+ get :index
46
+ assert_response :success
47
+ assert @controller.current_vendor.id == @vendor.id
48
+ end
49
+
50
+ test "raises method missing error appropriately" do
51
+ assert_raises(NoMethodError) do
52
+ get :show, params: {id: 1}
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,22 @@
1
+ require 'test_helper'
2
+
3
+ class VendorTokenControllerTest < ActionController::TestCase
4
+ def setup
5
+ @vendor = vendors(:one)
6
+ end
7
+
8
+ test "responds with 404 if user does not exist" do
9
+ post :create, params: {auth: { email: 'wrong@example.net', password: '' }}
10
+ assert_response :not_found
11
+ end
12
+
13
+ test "responds with 404 if password is invalid" do
14
+ post :create, params: {auth: { email: @vendor.email, password: 'wrong' }}
15
+ assert_response :not_found
16
+ end
17
+
18
+ test "responds with 201" do
19
+ post :create, params: {auth: { email: @vendor.email, password: 'secret' }}
20
+ assert_response :created
21
+ end
22
+ end