sinatra_resource 0.1.0 → 0.2.0

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 (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
@@ -18,12 +18,6 @@ class CategoriesPutResourceTest < ResourceTestCase
18
18
  @category.destroy
19
19
  end
20
20
 
21
- shared "category unchanged" do
22
- test "should not change category in database" do
23
- assert_equal @category_copy, Category.find_by_id(@category.id)
24
- end
25
- end
26
-
27
21
  context "put /:id" do
28
22
  context "anonymous" do
29
23
  before do
@@ -51,7 +45,7 @@ class CategoriesPutResourceTest < ResourceTestCase
51
45
  put "/#{@category.id}", valid_params_for(role).merge(invalid => 9)
52
46
  end
53
47
 
54
- use "return 401 Unauthorized"
48
+ use "return 401 because the API key is unauthorized"
55
49
  use "category unchanged"
56
50
  end
57
51
  end
@@ -62,17 +56,17 @@ class CategoriesPutResourceTest < ResourceTestCase
62
56
  put "/#{@category.id}", valid_params_for(role).merge(erase => "")
63
57
  end
64
58
 
65
- use "return 401 Unauthorized"
59
+ use "return 401 because the API key is unauthorized"
66
60
  use "category unchanged"
67
61
  end
68
62
  end
69
63
 
70
- context "#{role} : put /:id with no parameters" do
64
+ context "#{role} : put /:id with no params" do
71
65
  before do
72
66
  put "/#{@category.id}", :api_key => api_key_for(role)
73
67
  end
74
68
 
75
- use "return 401 Unauthorized"
69
+ use "return 401 because the API key is unauthorized"
76
70
  use "category unchanged"
77
71
  end
78
72
 
@@ -81,14 +75,14 @@ class CategoriesPutResourceTest < ResourceTestCase
81
75
  put "/#{@category.id}", valid_params_for(role)
82
76
  end
83
77
 
84
- use "return 401 Unauthorized"
78
+ use "return 401 because the API key is unauthorized"
85
79
  use "category unchanged"
86
80
  end
87
81
  end
88
82
 
89
83
  %w(curator admin).each do |role|
90
84
  [:created_at, :updated_at, :sources].each do |invalid|
91
- context "#{role} : put / but with #{invalid}" do
85
+ context "#{role} : put /:id but with #{invalid}" do
92
86
  before do
93
87
  put "/#{@category.id}", valid_params_for(role).merge(invalid => 9)
94
88
  end
@@ -100,7 +94,7 @@ class CategoriesPutResourceTest < ResourceTestCase
100
94
  end
101
95
 
102
96
  [:name].each do |erase|
103
- context "#{role} : put / but blanking out #{erase}" do
97
+ context "#{role} : put /:id but blanking out #{erase}" do
104
98
  before do
105
99
  put "/#{@category.id}", valid_params_for(role).merge(erase => "")
106
100
  end
@@ -111,12 +105,12 @@ class CategoriesPutResourceTest < ResourceTestCase
111
105
  end
112
106
  end
113
107
 
114
- context "#{role} : put /:id with no parameters" do
108
+ context "#{role} : put /:id with no params" do
115
109
  before do
116
110
  put "/#{@category.id}", :api_key => api_key_for(role)
117
111
  end
118
112
 
119
- use "return 400 because no parameters were given"
113
+ use "return 400 because no params were given"
120
114
  use "category unchanged"
121
115
  end
122
116
 
@@ -0,0 +1,148 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../helpers/resource_test_helper')
2
+
3
+ class CategoriesSourcesDeleteResourceTest < ResourceTestCase
4
+
5
+ include DataCatalog
6
+
7
+ def app; Categories end
8
+
9
+ before do
10
+ @category = create_category
11
+ @source = create_source
12
+ @categorization = create_categorization(
13
+ :source_id => @source.id,
14
+ :category_id => @category.id
15
+ )
16
+ @other_category = create_category
17
+ @other_source = create_source
18
+ @other_categorization = create_categorization(
19
+ :source_id => @other_source.id,
20
+ :category_id => @other_category.id
21
+ )
22
+ @source_count = Source.all.length
23
+ end
24
+
25
+ after do
26
+ @other_categorization.destroy
27
+ @other_source.destroy
28
+ @other_category.destroy
29
+ @categorization.destroy
30
+ @source.destroy
31
+ @category.destroy
32
+ end
33
+
34
+ context "delete /:id/sources/:id" do
35
+ context "anonymous" do
36
+ before do
37
+ delete "/#{@category.id}/sources/#{@source.id}"
38
+ end
39
+
40
+ use "return 401 because the API key is missing"
41
+ end
42
+
43
+ context "incorrect API key" do
44
+ before do
45
+ delete "/#{@category.id}/sources/#{@source.id}", :api_key => BAD_API_KEY
46
+ end
47
+
48
+ use "return 401 because the API key is invalid"
49
+ end
50
+ end
51
+
52
+ %w(basic).each do |role|
53
+ context "#{role} : delete /:fake_id/sources/:fake_id" do
54
+ before do
55
+ delete "/#{FAKE_ID}/sources/#{FAKE_ID}", :api_key => api_key_for(role)
56
+ end
57
+
58
+ use "return 404 Not Found with empty response body"
59
+ use "no change in source count"
60
+ end
61
+
62
+ context "#{role} : delete /:fake_id/sources/:id" do
63
+ before do
64
+ delete "/#{FAKE_ID}/sources/#{@source.id}", :api_key => api_key_for(role)
65
+ end
66
+
67
+ use "return 404 Not Found with empty response body"
68
+ use "no change in source count"
69
+ end
70
+
71
+ context "#{role} : delete /:id/sources/:fake_id" do
72
+ before do
73
+ delete "/#{@category.id}/sources/#{FAKE_ID}", :api_key => api_key_for(role)
74
+ end
75
+
76
+ use "return 401 because the API key is unauthorized"
77
+ use "no change in source count"
78
+ end
79
+
80
+ context "#{role} : delete /:id/sources/:not_related_id" do
81
+ before do
82
+ delete "/#{@category.id}/sources/#{@other_source.id}",
83
+ :api_key => api_key_for(role)
84
+ end
85
+
86
+ use "return 401 because the API key is unauthorized"
87
+ use "no change in source count"
88
+ end
89
+
90
+ context "#{role} : delete /:id/sources/:id" do
91
+ before do
92
+ delete "/#{@category.id}/sources/#{@source.id}", :api_key => api_key_for(role)
93
+ end
94
+
95
+ use "return 401 because the API key is unauthorized"
96
+ use "no change in source count"
97
+ end
98
+ end
99
+
100
+ %w(curator admin).each do |role|
101
+ context "#{role} : delete /:fake_id/sources/:fake_id" do
102
+ before do
103
+ delete "/#{FAKE_ID}/sources/#{FAKE_ID}", :api_key => api_key_for(role)
104
+ end
105
+
106
+ use "return 404 Not Found with empty response body"
107
+ use "no change in source count"
108
+ end
109
+
110
+ context "#{role} : delete /:fake_id/sources/:id" do
111
+ before do
112
+ delete "/#{FAKE_ID}/sources/#{@source.id}", :api_key => api_key_for(role)
113
+ end
114
+
115
+ use "return 404 Not Found with empty response body"
116
+ use "no change in source count"
117
+ end
118
+
119
+ context "#{role} : delete /:id/sources/:fake_id" do
120
+ before do
121
+ delete "/#{@category.id}/sources/#{FAKE_ID}", :api_key => api_key_for(role)
122
+ end
123
+
124
+ use "return 404 Not Found with empty response body"
125
+ use "no change in source count"
126
+ end
127
+
128
+ context "#{role} : delete /:id/sources/:not_related_id" do
129
+ before do
130
+ delete "/#{@category.id}/sources/#{@other_source.id}",
131
+ :api_key => api_key_for(role)
132
+ end
133
+
134
+ use "return 404 Not Found with empty response body"
135
+ use "no change in source count"
136
+ end
137
+
138
+ context "#{role} : delete /:id/sources/:id" do
139
+ before do
140
+ delete "/#{@category.id}/sources/#{@source.id}", :api_key => api_key_for(role)
141
+ end
142
+
143
+ use "return 204 No Content"
144
+ use "one less source"
145
+ end
146
+ end
147
+
148
+ end
@@ -0,0 +1,92 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../helpers/resource_test_helper')
2
+
3
+ class CategoriesSourcesGetManyResourceTest < ResourceTestCase
4
+
5
+ include DataCatalog
6
+
7
+ def app; Categories end
8
+
9
+ before do
10
+ raise "Unexpected Source count" unless Source.count == 0
11
+ @category = create_category
12
+ @sources = 3.times.map do |i|
13
+ create_source(:title => "Source #{i}")
14
+ end
15
+ @categorizations = 3.times.map do |i|
16
+ create_categorization(
17
+ :source_id => @sources[i].id,
18
+ :category_id => @category.id
19
+ )
20
+ end
21
+ # ----
22
+ @other_category = create_category(:name => 'Other Category')
23
+ @other_sources = 3.times.map do |i|
24
+ create_source(:title => "Other Source #{i}")
25
+ end
26
+ @other_categorizations = 3.times.map do |i|
27
+ create_categorization(
28
+ :source_id => @other_sources[i].id,
29
+ :category_id => @other_category.id
30
+ )
31
+ end
32
+ end
33
+
34
+ after do
35
+ @other_categorizations.each { |x| x.destroy }
36
+ @other_sources.each { |x| x.destroy }
37
+ @other_category.destroy
38
+ @categorizations.each { |x| x.destroy }
39
+ @sources.each { |x| x.destroy }
40
+ @category.destroy
41
+ end
42
+
43
+ SOURCES = ["Source 0", "Source 1", "Source 2"].sort
44
+
45
+ context "get /:id/sources/" do
46
+ context "anonymous" do
47
+ before do
48
+ get "/#{@category.id}/sources/"
49
+ end
50
+
51
+ use "return 401 because the API key is missing"
52
+ end
53
+
54
+ context "incorrect API key" do
55
+ before do
56
+ get "/#{@category.id}/sources/", :api_key => BAD_API_KEY
57
+ end
58
+
59
+ use "return 401 because the API key is invalid"
60
+ end
61
+ end
62
+
63
+ %w(basic curator admin).each do |role|
64
+ context "#{role} : get /:fake_id/sources" do
65
+ before do
66
+ get "/#{FAKE_ID}/sources/", :api_key => api_key_for(role)
67
+ end
68
+
69
+ use "return 404 Not Found with empty response body"
70
+ end
71
+
72
+ context "#{role} : get /:id/sources" do
73
+ before do
74
+ get "/#{@category.id}/sources/", :api_key => api_key_for(role)
75
+ end
76
+
77
+ use "return 200 Ok"
78
+
79
+ test "body should have 3 sources" do
80
+ assert_equal 3, parsed_response_body.length
81
+ end
82
+
83
+ test "body should have correct source titles" do
84
+ actual = parsed_response_body.map { |e| e["title"] }
85
+ assert_equal SOURCES, actual.sort
86
+ end
87
+
88
+ docs_properties %w(title url raw id created_at updated_at)
89
+ end
90
+ end
91
+
92
+ end
@@ -0,0 +1,95 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../helpers/resource_test_helper')
2
+
3
+ class CategoriesSourcesGetOneResourceTest < ResourceTestCase
4
+
5
+ include DataCatalog
6
+
7
+ def app; Categories end
8
+
9
+ before do
10
+ @category = create_category
11
+ @source = create_source
12
+ @categorization = create_categorization(
13
+ :source_id => @source.id,
14
+ :category_id => @category.id
15
+ )
16
+ @other_category = create_category
17
+ @other_source = create_source
18
+ @other_categorization = create_categorization(
19
+ :source_id => @other_source.id,
20
+ :category_id => @other_category.id
21
+ )
22
+ end
23
+
24
+ after do
25
+ @other_categorization.destroy
26
+ @other_source.destroy
27
+ @other_category.destroy
28
+ @categorization.destroy
29
+ @source.destroy
30
+ @category.destroy
31
+ end
32
+
33
+ context "get /:id/sources/:id" do
34
+ context "anonymous" do
35
+ before do
36
+ get "/#{@category.id}/sources/#{@source.id}"
37
+ end
38
+
39
+ use "return 401 because the API key is missing"
40
+ end
41
+
42
+ context "incorrect API key" do
43
+ before do
44
+ get "/#{@category.id}/sources/#{@source.id}", :api_key => BAD_API_KEY
45
+ end
46
+
47
+ use "return 401 because the API key is invalid"
48
+ end
49
+ end
50
+
51
+ %w(basic curator admin).each do |role|
52
+ context "#{role} : get /:fake_id/sources/:fake_id" do
53
+ before do
54
+ get "/#{FAKE_ID}/sources/#{FAKE_ID}", :api_key => api_key_for(role)
55
+ end
56
+
57
+ use "return 404 Not Found with empty response body"
58
+ end
59
+
60
+ context "#{role} : get /:fake_id/sources/:id" do
61
+ before do
62
+ get "/#{FAKE_ID}/sources/#{@source.id}", :api_key => api_key_for(role)
63
+ end
64
+
65
+ use "return 404 Not Found with empty response body"
66
+ end
67
+
68
+ context "#{role} : get /:id/sources/:fake_id" do
69
+ before do
70
+ get "/#{@category.id}/sources/#{FAKE_ID}", :api_key => api_key_for(role)
71
+ end
72
+
73
+ use "return 404 Not Found with empty response body"
74
+ end
75
+
76
+ context "#{role} : get /:id/sources/:not_related_id" do
77
+ before do
78
+ get "/#{@category.id}/sources/#{@other_source.id}",
79
+ :api_key => api_key_for(role)
80
+ end
81
+
82
+ use "return 404 Not Found with empty response body"
83
+ end
84
+
85
+ context "#{role} : get /:id/sources/:id" do
86
+ before do
87
+ get "/#{@category.id}/sources/#{@source.id}", :api_key => api_key_for(role)
88
+ end
89
+
90
+ use "return 200 Ok"
91
+ doc_properties %w(title url raw id created_at updated_at)
92
+ end
93
+ end
94
+
95
+ end
@@ -0,0 +1,187 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../helpers/resource_test_helper')
2
+
3
+ class CategoriesSourcesPostResourceTest < ResourceTestCase
4
+
5
+ include DataCatalog
6
+
7
+ def app; Categories end
8
+
9
+ before do
10
+ @category = create_category
11
+ @source = create_source
12
+ @categorization = create_categorization(
13
+ :source_id => @source.id,
14
+ :category_id => @category.id
15
+ )
16
+ @source_count = Source.all.length
17
+ @valid_params = {
18
+ :title => "New Source",
19
+ :url => "http://data.gov/details/123"
20
+ }
21
+ @extra_admin_params = { :raw => { "key" => "value" } }
22
+ end
23
+
24
+ after do
25
+ @categorization.destroy
26
+ @source.destroy
27
+ @category.destroy
28
+ end
29
+
30
+ context "post /:id/sources" do
31
+ context "anonymous" do
32
+ before do
33
+ post "/#{@category.id}/sources/"
34
+ end
35
+
36
+ use "return 401 because the API key is missing"
37
+ use "no change in source count"
38
+ end
39
+
40
+ context "incorrect API key" do
41
+ before do
42
+ post "/#{@category.id}/sources/", :api_key => BAD_API_KEY
43
+ end
44
+
45
+ use "return 401 because the API key is invalid"
46
+ use "no change in source count"
47
+ end
48
+ end
49
+
50
+ %w(basic).each do |role|
51
+ context "#{role} : post /:fake_id/sources" do
52
+ before do
53
+ post "/#{FAKE_ID}/sources/", :api_key => api_key_for(role)
54
+ end
55
+
56
+ use "return 404 Not Found with empty response body"
57
+ use "no change in source count"
58
+ end
59
+
60
+ context "#{role} : post /:id/sources" do
61
+ before do
62
+ post "/#{@category.id}/sources/", :api_key => api_key_for(role)
63
+ end
64
+
65
+ use "return 401 because the API key is unauthorized"
66
+ use "no change in source count"
67
+ end
68
+ end
69
+
70
+ %w(curator).each do |role|
71
+ context "#{role} : post /:fake_id/sources" do
72
+ before do
73
+ post "/#{FAKE_ID}/sources/", :api_key => api_key_for(role)
74
+ end
75
+
76
+ use "return 404 Not Found with empty response body"
77
+ use "no change in source count"
78
+ end
79
+
80
+ [:title, :url].each do |missing|
81
+ context "#{role} : post /:id/sources/ but missing #{missing}" do
82
+ before do
83
+ post "/#{@category.id}/sources/",
84
+ valid_params_for(role).delete_if { |k, v| k == missing }
85
+ end
86
+
87
+ use "return 400 Bad Request"
88
+ use "no change in source count"
89
+ missing_param missing
90
+ end
91
+ end
92
+
93
+ [:raw, :id, :created_at, :updated_at].each do |invalid|
94
+ context "#{role} : post /:id/sources/ but with #{invalid}" do
95
+ before do
96
+ post "/#{@category.id}/sources/", valid_params_for(role).
97
+ merge(invalid => 9)
98
+ end
99
+
100
+ use "return 400 Bad Request"
101
+ use "no change in source count"
102
+ invalid_param invalid
103
+ end
104
+ end
105
+
106
+ context "#{role} : post /:id/sources with valid params" do
107
+ before do
108
+ post "/#{@category.id}/sources/", valid_params_for(role)
109
+ end
110
+
111
+ after do
112
+ Source.find_by_id(parsed_response_body["id"]).destroy
113
+ end
114
+
115
+ use "return 201 Created"
116
+ location_header "sources"
117
+ use "one new source"
118
+ doc_properties %w(title url raw id created_at updated_at)
119
+
120
+ test "source connected to category" do
121
+ source = Source.find_by_id(parsed_response_body["id"])
122
+ assert_equal [@category], source.categories
123
+ end
124
+ end
125
+ end
126
+
127
+ %w(admin).each do |role|
128
+ context "#{role} : post /:fake_id/sources" do
129
+ before do
130
+ post "/#{FAKE_ID}/sources/",
131
+ valid_params_for(role).merge(@extra_admin_params)
132
+ end
133
+
134
+ use "return 404 Not Found with empty response body"
135
+ use "no change in source count"
136
+ end
137
+
138
+ [:title, :url].each do |missing|
139
+ context "#{role} : post /:id/sources/ but missing #{missing}" do
140
+ before do
141
+ post "/#{@category.id}/sources/",
142
+ valid_params_for(role).merge(@extra_admin_params).
143
+ delete_if { |k, v| k == missing }
144
+ end
145
+
146
+ use "return 400 Bad Request"
147
+ use "no change in source count"
148
+ missing_param missing
149
+ end
150
+ end
151
+
152
+ [:id, :created_at, :updated_at].each do |invalid|
153
+ context "#{role} : post /:id/sources/ but with #{invalid}" do
154
+ before do
155
+ post "/#{@category.id}/sources/", valid_params_for(role).
156
+ merge(@extra_admin_params).merge(invalid => 9)
157
+ end
158
+
159
+ use "return 400 Bad Request"
160
+ use "no change in source count"
161
+ invalid_param invalid
162
+ end
163
+ end
164
+
165
+ context "#{role} : post /:id/sources with valid params" do
166
+ before do
167
+ post "/#{@category.id}/sources/",
168
+ valid_params_for(role).merge(@extra_admin_params)
169
+ end
170
+
171
+ after do
172
+ Source.find_by_id(parsed_response_body["id"]).destroy
173
+ end
174
+
175
+ use "return 201 Created"
176
+ location_header "sources"
177
+ use "one new source"
178
+ doc_properties %w(title url raw id created_at updated_at)
179
+
180
+ test "source connected to category" do
181
+ source = Source.find_by_id(parsed_response_body["id"])
182
+ assert_equal [@category], source.categories
183
+ end
184
+ end
185
+ end
186
+
187
+ end