sinatra_resource 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. data/VERSION +1 -1
  2. data/examples/datacatalog/lib/resource.rb +2 -2
  3. data/examples/datacatalog/lib/roles.rb +1 -1
  4. data/examples/datacatalog/resources/categories_sources.rb +43 -0
  5. data/examples/datacatalog/test/helpers/lib/request_helpers.rb +10 -2
  6. data/examples/datacatalog/test/helpers/resource_test_helper.rb +1 -1
  7. data/examples/datacatalog/test/helpers/shared/api_keys.rb +4 -4
  8. data/examples/datacatalog/test/helpers/shared/model_counts.rb +81 -0
  9. data/examples/datacatalog/test/helpers/shared/status_codes.rb +7 -3
  10. data/examples/datacatalog/test/helpers/test_helper.rb +2 -9
  11. data/examples/datacatalog/test/resources/categories/categories_delete_test.rb +5 -17
  12. data/examples/datacatalog/test/resources/categories/categories_get_many_test.rb +5 -2
  13. data/examples/datacatalog/test/resources/categories/categories_get_one_test.rb +4 -3
  14. data/examples/datacatalog/test/resources/categories/categories_post_test.rb +27 -43
  15. data/examples/datacatalog/test/resources/categories/categories_put_test.rb +9 -15
  16. data/examples/datacatalog/test/resources/categories_sources/categories_sources_delete_test.rb +148 -0
  17. data/examples/datacatalog/test/resources/categories_sources/categories_sources_get_many_test.rb +92 -0
  18. data/examples/datacatalog/test/resources/categories_sources/categories_sources_get_one_test.rb +95 -0
  19. data/examples/datacatalog/test/resources/categories_sources/categories_sources_post_test.rb +187 -0
  20. data/examples/datacatalog/test/resources/categories_sources/categories_sources_put_test.rb +323 -0
  21. data/examples/datacatalog/test/resources/sources/sources_delete_test.rb +5 -17
  22. data/examples/datacatalog/test/resources/sources/sources_get_many_test.rb +5 -2
  23. data/examples/datacatalog/test/resources/sources/sources_get_one_test.rb +4 -3
  24. data/examples/datacatalog/test/resources/sources/sources_post_test.rb +22 -35
  25. data/examples/datacatalog/test/resources/sources/sources_put_test.rb +12 -18
  26. data/examples/datacatalog/test/resources/users/users_delete_test.rb +10 -22
  27. data/examples/datacatalog/test/resources/users/users_get_many_test.rb +5 -2
  28. data/examples/datacatalog/test/resources/users/users_get_one_test.rb +4 -3
  29. data/examples/datacatalog/test/resources/users/users_post_test.rb +15 -32
  30. data/examples/datacatalog/test/resources/users/users_put_test.rb +15 -23
  31. data/lib/builder/action_definitions.rb +60 -0
  32. data/lib/builder/helpers.rb +53 -26
  33. data/lib/builder/mongo_helpers.rb +61 -10
  34. data/lib/builder.rb +136 -38
  35. data/lib/resource.rb +99 -16
  36. data/lib/sinatra_resource.rb +6 -6
  37. data/notes/permissions.mdown +6 -6
  38. data/sinatra_resource.gemspec +17 -2
  39. metadata +17 -2
@@ -0,0 +1,323 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../helpers/resource_test_helper')
2
+
3
+ class CategoriesSourcesPutResourceTest < ResourceTestCase
4
+
5
+ include DataCatalog
6
+
7
+ def app; Categories end
8
+
9
+ before do
10
+ @category = create_category
11
+ @source = create_source
12
+ @source_copy = @source.dup
13
+ @categorization = create_categorization(
14
+ :source_id => @source.id,
15
+ :category_id => @category.id
16
+ )
17
+ @other_category = create_category
18
+ @other_source = create_source
19
+ @other_categorization = create_categorization(
20
+ :source_id => @other_source.id,
21
+ :category_id => @other_category.id
22
+ )
23
+ @valid_params = {
24
+ :title => "Changed Source",
25
+ :url => "http://updated.com/details/7"
26
+ }
27
+ @extra_admin_params = { :raw => { "key" => "value" } }
28
+ end
29
+
30
+ after do
31
+ @other_categorization.destroy
32
+ @other_source.destroy
33
+ @other_category.destroy
34
+ @categorization.destroy
35
+ @source.destroy
36
+ @category.destroy
37
+ end
38
+
39
+ context "put /:id/sources/:id" do
40
+ context "anonymous" do
41
+ before do
42
+ put "/#{@category.id}/sources/#{@source.id}"
43
+ end
44
+
45
+ use "return 401 because the API key is missing"
46
+ use "source unchanged"
47
+ end
48
+
49
+ context "incorrect API key" do
50
+ before do
51
+ put "/#{@category.id}/sources/#{@source.id}", :api_key => BAD_API_KEY
52
+ end
53
+
54
+ use "return 401 because the API key is invalid"
55
+ use "source unchanged"
56
+ end
57
+ end
58
+
59
+ %w(basic).each do |role|
60
+ context "#{role} : put /:fake_id/sources/:fake_id" do
61
+ before do
62
+ put "/#{FAKE_ID}/sources/#{FAKE_ID}", :api_key => api_key_for(role)
63
+ end
64
+
65
+ use "return 404 Not Found with empty response body"
66
+ use "source unchanged"
67
+ end
68
+
69
+ context "#{role} : put /:fake_id/sources/:id" do
70
+ before do
71
+ put "/#{FAKE_ID}/sources/#{@source.id}", :api_key => api_key_for(role)
72
+ end
73
+
74
+ use "return 404 Not Found with empty response body"
75
+ use "source unchanged"
76
+ end
77
+
78
+ context "#{role} : put /:id/sources/:fake_id" do
79
+ before do
80
+ put "/#{@category.id}/sources/#{FAKE_ID}", :api_key => api_key_for(role)
81
+ end
82
+
83
+ use "return 401 because the API key is unauthorized"
84
+ use "source unchanged"
85
+ end
86
+
87
+ context "#{role} : put /:id/sources/:not_related_id" do
88
+ before do
89
+ put "/#{@category.id}/sources/#{@other_source.id}",
90
+ :api_key => api_key_for(role)
91
+ end
92
+
93
+ use "return 401 because the API key is unauthorized"
94
+ use "source unchanged"
95
+ end
96
+
97
+ context "#{role} : put /:id/sources/:id" do
98
+ before do
99
+ put "/#{@category.id}/sources/#{@source.id}", :api_key => api_key_for(role)
100
+ end
101
+
102
+ use "return 401 because the API key is unauthorized"
103
+ use "source unchanged"
104
+ end
105
+ end
106
+
107
+ %w(curator).each do |role|
108
+ context "#{role} : put /:fake_id/sources/:fake_id" do
109
+ before do
110
+ put "/#{FAKE_ID}/sources/#{FAKE_ID}", :api_key => api_key_for(role)
111
+ end
112
+
113
+ use "return 404 Not Found with empty response body"
114
+ use "source unchanged"
115
+ end
116
+
117
+ context "#{role} : put /:fake_id/sources/:id" do
118
+ before do
119
+ put "/#{FAKE_ID}/sources/#{@source.id}", :api_key => api_key_for(role)
120
+ end
121
+
122
+ use "return 404 Not Found with empty response body"
123
+ use "source unchanged"
124
+ end
125
+
126
+ context "#{role} : put /:id/sources/:fake_id" do
127
+ before do
128
+ put "/#{@category.id}/sources/#{FAKE_ID}", :api_key => api_key_for(role)
129
+ end
130
+
131
+ use "return 404 Not Found with empty response body"
132
+ use "source unchanged"
133
+ end
134
+
135
+ context "#{role} : put /:id/sources/:not_related_id" do
136
+ before do
137
+ put "/#{@category.id}/sources/#{@other_source.id}",
138
+ :api_key => api_key_for(role)
139
+ end
140
+
141
+ use "return 404 Not Found with empty response body"
142
+ use "source unchanged"
143
+ end
144
+
145
+ [:raw, :created_at, :updated_at].each do |invalid|
146
+ context "#{role} : put /:id/sources/:id with #{invalid}" do
147
+ before do
148
+ put "/#{@category.id}/sources/#{@source.id}",
149
+ valid_params_for(role).merge(invalid => 9)
150
+ end
151
+
152
+ use "return 400 Bad Request"
153
+ use "source unchanged"
154
+ invalid_param invalid
155
+ end
156
+ end
157
+
158
+ [:title, :url].each do |erase|
159
+ context "#{role} : put /:id/sources/:id but blanking out #{erase}" do
160
+ before do
161
+ put "/#{@category.id}/sources/#{@source.id}",
162
+ valid_params_for(role).merge(erase => "")
163
+ end
164
+
165
+ use "return 400 Bad Request"
166
+ use "source unchanged"
167
+ missing_param erase
168
+ end
169
+ end
170
+
171
+ [:title, :url].each do |missing|
172
+ context "#{role} : put /:id/sources/:id without #{missing}" do
173
+ before do
174
+ put "/#{@category.id}/sources/#{@source.id}",
175
+ valid_params_for(role).delete_if { |k, v| k == missing }
176
+ end
177
+
178
+ use "return 200 Ok"
179
+ doc_properties %w(title url raw id created_at updated_at)
180
+
181
+ test "should change correct fields in database" do
182
+ source = Source.find_by_id(@source.id)
183
+ @valid_params.each_pair do |key, value|
184
+ assert_equal(value, source[key]) if key != missing
185
+ end
186
+ assert_equal @source_copy[missing], source[missing]
187
+ end
188
+ end
189
+ end
190
+
191
+ context "#{role} : put /:id/sources/:id with valid params" do
192
+ before do
193
+ put "/#{@category.id}/sources/#{@source.id}", valid_params_for(role)
194
+ end
195
+
196
+ use "return 200 Ok"
197
+ doc_properties %w(title url raw id created_at updated_at)
198
+
199
+ test "should change correct fields in database" do
200
+ source = Source.find_by_id(@source.id)
201
+ @valid_params.each_pair do |key, value|
202
+ assert_equal value, source[key]
203
+ end
204
+ end
205
+ end
206
+ end
207
+
208
+ %w(admin).each do |role|
209
+ context "#{role} : put /:fake_id/sources/:fake_id" do
210
+ before do
211
+ put "/#{FAKE_ID}/sources/#{FAKE_ID}",
212
+ valid_params_for(role).merge(@extra_admin_params)
213
+ end
214
+
215
+ use "return 404 Not Found with empty response body"
216
+ use "source unchanged"
217
+ end
218
+
219
+ context "#{role} : put /:fake_id/sources/:id" do
220
+ before do
221
+ put "/#{FAKE_ID}/sources/#{@source.id}",
222
+ valid_params_for(role).merge(@extra_admin_params)
223
+ end
224
+
225
+ use "return 404 Not Found with empty response body"
226
+ use "source unchanged"
227
+ end
228
+
229
+ context "#{role} : put /:id/sources/:fake_id" do
230
+ before do
231
+ put "/#{@category.id}/sources/#{FAKE_ID}",
232
+ valid_params_for(role).merge(@extra_admin_params)
233
+ end
234
+
235
+ use "return 404 Not Found with empty response body"
236
+ use "source unchanged"
237
+ end
238
+
239
+ context "#{role} : put /:id/sources/:not_related_id" do
240
+ before do
241
+ put "/#{@category.id}/sources/#{@other_source.id}",
242
+ valid_params_for(role).merge(@extra_admin_params)
243
+ end
244
+
245
+ use "return 404 Not Found with empty response body"
246
+ use "source unchanged"
247
+ end
248
+
249
+ [:created_at, :updated_at].each do |invalid|
250
+ context "#{role} : put /:id/sources/:id but with #{invalid}" do
251
+ before do
252
+ put "/#{@category.id}/sources/#{@source.id}",
253
+ valid_params_for(role).merge(@extra_admin_params).merge(invalid => 9)
254
+ end
255
+
256
+ use "return 400 Bad Request"
257
+ use "source unchanged"
258
+ invalid_param invalid
259
+ end
260
+ end
261
+
262
+ [:title, :url].each do |erase|
263
+ context "#{role} : put /:id/sources/:id but blanking out #{erase}" do
264
+ before do
265
+ put "/#{@category.id}/sources/#{@source.id}",
266
+ valid_params_for(role).merge(@extra_admin_params).merge(erase => "")
267
+ end
268
+
269
+ use "return 400 Bad Request"
270
+ use "source unchanged"
271
+ missing_param erase
272
+ end
273
+ end
274
+
275
+ context "#{role} : put /:id/sources/:id with no params" do
276
+ before do
277
+ put "/#{@category.id}/sources/#{@source.id}",
278
+ :api_key => api_key_for(role)
279
+ end
280
+
281
+ use "return 400 because no params were given"
282
+ use "source unchanged"
283
+ end
284
+
285
+ [:title, :url].each do |missing|
286
+ context "#{role} : put /:id/sources/:id without #{missing}" do
287
+ before do
288
+ put "/#{@category.id}/sources/#{@source.id}",
289
+ valid_params_for(role).merge(@extra_admin_params).delete_if { |k, v| k == missing }
290
+ end
291
+
292
+ use "return 200 Ok"
293
+ doc_properties %w(title url raw id created_at updated_at)
294
+
295
+ test "should change correct fields in database" do
296
+ source = Source.find_by_id(@source.id)
297
+ @valid_params.merge(@extra_admin_params).each_pair do |key, value|
298
+ assert_equal(value, source[key]) if key != missing
299
+ end
300
+ assert_equal @source_copy[missing], source[missing]
301
+ end
302
+ end
303
+ end
304
+
305
+ context "#{role} : put /:id/sources/:id" do
306
+ before do
307
+ put "/#{@category.id}/sources/#{@source.id}",
308
+ valid_params_for(role).merge(@extra_admin_params)
309
+ end
310
+
311
+ use "return 200 Ok"
312
+ doc_properties %w(title url raw id created_at updated_at)
313
+
314
+ test "should change all fields in database" do
315
+ source = Source.find_by_id(@source.id)
316
+ @valid_params.merge(@extra_admin_params).each_pair do |key, value|
317
+ assert_equal value, source[key]
318
+ end
319
+ end
320
+ end
321
+ end
322
+
323
+ end
@@ -14,18 +14,6 @@ class SourcesDeleteResourceTest < ResourceTestCase
14
14
  after do
15
15
  @source.destroy
16
16
  end
17
-
18
- shared "no change in source count" do
19
- test "should not change number of source documents in database" do
20
- assert_equal @source_count, Source.all.length
21
- end
22
- end
23
-
24
- shared "one less source" do
25
- test "should remove one source document from database" do
26
- assert_equal @source_count - 1, Source.all.length
27
- end
28
- end
29
17
 
30
18
  context "delete /:id" do
31
19
  context "anonymous" do
@@ -53,7 +41,7 @@ class SourcesDeleteResourceTest < ResourceTestCase
53
41
  delete "/#{FAKE_ID}", :api_key => api_key_for(role)
54
42
  end
55
43
 
56
- use "return 401 Unauthorized"
44
+ use "return 401 because the API key is unauthorized"
57
45
  use "no change in source count"
58
46
  end
59
47
 
@@ -64,7 +52,7 @@ class SourcesDeleteResourceTest < ResourceTestCase
64
52
  :key => "value"
65
53
  end
66
54
 
67
- use "return 401 Unauthorized"
55
+ use "return 401 because the API key is unauthorized"
68
56
  use "no change in source count"
69
57
  end
70
58
 
@@ -73,7 +61,7 @@ class SourcesDeleteResourceTest < ResourceTestCase
73
61
  delete "/#{@source.id}", :api_key => api_key_for(role)
74
62
  end
75
63
 
76
- use "return 401 Unauthorized"
64
+ use "return 401 because the API key is unauthorized"
77
65
  use "no change in source count"
78
66
  end
79
67
  end
@@ -84,7 +72,7 @@ class SourcesDeleteResourceTest < ResourceTestCase
84
72
  delete "/#{FAKE_ID}", :api_key => api_key_for(role)
85
73
  end
86
74
 
87
- use "return 404 Not Found"
75
+ use "return 404 Not Found with empty response body"
88
76
  use "no change in source count"
89
77
  end
90
78
 
@@ -95,7 +83,7 @@ class SourcesDeleteResourceTest < ResourceTestCase
95
83
  :key => "value"
96
84
  end
97
85
 
98
- use "return 400 because parameters were not empty"
86
+ use "return 400 because params were not empty"
99
87
  use "no change in source count"
100
88
  end
101
89
 
@@ -2,16 +2,19 @@ require File.expand_path(File.dirname(__FILE__) + '/../../helpers/resource_test_
2
2
 
3
3
  class SourcesGetManyResourceTest < ResourceTestCase
4
4
 
5
- def app; DataCatalog::Sources end
5
+ include DataCatalog
6
+
7
+ def app; Sources end
6
8
 
7
9
  before do
10
+ raise "Unexpected Source count" unless Source.count == 0
8
11
  @sources = 3.times.map do |i|
9
12
  create_source(:title => "Source #{i}")
10
13
  end
11
14
  end
12
15
 
13
16
  after do
14
- @sources.each { |x| x.destroy }
17
+ @sources.each { |x| x.destroy } if @sources
15
18
  end
16
19
 
17
20
  SOURCES = ["Source 0", "Source 1", "Source 2"].sort
@@ -2,7 +2,9 @@ require File.expand_path(File.dirname(__FILE__) + '/../../helpers/resource_test_
2
2
 
3
3
  class SourcesGetOneResourceTest < ResourceTestCase
4
4
 
5
- def app; DataCatalog::Sources end
5
+ include DataCatalog
6
+
7
+ def app; Sources end
6
8
 
7
9
  before do
8
10
  @source = create_source
@@ -36,8 +38,7 @@ class SourcesGetOneResourceTest < ResourceTestCase
36
38
  get "/#{FAKE_ID}", :api_key => api_key_for(role)
37
39
  end
38
40
 
39
- use "return 404 Not Found"
40
- use "return an empty response body"
41
+ use "return 404 Not Found with empty response body"
41
42
  end
42
43
 
43
44
  context "#{role} : get /:id" do
@@ -15,27 +15,6 @@ class SourcesPostResourceTest < ResourceTestCase
15
15
  @extra_admin_params = { :raw => { "key" => "value" } }
16
16
  end
17
17
 
18
- shared "no new sources" do
19
- test "should not change number of source documents in database" do
20
- assert_equal @source_count, Source.all.length
21
- end
22
- end
23
-
24
- shared "one new source" do
25
- test "should add one source document to database" do
26
- assert_equal @source_count + 1, Source.all.length
27
- end
28
- end
29
-
30
- shared "correct Location header" do
31
- test "should set Location header correctly" do
32
- base_uri = Config.environment_config["base_uri"]
33
- path = %(/sources/#{parsed_response_body["id"]})
34
- expected = URI.join(base_uri, path).to_s
35
- assert_equal expected, last_response.headers['Location']
36
- end
37
- end
38
-
39
18
  context "post /" do
40
19
  context "anonymous" do
41
20
  before do
@@ -43,7 +22,7 @@ class SourcesPostResourceTest < ResourceTestCase
43
22
  end
44
23
 
45
24
  use "return 401 because the API key is missing"
46
- use "no new sources"
25
+ use "no change in source count"
47
26
  end
48
27
 
49
28
  context "incorrect API key" do
@@ -52,7 +31,7 @@ class SourcesPostResourceTest < ResourceTestCase
52
31
  end
53
32
 
54
33
  use "return 401 because the API key is invalid"
55
- use "no new sources"
34
+ use "no change in source count"
56
35
  end
57
36
  end
58
37
 
@@ -63,8 +42,8 @@ class SourcesPostResourceTest < ResourceTestCase
63
42
  post "/", valid_params_for(role).delete_if { |k, v| k == missing }
64
43
  end
65
44
 
66
- use "return 401 Unauthorized"
67
- use "no new sources"
45
+ use "return 401 because the API key is unauthorized"
46
+ use "no change in source count"
68
47
  end
69
48
  end
70
49
 
@@ -74,8 +53,8 @@ class SourcesPostResourceTest < ResourceTestCase
74
53
  post "/", valid_params_for(role).merge(invalid => 9)
75
54
  end
76
55
 
77
- use "return 401 Unauthorized"
78
- use "no new sources"
56
+ use "return 401 because the API key is unauthorized"
57
+ use "no change in source count"
79
58
  end
80
59
  end
81
60
 
@@ -84,8 +63,8 @@ class SourcesPostResourceTest < ResourceTestCase
84
63
  post "/", valid_params_for(role)
85
64
  end
86
65
 
87
- use "return 401 Unauthorized"
88
- use "no new sources"
66
+ use "return 401 because the API key is unauthorized"
67
+ use "no change in source count"
89
68
  end
90
69
  end
91
70
 
@@ -97,7 +76,7 @@ class SourcesPostResourceTest < ResourceTestCase
97
76
  end
98
77
 
99
78
  use "return 400 Bad Request"
100
- use "no new sources"
79
+ use "no change in source count"
101
80
  missing_param missing
102
81
  end
103
82
  end
@@ -109,7 +88,7 @@ class SourcesPostResourceTest < ResourceTestCase
109
88
  end
110
89
 
111
90
  use "return 400 Bad Request"
112
- use "no new sources"
91
+ use "no change in source count"
113
92
  invalid_param invalid
114
93
  end
115
94
  end
@@ -118,9 +97,13 @@ class SourcesPostResourceTest < ResourceTestCase
118
97
  before do
119
98
  post "/", valid_params_for(role)
120
99
  end
100
+
101
+ after do
102
+ Source.find_by_id(parsed_response_body["id"]).destroy
103
+ end
121
104
 
122
105
  use "return 201 Created"
123
- use "correct Location header"
106
+ location_header "sources"
124
107
  use "one new source"
125
108
  doc_properties %w(title url raw id created_at updated_at categories)
126
109
 
@@ -143,7 +126,7 @@ class SourcesPostResourceTest < ResourceTestCase
143
126
  end
144
127
 
145
128
  use "return 400 Bad Request"
146
- use "no new sources"
129
+ use "no change in source count"
147
130
  missing_param missing
148
131
  end
149
132
  end
@@ -156,7 +139,7 @@ class SourcesPostResourceTest < ResourceTestCase
156
139
  end
157
140
 
158
141
  use "return 400 Bad Request"
159
- use "no new sources"
142
+ use "no change in source count"
160
143
  invalid_param invalid
161
144
  end
162
145
  end
@@ -165,9 +148,13 @@ class SourcesPostResourceTest < ResourceTestCase
165
148
  before do
166
149
  post "/", valid_params_for(role).merge(@extra_admin_params)
167
150
  end
151
+
152
+ after do
153
+ Source.find_by_id(parsed_response_body["id"]).destroy
154
+ end
168
155
 
169
156
  use "return 201 Created"
170
- use "correct Location header"
157
+ location_header "sources"
171
158
  use "one new source"
172
159
  doc_properties %w(title url raw id created_at updated_at categories)
173
160