# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require 'simplecov'
SimpleCov.start 'rails'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/matchers'
require 'capybara/rspec'
require 'turnip/capybara'

# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
#Dir[Rails.root.join("spec/factories/**/*.rb")].each {|f| require f }
Dir.glob("spec/acceptance_steps/**/*steps.rb") { |f| load f, true }

I18n.locale =I18n.default_locale
Devise.stretches = 1
Capybara.javascript_driver = :webkit
Capybara.default_wait_time = 4 # ember needs more time than the default of 2
#Capybara.javascript_driver = :selenium

module FactoryAttributesFor
  def attributes_for(obj, options={})
    super(obj, options).merge(build(obj).attributes.select{|k,v| k =~ /_id$/}).symbolize_keys
  end
end
module SpecSelectorHelpers
  def top_navigation
    '.navbar-fixed-top'
  end

  # Uses the click_on method for capybara
  def click_on_translation(key)
    text = I18n.t(key)
    text.should_not =~ /missing/
    click_on text
  end

  # same as save_and_open_page but with styling
  def show_page
    save_page Rails.root.join( 'public', 'capybara.html' )
    %x(launchy http://localhost:3000/capybara.html)
  end
end

class Couchbase::View
  alias :old_initialize :initialize
  def initialize(bucket, endpoint, params = {})
    old_initialize(bucket, endpoint, params.merge(stale: false))
  end
end

RSpec.configure do |config|
  # == Mock Framework
  #
  # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
  #
  # config.mock_with :mocha
  # config.mock_with :flexmock
  # config.mock_with :rr
  config.mock_with :rspec
  config.include FactoryGirl::Syntax::Methods
  config.include FactoryAttributesFor
  config.include Devise::TestHelpers, type: :controller
  config.include EndWithMatcher
  config.include Matchers
  config.include Features::BasicHelpers, type: :feature
  config.include SpecRouteHelpers, type: :feature
  config.include SpecEmberHelpers, type: :feature
  #config.use_transactional_fixtures = true
  config.infer_base_class_for_anonymous_controllers = true
  config.filter_run_excluding broken: true
  config.render_views = true

  OmniAuth.config.test_mode = true
  OmniAuth.config.add_mock :facebook, {
    info: {
      nickname: 'Joey',
      name: "Facebook Joe", 
      first_name: "Facebook Joe"
    },
    credentials: {
      'token' => 'fbAuthToken234',
      'expires_at' => 1.week.from_now.to_i,
      'expires' => true
    },
    uid: '123456790'
  }

  # Use color in STDOUT
  config.color = true
  config.fail_fast = false

  # Use color not only in STDOUT but also in pagers and files
  config.tty = true

  # Use the specified formatter
  #config.formatter = :documentation # :progress, :html, :textmate

  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  #config.fixture_path = "#{::Rails.root}/spec/fixtures"

  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples within a transaction, remove the following line or assign false
  # instead of true.
  #config.use_transactional_fixtures = true
  config.before :suite do
    Qwaiter::Couchbase.load_design_docs!
    # NOT THREADSAFE!!!!!! but good enough for testing since the real couchbase flush is slowwwwww....
    Qwaiter::Counter.connection = InMemoryQCounter.new
    # Threadsafe would be using the drb counter
    # require 'drb'
    # counter = DRbObject.new nil, 'druby://:9000'
    # Qwaiter::Counter.connection = counter
  end

  config.before :each do
    CouchPotato.couchrest_database.recreate!
    Qwaiter::Counter.connection.flush
  end

  config.before :each, type: :feature do
    #Supplier.any_instance.stub send_confirmation_instructions: true
    Capybara.session_name = :default
  end

  # If true, the base class of anonymous controllers will be inferred
  # automatically. This will be the default behavior in future versions of
  # rspec-rails.
  #config.infer_base_class_for_anonymous_controllers = true
  def sign_in_user_through_request
    visit "/users/sign_in"
    fill_in 'user[email]', with: @user.email
    fill_in 'user[password]', with: @user.password
    click_on 'Inloggen'
  end
end
