rapid_api 0.2.0 → 0.2.1

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 (49) hide show
  1. checksums.yaml +5 -5
  2. data/README.md +2 -2
  3. data/lib/rapid_api/action_controller/errors.rb +10 -10
  4. data/lib/rapid_api/action_controller/filterable_params.rb +0 -0
  5. data/lib/rapid_api/action_controller/macros.rb +0 -0
  6. data/lib/rapid_api/action_controller/permitted_params.rb +2 -2
  7. data/lib/rapid_api/action_controller/resource_actions.rb +12 -7
  8. data/lib/rapid_api/action_controller/scope.rb +4 -9
  9. data/lib/rapid_api/action_controller.rb +0 -0
  10. data/lib/rapid_api/auth/concerns/authenticated_controller.rb +16 -26
  11. data/lib/rapid_api/auth/concerns/jwt_helpers.rb +0 -9
  12. data/lib/rapid_api/auth/concerns/sessions_controller.rb +5 -5
  13. data/lib/rapid_api/auth/concerns.rb +0 -0
  14. data/lib/rapid_api/auth/support/jwt.rb +3 -3
  15. data/lib/rapid_api/auth/support.rb +0 -0
  16. data/lib/rapid_api/auth.rb +0 -0
  17. data/lib/rapid_api/configuration.rb +0 -0
  18. data/lib/rapid_api/model_adapters/abstract.rb +0 -0
  19. data/lib/rapid_api/model_adapters/active_record_adapter.rb +0 -0
  20. data/lib/rapid_api/model_adapters/query_result.rb +0 -0
  21. data/lib/rapid_api/model_adapters.rb +0 -0
  22. data/lib/rapid_api/serializer_adapters/abstract.rb +11 -0
  23. data/lib/rapid_api/serializer_adapters/ams_adapter.rb +13 -0
  24. data/lib/rapid_api/serializer_adapters.rb +0 -0
  25. data/lib/rapid_api/version.rb +1 -1
  26. data/lib/rapid_api.rb +0 -0
  27. data/test/integration/ams_ar_test.rb +4 -4
  28. data/test/support/code_climate.rb +0 -0
  29. data/test/support/models.rb +0 -0
  30. data/test/support/schema.rb +0 -0
  31. data/test/support/serializers.rb +2 -1
  32. data/test/support/test_model_adapter.rb +0 -0
  33. data/test/support/test_module/test_model_adapter.rb +0 -0
  34. data/test/support/test_serializer_adapter.rb +13 -1
  35. data/test/test_helper.rb +0 -0
  36. data/test/unit/rapid_api/action_controller/errors_test.rb +2 -2
  37. data/test/unit/rapid_api/action_controller/filterable_params_test.rb +0 -0
  38. data/test/unit/rapid_api/action_controller/permitted_params_test.rb +1 -1
  39. data/test/unit/rapid_api/action_controller/resource_actions_test.rb +12 -2
  40. data/test/unit/rapid_api/action_controller/scope_test.rb +2 -2
  41. data/test/unit/rapid_api/auth/concerns/authenticated_controller_test.rb +5 -9
  42. data/test/unit/rapid_api/auth/concerns/sessions_controller_test.rb +0 -0
  43. data/test/unit/rapid_api/auth/support/jwt_test.rb +0 -0
  44. data/test/unit/rapid_api/model_adapters/abstract_test.rb +0 -0
  45. data/test/unit/rapid_api/model_adapters/active_record_adapter_test.rb +1 -1
  46. data/test/unit/rapid_api/serializer_adapters/abstract_test.rb +0 -0
  47. data/test/unit/rapid_api/serializer_adapters/ams_adapter_test.rb +27 -2
  48. data/test/unit/rapid_api/test_configuration.rb +0 -0
  49. metadata +13 -13
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: a317e01d9c60b75776646b2c520414e8fab282b8
4
- data.tar.gz: c61257d9c0ff646b86dc615dca07d8002bcd3e22
2
+ SHA256:
3
+ metadata.gz: 7be5a951b804eca79ba595e4bc916c4c50ab6dfad139c890f9f235ffbf8d4656
4
+ data.tar.gz: 3c70c06609a2760e41920e7bcc83a358d151ea2f3a5b89afcccd092a35fa11ee
5
5
  SHA512:
6
- metadata.gz: e2f62bc1b1c41408b0099c3145465a5a433a5584889f6b18f1353455d60ac848f8536a60f42bc5e6cccfa28d343599bcd86fe8bdc4263a66196e0fb1f1520bf6
7
- data.tar.gz: 89d9835c7aaf5bfca6a5ff8ab10f37c193ec12ca4e060107216a1946ea5e6dd5029080875c251ddde36e1a9a46871ecdd89a4ae0d24bb51751473b87accf0050
6
+ metadata.gz: 8ab4b799f58a483723b45fc720318d127be9f3a48e9b22ca926d6470c0045f77f5a34a218f8af635a6486cb22c5734d3433f81517e6b2ec5f9459c97d64df175
7
+ data.tar.gz: ab8442c402ff6bf09c4771a3c27a8ec4f01eaa71560e2d190e2d1724233a1d7889dc7bbb131375e73c277fa4515e4e9e7ea6325a34ff81e7c12acf376a1b0079
data/README.md CHANGED
@@ -99,7 +99,7 @@ end
99
99
  class ApplicationController
100
100
  rapid_base_controller
101
101
 
102
- authenticate do |controller|
102
+ authorize do |controller|
103
103
  token = controller.decode_jwt_token!(controller.request.headers.env['Authorization'])
104
104
  user_id = token[0].try :[], 'user_id'
105
105
  if user_id.present?
@@ -110,7 +110,7 @@ class ApplicationController
110
110
  end
111
111
  end
112
112
  ```
113
- `rapid_base_controller` can be added to the base class for your api controllers to provide a before_action that checks authentication. Note if your SessionController is derived from the same base class, then you should add `skip_before_action :authenticate!` to your SessionsController. The `authenticate` macro should be passed a block that returns the authenticated object. *In the example, the 'Authorization' token is parsed to return the current user*. If your authenticate block returns nil, then `:unauthorized` will be rendered.
113
+ `rapid_base_controller` can be added to the base class for your api controllers to provide a before_action that checks authentication. Note if your SessionController is derived from the same base class, then you should add `skip_before_action :authorize!` to your SessionsController. The `authenticate` macro should be passed a block that returns the authenticated object. *In the example, the 'Authorization' token is parsed to return the current user*. If your authenticate block returns nil, then `:unauthorized` will be rendered.
114
114
 
115
115
  Also note that the `decode_jwt_token!` can raise errors that will result in the rendering of an `:unauthorized` response.
116
116
 
@@ -4,8 +4,8 @@ module RapidApi
4
4
  module Errors
5
5
  extend ActiveSupport::Concern
6
6
 
7
- NotAuthenticatedError = Class.new StandardError
8
- NotFoundError = Class.new StandardError
7
+ NotAuthorizedError = Class.new StandardError
8
+ NotFoundError = Class.new StandardError
9
9
 
10
10
  class NotProcessableError < StandardError
11
11
  attr_accessor :errors
@@ -17,14 +17,14 @@ module RapidApi
17
17
  end
18
18
 
19
19
  included do
20
- rescue_from StandardError, with: :_server_error
21
- rescue_from NotAuthenticatedError, with: :_not_authenticated
22
- rescue_from NotFoundError, with: :_not_found
23
- rescue_from NotProcessableError, with: :_not_processable
20
+ rescue_from StandardError, with: :_server_error
21
+ rescue_from NotAuthorizedError, with: :_not_authorized
22
+ rescue_from NotFoundError, with: :_not_found
23
+ rescue_from NotProcessableError, with: :_not_processable
24
24
  end
25
25
 
26
- def not_authenticated!
27
- raise NotAuthenticatedError
26
+ def not_authorized!
27
+ raise NotAuthorizedError
28
28
  end
29
29
 
30
30
  def not_found!
@@ -50,7 +50,7 @@ module RapidApi
50
50
  e.backtrace.map { |m| puts m }
51
51
  end
52
52
 
53
- def _not_authenticated(e)
53
+ def _not_authorized(e)
54
54
  render_error_message 'Not Authenticated', :unauthorized, e
55
55
  end
56
56
 
@@ -59,7 +59,7 @@ module RapidApi
59
59
  end
60
60
 
61
61
  def _not_processable(e)
62
- render json: { errors: e.errors }, status: response_code_for(:unprocessable_entity)
62
+ render json: e.errors, status: response_code_for(:unprocessable_entity)
63
63
  end
64
64
 
65
65
  def _server_error(e)
File without changes
File without changes
@@ -14,8 +14,8 @@ module RapidApi
14
14
 
15
15
  private
16
16
 
17
- def _permitted_params_for(key)
18
- params.require(key).permit(*permitted_params)
17
+ def _permitted_params_for(attributes)
18
+ attributes.permit(*permitted_params)
19
19
  end
20
20
 
21
21
 
@@ -19,7 +19,8 @@ module RapidApi
19
19
  end
20
20
 
21
21
  def show
22
- query_result = _adapted_model.find params[:id], scope
22
+ id = _adapted_serializer.deserialize_id(params, _params_key)
23
+ query_result = _adapted_model.find id, scope
23
24
  if query_result.found?
24
25
  render json: _adapted_serializer.serialize(query_result.data) , status: response_code_for(:ok)
25
26
  else
@@ -28,19 +29,22 @@ module RapidApi
28
29
  end
29
30
 
30
31
  def create
31
- query_result = _adapted_model.create _member_params, scope
32
+ attributes = _member_params
33
+ query_result = _adapted_model.create attributes, scope
32
34
  if query_result.has_errors?
33
- not_processable! query_result.errors
35
+ not_processable! _adapted_serializer.serialize_errors(query_result)
34
36
  else
35
37
  render json: _adapted_serializer.serialize(query_result.data), status: response_code_for(:created)
36
38
  end
37
39
  end
38
40
 
39
41
  def update
40
- query_result = _adapted_model.update params[:id], _member_params, scope
42
+ attributes = _member_params
43
+ id = _adapted_serializer.deserialize_id(params, _params_key)
44
+ query_result = _adapted_model.update id, attributes, scope
41
45
  if query_result.found?
42
46
  if query_result.has_errors?
43
- not_processable! query_result.errors
47
+ not_processable! _adapted_serializer.serialize_errors(query_result)
44
48
  else
45
49
  render json: _adapted_serializer.serialize(query_result.data), status: response_code_for(:ok)
46
50
  end
@@ -50,7 +54,8 @@ module RapidApi
50
54
  end
51
55
 
52
56
  def destroy
53
- query_result = _adapted_model.destroy params[:id], scope
57
+ id = _adapted_serializer.deserialize_id(params, _params_key)
58
+ query_result = _adapted_model.destroy id, scope
54
59
  if query_result.has_errors?
55
60
  not_processable! query_result.errors
56
61
  else
@@ -69,7 +74,7 @@ module RapidApi
69
74
  end
70
75
 
71
76
  def _member_params
72
- _permitted_params_for(_params_key).to_h
77
+ _permitted_params_for(_adapted_serializer.deserialize_attributes(params, _params_key)).to_h
73
78
  end
74
79
 
75
80
  def _model
@@ -6,10 +6,6 @@ module RapidApi
6
6
 
7
7
  attr_accessor :scope
8
8
 
9
- included do
10
- before_action :define_scope
11
- end
12
-
13
9
  def scope
14
10
  @scope ||= {}
15
11
  end
@@ -17,11 +13,9 @@ module RapidApi
17
13
  protected
18
14
 
19
15
  def define_scope
20
- return unless self.class.scope_by_proc.present?
21
- scope_by = self.class.scope_by_proc.call(self)
22
- scope_by_array = [*scope_by]
16
+ return unless try :_scope_array
23
17
  self.class.scope_params.each_with_index do |key, index|
24
- scope[key] = scope_by_array[index]
18
+ scope[key] = _scope_array[index]
25
19
  end
26
20
  end
27
21
 
@@ -34,7 +28,8 @@ module RapidApi
34
28
  end
35
29
 
36
30
  def scope_by(*params, &block)
37
- self.scope_by_proc = Proc.new { |p| block.call(p) }
31
+ define_method :_scope_array, &block
32
+ before_action(:define_scope)
38
33
  [*params].each { |p| self.scope_params << p }
39
34
  end
40
35
  end
File without changes
@@ -4,45 +4,35 @@ module RapidApi
4
4
 
5
5
  module AuthenticatedController
6
6
  extend ActiveSupport::Concern
7
+ include JWTHelpers
7
8
 
8
9
  included do
9
- before_action :authenticate!
10
+ before_action :authorize!
10
11
 
11
- attr_accessor :authenticated
12
+ attr_accessor :authorized
12
13
  end
13
14
 
14
- def authenticate!
15
- self.extend JWTHelpers
16
- self.extend AuthenticationHelpers
17
- self.authenticated = self.class.authenticate_proc.call(self)
18
- not_authenticated! if authenticated.nil?
15
+ def authorize!
16
+ self.authorized = _authorize
17
+ not_authorized! if authorized.nil?
19
18
  end
20
19
 
21
20
  protected
22
21
 
23
- module ClassMethods
24
- attr_accessor :authenticate_proc
25
-
26
- def authenticate(&block)
27
- self.authenticate_proc = Proc.new { |request| block.call(request) }
28
- end
29
-
30
- def inherited(child)
31
- child.authenticate_proc = authenticate_proc
22
+ def decode_jwt_token!(token)
23
+ begin
24
+ jwt_decode(token)
25
+ rescue JWT::ExpiredSignature
26
+ not_authorized!
27
+ rescue JWT::VerificationError, JWT::DecodeError
28
+ not_authorized!
32
29
  end
33
-
34
30
  end
35
31
 
36
- module AuthenticationHelpers
32
+ module ClassMethods
37
33
 
38
- def decode_jwt_token!(token)
39
- begin
40
- jwt_decode(token)
41
- rescue JWT::ExpiredSignature
42
- not_authenticated!
43
- rescue JWT::VerificationError, JWT::DecodeError
44
- not_authenticated!
45
- end
34
+ def authorize(&block)
35
+ define_method :_authorize, &block
46
36
  end
47
37
 
48
38
  end
@@ -17,15 +17,6 @@ module RapidApi
17
17
  Support::JWT.decode(token)
18
18
  end
19
19
 
20
- module ClassMethods
21
- def jwt_encode(payload)
22
- Support::JWT.encode(payload)
23
- end
24
-
25
- def jwt_decode(token)
26
- Support::JWT.decode(token)
27
- end
28
- end
29
20
  end
30
21
 
31
22
  end
@@ -7,13 +7,13 @@ module RapidApi
7
7
  include JWTHelpers
8
8
 
9
9
  included do
10
- skip_before_action :authenticate!
10
+ skip_before_action :authorize!, only: [:authenticate]
11
11
  end
12
12
 
13
13
  def authenticate
14
- authenticated = self.class.auth_proc.call(permitted_auth_params)
14
+ authenticated = _authenticate(permitted_auth_params)
15
15
  if authenticated.present?
16
- render json: self.class.responds_with_proc.call(authenticated),
16
+ render json: _authentication_response_json(authenticated),
17
17
  status: :ok
18
18
  else
19
19
  render json: { errors: ['Invalid credentials'] }, status: :unauthorized
@@ -34,12 +34,12 @@ module RapidApi
34
34
  end
35
35
 
36
36
  def authenticates_with(*params, &block)
37
- self.auth_proc = Proc.new { |params| block.call(params) }
37
+ define_method :_authenticate, &block
38
38
  [*params].each { |p| self.auth_params << p }
39
39
  end
40
40
 
41
41
  def responds_with(&block)
42
- self.responds_with_proc = Proc.new { |authenticated| block.call(authenticated) }
42
+ define_method :_authentication_response_json, &block
43
43
  end
44
44
  end
45
45
  end
File without changes
@@ -6,9 +6,9 @@ module RapidApi
6
6
 
7
7
  class JWT
8
8
 
9
- def self.encode(payload={}, ttl=3600)
10
- if ttl.present?
11
- payload[:exp] = Time.current.to_i + ttl
9
+ def self.encode(payload={}, ttl_in_sec=nil)
10
+ if ttl_in_sec.present?
11
+ payload[:exp] = Time.current.to_i + ttl_in_sec
12
12
  end
13
13
  encrypt_key = nil; #TODO: Make this configurable
14
14
  encrypt_alg = 'none'
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -17,6 +17,17 @@ module RapidApi
17
17
  raise NotImplementedError
18
18
  end
19
19
 
20
+ def serialize_errors(errors)
21
+ raise NotImplementedError
22
+ end
23
+
24
+ def deserialize_attributes(params, root_key)
25
+ raise NotImplementedError
26
+ end
27
+
28
+ def deserialize_id(params, root_key)
29
+ raise NotImplementedError
30
+ end
20
31
  end
21
32
  end
22
33
  end
@@ -14,6 +14,19 @@ module RapidApi
14
14
  ActiveModelSerializers::Adapter.create(collection_serializer).to_json
15
15
  end
16
16
 
17
+ def serialize_errors(query_result)
18
+ serializer = ActiveModel::Serializer::ErrorSerializer.new(query_result)
19
+ ActiveModelSerializers::Adapter.create(serializer).to_json
20
+ end
21
+
22
+ # NOTE: Need to add support for :attributes serialization
23
+ def deserialize_attributes(params, root_key)
24
+ params.require(:data).require(:attributes)
25
+ end
26
+
27
+ def deserialize_id(params, root_key)
28
+ params.require(:id)
29
+ end
17
30
  end
18
31
  end
19
32
  end
File without changes
@@ -1,3 +1,3 @@
1
1
  module RapidApi
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
data/lib/rapid_api.rb CHANGED
File without changes
@@ -6,8 +6,8 @@ class AmsArTest < ActionController::TestCase
6
6
 
7
7
  rapid_actions model: Brick, serializer: BrickSerializer
8
8
 
9
- scope_by :user_id do |controller|
10
- controller.params['user_id']
9
+ scope_by :user_id do
10
+ params['user_id']
11
11
  end
12
12
 
13
13
  permit_params :color, :weight, :material
@@ -37,8 +37,8 @@ class AmsArTest < ActionController::TestCase
37
37
  end
38
38
 
39
39
  def test_show
40
- brick = Brick.create color: 'red', weight: 1, material: 'clay'
41
- get :show, params: {id: brick.id}
40
+ brick = Brick.create color: 'red', weight: 1, material: 'clay'
41
+ get :show, params: { id: brick.id }
42
42
  assert_equal "{\"data\":{\"id\":\"#{brick.id}\",\"type\":\"bricks\",\"attributes\":{\"color\":\"red\",\"weight\":\"1.0\",\"material\":\"clay\"}}}", response.body
43
43
  end
44
44
  end
File without changes
File without changes
File without changes
@@ -1,7 +1,8 @@
1
1
  require 'active_model_serializers'
2
2
 
3
3
  class BrickSerializer < ActiveModel::Serializer
4
- attributes :color,
4
+ attributes :id,
5
+ :color,
5
6
  :weight,
6
7
  :material
7
8
  end
File without changes
File without changes
@@ -6,5 +6,17 @@ class TestSerializerAdapter < RapidApi::SerializerAdapters::Abstract
6
6
  def serializer_collection(collection)
7
7
  return nil
8
8
  end
9
-
9
+
10
+ def serialize_errors(query_result)
11
+ { errors: query_result.errors }
12
+ end
13
+
14
+ def deserialize_attributes(params, root_key)
15
+ params.require(root_key)
16
+ end
17
+
18
+ def deserialize_id(params, root_key)
19
+ params.require(:id)
20
+ end
21
+
10
22
  end
data/test/test_helper.rb CHANGED
File without changes
@@ -6,7 +6,7 @@ class ActionControllerErrorsTest < ActionController::TestCase
6
6
  include RapidApi::ActionController::Errors
7
7
 
8
8
  def unauthenticated_action
9
- not_authenticated!
9
+ not_authorized!
10
10
  end
11
11
 
12
12
  def not_found_action
@@ -16,7 +16,7 @@ class ActionControllerErrorsTest < ActionController::TestCase
16
16
  def not_processable_action
17
17
  brick = Brick.create
18
18
  brick.errors.add(:base, 'Sample Error')
19
- not_processable!(brick.errors)
19
+ not_processable!({ errors: brick.errors })
20
20
  end
21
21
 
22
22
  def server_error_action
@@ -10,7 +10,7 @@ module ActionController
10
10
  permit_params :color
11
11
 
12
12
  def permissive_bricks
13
- render json: _permitted_params_for(:brick).to_json, status: :ok
13
+ render json: _permitted_params_for(params.require(:brick)).to_json, status: :ok
14
14
  end
15
15
  end
16
16
 
@@ -13,8 +13,8 @@ module ActionController
13
13
  permit_params :color, :weight, :material
14
14
  filterable_params :color
15
15
 
16
- scope_by :color do |controller|
17
- 'blue'
16
+ scope_by :color do
17
+ ['blue']
18
18
  end
19
19
  end
20
20
 
@@ -68,6 +68,7 @@ module ActionController
68
68
  BricksController.adapted_model.expect :find, query_result, ["1", {color: 'blue'}]
69
69
  BricksController.adapted_serializer = Minitest::Mock.new
70
70
  BricksController.adapted_serializer.expect :serialize, nil, ['data']
71
+ BricksController.adapted_serializer.expect :deserialize_id, "1", [ActionController::Parameters, String]
71
72
  get :show, params: {id: 1}
72
73
  assert_response :ok
73
74
  BricksController.adapted_model.verify
@@ -76,6 +77,7 @@ module ActionController
76
77
 
77
78
  def test_show_not_found
78
79
  BricksController.adapted_model = Minitest::Mock.new
80
+ BricksController.serializer = TestSerializerAdapter
79
81
  query_result = RapidApi::ModelAdapters::QueryResult.new()
80
82
  BricksController.adapted_model.expect :find, query_result, ["1", {color: 'blue'}]
81
83
  get :show, params: {id: 1}
@@ -90,6 +92,7 @@ module ActionController
90
92
  BricksController.adapted_model.expect :create, query_result, [params, {color: 'blue'}]
91
93
  BricksController.adapted_serializer = Minitest::Mock.new
92
94
  BricksController.adapted_serializer.expect :serialize, nil, ['data']
95
+ BricksController.adapted_serializer.expect :deserialize_attributes, ActionController::Parameters.new(params), [ActionController::Parameters, String]
93
96
  post :create, params: {brick: params}
94
97
  assert_response :created
95
98
  BricksController.adapted_model.verify
@@ -99,6 +102,7 @@ module ActionController
99
102
  def test_create_errors
100
103
  params = {'color' => 'red', 'weight' => '10', 'material' => 'clay'}
101
104
  BricksController.adapted_model = Minitest::Mock.new
105
+ BricksController.serializer = TestSerializerAdapter
102
106
  query_result = RapidApi::ModelAdapters::QueryResult.new(data: 'data', errors: 'dummy error')
103
107
  BricksController.adapted_model.expect :create, query_result, [params, {color: 'blue'}]
104
108
  post :create, params: {brick: params}
@@ -113,6 +117,8 @@ module ActionController
113
117
  BricksController.adapted_model.expect :update, query_result, ["1", params, {color: 'blue'}]
114
118
  BricksController.adapted_serializer = Minitest::Mock.new
115
119
  BricksController.adapted_serializer.expect :serialize, nil, ['data']
120
+ BricksController.adapted_serializer.expect :deserialize_attributes, ActionController::Parameters.new(params), [ActionController::Parameters, String]
121
+ BricksController.adapted_serializer.expect :deserialize_id, "1", [ActionController::Parameters, String]
116
122
  post :update, params: {brick: params, id: 1}
117
123
  assert_response :ok
118
124
  BricksController.adapted_model.verify
@@ -122,6 +128,7 @@ module ActionController
122
128
  def test_update_not_found
123
129
  params = {'color' => 'red', 'weight' => '10', 'material' => 'clay'}
124
130
  BricksController.adapted_model = Minitest::Mock.new
131
+ BricksController.serializer = TestSerializerAdapter
125
132
  query_result = RapidApi::ModelAdapters::QueryResult.new()
126
133
  BricksController.adapted_model.expect :update, query_result, ["1", params, {color: 'blue'}]
127
134
  post :update, params: {brick: params, id: 1}
@@ -132,6 +139,7 @@ module ActionController
132
139
  def test_update_errors
133
140
  params = {'color' => 'red', 'weight' => '10', 'material' => 'clay'}
134
141
  BricksController.adapted_model = Minitest::Mock.new
142
+ BricksController.serializer = TestSerializerAdapter
135
143
  query_result = RapidApi::ModelAdapters::QueryResult.new(data: 'data', errors: 'dummy error')
136
144
  BricksController.adapted_model.expect :update, query_result, ["1", params, {color: 'blue'}]
137
145
  post :update, params: {brick: params, id: 1}
@@ -141,6 +149,7 @@ module ActionController
141
149
 
142
150
  def test_destroy
143
151
  BricksController.adapted_model = Minitest::Mock.new
152
+ BricksController.serializer = TestSerializerAdapter
144
153
  query_result = RapidApi::ModelAdapters::QueryResult.new()
145
154
  BricksController.adapted_model.expect :destroy, query_result, ["1", {color: 'blue'}]
146
155
  post :destroy, params: {id: 1}
@@ -150,6 +159,7 @@ module ActionController
150
159
 
151
160
  def test_destroy_errors
152
161
  BricksController.adapted_model = Minitest::Mock.new
162
+ BricksController.serializer = TestSerializerAdapter
153
163
  query_result = RapidApi::ModelAdapters::QueryResult.new(errors: 'dummy error')
154
164
  BricksController.adapted_model.expect :destroy, query_result, ["1", {color: 'blue'}]
155
165
  post :destroy, params: {id: 1}
@@ -7,8 +7,8 @@ module ActionController
7
7
  class TestScopesController < ActionController::Base
8
8
  include RapidApi::ActionController::Scope
9
9
 
10
- scope_by :user_id, :org_id do |controller|
11
- [controller.params['user_id'], controller.params['org_id']]
10
+ scope_by :user_id, :org_id do
11
+ [params['user_id'], params['org_id']]
12
12
  end
13
13
 
14
14
  def scoped_bricks
@@ -10,8 +10,8 @@ class AuthenticatedControllerTest < ActionController::TestCase
10
10
 
11
11
  permit_params :color, :weight, :material
12
12
 
13
- authenticate do |controller|
14
- token = controller.decode_jwt_token!(controller.request.headers.env['Authorization'])
13
+ authorize do
14
+ token = decode_jwt_token!(request.headers.env['Authorization'])
15
15
  user_id = token[0].try :[], 'user_id'
16
16
  if user_id.present?
17
17
  User.find user_id
@@ -34,23 +34,19 @@ class AuthenticatedControllerTest < ActionController::TestCase
34
34
  DatabaseCleaner.clean
35
35
  end
36
36
 
37
- def test_authenticated
37
+ def test_authorized
38
38
  @user = User.create
39
39
  token = RapidApi::Auth::Support::JWT.encode({ user_id: @user.id })
40
40
  @request.env['Authorization'] = token
41
41
  get :index
42
42
  assert_response :ok
43
- assert_equal @controller.authenticated.id, @user.id
43
+ assert_equal @controller.authorized.id, @user.id
44
44
  end
45
45
 
46
- def test_not_authenticated_without_user
46
+ def test_not_authorized_without_user
47
47
  User.delete_all
48
48
  get :index
49
49
  assert_response :unauthorized
50
50
  end
51
51
 
52
- def test_child_controller
53
- assert_equal TestAuthenticatedController.authenticate_proc, TestChildAuthenticatedController.authenticate_proc
54
- end
55
-
56
52
  end
File without changes
File without changes
@@ -84,7 +84,7 @@ module RapidApi
84
84
  def test_find_with_scope
85
85
  brick = Brick.create color: 'magenta'
86
86
  query_result = @adapter.find(brick.id, {color: 'orange'})
87
- assert_equal nil, query_result.data
87
+ assert_nil query_result.data
88
88
  query_result = @adapter.find(brick.id, {color: 'magenta'})
89
89
  assert_equal brick, query_result.data
90
90
  end
@@ -13,9 +13,9 @@ module RapidApi
13
13
  end
14
14
 
15
15
  def test_serialize
16
- brick = Brick.create color: 'yellow', weight: 10, material: 'gold'
16
+ brick = Brick.create color: 'yellow', weight: 10, material: 'paper'
17
17
  serialized_brick = @adapter.serialize brick
18
- assert_equal "{\"data\":{\"id\":\"#{brick.id}\",\"type\":\"bricks\",\"attributes\":{\"color\":\"yellow\",\"weight\":\"10.0\",\"material\":\"gold\"}}}", serialized_brick
18
+ assert_equal "{\"data\":{\"id\":\"#{brick.id}\",\"type\":\"bricks\",\"attributes\":{\"color\":\"yellow\",\"weight\":\"10.0\",\"material\":\"paper\"}}}", serialized_brick
19
19
  end
20
20
 
21
21
  def test_serialize_collection
@@ -26,6 +26,31 @@ module RapidApi
26
26
  assert_equal "{\"data\":[{\"id\":\"#{brick1.id}\",\"type\":\"bricks\",\"attributes\":{\"color\":\"yellow\",\"weight\":\"10.0\",\"material\":\"gold\"}},{\"id\":\"#{brick2.id}\",\"type\":\"bricks\",\"attributes\":{\"color\":\"red\",\"weight\":\"1.0\",\"material\":\"clay\"}}]}", serialized_brick_array
27
27
  end
28
28
 
29
+ def test_serialize_errors
30
+ brick1 = Brick.create color: 'teal', weight: 10, material: 'gold'
31
+ brick1.errors.add(:color,'Invalid color.')
32
+ serialized_errors = @adapter.serialize_errors RapidApi::ModelAdapters::QueryResult.new(errors: brick1.errors)
33
+ assert_equal "{\"errors\":[{\"source\":{\"pointer\":\"/data/attributes/color\"},\"detail\":\"Invalid color.\"}]}", serialized_errors
34
+ end
35
+
36
+ def test_deserialize_attributes
37
+ params = ::ActionController::Parameters.new({
38
+ data: {
39
+ attributes: { color: 'red', weight: '10.0' },
40
+ id: 10,
41
+ type: 'bricks'
42
+ }
43
+ })
44
+ assert_equal({ color: 'red', weight: '10.0' }.to_json, @adapter.deserialize_attributes(params, '').to_json)
45
+ end
46
+
47
+ def test_deserialize_id
48
+ params = ::ActionController::Parameters.new({
49
+ id: 10
50
+ })
51
+ assert_equal(10 , @adapter.deserialize_id(params, ''))
52
+ end
53
+
29
54
  end
30
55
  end
31
56
  end
File without changes
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rapid_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - briandavidwetzel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-15 00:00:00.000000000 Z
11
+ date: 2018-09-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack
@@ -168,30 +168,30 @@ required_rubygems_version: !ruby/object:Gem::Requirement
168
168
  version: '0'
169
169
  requirements: []
170
170
  rubyforge_project:
171
- rubygems_version: 2.6.6
171
+ rubygems_version: 2.7.7
172
172
  signing_key:
173
173
  specification_version: 4
174
174
  summary: A framework for rapid development of Rails APIs.
175
175
  test_files:
176
- - test/support/test_serializer_adapter.rb
176
+ - test/integration/ams_ar_test.rb
177
177
  - test/support/code_climate.rb
178
178
  - test/support/models.rb
179
- - test/support/test_module/test_model_adapter.rb
180
- - test/support/test_model_adapter.rb
181
- - test/support/serializers.rb
182
179
  - test/support/schema.rb
183
- - test/integration/ams_ar_test.rb
180
+ - test/support/serializers.rb
181
+ - test/support/test_model_adapter.rb
182
+ - test/support/test_module/test_model_adapter.rb
183
+ - test/support/test_serializer_adapter.rb
184
184
  - test/test_helper.rb
185
- - test/unit/rapid_api/test_configuration.rb
186
185
  - test/unit/rapid_api/action_controller/errors_test.rb
187
186
  - test/unit/rapid_api/action_controller/filterable_params_test.rb
187
+ - test/unit/rapid_api/action_controller/permitted_params_test.rb
188
188
  - test/unit/rapid_api/action_controller/resource_actions_test.rb
189
189
  - test/unit/rapid_api/action_controller/scope_test.rb
190
- - test/unit/rapid_api/action_controller/permitted_params_test.rb
191
- - test/unit/rapid_api/serializer_adapters/ams_adapter_test.rb
192
- - test/unit/rapid_api/serializer_adapters/abstract_test.rb
193
- - test/unit/rapid_api/auth/concerns/sessions_controller_test.rb
194
190
  - test/unit/rapid_api/auth/concerns/authenticated_controller_test.rb
191
+ - test/unit/rapid_api/auth/concerns/sessions_controller_test.rb
195
192
  - test/unit/rapid_api/auth/support/jwt_test.rb
196
193
  - test/unit/rapid_api/model_adapters/abstract_test.rb
197
194
  - test/unit/rapid_api/model_adapters/active_record_adapter_test.rb
195
+ - test/unit/rapid_api/serializer_adapters/abstract_test.rb
196
+ - test/unit/rapid_api/serializer_adapters/ams_adapter_test.rb
197
+ - test/unit/rapid_api/test_configuration.rb