effective_test_bot 1.0.0 → 1.0.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 64b44fba9d50f01fd32b612de3d11d810c6ba12a
4
- data.tar.gz: a07e93aee37f18a0af63098c6ac6ac540d61d8a8
3
+ metadata.gz: 24fabf2bbe4a09876a2920587a60881ef48559e1
4
+ data.tar.gz: fd5df539a7a87cdd4b0ca95854c3917d34793c31
5
5
  SHA512:
6
- metadata.gz: 5ba5cc490a7296da4d6a82f33faa5cc7bce2f8b2cbe29b01b913812bd59963b0381d73c58d886792144a8a318f27c56389ebdbc5412451abcf624863f0e6bbc7
7
- data.tar.gz: fc19738de32fde123f04dd8b606eb17627236601323dc8a9267d58a5d88ca195a467943a97a5ba9efb9fe6a0004b8711bb1fc3c37ffe1863f69309e391a1c75f
6
+ metadata.gz: 8cd5f562b9690e6a9f593f3cc9702198d8a5bdcd1a6493b07831f765d601c77a59407083f77e1a8cb967a5802c0b4d85034cf531d9744a05714c173d17f71ced
7
+ data.tar.gz: 34737fdab12778d809e632761248cd23cbbcca8c3a388d64806d7437fe7814cce4ea27679f12ac59ae54a664f2718dd45fccde95dcfafad2268a519b2b900518
@@ -3,22 +3,18 @@ module EffectiveTestBotControllerHelper
3
3
 
4
4
  # This is included as an after_action in the controller
5
5
  def assign_test_bot_payload(payload = {})
6
- return unless response.content_type == 'text/html'.freeze
7
- return unless !!(response.body[BODY_TAG])
8
-
9
6
  payload.merge!({ response_code: response.code, assigns: test_bot_view_assigns, flash: flash.to_hash })
10
7
 
11
- payload = view_context.content_tag(:script, id: 'test_bot_payload') do
12
- [
13
- '',
14
- 'window.effective_test_bot = {};',
15
- payload.map { |k, v| "window.effective_test_bot.#{k} = #{v.respond_to?(:to_json) ? v.to_json : ("'" + v + "'")};" },
16
- '',
17
- ].join("\n").html_safe
18
- end
8
+ if response.content_type == 'text/html' && response.body[BODY_TAG].present?
9
+ payload = view_context.content_tag(:script, build_payload_javascript(payload), id: 'test_bot_payload')
10
+
11
+ split = response.body.split(BODY_TAG)
12
+ response.body = "#{split.first}#{payload}#{BODY_TAG}#{split.last if split.size > 1}"
13
+ elsif response.content_type == 'text/javascript' && response.body.present?
14
+ payload = build_payload_javascript(payload)
19
15
 
20
- split = response.body.split(BODY_TAG)
21
- response.body = "#{split.first}#{payload}#{BODY_TAG}#{split.last if split.size > 1}"
16
+ response.body = "#{response.body};#{payload}"
17
+ end
22
18
  end
23
19
 
24
20
  # This is called in an ActionController rescue_from.
@@ -28,6 +24,15 @@ module EffectiveTestBotControllerHelper
28
24
 
29
25
  private
30
26
 
27
+ def build_payload_javascript(payload)
28
+ [
29
+ '',
30
+ 'window.effective_test_bot = {};',
31
+ payload.map { |k, v| "window.effective_test_bot.#{k} = #{v.respond_to?(:to_json) ? v.to_json : ("'" + v + "'")};" },
32
+ '',
33
+ ].join("\n").html_safe
34
+ end
35
+
31
36
  def test_bot_access_denied(exception)
32
37
  {
33
38
  access_denied: exception,
@@ -1,6 +1,7 @@
1
1
  require 'effective_resources'
2
2
  require 'effective_test_bot/engine'
3
3
  require 'effective_test_bot/dsl'
4
+ require 'effective_test_bot/middleware'
4
5
  require 'effective_test_bot/version'
5
6
 
6
7
  module EffectiveTestBot
@@ -12,6 +12,12 @@ module EffectiveTestBot
12
12
  eval File.read("#{config.root}/config/effective_test_bot.rb")
13
13
  end
14
14
 
15
+ initializer 'effective_test_bot.middleware' do |app|
16
+ if Rails.env.test?
17
+ Rails.application.config.middleware.use EffectiveTestBot::Middleware
18
+ end
19
+ end
20
+
15
21
  initializer 'effective_test_bot.email_logger' do |app|
16
22
  if Rails.env.test?
17
23
  ActiveSupport.on_load :action_mailer do
@@ -0,0 +1,40 @@
1
+ # Watch for any rails server exceptions and write the stacktrace to ./tmp/test_bot/exception.txt
2
+ # This file is checked for by assert_no_exceptions
3
+
4
+ module EffectiveTestBot
5
+ class Middleware
6
+ def initialize(app)
7
+ @app = app
8
+ end
9
+
10
+ def call(env)
11
+ begin
12
+ @app.call(env)
13
+ rescue Exception => exception
14
+ begin
15
+ save(exception)
16
+ rescue => e
17
+ puts "TestBotError: An error occurred while attempting to save a rails server exception: #{e.message}"
18
+ end
19
+
20
+ raise exception
21
+ end
22
+ end
23
+
24
+ def save(exception)
25
+ lines = [exception.message] + exception.backtrace.first(EffectiveTestBot&.backtrace_lines || 10)
26
+
27
+ dir = File.join(Dir.pwd, 'tmp', 'test_bot')
28
+ file = File.join(dir, 'exception.txt')
29
+
30
+ Dir.mkdir(dir) unless File.exist?(dir)
31
+ File.delete(file) if File.exist?(file)
32
+
33
+ File.open(file, 'w') do |file|
34
+ file.write "================== Start server exception ==================\n"
35
+ lines.each { |line| file.write(line); file.write("\n") }
36
+ file.write "=================== End server exception ===================\n"
37
+ end
38
+ end
39
+ end
40
+ end
@@ -1,3 +1,3 @@
1
1
  module EffectiveTestBot
2
- VERSION = '1.0.0'.freeze
2
+ VERSION = '1.0.1'.freeze
3
3
  end
@@ -17,6 +17,17 @@ module EffectiveTestBotAssertions
17
17
  assert assigns['current_user'].blank?, message || 'Expected @current_user to be blank when signed out'
18
18
  end
19
19
 
20
+ def assert_no_exceptions(message = "(no_exceptions) Unexpected rails server exception:\n:exception:")
21
+ # this file is created by EffectiveTestBot::Middleware when an exception is encountered in the rails app
22
+ file = File.join(Dir.pwd, 'tmp', 'test_bot', 'exception.txt')
23
+ return unless File.exist?(file)
24
+
25
+ exception = File.read(file)
26
+ File.delete(file)
27
+
28
+ assert false, message.sub(':exception:', exception)
29
+ end
30
+
20
31
  def assert_can_execute_javascript(message = "Expected page.evaluate_script() to be successful")
21
32
  error = nil; result = nil;
22
33
 
@@ -60,7 +71,7 @@ module EffectiveTestBotAssertions
60
71
 
61
72
  info = [
62
73
  "Encountered a 403 Access Denied",
63
- ("(cannot :#{exception['action']}, #{exception['subject']})" if exception.present?),
74
+ ("(cannot :#{exception[:action]}, #{exception[:subject]})" if exception.present?),
64
75
  "on #{page.current_path} as user #{user || 'no user'}.",
65
76
  ("\nAdd assign_test_bot_access_denied_exception(exception) if defined?(EffectiveTestBot) to the very bottom of your ApplicationController's rescue_from block to gather more information." unless exception.present?),
66
77
  ].compact.join(' ')
@@ -28,6 +28,7 @@ module EffectiveTestBotFormHelper
28
28
  end
29
29
 
30
30
  assert_no_assigns_errors unless test_bot_skip?(:no_assigns_errors)
31
+ assert_no_exceptions unless test_bot_skip?(:exceptions)
31
32
  assert_authorization unless test_bot_skip?(:authorization)
32
33
  assert_page_status unless test_bot_skip?(:page_status)
33
34
 
@@ -37,6 +38,7 @@ module EffectiveTestBotFormHelper
37
38
  # Submit form after disabling any HTML5 validations
38
39
  def submit_novalidate_form(label = nil)
39
40
  page.execute_script "for(var f=document.forms,i=f.length;i--;)f[i].setAttribute('novalidate','');"
41
+ page.execute_script "$('form').find('[required]').removeAttr('required');"
40
42
  click_submit(label)
41
43
  true
42
44
  end
@@ -53,7 +53,11 @@ module EffectiveTestBotTestHelper
53
53
 
54
54
  # Calls capybara within do .. end if selector is present and bool is true
55
55
  def within_if(selector, bool = true, &block)
56
- (selector.present? && bool) ? within(selector) { yield } : yield
56
+ (selector.present? && bool) ? within(first(selector)) { yield } : yield
57
+ end
58
+
59
+ def within_each(selector, &block)
60
+ all(selector).each { |field| within(field) { yield } }
57
61
  end
58
62
 
59
63
  def click_first(label)
@@ -63,7 +67,7 @@ module EffectiveTestBotTestHelper
63
67
  # EffectiveTestBot includes an after_filter on ApplicationController to set an http header
64
68
  # These values are 'from the last page submit or refresh'
65
69
  def response_code
66
- page.evaluate_script('window.effective_test_bot.response_code')&.to_i
70
+ (page.evaluate_script('window.effective_test_bot.response_code')&.to_i rescue nil)
67
71
  end
68
72
 
69
73
  def flash
@@ -78,9 +82,9 @@ module EffectiveTestBotTestHelper
78
82
  return nil unless page.evaluate_script('window.effective_test_bot.access_denied').present?
79
83
 
80
84
  {
81
- 'exception': page.evaluate_script('window.effective_test_bot.access_denied'),
82
- 'action': page.evaluate_script('window.effective_test_bot.action'),
83
- 'subject': page.evaluate_script('window.effective_test_bot.subject')
85
+ exception: page.evaluate_script('window.effective_test_bot.access_denied'),
86
+ action: page.evaluate_script('window.effective_test_bot.action'),
87
+ subject: page.evaluate_script('window.effective_test_bot.subject')
84
88
  }
85
89
  end
86
90
 
@@ -8,6 +8,7 @@ module BaseTest
8
8
  return if test_bot_skip?(:normal)
9
9
 
10
10
  assert_authorization unless test_bot_skip?(:authorization)
11
+ assert_no_exceptions unless test_bot_skip?(:exceptions)
11
12
  assert_page_status unless test_bot_skip?(:page_status)
12
13
  assert_no_js_errors unless test_bot_skip?(:no_js_errors)
13
14
  assert_page_title unless (test_bot_skip?(:page_title) || all('head').blank?)
@@ -53,6 +54,7 @@ module BaseTest
53
54
  visit(new_path)
54
55
 
55
56
  assert_authorization(hint)
57
+ assert_no_exceptions
56
58
  assert_page_status
57
59
 
58
60
  assert_form("form#new_#{resource_name}", "TestBotError: Failed to find form#new_#{resource_name}. #{hint}") unless test_bot_skip?(:form)
@@ -64,6 +66,7 @@ module BaseTest
64
66
  submit_novalidate_form
65
67
 
66
68
  assert_authorization(hint)
69
+ assert_no_exceptions
67
70
  assert_page_status
68
71
  end
69
72
 
@@ -93,23 +96,23 @@ module BaseTest
93
96
  (page.document.first(:css, selector) rescue nil)
94
97
  end
95
98
 
99
+ def effective_resource
100
+ @effective_resource ||= Effective::Resource.new([controller_namespace, resource_class].join('/'))
101
+ end
102
+
96
103
  def resources_path # index, create
97
- path = polymorphic_path([*controller_namespace, resource_class]) rescue nil
98
- path || polymorphic_path([*controller_namespace.try(:singularize), resource_class])
104
+ effective_resource.action_path(:index)
99
105
  end
100
106
 
101
107
  def resource_path(resource) # show, update, destroy
102
- path = polymorphic_path([*controller_namespace, resource]) rescue nil
103
- path || polymorphic_path([*controller_namespace.try(:singularize), resource])
108
+ effective_resource.action_path(:show, resource)
104
109
  end
105
110
 
106
111
  def new_resource_path # new
107
- path = new_polymorphic_path([*controller_namespace, resource_class]) rescue nil
108
- path || new_polymorphic_path([*controller_namespace.try(:singularize), resource_class])
112
+ effective_resource.action_path(:new)
109
113
  end
110
114
 
111
115
  def edit_resource_path(resource) # edit
112
- path = edit_polymorphic_path([*controller_namespace, resource]) rescue nil
113
- path || edit_polymorphic_path([*controller_namespace.try(:singularize), resource])
116
+ effective_resource.action_path(:edit, resource)
114
117
  end
115
118
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: effective_test_bot
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Code and Effect
@@ -160,6 +160,7 @@ files:
160
160
  - lib/effective_test_bot.rb
161
161
  - lib/effective_test_bot/dsl.rb
162
162
  - lib/effective_test_bot/engine.rb
163
+ - lib/effective_test_bot/middleware.rb
163
164
  - lib/effective_test_bot/version.rb
164
165
  - lib/generators/effective_test_bot/install_generator.rb
165
166
  - lib/tasks/effective_test_bot_tasks.rake