aboutsummaryrefslogtreecommitdiffstats
path: root/spec/spec_helper.rb.rails2
blob: 3be11c0c785e136dd465f5c19819af0abe1f0a27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# This file is copied to ~/spec when you run 'ruby script/generate rspec'
# from the project root directory.
ENV["RAILS_ENV"] = 'test'
require File.expand_path(File.join('..', '..', 'config', 'environment'), __FILE__)
require 'spec/autorun'
require 'spec/rails'

# set a default username and password so we can test
config = MySociety::Config.load_default()
config['ADMIN_USERNAME'] = 'foo'
config['ADMIN_PASSWORD'] = 'baz'

# tests assume 20 days
config['REPLY_LATE_AFTER_DAYS'] = 20

# register a fake Varnish server
require 'fakeweb'
FakeWeb.register_uri(:purge, %r|varnish.localdomain|, :body => "OK")

# Uncomment the next line to use webrat's matchers
#require 'webrat/integrations/rspec-rails'

# Use test-specific translations
FastGettext.add_text_domain 'app', :path => File.join(File.dirname(__FILE__), 'fixtures', 'locale'), :type => :po
FastGettext.default_text_domain = 'app'
Spec::Runner.configure do |config|
  # If you're not using ActiveRecord you should remove these
  # lines, delete config/database.yml and disable :active_record
  # in your config/boot.rb

  # fixture_path must end in a separator
  config.fixture_path = File.join(Rails.root, 'spec', 'fixtures') + File::SEPARATOR
  config.global_fixtures = :users,
                           :public_bodies,
                           :public_body_translations,
                           :public_body_versions,
                           :info_requests,
                           :raw_emails,
                           :incoming_messages,
                           :outgoing_messages,
                           :comments,
                           :info_request_events,
                           :track_things,
                           :foi_attachments,
                           :has_tag_string_tags,
                           :holidays,
                           :track_things_sent_emails

  # This section makes the garbage collector run less often to speed up tests
  last_gc_run = Time.now

  config.before(:each) do
    GC.disable
  end

  config.after(:each) do
    if Time.now - last_gc_run > 4
      GC.enable
      GC.start
      last_gc_run = Time.now
    end
  end

  # == Fixtures
  #
  # You can declare fixtures for each example_group like this:
  #   describe "...." do
  #     fixtures :table_a, :table_b
  #
  # Alternatively, if you prefer to declare them only once, you can
  # do so right here. Just uncomment the next line and replace the fixture
  # names with your fixtures.
  #
  # config.global_fixtures = :table_a, :table_b
  #
  # If you declare global fixtures, be aware that they will be declared
  # for all of your examples, even those that don't use them.
  #
  # You can also declare which fixtures to use (for example fixtures for test/fixtures):
  #
  # config.fixture_path = Rails.root + '/spec/fixtures/'
  #
  # == Mock Framework
  #
  # RSpec uses its own mocking framework by default. 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
  #
  # == Notes
  #
  # For more information take a look at Spec::Runner::Configuration and Spec::Runner
end

# XXX No idea what namespace/class/module to put this in
# Create a clean xapian index based on the fixture files and the raw_email data.
def create_fixtures_xapian_index
    load_raw_emails_data
    rebuild_xapian_index
end

# Copy the xapian index created in create_fixtures_xapian_index to a temporary
# copy at the same level and point xapian at the copy
def get_fixtures_xapian_index()
    # Create a base index for the fixtures if not already created
    $existing_xapian_db ||= create_fixtures_xapian_index
    # Store whatever the xapian db path is originally
    $original_xapian_path ||= ActsAsXapian.db_path
    path_array = $original_xapian_path.split(File::Separator)
    path_array.pop
    temp_path = File.join(path_array, 'test.temp')
    FileUtils.remove_entry_secure(temp_path, force=true)
    FileUtils.cp_r($original_xapian_path, temp_path)
    ActsAsXapian.db_path = temp_path
end

def basic_auth_login(request, username = nil, password = nil)
    username = Configuration::admin_username if username.nil?
    password = Configuration::admin_password if password.nil?
    request.env["HTTP_AUTHORIZATION"] = "Basic " + Base64::encode64("#{username}:#{password}")
end

# to_ary differs in Ruby 1.8 and 1.9
# @see http://yehudakatz.com/2010/01/02/the-craziest-fing-bug-ive-ever-seen/
def safe_mock_model(model, args = {})
  mock = mock_model(model, args)
  mock.should_receive(:to_ary).any_number_of_times
  mock
end

def load_test_categories
    PublicBodyCategories.add(:en, [
        "Local and regional",
            [ "local_council", "Local councils", "a local council" ],
        "Miscellaneous",
            [ "other", "Miscellaneous", "miscellaneous" ],])
end


# Monkeypatch applicationcontroller because the `render_to_string`
# method in the original breaks all the rspec test assertions such as
# `should render_template('foo')`.  Same problem as
# http://stackoverflow.com/questions/8174415/is-it-possible-to-assert-template-or-render-template-against-the-same-partial-wi
# - a bug in either Rails or Rspec I don't have the time to fix :(

class ApplicationController < ActionController::Base
    def set_popup_banner
        @popup_banner = nil
    end
end