factory_bot_instrumentation 0.2.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 32b62f67039dbe0cd562eaf5ea41336866bce49cac7153b0abb86858a6ecf5e0
4
- data.tar.gz: 3efc8629cced06d8d68da53aaaaa6ba1280d5b25be43b71b9d930fbd9f1424e5
3
+ metadata.gz: ff6458a1179520c115511b80ad9bdcb335fb652a04c647a12e7515a678440942
4
+ data.tar.gz: dce3493555ed93a948fea4b4d79b0dbb8c9d1984439706d4b0d1b400a75748fb
5
5
  SHA512:
6
- metadata.gz: 8b8deb694372a595010a82a314234e4f0b5a2c5342c6ebb5568a497fe677530d54d2d4ea6443ef5f2154cc1fc601d1367e755035cb06c4fb26c39d06947b4ae1
7
- data.tar.gz: 47b5760d942fcc51e5fc5d6df7c3fc9dfe8ed1a0b0ce593224aba027e925c7124239e0dbf28a222d143102dd6a16e593fa84c51446cd8c75b5b1cf4e50dc5d35
6
+ metadata.gz: 24a2ffa2f0c9092222f13826ec2e5232db505fcaddb8df33ae0eed39494ac8a36248f3c3cd39582fc88b6447401dbdd7850a08b6ed045091321f543d67eacc89
7
+ data.tar.gz: ab32dc405102f93ea3b6dfc409c292600b8bfdac8839bc4cabe949fe8168729d8f8a6b435798c3703bfcd1eba9853870344e2875f9ebef77d1182bea81c71276
@@ -8,7 +8,7 @@ Documentation:
8
8
 
9
9
  AllCops:
10
10
  DisplayCopNames: true
11
- TargetRubyVersion: 2.3
11
+ TargetRubyVersion: 2.4
12
12
  Exclude:
13
13
  - db/schema.rb
14
14
  - bin/**/*
@@ -9,7 +9,6 @@ rvm:
9
9
  - 2.6
10
10
  - 2.5
11
11
  - 2.4
12
- - 2.3
13
12
 
14
13
  gemfile:
15
14
  - gemfiles/rails_4.gemfile
data/Appraisals CHANGED
@@ -1,5 +1,6 @@
1
1
  appraise 'rails-4' do
2
2
  gem 'rails', '~> 4.2.11'
3
+ gem 'rails-api', '~> 0.4.1'
3
4
  end
4
5
 
5
6
  appraise 'rails-5.0' do
@@ -1,3 +1,26 @@
1
+ ### 0.6.0
2
+
3
+ * Added support for custom error handling and improved the default error
4
+ handling on FactoryBot usage (#9)
5
+
6
+ ### 0.5.1
7
+
8
+ * Corrected a bug on the scenario description update (#8)
9
+
10
+ ### 0.5.0
11
+
12
+ * Added support for custom before action logics (#7)
13
+
14
+ ### 0.4.0
15
+
16
+ * Added support for configurable rendering (#6)
17
+
18
+ ### 0.3.0
19
+
20
+ * Removed CI support for Ruby 2.3 (it never worked before)
21
+ * Fixed the overwrite params bug on Rails 4.2
22
+ * Added a test suite for the engine
23
+
1
24
  ### 0.2.0
2
25
 
3
26
  * Removed the Gemfile locks and added the to the ignore list
data/Makefile CHANGED
@@ -93,4 +93,4 @@ shell-irb: install
93
93
 
94
94
  release:
95
95
  # Release a new gem version
96
- @$(RAKE) release
96
+ @$(BUNDLE) exec $(RAKE) release
data/README.md CHANGED
@@ -48,6 +48,14 @@ Add this line to your application's Gemfile:
48
48
  gem 'factory_bot_instrumentation'
49
49
  ```
50
50
 
51
+ **Heads up!** In case you use Rails 4.2, you need to add the
52
+ [rails-api](https://github.com/rails-api/rails-api) gem as well, because this
53
+ feature was first introduced with Rails 5.0.
54
+
55
+ ```ruby
56
+ gem 'rails-api'
57
+ ```
58
+
51
59
  And then execute:
52
60
 
53
61
  ```bash
@@ -107,20 +115,20 @@ FactoryBot.define do
107
115
 
108
116
  trait :with_friend do
109
117
  after(:create) do |user, elevator|
110
- FactoryBot.create(:lead,
111
- *elevator.lead_traits.map(&:to_sym),
112
- friends: [user],
113
- **elevator.friend_overwrites)
118
+ FactoryBot.create(:user,
119
+ *elevator.friend_traits.map(&:to_sym),
120
+ friends: [user],
121
+ **elevator.friend_overwrites)
114
122
  end
115
123
  end
116
124
 
117
125
  trait :with_friends do
118
126
  after(:create) do |user, elevator|
119
127
  FactoryBot.create_list(:user,
120
- elevator.leads_amount,
121
- *elevator.lead_traits.map(&:to_sym),
122
- friends: [user],
123
- **elevator.friend_overwrites)
128
+ elevator.friends_amount,
129
+ *elevator.friend_traits.map(&:to_sym),
130
+ friends: [user],
131
+ **elevator.friend_overwrites)
124
132
  end
125
133
  end
126
134
  end
@@ -263,8 +271,8 @@ ease the integration. But as you can imagine the Instrumentation engine opens
263
271
  up some risky possibilities to your application. This is fine for a canary or
264
272
  development environment, but not for a production environment.
265
273
 
266
- There is currently only one way to secure the Instrumentation engine. You can
267
- completly disable it on your production environment by reconfiguring your
274
+ There are currently multiple ways to secure the Instrumentation engine. You can
275
+ completely disable it on your production environment by reconfiguring your
268
276
  routes like this:
269
277
 
270
278
  ```ruby
@@ -275,8 +283,17 @@ Rails.application.routes.draw do
275
283
  end
276
284
  ```
277
285
 
278
- Another option would be an HTTP basic authentication on this routes, but this
279
- is not yet implemented.
286
+ Another option would be an HTTP basic authentication. Just configure the gem
287
+ like this on the initializer:
288
+
289
+ ```ruby
290
+ FactoryBot::Instrumentation.configure do |conf|
291
+ conf.before_action = proc do |controller|
292
+ basic_auth(username: ENV.fetch('INSTRUMENTATION_USERNAME'),
293
+ password: ENV.fetch('INSTRUMENTATION_PASSWORD'))
294
+ end
295
+ end
296
+ ```
280
297
 
281
298
  #### Global settings
282
299
 
@@ -293,6 +310,30 @@ FactoryBot::Instrumentation.configure do |conf|
293
310
  # The instrumentation configuration file path we should use,
294
311
  # defaults to config/instrumentation.yml
295
312
  conf.config_file = 'config/scenarios.yml'
313
+ # By default we use the Rails default JSON rendering mechanism, but
314
+ # you can configure your own logic here (eg. a custom representer)
315
+ conf.render_entity = proc do |controller, entity|
316
+ controller.render plain: entity.to_json,
317
+ content_type: 'application/json'
318
+ end
319
+ # By default we assemble a JSON response on errors which may be
320
+ # helpful for debugging, but you can configure your own logic here
321
+ conf.render_error = proc do |controller, error|
322
+ app_name = FactoryBot::Instrumentation.configuration.application_name
323
+ controller.render status: :internal_server_error,
324
+ content_type: 'application/json',
325
+ plain: {
326
+ application: app_name,
327
+ error: error.message,
328
+ backtrace: error.backtrace.join("\n")
329
+ }.to_json
330
+ end
331
+ # By default we do not perform any custom +before_action+ filters on the
332
+ # instrumentation controllers, with this option you can implement your
333
+ # custom logic like authentication
334
+ conf.before_action = proc do |controller|
335
+ # do your custom logic here
336
+ end
296
337
  end
297
338
  ```
298
339
 
data/Rakefile CHANGED
@@ -1,8 +1,26 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # Configure Rails Environment
4
+ ENV['RAILS_ENV'] = 'test'
5
+ ENV['DISABLE_DATABASE_ENVIRONMENT_CHECK'] = '1'
6
+
7
+ require 'bundler/setup'
3
8
  require 'bundler/gem_tasks'
4
9
  require 'rspec/core/rake_task'
5
10
 
6
- RSpec::Core::RakeTask.new(:spec)
11
+ APP_RAKEFILE = File.expand_path('../spec/dummy/Rakefile', __FILE__)
12
+ load 'rails/tasks/engine.rake'
13
+
14
+ Bundler::GemHelper.install_tasks
15
+
16
+ Dir[File.join(File.dirname(__FILE__), 'tasks/**/*.rake')].each {|f| load f }
17
+
18
+ require 'rspec/core'
19
+ require 'rspec/core/rake_task'
20
+
21
+ desc 'Run all specs in spec directory (excluding plugin specs)'
22
+ RSpec::Core::RakeTask.new(spec: [
23
+ 'db:drop', 'db:create', 'db:migrate', 'db:setup'
24
+ ])
7
25
 
8
26
  task default: :spec
@@ -101,7 +101,7 @@ CreateForm.prototype.bind = function()
101
101
  this.submit();
102
102
  });
103
103
 
104
- this.select.on('change', this.updateDesc);
104
+ this.select.on('change', this.updateDesc.bind(this));
105
105
  this.updateDesc();
106
106
  };
107
107
 
@@ -3,5 +3,33 @@
3
3
  module FactoryBot::Instrumentation
4
4
  # A base engine application controller.
5
5
  class ApplicationController < ActionController::API
6
+ # Extend our core functionality to support easy authentication logics
7
+ include ActionController::HttpAuthentication::Basic::ControllerMethods
8
+ include ActionController::HttpAuthentication::Digest::ControllerMethods
9
+ include ActionController::HttpAuthentication::Token::ControllerMethods
10
+
11
+ # Allow the users to implement additional instrumentation-wide before
12
+ # action logic, like authentication
13
+ before_action do |controller|
14
+ if FactoryBot::Instrumentation.configuration.before_action
15
+ instance_eval &FactoryBot::Instrumentation.configuration.before_action
16
+ end
17
+ end
18
+
19
+ protected
20
+
21
+ # A simple shortcut for Basic Auth on Rails 4.2+. Just configure the
22
+ # username and password and you're ready to check the current request.
23
+ #
24
+ # @param username [String] the required user name
25
+ # @param password [String] the required password of the user
26
+ # @param realm [String] the authentication realm
27
+ def basic_auth(username:, password:, realm: 'Instrumentation')
28
+ authenticate_or_request_with_http_basic(realm) \
29
+ do |given_name, given_password|
30
+ ActiveSupport::SecurityUtils.secure_compare(given_name, username) &
31
+ ActiveSupport::SecurityUtils.secure_compare(given_password, password)
32
+ end
33
+ end
6
34
  end
7
35
  end
@@ -40,12 +40,11 @@ module FactoryBot::Instrumentation
40
40
  FactoryBot.reload
41
41
  # Call the factory construction with the user given parameters
42
42
  entity = FactoryBot.create(*factory_params)
43
- # Render the resulting entity as an API v1 representation
44
- render plain: entity.to_json, content_type: 'application/json'
43
+ # Render the resulting entity with the configured rendering block
44
+ FactoryBot::Instrumentation.configuration.render_entity.call(self, entity)
45
45
  rescue StandardError => err
46
- # Log for local factory debugging and re-raise for canary onwards
47
- Rails.logger.error("#{err}\n#{err.backtrace.join("\n")}")
48
- raise err
46
+ # Handle any error gracefully with the configured error handler
47
+ FactoryBot::Instrumentation.configuration.render_error.call(self, err)
49
48
  end
50
49
 
51
50
  private
@@ -56,7 +55,13 @@ module FactoryBot::Instrumentation
56
55
  # @return [Array<Mixed>] the FactoryBot options
57
56
  def factory_params
58
57
  data = params.permit(:factory, traits: [])
59
- overwrite = params.to_unsafe_h.fetch(:overwrite, {}).deep_symbolize_keys
58
+
59
+ if Rails::VERSION::MAJOR >= 5
60
+ overwrite = params.to_unsafe_h.fetch(:overwrite, {})
61
+ .deep_symbolize_keys
62
+ else
63
+ overwrite = params.fetch('overwrite', {}).deep_symbolize_keys
64
+ end
60
65
 
61
66
  [
62
67
  data.fetch(:factory).to_sym,
@@ -1,7 +1,7 @@
1
1
  version: "3"
2
2
  services:
3
3
  test:
4
- image: ruby:2.3
4
+ image: ruby:2.4
5
5
  network_mode: bridge
6
6
  working_dir: /app
7
7
  volumes:
@@ -25,9 +25,9 @@ Gem::Specification.new do |spec|
25
25
  spec.add_runtime_dependency 'factory_bot'
26
26
 
27
27
  spec.add_development_dependency 'bundler', '>= 1.16', '< 3'
28
- spec.add_development_dependency 'rspec', '~> 3.0'
28
+ spec.add_development_dependency 'rspec-rails', '~> 3.8'
29
29
  spec.add_development_dependency 'simplecov', '~> 0.15'
30
30
  spec.add_development_dependency 'timecop', '~> 0.9.1'
31
- spec.add_development_dependency 'sqlite3'
31
+ spec.add_development_dependency 'sqlite3', '~> 1.3.6'
32
32
  spec.add_development_dependency 'appraisal'
33
33
  end
@@ -3,5 +3,6 @@
3
3
  source "https://rubygems.org"
4
4
 
5
5
  gem "rails", "~> 4.2.11"
6
+ gem "rails-api", "~> 0.4.1"
6
7
 
7
8
  gemspec path: "../"
@@ -15,6 +15,35 @@ module FactoryBot
15
15
  # You can set a fixed application name here,
16
16
  # defaults to your Rails application name in a titlized version
17
17
  config_accessor(:application_name) { nil }
18
+
19
+ # By default we use the Rails default JSON rendering mechanism, but
20
+ # you can configure your own logic here
21
+ config_accessor(:render_entity) do
22
+ proc do |controller, entity|
23
+ controller.render plain: entity.to_json,
24
+ content_type: 'application/json'
25
+ end
26
+ end
27
+
28
+ # By default we assemble a JSON response on errors which may be
29
+ # helpful for debugging, but you can configure your own logic here
30
+ config_accessor(:render_error) do
31
+ proc do |controller, error|
32
+ app_name = FactoryBot::Instrumentation.configuration.application_name
33
+ controller.render status: :internal_server_error,
34
+ content_type: 'application/json',
35
+ plain: {
36
+ application: app_name,
37
+ error: error.message,
38
+ backtrace: error.backtrace.join("\n")
39
+ }.to_json
40
+ end
41
+ end
42
+
43
+ # By default we do not perform any custom +before_action+ filters on the
44
+ # instrumentation controllers, with this option you can implement your
45
+ # custom logic like authentication
46
+ config_accessor(:before_action) { nil }
18
47
  end
19
48
  end
20
49
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module FactoryBot
4
4
  module Instrumentation
5
- VERSION = '0.2.0'.freeze
5
+ VERSION = '0.6.0'.freeze
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: factory_bot_instrumentation
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hermann Mayer
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-03-22 00:00:00.000000000 Z
11
+ date: 2020-09-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -59,19 +59,19 @@ dependencies:
59
59
  - !ruby/object:Gem::Version
60
60
  version: '3'
61
61
  - !ruby/object:Gem::Dependency
62
- name: rspec
62
+ name: rspec-rails
63
63
  requirement: !ruby/object:Gem::Requirement
64
64
  requirements:
65
65
  - - "~>"
66
66
  - !ruby/object:Gem::Version
67
- version: '3.0'
67
+ version: '3.8'
68
68
  type: :development
69
69
  prerelease: false
70
70
  version_requirements: !ruby/object:Gem::Requirement
71
71
  requirements:
72
72
  - - "~>"
73
73
  - !ruby/object:Gem::Version
74
- version: '3.0'
74
+ version: '3.8'
75
75
  - !ruby/object:Gem::Dependency
76
76
  name: simplecov
77
77
  requirement: !ruby/object:Gem::Requirement
@@ -104,16 +104,16 @@ dependencies:
104
104
  name: sqlite3
105
105
  requirement: !ruby/object:Gem::Requirement
106
106
  requirements:
107
- - - ">="
107
+ - - "~>"
108
108
  - !ruby/object:Gem::Version
109
- version: '0'
109
+ version: 1.3.6
110
110
  type: :development
111
111
  prerelease: false
112
112
  version_requirements: !ruby/object:Gem::Requirement
113
113
  requirements:
114
- - - ">="
114
+ - - "~>"
115
115
  - !ruby/object:Gem::Version
116
- version: '0'
116
+ version: 1.3.6
117
117
  - !ruby/object:Gem::Dependency
118
118
  name: appraisal
119
119
  requirement: !ruby/object:Gem::Requirement
@@ -188,7 +188,7 @@ files:
188
188
  homepage: https://github.com/hausgold/factory_bot_instrumentation
189
189
  licenses: []
190
190
  metadata: {}
191
- post_install_message:
191
+ post_install_message:
192
192
  rdoc_options: []
193
193
  require_paths:
194
194
  - lib
@@ -203,8 +203,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
203
203
  - !ruby/object:Gem::Version
204
204
  version: '0'
205
205
  requirements: []
206
- rubygems_version: 3.0.3
207
- signing_key:
206
+ rubygems_version: 3.1.4
207
+ signing_key:
208
208
  specification_version: 4
209
209
  summary: Allow your testers to generate test data on demand
210
210
  test_files: []