standardapi 6.0.0.32 → 7.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +80 -58
  3. data/lib/standard_api/access_control_list.rb +148 -0
  4. data/lib/standard_api/controller.rb +116 -27
  5. data/lib/standard_api/helpers.rb +17 -10
  6. data/lib/standard_api/includes.rb +9 -0
  7. data/lib/standard_api/middleware/query_encoding.rb +3 -3
  8. data/lib/standard_api/middleware.rb +5 -0
  9. data/lib/standard_api/railtie.rb +30 -2
  10. data/lib/standard_api/route_helpers.rb +64 -14
  11. data/lib/standard_api/test_case/calculate_tests.rb +15 -8
  12. data/lib/standard_api/test_case/create_tests.rb +7 -9
  13. data/lib/standard_api/test_case/destroy_tests.rb +19 -7
  14. data/lib/standard_api/test_case/index_tests.rb +10 -6
  15. data/lib/standard_api/test_case/schema_tests.rb +7 -1
  16. data/lib/standard_api/test_case/show_tests.rb +8 -7
  17. data/lib/standard_api/test_case/update_tests.rb +15 -15
  18. data/lib/standard_api/test_case.rb +13 -3
  19. data/lib/standard_api/version.rb +1 -1
  20. data/lib/standard_api/views/application/_record.json.jbuilder +18 -17
  21. data/lib/standard_api/views/application/_record.streamer +40 -37
  22. data/lib/standard_api/views/application/_schema.json.jbuilder +20 -8
  23. data/lib/standard_api/views/application/_schema.streamer +22 -8
  24. data/lib/standard_api/views/application/new.streamer +1 -1
  25. data/lib/standard_api.rb +5 -0
  26. data/test/standard_api/caching_test.rb +14 -4
  27. data/test/standard_api/controller/include_test.rb +107 -0
  28. data/test/standard_api/controller/subresource_test.rb +157 -0
  29. data/test/standard_api/helpers_test.rb +34 -17
  30. data/test/standard_api/nested_attributes/belongs_to_test.rb +71 -0
  31. data/test/standard_api/nested_attributes/has_and_belongs_to_many_test.rb +70 -0
  32. data/test/standard_api/nested_attributes/has_many_test.rb +85 -0
  33. data/test/standard_api/nested_attributes/has_one_test.rb +71 -0
  34. data/test/standard_api/route_helpers_test.rb +56 -0
  35. data/test/standard_api/standard_api_test.rb +182 -44
  36. data/test/standard_api/test_app/app/controllers/acl/account_acl.rb +15 -0
  37. data/test/standard_api/test_app/app/controllers/acl/camera_acl.rb +7 -0
  38. data/test/standard_api/test_app/app/controllers/acl/photo_acl.rb +13 -0
  39. data/test/standard_api/test_app/app/controllers/acl/property_acl.rb +33 -0
  40. data/test/standard_api/test_app/app/controllers/acl/reference_acl.rb +7 -0
  41. data/test/standard_api/test_app/controllers.rb +28 -43
  42. data/test/standard_api/test_app/models.rb +76 -7
  43. data/test/standard_api/test_app/test/factories.rb +7 -3
  44. data/test/standard_api/test_app/views/photos/_photo.json.jbuilder +1 -0
  45. data/test/standard_api/test_app/views/photos/_photo.streamer +2 -1
  46. data/test/standard_api/test_app/views/sessions/create.json.jbuilder +1 -0
  47. data/test/standard_api/test_app/views/sessions/create.streamer +3 -0
  48. data/test/standard_api/test_app.rb +12 -1
  49. data/test/standard_api/test_helper.rb +21 -0
  50. metadata +59 -16
@@ -0,0 +1,71 @@
1
+ require 'standard_api/test_helper'
2
+
3
+ module NestedAttributes
4
+ class BelongsToTest < ActionDispatch::IntegrationTest
5
+ # include StandardAPI::TestCase
6
+ include StandardAPI::Helpers
7
+
8
+
9
+ # = Create Test
10
+
11
+ test 'create record and create nested record' do
12
+ @controller = PhotosController.new
13
+ post photos_path, params: { photo: { account: {name: 'Big Ben'}} }, as: :json
14
+
15
+ assert_response :created
16
+ photo = Photo.last
17
+ assert_equal 'Big Ben', photo.account.name
18
+ end
19
+
20
+ test 'create record and update nested record' do
21
+ account = create(:account, name: 'Big Ben')
22
+
23
+ @controller = PhotosController.new
24
+ post photos_path, params: { photo: { account: {id: account.id, name: 'Little Jimmie'}} }, as: :json
25
+
26
+
27
+ assert_response :created
28
+ photo = Photo.last
29
+ assert_equal account.id, photo.account_id
30
+ assert_equal 'Little Jimmie', photo.account.name
31
+ end
32
+
33
+ # = Update Test
34
+
35
+ test 'update record and create nested record' do
36
+ photo = create(:photo)
37
+
38
+ @controller = PhotosController.new
39
+ put photo_path(photo), params: { photo: { account: {name: 'Big Ben'}} }, as: :json
40
+
41
+ assert_response :ok
42
+ photo.reload
43
+ assert_equal 'Big Ben', photo.account.name
44
+ end
45
+
46
+ test 'update record and update nested record' do
47
+ account = create(:account, name: 'Big Ben')
48
+ photo = create(:photo, account: account)
49
+
50
+ @controller = PhotosController.new
51
+ put photo_path(photo), params: { photo: { account: {name: 'Little Jimmie'}} }, as: :json
52
+
53
+ assert_response :ok
54
+ photo.reload
55
+ assert_equal 'Little Jimmie', photo.account.name
56
+ end
57
+
58
+ test 'update record and set relation to nil' do
59
+ account = create(:account, name: 'Big Ben')
60
+ photo = create(:photo, account: account)
61
+
62
+ @controller = PhotosController.new
63
+ put photo_path(photo), params: { photo: { account: nil} }, as: :json
64
+
65
+ assert_response :ok
66
+ photo.reload
67
+ assert_nil photo.account
68
+ end
69
+
70
+ end
71
+ end
@@ -0,0 +1,70 @@
1
+ require 'standard_api/test_helper'
2
+
3
+ module NestedAttributes
4
+ class HasAndBelongsToManyTest < ActionDispatch::IntegrationTest
5
+ # include StandardAPI::TestCase
6
+ include StandardAPI::Helpers
7
+
8
+
9
+ # = Create Test
10
+
11
+ test 'create record and create nested record' do
12
+ @controller = PropertiesController.new
13
+ post properties_path, params: { property: { name: 'Beach House', photos: [{format: 'image/jpeg'}]} }, as: :json
14
+
15
+ assert_response :created
16
+ property = Property.last
17
+ assert_equal 'Beach House', property.name
18
+ assert_equal ['image/jpeg'], property.photos.map(&:format)
19
+ end
20
+
21
+ test 'create record and update nested record' do
22
+ photo = create(:photo, format: 'image/png')
23
+
24
+ @controller = PropertiesController.new
25
+ post properties_path, params: { property: { name: 'Beach House', photos: [{id: photo.id, format: 'image/jpeg'}] } }, as: :json
26
+
27
+ assert_response :created
28
+ property = Property.last
29
+ assert_equal 'Beach House', property.name
30
+ assert_equal [photo.id], property.photos.map(&:id)
31
+ assert_equal ['image/jpeg'], property.photos.map(&:format)
32
+ assert_equal 'image/jpeg', photo.reload.format
33
+ end
34
+
35
+ # = Update Test
36
+
37
+ test 'update record and create nested record' do
38
+ property = create(:property)
39
+
40
+ @controller = PropertiesController.new
41
+ put property_path(property), params: { property: { photos: [{format: "image/tiff"}]} }, as: :json
42
+
43
+ assert_response :ok
44
+ assert_equal ["image/tiff"], property.reload.photos.map(&:format)
45
+ end
46
+
47
+ test 'update record and update nested record' do
48
+ photo = create(:photo, format: 'image/gif')
49
+ property = create(:property, photos: [photo])
50
+
51
+ @controller = PropertiesController.new
52
+ put property_path(property), params: { property: { photos: [{id: photo.id, format: "image/heic"}]} }, as: :json
53
+
54
+ assert_response :ok
55
+ assert_equal ['image/heic'], property.reload.photos.map(&:format)
56
+ end
57
+
58
+ test 'update record and set relation to an empty array' do
59
+ photo = create(:photo, format: 'image/gif')
60
+ property = create(:property, photos: [photo])
61
+
62
+ @controller = PropertiesController.new
63
+ put property_path(property), params: { property: { photos: [] } }, as: :json
64
+
65
+ assert_response :ok
66
+ assert_equal [], property.reload.photos
67
+ end
68
+
69
+ end
70
+ end
@@ -0,0 +1,85 @@
1
+ require 'standard_api/test_helper'
2
+
3
+ module NestedAttributes
4
+ class HasManyTest < ActionDispatch::IntegrationTest
5
+ # include StandardAPI::TestCase
6
+ include StandardAPI::Helpers
7
+
8
+
9
+ # = Create Test
10
+
11
+ test 'create record and create nested record' do
12
+ @controller = PropertiesController.new
13
+ post properties_path, params: { property: { name: 'Beach House', accounts: [{name: 'Billabong'}]} }, as: :json
14
+
15
+ assert_response :created
16
+ property = Property.last
17
+ assert_equal 'Beach House', property.name
18
+ assert_equal ['Billabong'], property.accounts.map(&:name)
19
+ end
20
+
21
+ test 'create record and update nested record' do
22
+ account = create(:account, name: 'Coco Chanel')
23
+
24
+ @controller = PropertiesController.new
25
+ post properties_path, params: { property: { name: 'Beach House', accounts: [{id: account.id, name: 'Crazy Chanel'}]} }, as: :json
26
+
27
+ assert_response :created
28
+ property = Property.last
29
+ assert_equal [account.id], property.accounts.map(&:id)
30
+ assert_equal ['Crazy Chanel'], property.accounts.map(&:name)
31
+ assert_equal 'Crazy Chanel', account.reload.name
32
+ end
33
+
34
+ # = Update Test
35
+
36
+ test 'update record and create nested record' do
37
+ property = create(:property)
38
+
39
+ @controller = PropertiesController.new
40
+ put property_path(property), params: { property: { accounts: [{name: "Hersey's"}]} }, as: :json
41
+
42
+ assert_response :ok
43
+ assert_equal ["Hersey's"], property.accounts.map(&:name)
44
+ end
45
+
46
+ test 'update record and update nested record' do
47
+ account = create(:account, name: 'A Co.')
48
+ property = create(:property, name: 'Empire State Building', accounts: [account])
49
+
50
+ @controller = PropertiesController.new
51
+ put property_path(property), params: { property: { name: 'John Hancock Center', accounts: [{id: account.id, name: "B Co."}]} }, as: :json
52
+
53
+ assert_response :ok
54
+ property.reload
55
+ assert_equal 'John Hancock Center', property.name
56
+ assert_equal ['B Co.'], property.accounts.map(&:name)
57
+ end
58
+
59
+ test 'update record and set relation to an empty array' do
60
+ account = create(:account, name: 'A Co.')
61
+ property = create(:property, name: 'Empire State Building', accounts: [account])
62
+
63
+ @controller = PropertiesController.new
64
+ put property_path(property), params: { property: { accounts: [] } }, as: :json
65
+
66
+ assert_response :ok
67
+ property.reload
68
+ assert_equal [], property.accounts
69
+ end
70
+
71
+ test 'update record and include nested record in response' do
72
+ account = create(:account, name: 'A Co.')
73
+ property = create(:property, name: 'Empire State Building', accounts: [account])
74
+
75
+ @controller = PropertiesController.new
76
+ put property_path(property), params: { property: { name: 'John Hancock Center', accounts: [{id: account.id, name: "B Co."}]} }, as: :json
77
+
78
+ attributes = JSON.parse(response.body)
79
+ assert_response :ok
80
+ assert_equal account.id, attributes["accounts"][0]["id"]
81
+ assert_equal "B Co.", attributes["accounts"][0]["name"]
82
+ end
83
+
84
+ end
85
+ end
@@ -0,0 +1,71 @@
1
+ require 'standard_api/test_helper'
2
+
3
+ module NestedAttributes
4
+ class HasOneTest < ActionDispatch::IntegrationTest
5
+ # include StandardAPI::TestCase
6
+ include StandardAPI::Helpers
7
+
8
+
9
+ # = Create Test
10
+
11
+ test 'create record and create nested record' do
12
+ @controller = PhotosController.new
13
+
14
+ post photos_path, params: { photo: { camera: {make: 'Sony'}} }, as: :json
15
+
16
+ photo = Photo.last
17
+ assert_equal photo.id, photo.camera.photo_id
18
+ assert_equal 'Sony', photo.camera.make
19
+ end
20
+
21
+ test 'create record and update nested record' do
22
+ camera = create(:camera, make: 'Sony')
23
+
24
+ @controller = PhotosController.new
25
+ post photos_path, params: { photo: { camera: {id: camera.id, make: 'Nokia'} } }, as: :json
26
+
27
+ assert_response :created
28
+ photo = Photo.last
29
+ assert_equal photo.id, photo.camera.photo_id
30
+ assert_equal 'Nokia', photo.camera.make
31
+ end
32
+
33
+ # = Update Test
34
+
35
+ test 'update record and create nested record' do
36
+ photo = create(:photo)
37
+
38
+ @controller = PhotosController.new
39
+ put photo_path(photo), params: { photo: { camera: {make: 'Canon'}} }, as: :json
40
+
41
+ assert_response :ok
42
+ photo.reload
43
+ assert_equal 'Canon', photo.camera.make
44
+ end
45
+
46
+ test 'update record and update nested record' do
47
+ camera = create(:camera, make: 'Leica')
48
+ photo = create(:photo, camera: camera)
49
+
50
+ @controller = PhotosController.new
51
+ put photo_path(photo), params: { photo: { camera: {make: 'Nokia'}} }, as: :json
52
+
53
+ assert_response :ok
54
+ photo.reload
55
+ assert_equal 'Nokia', photo.camera.make
56
+ end
57
+
58
+ test 'update record and set relation to nil' do
59
+ camera = create(:camera, make: 'Leica')
60
+ photo = create(:photo, camera: camera)
61
+
62
+ @controller = PhotosController.new
63
+ put photo_path(photo), params: { photo: { camera: nil} }, as: :json
64
+
65
+ assert_response :ok
66
+ photo.reload
67
+ assert_nil photo.camera
68
+ end
69
+
70
+ end
71
+ end
@@ -30,4 +30,60 @@ class RouteHelpersTest < ActionDispatch::IntegrationTest
30
30
  assert_routing({ path: '/photos/1/properties/1', method: :delete }, { controller: 'photos', action: 'remove_resource', id: '1', relationship: 'properties', resource_id: '1' })
31
31
  end
32
32
 
33
+ test "standard_resources with only option" do
34
+ with_routing do |set|
35
+ set.draw do
36
+ standard_resources :photos, only: [ :index, :show ]
37
+ end
38
+ assert_routing({ path: '/photos', method: :get }, { controller: 'photos', action: 'index' })
39
+ assert_routing({ path: '/photos/1', method: :get }, { controller: 'photos', action: 'show', id: '1' })
40
+ assert_equal 2, set.routes.size
41
+ end
42
+ end
43
+
44
+ test "standard_resources with except option" do
45
+ with_routing do |set|
46
+ set.draw do
47
+ standard_resources :photos, except: [ :destroy ]
48
+ end
49
+ assert_routing({ path: '/photos', method: :get }, { controller: 'photos', action: 'index' })
50
+ assert_routing({ path: '/photos/1', method: :get }, { controller: 'photos', action: 'show', id: '1' })
51
+ assert_routing({ path: '/photos/new', method: :get }, { controller: 'photos', action: 'new' })
52
+ assert_routing({ path: '/photos', method: :post }, { controller: 'photos', action: 'create' })
53
+ assert_routing({ path: '/photos/1', method: :put }, { controller: 'photos', action: 'update', id: '1' })
54
+ assert_routing({ path: '/photos/1', method: :patch }, { controller: 'photos', action: 'update', id: '1' })
55
+ assert_routing({ path: '/photos/schema', method: :get }, { controller: 'photos', action: 'schema' })
56
+ assert_routing({ path: '/photos/calculate', method: :get }, { controller: 'photos', action: 'calculate' })
57
+ assert_routing({ path: '/photos/1/properties/1', method: :post }, { controller: 'photos', action: 'add_resource', id: '1', relationship: 'properties', resource_id: '1' })
58
+ assert_routing({ path: '/photos/1/properties/1', method: :delete }, { controller: 'photos', action: 'remove_resource', id: '1', relationship: 'properties', resource_id: '1' })
59
+ assert_routing({ path: '/photos/1/properties', method: :post }, { controller: 'photos', action: 'create_resource', id: '1', relationship: 'properties' })
60
+ assert_equal 12, set.routes.size
61
+ end
62
+ end
63
+
64
+ test "standard_resource with only option" do
65
+ with_routing do |set|
66
+ set.draw do
67
+ standard_resource :accounts, only: :show
68
+ end
69
+ assert_routing({ path: '/accounts', method: :get }, { controller: 'accounts', action: 'show' })
70
+ assert_equal 1, set.routes.size
71
+ end
72
+ end
73
+
74
+ test "standard_resource with except option" do
75
+ with_routing do |set|
76
+ set.draw do
77
+ standard_resource :accounts, except: [ :destroy ]
78
+ end
79
+ assert_routing({ path: '/accounts', method: :get }, { controller: 'accounts', action: 'show' })
80
+ assert_routing({ path: '/accounts/new', method: :get }, { controller: 'accounts', action: 'new' })
81
+ assert_routing({ path: '/accounts', method: :post }, { controller: 'accounts', action: 'create' })
82
+ assert_routing({ path: '/accounts', method: :put }, { controller: 'accounts', action: 'update' })
83
+ assert_routing({ path: '/accounts', method: :patch }, { controller: 'accounts', action: 'update' })
84
+ assert_routing({ path: '/accounts/schema', method: :get }, { controller: 'accounts', action: 'schema' })
85
+ assert_routing({ path: '/accounts/calculate', method: :get }, { controller: 'accounts', action: 'calculate' })
86
+ assert_equal 10, set.routes.size
87
+ end
88
+ end
33
89
  end