grape 1.8.0 → 2.0.0

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.
Files changed (140) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +15 -0
  3. data/README.md +19 -22
  4. data/UPGRADING.md +35 -0
  5. data/grape.gemspec +1 -4
  6. data/lib/grape/dsl/desc.rb +1 -1
  7. data/lib/grape/dsl/inside_route.rb +9 -9
  8. data/lib/grape/endpoint.rb +9 -1
  9. data/lib/grape/exceptions/missing_group_type.rb +1 -1
  10. data/lib/grape/exceptions/unsupported_group_type.rb +1 -1
  11. data/lib/grape/http/headers.rb +12 -2
  12. data/lib/grape/middleware/auth/strategies.rb +1 -2
  13. data/lib/grape/middleware/error.rb +4 -4
  14. data/lib/grape/middleware/formatter.rb +5 -5
  15. data/lib/grape/railtie.rb +9 -0
  16. data/lib/grape/request.rb +8 -2
  17. data/lib/grape/router/route.rb +1 -1
  18. data/lib/grape/validations/validators/base.rb +1 -1
  19. data/lib/grape/validations/validators/values_validator.rb +2 -2
  20. data/lib/grape/version.rb +1 -1
  21. data/lib/grape.rb +15 -2
  22. metadata +8 -243
  23. data/spec/grape/api/custom_validations_spec.rb +0 -213
  24. data/spec/grape/api/deeply_included_options_spec.rb +0 -56
  25. data/spec/grape/api/defines_boolean_in_params_spec.rb +0 -38
  26. data/spec/grape/api/documentation_spec.rb +0 -59
  27. data/spec/grape/api/inherited_helpers_spec.rb +0 -114
  28. data/spec/grape/api/instance_spec.rb +0 -103
  29. data/spec/grape/api/invalid_format_spec.rb +0 -45
  30. data/spec/grape/api/namespace_parameters_in_route_spec.rb +0 -38
  31. data/spec/grape/api/nested_helpers_spec.rb +0 -50
  32. data/spec/grape/api/optional_parameters_in_route_spec.rb +0 -43
  33. data/spec/grape/api/parameters_modification_spec.rb +0 -41
  34. data/spec/grape/api/patch_method_helpers_spec.rb +0 -79
  35. data/spec/grape/api/recognize_path_spec.rb +0 -21
  36. data/spec/grape/api/required_parameters_in_route_spec.rb +0 -37
  37. data/spec/grape/api/required_parameters_with_invalid_method_spec.rb +0 -26
  38. data/spec/grape/api/routes_with_requirements_spec.rb +0 -59
  39. data/spec/grape/api/shared_helpers_exactly_one_of_spec.rb +0 -41
  40. data/spec/grape/api/shared_helpers_spec.rb +0 -36
  41. data/spec/grape/api_remount_spec.rb +0 -509
  42. data/spec/grape/api_spec.rb +0 -4356
  43. data/spec/grape/dsl/callbacks_spec.rb +0 -45
  44. data/spec/grape/dsl/desc_spec.rb +0 -98
  45. data/spec/grape/dsl/headers_spec.rb +0 -62
  46. data/spec/grape/dsl/helpers_spec.rb +0 -100
  47. data/spec/grape/dsl/inside_route_spec.rb +0 -531
  48. data/spec/grape/dsl/logger_spec.rb +0 -24
  49. data/spec/grape/dsl/middleware_spec.rb +0 -60
  50. data/spec/grape/dsl/parameters_spec.rb +0 -180
  51. data/spec/grape/dsl/request_response_spec.rb +0 -225
  52. data/spec/grape/dsl/routing_spec.rb +0 -275
  53. data/spec/grape/dsl/settings_spec.rb +0 -261
  54. data/spec/grape/dsl/validations_spec.rb +0 -55
  55. data/spec/grape/endpoint/declared_spec.rb +0 -846
  56. data/spec/grape/endpoint_spec.rb +0 -1085
  57. data/spec/grape/entity_spec.rb +0 -336
  58. data/spec/grape/exceptions/base_spec.rb +0 -81
  59. data/spec/grape/exceptions/body_parse_errors_spec.rb +0 -185
  60. data/spec/grape/exceptions/invalid_accept_header_spec.rb +0 -358
  61. data/spec/grape/exceptions/invalid_formatter_spec.rb +0 -15
  62. data/spec/grape/exceptions/invalid_response_spec.rb +0 -11
  63. data/spec/grape/exceptions/invalid_versioner_option_spec.rb +0 -15
  64. data/spec/grape/exceptions/missing_group_type_spec.rb +0 -17
  65. data/spec/grape/exceptions/missing_mime_type_spec.rb +0 -17
  66. data/spec/grape/exceptions/missing_option_spec.rb +0 -15
  67. data/spec/grape/exceptions/unknown_options_spec.rb +0 -15
  68. data/spec/grape/exceptions/unknown_validator_spec.rb +0 -15
  69. data/spec/grape/exceptions/unsupported_group_type_spec.rb +0 -19
  70. data/spec/grape/exceptions/validation_errors_spec.rb +0 -92
  71. data/spec/grape/exceptions/validation_spec.rb +0 -19
  72. data/spec/grape/extensions/param_builders/hash_spec.rb +0 -83
  73. data/spec/grape/extensions/param_builders/hash_with_indifferent_access_spec.rb +0 -105
  74. data/spec/grape/extensions/param_builders/hashie/mash_spec.rb +0 -79
  75. data/spec/grape/grape_spec.rb +0 -9
  76. data/spec/grape/integration/global_namespace_function_spec.rb +0 -29
  77. data/spec/grape/integration/rack_sendfile_spec.rb +0 -48
  78. data/spec/grape/integration/rack_spec.rb +0 -51
  79. data/spec/grape/loading_spec.rb +0 -44
  80. data/spec/grape/middleware/auth/base_spec.rb +0 -31
  81. data/spec/grape/middleware/auth/dsl_spec.rb +0 -60
  82. data/spec/grape/middleware/auth/strategies_spec.rb +0 -120
  83. data/spec/grape/middleware/base_spec.rb +0 -221
  84. data/spec/grape/middleware/error_spec.rb +0 -85
  85. data/spec/grape/middleware/exception_spec.rb +0 -294
  86. data/spec/grape/middleware/formatter_spec.rb +0 -461
  87. data/spec/grape/middleware/globals_spec.rb +0 -30
  88. data/spec/grape/middleware/stack_spec.rb +0 -155
  89. data/spec/grape/middleware/versioner/accept_version_header_spec.rb +0 -122
  90. data/spec/grape/middleware/versioner/header_spec.rb +0 -345
  91. data/spec/grape/middleware/versioner/param_spec.rb +0 -171
  92. data/spec/grape/middleware/versioner/path_spec.rb +0 -62
  93. data/spec/grape/middleware/versioner_spec.rb +0 -21
  94. data/spec/grape/named_api_spec.rb +0 -19
  95. data/spec/grape/parser_spec.rb +0 -86
  96. data/spec/grape/path_spec.rb +0 -252
  97. data/spec/grape/presenters/presenter_spec.rb +0 -71
  98. data/spec/grape/request_spec.rb +0 -126
  99. data/spec/grape/util/inheritable_setting_spec.rb +0 -242
  100. data/spec/grape/util/inheritable_values_spec.rb +0 -79
  101. data/spec/grape/util/reverse_stackable_values_spec.rb +0 -134
  102. data/spec/grape/util/stackable_values_spec.rb +0 -128
  103. data/spec/grape/util/strict_hash_configuration_spec.rb +0 -38
  104. data/spec/grape/validations/attributes_doc_spec.rb +0 -153
  105. data/spec/grape/validations/instance_behaivour_spec.rb +0 -43
  106. data/spec/grape/validations/multiple_attributes_iterator_spec.rb +0 -38
  107. data/spec/grape/validations/params_scope_spec.rb +0 -1420
  108. data/spec/grape/validations/single_attribute_iterator_spec.rb +0 -56
  109. data/spec/grape/validations/types/array_coercer_spec.rb +0 -33
  110. data/spec/grape/validations/types/primitive_coercer_spec.rb +0 -150
  111. data/spec/grape/validations/types/set_coercer_spec.rb +0 -32
  112. data/spec/grape/validations/types_spec.rb +0 -111
  113. data/spec/grape/validations/validators/all_or_none_spec.rb +0 -162
  114. data/spec/grape/validations/validators/allow_blank_spec.rb +0 -575
  115. data/spec/grape/validations/validators/at_least_one_of_spec.rb +0 -205
  116. data/spec/grape/validations/validators/base_spec.rb +0 -38
  117. data/spec/grape/validations/validators/coerce_spec.rb +0 -1261
  118. data/spec/grape/validations/validators/default_spec.rb +0 -463
  119. data/spec/grape/validations/validators/exactly_one_of_spec.rb +0 -233
  120. data/spec/grape/validations/validators/except_values_spec.rb +0 -192
  121. data/spec/grape/validations/validators/mutual_exclusion_spec.rb +0 -214
  122. data/spec/grape/validations/validators/presence_spec.rb +0 -315
  123. data/spec/grape/validations/validators/regexp_spec.rb +0 -161
  124. data/spec/grape/validations/validators/same_as_spec.rb +0 -57
  125. data/spec/grape/validations/validators/values_spec.rb +0 -733
  126. data/spec/grape/validations/validators/zh-CN.yml +0 -10
  127. data/spec/grape/validations_spec.rb +0 -2030
  128. data/spec/integration/eager_load/eager_load_spec.rb +0 -15
  129. data/spec/integration/multi_json/json_spec.rb +0 -7
  130. data/spec/integration/multi_xml/xml_spec.rb +0 -7
  131. data/spec/shared/deprecated_class_examples.rb +0 -16
  132. data/spec/shared/versioning_examples.rb +0 -215
  133. data/spec/spec_helper.rb +0 -52
  134. data/spec/support/basic_auth_encode_helpers.rb +0 -11
  135. data/spec/support/chunks.rb +0 -14
  136. data/spec/support/content_type_helpers.rb +0 -15
  137. data/spec/support/endpoint_faker.rb +0 -25
  138. data/spec/support/file_streamer.rb +0 -13
  139. data/spec/support/integer_helpers.rb +0 -13
  140. data/spec/support/versioned_helpers.rb +0 -55
@@ -1,83 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- describe Grape::Extensions::Hash::ParamBuilder do
4
- subject { Class.new(Grape::API) }
5
-
6
- def app
7
- subject
8
- end
9
-
10
- describe 'in an endpoint' do
11
- describe '#params' do
12
- before do
13
- subject.params do
14
- build_with Grape::Extensions::Hash::ParamBuilder # rubocop:disable RSpec/DescribedClass
15
- end
16
-
17
- subject.get do
18
- params.class
19
- end
20
- end
21
-
22
- it 'is of type Hash' do
23
- get '/'
24
- expect(last_response.status).to eq(200)
25
- expect(last_response.body).to eq('Hash')
26
- end
27
- end
28
- end
29
-
30
- describe 'in an api' do
31
- before do
32
- subject.send(:include, Grape::Extensions::Hash::ParamBuilder) # rubocop:disable RSpec/DescribedClass
33
- end
34
-
35
- describe '#params' do
36
- before do
37
- subject.get do
38
- params.class
39
- end
40
- end
41
-
42
- it 'is Hash' do
43
- get '/'
44
- expect(last_response.status).to eq(200)
45
- expect(last_response.body).to eq('Hash')
46
- end
47
- end
48
-
49
- it 'symbolizes params keys' do
50
- subject.params do
51
- optional :a, type: Hash do
52
- optional :b, type: Hash do
53
- optional :c, type: String
54
- end
55
- optional :d, type: Array
56
- end
57
- end
58
-
59
- subject.get '/foo' do
60
- [params[:a][:b][:c], params[:a][:d]]
61
- end
62
-
63
- get '/foo', 'a' => { b: { c: 'bar' }, 'd' => ['foo'] }
64
- expect(last_response.status).to eq(200)
65
- expect(last_response.body).to eq('["bar", ["foo"]]')
66
- end
67
-
68
- it 'symbolizes the params' do
69
- subject.params do
70
- build_with Grape::Extensions::Hash::ParamBuilder # rubocop:disable RSpec/DescribedClass
71
- requires :a, type: String
72
- end
73
-
74
- subject.get '/foo' do
75
- [params[:a], params['a']]
76
- end
77
-
78
- get '/foo', a: 'bar'
79
- expect(last_response.status).to eq(200)
80
- expect(last_response.body).to eq('["bar", nil]')
81
- end
82
- end
83
- end
@@ -1,105 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- describe Grape::Extensions::ActiveSupport::HashWithIndifferentAccess::ParamBuilder do
4
- subject { Class.new(Grape::API) }
5
-
6
- def app
7
- subject
8
- end
9
-
10
- describe 'in an endpoint' do
11
- describe '#params' do
12
- before do
13
- subject.params do
14
- build_with Grape::Extensions::ActiveSupport::HashWithIndifferentAccess::ParamBuilder # rubocop:disable RSpec/DescribedClass
15
- end
16
-
17
- subject.get do
18
- params.class
19
- end
20
- end
21
-
22
- it 'is of type Hash' do
23
- get '/'
24
- expect(last_response.status).to eq(200)
25
- expect(last_response.body).to eq('ActiveSupport::HashWithIndifferentAccess')
26
- end
27
- end
28
- end
29
-
30
- describe 'in an api' do
31
- before do
32
- subject.send(:include, Grape::Extensions::ActiveSupport::HashWithIndifferentAccess::ParamBuilder) # rubocop:disable RSpec/DescribedClass
33
- end
34
-
35
- describe '#params' do
36
- before do
37
- subject.get do
38
- params.class
39
- end
40
- end
41
-
42
- it 'is a Hash' do
43
- get '/'
44
- expect(last_response.status).to eq(200)
45
- expect(last_response.body).to eq('ActiveSupport::HashWithIndifferentAccess')
46
- end
47
-
48
- it 'parses sub hash params' do
49
- subject.params do
50
- build_with Grape::Extensions::ActiveSupport::HashWithIndifferentAccess::ParamBuilder # rubocop:disable RSpec/DescribedClass
51
-
52
- optional :a, type: Hash do
53
- optional :b, type: Hash do
54
- optional :c, type: String
55
- end
56
- optional :d, type: Array
57
- end
58
- end
59
-
60
- subject.get '/foo' do
61
- [params[:a]['b'][:c], params['a'][:d]]
62
- end
63
-
64
- get '/foo', a: { b: { c: 'bar' }, d: ['foo'] }
65
- expect(last_response.status).to eq(200)
66
- expect(last_response.body).to eq('["bar", ["foo"]]')
67
- end
68
-
69
- it 'params are indifferent to symbol or string keys' do
70
- subject.params do
71
- build_with Grape::Extensions::ActiveSupport::HashWithIndifferentAccess::ParamBuilder # rubocop:disable RSpec/DescribedClass
72
- optional :a, type: Hash do
73
- optional :b, type: Hash do
74
- optional :c, type: String
75
- end
76
- optional :d, type: Array
77
- end
78
- end
79
-
80
- subject.get '/foo' do
81
- [params[:a]['b'][:c], params['a'][:d]]
82
- end
83
-
84
- get '/foo', 'a' => { b: { c: 'bar' }, 'd' => ['foo'] }
85
- expect(last_response.status).to eq(200)
86
- expect(last_response.body).to eq('["bar", ["foo"]]')
87
- end
88
-
89
- it 'responds to string keys' do
90
- subject.params do
91
- build_with Grape::Extensions::ActiveSupport::HashWithIndifferentAccess::ParamBuilder # rubocop:disable RSpec/DescribedClass
92
- requires :a, type: String
93
- end
94
-
95
- subject.get '/foo' do
96
- [params[:a], params['a']]
97
- end
98
-
99
- get '/foo', a: 'bar'
100
- expect(last_response.status).to eq(200)
101
- expect(last_response.body).to eq('["bar", "bar"]')
102
- end
103
- end
104
- end
105
- end
@@ -1,79 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- describe Grape::Extensions::Hashie::Mash::ParamBuilder do
4
- subject { Class.new(Grape::API) }
5
-
6
- def app
7
- subject
8
- end
9
-
10
- describe 'in an endpoint' do
11
- describe '#params' do
12
- before do
13
- subject.params do
14
- build_with Grape::Extensions::Hashie::Mash::ParamBuilder # rubocop:disable RSpec/DescribedClass
15
- end
16
-
17
- subject.get do
18
- params.class
19
- end
20
- end
21
-
22
- it 'is of type Hashie::Mash' do
23
- get '/'
24
- expect(last_response.status).to eq(200)
25
- expect(last_response.body).to eq('Hashie::Mash')
26
- end
27
- end
28
- end
29
-
30
- describe 'in an api' do
31
- before do
32
- subject.send(:include, Grape::Extensions::Hashie::Mash::ParamBuilder) # rubocop:disable RSpec/DescribedClass
33
- end
34
-
35
- describe '#params' do
36
- before do
37
- subject.get do
38
- params.class
39
- end
40
- end
41
-
42
- it 'is Hashie::Mash' do
43
- get '/'
44
- expect(last_response.status).to eq(200)
45
- expect(last_response.body).to eq('Hashie::Mash')
46
- end
47
- end
48
-
49
- context 'in a nested namespace api' do
50
- before do
51
- subject.namespace :foo do
52
- get do
53
- params.class
54
- end
55
- end
56
- end
57
-
58
- it 'is Hashie::Mash' do
59
- get '/foo'
60
- expect(last_response.status).to eq(200)
61
- expect(last_response.body).to eq('Hashie::Mash')
62
- end
63
- end
64
-
65
- it 'is indifferent to key or symbol access' do
66
- subject.params do
67
- build_with Grape::Extensions::Hashie::Mash::ParamBuilder # rubocop:disable RSpec/DescribedClass
68
- requires :a, type: String
69
- end
70
- subject.get '/foo' do
71
- [params[:a], params['a']]
72
- end
73
-
74
- get '/foo', a: 'bar'
75
- expect(last_response.status).to eq(200)
76
- expect(last_response.body).to eq('["bar", "bar"]')
77
- end
78
- end
79
- end
@@ -1,9 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe Grape do
4
- describe '.config' do
5
- subject { described_class.config }
6
-
7
- it { is_expected.to eq(param_builder: Grape::Extensions::ActiveSupport::HashWithIndifferentAccess::ParamBuilder) }
8
- end
9
- end
@@ -1,29 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # see https://github.com/ruby-grape/grape/issues/1348
4
-
5
- def namespace
6
- raise
7
- end
8
-
9
- describe Grape::API do
10
- subject do
11
- Class.new(Grape::API) do
12
- format :json
13
- get do
14
- { ok: true }
15
- end
16
- end
17
- end
18
-
19
- def app
20
- subject
21
- end
22
-
23
- context 'with a global namespace function' do
24
- it 'works' do
25
- get '/'
26
- expect(last_response.status).to eq 200
27
- end
28
- end
29
- end
@@ -1,48 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- describe Rack::Sendfile do
4
- subject do
5
- content_object = file_object
6
- app = Class.new(Grape::API) do
7
- use Rack::Sendfile
8
- format :json
9
- get do
10
- if content_object.is_a?(String)
11
- sendfile content_object
12
- else
13
- stream content_object
14
- end
15
- end
16
- end
17
-
18
- options = {
19
- method: 'GET',
20
- 'HTTP_X_SENDFILE_TYPE' => 'X-Accel-Redirect',
21
- 'HTTP_X_ACCEL_MAPPING' => '/accel/mapping/=/replaced/'
22
- }
23
- env = Rack::MockRequest.env_for('/', options)
24
- app.call(env)
25
- end
26
-
27
- context 'when calling sendfile' do
28
- let(:file_object) do
29
- '/accel/mapping/some/path'
30
- end
31
-
32
- it 'contains Sendfile headers' do
33
- headers = subject[1]
34
- expect(headers).to include('X-Accel-Redirect')
35
- end
36
- end
37
-
38
- context 'when streaming non file content' do
39
- let(:file_object) do
40
- double(:file_object, each: nil)
41
- end
42
-
43
- it 'not contains Sendfile headers' do
44
- headers = subject[1]
45
- expect(headers).not_to include('X-Accel-Redirect')
46
- end
47
- end
48
- end
@@ -1,51 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- describe Rack do
4
- it 'correctly populates params from a Tempfile' do
5
- input = Tempfile.new 'rubbish'
6
- begin
7
- app = Class.new(Grape::API) do
8
- format :json
9
- post do
10
- { params_keys: params.keys }
11
- end
12
- end
13
- input.write({ test: '123' * 10_000 }.to_json)
14
- input.rewind
15
- options = {
16
- input: input,
17
- method: 'POST',
18
- 'CONTENT_TYPE' => 'application/json'
19
- }
20
- env = Rack::MockRequest.env_for('/', options)
21
-
22
- expect(JSON.parse(read_chunks(app.call(env)[2]).join)['params_keys']).to match_array('test')
23
- ensure
24
- input.close
25
- input.unlink
26
- end
27
- end
28
-
29
- context 'when the app is mounted' do
30
- let(:ping_mount) do
31
- Class.new(Grape::API) do
32
- get 'ping'
33
- end
34
- end
35
-
36
- let(:app) do
37
- app_to_mount = ping_mount
38
- app = Class.new(Grape::API) do
39
- namespace 'namespace' do
40
- mount app_to_mount
41
- end
42
- end
43
- Rack::Builder.new(app)
44
- end
45
-
46
- it 'finds the app on the namespace' do
47
- get '/namespace/ping'
48
- expect(last_response.status).to eq 200
49
- end
50
- end
51
- end
@@ -1,44 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- describe Grape::API do
4
- subject do
5
- CombinedApi = combined_api
6
- Class.new(Grape::API) do
7
- format :json
8
- mount CombinedApi => '/'
9
- end
10
- end
11
-
12
- let(:jobs_api) do
13
- Class.new(Grape::API) do
14
- namespace :one do
15
- namespace :two do
16
- namespace :three do
17
- get :one do
18
- end
19
- get :two do
20
- end
21
- end
22
- end
23
- end
24
- end
25
- end
26
-
27
- let(:combined_api) do
28
- JobsApi = jobs_api
29
- Class.new(Grape::API) do
30
- version :v1, using: :accept_version_header, cascade: true
31
- mount JobsApi
32
- end
33
- end
34
-
35
- def app
36
- subject
37
- end
38
-
39
- it 'execute first request in reasonable time' do
40
- started = Time.now
41
- get '/mount1/nested/test_method'
42
- expect(Time.now - started).to be < 5
43
- end
44
- end
@@ -1,31 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'base64'
4
-
5
- describe Grape::Middleware::Auth::Base do
6
- subject do
7
- Class.new(Grape::API) do
8
- http_basic realm: 'my_realm' do |user, password|
9
- user && password && user == password
10
- end
11
- get '/authorized' do
12
- 'DONE'
13
- end
14
- end
15
- end
16
-
17
- def app
18
- subject
19
- end
20
-
21
- it 'authenticates if given valid creds' do
22
- get '/authorized', {}, 'HTTP_AUTHORIZATION' => encode_basic_auth('admin', 'admin')
23
- expect(last_response.status).to eq(200)
24
- expect(last_response.body).to eq('DONE')
25
- end
26
-
27
- it 'throws a 401 is wrong auth is given' do
28
- get '/authorized', {}, 'HTTP_AUTHORIZATION' => encode_basic_auth('admin', 'wrong')
29
- expect(last_response.status).to eq(401)
30
- end
31
- end
@@ -1,60 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- describe Grape::Middleware::Auth::DSL do
4
- subject { Class.new(Grape::API) }
5
-
6
- let(:block) { -> {} }
7
- let(:settings) do
8
- {
9
- opaque: 'secret',
10
- proc: block,
11
- realm: 'API Authorization',
12
- type: :http_digest
13
- }
14
- end
15
-
16
- describe '.auth' do
17
- it 'sets auth parameters' do
18
- expect(subject.base_instance).to receive(:use).with(Grape::Middleware::Auth::Base, settings)
19
-
20
- subject.auth :http_digest, realm: settings[:realm], opaque: settings[:opaque], &settings[:proc]
21
- expect(subject.auth).to eq(settings)
22
- end
23
-
24
- it 'can be called multiple times' do
25
- expect(subject.base_instance).to receive(:use).with(Grape::Middleware::Auth::Base, settings)
26
- expect(subject.base_instance).to receive(:use).with(Grape::Middleware::Auth::Base, settings.merge(realm: 'super_secret'))
27
-
28
- subject.auth :http_digest, realm: settings[:realm], opaque: settings[:opaque], &settings[:proc]
29
- first_settings = subject.auth
30
-
31
- subject.auth :http_digest, realm: 'super_secret', opaque: settings[:opaque], &settings[:proc]
32
-
33
- expect(subject.auth).to eq(settings.merge(realm: 'super_secret'))
34
- expect(subject.auth.object_id).not_to eq(first_settings.object_id)
35
- end
36
- end
37
-
38
- describe '.http_basic' do
39
- it 'sets auth parameters' do
40
- subject.http_basic realm: 'my_realm', &settings[:proc]
41
- expect(subject.auth).to eq(realm: 'my_realm', type: :http_basic, proc: block)
42
- end
43
- end
44
-
45
- describe '.http_digest' do
46
- context 'when realm is a hash' do
47
- it 'sets auth parameters' do
48
- subject.http_digest realm: { realm: 'my_realm', opaque: 'my_opaque' }, &settings[:proc]
49
- expect(subject.auth).to eq(realm: { realm: 'my_realm', opaque: 'my_opaque' }, type: :http_digest, proc: block)
50
- end
51
- end
52
-
53
- context 'when realm is not hash' do
54
- it 'sets auth parameters' do
55
- subject.http_digest realm: 'my_realm', opaque: 'my_opaque', &settings[:proc]
56
- expect(subject.auth).to eq(realm: 'my_realm', type: :http_digest, proc: block, opaque: 'my_opaque')
57
- end
58
- end
59
- end
60
- end
@@ -1,120 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'base64'
4
-
5
- describe Grape::Middleware::Auth::Strategies do
6
- context 'Basic Auth' do
7
- def app
8
- proc = ->(u, p) { u && p && u == p }
9
- Rack::Builder.new do |b|
10
- b.use Grape::Middleware::Error
11
- b.use(Grape::Middleware::Auth::Base, type: :http_basic, proc: proc)
12
- b.run ->(_env) { [200, {}, ['Hello there.']] }
13
- end
14
- end
15
-
16
- it 'throws a 401 if no auth is given' do
17
- @proc = -> { false }
18
- get '/whatever'
19
- expect(last_response.status).to eq(401)
20
- end
21
-
22
- it 'authenticates if given valid creds' do
23
- get '/whatever', {}, 'HTTP_AUTHORIZATION' => encode_basic_auth('admin', 'admin')
24
- expect(last_response.status).to eq(200)
25
- end
26
-
27
- it 'throws a 401 is wrong auth is given' do
28
- get '/whatever', {}, 'HTTP_AUTHORIZATION' => encode_basic_auth('admin', 'wrong')
29
- expect(last_response.status).to eq(401)
30
- end
31
- end
32
-
33
- context 'Digest MD5 Auth' do
34
- RSpec::Matchers.define :be_challenge do
35
- match do |actual_response|
36
- actual_response.status == 401 &&
37
- actual_response['WWW-Authenticate'].start_with?('Digest ') &&
38
- actual_response.body.empty?
39
- end
40
- end
41
-
42
- module StrategiesSpec
43
- class PasswordHashed < Grape::API
44
- http_digest(realm: { realm: 'Test Api', opaque: 'secret', passwords_hashed: true }) do |username|
45
- { 'foo' => Digest::MD5.hexdigest(['foo', 'Test Api', 'bar'].join(':')) }[username]
46
- end
47
-
48
- get '/test' do
49
- [{ hey: 'you' }, { there: 'bar' }, { foo: 'baz' }]
50
- end
51
- end
52
-
53
- class PasswordIsNotHashed < Grape::API
54
- http_digest(realm: 'Test Api', opaque: 'secret') do |username|
55
- { 'foo' => 'bar' }[username]
56
- end
57
-
58
- get '/test' do
59
- [{ hey: 'you' }, { there: 'bar' }, { foo: 'baz' }]
60
- end
61
- end
62
- end
63
-
64
- context 'when password is hashed' do
65
- def app
66
- StrategiesSpec::PasswordHashed
67
- end
68
-
69
- it 'is a digest authentication challenge' do
70
- get '/test'
71
- expect(last_response).to be_challenge
72
- end
73
-
74
- it 'throws a 401 if no auth is given' do
75
- get '/test'
76
- expect(last_response.status).to eq(401)
77
- end
78
-
79
- it 'authenticates if given valid creds' do
80
- digest_authorize 'foo', 'bar'
81
- get '/test'
82
- expect(last_response.status).to eq(200)
83
- end
84
-
85
- it 'throws a 401 if given invalid creds' do
86
- digest_authorize 'bar', 'foo'
87
- get '/test'
88
- expect(last_response.status).to eq(401)
89
- end
90
- end
91
-
92
- context 'when password is not hashed' do
93
- def app
94
- StrategiesSpec::PasswordIsNotHashed
95
- end
96
-
97
- it 'is a digest authentication challenge' do
98
- get '/test'
99
- expect(last_response).to be_challenge
100
- end
101
-
102
- it 'throws a 401 if no auth is given' do
103
- get '/test'
104
- expect(last_response.status).to eq(401)
105
- end
106
-
107
- it 'authenticates if given valid creds' do
108
- digest_authorize 'foo', 'bar'
109
- get '/test'
110
- expect(last_response.status).to eq(200)
111
- end
112
-
113
- it 'throws a 401 if given invalid creds' do
114
- digest_authorize 'bar', 'foo'
115
- get '/test'
116
- expect(last_response.status).to eq(401)
117
- end
118
- end
119
- end
120
- end