trailblazer-endpoint 0.0.4 → 0.0.5
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 +16 -0
- data/Appraisals +5 -0
- data/CHANGES.md +6 -0
- data/Rakefile +7 -1
- data/gemfiles/rails_app.gemfile +12 -0
- data/lib/trailblazer/endpoint.rb +20 -5
- data/lib/trailblazer/endpoint/adapter.rb +30 -121
- data/lib/trailblazer/endpoint/builder.rb +1 -1
- data/lib/trailblazer/endpoint/controller.rb +203 -1
- data/lib/trailblazer/endpoint/dsl.rb +6 -3
- data/lib/trailblazer/endpoint/options.rb +13 -44
- data/lib/trailblazer/endpoint/protocol.rb +5 -8
- data/lib/trailblazer/endpoint/version.rb +1 -1
- data/test/adapter/api_test.rb +6 -11
- data/test/adapter/web_test.rb +2 -5
- data/test/config_test.rb +25 -0
- data/test/docs/controller_test.rb +160 -73
- data/test/endpoint_test.rb +1 -1
- data/test/rails-app/.gitignore +8 -2
- data/test/rails-app/.ruby-version +1 -0
- data/test/rails-app/Gemfile +11 -9
- data/test/rails-app/Gemfile.lock +137 -121
- data/test/rails-app/app/concepts/app/api/v1/representer/errors.rb +16 -0
- data/test/rails-app/app/concepts/auth/jwt.rb +35 -0
- data/test/rails-app/app/concepts/auth/operation/authenticate.rb +32 -0
- data/test/rails-app/app/concepts/auth/operation/policy.rb +9 -0
- data/test/rails-app/app/concepts/song/operation/create.rb +13 -0
- data/test/rails-app/app/concepts/song/operation/show.rb +10 -0
- data/test/rails-app/app/concepts/song/representer.rb +5 -0
- data/test/rails-app/app/controllers/api/v1/songs_controller.rb +35 -0
- data/test/rails-app/app/controllers/application_controller.rb +6 -1
- data/test/rails-app/app/controllers/application_controller/api.rb +105 -0
- data/test/rails-app/app/controllers/application_controller/web.rb +30 -0
- data/test/rails-app/app/controllers/songs_controller.rb +15 -17
- data/test/rails-app/app/models/song.rb +3 -0
- data/test/rails-app/app/models/user.rb +5 -0
- data/test/rails-app/bin/bundle +114 -0
- data/test/rails-app/bin/rails +4 -0
- data/test/rails-app/bin/rake +4 -0
- data/test/rails-app/bin/setup +33 -0
- data/test/rails-app/config/application.rb +26 -3
- data/test/rails-app/config/credentials.yml.enc +1 -0
- data/test/rails-app/config/database.yml +2 -2
- data/test/rails-app/config/environments/development.rb +7 -17
- data/test/rails-app/config/environments/production.rb +28 -23
- data/test/rails-app/config/environments/test.rb +8 -12
- data/test/rails-app/config/initializers/application_controller_renderer.rb +6 -4
- data/test/rails-app/config/initializers/cors.rb +16 -0
- data/test/rails-app/config/initializers/trailblazer.rb +2 -0
- data/test/rails-app/config/locales/en.yml +11 -1
- data/test/rails-app/config/master.key +1 -0
- data/test/rails-app/config/routes.rb +8 -3
- data/test/rails-app/db/schema.rb +15 -0
- data/test/rails-app/test/controllers/api_songs_controller_test.rb +83 -0
- data/test/rails-app/test/controllers/songs_controller_test.rb +36 -144
- data/test/rails-app/test/test_helper.rb +7 -1
- data/test/test_helper.rb +0 -2
- data/trailblazer-endpoint.gemspec +1 -0
- metadata +52 -21
- data/test/rails-app/config/initializers/cookies_serializer.rb +0 -5
- data/test/rails-app/config/initializers/new_framework_defaults.rb +0 -24
- data/test/rails-app/config/initializers/session_store.rb +0 -3
- data/test/rails-app/config/secrets.yml +0 -22
- data/test/rails-app/test/helpers/.keep +0 -0
- data/test/rails-app/test/integration/.keep +0 -0
- data/test/rails-app/test/mailers/.keep +0 -0
- data/test/rails-app/vendor/assets/javascripts/.keep +0 -0
- data/test/rails-app/vendor/assets/stylesheets/.keep +0 -0
@@ -0,0 +1 @@
|
|
1
|
+
DrKH8tXT2zcMyRNuO17XGyj5qNwIUtoMDC6d+VfPQdJRIgRbfCd6tLY4N9AOkXXQP2e9DeJFgBPnfirNu4ixAoJmEwlnjhoobiMizxpbYgI9YLVhtCMyRNhQG9RsVfb4yoo6S4b9FEPlUmf0JKC4L/THuE15ro2tLBkx7dg+IYEz/oBe//xHqcD4wE1WYOJVzT53wpCWan6Ju+bdDmS9M7TmCPm6DQ0Kt9y+I350X5rUIPPTGO0akf3g+AkYRdEu/1Z6C8NmrmvBzcQ3UTRDUy+LubJ+JT342J/eQo1A480fhqDUTzWNUpYWD6zA0PRCKyaTaP4w/WvvPF6F6fmuHLDkMa1+TdX+rkLPIBbWYkba6DpVtwjLz+t5LUvk4BDNTfk59SNRQxC4jJOEWbxtU4rDopDCi/qGjP6i--fFGcdGkm4cR2s7ZR--3aMr7ojTm6N0V7CF/HjuHA==
|
@@ -1,4 +1,4 @@
|
|
1
|
-
# SQLite
|
1
|
+
# SQLite. Versions 3.8.0 and up are supported.
|
2
2
|
# gem install sqlite3
|
3
3
|
#
|
4
4
|
# Ensure the SQLite 3 gem is defined in your Gemfile
|
@@ -6,7 +6,7 @@
|
|
6
6
|
#
|
7
7
|
default: &default
|
8
8
|
adapter: sqlite3
|
9
|
-
pool: 5
|
9
|
+
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
|
10
10
|
timeout: 5000
|
11
11
|
|
12
12
|
development:
|
@@ -13,12 +13,11 @@ Rails.application.configure do
|
|
13
13
|
config.consider_all_requests_local = true
|
14
14
|
|
15
15
|
# Enable/disable caching. By default caching is disabled.
|
16
|
-
|
17
|
-
|
18
|
-
|
16
|
+
# Run rails dev:cache to toggle caching.
|
17
|
+
if Rails.root.join('tmp', 'caching-dev.txt').exist?
|
19
18
|
config.cache_store = :memory_store
|
20
19
|
config.public_file_server.headers = {
|
21
|
-
'Cache-Control' =>
|
20
|
+
'Cache-Control' => "public, max-age=#{2.days.to_i}"
|
22
21
|
}
|
23
22
|
else
|
24
23
|
config.action_controller.perform_caching = false
|
@@ -26,29 +25,20 @@ Rails.application.configure do
|
|
26
25
|
config.cache_store = :null_store
|
27
26
|
end
|
28
27
|
|
29
|
-
# Don't care if the mailer can't send.
|
30
|
-
config.action_mailer.raise_delivery_errors = false
|
31
|
-
|
32
|
-
config.action_mailer.perform_caching = false
|
33
|
-
|
34
28
|
# Print deprecation notices to the Rails logger.
|
35
29
|
config.active_support.deprecation = :log
|
36
30
|
|
37
31
|
# Raise an error on page load if there are pending migrations.
|
38
32
|
config.active_record.migration_error = :page_load
|
39
33
|
|
40
|
-
#
|
41
|
-
|
42
|
-
# number of complex assets.
|
43
|
-
config.assets.debug = true
|
34
|
+
# Highlight code that triggered database queries in logs.
|
35
|
+
config.active_record.verbose_query_logs = true
|
44
36
|
|
45
|
-
# Suppress logger output for asset requests.
|
46
|
-
config.assets.quiet = true
|
47
37
|
|
48
|
-
# Raises error for missing translations
|
38
|
+
# Raises error for missing translations.
|
49
39
|
# config.action_view.raise_on_missing_translations = true
|
50
40
|
|
51
41
|
# Use an evented file watcher to asynchronously detect changes in source code,
|
52
42
|
# routes, locales, etc. This feature depends on the listen gem.
|
53
|
-
config.file_watcher = ActiveSupport::EventedFileUpdateChecker
|
43
|
+
# config.file_watcher = ActiveSupport::EventedFileUpdateChecker
|
54
44
|
end
|
@@ -12,21 +12,15 @@ Rails.application.configure do
|
|
12
12
|
|
13
13
|
# Full error reports are disabled and caching is turned on.
|
14
14
|
config.consider_all_requests_local = false
|
15
|
-
|
15
|
+
|
16
|
+
# Ensures that a master key has been made available in either ENV["RAILS_MASTER_KEY"]
|
17
|
+
# or in config/master.key. This key is used to decrypt credentials (and other encrypted files).
|
18
|
+
# config.require_master_key = true
|
16
19
|
|
17
20
|
# Disable serving static files from the `/public` folder by default since
|
18
21
|
# Apache or NGINX already handles this.
|
19
22
|
config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
|
20
23
|
|
21
|
-
# Compress JavaScripts and CSS.
|
22
|
-
config.assets.js_compressor = :uglifier
|
23
|
-
# config.assets.css_compressor = :sass
|
24
|
-
|
25
|
-
# Do not fallback to assets pipeline if a precompiled asset is missed.
|
26
|
-
config.assets.compile = false
|
27
|
-
|
28
|
-
# `config.assets.precompile` and `config.assets.version` have moved to config/initializers/assets.rb
|
29
|
-
|
30
24
|
# Enable serving of images, stylesheets, and JavaScripts from an asset server.
|
31
25
|
# config.action_controller.asset_host = 'http://assets.example.com'
|
32
26
|
|
@@ -34,11 +28,6 @@ Rails.application.configure do
|
|
34
28
|
# config.action_dispatch.x_sendfile_header = 'X-Sendfile' # for Apache
|
35
29
|
# config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for NGINX
|
36
30
|
|
37
|
-
# Mount Action Cable outside main process or domain
|
38
|
-
# config.action_cable.mount_path = nil
|
39
|
-
# config.action_cable.url = 'wss://example.com/cable'
|
40
|
-
# config.action_cable.allowed_request_origins = [ 'http://example.com', /http:\/\/example.*/ ]
|
41
|
-
|
42
31
|
# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
|
43
32
|
# config.force_ssl = true
|
44
33
|
|
@@ -52,14 +41,9 @@ Rails.application.configure do
|
|
52
41
|
# Use a different cache store in production.
|
53
42
|
# config.cache_store = :mem_cache_store
|
54
43
|
|
55
|
-
# Use a real queuing backend for Active Job (and separate queues per environment)
|
44
|
+
# Use a real queuing backend for Active Job (and separate queues per environment).
|
56
45
|
# config.active_job.queue_adapter = :resque
|
57
|
-
# config.active_job.queue_name_prefix = "
|
58
|
-
config.action_mailer.perform_caching = false
|
59
|
-
|
60
|
-
# Ignore bad email addresses and do not raise email delivery errors.
|
61
|
-
# Set this to true and configure the email server for immediate delivery to raise delivery errors.
|
62
|
-
# config.action_mailer.raise_delivery_errors = false
|
46
|
+
# config.active_job.queue_name_prefix = "rails_app_production"
|
63
47
|
|
64
48
|
# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
|
65
49
|
# the I18n.default_locale when a translation cannot be found).
|
@@ -78,9 +62,30 @@ Rails.application.configure do
|
|
78
62
|
if ENV["RAILS_LOG_TO_STDOUT"].present?
|
79
63
|
logger = ActiveSupport::Logger.new(STDOUT)
|
80
64
|
logger.formatter = config.log_formatter
|
81
|
-
config.logger
|
65
|
+
config.logger = ActiveSupport::TaggedLogging.new(logger)
|
82
66
|
end
|
83
67
|
|
84
68
|
# Do not dump schema after migrations.
|
85
69
|
config.active_record.dump_schema_after_migration = false
|
70
|
+
|
71
|
+
# Inserts middleware to perform automatic connection switching.
|
72
|
+
# The `database_selector` hash is used to pass options to the DatabaseSelector
|
73
|
+
# middleware. The `delay` is used to determine how long to wait after a write
|
74
|
+
# to send a subsequent read to the primary.
|
75
|
+
#
|
76
|
+
# The `database_resolver` class is used by the middleware to determine which
|
77
|
+
# database is appropriate to use based on the time delay.
|
78
|
+
#
|
79
|
+
# The `database_resolver_context` class is used by the middleware to set
|
80
|
+
# timestamps for the last write to the primary. The resolver uses the context
|
81
|
+
# class timestamps to determine how long to wait before reading from the
|
82
|
+
# replica.
|
83
|
+
#
|
84
|
+
# By default Rails will store a last write timestamp in the session. The
|
85
|
+
# DatabaseSelector middleware is designed as such you can define your own
|
86
|
+
# strategy for connection switching and pass that into the middleware through
|
87
|
+
# these configuration options.
|
88
|
+
# config.active_record.database_selector = { delay: 2.seconds }
|
89
|
+
# config.active_record.database_resolver = ActiveRecord::Middleware::DatabaseSelector::Resolver
|
90
|
+
# config.active_record.database_resolver_context = ActiveRecord::Middleware::DatabaseSelector::Resolver::Session
|
86
91
|
end
|
@@ -1,10 +1,11 @@
|
|
1
|
+
# The test environment is used exclusively to run your application's
|
2
|
+
# test suite. You never need to work with it otherwise. Remember that
|
3
|
+
# your test database is "scratch space" for the test suite and is wiped
|
4
|
+
# and recreated between test runs. Don't rely on the data there!
|
5
|
+
|
1
6
|
Rails.application.configure do
|
2
7
|
# Settings specified here will take precedence over those in config/application.rb.
|
3
8
|
|
4
|
-
# The test environment is used exclusively to run your application's
|
5
|
-
# test suite. You never need to work with it otherwise. Remember that
|
6
|
-
# your test database is "scratch space" for the test suite and is wiped
|
7
|
-
# and recreated between test runs. Don't rely on the data there!
|
8
9
|
config.cache_classes = true
|
9
10
|
|
10
11
|
# Do not eager load code on boot. This avoids loading your whole application
|
@@ -15,28 +16,23 @@ Rails.application.configure do
|
|
15
16
|
# Configure public file server for tests with Cache-Control for performance.
|
16
17
|
config.public_file_server.enabled = true
|
17
18
|
config.public_file_server.headers = {
|
18
|
-
'Cache-Control' =>
|
19
|
+
'Cache-Control' => "public, max-age=#{1.hour.to_i}"
|
19
20
|
}
|
20
21
|
|
21
22
|
# Show full error reports and disable caching.
|
22
23
|
config.consider_all_requests_local = true
|
23
24
|
config.action_controller.perform_caching = false
|
25
|
+
config.cache_store = :null_store
|
24
26
|
|
25
27
|
# Raise exceptions instead of rendering exception templates.
|
26
28
|
config.action_dispatch.show_exceptions = false
|
27
29
|
|
28
30
|
# Disable request forgery protection in test environment.
|
29
31
|
config.action_controller.allow_forgery_protection = false
|
30
|
-
config.action_mailer.perform_caching = false
|
31
|
-
|
32
|
-
# Tell Action Mailer not to deliver emails to the real world.
|
33
|
-
# The :test delivery method accumulates sent emails in the
|
34
|
-
# ActionMailer::Base.deliveries array.
|
35
|
-
config.action_mailer.delivery_method = :test
|
36
32
|
|
37
33
|
# Print deprecation notices to the stderr.
|
38
34
|
config.active_support.deprecation = :stderr
|
39
35
|
|
40
|
-
# Raises error for missing translations
|
36
|
+
# Raises error for missing translations.
|
41
37
|
# config.action_view.raise_on_missing_translations = true
|
42
38
|
end
|
@@ -1,6 +1,8 @@
|
|
1
1
|
# Be sure to restart your server when you modify this file.
|
2
2
|
|
3
|
-
#
|
4
|
-
#
|
5
|
-
#
|
6
|
-
#
|
3
|
+
# ActiveSupport::Reloader.to_prepare do
|
4
|
+
# ApplicationController.renderer.defaults.merge!(
|
5
|
+
# http_host: 'example.org',
|
6
|
+
# https: false
|
7
|
+
# )
|
8
|
+
# end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# Be sure to restart your server when you modify this file.
|
2
|
+
|
3
|
+
# Avoid CORS issues when API is called from the frontend app.
|
4
|
+
# Handle Cross-Origin Resource Sharing (CORS) in order to accept cross-origin AJAX requests.
|
5
|
+
|
6
|
+
# Read more: https://github.com/cyu/rack-cors
|
7
|
+
|
8
|
+
# Rails.application.config.middleware.insert_before 0, Rack::Cors do
|
9
|
+
# allow do
|
10
|
+
# origins 'example.com'
|
11
|
+
#
|
12
|
+
# resource '*',
|
13
|
+
# headers: :any,
|
14
|
+
# methods: [:get, :post, :put, :patch, :delete, :options, :head]
|
15
|
+
# end
|
16
|
+
# end
|
@@ -16,8 +16,18 @@
|
|
16
16
|
#
|
17
17
|
# This would use the information in config/locales/es.yml.
|
18
18
|
#
|
19
|
+
# The following keys must be escaped otherwise they will not be retrieved by
|
20
|
+
# the default I18n backend:
|
21
|
+
#
|
22
|
+
# true, false, on, off, yes, no
|
23
|
+
#
|
24
|
+
# Instead, surround them with single quotes.
|
25
|
+
#
|
26
|
+
# en:
|
27
|
+
# 'true': 'foo'
|
28
|
+
#
|
19
29
|
# To learn more, please read the Rails Internationalization guide
|
20
|
-
# available at
|
30
|
+
# available at https://guides.rubyonrails.org/i18n.html.
|
21
31
|
|
22
32
|
en:
|
23
33
|
hello: "Hello world"
|
@@ -0,0 +1 @@
|
|
1
|
+
fee3e51bb20a824ec13dc5912f3548f2
|
@@ -1,6 +1,11 @@
|
|
1
1
|
Rails.application.routes.draw do
|
2
|
-
# For details on the DSL available within this file, see
|
2
|
+
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
|
3
3
|
resources :songs
|
4
|
-
post "
|
5
|
-
post "
|
4
|
+
post "/songs/create_with_options", to: "songs#create_with_options"
|
5
|
+
post "/songs/create_with_or", to: "songs#create_with_or"
|
6
|
+
|
7
|
+
post "/v1/songs", to: "api/v1/songs#create"
|
8
|
+
get "/v1/songs/:id", to: "api/v1/songs#show"
|
9
|
+
|
10
|
+
get "/v1/songs_with_options/:id", to: "api/v1/songs_controller/with_options#show"
|
6
11
|
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# This file is auto-generated from the current state of the database. Instead
|
2
|
+
# of editing this file, please use the migrations feature of Active Record to
|
3
|
+
# incrementally modify your database, and then regenerate this schema definition.
|
4
|
+
#
|
5
|
+
# This file is the source Rails uses to define your schema when running `rails
|
6
|
+
# db:schema:load`. When creating a new database, `rails db:schema:load` tends to
|
7
|
+
# be faster and is potentially less error prone than running all of your
|
8
|
+
# migrations from scratch. Old migrations may fail to apply correctly if those
|
9
|
+
# migrations use external dependencies or application code.
|
10
|
+
#
|
11
|
+
# It's strongly recommended that you check this file into your version control system.
|
12
|
+
|
13
|
+
ActiveRecord::Schema.define(version: 0) do
|
14
|
+
|
15
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class ApiSongsControllerTest < ActionDispatch::IntegrationTest
|
4
|
+
|
5
|
+
def jwt(user_id)
|
6
|
+
Auth::Jwt.generate('user_id', user_id, {}
|
7
|
+
# 'email': options['current_user'].email
|
8
|
+
)
|
9
|
+
end
|
10
|
+
|
11
|
+
def post_json(endpoint, params_hash, api_token = nil, headers={})
|
12
|
+
post endpoint, params: params_hash.to_json, headers: request_headers(api_token).merge(headers)
|
13
|
+
end
|
14
|
+
def get_json(endpoint, params = nil, api_token = nil)
|
15
|
+
get endpoint, params: params, headers: request_headers(api_token)
|
16
|
+
end
|
17
|
+
|
18
|
+
def request_headers(api_token = nil)
|
19
|
+
headers = {
|
20
|
+
'Content-Type' => 'application/json'
|
21
|
+
}
|
22
|
+
unless api_token.nil?
|
23
|
+
headers.merge!(authorization_header(api_token))
|
24
|
+
end
|
25
|
+
headers
|
26
|
+
end
|
27
|
+
def authorization_header(api_token)
|
28
|
+
{ 'Authorization' => "Bearer #{api_token}"}
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
test "API interface" do
|
33
|
+
yogi_jwt = jwt(1)
|
34
|
+
|
35
|
+
post_json "/v1/songs", {id: 1}, yogi_jwt
|
36
|
+
|
37
|
+
# default {success}
|
38
|
+
assert_response 200
|
39
|
+
assert_equal "{\"id\":1}", response.body
|
40
|
+
|
41
|
+
# no proper input/params
|
42
|
+
post_json "/v1/songs", {}, yogi_jwt
|
43
|
+
# default {failure}
|
44
|
+
assert_response 422
|
45
|
+
assert_equal "{\"errors\":{\"message\":\"The submitted data is invalid.\"}}", response.body
|
46
|
+
|
47
|
+
# 401
|
48
|
+
post_json "/v1/songs", {authenticate: false}
|
49
|
+
assert_response 401
|
50
|
+
assert_equal "{\"errors\":{\"message\":\"Authentication credentials were not provided or are invalid.\"}}", response.body
|
51
|
+
|
52
|
+
# 403
|
53
|
+
post_json "/v1/songs", {id: 1, policy: false}, yogi_jwt
|
54
|
+
assert_response 403
|
55
|
+
assert_equal "{\"errors\":{\"message\":\"Action not allowed due to a policy setting.\"}}", response.body
|
56
|
+
|
57
|
+
# 200 / GET
|
58
|
+
get_json "/v1/songs/1", {}, yogi_jwt
|
59
|
+
assert_response 200
|
60
|
+
assert_equal "{\"id\":\"1\"}", response.body
|
61
|
+
|
62
|
+
# 404
|
63
|
+
get_json "/v1/songs/0", {}, yogi_jwt
|
64
|
+
assert_response 404
|
65
|
+
assert_equal "{\"errors\":{}}", response.body
|
66
|
+
|
67
|
+
# TODO: CHANGE/customize block
|
68
|
+
end
|
69
|
+
|
70
|
+
test "allows overriding {:success_block} and friends" do
|
71
|
+
yogi_jwt = jwt(1)
|
72
|
+
|
73
|
+
# Not authenticated, 401, overridden {:protocol_failure_block} kicks in
|
74
|
+
get_json "/v1/songs_with_options/1"
|
75
|
+
assert_response 402
|
76
|
+
|
77
|
+
# All good, default block
|
78
|
+
get_json "/v1/songs_with_options/1", yogi_jwt
|
79
|
+
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
# TODO: test 404 with NotFound config
|
@@ -1,156 +1,48 @@
|
|
1
1
|
require "test_helper"
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
3
|
+
class SongsControllerTest < ActionDispatch::IntegrationTest
|
4
|
+
test "create 200" do
|
5
|
+
post "/songs", params: {id: 1}
|
6
|
+
# default {success} block doesn't do anything
|
7
|
+
assert_response 200
|
8
|
+
assert_equal "", response.body
|
9
|
+
|
10
|
+
post "/songs", params: {}
|
11
|
+
# default {failure} block doesn't do anything
|
12
|
+
assert_response 422
|
13
|
+
assert_equal "", response.body
|
6
14
|
|
15
|
+
# 401
|
16
|
+
post "/songs", params: {authenticate: false}
|
17
|
+
assert_response 401
|
18
|
+
assert_equal "", response.body
|
7
19
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
require "trailblazer"
|
13
|
-
require "trailblazer/operation/model"
|
14
|
-
# require "reform/form/active_model/validations"
|
15
|
-
require "trailblazer/operation/contract"
|
16
|
-
require "trailblazer/operation/representer"
|
17
|
-
require "trailblazer/operation/guard"
|
18
|
-
require "trailblazer/endpoint"
|
19
|
-
|
20
|
-
|
21
|
-
require "representable/json"
|
22
|
-
class Serializer < Representable::Decorator
|
23
|
-
include Representable::JSON
|
24
|
-
property :id
|
25
|
-
property :title
|
26
|
-
property :length
|
27
|
-
|
28
|
-
class Errors < Representable::Decorator
|
29
|
-
include Representable::JSON
|
30
|
-
property :messages
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
class Deserializer < Representable::Decorator
|
35
|
-
include Representable::JSON
|
36
|
-
property :title
|
37
|
-
end
|
38
|
-
|
39
|
-
|
40
|
-
class Create < Trailblazer::Operation
|
41
|
-
include Policy::Guard
|
42
|
-
policy ->(*) { self["user.current"] == ::Module }
|
43
|
-
|
44
|
-
extend Representer::DSL
|
45
|
-
representer :serializer, Serializer
|
46
|
-
representer :deserializer, Deserializer
|
47
|
-
representer :errors, Serializer::Errors
|
48
|
-
# self["representer.serializer.class"] = Representer
|
49
|
-
# self["representer.deserializer.class"] = Deserializer
|
20
|
+
# 403
|
21
|
+
post "/songs", params: {policy: false}
|
22
|
+
assert_response 403
|
23
|
+
assert_equal "", response.body
|
50
24
|
|
51
|
-
|
52
|
-
|
25
|
+
post "/songs/create_with_options", params: {id: 1}
|
26
|
+
# {success} block renders model
|
27
|
+
assert_response 200
|
28
|
+
assert_equal "[\"1\",\"yay!\"]", response.body
|
53
29
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
# property :length
|
30
|
+
post "/songs/create_with_options", params: {}
|
31
|
+
# default {failure} block doesn't do anything
|
32
|
+
assert_response 422
|
33
|
+
assert_equal "", response.body
|
59
34
|
|
60
|
-
|
61
|
-
#
|
62
|
-
|
35
|
+
post "/songs/create_with_or", params: {id: 1}
|
36
|
+
# {success} block renders model
|
37
|
+
assert_response 200
|
38
|
+
assert_equal "{\"or\":\"1\"}", response.body
|
63
39
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
if params[:title]
|
69
|
-
@title, @length = params[:title],params[:length]
|
70
|
-
return Trailblazer::Operation::Result.new(true, {})
|
71
|
-
end
|
40
|
+
post "/songs/create_with_or", params: {}
|
41
|
+
# default {failure} block doesn't do anything
|
42
|
+
assert_response 422
|
43
|
+
assert_equal "null", response.body
|
72
44
|
|
73
|
-
return Struct.new(:errors, :success?).new(Struct.new(:messages).new({"title": ["must be set"]}), false)
|
74
|
-
end
|
75
|
-
def sync; @model.title = @title; @model.length = @length; end
|
76
45
|
end
|
77
|
-
|
78
|
-
contract FakeContract
|
79
|
-
|
80
|
-
def process(params)
|
81
|
-
validate(params) do |f|
|
82
|
-
self["contract"].sync
|
83
|
-
self["model"].id = 9
|
84
|
-
end
|
85
|
-
end
|
86
|
-
end
|
87
|
-
|
88
|
-
class Update < Create
|
89
|
-
action :find_by
|
90
|
-
end
|
91
|
-
|
92
|
-
# TODO: test present.
|
93
|
-
class Show < Trailblazer::Operation
|
94
|
-
include Policy::Guard
|
95
|
-
policy ->(*) { self["user.current"] == ::Module }
|
96
|
-
|
97
|
-
extend Representer::DSL
|
98
|
-
representer :serializer, Serializer
|
99
|
-
|
100
|
-
include Model
|
101
|
-
model Song, :find_by
|
102
|
-
|
103
|
-
self.> ->(input, options) { options["present"] = true }, before: "operation.result"
|
104
46
|
end
|
105
47
|
|
106
|
-
|
107
|
-
# 404
|
108
|
-
test "update 404" do
|
109
|
-
post :update, params: { id: 0 }
|
110
|
-
assert_equal 404, response.status
|
111
|
-
end
|
112
|
-
|
113
|
-
# 401
|
114
|
-
test "update 401" do
|
115
|
-
post :update, params: { id: 1 }
|
116
|
-
assert_equal 401, response.status
|
117
|
-
end
|
118
|
-
|
119
|
-
# 422
|
120
|
-
# TODO: test when no error repr set.
|
121
|
-
test "update 422" do
|
122
|
-
post :create, params: { id: 1 }
|
123
|
-
assert_equal 422, response.status
|
124
|
-
assert_equal %{{\"messages\":{\"title\":[\"must be set\"]}}}, response.body
|
125
|
-
end
|
126
|
-
|
127
|
-
# 201
|
128
|
-
test "create 201" do
|
129
|
-
post :create, params: { id: 1, title: "AVH" }
|
130
|
-
assert_equal 201, response.status
|
131
|
-
assert_equal %{}, response.body
|
132
|
-
assert_equal "/songs/9", response.header["Location"]
|
133
|
-
end
|
134
|
-
|
135
|
-
# 200 present
|
136
|
-
test "show 200" do
|
137
|
-
get :show, params: { id: 1 }
|
138
|
-
assert_equal 200, response.status
|
139
|
-
assert_equal %{{"id":"1","title":"A Song"}}, response.body
|
140
|
-
end
|
141
|
-
|
142
|
-
# 201 update
|
143
|
-
test "update 200" do
|
144
|
-
post :update_with_user, params: { id: 1, title: "AVH" }
|
145
|
-
assert_equal 200, response.status
|
146
|
-
assert_equal %{}, response.body
|
147
|
-
assert_equal "/songs/9", response.header["Location"]
|
148
|
-
end
|
149
|
-
|
150
|
-
# custom 999
|
151
|
-
test "custom 999" do
|
152
|
-
post :create_with_custom_handlers, params: { id: 1, title: "AVH" }
|
153
|
-
assert_equal 999, response.status
|
154
|
-
assert_equal %{{"id":9,"title":"AVH"}}, response.body
|
155
|
-
end
|
156
|
-
end
|
48
|
+
# TODO: test 404 with NotFound config
|