forked from fixin.me/fixin.me
		
	Merging from main master to my repo master. #4
@ -1,10 +1,16 @@
 | 
				
			|||||||
require "application_system_test_case"
 | 
					require "application_system_test_case"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class UnitsTest < ApplicationSystemTestCase
 | 
					class UnitsTest < ApplicationSystemTestCase
 | 
				
			||||||
  ADD_UNIT_LABELS = [t('units.index.add_unit'), t('units.unit.add_subunit')]
 | 
					  LINK_LABELS = {
 | 
				
			||||||
 | 
					    add_unit: t('units.index.add_unit'),
 | 
				
			||||||
 | 
					    add_subunit: t('units.unit.add_subunit'),
 | 
				
			||||||
 | 
					    edit: nil
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  setup do
 | 
					  setup do
 | 
				
			||||||
    @user = sign_in
 | 
					    @user = sign_in
 | 
				
			||||||
 | 
					    LINK_LABELS[:edit] = Regexp.union(@user.units.map(&:symbol))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    visit units_path
 | 
					    visit units_path
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -24,7 +30,8 @@ class UnitsTest < ApplicationSystemTestCase
 | 
				
			|||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  test "new" do
 | 
					  test "new" do
 | 
				
			||||||
    add_link = all(:link, exact_text: ADD_UNIT_LABELS.sample).sample
 | 
					    type, label = LINK_LABELS.assoc([:add_unit, :add_subunit].sample)
 | 
				
			||||||
 | 
					    add_link = all(:link, exact_text: label).sample
 | 
				
			||||||
    add_link.click
 | 
					    add_link.click
 | 
				
			||||||
    assert_equal 'disabled', add_link[:disabled]
 | 
					    assert_equal 'disabled', add_link[:disabled]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -34,9 +41,8 @@ class UnitsTest < ApplicationSystemTestCase
 | 
				
			|||||||
      maxlength = all(:fillable_field).to_h { |f| [f[:name], f[:maxlength].to_i || 2**16] }
 | 
					      maxlength = all(:fillable_field).to_h { |f| [f[:name], f[:maxlength].to_i || 2**16] }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      fill_in 'unit[symbol]',
 | 
					      fill_in 'unit[symbol]',
 | 
				
			||||||
        with: SecureRandom.random_symbol(rand([1..15, 15..maxlength['unit[symbol]']].sample))
 | 
					        with: random_string(rand([1..3, 4..maxlength['unit[symbol]']].sample))
 | 
				
			||||||
      fill_in 'unit[description]',
 | 
					      fill_in 'unit[description]', with: random_string(rand(0..maxlength['unit[description]']))
 | 
				
			||||||
        with: [nil, SecureRandom.alphanumeric(rand(1..maxlength['unit[description]']))].sample
 | 
					 | 
				
			||||||
      within :field, 'unit[multiplier]' do |field|
 | 
					      within :field, 'unit[multiplier]' do |field|
 | 
				
			||||||
        fill_in with: random_number(field[:max], field[:step])
 | 
					        fill_in with: random_number(field[:max], field[:step])
 | 
				
			||||||
      end if add_link[:text] != t('units.index.add_unit')
 | 
					      end if add_link[:text] != t('units.index.add_unit')
 | 
				
			||||||
@ -51,8 +57,8 @@ class UnitsTest < ApplicationSystemTestCase
 | 
				
			|||||||
      assert_selector 'tr', count: @user.units.count
 | 
					      assert_selector 'tr', count: @user.units.count
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
    assert_no_selector :element, :a, 'disabled': 'disabled',
 | 
					    assert_no_selector :element, :a, 'disabled': 'disabled',
 | 
				
			||||||
      exact_text: Regexp.union(ADD_UNIT_LABELS)
 | 
					      exact_text: Regexp.union(LINK_LABELS.fetch_values(:add_unit, :add_subunit))
 | 
				
			||||||
    assert_selector '.flash.notice', text: t('units.create.success', unit: @user.units.last.symbol)
 | 
					    assert_selector '.flash.notice', text: t('units.create.success', unit: Unit.all.last.symbol)
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  # TODO: check proper form/button redisplay and flash messages on add/edit
 | 
					  # TODO: check proper form/button redisplay and flash messages on add/edit
 | 
				
			||||||
@ -64,11 +70,7 @@ class UnitsTest < ApplicationSystemTestCase
 | 
				
			|||||||
    # Once new/edit form is open, attempt to open another one will close it
 | 
					    # Once new/edit form is open, attempt to open another one will close it
 | 
				
			||||||
    links = {}
 | 
					    links = {}
 | 
				
			||||||
    targets = {}
 | 
					    targets = {}
 | 
				
			||||||
    {
 | 
					    LINK_LABELS.each_pair do |type, labels|
 | 
				
			||||||
      add_unit: t('units.index.add_unit'),
 | 
					 | 
				
			||||||
      add_subunit: t('units.unit.add_subunit'),
 | 
					 | 
				
			||||||
      edit: Regexp.union(units.map(&:symbol))
 | 
					 | 
				
			||||||
    }.each_pair do |type, labels|
 | 
					 | 
				
			||||||
      links[type] = all(:link_or_button, exact_text: labels).to_a
 | 
					      links[type] = all(:link_or_button, exact_text: labels).to_a
 | 
				
			||||||
      targets[type] = links[type].sample
 | 
					      targets[type] = links[type].sample
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
				
			|||||||
@ -12,24 +12,26 @@ class ActiveSupport::TestCase
 | 
				
			|||||||
  include ActionMailer::TestHelper
 | 
					  include ActionMailer::TestHelper
 | 
				
			||||||
  include ActionView::Helpers::TranslationHelper
 | 
					  include ActionView::Helpers::TranslationHelper
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  # NOTE: use public #alphanumeric(chars: ...) from Ruby 3.3 onwards
 | 
					  # List of categorized Unicode characters:
 | 
				
			||||||
  SecureRandom.class_eval do
 | 
					  # * http://www.unicode.org/Public/UNIDATA/UnicodeData.txt
 | 
				
			||||||
    def self.random_symbol(n = 10)
 | 
					  # File format: http://www.unicode.org/L2/L1999/UnicodeData.html
 | 
				
			||||||
      # Unicode characters: 32-126, 160-383
 | 
					  # Select from graphic ranges: L, M, N, P, S, Zs
 | 
				
			||||||
      choose([*' '..'~', 160.chr(Encoding::UTF_8), *'¡'..'ſ'], n)
 | 
					  UNICODE_CHARS = {
 | 
				
			||||||
 | 
					    1 => [*"\u0020".."\u007E"],
 | 
				
			||||||
 | 
					    2 => [*"\u00A0".."\u00AC",
 | 
				
			||||||
 | 
					          *"\u00AE".."\u05FF",
 | 
				
			||||||
 | 
					          *"\u0606".."\u061B",
 | 
				
			||||||
 | 
					          *"\u061D".."\u06DC",
 | 
				
			||||||
 | 
					          *"\u06DE".."\u070E",
 | 
				
			||||||
 | 
					          *"\u0710".."\u07FF"]
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  UNICODE_CHARS.default = UNICODE_CHARS[1] + UNICODE_CHARS[2]
 | 
				
			||||||
 | 
					  def random_string(bytes = 10)
 | 
				
			||||||
 | 
					    result = ''
 | 
				
			||||||
 | 
					    while bytes > 0
 | 
				
			||||||
 | 
					      result += UNICODE_CHARS[bytes].sample.tap { |c| bytes -= c.bytesize }
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
  end
 | 
					    result
 | 
				
			||||||
 | 
					 | 
				
			||||||
  def randomize_user_password!(user)
 | 
					 | 
				
			||||||
    random_password.tap { |p| user.update!(password: p) }
 | 
					 | 
				
			||||||
  end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  def random_password
 | 
					 | 
				
			||||||
    SecureRandom.alphanumeric rand(Rails.configuration.devise.password_length)
 | 
					 | 
				
			||||||
  end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  def random_email
 | 
					 | 
				
			||||||
    "%s@%s.%s" % (1..3).map { SecureRandom.alphanumeric(rand(1..20)) }
 | 
					 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  # Assumes: max >= step and step = 1e[-]N, both as strings
 | 
					  # Assumes: max >= step and step = 1e[-]N, both as strings
 | 
				
			||||||
@ -44,6 +46,18 @@ class ActiveSupport::TestCase
 | 
				
			|||||||
    BigDecimal(step) * d
 | 
					    BigDecimal(step) * d
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  def randomize_user_password!(user)
 | 
				
			||||||
 | 
					    random_password.tap { |p| user.update!(password: p) }
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  def random_password
 | 
				
			||||||
 | 
					    Random.alphanumeric rand(Rails.configuration.devise.password_length)
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  def random_email
 | 
				
			||||||
 | 
					    "%s@%s.%s" % (1..3).map { Random.alphanumeric(rand(1..20)) }
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def with_last_email
 | 
					  def with_last_email
 | 
				
			||||||
    yield(ActionMailer::Base.deliveries.last)
 | 
					    yield(ActionMailer::Base.deliveries.last)
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user