<commit_before># encoding: utf-8

require File.expand_path('../../test_helper', __FILE__)

class MaybeeTest < ActiveSupport::TestCase

  setup do
    %w(Opel GM Volkswagen Audi Porsche Jaguar Bugatti).each { |name| Make.create!(:name => name) }
    @bonnie = Driver.create!(:name => 'Bonnie', :level => 1)
    @clyde = Driver.create!(:name => 'Clyde', :level => 3)
    @ferdinand = Driver.create!(:name => 'Ferdinand', :level => 3, :super_powers => true)
    @opel_workshop = Workshop.create(:name => 'Opel Workshop', :make_ids => Make['Opel', 'GM'])
    @vag_workshop  = Workshop.create(:name => 'VAG Workshop', :make_ids => Make['Volkswagen', 'Audi', 'Porsche'])
    @jaguar_workshop = Workshop.create(:name => 'Jaguar Workshop', :make_ids => Make['Jaguar'])
  end
  
  test 'Translation of error messages is present' do
    assert_equal 'You are not authorized to create this object', I18n.t('activerecord.errors.messages.not_authorized', :access => 'create')
  end
  
  test 'Authorize sets error if unauthorized' do
    mantra = Car.new(:model => 'Opel Mantra', :make_id => Make[:Opel], :minimum_driver_level => 0)
    assert_valid mantra
    assert_nil mantra.authorization_subject
    assert_save mantra
    assert_equal false, mantra.authorize?(:destroy)
    assert_error_on mantra, :not_authorized
    mantra.authorization_subject = @bonnie
    assert_equal true, mantra.authorize?(:destroy)
  end
    
  test 'Creation, updating and destruction' do
    mantra = Car.new(:model => 'Opel Mantra', :make_id => Make[:Opel], :minimum_driver_level => 0)
    assert_valid mantra
    assert_nil mantra.authorization_subject
    assert_save mantra
    assert_equal true, mantra.update_attributes!(:minimum_driver_level => 2)
    assert_equal 2, mantra.reload.minimum_driver_level
    assert_no_difference 'Car.count' do
      assert_equal false, mantra.destroy
      assert_error_on mantra, :not_authorized
    end
    
  end
  
  test 'Exclusive authorizations' do
    bugatti = ExclusiveCar.new(:model => '<NAME>', :make_id => Make['Bugatti'], :minimum_driver_level => 9)
    assert_equal false, bugatti.save
    bugatti.authorization_subject = @clyde
    assert_equal false, @clyde.super_powers?
    assert_equal false, bugatti.save
    assert_valid bugatti # clears errors
    assert_equal false, bugatti.save
    assert_error_on bugatti, :not_authorized
    bugatti.authorization_subject = @ferdinand
    assert_equal true, bugatti.authorize?(:create)
    assert_save bugatti
    bugatti = Car.find(bugatti.id)
    assert_nil bugatti.authorization_subject
    assert_no_difference 'Car.count' do
      assert_equal false, bugatti.destroy
      assert_error_on bugatti, :not_authorized
    end
    bugatti.authorization_subject = @ferdinand
    assert_difference 'Car.count', -1 do
      assert bugatti.destroy
    end
  end
  

  test 'Inheritance' do
    skip 'Test missing, flunk!'
  end

end<commit_msg>Ensure Ferdinand has super_powers<commit_after># encoding: utf-8

require File.expand_path('../../test_helper', __FILE__)

class MaybeeTest < ActiveSupport::TestCase

  setup do
    %w(Opel GM Volkswagen Audi Porsche Jaguar Bugatti).each { |name| Make.create!(:name => name) }
    @bonnie = Driver.create!(:name => 'Bonnie', :level => 1)
    @clyde = Driver.create!(:name => 'Clyde', :level => 3)
    @ferdinand = Driver.create!(:name => 'Ferdinand', :level => 3, :super_powers => true)
    @opel_workshop = Workshop.create(:name => 'Opel Workshop', :make_ids => Make['Opel', 'GM'])
    @vag_workshop  = Workshop.create(:name => 'VAG Workshop', :make_ids => Make['Volkswagen', 'Audi', 'Porsche'])
    @jaguar_workshop = Workshop.create(:name => 'Jaguar Workshop', :make_ids => Make['Jaguar'])
  end
  
  test 'Translation of error messages is present' do
    assert_equal 'You are not authorized to create this object', I18n.t('activerecord.errors.messages.not_authorized', :access => 'create')
  end
  
  test 'Authorize sets error if unauthorized' do
    mantra = Car.new(:model => 'Opel Mantra', :make_id => Make[:Opel], :minimum_driver_level => 0)
    assert_valid mantra
    assert_nil mantra.authorization_subject
    assert_save mantra
    assert_equal false, mantra.authorize?(:destroy)
    assert_error_on mantra, :not_authorized
    mantra.authorization_subject = @bonnie
    assert_equal true, mantra.authorize?(:destroy)
  end
    
  test 'Creation, updating and destruction' do
    mantra = Car.new(:model => 'Opel Mantra', :make_id => Make[:Opel], :minimum_driver_level => 0)
    assert_valid mantra
    assert_nil mantra.authorization_subject
    assert_save mantra
    assert_equal true, mantra.update_attributes!(:minimum_driver_level => 2)
    assert_equal 2, mantra.reload.minimum_driver_level
    assert_no_difference 'Car.count' do
      assert_equal false, mantra.destroy
      assert_error_on mantra, :not_authorized
    end
    
  end
  
  test 'Exclusive authorizations' do
    bugatti = ExclusiveCar.new(:model => 'Bugatti Veyron', :make_id => Make['Bugatti'], :minimum_driver_level => 9)
    assert_equal false, bugatti.save
    bugatti.authorization_subject = @clyde
    assert_equal false, @clyde.super_powers?
    assert_equal false, bugatti.save
    assert_valid bugatti # clears errors
    assert_equal false, bugatti.save
    assert_error_on bugatti, :not_authorized
    bugatti.authorization_subject = @ferdinand
    assert_equal true, @ferdinand.super_powers?
    assert_equal true, bugatti.authorize?(:create)
    assert_save bugatti
    bugatti = Car.find(bugatti.id)
    assert_nil bugatti.authorization_subject
    assert_no_difference 'Car.count' do
      assert_equal false, bugatti.destroy
      assert_error_on bugatti, :not_authorized
    end
    bugatti.authorization_subject = @ferdinand
    assert_difference 'Car.count', -1 do
      assert bugatti.destroy
    end
  end
  

  test 'Inheritance' do
    skip 'Test missing, flunk!'
  end

end
{'fraction_non_alphanumeric': 0.1111864406779661, 'fraction_numerical': 0.004067796610169492, 'mean_word_length': 4.126846220677671, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 54, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7429247', 'n_tokens_mistral': 1955, 'n_tokens_neox': 1874, 'n_words': 454}