trailblazer-endpoint 0.0.1 → 0.0.6
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 +26 -0
- data/README.md +40 -5
- data/Rakefile +7 -1
- data/gemfiles/rails_app.gemfile +12 -0
- data/lib/trailblazer/endpoint.rb +29 -14
- data/lib/trailblazer/endpoint/adapter.rb +30 -121
- data/lib/trailblazer/endpoint/builder.rb +1 -1
- data/lib/trailblazer/endpoint/controller.rb +223 -0
- data/lib/trailblazer/endpoint/dsl.rb +29 -0
- data/lib/trailblazer/endpoint/options.rb +92 -0
- 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 +128 -0
- data/test/docs/controller_test.rb +339 -58
- 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 +15 -9
- data/test/rails-app/Gemfile.lock +162 -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/cell/create.rb +4 -0
- data/test/rails-app/app/concepts/song/cell/new.rb +4 -0
- data/test/rails-app/app/concepts/song/operation/create.rb +17 -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/concepts/song/view/create.erb +1 -0
- data/test/rails-app/app/concepts/song/view/new.erb +1 -0
- data/test/rails-app/app/controllers/api/v1/songs_controller.rb +41 -0
- data/test/rails-app/app/controllers/application_controller.rb +8 -1
- data/test/rails-app/app/controllers/application_controller/api.rb +107 -0
- data/test/rails-app/app/controllers/application_controller/web.rb +44 -0
- data/test/rails-app/app/controllers/auth_controller.rb +44 -0
- data/test/rails-app/app/controllers/home_controller.rb +5 -0
- data/test/rails-app/app/controllers/songs_controller.rb +71 -13
- data/test/rails-app/app/models/song.rb +3 -0
- data/test/rails-app/app/models/user.rb +7 -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 +16 -4
- data/test/rails-app/db/schema.rb +15 -0
- data/test/rails-app/test/controllers/api_songs_controller_test.rb +87 -0
- data/test/rails-app/test/controllers/songs_controller_test.rb +80 -147
- data/test/rails-app/test/test_helper.rb +7 -1
- data/test/test_helper.rb +0 -2
- data/trailblazer-endpoint.gemspec +2 -1
- metadata +70 -22
- 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,33 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'fileutils'
|
3
|
+
|
4
|
+
# path to your application root.
|
5
|
+
APP_ROOT = File.expand_path('..', __dir__)
|
6
|
+
|
7
|
+
def system!(*args)
|
8
|
+
system(*args) || abort("\n== Command #{args} failed ==")
|
9
|
+
end
|
10
|
+
|
11
|
+
FileUtils.chdir APP_ROOT do
|
12
|
+
# This script is a way to setup or update your development environment automatically.
|
13
|
+
# This script is idempotent, so that you can run it at anytime and get an expectable outcome.
|
14
|
+
# Add necessary setup steps to this file.
|
15
|
+
|
16
|
+
puts '== Installing dependencies =='
|
17
|
+
system! 'gem install bundler --conservative'
|
18
|
+
system('bundle check') || system!('bundle install')
|
19
|
+
|
20
|
+
# puts "\n== Copying sample files =="
|
21
|
+
# unless File.exist?('config/database.yml')
|
22
|
+
# FileUtils.cp 'config/database.yml.sample', 'config/database.yml'
|
23
|
+
# end
|
24
|
+
|
25
|
+
puts "\n== Preparing database =="
|
26
|
+
system! 'bin/rails db:prepare'
|
27
|
+
|
28
|
+
puts "\n== Removing old logs and tempfiles =="
|
29
|
+
system! 'bin/rails log:clear tmp:clear'
|
30
|
+
|
31
|
+
puts "\n== Restarting application server =="
|
32
|
+
system! 'bin/rails restart'
|
33
|
+
end
|
@@ -1,6 +1,20 @@
|
|
1
|
+
require 'uri'
|
1
2
|
require_relative 'boot'
|
2
3
|
|
3
|
-
require
|
4
|
+
require "rails"
|
5
|
+
# Pick the frameworks you want:
|
6
|
+
require "active_model/railtie"
|
7
|
+
# require "active_job/railtie"
|
8
|
+
require "active_record/railtie"
|
9
|
+
# require "active_storage/engine"
|
10
|
+
require "action_controller/railtie"
|
11
|
+
# require "action_mailer/railtie"
|
12
|
+
# require "action_mailbox/engine"
|
13
|
+
# require "action_text/engine"
|
14
|
+
# require "action_view/railtie"
|
15
|
+
# require "action_cable/engine"
|
16
|
+
# require "sprockets/railtie"
|
17
|
+
require "rails/test_unit/railtie"
|
4
18
|
|
5
19
|
# Require the gems listed in Gemfile, including any gems
|
6
20
|
# you've limited to :test, :development, or :production.
|
@@ -8,8 +22,17 @@ Bundler.require(*Rails.groups)
|
|
8
22
|
|
9
23
|
module RailsApp
|
10
24
|
class Application < Rails::Application
|
25
|
+
# Initialize configuration defaults for originally generated Rails version.
|
26
|
+
config.load_defaults 6.0
|
27
|
+
|
11
28
|
# Settings in config/environments/* take precedence over those specified here.
|
12
|
-
# Application configuration
|
13
|
-
# -- all .rb files in that directory are automatically loaded
|
29
|
+
# Application configuration can go into files in config/initializers
|
30
|
+
# -- all .rb files in that directory are automatically loaded after loading
|
31
|
+
# the framework and any gems in your application.
|
32
|
+
|
33
|
+
# Only loads a smaller set of middleware suitable for API only apps.
|
34
|
+
# Middleware like session, flash, cookies can be added back manually.
|
35
|
+
# Skip views, helpers and assets when generating a new resource.
|
36
|
+
config.api_only = false
|
14
37
|
end
|
15
38
|
end
|
@@ -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,18 @@
|
|
1
1
|
Rails.application.routes.draw do
|
2
|
-
# For details on the DSL available within this file, see
|
3
|
-
|
4
|
-
post "
|
5
|
-
post "
|
2
|
+
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
|
3
|
+
post "/songs/create_with_options", to: "songs_controller/create_with_options#create"
|
4
|
+
post "/songs/create_or", to: "songs_controller/create_or#create"
|
5
|
+
post "/songs/endpoint_ctx", to: "songs_controller/create_endpoint_ctx#create"
|
6
|
+
post "/songs/create_with_or", to: "songs#create"
|
7
|
+
post "/songs", to: "songs#create_without_block"
|
8
|
+
post "/songs/create_with_protocol_failure", to: "songs_controller/create_with_protocol_failure#create_with_protocol_failure"
|
9
|
+
post "/songs/create_with_options_for_domain_ctx", to: "songs_controller/create_with_options_for_domain_ctx#create"
|
10
|
+
post "/auth/sign_in", to: "auth#sign_in"
|
11
|
+
|
12
|
+
post "/v1/songs", to: "api/v1/songs#create"
|
13
|
+
get "/v1/songs/:id", to: "api/v1/songs#show"
|
14
|
+
|
15
|
+
get "/v1/songs_with_options/:id", to: "api/v1/songs_controller/with_options#show"
|
16
|
+
|
17
|
+
get "/", to: "home#dashboard"
|
6
18
|
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,87 @@
|
|
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
|
+
# default {success}
|
36
|
+
#:success
|
37
|
+
post_json "/v1/songs", {id: 1}, yogi_jwt
|
38
|
+
|
39
|
+
assert_response 200
|
40
|
+
assert_equal "{\"id\":1}", response.body
|
41
|
+
#:success end
|
42
|
+
|
43
|
+
# no proper input/params
|
44
|
+
post_json "/v1/songs", {}, yogi_jwt
|
45
|
+
# default {failure}
|
46
|
+
assert_response 422
|
47
|
+
assert_equal "{\"errors\":{\"message\":\"The submitted data is invalid.\"}}", response.body
|
48
|
+
|
49
|
+
# 401
|
50
|
+
#:not_authenticated
|
51
|
+
post_json "/v1/songs", {} # no token
|
52
|
+
assert_response 401
|
53
|
+
assert_equal "{\"errors\":{\"message\":\"Authentication credentials were not provided or are invalid.\"}}", response.body
|
54
|
+
#:not_authenticated end
|
55
|
+
|
56
|
+
# 403
|
57
|
+
post_json "/v1/songs", {id: 1, policy: false}, yogi_jwt
|
58
|
+
assert_response 403
|
59
|
+
assert_equal "{\"errors\":{\"message\":\"Action not allowed due to a policy setting.\"}}", response.body
|
60
|
+
|
61
|
+
# 200 / GET
|
62
|
+
get_json "/v1/songs/1", {}, yogi_jwt
|
63
|
+
assert_response 200
|
64
|
+
assert_equal "{\"id\":\"1\"}", response.body
|
65
|
+
|
66
|
+
# 404
|
67
|
+
get_json "/v1/songs/0", {}, yogi_jwt
|
68
|
+
assert_response 404
|
69
|
+
assert_equal "{\"errors\":{}}", response.body
|
70
|
+
|
71
|
+
# TODO: CHANGE/customize block
|
72
|
+
end
|
73
|
+
|
74
|
+
test "allows overriding {:success_block} and friends" do
|
75
|
+
yogi_jwt = jwt(1)
|
76
|
+
|
77
|
+
# Not authenticated, 401, overridden {:protocol_failure_block} kicks in
|
78
|
+
get_json "/v1/songs_with_options/1"
|
79
|
+
assert_response 402
|
80
|
+
|
81
|
+
# All good, default block
|
82
|
+
get_json "/v1/songs_with_options/1", yogi_jwt
|
83
|
+
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
# TODO: test 404 with NotFound config
|