aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--app/controllers/admin_general_controller.rb7
-rw-r--r--app/controllers/admin_request_controller.rb17
-rw-r--r--app/controllers/application_controller.rb12
-rw-r--r--app/controllers/request_controller.rb2
-rw-r--r--app/models/incoming_message.rb3
-rw-r--r--app/models/info_request.rb52
-rw-r--r--app/models/raw_email.rb2
-rw-r--r--app/models/request_mailer.rb5
-rw-r--r--app/models/user.rb3
-rw-r--r--app/views/admin_request/show_raw_email.rhtml5
-rw-r--r--app/views/public_body/show.rhtml4
-rw-r--r--app/views/request/_describe_state.rhtml2
-rw-r--r--app/views/request/_followup.rhtml3
-rw-r--r--app/views/request/new.rhtml2
-rw-r--r--config/environment.rb1
-rw-r--r--config/general.yml-example1
-rw-r--r--db/migrate/101_add_hash_to_info_request.rb22
-rw-r--r--doc/INSTALL.md2
-rwxr-xr-xscript/load-sample-data12
-rw-r--r--spec/controllers/admin_general_controller_spec.rb8
-rw-r--r--spec/controllers/admin_public_body_controller_spec.rb12
-rw-r--r--spec/controllers/admin_request_controller_spec.rb55
-rw-r--r--spec/controllers/general_controller_spec.rb42
-rw-r--r--spec/controllers/public_body_controller_spec.rb4
-rw-r--r--spec/fixtures/info_requests.yml6
-rw-r--r--spec/models/info_request_spec.rb34
-rw-r--r--spec/models/request_mailer_spec.rb44
-rw-r--r--spec/models/user_spec.rb18
28 files changed, 325 insertions, 55 deletions
diff --git a/app/controllers/admin_general_controller.rb b/app/controllers/admin_general_controller.rb
index 5073cdc5b..ae51e0923 100644
--- a/app/controllers/admin_general_controller.rb
+++ b/app/controllers/admin_general_controller.rb
@@ -8,6 +8,13 @@
class AdminGeneralController < AdminController
def index
+ # ensure we have a trailing slash
+ current_uri = request.env['REQUEST_URI']
+ if params[:suppress_redirect].nil? && !(current_uri =~ /\/$/)
+ redirect_to admin_general_index_url + "/"
+ return
+ end
+
# Overview counts of things
@public_body_count = PublicBody.count
diff --git a/app/controllers/admin_request_controller.rb b/app/controllers/admin_request_controller.rb
index d5bd4c4d6..b3fb1ce40 100644
--- a/app/controllers/admin_request_controller.rb
+++ b/app/controllers/admin_request_controller.rb
@@ -177,7 +177,7 @@ class AdminRequestController < AdminController
raw_email_data = incoming_message.raw_email.data
mail = TMail::Mail.parse(raw_email_data)
mail.base64_decode
- destination_request.receive(mail, raw_email_data)
+ destination_request.receive(mail, raw_email_data, true)
incoming_message_id = incoming_message.id
incoming_message.fully_destroy
@@ -275,7 +275,6 @@ class AdminRequestController < AdminController
def show_raw_email
@raw_email = RawEmail.find(params[:id])
-
# For the holding pen, try to guess where it should be ...
@holding_pen = false
if (@raw_email.incoming_message.info_request == InfoRequest.holding_pen_request && !@raw_email.incoming_message.mail.from_addrs.nil? && @raw_email.incoming_message.mail.from_addrs.size > 0)
@@ -294,15 +293,11 @@ class AdminRequestController < AdminController
end
# 2. Match the email address in the message without matching the hash
- @info_requests = []
- addresses =
- (@raw_email.incoming_message.mail.to || []) +
- (@raw_email.incoming_message.mail.cc || []) +
- (@raw_email.incoming_message.mail.envelope_to || [])
- addresses.uniq!
- for address in addresses
- @info_requests += InfoRequest.guess_by_incoming_email(address)
- end
+ @info_requests = InfoRequest.guess_by_incoming_email(@raw_email.incoming_message)
+
+ # 3. Give a reason why it's in the holding pen
+ last_event = @raw_email.incoming_message.info_request.get_last_event
+ @rejected_reason = last_event.params[:rejected_reason]
end
end
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index 5f18be2e5..0d8c83d6c 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -42,7 +42,11 @@ class ApplicationController < ActionController::Base
end
def set_gettext_locale
- requested_locale = params[:locale] || session[:locale] || cookies[:locale] || request.env['HTTP_ACCEPT_LANGUAGE']
+ if MySociety::Config.get('USE_DEFAULT_BROWSER_LANGUAGE', true)
+ requested_locale = params[:locale] || session[:locale] || cookies[:locale] || request.env['HTTP_ACCEPT_LANGUAGE'] || I18n.default_locale
+ else
+ requested_locale = params[:locale] || session[:locale] || cookies[:locale] || I18n.default_locale
+ end
session[:locale] = FastGettext.set_locale(requested_locale)
end
@@ -221,7 +225,11 @@ class ApplicationController < ActionController::Base
if session[:user_id].nil?
return nil
else
- return User.find(session[:user_id])
+ begin
+ return User.find(session[:user_id])
+ rescue ActiveRecord::RecordNotFound
+ return nil
+ end
end
end
diff --git a/app/controllers/request_controller.rb b/app/controllers/request_controller.rb
index 7b9421464..e446854ab 100644
--- a/app/controllers/request_controller.rb
+++ b/app/controllers/request_controller.rb
@@ -375,7 +375,7 @@ class RequestController < ApplicationController
# Don't give advice on what to do next, as it isn't their request
RequestMailer.deliver_old_unclassified_updated(@info_request)
if session[:request_game]
- flash[:notice] = _('Thank you for updating the status of the request \'<a href="%s">{{info_request_title}}</a>\'. There are some more requests below for you to classify.',:info_request_title=>CGI.escapeHTML(@info_request.title)) % [CGI.escapeHTML(request_url(@info_request))]
+ flash[:notice] = _('Thank you for updating the status of the request \'<a href="{{url}}">{{info_request_title}}</a>\'. There are some more requests below for you to classify.',:info_request_title=>CGI.escapeHTML(@info_request.title), :url=>CGI.escapeHTML(request_url(@info_request)))
redirect_to play_url
else
flash[:notice] = _('Thank you for updating this request!')
diff --git a/app/models/incoming_message.rb b/app/models/incoming_message.rb
index 581c73f8b..7d9cfbfa1 100644
--- a/app/models/incoming_message.rb
+++ b/app/models/incoming_message.rb
@@ -1237,9 +1237,8 @@ class IncomingMessage < ActiveRecord::Base
info_request_event.track_things_sent_emails.each { |a| a.destroy }
info_request_event.user_info_request_sent_alerts.each { |a| a.destroy }
info_request_event.destroy
- raw_email = self.raw_email
+ self.raw_email.destroy_file_representation!
self.destroy
- self.raw_email.destroy
end
end
diff --git a/app/models/info_request.rb b/app/models/info_request.rb
index 209954b16..419546c99 100644
--- a/app/models/info_request.rb
+++ b/app/models/info_request.rb
@@ -17,7 +17,6 @@
# allow_new_responses_from :string(255) default("anybody"), not null
# handle_rejected_responses :string(255) default("bounce"), not null
#
-
# models/info_request.rb:
# A Freedom of Information request.
#
@@ -309,13 +308,20 @@ public
# Return list of info requests which *might* be right given email address
# e.g. For the id-hash email addresses, don't match the hash.
- def InfoRequest.guess_by_incoming_email(incoming_email)
- id, hash = InfoRequest._extract_id_hash_from_email(incoming_email)
- begin
- return [InfoRequest.find(id)]
- rescue ActiveRecord::RecordNotFound
- return []
+ def InfoRequest.guess_by_incoming_email(incoming_message)
+ guesses = []
+ # 1. Try to guess based on the email address(es)
+ addresses =
+ (incoming_message.mail.to || []) +
+ (incoming_message.mail.cc || []) +
+ (incoming_message.mail.envelope_to || [])
+ addresses.uniq!
+ for address in addresses
+ id, hash = InfoRequest._extract_id_hash_from_email(address)
+ guesses.push(InfoRequest.find_by_id(id))
+ guesses.push(InfoRequest.find_by_idhash(hash))
end
+ return guesses.select{|x| !x.nil?}.uniq
end
# Internal function used by find_by_magic_email and guess_by_incoming_email
@@ -326,7 +332,7 @@ public
# The optional bounce- dates from when we used to have separate emails for the envelope from.
# (that was abandoned because councils would send hand written responses to them, not just
# bounce messages)
- incoming_email =~ /request-(?:bounce-)?(\d+)-([a-z0-9]+)/
+ incoming_email =~ /request-(?:bounce-)?([a-z0-9]+)-([a-z0-9]+)/
id = $1.to_i
hash = $2
@@ -379,21 +385,24 @@ public
end
# A new incoming email to this request
- def receive(email, raw_email_data, override_stop_new_responses = false)
+ def receive(email, raw_email_data, override_stop_new_responses = false, rejected_reason = "")
if !override_stop_new_responses
allow = nil
-
+ reason = nil
# See if new responses are prevented for spam reasons
if self.allow_new_responses_from == 'nobody'
allow = false
+ reason = _('This request has been set by an administrator to "allow new responses from nobody"')
elsif self.allow_new_responses_from == 'anybody'
allow = true
elsif self.allow_new_responses_from == 'authority_only'
if email.from_addrs.nil? || email.from_addrs.size == 0
allow = false
+ reason = _('Only the authority can reply to this request, but there is no "From" address to check against')
else
sender_email = email.from_addrs[0].spec
sender_domain = PublicBody.extract_domain_from_email(sender_email)
+ reason = _("Only the authority can reply to this request, and I don't recognise the address this reply was sent from")
allow = false
# Allow any domain that has already sent reply
for row in self.who_can_followup_to
@@ -411,7 +420,7 @@ public
if self.handle_rejected_responses == 'bounce'
RequestMailer.deliver_stopped_responses(self, email, raw_email_data)
elsif self.handle_rejected_responses == 'holding_pen'
- InfoRequest.holding_pen_request.receive(email, raw_email_data)
+ InfoRequest.holding_pen_request.receive(email, raw_email_data, false, reason)
elsif self.handle_rejected_responses == 'blackhole'
# do nothing - just lose the message (Note: a copy will be
# in the backup mailbox if the server is configured to send
@@ -435,7 +444,11 @@ public
raw_email.save!
self.awaiting_description = true
- self.log_event("response", { :incoming_message_id => incoming_message.id })
+ params = { :incoming_message_id => incoming_message.id }
+ if !rejected_reason.empty?
+ params[:rejected_reason] = rejected_reason
+ end
+ self.log_event("response", params)
self.save!
end
@@ -671,6 +684,7 @@ public
end
end
return nil
+
end
def get_last_response_event
for e in self.info_request_events.reverse
@@ -831,15 +845,25 @@ public
def InfoRequest.magic_email_for_id(prefix_part, id)
magic_email = MySociety::Config.get("INCOMING_EMAIL_PREFIX", "")
magic_email += prefix_part + id.to_s
- magic_email += "-" + Digest::SHA1.hexdigest(id.to_s + MySociety::Config.get("INCOMING_EMAIL_SECRET", 'dummysecret'))[0,8]
+ magic_email += "-" + InfoRequest.hash_from_id(id)
magic_email += "@" + MySociety::Config.get("INCOMING_EMAIL_DOMAIN", "localhost")
return magic_email
end
+ before_validation :compute_idhash
+
+ def compute_idhash
+ self.idhash = InfoRequest.hash_from_id(self.id)
+ end
+
+ def InfoRequest.hash_from_id(id)
+ return Digest::SHA1.hexdigest(id.to_s + MySociety::Config.get("INCOMING_EMAIL_SECRET", 'dummysecret'))[0,8]
+ end
+
# Called by find_by_incoming_email - and used to be called by separate
# function for envelope from address, until we abandoned it.
def InfoRequest.find_by_magic_email(id, hash)
- expected_hash = Digest::SHA1.hexdigest(id.to_s + MySociety::Config.get("INCOMING_EMAIL_SECRET", 'dummysecret'))[0,8]
+ expected_hash = InfoRequest.hash_from_id(id)
#print "expected: " + expected_hash + "\nhash: " + hash + "\n"
if hash != expected_hash
return nil
diff --git a/app/models/raw_email.rb b/app/models/raw_email.rb
index eb36053c1..c6066cbf4 100644
--- a/app/models/raw_email.rb
+++ b/app/models/raw_email.rb
@@ -20,8 +20,6 @@ class RawEmail < ActiveRecord::Base
has_one :incoming_message
- before_destroy :destroy_file_representation!
-
# We keep the old data_text field (which is of type text) for backwards
# compatibility. We use the new data_binary field because only it works
# properly in recent versions of PostgreSQL (get seg faults escaping
diff --git a/app/models/request_mailer.rb b/app/models/request_mailer.rb
index e73b153b9..fc317d20d 100644
--- a/app/models/request_mailer.rb
+++ b/app/models/request_mailer.rb
@@ -205,10 +205,11 @@ class RequestMailer < ApplicationMailer
def receive(email, raw_email)
# Find which info requests the email is for
reply_info_requests = self.requests_matching_email(email)
-
# Nothing found, so save in holding pen
if reply_info_requests.size == 0
- InfoRequest.holding_pen_request.receive(email, raw_email)
+ reason = _("Could not identify the request from the email address")
+ request = InfoRequest.holding_pen_request
+ request.receive(email, raw_email, false, reason)
return
end
diff --git a/app/models/user.rb b/app/models/user.rb
index e29ae3101..fddb6b035 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -118,6 +118,9 @@ class User < ActiveRecord::Base
if not name.nil?
name.strip!
end
+ if self.public_banned?
+ name = _("{{user_name}} (Banned)", :user_name=>name)
+ end
name
end
diff --git a/app/views/admin_request/show_raw_email.rhtml b/app/views/admin_request/show_raw_email.rhtml
index 10b97f4fd..fa1470e77 100644
--- a/app/views/admin_request/show_raw_email.rhtml
+++ b/app/views/admin_request/show_raw_email.rhtml
@@ -3,8 +3,9 @@
<h1>Incoming message <%=@raw_email.incoming_message.id.to_s %></h1>
<p>
- FOI request: <%= link_to request_both_links(@raw_email.incoming_message.info_request) %>
+ FOI request: <%= request_both_links(@raw_email.incoming_message.info_request) %>
<% if @holding_pen %>
+ <br>This is in the holding pen because: <strong><%= @rejected_reason %></strong>
<% if @public_bodies.size > 0 %>
<br>Guessed authority:
<% for public_body in @public_bodies %>
@@ -30,7 +31,7 @@
<h2>Raw email</h2>
-<%= link_to "Download", "../download_raw_email/" + @raw_email.id.to_s %>
+<p><%= link_to "Download", "../download_raw_email/" + @raw_email.id.to_s %></p>
<pre><%=h(@raw_email.data).gsub(/\n/, '<br>') %></pre>
diff --git a/app/views/public_body/show.rhtml b/app/views/public_body/show.rhtml
index dce7ca4f8..3d325e2b8 100644
--- a/app/views/public_body/show.rhtml
+++ b/app/views/public_body/show.rhtml
@@ -47,8 +47,8 @@
<%= link_to _("Make a new Environmental Information request"), new_request_to_body_url(:url_name => @public_body.url_name)%> to <%= h(@public_body.name) %>
<% else %>
- <%= _('<a href="%s">Make a new Freedom of Information request</a> to {{public_body_name}}',
- :public_body_name => h(@public_body.name)) % new_request_to_body_url(:url_name => @public_body.url_name)%>
+ <%= _('<a href="{{url}}">Make a new Freedom of Information request</a> to {{public_body_name}}',
+ :public_body_name => h(@public_body.name), :url=>new_request_to_body_url(:url_name => @public_body.url_name))%>
<% end %>
<% elsif @public_body.has_notes? %>
<%= @public_body.notes_as_html %>
diff --git a/app/views/request/_describe_state.rhtml b/app/views/request/_describe_state.rhtml
index 8164637bf..052833a67 100644
--- a/app/views/request/_describe_state.rhtml
+++ b/app/views/request/_describe_state.rhtml
@@ -107,6 +107,6 @@
<%= _('We don\'t know whether the most recent response to this request contains
information or not
&ndash;
- if you are {{user_link}} please <a href="%s">sign in</a> and let everyone know.',:user_link=>user_link(@info_request.user)) % [signin_url(:r => request.request_uri)] %>
+ if you are {{user_link}} please <a href="{{url}}">sign in</a> and let everyone know.',:user_link=>user_link(@info_request.user), :url=>signin_url(:r => request.request_uri)) %>
<% end %>
diff --git a/app/views/request/_followup.rhtml b/app/views/request/_followup.rhtml
index 78de7decd..a15f2912d 100644
--- a/app/views/request/_followup.rhtml
+++ b/app/views/request/_followup.rhtml
@@ -13,8 +13,7 @@
<% end %>
<% if @info_request.allow_new_responses_from == 'nobody' %>
- <p><%= _('Follow ups and new responses to this request have been stopped to prevent spam. Please
- <a href="%s">contact us</a> if you are {{user_link}} and need to send a follow up.',:user_link=>user_link(@info_request.user) ) % [help_contact_path] %></p>
+ <p><%= _('Follow ups and new responses to this request have been stopped to prevent spam. Please <a href="{{url}}">contact us</a> if you are {{user_link}} and need to send a follow up.',:user_link=>user_link(@info_request.user), :url=>help_contact_path) %></p>
<% else %>
<% if @internal_review %>
<p>
diff --git a/app/views/request/new.rhtml b/app/views/request/new.rhtml
index a97116aa3..b8625a8e6 100644
--- a/app/views/request/new.rhtml
+++ b/app/views/request/new.rhtml
@@ -38,7 +38,7 @@
<li>
<% if @info_request.public_body.info_requests.size > 0 %>
- <%= _("Browse <a href='%s'>other requests</a> to '{{public_body_name}}' for examples of how to word your request.", :public_body_name=>h(@info_request.public_body.name)) % [public_body_url(@info_request.public_body)] %>
+ <%= _("Browse <a href='{{url}}'>other requests</a> to '{{public_body_name}}' for examples of how to word your request.", :public_body_name=>h(@info_request.public_body.name), :url=>public_body_url(@info_request.public_body)) %>
<% else %>
<%= _('Browse <a href="%s">other requests</a> for examples of how to word your request.') % [request_list_url] %>
<% end %>
diff --git a/config/environment.rb b/config/environment.rb
index a40c2df4e..17a16efed 100644
--- a/config/environment.rb
+++ b/config/environment.rb
@@ -122,6 +122,7 @@ else
end
FastGettext.default_available_locales = available_locales
+I18n.locale = default_locale
I18n.available_locales = available_locales
I18n.default_locale = default_locale
diff --git a/config/general.yml-example b/config/general.yml-example
index 9db95c4d0..729278a8d 100644
--- a/config/general.yml-example
+++ b/config/general.yml-example
@@ -19,6 +19,7 @@ TWITTER_USERNAME: 'whatdotheyknow'
# Locales we wish to support in this app, space-delimited
AVAILABLE_LOCALES: 'en es'
DEFAULT_LOCALE: 'en'
+USE_DEFAULT_BROWSER_LANGUAGE: true
# How many days should have passed before an answer to a request is officially late?
REPLY_LATE_AFTER_DAYS: 20
diff --git a/db/migrate/101_add_hash_to_info_request.rb b/db/migrate/101_add_hash_to_info_request.rb
new file mode 100644
index 000000000..20608aac1
--- /dev/null
+++ b/db/migrate/101_add_hash_to_info_request.rb
@@ -0,0 +1,22 @@
+require 'digest/sha1'
+
+class AddHashToInfoRequest < ActiveRecord::Migration
+ def self.up
+ add_column :info_requests, :idhash, :string
+
+ # Create the missing events for requests already sent
+ InfoRequest.find(:all).each do |info_request|
+ info_request.idhash = Digest::SHA1.hexdigest(info_request.id.to_s + MySociety::Config.get("INCOMING_EMAIL_SECRET", 'dummysecret'))[0,8]
+ info_request.save!
+ puts info_request.idhash
+ end
+ change_column :info_requests, :idhash, :string, :null => false
+ puts InfoRequest.find_by_idhash
+ end
+ def self.down
+ remove_column :info_requests, :idhash
+ end
+end
+
+
+
diff --git a/doc/INSTALL.md b/doc/INSTALL.md
index e6f7caec7..eb0a77dd9 100644
--- a/doc/INSTALL.md
+++ b/doc/INSTALL.md
@@ -110,7 +110,7 @@ If you want some dummy data to play with, you can try loading the
fixtures that the test suite uses into your development database. You
can do this with:
- rake spec:db:fixtures:load
+ ./script/load-sample-data
Next we need to create the index for the search engine (Xapian):
diff --git a/script/load-sample-data b/script/load-sample-data
new file mode 100755
index 000000000..0521ef849
--- /dev/null
+++ b/script/load-sample-data
@@ -0,0 +1,12 @@
+#!/bin/bash
+
+# This script loads spec fixtures, but also needs to do a hack around
+# the fact that the fixtures aren't aware of the fact that RawEmails
+# have a filesystem representation of their contents
+
+LOC=`dirname $0`
+rake --silent spec:db:fixtures:load
+"$LOC/runner" 'puts File.expand_path(File.dirname(__FILE__) + "/spec/spec_helper"); require File.expand_path(File.dirname(__FILE__) + "/spec/spec_helper"); RawEmail.all().each {|email| email.data = load_file_fixture("useless_raw_email.email")}'
+
+
+echo "Loaded fixtures." \ No newline at end of file
diff --git a/spec/controllers/admin_general_controller_spec.rb b/spec/controllers/admin_general_controller_spec.rb
index 4c3708268..820d1e7f3 100644
--- a/spec/controllers/admin_general_controller_spec.rb
+++ b/spec/controllers/admin_general_controller_spec.rb
@@ -5,8 +5,14 @@ describe AdminGeneralController, "when viewing front page of admin interface" do
before { basic_auth_login @request }
it "should render the front page" do
- get :index
+ get :index, :suppress_redirect => 1
response.should render_template('index')
end
+ it "should redirect to include trailing slash" do
+ get :index
+ response.should redirect_to(:controller => 'admin_general',
+ :action => 'index')
+ end
+
end
diff --git a/spec/controllers/admin_public_body_controller_spec.rb b/spec/controllers/admin_public_body_controller_spec.rb
index 6b88fe39d..357564211 100644
--- a/spec/controllers/admin_public_body_controller_spec.rb
+++ b/spec/controllers/admin_public_body_controller_spec.rb
@@ -110,27 +110,27 @@ describe AdminPublicBodyController, "when administering public bodies with i18n"
end
it "creates a new public body" do
- I18n.locale = :es
+ I18n.default_locale = :es
PublicBody.count.should == 2
post :create, { :public_body => { :name => "New Quango", :short_name => "", :tag_string => "blah", :request_email => 'newquango@localhost', :last_edit_comment => 'From test code' } }
PublicBody.count.should == 3
- I18n.locale = :en
+ I18n.default_locale = :en
end
it "edits a public body" do
- I18n.locale = :es
+ I18n.default_locale = :es
get :edit, {:id => 3, :locale => :es}
response.body.should include('Baguette')
- I18n.locale = :en
+ I18n.default_locale = :en
end
it "saves edits to a public body" do
- I18n.locale = :es
+ I18n.default_locale = :es
pb = PublicBody.find(id=3)
pb.name.should == "El Department for Humpadinking"
post :update, { :id => 3, :public_body => { :name => "Renamed", :short_name => "", :tag_string => "some tags", :request_email => 'edited@localhost', :last_edit_comment => 'From test code' }}
response.flash[:notice].should include('successful')
- I18n.locale = :en
+ I18n.default_locale = :en
pb = PublicBody.find(public_bodies(:humpadink_public_body).id)
PublicBody.with_locale(:es) do
diff --git a/spec/controllers/admin_request_controller_spec.rb b/spec/controllers/admin_request_controller_spec.rb
index d82e4a49c..1d9ebcdf3 100644
--- a/spec/controllers/admin_request_controller_spec.rb
+++ b/spec/controllers/admin_request_controller_spec.rb
@@ -39,3 +39,58 @@ describe AdminRequestController, "when administering requests" do
end
+describe AdminRequestController, "when administering the holding pen" do
+ integrate_views
+ fixtures :info_requests, :incoming_messages, :raw_emails, :users, :public_bodies, :public_body_translations
+ before(:each) do
+ basic_auth_login @request
+ load_raw_emails_data(raw_emails)
+ end
+
+ it "shows a rejection reason for an incoming message from an invalid address" do
+ ir = info_requests(:fancy_dog_request)
+ ir.allow_new_responses_from = 'authority_only'
+ ir.handle_rejected_responses = 'holding_pen'
+ ir.save!
+ receive_incoming_mail('incoming-request-plain.email', ir.incoming_email, "frob@nowhere.com")
+ get :show_raw_email, :id => InfoRequest.holding_pen_request.get_last_response.raw_email.id
+ response.should have_text(/Only the authority can reply to this request/)
+ end
+
+ it "allows redelivery even to a closed request" do
+ ir = info_requests(:fancy_dog_request)
+ ir.allow_new_responses_from = 'nobody'
+ ir.handle_rejected_responses = 'holding_pen'
+ ir.save!
+ InfoRequest.holding_pen_request.incoming_messages.length.should == 0
+ ir.incoming_messages.length.should == 1
+ receive_incoming_mail('incoming-request-plain.email', ir.incoming_email, "frob@nowhere.com")
+ InfoRequest.holding_pen_request.incoming_messages.length.should == 1
+ new_im = InfoRequest.holding_pen_request.incoming_messages[0]
+ ir.incoming_messages.length.should == 1
+ post :redeliver_incoming, :redeliver_incoming_message_id => new_im.id, :url_title => ir.url_title
+ ir = InfoRequest.find_by_url_title(ir.url_title)
+ ir.incoming_messages.length.should == 2
+ response.should redirect_to('http://test.host/admin/request/show/101')
+ InfoRequest.holding_pen_request.incoming_messages.length.should == 0
+ end
+
+ it "guesses a misdirected request" do
+ ir = info_requests(:fancy_dog_request)
+ ir.handle_rejected_responses = 'holding_pen'
+ ir.save!
+ mail_to = "request-#{ir.id}-asdfg@example.com"
+ receive_incoming_mail('incoming-request-plain.email', mail_to)
+ get :show_raw_email, :id => InfoRequest.holding_pen_request.get_last_response.raw_email.id
+ response.should have_text(/Could not identify the request/)
+ assigns[:info_requests][0].should == ir
+ end
+
+ it "destroys an incoming message" do
+ im = incoming_messages(:useless_incoming_message)
+ raw_email = im.raw_email.filepath
+ post :destroy_incoming, :incoming_message_id => im.id
+ assert_equal File.exists?(raw_email), false
+ end
+
+end
diff --git a/spec/controllers/general_controller_spec.rb b/spec/controllers/general_controller_spec.rb
index 7807a5541..bc744a9cd 100644
--- a/spec/controllers/general_controller_spec.rb
+++ b/spec/controllers/general_controller_spec.rb
@@ -17,6 +17,48 @@ describe GeneralController, "when searching" do
response.should be_success
end
+ it "should render the front page with default language" do
+ get :frontpage
+ response.should have_tag('html[lang="en"]')
+ 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_tag('html[lang="es"]')
+ I18n.default_locale = old_default_locale
+ end
+
+ it "should render the front page with default language and ignore the browser setting" do
+ config = MySociety::Config.load_default()
+ 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_tag('html[lang="es"]')
+ I18n.default_locale = old_default_locale
+ end
+
+ it "should render the front page with browser-selected language when there's no default set" do
+ config = MySociety::Config.load_default()
+ config['USE_DEFAULT_BROWSER_LANGUAGE'] = true
+ accept_language = "es-ES,en-GB,en-US;q=0.8,en;q=0.6"
+ request.env['HTTP_ACCEPT_LANGUAGE'] = accept_language
+ get :frontpage
+ response.should have_tag('html[lang="es"]')
+ request.env['HTTP_ACCEPT_LANGUAGE'] = nil
+ end
+
+ it "doesn't raise an error when there's no user matching the one in the session" do
+ session[:user_id] = 999
+ get :frontpage
+ response.should be_success
+ end
+
+
it "should redirect from search query URL to pretty URL" do
post :search_redirect, :query => "mouse" # query hidden in POST parameters
response.should redirect_to(:action => 'search', :combined => "mouse") # URL /search/:query
diff --git a/spec/controllers/public_body_controller_spec.rb b/spec/controllers/public_body_controller_spec.rb
index d15482e51..0050678d2 100644
--- a/spec/controllers/public_body_controller_spec.rb
+++ b/spec/controllers/public_body_controller_spec.rb
@@ -72,13 +72,13 @@ describe PublicBodyController, "when listing bodies" do
end
it "should list bodies in alphabetical order with different locale" do
- I18n.locale = :es
+ 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 == "all"
- I18n.locale = :en
+ I18n.default_locale = :en
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/fixtures/info_requests.yml b/spec/fixtures/info_requests.yml
index bdeef4800..c1e3c1910 100644
--- a/spec/fixtures/info_requests.yml
+++ b/spec/fixtures/info_requests.yml
@@ -8,6 +8,7 @@ fancy_dog_request:
user_id: 1
described_state: waiting_response
awaiting_description: true
+ idhash: 50929748
naughty_chicken_request:
id: 103
title: How much public money is wasted on breeding naughty chickens?
@@ -17,6 +18,5 @@ naughty_chicken_request:
public_body_id: 2
user_id: 1
described_state: waiting_response
- awaiting_description: false
-
-
+ awaiting_description: false
+ idhash: e8d18c84
diff --git a/spec/models/info_request_spec.rb b/spec/models/info_request_spec.rb
index d0b0e0e32..ba80256ab 100644
--- a/spec/models/info_request_spec.rb
+++ b/spec/models/info_request_spec.rb
@@ -2,6 +2,40 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe InfoRequest do
+ describe "guessing a request from an email" do
+ fixtures :info_requests, :public_bodies, :incoming_messages, :raw_emails
+
+ before do
+ @im = incoming_messages(:useless_incoming_message)
+ load_raw_emails_data(raw_emails)
+ end
+
+ it 'should compute a hash' do
+ @info_request = InfoRequest.new(:title => "testing",
+ :public_body => public_bodies(:geraldine_public_body),
+ :user_id => 1)
+ @info_request.save!
+ @info_request.idhash.should_not == nil
+ end
+
+ it 'should find a request based on an email with an intact id and a broken hash' do
+ ir = info_requests(:fancy_dog_request)
+ id = ir.id
+ @im.mail.to = "request-#{id}-asdfg@example.com"
+ guessed = InfoRequest.guess_by_incoming_email(@im)
+ guessed[0].idhash.should == ir.idhash
+ end
+
+ it 'should find a request based on an email with a broken id and an intact hash' do
+ ir = info_requests(:fancy_dog_request)
+ idhash = ir.idhash
+ @im.mail.to = "request-123ab-#{idhash}@example.com"
+ guessed = InfoRequest.guess_by_incoming_email(@im)
+ guessed[0].id.should == ir.id
+ end
+
+ end
+
describe "making up the URL title" do
before do
@info_request = InfoRequest.new
diff --git a/spec/models/request_mailer_spec.rb b/spec/models/request_mailer_spec.rb
index a5f59b9bf..fbe22c220 100644
--- a/spec/models/request_mailer_spec.rb
+++ b/spec/models/request_mailer_spec.rb
@@ -27,7 +27,49 @@ describe RequestMailer, " when receiving incoming mail" do
receive_incoming_mail('incoming-request-plain.email', 'dummy@localhost')
ir.incoming_messages.size.should == 1
InfoRequest.holding_pen_request.incoming_messages.size.should == 1
+ last_event = InfoRequest.holding_pen_request.incoming_messages[0].info_request.get_last_event
+ last_event.params[:rejected_reason].should == "Could not identify the request from the email address"
+
+ deliveries = ActionMailer::Base.deliveries
+ deliveries.size.should == 1
+ mail = deliveries[0]
+ mail.to.should == [ MySociety::Config.get("CONTACT_EMAIL", 'contact@localhost') ]
+ deliveries.clear
+ end
+ it "should store mail in holding pen and send to admin when the from email is empty and only authorites can reply" do
+ ir = info_requests(:fancy_dog_request)
+ ir.allow_new_responses_from = 'authority_only'
+ ir.handle_rejected_responses = 'holding_pen'
+ ir.save!
+ ir.incoming_messages.size.should == 1
+ InfoRequest.holding_pen_request.incoming_messages.size.should == 0
+ receive_incoming_mail('incoming-request-plain.email', ir.incoming_email, "")
+ ir.incoming_messages.size.should == 1
+ InfoRequest.holding_pen_request.incoming_messages.size.should == 1
+ last_event = InfoRequest.holding_pen_request.incoming_messages[0].info_request.get_last_event
+ last_event.params[:rejected_reason].should =~ /there is no "From" address/
+
+ deliveries = ActionMailer::Base.deliveries
+ deliveries.size.should == 1
+ mail = deliveries[0]
+ mail.to.should == [ MySociety::Config.get("CONTACT_EMAIL", 'contact@localhost') ]
+ deliveries.clear
+ end
+
+ it "should store mail in holding pen and send to admin when the from email is unknown and only authorites can reply" do
+ ir = info_requests(:fancy_dog_request)
+ ir.allow_new_responses_from = 'authority_only'
+ ir.handle_rejected_responses = 'holding_pen'
+ ir.save!
+ ir.incoming_messages.size.should == 1
+ InfoRequest.holding_pen_request.incoming_messages.size.should == 0
+ receive_incoming_mail('incoming-request-plain.email', ir.incoming_email, "frob@nowhere.com")
+ ir.incoming_messages.size.should == 1
+ InfoRequest.holding_pen_request.incoming_messages.size.should == 1
+ last_event = InfoRequest.holding_pen_request.incoming_messages[0].info_request.get_last_event
+ last_event.params[:rejected_reason].should =~ /Only the authority can reply/
+
deliveries = ActionMailer::Base.deliveries
deliveries.size.should == 1
mail = deliveries[0]
@@ -108,6 +150,8 @@ describe RequestMailer, " when receiving incoming mail" do
receive_incoming_mail('incoming-request-plain.email', ir.incoming_email)
ir.incoming_messages.size.should == 1
InfoRequest.holding_pen_request.incoming_messages.size.should == 1 # arrives in holding pen
+ last_event = InfoRequest.holding_pen_request.incoming_messages[0].info_request.get_last_event
+ last_event.params[:rejected_reason].should =~ /allow new responses from nobody/
# should be a message to admin regarding holding pen
deliveries = ActionMailer::Base.deliveries
diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb
index c913ce3a8..ee6916ffc 100644
--- a/spec/models/user_spec.rb
+++ b/spec/models/user_spec.rb
@@ -15,6 +15,24 @@ describe User, "making up the URL name" do
@user.url_name.should == 'user'
end
end
+
+
+describe User, "showing the name" do
+ before do
+ @user = User.new
+ @user.name = 'Some Name '
+ end
+
+ it 'should strip whitespace' do
+ @user.name.should == 'Some Name'
+ end
+
+ it 'should show if user has been banned' do
+ @user.ban_text = "Naughty user"
+ @user.name.should == 'Some Name (Banned)'
+ end
+
+end
describe User, " when authenticating" do
before do