|
| 1 | +require 'test_helper' |
| 2 | + |
| 3 | +module ActionController |
| 4 | + module Serialization |
| 5 | + class NamespaceLookupTest < ActionController::TestCase |
| 6 | + class Book < ::Model; end |
| 7 | + class Page < ::Model; end |
| 8 | + class Writer < ::Model; end |
| 9 | + |
| 10 | + module Api |
| 11 | + module V2 |
| 12 | + class BookSerializer < ActiveModel::Serializer |
| 13 | + attributes :title |
| 14 | + end |
| 15 | + end |
| 16 | + |
| 17 | + module V3 |
| 18 | + class BookSerializer < ActiveModel::Serializer |
| 19 | + attributes :title, :body |
| 20 | + |
| 21 | + belongs_to :writer |
| 22 | + end |
| 23 | + |
| 24 | + class WriterSerializer < ActiveModel::Serializer |
| 25 | + attributes :name |
| 26 | + end |
| 27 | + |
| 28 | + class LookupTestController < ActionController::Base |
| 29 | + before_action only: [:namespace_set_in_before_filter] do |
| 30 | + self.namespace_for_serializer = Api::V2 |
| 31 | + end |
| 32 | + |
| 33 | + def implicit_namespaced_serializer |
| 34 | + writer = Writer.new(name: 'Bob') |
| 35 | + book = Book.new(title: 'New Post', body: 'Body', writer: writer) |
| 36 | + |
| 37 | + render json: book |
| 38 | + end |
| 39 | + |
| 40 | + def explicit_namespace_as_module |
| 41 | + book = Book.new(title: 'New Post', body: 'Body') |
| 42 | + |
| 43 | + render json: book, namespace: Api::V2 |
| 44 | + end |
| 45 | + |
| 46 | + def explicit_namespace_as_string |
| 47 | + book = Book.new(title: 'New Post', body: 'Body') |
| 48 | + |
| 49 | + # because this is a string, ruby can't auto-lookup the constant, so otherwise |
| 50 | + # the looku things we mean ::Api::V2 |
| 51 | + render json: book, namespace: 'ActionController::Serialization::NamespaceLookupTest::Api::V2' |
| 52 | + end |
| 53 | + |
| 54 | + def explicit_namespace_as_symbol |
| 55 | + book = Book.new(title: 'New Post', body: 'Body') |
| 56 | + |
| 57 | + # because this is a string, ruby can't auto-lookup the constant, so otherwise |
| 58 | + # the looku things we mean ::Api::V2 |
| 59 | + render json: book, namespace: :'ActionController::Serialization::NamespaceLookupTest::Api::V2' |
| 60 | + end |
| 61 | + |
| 62 | + def invalid_namespace |
| 63 | + book = Book.new(title: 'New Post', body: 'Body') |
| 64 | + |
| 65 | + render json: book, namespace: :api_v2 |
| 66 | + end |
| 67 | + |
| 68 | + def namespace_set_in_before_filter |
| 69 | + book = Book.new(title: 'New Post', body: 'Body') |
| 70 | + render json: book |
| 71 | + end |
| 72 | + end |
| 73 | + end |
| 74 | + end |
| 75 | + |
| 76 | + tests Api::V3::LookupTestController |
| 77 | + |
| 78 | + setup do |
| 79 | + @test_namespace = self.class.parent |
| 80 | + end |
| 81 | + |
| 82 | + test 'implicitly uses namespaced serializer' do |
| 83 | + get :implicit_namespaced_serializer |
| 84 | + |
| 85 | + assert_serializer Api::V3::BookSerializer |
| 86 | + |
| 87 | + expected = { 'title' => 'New Post', 'body' => 'Body', 'writer' => { 'name' => 'Bob' } } |
| 88 | + actual = JSON.parse(@response.body) |
| 89 | + |
| 90 | + assert_equal expected, actual |
| 91 | + end |
| 92 | + |
| 93 | + test 'explicit namespace as module' do |
| 94 | + get :explicit_namespace_as_module |
| 95 | + |
| 96 | + assert_serializer Api::V2::BookSerializer |
| 97 | + |
| 98 | + expected = { 'title' => 'New Post' } |
| 99 | + actual = JSON.parse(@response.body) |
| 100 | + |
| 101 | + assert_equal expected, actual |
| 102 | + end |
| 103 | + |
| 104 | + test 'explicit namespace as string' do |
| 105 | + get :explicit_namespace_as_string |
| 106 | + |
| 107 | + assert_serializer Api::V2::BookSerializer |
| 108 | + |
| 109 | + expected = { 'title' => 'New Post' } |
| 110 | + actual = JSON.parse(@response.body) |
| 111 | + |
| 112 | + assert_equal expected, actual |
| 113 | + end |
| 114 | + |
| 115 | + test 'explicit namespace as symbol' do |
| 116 | + get :explicit_namespace_as_symbol |
| 117 | + |
| 118 | + assert_serializer Api::V2::BookSerializer |
| 119 | + |
| 120 | + expected = { 'title' => 'New Post' } |
| 121 | + actual = JSON.parse(@response.body) |
| 122 | + |
| 123 | + assert_equal expected, actual |
| 124 | + end |
| 125 | + |
| 126 | + test 'invalid namespace' do |
| 127 | + get :invalid_namespace |
| 128 | + |
| 129 | + assert_serializer ActiveModel::Serializer::Null |
| 130 | + |
| 131 | + expected = { 'title' => 'New Post', 'body' => 'Body' } |
| 132 | + actual = JSON.parse(@response.body) |
| 133 | + |
| 134 | + assert_equal expected, actual |
| 135 | + end |
| 136 | + |
| 137 | + test 'namespace set in before filter' do |
| 138 | + get :namespace_set_in_before_filter |
| 139 | + |
| 140 | + assert_serializer Api::V2::BookSerializer |
| 141 | + |
| 142 | + expected = { 'title' => 'New Post' } |
| 143 | + actual = JSON.parse(@response.body) |
| 144 | + |
| 145 | + assert_equal expected, actual |
| 146 | + end |
| 147 | + end |
| 148 | + end |
| 149 | +end |
0 commit comments