doorkeeper-mongodb 5.2.1 → 5.2.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (177) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +12 -27
  3. data/lib/doorkeeper/orm/concerns/mongoid/ownership.rb +1 -1
  4. data/lib/doorkeeper/orm/concerns/mongoid/resource_ownerable.rb +1 -1
  5. data/lib/doorkeeper/orm/mongoid4.rb +18 -0
  6. data/lib/doorkeeper/orm/mongoid5.rb +18 -0
  7. data/lib/doorkeeper/orm/mongoid6.rb +18 -0
  8. data/lib/doorkeeper/orm/mongoid7.rb +18 -0
  9. data/lib/doorkeeper-mongodb/mixins/mongoid/access_grant_mixin.rb +7 -6
  10. data/lib/doorkeeper-mongodb/mixins/mongoid/access_token_mixin.rb +8 -5
  11. data/lib/doorkeeper-mongodb/mixins/mongoid/application_mixin.rb +82 -5
  12. data/lib/doorkeeper-mongodb/mixins/mongoid/base_mixin.rb +0 -8
  13. data/lib/doorkeeper-mongodb/mixins/mongoid/json_serializable.rb +17 -0
  14. data/lib/doorkeeper-mongodb/version.rb +1 -1
  15. data/lib/doorkeeper-mongodb.rb +1 -0
  16. data/spec/controllers/application_metal_controller_spec.rb +4 -4
  17. data/spec/controllers/applications_controller_spec.rb +198 -202
  18. data/spec/controllers/authorizations_controller_spec.rb +32 -31
  19. data/spec/controllers/protected_resources_controller_spec.rb +10 -10
  20. data/spec/controllers/token_info_controller_spec.rb +1 -1
  21. data/spec/controllers/tokens_controller_spec.rb +105 -62
  22. data/spec/doorkeeper/redirect_uri_validator_spec.rb +183 -0
  23. data/spec/{lib → doorkeeper}/server_spec.rb +5 -4
  24. data/spec/{lib → doorkeeper}/stale_records_cleaner_spec.rb +8 -7
  25. data/spec/{version → doorkeeper}/version_spec.rb +3 -3
  26. data/spec/dummy/app/assets/config/manifest.js +0 -0
  27. data/spec/dummy/app/controllers/application_controller.rb +0 -0
  28. data/spec/dummy/app/controllers/custom_authorizations_controller.rb +0 -0
  29. data/spec/dummy/app/controllers/full_protected_resources_controller.rb +0 -0
  30. data/spec/dummy/app/controllers/home_controller.rb +0 -0
  31. data/spec/dummy/app/controllers/metal_controller.rb +0 -0
  32. data/spec/dummy/app/controllers/semi_protected_resources_controller.rb +0 -0
  33. data/spec/dummy/app/helpers/application_helper.rb +0 -0
  34. data/spec/dummy/app/models/user.rb +0 -1
  35. data/spec/dummy/app/views/home/index.html.erb +0 -0
  36. data/spec/dummy/app/views/layouts/application.html.erb +0 -0
  37. data/spec/dummy/config/application.rb +0 -0
  38. data/spec/dummy/config/boot.rb +0 -0
  39. data/spec/dummy/config/database.yml +0 -0
  40. data/spec/dummy/config/environment.rb +0 -0
  41. data/spec/dummy/config/environments/development.rb +0 -0
  42. data/spec/dummy/config/environments/production.rb +0 -0
  43. data/spec/dummy/config/environments/test.rb +0 -0
  44. data/spec/dummy/config/initializers/backtrace_silencers.rb +0 -0
  45. data/spec/dummy/config/initializers/doorkeeper.rb +0 -0
  46. data/spec/dummy/config/initializers/secret_token.rb +0 -0
  47. data/spec/dummy/config/initializers/session_store.rb +0 -0
  48. data/spec/dummy/config/initializers/wrap_parameters.rb +0 -0
  49. data/spec/dummy/config/locales/doorkeeper.en.yml +0 -0
  50. data/spec/dummy/config/mongo.yml +2 -2
  51. data/spec/dummy/config/routes.rb +0 -0
  52. data/spec/dummy/config.ru +0 -0
  53. data/spec/dummy/db/migrate/20111122132257_create_users.rb +0 -0
  54. data/spec/dummy/db/migrate/20120312140401_add_password_to_users.rb +0 -0
  55. data/spec/dummy/db/migrate/20151223192035_create_doorkeeper_tables.rb +0 -0
  56. data/spec/dummy/db/migrate/20151223200000_add_owner_to_application.rb +0 -0
  57. data/spec/dummy/db/migrate/20160320211015_add_previous_refresh_token_to_access_tokens.rb +0 -0
  58. data/spec/dummy/db/migrate/20170822064514_enable_pkce.rb +0 -0
  59. data/spec/dummy/db/migrate/20180210183654_add_confidential_to_applications.rb +0 -0
  60. data/spec/dummy/db/schema.rb +0 -0
  61. data/spec/dummy/public/404.html +0 -0
  62. data/spec/dummy/public/422.html +0 -0
  63. data/spec/dummy/public/500.html +0 -0
  64. data/spec/dummy/public/favicon.ico +0 -0
  65. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/-o/-ofn45zIfO5vx4VD6m6NXesLf6da5usA-Sbw2SVju3o.cache +0 -0
  66. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/0_/0_ouGcG_o_1zmVmBe-tdQYk594LBwVa1bumjrKtdfEw.cache +0 -0
  67. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/2X/2XMMcHbyTSRqh1GkV0xzyIETNtt-zgN6iniziShaKmQ.cache +0 -0
  68. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/4Q/4QQw-pMQ98JIj3xdcFhGBQdysdGPY9rWffRqWMmyjew.cache +0 -0
  69. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/7H/7HjRsym0tTpXFCVhWt6kteMs_-CozKVbr9s5syHm8es.cache +0 -0
  70. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/8B/8BxJyv22HPFHu1se_l2J8qW1N9NzZ16UOWOy1YMSgs4.cache +0 -0
  71. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/8D/8DJG7JfPvShfLLyeSom6NZ_TcQc6QH27tJ7prEWZPfI.cache +0 -0
  72. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/9x/9xjtGv-yKjj62x9uFwBZg8pTSh9ERPAgANwNo9uwIaA.cache +0 -0
  73. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/Bt/BtXO7f0PZ8DNt3TMLYM-zY7LkbWa_KgyB0v1V98M1CQ.cache +0 -0
  74. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/E0/E02f1Q2tutWumMulGCFNfqQNjvEP_hMAK_5E83eWepI.cache +0 -0
  75. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/EZ/EZGpd6pUEJZxO6uT2yIS4Fpn5KX7VDgC9VB3AaemF5Q.cache +0 -0
  76. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/FJ/FJlQpfMtpRbrM5P8A7d2FAelFKwo6GrVs2xks8z5GKU.cache +0 -0
  77. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/L8/L8mOWakJlWKr6MU85rj0V7yGFfNyIR6vE6YXtqArPGw.cache +0 -0
  78. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/Lk/LklIq5hrBAPESXVUpFMwQ9L-1qTj90sPVi6U19_Xi-Q.cache +0 -0
  79. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/MB/MBWRxa9S470ee8suxVKp0jYgv6K6FQOqKGV0kPGQNMs.cache +0 -0
  80. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/Ma/MaK3K1AoZZ4xM5fOOifhROd73hfZJpP4yGbkGdoYtEk.cache +0 -0
  81. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/PA/PAH_jI20wRw12tngeJ-V619c30avNLDu5U9Z-9Pc5SQ.cache +0 -0
  82. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/Ph/Ph7RtH_NvG2I8XpTa8mA8SBQXZDzBPVIh4CH6g7OXJ0.cache +0 -0
  83. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/{eS/eSL1QMz46gKLM0GR6S9fL6uyARPxOImcappZ9_ZtSyg.cache → Pm/PmheG0PGFqDws1qgFOxOyIL-gpMof3Ar9eSRKVLYuik.cache} +0 -0
  84. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/Pn/PnXU_mkG2fLZFm9BwiZ03BZdBTjKuInP-cRXjHS7yJo.cache +0 -0
  85. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/Qs/QsVgO6vM5Yn9oeYTYlrqtyVnK6sdVDWDa083N7zpfHw.cache +0 -0
  86. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/So/So59ksdx2dw-_A3-zFZ6Palr7fROjm7_0JDZb04temw.cache +0 -0
  87. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/Wt/WtXL_iDofLeTH_v0Yf3PM421r9b1V0g-VBvMPeu9KA0.cache +0 -0
  88. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/b6/b6QRH6ZdCc0e6bUWu4qni_kZmptaMgWciO8Jl9q6_p8.cache +0 -0
  89. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/dX/dX6FDdUIy8yBCOoaoXcjf71rX9N_bpiXGJ4Urt32NTk.cache +0 -0
  90. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/jC/jCTZ1jAldKBn4OTANBBmCKzxLrDgok1ur4meoTqlDNg.cache +0 -0
  91. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/qT/qTQIQsmS0Wbbg2JxNn9rxdI5qVOTg5SfyQdaTvr9vLo.cache +0 -0
  92. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/yN/yNOFGOsnHmxXNMkuEAWuL1u3jlmdvrdeoTx6DDJ1in8.cache +0 -0
  93. data/spec/factories.rb +0 -0
  94. data/spec/grape/grape_integration_spec.rb +1 -1
  95. data/spec/helpers/doorkeeper/dashboard_helper_spec.rb +1 -1
  96. data/spec/lib/config_spec.rb +23 -12
  97. data/spec/lib/doorkeeper_spec.rb +4 -4
  98. data/spec/lib/models/expirable_spec.rb +9 -9
  99. data/spec/lib/models/reusable_spec.rb +2 -2
  100. data/spec/lib/models/revocable_spec.rb +4 -7
  101. data/spec/lib/models/scopes_spec.rb +7 -7
  102. data/spec/lib/models/secret_storable_spec.rb +9 -8
  103. data/spec/lib/oauth/authorization/uri_builder_spec.rb +23 -27
  104. data/spec/lib/oauth/authorization_code_request_spec.rb +6 -6
  105. data/spec/lib/oauth/base_request_spec.rb +11 -27
  106. data/spec/lib/oauth/base_response_spec.rb +2 -2
  107. data/spec/lib/oauth/client/credentials_spec.rb +25 -25
  108. data/spec/lib/oauth/client_credentials/creator_spec.rb +89 -91
  109. data/spec/lib/oauth/client_credentials/issuer_spec.rb +84 -86
  110. data/spec/lib/oauth/client_credentials/validation_spec.rb +72 -39
  111. data/spec/lib/oauth/client_credentials_integration_spec.rb +5 -5
  112. data/spec/lib/oauth/client_credentials_request_spec.rb +7 -10
  113. data/spec/lib/oauth/client_spec.rb +8 -8
  114. data/spec/lib/oauth/code_request_spec.rb +5 -5
  115. data/spec/lib/oauth/code_response_spec.rb +4 -4
  116. data/spec/lib/oauth/error_response_spec.rb +6 -5
  117. data/spec/lib/oauth/error_spec.rb +1 -1
  118. data/spec/lib/oauth/forbidden_token_response_spec.rb +2 -2
  119. data/spec/lib/oauth/helpers/scope_checker_spec.rb +37 -37
  120. data/spec/lib/oauth/helpers/unique_token_spec.rb +2 -2
  121. data/spec/lib/oauth/helpers/uri_checker_spec.rb +54 -54
  122. data/spec/lib/oauth/invalid_request_response_spec.rb +6 -6
  123. data/spec/lib/oauth/invalid_token_response_spec.rb +4 -4
  124. data/spec/lib/oauth/password_access_token_request_spec.rb +10 -9
  125. data/spec/lib/oauth/pre_authorization_spec.rb +20 -8
  126. data/spec/lib/oauth/refresh_token_request_spec.rb +10 -10
  127. data/spec/lib/oauth/scopes_spec.rb +14 -14
  128. data/spec/lib/oauth/token_request_spec.rb +9 -9
  129. data/spec/lib/oauth/token_response_spec.rb +5 -5
  130. data/spec/lib/oauth/token_spec.rb +5 -5
  131. data/spec/lib/option_spec.rb +1 -1
  132. data/spec/lib/request/strategy_spec.rb +34 -37
  133. data/spec/lib/secret_storing/base_spec.rb +3 -2
  134. data/spec/lib/secret_storing/bcrypt_spec.rb +2 -1
  135. data/spec/lib/secret_storing/plain_spec.rb +2 -1
  136. data/spec/lib/secret_storing/sha256_hash_spec.rb +2 -1
  137. data/spec/models/doorkeeper/access_grant_spec.rb +7 -9
  138. data/spec/models/doorkeeper/access_token_spec.rb +20 -26
  139. data/spec/models/doorkeeper/application_spec.rb +83 -26
  140. data/spec/requests/applications/applications_request_spec.rb +91 -93
  141. data/spec/requests/applications/authorized_applications_spec.rb +0 -0
  142. data/spec/requests/endpoints/authorization_spec.rb +1 -1
  143. data/spec/requests/endpoints/token_spec.rb +22 -16
  144. data/spec/requests/flows/authorization_code_errors_spec.rb +12 -8
  145. data/spec/requests/flows/authorization_code_spec.rb +108 -79
  146. data/spec/requests/flows/client_credentials_spec.rb +57 -45
  147. data/spec/requests/flows/implicit_grant_errors_spec.rb +0 -0
  148. data/spec/requests/flows/implicit_grant_spec.rb +4 -4
  149. data/spec/requests/flows/password_spec.rb +253 -213
  150. data/spec/requests/flows/refresh_token_spec.rb +53 -39
  151. data/spec/requests/flows/revoke_token_spec.rb +24 -24
  152. data/spec/requests/flows/skip_authorization_spec.rb +1 -1
  153. data/spec/requests/protected_resources/metal_spec.rb +2 -2
  154. data/spec/requests/protected_resources/private_api_spec.rb +0 -0
  155. data/spec/routing/custom_controller_routes_spec.rb +1 -1
  156. data/spec/routing/default_routes_spec.rb +1 -1
  157. data/spec/routing/scoped_routes_spec.rb +1 -1
  158. data/spec/spec_helper.rb +0 -0
  159. data/spec/spec_helper_integration.rb +0 -0
  160. data/spec/support/dependencies/factory_bot.rb +0 -0
  161. data/spec/support/doorkeeper_rspec.rb +0 -0
  162. data/spec/support/helpers/access_token_request_helper.rb +0 -0
  163. data/spec/support/helpers/authorization_request_helper.rb +0 -0
  164. data/spec/support/helpers/config_helper.rb +0 -0
  165. data/spec/support/helpers/model_helper.rb +0 -0
  166. data/spec/support/helpers/request_spec_helper.rb +1 -13
  167. data/spec/support/helpers/url_helper.rb +2 -2
  168. data/spec/support/orm/active_record.rb +0 -0
  169. data/spec/support/orm/mongoid4.rb +1 -1
  170. data/spec/support/orm/mongoid5.rb +1 -1
  171. data/spec/support/orm/mongoid6.rb +1 -1
  172. data/spec/support/orm/mongoid7.rb +1 -1
  173. data/spec/support/shared/controllers_shared_context.rb +5 -38
  174. data/spec/support/shared/hashing_shared_context.rb +4 -0
  175. data/spec/support/shared/models_shared_examples.rb +6 -6
  176. metadata +154 -167
  177. data/spec/dummy/log/test.log +0 -6108
@@ -2,315 +2,355 @@
2
2
 
3
3
  require "spec_helper"
4
4
 
5
- describe "Resource Owner Password Credentials Flow not set up" do
6
- before do
7
- client_exists
8
- create_resource_owner
9
- end
5
+ RSpec.describe "Resource Owner Password Credentials Flow" do
6
+ context "when not setup properly" do
7
+ before do
8
+ client_exists
9
+ create_resource_owner
10
+ end
10
11
 
11
- context "with valid user credentials" do
12
- it "does not issue new token" do
13
- expect do
14
- post password_token_endpoint_url(client: @client, resource_owner: @resource_owner)
15
- end.to_not(change { Doorkeeper::AccessToken.count })
12
+ context "with valid user credentials" do
13
+ it "does not issue new token" do
14
+ expect do
15
+ post password_token_endpoint_url(client: @client, resource_owner: @resource_owner)
16
+ end.not_to(change { Doorkeeper::AccessToken.count })
17
+ end
16
18
  end
17
19
  end
18
- end
19
20
 
20
- describe "Resource Owner Password Credentials Flow" do
21
- let(:client_attributes) { { redirect_uri: nil } }
21
+ context "when grant type configured" do
22
+ let(:client_attributes) { { redirect_uri: nil } }
22
23
 
23
- before do
24
- config_is_set(:grant_flows, ["password"])
25
- config_is_set(:resource_owner_from_credentials) { User.authenticate! params[:username], params[:password] }
26
- client_exists(client_attributes)
27
- create_resource_owner
28
- end
24
+ before do
25
+ config_is_set(:grant_flows, ["password"])
26
+ config_is_set(:resource_owner_from_credentials) { User.authenticate! params[:username], params[:password] }
27
+ client_exists(client_attributes)
28
+ create_resource_owner
29
+ end
29
30
 
30
- context "with valid user credentials" do
31
- context "with non-confidential/public client" do
32
- let(:client_attributes) { { confidential: false } }
31
+ context "with valid user credentials" do
32
+ context "with confidential client authorized using Basic auth" do
33
+ it "issues a new token" do
34
+ expect do
35
+ post password_token_endpoint_url(
36
+ resource_owner: @resource_owner,
37
+ ), headers: { "HTTP_AUTHORIZATION" => basic_auth_header_for_client(@client) }
38
+ end.to(change { Doorkeeper::AccessToken.count })
33
39
 
34
- context "when configured to check application supported grant flow" do
35
- before do
36
- Doorkeeper.configuration.instance_variable_set(
37
- :@allow_grant_flow_for_client,
38
- ->(_grant_flow, client) { client.name == "admin" },
40
+ token = Doorkeeper::AccessToken.first
41
+ expect(token.application_id).to eq(@client.id)
42
+
43
+ expect(json_response).to match(
44
+ "access_token" => token.token,
45
+ "expires_in" => an_instance_of(Integer),
46
+ "token_type" => "Bearer",
47
+ "created_at" => an_instance_of(Integer),
39
48
  )
40
49
  end
50
+ end
41
51
 
42
- scenario "forbids the request when doesn't satisfy condition" do
43
- @client.update(name: "sample app")
52
+ context "with non-confidential/public client" do
53
+ let(:client_attributes) { { confidential: false } }
44
54
 
45
- expect do
46
- post password_token_endpoint_url(
47
- client_id: @client.uid,
48
- client_secret: "foobar",
49
- resource_owner: @resource_owner,
55
+ context "when configured to check application supported grant flow" do
56
+ before do
57
+ Doorkeeper.configuration.instance_variable_set(
58
+ :@allow_grant_flow_for_client,
59
+ ->(_grant_flow, client) { client.name == "admin" },
50
60
  )
51
- end.not_to(change { Doorkeeper::AccessToken.count })
61
+ end
52
62
 
53
- expect(response.status).to eq(401)
54
- should_have_json "error", "invalid_client"
55
- end
63
+ scenario "forbids the request when doesn't satisfy condition" do
64
+ @client.update(name: "sample app")
65
+
66
+ expect do
67
+ post password_token_endpoint_url(
68
+ client_id: @client.uid,
69
+ client_secret: "foobar",
70
+ resource_owner: @resource_owner,
71
+ )
72
+ end.not_to(change { Doorkeeper::AccessToken.count })
56
73
 
57
- scenario "allows the request when satisfies condition" do
58
- @client.update(name: "admin")
74
+ expect(response.status).to eq(401)
75
+ expect(json_response).to match(
76
+ "error" => "invalid_client",
77
+ "error_description" => an_instance_of(String),
78
+ )
79
+ end
59
80
 
60
- expect do
61
- post password_token_endpoint_url(client_id: @client.uid, resource_owner: @resource_owner)
62
- end.to change { Doorkeeper::AccessToken.count }.by(1)
81
+ scenario "allows the request when satisfies condition" do
82
+ @client.update(name: "admin")
63
83
 
64
- token = Doorkeeper::AccessToken.first
84
+ expect do
85
+ post password_token_endpoint_url(client_id: @client.uid, resource_owner: @resource_owner)
86
+ end.to change { Doorkeeper::AccessToken.count }.by(1)
87
+
88
+ token = Doorkeeper::AccessToken.first
89
+ expect(token.application_id).to eq(@client.id)
65
90
 
66
- expect(token.application_id).to eq @client.id
67
- should_have_json "access_token", token.token
91
+ expect(json_response).to include("access_token" => token.token)
92
+ end
68
93
  end
69
- end
70
94
 
71
- context "when client_secret absent" do
72
- it "should issue new token" do
73
- expect do
74
- post password_token_endpoint_url(client_id: @client.uid, resource_owner: @resource_owner)
75
- end.to change { Doorkeeper::AccessToken.count }.by(1)
95
+ context "when client_secret absent" do
96
+ it "issues a new token" do
97
+ expect do
98
+ post password_token_endpoint_url(client_id: @client.uid, resource_owner: @resource_owner)
99
+ end.to change { Doorkeeper::AccessToken.count }.by(1)
76
100
 
77
- token = Doorkeeper::AccessToken.first
101
+ token = Doorkeeper::AccessToken.first
78
102
 
79
- expect(token.application_id).to eq @client.id
80
- should_have_json "access_token", token.token
103
+ expect(token.application_id).to eq(@client.id)
104
+ expect(json_response).to include("access_token" => token.token)
105
+ end
106
+ end
107
+
108
+ context "when client_secret present" do
109
+ it "issues a new token" do
110
+ expect do
111
+ post password_token_endpoint_url(client: @client, resource_owner: @resource_owner)
112
+ end.to change { Doorkeeper::AccessToken.count }.by(1)
113
+
114
+ token = Doorkeeper::AccessToken.first
115
+
116
+ expect(token.application_id).to eq(@client.id)
117
+ expect(json_response).to include("access_token" => token.token)
118
+ end
119
+
120
+ context "when client_secret incorrect" do
121
+ it "doesn't issue new token" do
122
+ expect do
123
+ post password_token_endpoint_url(
124
+ client_id: @client.uid,
125
+ client_secret: "foobar",
126
+ resource_owner: @resource_owner,
127
+ )
128
+ end.not_to(change { Doorkeeper::AccessToken.count })
129
+
130
+ expect(response.status).to eq(401)
131
+ expect(json_response).to include(
132
+ "error" => "invalid_client",
133
+ "error_description" => an_instance_of(String),
134
+ )
135
+ end
136
+ end
81
137
  end
82
138
  end
83
139
 
84
- context "when client_secret present" do
85
- it "should issue new token" do
140
+ context "with confidential/private client" do
141
+ it "issues a new token" do
86
142
  expect do
87
143
  post password_token_endpoint_url(client: @client, resource_owner: @resource_owner)
88
144
  end.to change { Doorkeeper::AccessToken.count }.by(1)
89
145
 
90
146
  token = Doorkeeper::AccessToken.first
91
147
 
92
- expect(token.application_id).to eq @client.id
93
- should_have_json "access_token", token.token
148
+ expect(token.application_id).to eq(@client.id)
149
+ expect(json_response).to include("access_token" => token.token)
94
150
  end
95
151
 
96
- context "when client_secret incorrect" do
97
- it "should not issue new token" do
152
+ context "when client_secret absent" do
153
+ it "doesn't issue new token" do
98
154
  expect do
99
- post password_token_endpoint_url(
100
- client_id: @client.uid,
101
- client_secret: "foobar",
102
- resource_owner: @resource_owner,
103
- )
155
+ post password_token_endpoint_url(client_id: @client.uid, resource_owner: @resource_owner)
104
156
  end.not_to(change { Doorkeeper::AccessToken.count })
105
157
 
106
158
  expect(response.status).to eq(401)
107
- should_have_json "error", "invalid_client"
159
+ expect(json_response).to match(
160
+ "error" => "invalid_client",
161
+ "error_description" => an_instance_of(String),
162
+ )
108
163
  end
109
164
  end
110
165
  end
111
- end
112
166
 
113
- context "with confidential/private client" do
114
- it "should issue new token" do
167
+ it "issues new token without client credentials" do
115
168
  expect do
116
- post password_token_endpoint_url(client: @client, resource_owner: @resource_owner)
117
- end.to change { Doorkeeper::AccessToken.count }.by(1)
169
+ post password_token_endpoint_url(resource_owner: @resource_owner)
170
+ end.to(change { Doorkeeper::AccessToken.count }.by(1))
118
171
 
119
172
  token = Doorkeeper::AccessToken.first
120
173
 
121
- expect(token.application_id).to eq @client.id
122
- should_have_json "access_token", token.token
123
- end
124
-
125
- context "when client_secret absent" do
126
- it "should not issue new token" do
127
- expect do
128
- post password_token_endpoint_url(client_id: @client.uid, resource_owner: @resource_owner)
129
- end.not_to(change { Doorkeeper::AccessToken.count })
130
-
131
- expect(response.status).to eq(401)
132
- should_have_json "error", "invalid_client"
133
- end
174
+ expect(token.application_id).to be_nil
175
+ expect(json_response).to include("access_token" => token.token)
134
176
  end
135
- end
136
177
 
137
- it "should issue new token without client credentials" do
138
- expect do
139
- post password_token_endpoint_url(resource_owner: @resource_owner)
140
- end.to(change { Doorkeeper::AccessToken.count }.by(1))
178
+ it "issues a refresh token if enabled" do
179
+ config_is_set(:refresh_token_enabled, true)
141
180
 
142
- token = Doorkeeper::AccessToken.first
181
+ post password_token_endpoint_url(client: @client, resource_owner: @resource_owner)
143
182
 
144
- expect(token.application_id).to be_nil
145
- should_have_json "access_token", token.token
146
- end
183
+ token = Doorkeeper::AccessToken.first
184
+ expect(json_response).to include("refresh_token" => token.refresh_token)
185
+ end
147
186
 
148
- it "should issue a refresh token if enabled" do
149
- config_is_set(:refresh_token_enabled, true)
187
+ it "returns the same token if it is still accessible" do
188
+ allow(Doorkeeper.configuration).to receive(:reuse_access_token).and_return(true)
150
189
 
151
- post password_token_endpoint_url(client: @client, resource_owner: @resource_owner)
190
+ client_is_authorized(@client, @resource_owner)
152
191
 
153
- token = Doorkeeper::AccessToken.first
192
+ post password_token_endpoint_url(client: @client, resource_owner: @resource_owner)
154
193
 
155
- should_have_json "refresh_token", token.refresh_token
156
- end
194
+ expect(Doorkeeper::AccessToken.count).to be(1)
195
+ expect(json_response).to include("access_token" => Doorkeeper::AccessToken.first.token)
196
+ end
157
197
 
158
- it "should return the same token if it is still accessible" do
159
- allow(Doorkeeper.configuration).to receive(:reuse_access_token).and_return(true)
198
+ context "with valid, default scope" do
199
+ before do
200
+ default_scopes_exist :public
201
+ end
160
202
 
161
- client_is_authorized(@client, @resource_owner)
203
+ it "issues new token" do
204
+ expect do
205
+ post password_token_endpoint_url(client: @client, resource_owner: @resource_owner, scope: "public")
206
+ end.to change { Doorkeeper::AccessToken.count }.by(1)
162
207
 
163
- post password_token_endpoint_url(client: @client, resource_owner: @resource_owner)
208
+ token = Doorkeeper::AccessToken.first
164
209
 
165
- expect(Doorkeeper::AccessToken.count).to be(1)
166
- should_have_json "access_token", Doorkeeper::AccessToken.first.token
210
+ expect(token.application_id).to eq(@client.id)
211
+ expect(json_response).to include(
212
+ "access_token" => token.token,
213
+ "scope" => "public",
214
+ )
215
+ end
216
+ end
167
217
  end
168
218
 
169
- context "with valid, default scope" do
219
+ context "when application scopes are present and differs from configured default scopes and no scope is passed" do
170
220
  before do
171
221
  default_scopes_exist :public
222
+ @client.update(scopes: "abc")
172
223
  end
173
224
 
174
- it "should issue new token" do
225
+ it "issues new token without any scope" do
175
226
  expect do
176
- post password_token_endpoint_url(client: @client, resource_owner: @resource_owner, scope: "public")
227
+ post password_token_endpoint_url(client: @client, resource_owner: @resource_owner)
177
228
  end.to change { Doorkeeper::AccessToken.count }.by(1)
178
229
 
179
230
  token = Doorkeeper::AccessToken.first
180
231
 
181
- expect(token.application_id).to eq @client.id
182
- should_have_json "access_token", token.token
183
- should_have_json "scope", "public"
232
+ expect(token.application_id).to eq(@client.id)
233
+ expect(token.scopes).to be_empty
234
+ expect(json_response).to include("access_token" => token.token)
235
+ expect(json_response).not_to include("scope")
184
236
  end
185
237
  end
186
- end
187
238
 
188
- context "when application scopes are present and differs from configured default scopes and no scope is passed" do
189
- before do
190
- default_scopes_exist :public
191
- @client.update(scopes: "abc")
192
- end
239
+ context "when application scopes contain some of the default scopes and no scope is passed" do
240
+ before do
241
+ @client.update(scopes: "read write public")
242
+ end
193
243
 
194
- it "issues new token without any scope" do
195
- expect do
196
- post password_token_endpoint_url(client: @client, resource_owner: @resource_owner)
197
- end.to change { Doorkeeper::AccessToken.count }.by(1)
244
+ it "issues new token with one default scope that are present in application scopes" do
245
+ default_scopes_exist :public, :admin
198
246
 
199
- token = Doorkeeper::AccessToken.first
247
+ expect do
248
+ post password_token_endpoint_url(client: @client, resource_owner: @resource_owner)
249
+ end.to change { Doorkeeper::AccessToken.count }.by(1)
200
250
 
201
- expect(token.application_id).to eq @client.id
202
- expect(token.scopes).to be_empty
203
- should_have_json "access_token", token.token
204
- should_not_have_json "scope"
205
- end
206
- end
251
+ token = Doorkeeper::AccessToken.first
207
252
 
208
- context "when application scopes contain some of the default scopes and no scope is passed" do
209
- before do
210
- @client.update(scopes: "read write public")
211
- end
253
+ expect(token.application_id).to eq(@client.id)
254
+ expect(json_response).to include(
255
+ "access_token" => token.token,
256
+ "scope" => "public",
257
+ )
258
+ end
212
259
 
213
- it "issues new token with one default scope that are present in application scopes" do
214
- default_scopes_exist :public, :admin
260
+ it "issues new token with multiple default scopes that are present in application scopes" do
261
+ default_scopes_exist :public, :read, :update
215
262
 
216
- expect do
217
- post password_token_endpoint_url(client: @client, resource_owner: @resource_owner)
218
- end.to change { Doorkeeper::AccessToken.count }.by(1)
263
+ expect do
264
+ post password_token_endpoint_url(client: @client, resource_owner: @resource_owner)
265
+ end.to change { Doorkeeper::AccessToken.count }.by(1)
219
266
 
220
- token = Doorkeeper::AccessToken.first
267
+ token = Doorkeeper::AccessToken.first
221
268
 
222
- expect(token.application_id).to eq @client.id
223
- should_have_json "access_token", token.token
224
- should_have_json "scope", "public"
269
+ expect(token.application_id).to eq(@client.id)
270
+ expect(json_response).to include(
271
+ "access_token" => token.token,
272
+ "scope" => "public read",
273
+ )
274
+ end
225
275
  end
226
276
 
227
- it "issues new token with multiple default scopes that are present in application scopes" do
228
- default_scopes_exist :public, :read, :update
229
-
230
- expect do
231
- post password_token_endpoint_url(client: @client, resource_owner: @resource_owner)
232
- end.to change { Doorkeeper::AccessToken.count }.by(1)
233
-
234
- token = Doorkeeper::AccessToken.first
277
+ context "with invalid scopes" do
278
+ subject do
279
+ post password_token_endpoint_url(
280
+ client: @client,
281
+ resource_owner: @resource_owner,
282
+ scope: "random",
283
+ )
284
+ end
235
285
 
236
- expect(token.application_id).to eq @client.id
237
- should_have_json "access_token", token.token
238
- should_have_json "scope", "public read"
239
- end
240
- end
286
+ it "doesn't issue new token" do
287
+ expect { subject }.not_to(change { Doorkeeper::AccessToken.count })
288
+ end
241
289
 
242
- context "with invalid scopes" do
243
- subject do
244
- post password_token_endpoint_url(
245
- client: @client,
246
- resource_owner: @resource_owner,
247
- scope: "random",
248
- )
249
- end
290
+ it "returns invalid_scope error" do
291
+ subject
250
292
 
251
- it "should not issue new token" do
252
- expect { subject }.to_not(change { Doorkeeper::AccessToken.count })
253
- end
293
+ expect(json_response).to include(
294
+ "error" => "invalid_scope",
295
+ "error_description" => translated_error_message(:invalid_scope),
296
+ )
254
297
 
255
- it "should return invalid_scope error" do
256
- subject
257
- should_have_json "error", "invalid_scope"
258
- should_have_json "error_description", translated_error_message(:invalid_scope)
259
- should_not_have_json "access_token"
298
+ expect(json_response).not_to include("access_token")
260
299
 
261
- expect(response.status).to eq(400)
300
+ expect(response.status).to eq(400)
301
+ end
262
302
  end
263
- end
264
303
 
265
- context "with invalid user credentials" do
266
- it "should not issue new token with bad password" do
267
- expect do
268
- post password_token_endpoint_url(
269
- client: @client,
270
- resource_owner_username: @resource_owner.name,
271
- resource_owner_password: "wrongpassword",
272
- )
273
- end.to_not(change { Doorkeeper::AccessToken.count })
274
- end
304
+ context "with invalid user credentials" do
305
+ it "doesn't issue new token with bad password" do
306
+ expect do
307
+ post password_token_endpoint_url(
308
+ client: @client,
309
+ resource_owner_username: @resource_owner.name,
310
+ resource_owner_password: "wrongpassword",
311
+ )
312
+ end.not_to(change { Doorkeeper::AccessToken.count })
313
+ end
275
314
 
276
- it "should not issue new token without credentials" do
277
- expect do
278
- post password_token_endpoint_url(client: @client)
279
- end.to_not(change { Doorkeeper::AccessToken.count })
280
- end
315
+ it "doesn't issue new token without credentials" do
316
+ expect do
317
+ post password_token_endpoint_url(client: @client)
318
+ end.not_to(change { Doorkeeper::AccessToken.count })
319
+ end
281
320
 
282
- it "should not issue new token if resource_owner_from_credentials returned false or nil" do
283
- config_is_set(:resource_owner_from_credentials) { false }
321
+ it "doesn't issue new token if resource_owner_from_credentials returned false or nil" do
322
+ config_is_set(:resource_owner_from_credentials) { false }
284
323
 
285
- expect do
286
- post password_token_endpoint_url(client: @client)
287
- end.to_not(change { Doorkeeper::AccessToken.count })
324
+ expect do
325
+ post password_token_endpoint_url(client: @client)
326
+ end.not_to(change { Doorkeeper::AccessToken.count })
288
327
 
289
- config_is_set(:resource_owner_from_credentials) { nil }
328
+ config_is_set(:resource_owner_from_credentials) { nil }
290
329
 
291
- expect do
292
- post password_token_endpoint_url(client: @client)
293
- end.to_not(change { Doorkeeper::AccessToken.count })
330
+ expect do
331
+ post password_token_endpoint_url(client: @client)
332
+ end.not_to(change { Doorkeeper::AccessToken.count })
333
+ end
294
334
  end
295
- end
296
335
 
297
- context "with invalid confidential client credentials" do
298
- it "should not issue new token with bad client credentials" do
299
- expect do
300
- post password_token_endpoint_url(
301
- client_id: @client.uid,
302
- client_secret: "bad_secret",
303
- resource_owner: @resource_owner,
304
- )
305
- end.to_not(change { Doorkeeper::AccessToken.count })
336
+ context "with invalid confidential client credentials" do
337
+ it "doesn't issue new token with bad client credentials" do
338
+ expect do
339
+ post password_token_endpoint_url(
340
+ client_id: @client.uid,
341
+ client_secret: "bad_secret",
342
+ resource_owner: @resource_owner,
343
+ )
344
+ end.not_to(change { Doorkeeper::AccessToken.count })
345
+ end
306
346
  end
307
- end
308
347
 
309
- context "with invalid public client id" do
310
- it "should not issue new token with bad client id" do
311
- expect do
312
- post password_token_endpoint_url(client_id: "bad_id", resource_owner: @resource_owner)
313
- end.to_not(change { Doorkeeper::AccessToken.count })
348
+ context "with invalid public client id" do
349
+ it "doesn't issue new token with bad client id" do
350
+ expect do
351
+ post password_token_endpoint_url(client_id: "bad_id", resource_owner: @resource_owner)
352
+ end.not_to(change { Doorkeeper::AccessToken.count })
353
+ end
314
354
  end
315
355
  end
316
356
  end