aboutsummaryrefslogtreecommitdiffstats
path: root/spec
diff options
context:
space:
mode:
Diffstat (limited to 'spec')
-rw-r--r--spec/controllers/general_controller_spec.rb18
-rw-r--r--spec/controllers/public_body_controller_spec.rb44
-rw-r--r--spec/integration/cookie_stripping_spec.rb12
-rw-r--r--spec/lib/whatdotheyknow/strip_empty_sessions_spec.rb56
-rw-r--r--spec/spec_helper.rb8
5 files changed, 79 insertions, 59 deletions
diff --git a/spec/controllers/general_controller_spec.rb b/spec/controllers/general_controller_spec.rb
index 593d51683..116dbe07a 100644
--- a/spec/controllers/general_controller_spec.rb
+++ b/spec/controllers/general_controller_spec.rb
@@ -73,11 +73,10 @@ describe GeneralController, "when showing the frontpage" do
end
it "should render the front page with default language" do
- old_default_locale = I18n.default_locale
- I18n.default_locale = "es"
- get :frontpage
- response.should have_selector('html[lang="es"]')
- I18n.default_locale = old_default_locale
+ with_default_locale("es") do
+ get :frontpage
+ response.should have_selector('html[lang="es"]')
+ end
end
it "should render the front page with default language and ignore the browser setting" do
@@ -85,11 +84,10 @@ describe GeneralController, "when showing the frontpage" do
config['USE_DEFAULT_BROWSER_LANGUAGE'] = false
accept_language = "en-GB,en-US;q=0.8,en;q=0.6"
request.env['HTTP_ACCEPT_LANGUAGE'] = accept_language
- old_default_locale = I18n.default_locale
- I18n.default_locale = "es"
- get :frontpage
- response.should have_selector('html[lang="es"]')
- I18n.default_locale = old_default_locale
+ with_default_locale("es") do
+ get :frontpage
+ response.should have_selector('html[lang="es"]')
+ end
end
it "should render the front page with browser-selected language when there's no default set" do
diff --git a/spec/controllers/public_body_controller_spec.rb b/spec/controllers/public_body_controller_spec.rb
index 2663f2a75..63989baaa 100644
--- a/spec/controllers/public_body_controller_spec.rb
+++ b/spec/controllers/public_body_controller_spec.rb
@@ -82,21 +82,23 @@ describe PublicBodyController, "when listing bodies" do
def make_single_language_example(locale)
result = nil
- I18n.with_locale(locale) do
- case locale
- when :en
- result = PublicBody.new(:name => 'English only',
- :short_name => 'EO')
- when :es
- result = PublicBody.new(:name => 'Español Solamente',
- :short_name => 'ES')
- else
- raise StandardError.new "Unknown locale #{locale}"
+ with_default_locale(locale) do
+ I18n.with_locale(locale) do
+ case locale
+ when :en
+ result = PublicBody.new(:name => 'English only',
+ :short_name => 'EO')
+ when :es
+ result = PublicBody.new(:name => 'Español Solamente',
+ :short_name => 'ES')
+ else
+ raise StandardError.new "Unknown locale #{locale}"
+ end
+ result.request_email = "#{locale}@example.org"
+ result.last_edit_editor = 'test'
+ result.last_edit_comment = ''
+ result.save
end
- result.request_email = "#{locale}@example.org"
- result.last_edit_editor = 'test'
- result.last_edit_comment = ''
- result.save
end
result
end
@@ -188,13 +190,13 @@ describe PublicBodyController, "when listing bodies" do
end
it "should list bodies in alphabetical order with different locale" do
- I18n.default_locale = :es
- get :list
- response.should render_template('list')
- assigns[:public_bodies].should == [ public_bodies(:geraldine_public_body), public_bodies(:humpadink_public_body) ]
- assigns[:tag].should == "all"
- assigns[:description].should == ""
- I18n.default_locale = :en
+ with_default_locale(:es) do
+ get :list
+ response.should render_template('list')
+ assigns[:public_bodies].should == [ public_bodies(:geraldine_public_body), public_bodies(:humpadink_public_body) ]
+ assigns[:tag].should == "all"
+ assigns[:description].should == ""
+ end
end
it "should list a tagged thing on the appropriate list page, and others on the other page, and all still on the all page" do
diff --git a/spec/integration/cookie_stripping_spec.rb b/spec/integration/cookie_stripping_spec.rb
new file mode 100644
index 000000000..897899fd5
--- /dev/null
+++ b/spec/integration/cookie_stripping_spec.rb
@@ -0,0 +1,12 @@
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+require File.expand_path(File.dirname(__FILE__) + '/alaveteli_dsl')
+
+describe 'when making stripping cookies' do
+
+ it 'should not set a cookie when no significant session data is set' do
+ get 'country_message'
+ response.headers['Set-Cookie'].should be_blank
+ end
+
+end
+
diff --git a/spec/lib/whatdotheyknow/strip_empty_sessions_spec.rb b/spec/lib/whatdotheyknow/strip_empty_sessions_spec.rb
index 9bd5ccb93..fcd729b48 100644
--- a/spec/lib/whatdotheyknow/strip_empty_sessions_spec.rb
+++ b/spec/lib/whatdotheyknow/strip_empty_sessions_spec.rb
@@ -1,71 +1,71 @@
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
describe WhatDoTheyKnow::StripEmptySessions do
-
+
def make_response(session_data, response_headers)
app = lambda do |env|
env['rack.session'] = session_data
- return [200, response_headers, ['content']]
+ return [200, response_headers, ['content']]
end
strip_empty_sessions = WhatDoTheyKnow::StripEmptySessions
app = strip_empty_sessions.new(app, {:key => 'mykey', :path => '', :httponly => true})
response = Rack::MockRequest.new(app).get('/', 'HTTP_ACCEPT' => 'text/html')
end
-
- it 'should not prevent a cookie being set if there is data in the session' do
- session_data = { :some_real_data => 'important',
- :session_id => 'my_session_id',
- :_csrf_token => 'hi_there' }
- application_response_headers = { 'Content-Type' => 'text/html',
+
+ it 'should not prevent a cookie being set if there is data in the session' do
+ session_data = { 'some_real_data' => 'important',
+ 'session_id' => 'my_session_id',
+ '_csrf_token' => 'hi_there' }
+ application_response_headers = { 'Content-Type' => 'text/html',
'Set-Cookie' => 'mykey=f274c61a35320c52d45e9f8d7d4e2649; path=/; HttpOnly'}
response = make_response(session_data, application_response_headers)
response.headers['Set-Cookie'].should == 'mykey=f274c61a35320c52d45e9f8d7d4e2649; path=/; HttpOnly'
end
- describe 'if there is no meaningful data in the session' do
+ describe 'if there is no meaningful data in the session' do
- before do
- @session_data = { :session_id => 'my_session_id',
- :_csrf_token => 'hi_there' }
+ before do
+ @session_data = { 'session_id' => 'my_session_id',
+ '_csrf_token' => 'hi_there' }
end
-
- it 'should not strip any other header' do
+
+ it 'should not strip any other header' do
application_response_headers = { 'Content-Type' => 'text/html',
'Set-Cookie' => 'mykey=f274c61a35320c52d45e9f8d7d4e2649; path=/; HttpOnly'}
response = make_response(@session_data, application_response_headers)
response.headers['Content-Type'].should == 'text/html'
end
-
- it 'should strip the session cookie setting header ' do
- application_response_headers = { 'Content-Type' => 'text/html',
+
+ it 'should strip the session cookie setting header ' do
+ application_response_headers = { 'Content-Type' => 'text/html',
'Set-Cookie' => 'mykey=f274c61a35320c52d45e9f8d7d4e2649; path=/; HttpOnly'}
response = make_response(@session_data, application_response_headers)
response.headers['Set-Cookie'].should == ""
end
-
- it 'should strip the session cookie setting header even with a locale' do
- @session_data[:locale] = 'en'
- application_response_headers = { 'Content-Type' => 'text/html',
+
+ it 'should strip the session cookie setting header even with a locale' do
+ @session_data['locale'] = 'en'
+ application_response_headers = { 'Content-Type' => 'text/html',
'Set-Cookie' => 'mykey=f274c61a35320c52d45e9f8d7d4e2649; path=/; HttpOnly'}
response = make_response(@session_data, application_response_headers)
response.headers['Set-Cookie'].should == ""
end
- it 'should not strip the session cookie setting for admins' do
- @session_data[:using_admin] = 1
- application_response_headers = { 'Content-Type' => 'text/html',
+ it 'should not strip the session cookie setting for admins' do
+ @session_data['using_admin'] = 1
+ application_response_headers = { 'Content-Type' => 'text/html',
'Set-Cookie' => 'mykey=f274c61a35320c52d45e9f8d7d4e2649; path=/; HttpOnly'}
response = make_response(@session_data, application_response_headers)
response.headers['Set-Cookie'].should == "mykey=f274c61a35320c52d45e9f8d7d4e2649; path=/; HttpOnly"
end
-
- it 'should strip the session cookie setting header (but no other cookie setting header) if there is more than one' do
- application_response_headers = { 'Content-Type' => 'text/html',
+
+ it 'should strip the session cookie setting header (but no other cookie setting header) if there is more than one' do
+ application_response_headers = { 'Content-Type' => 'text/html',
'Set-Cookie' => ['mykey=f274c61a35320c52d45e9f8d7d4e2649; path=/; HttpOnly',
'other=mydata']}
response = make_response(@session_data, application_response_headers)
response.headers['Set-Cookie'].should == ['other=mydata']
end
-
+
end
end
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index 9d16f6387..d22f3c0ff 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -183,6 +183,14 @@ Spork.prefork do
end
end
+ def with_default_locale(locale)
+ original_default_locale = I18n.default_locale
+ I18n.default_locale = locale
+ yield
+ ensure
+ I18n.default_locale = original_default_locale
+ end
+
def load_test_categories
PublicBodyCategories.add(:en, [
"Local and regional",