jwtbuilder 0.0.1 → 2.2.12.jwt
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.
- checksums.yaml +4 -4
- data/.gitignore +5 -0
- data/.travis.yml +28 -0
- data/Appraisals +32 -0
- data/CHANGELOG.md +174 -0
- data/Gemfile +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +260 -0
- data/Rakefile +23 -0
- data/gemfiles/rails_3_0.gemfile +12 -0
- data/gemfiles/rails_3_1.gemfile +12 -0
- data/gemfiles/rails_3_2.gemfile +12 -0
- data/gemfiles/rails_4_0.gemfile +11 -0
- data/gemfiles/rails_4_1.gemfile +11 -0
- data/gemfiles/rails_4_2.gemfile +11 -0
- data/jbuilder.gemspec +18 -0
- data/lib/generators/rails/jbuilder_generator.rb +48 -0
- data/lib/generators/rails/scaffold_controller_generator.rb +12 -0
- data/lib/generators/rails/templates/controller.rb +84 -0
- data/lib/generators/rails/templates/index.json.jbuilder +5 -0
- data/lib/generators/rails/templates/show.json.jbuilder +3 -0
- data/lib/jbuilder/blank.rb +11 -0
- data/lib/jbuilder/dependency_tracker.rb +61 -0
- data/lib/jbuilder/errors.rb +17 -0
- data/lib/jbuilder/jbuilder.rb +7 -0
- data/lib/jbuilder/jbuilder_template.rb +145 -0
- data/lib/jbuilder/key_formatter.rb +34 -0
- data/lib/jbuilder/railtie.rb +21 -0
- data/lib/jbuilder.rb +313 -0
- data/test/jbuilder_dependency_tracker_test.rb +72 -0
- data/test/jbuilder_generator_test.rb +32 -0
- data/test/jbuilder_template_test.rb +343 -0
- data/test/jbuilder_test.rb +686 -0
- data/test/scaffold_controller_generator_test.rb +57 -0
- data/test/test_helper.rb +14 -0
- metadata +48 -8
@@ -0,0 +1,343 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'mocha/setup'
|
3
|
+
require 'action_view'
|
4
|
+
require 'action_view/testing/resolvers'
|
5
|
+
require 'active_support/cache'
|
6
|
+
require 'jbuilder/jbuilder_template'
|
7
|
+
|
8
|
+
BLOG_POST_PARTIAL = <<-JBUILDER
|
9
|
+
json.extract! blog_post, :id, :body
|
10
|
+
json.author do
|
11
|
+
name = blog_post.author_name.split(nil, 2)
|
12
|
+
json.first_name name[0]
|
13
|
+
json.last_name name[1]
|
14
|
+
end
|
15
|
+
JBUILDER
|
16
|
+
|
17
|
+
COLLECTION_PARTIAL = <<-JBUILDER
|
18
|
+
json.extract! collection, :id, :name
|
19
|
+
JBUILDER
|
20
|
+
|
21
|
+
BlogPost = Struct.new(:id, :body, :author_name)
|
22
|
+
Collection = Struct.new(:id, :name)
|
23
|
+
blog_authors = [ 'David Heinemeier Hansson', 'Pavel Pravosud' ].cycle
|
24
|
+
BLOG_POST_COLLECTION = 10.times.map{ |i| BlogPost.new(i+1, "post body #{i+1}", blog_authors.next) }
|
25
|
+
COLLECTION_COLLECTION = 5.times.map{ |i| Collection.new(i+1, "collection #{i+1}") }
|
26
|
+
|
27
|
+
ActionView::Template.register_template_handler :jbuilder, JbuilderHandler
|
28
|
+
|
29
|
+
module Rails
|
30
|
+
def self.cache
|
31
|
+
@cache ||= ActiveSupport::Cache::MemoryStore.new
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
class JbuilderTemplateTest < ActionView::TestCase
|
36
|
+
setup do
|
37
|
+
@context = self
|
38
|
+
Rails.cache.clear
|
39
|
+
end
|
40
|
+
|
41
|
+
def partials
|
42
|
+
{
|
43
|
+
'_partial.json.jbuilder' => 'foo ||= "hello"; json.content foo',
|
44
|
+
'_blog_post.json.jbuilder' => BLOG_POST_PARTIAL,
|
45
|
+
'_collection.json.jbuilder' => COLLECTION_PARTIAL
|
46
|
+
}
|
47
|
+
end
|
48
|
+
|
49
|
+
def render_jbuilder(source)
|
50
|
+
@rendered = []
|
51
|
+
lookup_context.view_paths = [ActionView::FixtureResolver.new(partials.merge('test.json.jbuilder' => source))]
|
52
|
+
ActionView::Template.new(source, 'test', JbuilderHandler, :virtual_path => 'test').render(self, {}).strip
|
53
|
+
end
|
54
|
+
|
55
|
+
def undef_context_methods(*names)
|
56
|
+
self.class_eval do
|
57
|
+
names.each do |name|
|
58
|
+
undef_method name.to_sym if method_defined?(name.to_sym)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def assert_collection_rendered(json, context = nil)
|
64
|
+
result = MultiJson.load(json)
|
65
|
+
result = result.fetch(context) if context
|
66
|
+
|
67
|
+
assert_equal 10, result.length
|
68
|
+
assert_equal Array, result.class
|
69
|
+
assert_equal 'post body 5', result[4]['body']
|
70
|
+
assert_equal 'Heinemeier Hansson', result[2]['author']['last_name']
|
71
|
+
assert_equal 'Pavel', result[5]['author']['first_name']
|
72
|
+
end
|
73
|
+
|
74
|
+
test 'rendering' do
|
75
|
+
json = render_jbuilder <<-JBUILDER
|
76
|
+
json.content 'hello'
|
77
|
+
JBUILDER
|
78
|
+
|
79
|
+
assert_equal 'hello', MultiJson.load(json)['content']
|
80
|
+
end
|
81
|
+
|
82
|
+
test 'key_format! with parameter' do
|
83
|
+
json = render_jbuilder <<-JBUILDER
|
84
|
+
json.key_format! :camelize => [:lower]
|
85
|
+
json.camel_style 'for JS'
|
86
|
+
JBUILDER
|
87
|
+
|
88
|
+
assert_equal ['camelStyle'], MultiJson.load(json).keys
|
89
|
+
end
|
90
|
+
|
91
|
+
test 'key_format! propagates to child elements' do
|
92
|
+
json = render_jbuilder <<-JBUILDER
|
93
|
+
json.key_format! :upcase
|
94
|
+
json.level1 'one'
|
95
|
+
json.level2 do
|
96
|
+
json.value 'two'
|
97
|
+
end
|
98
|
+
JBUILDER
|
99
|
+
|
100
|
+
result = MultiJson.load(json)
|
101
|
+
assert_equal 'one', result['LEVEL1']
|
102
|
+
assert_equal 'two', result['LEVEL2']['VALUE']
|
103
|
+
end
|
104
|
+
|
105
|
+
test 'partial! renders partial' do
|
106
|
+
json = render_jbuilder <<-JBUILDER
|
107
|
+
json.partial! 'partial'
|
108
|
+
JBUILDER
|
109
|
+
|
110
|
+
assert_equal 'hello', MultiJson.load(json)['content']
|
111
|
+
end
|
112
|
+
|
113
|
+
test 'partial! + locals via :locals option' do
|
114
|
+
json = render_jbuilder <<-JBUILDER
|
115
|
+
json.partial! 'partial', locals: { foo: 'howdy' }
|
116
|
+
JBUILDER
|
117
|
+
|
118
|
+
assert_equal 'howdy', MultiJson.load(json)['content']
|
119
|
+
end
|
120
|
+
|
121
|
+
test 'partial! + locals without :locals key' do
|
122
|
+
json = render_jbuilder <<-JBUILDER
|
123
|
+
json.partial! 'partial', foo: 'goodbye'
|
124
|
+
JBUILDER
|
125
|
+
|
126
|
+
assert_equal 'goodbye', MultiJson.load(json)['content']
|
127
|
+
end
|
128
|
+
|
129
|
+
test 'partial! renders collections' do
|
130
|
+
json = render_jbuilder <<-JBUILDER
|
131
|
+
json.partial! 'blog_post', :collection => BLOG_POST_COLLECTION, :as => :blog_post
|
132
|
+
JBUILDER
|
133
|
+
|
134
|
+
assert_collection_rendered json
|
135
|
+
end
|
136
|
+
|
137
|
+
test 'partial! renders collections when as argument is a string' do
|
138
|
+
json = render_jbuilder <<-JBUILDER
|
139
|
+
json.partial! 'blog_post', collection: BLOG_POST_COLLECTION, as: "blog_post"
|
140
|
+
JBUILDER
|
141
|
+
|
142
|
+
assert_collection_rendered json
|
143
|
+
end
|
144
|
+
|
145
|
+
test 'partial! renders collections as collections' do
|
146
|
+
json = render_jbuilder <<-JBUILDER
|
147
|
+
json.partial! 'collection', collection: COLLECTION_COLLECTION, as: :collection
|
148
|
+
JBUILDER
|
149
|
+
|
150
|
+
assert_equal 5, MultiJson.load(json).length
|
151
|
+
end
|
152
|
+
|
153
|
+
test 'partial! renders as empty array for nil-collection' do
|
154
|
+
json = render_jbuilder <<-JBUILDER
|
155
|
+
json.partial! 'blog_post', :collection => nil, :as => :blog_post
|
156
|
+
JBUILDER
|
157
|
+
|
158
|
+
assert_equal '[]', json
|
159
|
+
end
|
160
|
+
|
161
|
+
test 'partial! renders collection (alt. syntax)' do
|
162
|
+
json = render_jbuilder <<-JBUILDER
|
163
|
+
json.partial! :partial => 'blog_post', :collection => BLOG_POST_COLLECTION, :as => :blog_post
|
164
|
+
JBUILDER
|
165
|
+
|
166
|
+
assert_collection_rendered json
|
167
|
+
end
|
168
|
+
|
169
|
+
test 'partial! renders as empty array for nil-collection (alt. syntax)' do
|
170
|
+
json = render_jbuilder <<-JBUILDER
|
171
|
+
json.partial! :partial => 'blog_post', :collection => nil, :as => :blog_post
|
172
|
+
JBUILDER
|
173
|
+
|
174
|
+
assert_equal '[]', json
|
175
|
+
end
|
176
|
+
|
177
|
+
test 'render array of partials' do
|
178
|
+
json = render_jbuilder <<-JBUILDER
|
179
|
+
json.array! BLOG_POST_COLLECTION, :partial => 'blog_post', :as => :blog_post
|
180
|
+
JBUILDER
|
181
|
+
|
182
|
+
assert_collection_rendered json
|
183
|
+
end
|
184
|
+
|
185
|
+
test 'render array of partials as empty array with nil-collection' do
|
186
|
+
json = render_jbuilder <<-JBUILDER
|
187
|
+
json.array! nil, :partial => 'blog_post', :as => :blog_post
|
188
|
+
JBUILDER
|
189
|
+
|
190
|
+
assert_equal '[]', json
|
191
|
+
end
|
192
|
+
|
193
|
+
test 'render array if partials as a value' do
|
194
|
+
json = render_jbuilder <<-JBUILDER
|
195
|
+
json.posts BLOG_POST_COLLECTION, :partial => 'blog_post', :as => :blog_post
|
196
|
+
JBUILDER
|
197
|
+
|
198
|
+
assert_collection_rendered json, 'posts'
|
199
|
+
end
|
200
|
+
|
201
|
+
test 'render as empty array if partials as a nil value' do
|
202
|
+
json = render_jbuilder <<-JBUILDER
|
203
|
+
json.posts nil, :partial => 'blog_post', :as => :blog_post
|
204
|
+
JBUILDER
|
205
|
+
|
206
|
+
assert_equal '{"posts":[]}', json
|
207
|
+
end
|
208
|
+
|
209
|
+
test 'cache an empty block' do
|
210
|
+
undef_context_methods :fragment_name_with_digest, :cache_fragment_name
|
211
|
+
|
212
|
+
render_jbuilder <<-JBUILDER
|
213
|
+
json.cache! 'nothing' do
|
214
|
+
end
|
215
|
+
JBUILDER
|
216
|
+
|
217
|
+
json = nil
|
218
|
+
|
219
|
+
assert_nothing_raised do
|
220
|
+
json = render_jbuilder <<-JBUILDER
|
221
|
+
json.foo 'bar'
|
222
|
+
json.cache! 'nothing' do
|
223
|
+
end
|
224
|
+
JBUILDER
|
225
|
+
end
|
226
|
+
|
227
|
+
assert_equal 'bar', MultiJson.load(json)['foo']
|
228
|
+
end
|
229
|
+
|
230
|
+
test 'fragment caching a JSON object' do
|
231
|
+
undef_context_methods :fragment_name_with_digest, :cache_fragment_name
|
232
|
+
|
233
|
+
render_jbuilder <<-JBUILDER
|
234
|
+
json.cache! 'cachekey' do
|
235
|
+
json.name 'Cache'
|
236
|
+
end
|
237
|
+
JBUILDER
|
238
|
+
|
239
|
+
json = render_jbuilder <<-JBUILDER
|
240
|
+
json.cache! 'cachekey' do
|
241
|
+
json.name 'Miss'
|
242
|
+
end
|
243
|
+
JBUILDER
|
244
|
+
|
245
|
+
parsed = MultiJson.load(json)
|
246
|
+
assert_equal 'Cache', parsed['name']
|
247
|
+
end
|
248
|
+
|
249
|
+
test 'conditionally fragment caching a JSON object' do
|
250
|
+
undef_context_methods :fragment_name_with_digest, :cache_fragment_name
|
251
|
+
|
252
|
+
render_jbuilder <<-JBUILDER
|
253
|
+
json.cache_if! true, 'cachekey' do
|
254
|
+
json.test1 'Cache'
|
255
|
+
end
|
256
|
+
json.cache_if! false, 'cachekey' do
|
257
|
+
json.test2 'Cache'
|
258
|
+
end
|
259
|
+
JBUILDER
|
260
|
+
|
261
|
+
json = render_jbuilder <<-JBUILDER
|
262
|
+
json.cache_if! true, 'cachekey' do
|
263
|
+
json.test1 'Miss'
|
264
|
+
end
|
265
|
+
json.cache_if! false, 'cachekey' do
|
266
|
+
json.test2 'Miss'
|
267
|
+
end
|
268
|
+
JBUILDER
|
269
|
+
|
270
|
+
parsed = MultiJson.load(json)
|
271
|
+
assert_equal 'Cache', parsed['test1']
|
272
|
+
assert_equal 'Miss', parsed['test2']
|
273
|
+
end
|
274
|
+
|
275
|
+
test 'fragment caching deserializes an array' do
|
276
|
+
undef_context_methods :fragment_name_with_digest, :cache_fragment_name
|
277
|
+
|
278
|
+
render_jbuilder <<-JBUILDER
|
279
|
+
json.cache! 'cachekey' do
|
280
|
+
json.array! %w[a b c]
|
281
|
+
end
|
282
|
+
JBUILDER
|
283
|
+
|
284
|
+
json = render_jbuilder <<-JBUILDER
|
285
|
+
json.cache! 'cachekey' do
|
286
|
+
json.array! %w[1 2 3]
|
287
|
+
end
|
288
|
+
JBUILDER
|
289
|
+
|
290
|
+
parsed = MultiJson.load(json)
|
291
|
+
assert_equal %w[a b c], parsed
|
292
|
+
end
|
293
|
+
|
294
|
+
test 'fragment caching works with previous version of cache digests' do
|
295
|
+
undef_context_methods :cache_fragment_name
|
296
|
+
|
297
|
+
@context.expects :fragment_name_with_digest
|
298
|
+
|
299
|
+
render_jbuilder <<-JBUILDER
|
300
|
+
json.cache! 'cachekey' do
|
301
|
+
json.name 'Cache'
|
302
|
+
end
|
303
|
+
JBUILDER
|
304
|
+
end
|
305
|
+
|
306
|
+
test 'fragment caching works with current cache digests' do
|
307
|
+
undef_context_methods :fragment_name_with_digest
|
308
|
+
|
309
|
+
@context.expects :cache_fragment_name
|
310
|
+
ActiveSupport::Cache.expects :expand_cache_key
|
311
|
+
|
312
|
+
render_jbuilder <<-JBUILDER
|
313
|
+
json.cache! 'cachekey' do
|
314
|
+
json.name 'Cache'
|
315
|
+
end
|
316
|
+
JBUILDER
|
317
|
+
end
|
318
|
+
|
319
|
+
test 'current cache digest option accepts options' do
|
320
|
+
undef_context_methods :fragment_name_with_digest
|
321
|
+
|
322
|
+
@context.expects(:cache_fragment_name).with('cachekey', skip_digest: true)
|
323
|
+
ActiveSupport::Cache.expects :expand_cache_key
|
324
|
+
|
325
|
+
render_jbuilder <<-JBUILDER
|
326
|
+
json.cache! 'cachekey', skip_digest: true do
|
327
|
+
json.name 'Cache'
|
328
|
+
end
|
329
|
+
JBUILDER
|
330
|
+
end
|
331
|
+
|
332
|
+
test 'does not perform caching when controller.perform_caching is false' do
|
333
|
+
controller.perform_caching = false
|
334
|
+
render_jbuilder <<-JBUILDER
|
335
|
+
json.cache! 'cachekey' do
|
336
|
+
json.name 'Cache'
|
337
|
+
end
|
338
|
+
JBUILDER
|
339
|
+
|
340
|
+
assert_equal Rails.cache.inspect[/entries=(\d+)/, 1], '0'
|
341
|
+
end
|
342
|
+
|
343
|
+
end
|