doorkeeper-mongodb 5.2.1 → 5.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (87) hide show
  1. checksums.yaml +4 -4
  2. data/lib/doorkeeper-mongodb.rb +1 -0
  3. data/lib/doorkeeper-mongodb/mixins/mongoid/access_grant_mixin.rb +1 -0
  4. data/lib/doorkeeper-mongodb/mixins/mongoid/access_token_mixin.rb +1 -0
  5. data/lib/doorkeeper-mongodb/mixins/mongoid/application_mixin.rb +76 -0
  6. data/lib/doorkeeper-mongodb/mixins/mongoid/base_mixin.rb +0 -8
  7. data/lib/doorkeeper-mongodb/mixins/mongoid/json_serializable.rb +17 -0
  8. data/lib/doorkeeper-mongodb/version.rb +1 -1
  9. data/spec/controllers/application_metal_controller_spec.rb +4 -4
  10. data/spec/controllers/applications_controller_spec.rb +198 -202
  11. data/spec/controllers/authorizations_controller_spec.rb +32 -31
  12. data/spec/controllers/protected_resources_controller_spec.rb +10 -10
  13. data/spec/controllers/token_info_controller_spec.rb +1 -1
  14. data/spec/controllers/tokens_controller_spec.rb +105 -62
  15. data/spec/doorkeeper/redirect_uri_validator_spec.rb +183 -0
  16. data/spec/{lib → doorkeeper}/server_spec.rb +5 -4
  17. data/spec/{lib → doorkeeper}/stale_records_cleaner_spec.rb +8 -7
  18. data/spec/{version → doorkeeper}/version_spec.rb +3 -3
  19. data/spec/dummy/log/test.log +4220 -4184
  20. data/spec/dummy/tmp/cache/assets/sprockets/v4.0.0/{eS/eSL1QMz46gKLM0GR6S9fL6uyARPxOImcappZ9_ZtSyg.cache → Pm/PmheG0PGFqDws1qgFOxOyIL-gpMof3Ar9eSRKVLYuik.cache} +0 -0
  21. data/spec/grape/grape_integration_spec.rb +1 -1
  22. data/spec/helpers/doorkeeper/dashboard_helper_spec.rb +1 -1
  23. data/spec/lib/config_spec.rb +23 -12
  24. data/spec/lib/doorkeeper_spec.rb +4 -4
  25. data/spec/lib/models/expirable_spec.rb +9 -9
  26. data/spec/lib/models/reusable_spec.rb +2 -2
  27. data/spec/lib/models/revocable_spec.rb +4 -7
  28. data/spec/lib/models/scopes_spec.rb +7 -7
  29. data/spec/lib/models/secret_storable_spec.rb +9 -8
  30. data/spec/lib/oauth/authorization/uri_builder_spec.rb +23 -27
  31. data/spec/lib/oauth/authorization_code_request_spec.rb +6 -6
  32. data/spec/lib/oauth/base_request_spec.rb +11 -27
  33. data/spec/lib/oauth/base_response_spec.rb +2 -2
  34. data/spec/lib/oauth/client/credentials_spec.rb +25 -25
  35. data/spec/lib/oauth/client_credentials/creator_spec.rb +89 -91
  36. data/spec/lib/oauth/client_credentials/issuer_spec.rb +84 -86
  37. data/spec/lib/oauth/client_credentials/validation_spec.rb +72 -39
  38. data/spec/lib/oauth/client_credentials_integration_spec.rb +5 -5
  39. data/spec/lib/oauth/client_credentials_request_spec.rb +7 -10
  40. data/spec/lib/oauth/client_spec.rb +8 -8
  41. data/spec/lib/oauth/code_request_spec.rb +5 -5
  42. data/spec/lib/oauth/code_response_spec.rb +4 -4
  43. data/spec/lib/oauth/error_response_spec.rb +6 -5
  44. data/spec/lib/oauth/error_spec.rb +1 -1
  45. data/spec/lib/oauth/forbidden_token_response_spec.rb +2 -2
  46. data/spec/lib/oauth/helpers/scope_checker_spec.rb +37 -37
  47. data/spec/lib/oauth/helpers/unique_token_spec.rb +2 -2
  48. data/spec/lib/oauth/helpers/uri_checker_spec.rb +54 -54
  49. data/spec/lib/oauth/invalid_request_response_spec.rb +6 -6
  50. data/spec/lib/oauth/invalid_token_response_spec.rb +4 -4
  51. data/spec/lib/oauth/password_access_token_request_spec.rb +10 -9
  52. data/spec/lib/oauth/pre_authorization_spec.rb +20 -8
  53. data/spec/lib/oauth/refresh_token_request_spec.rb +10 -10
  54. data/spec/lib/oauth/scopes_spec.rb +14 -14
  55. data/spec/lib/oauth/token_request_spec.rb +9 -9
  56. data/spec/lib/oauth/token_response_spec.rb +5 -5
  57. data/spec/lib/oauth/token_spec.rb +5 -5
  58. data/spec/lib/option_spec.rb +1 -1
  59. data/spec/lib/request/strategy_spec.rb +34 -37
  60. data/spec/lib/secret_storing/base_spec.rb +3 -2
  61. data/spec/lib/secret_storing/bcrypt_spec.rb +2 -1
  62. data/spec/lib/secret_storing/plain_spec.rb +2 -1
  63. data/spec/lib/secret_storing/sha256_hash_spec.rb +2 -1
  64. data/spec/models/doorkeeper/access_grant_spec.rb +7 -9
  65. data/spec/models/doorkeeper/access_token_spec.rb +20 -26
  66. data/spec/models/doorkeeper/application_spec.rb +83 -26
  67. data/spec/requests/applications/applications_request_spec.rb +91 -93
  68. data/spec/requests/endpoints/authorization_spec.rb +1 -1
  69. data/spec/requests/endpoints/token_spec.rb +22 -16
  70. data/spec/requests/flows/authorization_code_errors_spec.rb +12 -8
  71. data/spec/requests/flows/authorization_code_spec.rb +108 -79
  72. data/spec/requests/flows/client_credentials_spec.rb +57 -45
  73. data/spec/requests/flows/implicit_grant_spec.rb +4 -4
  74. data/spec/requests/flows/password_spec.rb +253 -213
  75. data/spec/requests/flows/refresh_token_spec.rb +53 -39
  76. data/spec/requests/flows/revoke_token_spec.rb +24 -24
  77. data/spec/requests/flows/skip_authorization_spec.rb +1 -1
  78. data/spec/requests/protected_resources/metal_spec.rb +2 -2
  79. data/spec/routing/custom_controller_routes_spec.rb +1 -1
  80. data/spec/routing/default_routes_spec.rb +1 -1
  81. data/spec/routing/scoped_routes_spec.rb +1 -1
  82. data/spec/support/helpers/request_spec_helper.rb +1 -13
  83. data/spec/support/helpers/url_helper.rb +2 -2
  84. data/spec/support/shared/controllers_shared_context.rb +5 -38
  85. data/spec/support/shared/hashing_shared_context.rb +4 -0
  86. data/spec/support/shared/models_shared_examples.rb +6 -6
  87. metadata +13 -10
@@ -2,7 +2,7 @@
2
2
 
3
3
  require "spec_helper"
4
4
 
5
- describe Doorkeeper, "configuration option DSL" do
5
+ RSpec.describe Doorkeeper::Config::Option do
6
6
  class Extension
7
7
  def self.configure(&block)
8
8
  @config = Config::Builder.new(Config.new, &block).build
@@ -2,53 +2,50 @@
2
2
 
3
3
  require "spec_helper"
4
4
 
5
- module Doorkeeper
6
- module Request
7
- describe Strategy do
8
- let(:server) { double }
9
- subject(:strategy) { Strategy.new(server) }
10
-
11
- describe :initialize do
12
- it "sets the server attribute" do
13
- expect(strategy.server).to eq server
14
- end
15
- end
16
-
17
- describe :request do
18
- it "requires an implementation" do
19
- expect { strategy.request }.to raise_exception NotImplementedError
20
- end
21
- end
5
+ RSpec.describe Doorkeeper::Request::Strategy do
6
+ subject(:strategy) { described_class.new(server) }
22
7
 
23
- describe "a sample Strategy subclass" do
24
- let(:fake_request) { double }
8
+ let(:server) { double }
25
9
 
26
- let(:strategy_class) do
27
- subclass = Class.new(Strategy) do
28
- class << self
29
- attr_accessor :fake_request
30
- end
10
+ describe "#initialize" do
11
+ it "sets the server attribute" do
12
+ expect(strategy.server).to eq server
13
+ end
14
+ end
31
15
 
32
- def request
33
- self.class.fake_request
34
- end
35
- end
16
+ describe "#request" do
17
+ it "requires an implementation" do
18
+ expect { strategy.request }.to raise_exception NotImplementedError
19
+ end
20
+ end
36
21
 
37
- subclass.fake_request = fake_request
38
- subclass
39
- end
22
+ describe "a sample Strategy subclass" do
23
+ subject(:strategy) { strategy_class.new(server) }
40
24
 
41
- subject(:strategy) { strategy_class.new(server) }
25
+ let(:fake_request) { double }
42
26
 
43
- it "provides a request implementation" do
44
- expect(strategy.request).to eq fake_request
27
+ let(:strategy_class) do
28
+ subclass = Class.new(described_class) do
29
+ class << self
30
+ attr_accessor :fake_request
45
31
  end
46
32
 
47
- it "authorizes the request" do
48
- expect(fake_request).to receive :authorize
49
- strategy.authorize
33
+ def request
34
+ self.class.fake_request
50
35
  end
51
36
  end
37
+
38
+ subclass.fake_request = fake_request
39
+ subclass
40
+ end
41
+
42
+ it "provides a request implementation" do
43
+ expect(strategy.request).to eq fake_request
44
+ end
45
+
46
+ it "authorizes the request" do
47
+ expect(fake_request).to receive :authorize
48
+ strategy.authorize
52
49
  end
53
50
  end
54
51
  end
@@ -2,10 +2,11 @@
2
2
 
3
3
  require "spec_helper"
4
4
 
5
- describe ::Doorkeeper::SecretStoring::Base do
6
- let(:instance) { double("instance", token: "foo") }
5
+ RSpec.describe ::Doorkeeper::SecretStoring::Base do
7
6
  subject { described_class }
8
7
 
8
+ let(:instance) { double("instance", token: "foo") }
9
+
9
10
  describe "#transform_secret" do
10
11
  it "raises" do
11
12
  expect { subject.transform_secret("foo") }.to raise_error(NotImplementedError)
@@ -3,8 +3,9 @@
3
3
  require "spec_helper"
4
4
  require "bcrypt"
5
5
 
6
- describe ::Doorkeeper::SecretStoring::BCrypt do
6
+ RSpec.describe ::Doorkeeper::SecretStoring::BCrypt do
7
7
  subject { described_class }
8
+
8
9
  let(:instance) { double("instance", token: "foo") }
9
10
 
10
11
  describe "#transform_secret" do
@@ -2,8 +2,9 @@
2
2
 
3
3
  require "spec_helper"
4
4
 
5
- describe ::Doorkeeper::SecretStoring::Plain do
5
+ RSpec.describe ::Doorkeeper::SecretStoring::Plain do
6
6
  subject { described_class }
7
+
7
8
  let(:instance) { double("instance", token: "foo") }
8
9
 
9
10
  describe "#transform_secret" do
@@ -2,8 +2,9 @@
2
2
 
3
3
  require "spec_helper"
4
4
 
5
- describe ::Doorkeeper::SecretStoring::Sha256Hash do
5
+ RSpec.describe ::Doorkeeper::SecretStoring::Sha256Hash do
6
6
  subject { described_class }
7
+
7
8
  let(:instance) { double("instance") }
8
9
 
9
10
  let(:hash_function) do
@@ -2,11 +2,7 @@
2
2
 
3
3
  require "spec_helper"
4
4
 
5
- describe Doorkeeper::AccessGrant do
6
- let(:resource_owner) { FactoryBot.create(:resource_owner) }
7
- let(:client) { FactoryBot.build_stubbed(:application) }
8
- let(:clazz) { Doorkeeper::AccessGrant }
9
-
5
+ RSpec.describe Doorkeeper::AccessGrant do
10
6
  subject do
11
7
  FactoryBot.build(
12
8
  :access_grant,
@@ -16,6 +12,10 @@ describe Doorkeeper::AccessGrant do
16
12
  )
17
13
  end
18
14
 
15
+ let(:resource_owner) { FactoryBot.create(:resource_owner) }
16
+ let(:client) { FactoryBot.build_stubbed(:application) }
17
+ let(:clazz) { described_class }
18
+
19
19
  it { expect(subject).to be_valid }
20
20
 
21
21
  it_behaves_like "an accessible token"
@@ -31,6 +31,7 @@ describe Doorkeeper::AccessGrant do
31
31
  resource_owner_id: resource_owner.id,
32
32
  resource_owner_type: resource_owner.class.name
33
33
  end
34
+
34
35
  include_context "with token hashing enabled"
35
36
 
36
37
  it "holds a volatile plaintext token when created" do
@@ -143,10 +144,7 @@ describe Doorkeeper::AccessGrant do
143
144
  FactoryBot.create :access_grant, default_attributes
144
145
 
145
146
  described_class.revoke_all_for(application.id, resource_owner)
146
-
147
- described_class.all.each do |token|
148
- expect(token).to be_revoked
149
- end
147
+ expect(described_class.all).to all(be_revoked)
150
148
  end
151
149
 
152
150
  it "matches application" do
@@ -27,6 +27,7 @@ RSpec.describe Doorkeeper::AccessToken do
27
27
 
28
28
  context "with hashing enabled" do
29
29
  let(:token) { FactoryBot.create :access_token }
30
+
30
31
  include_context "with token hashing enabled"
31
32
 
32
33
  it "holds a volatile plaintext token when created" do
@@ -385,14 +386,15 @@ RSpec.describe Doorkeeper::AccessToken do
385
386
  end
386
387
  let(:access_token1) { FactoryBot.create :access_token, default_attributes }
387
388
 
388
- context "the second token has the same owner and same app" do
389
+ context "when the second token has the same owner and same app" do
389
390
  let(:access_token2) { FactoryBot.create :access_token, default_attributes }
391
+
390
392
  it "success" do
391
- expect(access_token1.same_credential?(access_token2)).to be_truthy
393
+ expect(access_token1).to be_same_credential(access_token2)
392
394
  end
393
395
  end
394
396
 
395
- context "the second token has same owner and different app" do
397
+ context "when the second token has same owner and different app" do
396
398
  let(:other_application) { FactoryBot.create :application }
397
399
  let(:access_token2) do
398
400
  FactoryBot.create :access_token,
@@ -401,12 +403,12 @@ RSpec.describe Doorkeeper::AccessToken do
401
403
  resource_owner_type: resource_owner.class.name
402
404
  end
403
405
 
404
- it "fail" do
405
- expect(access_token1.same_credential?(access_token2)).to be_falsey
406
+ it "fails" do
407
+ expect(access_token1).not_to be_same_credential(access_token2)
406
408
  end
407
409
  end
408
410
 
409
- context "the second token has different owner and different app" do
411
+ context "when the second token has different owner and different app" do
410
412
  let(:other_application) { FactoryBot.create :application }
411
413
  let(:access_token2) do
412
414
  FactoryBot.create :access_token,
@@ -414,50 +416,50 @@ RSpec.describe Doorkeeper::AccessToken do
414
416
  resource_owner_id: resource_owner.id + 1
415
417
  end
416
418
 
417
- it "fail" do
418
- expect(access_token1.same_credential?(access_token2)).to be_falsey
419
+ it "fails" do
420
+ expect(access_token1).not_to be_same_credential(access_token2)
419
421
  end
420
422
  end
421
423
 
422
- context "the second token has different owner and same app" do
424
+ context "when the second token has different owner and same app" do
423
425
  let(:access_token2) do
424
426
  FactoryBot.create :access_token,
425
427
  application: application,
426
428
  resource_owner_id: resource_owner.id + 1
427
429
  end
428
430
 
429
- it "fail" do
430
- expect(access_token1.same_credential?(access_token2)).to be_falsey
431
+ it "fails" do
432
+ expect(access_token1).not_to be_same_credential(access_token2)
431
433
  end
432
434
  end
433
435
  end
434
436
  end
435
437
 
436
438
  describe "#acceptable?" do
437
- context "a token that is not accessible" do
439
+ context "when token is not accessible" do
438
440
  let(:token) { FactoryBot.create(:access_token, created_at: 6.hours.ago) }
439
441
 
440
- it "should return false" do
442
+ it "returns false" do
441
443
  expect(token.acceptable?(nil)).to be false
442
444
  end
443
445
  end
444
446
 
445
- context "a token that has the incorrect scopes" do
447
+ context "when token has the incorrect scopes" do
446
448
  let(:token) { FactoryBot.create(:access_token) }
447
449
 
448
- it "should return false" do
450
+ it "returns false" do
449
451
  expect(token.acceptable?(["public"])).to be false
450
452
  end
451
453
  end
452
454
 
453
- context "a token is acceptable with the correct scopes" do
455
+ context "when token is acceptable with the correct scopes" do
454
456
  let(:token) do
455
457
  token = FactoryBot.create(:access_token)
456
458
  token[:scopes] = "public"
457
459
  token
458
460
  end
459
461
 
460
- it "should return true" do
462
+ it "returns true" do
461
463
  expect(token.acceptable?(["public"])).to be true
462
464
  end
463
465
  end
@@ -477,9 +479,7 @@ RSpec.describe Doorkeeper::AccessToken do
477
479
  it "revokes all tokens for given application and resource owner" do
478
480
  FactoryBot.create :access_token, default_attributes
479
481
  described_class.revoke_all_for application.id, resource_owner
480
- described_class.all.each do |token|
481
- expect(token).to be_revoked
482
- end
482
+ expect(described_class.all).to all(be_revoked)
483
483
  end
484
484
 
485
485
  it "matches application" do
@@ -529,12 +529,6 @@ RSpec.describe Doorkeeper::AccessToken do
529
529
  expect(last_token).to eq(token)
530
530
  end
531
531
 
532
- it "accepts resource owner as object" do
533
- token = FactoryBot.create :access_token, default_attributes
534
- last_token = described_class.matching_token_for(application, resource_owner, scopes)
535
- expect(last_token).to eq(token)
536
- end
537
-
538
532
  it "accepts nil as resource owner" do
539
533
  token = FactoryBot.create :access_token,
540
534
  default_attributes.merge(resource_owner_id: nil, resource_owner_type: nil)
@@ -3,7 +3,7 @@
3
3
  require "spec_helper"
4
4
  require "bcrypt"
5
5
 
6
- describe Doorkeeper::Application do
6
+ RSpec.describe Doorkeeper::Application do
7
7
  let(:require_owner) { Doorkeeper.config.instance_variable_set("@confirm_application_owner", true) }
8
8
  let(:unset_require_owner) { Doorkeeper.config.instance_variable_set("@confirm_application_owner", false) }
9
9
  let(:new_application) { FactoryBot.build(:application) }
@@ -83,7 +83,7 @@ describe Doorkeeper::Application do
83
83
  expect(new_application).not_to be_valid
84
84
  end
85
85
 
86
- context "application_owner is enabled" do
86
+ context "when application_owner is enabled" do
87
87
  before do
88
88
  Doorkeeper.configure do
89
89
  orm DOORKEEPER_ORM
@@ -91,8 +91,8 @@ describe Doorkeeper::Application do
91
91
  end
92
92
  end
93
93
 
94
- context "application owner is not required" do
95
- before(:each) do
94
+ context "when application owner is not required" do
95
+ before do
96
96
  unset_require_owner
97
97
  end
98
98
 
@@ -101,8 +101,8 @@ describe Doorkeeper::Application do
101
101
  end
102
102
  end
103
103
 
104
- context "application owner is required" do
105
- before(:each) do
104
+ context "when application owner is required" do
105
+ before do
106
106
  require_owner
107
107
  @owner = FactoryBot.build_stubbed(:doorkeeper_testing_user)
108
108
  end
@@ -118,10 +118,11 @@ describe Doorkeeper::Application do
118
118
  end
119
119
  end
120
120
 
121
- context "redirect URI" do
121
+ describe "redirect URI" do
122
122
  context "when grant flows allow blank redirect URI" do
123
123
  before do
124
124
  Doorkeeper.configure do
125
+ orm DOORKEEPER_ORM
125
126
  grant_flows %w[password client_credentials]
126
127
  end
127
128
  end
@@ -136,6 +137,7 @@ describe Doorkeeper::Application do
136
137
  context "when grant flows require redirect URI" do
137
138
  before do
138
139
  Doorkeeper.configure do
140
+ orm DOORKEEPER_ORM
139
141
  grant_flows %w[password client_credentials authorization_code]
140
142
  end
141
143
  end
@@ -150,6 +152,7 @@ describe Doorkeeper::Application do
150
152
  context "when blank URI option disabled" do
151
153
  before do
152
154
  Doorkeeper.configure do
155
+ orm DOORKEEPER_ORM
153
156
  grant_flows %w[password client_credentials]
154
157
  allow_blank_redirect_uri false
155
158
  end
@@ -179,6 +182,7 @@ describe Doorkeeper::Application do
179
182
  # will always be true
180
183
  before do
181
184
  Doorkeeper.configure do
185
+ orm DOORKEEPER_ORM
182
186
  hash_application_secrets using: "Doorkeeper::SecretStoring::BCrypt"
183
187
  end
184
188
  end
@@ -218,13 +222,13 @@ describe Doorkeeper::Application do
218
222
  end
219
223
 
220
224
  describe "destroy related models on cascade" do
221
- before(:each) do
225
+ before do
222
226
  new_application.save
223
227
  end
224
228
 
225
229
  let(:resource_owner) { FactoryBot.create(:resource_owner) }
226
230
 
227
- it "should destroy its access grants" do
231
+ it "destroys its access grants" do
228
232
  FactoryBot.create(
229
233
  :access_grant,
230
234
  application: new_application,
@@ -235,7 +239,7 @@ describe Doorkeeper::Application do
235
239
  expect { new_application.destroy }.to change { Doorkeeper::AccessGrant.count }.by(-1)
236
240
  end
237
241
 
238
- it "should destroy its access tokens" do
242
+ it "destroys its access tokens" do
239
243
  FactoryBot.create(:access_token, application: new_application)
240
244
  FactoryBot.create(:access_token, application: new_application, revoked_at: Time.now.utc)
241
245
  expect do
@@ -264,13 +268,14 @@ describe Doorkeeper::Application do
264
268
 
265
269
  describe "#redirect_uri=" do
266
270
  context "when array of valid redirect_uris" do
267
- it "should join by newline" do
271
+ it "joins by newline" do
268
272
  new_application.redirect_uri = ["http://localhost/callback1", "http://localhost/callback2"]
269
273
  expect(new_application.redirect_uri).to eq("http://localhost/callback1\nhttp://localhost/callback2")
270
274
  end
271
275
  end
276
+
272
277
  context "when string of valid redirect_uris" do
273
- it "should store as-is" do
278
+ it "stores as-is" do
274
279
  new_application.redirect_uri = "http://localhost/callback1\nhttp://localhost/callback2"
275
280
  expect(new_application.redirect_uri).to eq("http://localhost/callback1\nhttp://localhost/callback2")
276
281
  end
@@ -280,7 +285,7 @@ describe Doorkeeper::Application do
280
285
  describe "#renew_secret" do
281
286
  let(:app) { FactoryBot.create :application }
282
287
 
283
- it "should generate a new secret" do
288
+ it "generates a new secret" do
284
289
  old_secret = app.secret
285
290
  app.renew_secret
286
291
  expect(old_secret).not_to eq(app.secret)
@@ -372,8 +377,9 @@ describe Doorkeeper::Application do
372
377
  authenticated = described_class.by_uid_and_secret(app.uid, app.secret)
373
378
  expect(authenticated).to eq(app)
374
379
  end
380
+
375
381
  context "when secret is wrong" do
376
- it "should not find the application" do
382
+ it "does not find the application" do
377
383
  app = FactoryBot.create :application
378
384
  authenticated = described_class.by_uid_and_secret(app.uid, "bad")
379
385
  expect(authenticated).to eq(nil)
@@ -383,14 +389,15 @@ describe Doorkeeper::Application do
383
389
 
384
390
  context "when application is public/non-confidential" do
385
391
  context "when secret is blank" do
386
- it "should find the application" do
392
+ it "finds the application" do
387
393
  app = FactoryBot.create :application, confidential: false
388
394
  authenticated = described_class.by_uid_and_secret(app.uid, nil)
389
395
  expect(authenticated).to eq(app)
390
396
  end
391
397
  end
398
+
392
399
  context "when secret is wrong" do
393
- it "should not find the application" do
400
+ it "does not find the application" do
394
401
  app = FactoryBot.create :application, confidential: false
395
402
  authenticated = described_class.by_uid_and_secret(app.uid, "bad")
396
403
  expect(authenticated).to eq(nil)
@@ -404,11 +411,13 @@ describe Doorkeeper::Application do
404
411
 
405
412
  context "when application is private/confidential" do
406
413
  let(:confidential) { true }
414
+
407
415
  it { expect(subject).to eq(true) }
408
416
  end
409
417
 
410
418
  context "when application is public/non-confidential" do
411
419
  let(:confidential) { false }
420
+
412
421
  it { expect(subject).to eq(false) }
413
422
  end
414
423
  end
@@ -421,16 +430,7 @@ describe Doorkeeper::Application do
421
430
  .to receive(:application_secret_strategy).and_return(Doorkeeper::SecretStoring::Plain)
422
431
  end
423
432
 
424
- it "includes plaintext secret" do
425
- expect(app.as_json).to include("secret" => "123123123")
426
- end
427
-
428
- it "respects custom options" do
429
- expect(app.as_json(except: :secret)).not_to include("secret")
430
- expect(app.as_json(only: :id)).to match("id" => app.id)
431
- end
432
-
433
- # AR specific
433
+ # AR specific feature
434
434
  if DOORKEEPER_ORM == :active_record
435
435
  it "correctly works with #to_json" do
436
436
  ActiveRecord::Base.include_root_in_json = true
@@ -438,5 +438,62 @@ describe Doorkeeper::Application do
438
438
  ActiveRecord::Base.include_root_in_json = false
439
439
  end
440
440
  end
441
+
442
+ context "when called without authorized resource owner" do
443
+ it "includes minimal set of attributes" do
444
+ expect(app.as_json).to match(
445
+ "id" => app.id,
446
+ "name" => app.name,
447
+ "created_at" => anything,
448
+ )
449
+ end
450
+
451
+ it "includes application UID if it's public" do
452
+ app = FactoryBot.create :application, secret: "123123123", confidential: false
453
+
454
+ expect(app.as_json).to match(
455
+ "id" => app.id,
456
+ "name" => app.name,
457
+ "created_at" => anything,
458
+ "uid" => app.uid,
459
+ )
460
+ end
461
+
462
+ it "respects custom options" do
463
+ expect(app.as_json(except: :id)).not_to include("id")
464
+ expect(app.as_json(only: %i[name created_at secret]))
465
+ .to match(
466
+ "name" => app.name,
467
+ "created_at" => anything,
468
+ )
469
+ end
470
+ end
471
+
472
+ context "when called with authorized resource owner" do
473
+ let(:owner) { FactoryBot.create(:doorkeeper_testing_user) }
474
+ let(:other_owner) { FactoryBot.create(:doorkeeper_testing_user) }
475
+ let(:app) { FactoryBot.create(:application, secret: "123123123", owner: owner) }
476
+
477
+ before do
478
+ Doorkeeper.configure do
479
+ orm DOORKEEPER_ORM
480
+ enable_application_owner confirmation: false
481
+ end
482
+ end
483
+
484
+ it "includes all the attributes" do
485
+ expect(app.as_json(current_resource_owner: owner))
486
+ .to include(
487
+ "secret" => "123123123",
488
+ "redirect_uri" => app.redirect_uri,
489
+ "uid" => app.uid,
490
+ )
491
+ end
492
+
493
+ it "doesn't include unsafe attributes if current owner isn't the same as owner" do
494
+ expect(app.as_json(current_resource_owner: other_owner))
495
+ .not_to include("redirect_uri")
496
+ end
497
+ end
441
498
  end
442
499
  end