Skip to content

Commit

Permalink
Fix minor bug in extra_actions where action isn't passed; improve tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
gregschmit committed Mar 28, 2021
1 parent 3188bbf commit 4603f0a
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 10 deletions.
5 changes: 5 additions & 0 deletions lib/rest_framework/routers.rb
Expand Up @@ -25,6 +25,11 @@ class Mapper
path = v.delete(:path)
end

# Set the action to be the action key unless it's already defined.
if !kwargs[:action]
kwargs[:action] = k
end

# Pass any further kwargs to the underlying Rails interface.
kwargs = kwargs.merge(v)
elsif v.is_a?(Symbol) || v.is_a?(String)
Expand Down
4 changes: 4 additions & 0 deletions lib/rest_framework/serializers.rb
Expand Up @@ -3,6 +3,10 @@ def initialize(object: nil, controller: nil, **kwargs)
@object = object
@controller = controller
end

def serialize
raise NotImplementedError
end
end


Expand Down
2 changes: 1 addition & 1 deletion test/app/controllers/api2/root_controller.rb
@@ -1,5 +1,5 @@
class Api2::RootController < Api2Controller
self.extra_actions = {nil: :get, blank: :get}
self.extra_actions = {nil: :get, blank: :get, unserializable: :get}

def root
api_response({message: "Welcome to your custom API2 root!"})
Expand Down
13 changes: 11 additions & 2 deletions test/app/controllers/api2/thing_controller.rb
Expand Up @@ -3,14 +3,23 @@ class Api2::ThingController < Api2Controller

self.fields = ['id', 'name']
self.singleton_controller = true
self.extra_actions = {changed_action: {methods: [:get], path: :changed}}
self.extra_actions = {
changed_action: {methods: [:get], path: :changed},
another_changed: {methods: :get, action: :another_changed_action},
}

# Custom action if the thing changed.
# Custom action to test different action/path params in routing.
def changed_action
record = self.get_record
api_response({changed: record.updated_at})
end

# Another custom action to test different action/path params in routing.
def another_changed_action
record = self.get_record
api_response({another_changed: record.updated_at})
end

protected

def get_record
Expand Down
17 changes: 13 additions & 4 deletions test/app/controllers/api2/things_controller.rb
@@ -1,11 +1,20 @@
class Api2::ThingsController < Api2Controller
include RESTFramework::ModelControllerMixin

class ThingsSerializer < RESTFramework::NativeSerializer
self.action_config = {list: {only: [:id, :name, :price]}}
self.singular_config = {only: [:id, :name, :shape]}
self.plural_config = {only: [:id, :name]}
end

self.fields = %w(id name)
self.action_fields = {
create_fields: %w(name),
update_fields: %w(name),
}
self.action_fields = {create: %w(name), update: %w(name)}
self.paginator_class = RESTFramework::PageNumberPaginator
self.page_size = 2
self.serializer_class = ThingsSerializer
self.extra_actions = {alternate_list: :get}

def alternate_list
return self.index
end
end
6 changes: 3 additions & 3 deletions test/test/controllers/api2/root_controller_test.rb
Expand Up @@ -3,19 +3,19 @@

class Api2::RootControllerTest < ActionController::TestCase
def test_nil_fails
assert_raise RESTFramework::NilPassedToAPIResponseError do
assert_raises RESTFramework::NilPassedToAPIResponseError do
get :nil
end
end

def test_nil_json_fails
assert_raise RESTFramework::NilPassedToAPIResponseError do
assert_raises RESTFramework::NilPassedToAPIResponseError do
get :nil, as: :json
end
end

def test_nil_xml_fails
assert_raise RESTFramework::NilPassedToAPIResponseError do
assert_raises RESTFramework::NilPassedToAPIResponseError do
get :nil, as: :xml
end
end
Expand Down
25 changes: 25 additions & 0 deletions test/test/controllers/api2/thing_controller_test.rb
@@ -0,0 +1,25 @@
require_relative '../base'


class Api2::ThingControllerTest < ActionController::TestCase
def test_show
get :show, as: :json
assert_response :success
assert _parsed_body['name']
assert_nil _parsed_body['shape']
end

def test_changed_action
get :changed_action, as: :json
assert_response :success
assert _parsed_body['changed']
assert_nil _parsed_body['name']
end

def test_another_changed_action
get :another_changed_action, as: :json
assert_response :success
assert _parsed_body['another_changed']
assert_nil _parsed_body['name']
end
end
15 changes: 15 additions & 0 deletions test/test/controllers/api2/things_controller_test.rb
Expand Up @@ -5,7 +5,22 @@ class Api2::ThingsControllerTest < ActionController::TestCase
def test_list
get :index, as: :json
assert_response :success
assert _parsed_body['results'][0]['price']
assert_nil _parsed_body['results'][0]['shape']
end

def test_alternate_list
get :alternate_list, as: :json
assert_response :success
assert _parsed_body['results'][0]['name']
assert_nil _parsed_body['results'][0]['shape']
assert_nil _parsed_body['results'][0]['price']
end

def test_show
get :show, as: :json, params: {id: Thing.first.id}
assert_response :success
assert _parsed_body['shape']
assert_nil _parsed_body['price']
end
end
14 changes: 14 additions & 0 deletions test/test/integration/api2_routing_test.rb
Expand Up @@ -39,4 +39,18 @@ def test_can_not_get_network_resourceful_routes
get "/api1/network"
end
end

def test_thing_changed_action
get '/api2/thing/changed'
assert_response :success
get '/api2/thing/changed.json'
assert_response :success
end

def test_thing_another_changed_action
get '/api2/thing/another_changed'
assert_response :success
get '/api2/thing/another_changed.json'
assert_response :success
end
end
29 changes: 29 additions & 0 deletions test/test/unit/rest_framework_test.rb
@@ -1,6 +1,15 @@
require_relative '../test_helper'


class UnserializableThing
end


class UnserializableSerializer < RESTFramework::NativeSerializer
self.config = {only: [:id, :name]}
end


class RESTFrameworkTest < Minitest::Test
def test_version_number
refute_nil RESTFramework::VERSION
Expand All @@ -10,4 +19,24 @@ def test_get_version
assert RESTFramework::Version.get_version
assert RESTFramework::Version.get_version(skip_git: true)
end

def test_unserializable_serializer
unserializable_object = UnserializableThing.new
exception = assert_raises RESTFramework::UnserializableError do
UnserializableSerializer.new(object: unserializable_object).serialize
end
assert exception.message
end

def test_base_filter_get_filtered_data_not_implemented
exception = assert_raises NotImplementedError do
RESTFramework::BaseFilter.new(controller: nil).get_filtered_data([])
end
end

def test_base_serializer_serialize_not_implemented
exception = assert_raises NotImplementedError do
RESTFramework::BaseSerializer.new.serialize
end
end
end

0 comments on commit 4603f0a

Please sign in to comment.