Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bye bye Spree] Make OFN independent of spec helpers under core/lib/testing_support #5941

Merged
merged 11 commits into from
Aug 27, 2020
9 changes: 6 additions & 3 deletions lib/open_food_network/enterprise_issue_validator.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module OpenFoodNetwork
class EnterpriseIssueValidator
include Rails.application.routes.url_helpers
include Spree::TestingSupport::UrlHelpers

def initialize(enterprise)
@enterprise = enterprise
Expand All @@ -13,14 +12,14 @@ def issues
unless shipping_methods_ok?
issues << {
description: I18n.t('admin.enterprise_issues.has_no_shipping_methods', enterprise: @enterprise.name),
link: "<a class='button fullwidth' href='#{spree.new_admin_shipping_method_path}'>#{I18n.t('admin.enterprise_issues.create_new')}</a>"
link: "<a class='button fullwidth' href='#{spree_routes_helper.new_admin_shipping_method_path}'>#{I18n.t('admin.enterprise_issues.create_new')}</a>"
}
end

unless payment_methods_ok?
issues << {
description: I18n.t('admin.enterprise_issues.has_no_payment_methods', enterprise: @enterprise.name),
link: "<a class='button fullwidth' href='#{spree.new_admin_payment_method_path}'>#{I18n.t('admin.enterprise_issues.create_new')}</a>"
link: "<a class='button fullwidth' href='#{spree_routes_helper.new_admin_payment_method_path}'>#{I18n.t('admin.enterprise_issues.create_new')}</a>"
}
end

Expand Down Expand Up @@ -64,5 +63,9 @@ def payment_methods_ok?

@enterprise.payment_methods.available.any?
end

def spree_routes_helper
Spree::Core::Engine.routes.url_helpers
end
end
end
36 changes: 24 additions & 12 deletions spec/controllers/spree/admin/users_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'spec_helper'
require 'spree/testing_support/bar_ability'

describe Spree::Admin::UsersController do
context '#authorize_admin' do
Expand Down Expand Up @@ -34,18 +33,31 @@
expect(response).to redirect_to(spree.edit_admin_user_path(test_user))
end

it 'should deny access to users with an bar role' do
user.spree_roles << Spree::Role.find_or_create_by(name: 'bar')
Spree::Ability.register_ability(BarAbility)
spree_post :index
expect(response).to redirect_to('/unauthorized')
end
describe "with BarAbility" do
class BarAbility
include CanCan::Ability

it 'should deny access to users with an bar role' do
user.spree_roles << Spree::Role.find_or_create_by(name: 'bar')
Spree::Ability.register_ability(BarAbility)
spree_post :update, id: '9'
expect(response).to redirect_to('/unauthorized')
def initialize(user)
user ||= Spree::User.new
return unless user.has_spree_role?('bar')

can [:admin, :index, :show], Spree::Order
end
end

it 'should deny access to users with an bar role' do
user.spree_roles << Spree::Role.find_or_create_by(name: 'bar')
Spree::Ability.register_ability(BarAbility)
spree_post :index
expect(response).to redirect_to('/unauthorized')
end

it 'should deny access to users with an bar role' do
user.spree_roles << Spree::Role.find_or_create_by(name: 'bar')
Spree::Ability.register_ability(BarAbility)
spree_post :update, id: '9'
expect(response).to redirect_to('/unauthorized')
end
end

it 'should deny access to users without an admin role' do
Expand Down
1 change: 1 addition & 0 deletions spec/features/admin/configuration/states_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

describe "States" do
include AuthenticationHelper
include WebHelper

let!(:country) { create(:country) }

Expand Down
9 changes: 5 additions & 4 deletions spec/features/admin/configuration/tax_categories_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

describe "Tax Categories" do
include AuthenticationHelper
include WebHelper

before(:each) do
login_as_admin_and_visit spree.edit_admin_general_settings_path
Expand All @@ -13,9 +14,9 @@
click_link "Tax Categories"
expect(page).to have_content("Listing Tax Categories")
within_row(1) do
expect(column_text(1)).to eq("Clothing")
expect(column_text(2)).to eq("For Clothing")
expect(column_text(3)).to eq("False")
expect(find("td:nth-child(1)").text).to eq("Clothing")
expect(find("td:nth-child(2)").text).to eq("For Clothing")
expect(find("td:nth-child(3)").text).to eq("False")
end
end
end
Expand Down Expand Up @@ -44,7 +45,7 @@
it "should be able to update an existing tax category" do
create(:tax_category)
click_link "Tax Categories"
within_row(1) { click_icon :edit }
within_row(1) { find(".icon-edit").click }
fill_in "tax_category_description", with: "desc 99"
click_button "Update"
expect(page).to have_content("successfully updated!")
Expand Down
3 changes: 2 additions & 1 deletion spec/features/admin/configuration/taxonomies_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

describe "Taxonomies" do
include AuthenticationHelper
include WebHelper

before(:each) do
login_as_admin_and_visit spree.edit_admin_general_settings_path
Expand Down Expand Up @@ -43,7 +44,7 @@
it "should allow an admin to update an existing taxonomy" do
create(:taxonomy)
click_link "Taxonomies"
within_row(1) { click_icon :edit }
within_row(1) { find(".icon-edit").click }
fill_in "taxonomy_name", with: "sports 99"
click_button "Update"
expect(page).to have_content("successfully updated!")
Expand Down
1 change: 1 addition & 0 deletions spec/features/admin/configuration/zones_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

describe "Zones" do
include AuthenticationHelper
include WebHelper

before do
login_as_admin
Expand Down
4 changes: 2 additions & 2 deletions spec/features/admin/enterprise_groups_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
fill_in 'enterprise_group_name', with: 'EGEGEG'
fill_in 'enterprise_group_description', with: 'This is a description'
check 'enterprise_group_on_front_page'
select2_search e1.name, from: 'Enterprises'
select2_search e2.name, from: 'Enterprises'
targetted_select2_search e1.name, from: '#s2id_enterprise_group_enterprise_ids'
targetted_select2_search e2.name, from: '#s2id_enterprise_group_enterprise_ids'
click_link 'Contact'
fill_in 'enterprise_group_address_attributes_phone', with: '000'
fill_in 'enterprise_group_address_attributes_address1', with: 'My Street'
Expand Down
2 changes: 1 addition & 1 deletion spec/features/admin/orders_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
uncheck 'Only show complete orders'
page.find('a.icon-search').click

click_icon :edit
find(".icon-edit").click

expect(page).to have_current_path spree.edit_admin_order_path(incomplete_order)
end
Expand Down
1 change: 0 additions & 1 deletion spec/features/admin/overview_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
', js: true do
include WebHelper
include AuthenticationHelper
include ::Spree::TestingSupport::AuthorizationHelpers

context "as an enterprise user" do
before do
Expand Down
2 changes: 1 addition & 1 deletion spec/features/admin/subscriptions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ def expect_not_in_open_or_upcoming_order_cycle_warning(count)
def add_variant_to_subscription(variant, quantity)
row_count = all("#subscription-line-items .item").length
variant_name = variant.full_name.present? ? "#{variant.name} - #{variant.full_name}" : variant.name
select2_search variant.name, from: I18n.t(:name_or_sku), dropdown_css: ".select2-drop", select_text: variant_name
targetted_select2_search variant.name, from: "#s2id_add_variant_id", dropdown_css: ".select2-drop", select_text: variant_name
fill_in "add_quantity", with: quantity
click_link "Add"
expect(page).to have_selector("#subscription-line-items .item", count: row_count + 1)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'spec_helper'
require 'open_food_network/enterprise_issue_validator'

module OpenFoodNetwork
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/spree/i18n_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require 'rspec/expectations'
require 'spree/i18n'
require 'spree/testing_support/i18n'
require 'support/i18n_translations_checker'

describe "i18n" do
before do
Expand Down
2 changes: 2 additions & 0 deletions spec/lib/spree/money_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require 'spec_helper'

describe Spree::Money do
include PreferencesHelper

before do
configure_spree_preferences do |config|
config.currency = "USD"
Expand Down
13 changes: 7 additions & 6 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
end
end

# Allow connections to phantomjs/selenium whilst raising errors
# when connecting to external sites
# Allow connections to selenium whilst raising errors when connecting to external sites
require 'webmock/rspec'
WebMock.enable!
WebMock.disable_net_connect!(
Expand All @@ -44,10 +43,7 @@
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].sort.each { |f| require f }
require 'spree/testing_support/capybara_ext'
require 'spree/api/testing_support/setup'
require 'spree/testing_support/authorization_helpers'
require 'spree/testing_support/preferences'
require 'support/api_helper'

# Capybara config
Expand All @@ -65,6 +61,11 @@

Capybara.default_max_wait_time = 30

Capybara.configure do |config|
config.match = :prefer_exact
config.ignore_hidden_elements = true
end

require "paperclip/matchers"

# Override setting in Spree engine: Spree::Core::MailSettings
Expand Down Expand Up @@ -155,7 +156,7 @@ def restart_driver
config.include Spree::UrlHelpers
config.include Spree::CheckoutHelpers
config.include Spree::MoneyHelper
config.include Spree::TestingSupport::Preferences
config.include PreferencesHelper
config.include ControllerRequestsHelper, type: :controller
config.include Devise::TestHelpers, type: :controller
config.extend Spree::Api::TestingSupport::Setup, type: :controller
Expand Down
98 changes: 98 additions & 0 deletions spec/support/i18n_translations_checker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# frozen_string_literal: true

# This file exists solely to test whether or not there are missing translations
# within the code that Spree's test suite covers.
#
# If there is a translation referenced which has no corresponding key within the
# .yml file, then there will be a message output at the end of the suite showing
# that.
#
# If there is a translation within the locale file which *isn't* used in the
# test, this will also be shown at the end of the suite run.
module Spree
class << self
attr_accessor :used_translations, :missing_translation_messages,
:unused_translations, :unused_translation_messages
alias_method :normal_t, :t
end

def self.t(*args)
original_args = args.dup
options = args.extract_options!
self.used_translations ||= []
[*args.first].each do |translation_key|
key = ([*options[:scope]] << translation_key).join('.')
self.used_translations << key
end
normal_t(*original_args)
end

def self.check_missing_translations
self.missing_translation_messages = []
self.used_translations ||= []
used_translations.map { |a| a.split('.') }.each do |translation_keys|
root = translations
processed_keys = []
translation_keys.each do |key|
begin
root = root.fetch(key.to_sym)
processed_keys << key.to_sym
rescue KeyError
error = "#{(processed_keys << key).join('.')} (#{I18n.locale})"
unless Spree.missing_translation_messages.include?(error)
Spree.missing_translation_messages << error
end
end
end
end
end

def self.check_unused_translations
self.used_translations ||= []
self.unused_translation_messages = []
self.unused_translations = []
load_translations(translations)
translation_diff = unused_translations - used_translations
translation_diff.each do |translation|
Spree.unused_translation_messages << "#{translation} (#{I18n.locale})"
end
end

def self.load_translations(hash, root = [])
hash.each do |k, v|
if v.is_a?(Hash)
load_translations(v, root.dup << k)
else
key = (root + [k]).join('.')
unused_translations << key
end
end
end
private_class_method :load_translations

def self.translations
@translations ||= I18n.backend.__send__(:translations)[I18n.locale][:spree]
end
private_class_method :translations
end

RSpec.configure do |config|
# Need to check here again because this is used in i18n_spec too.
if ENV['CHECK_TRANSLATIONS']
config.after :suite do
Spree.check_missing_translations
if Spree.missing_translation_messages.any?
puts "\nThere are missing translations within Spree:"
puts Spree.missing_translation_messages.sort
exit(1)
end

Spree.check_unused_translations
if Spree.unused_translation_messages.any?
puts "\nThere are unused translations within Spree:"
puts Spree.unused_translation_messages.sort
exit(1)
end
end
end
end
2 changes: 0 additions & 2 deletions spec/support/matchers/select2_matchers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
match do |node|
@id, @options, @node = id, options, node

# id = find_label_by_text(locator)
from = "#s2id_#{id}"

results = []
Expand Down Expand Up @@ -35,7 +34,6 @@
match_when_negated do |node|
@id, @options, @node = id, options, node

# id = find_label_by_text(locator)
from = "#s2id_#{id}"

results = []
Expand Down
23 changes: 23 additions & 0 deletions spec/support/preferences_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

module PreferencesHelper
# Resets all preferences to default values, you can
# pass a block to override the defaults with a block
#
# reset_spree_preferences do |config|
# config.site_name = "my fancy pants store"
# end
#
def reset_spree_preferences(&config_block)
Spree::Preferences::Store.instance.persistence = false
Spree::Preferences::Store.instance.clear_cache

config = Rails.application.config.spree.preferences
configure_spree_preferences(&config_block) if block_given?
end

def configure_spree_preferences
config = Rails.application.config.spree.preferences
yield(config) if block_given?
end
end
Loading