diff options
86 files changed, 1771 insertions, 600 deletions
diff --git a/.cvsignore b/.cvsignore deleted file mode 100644 index 1e38e022c..000000000 --- a/.cvsignore +++ /dev/null @@ -1,5 +0,0 @@ -tmp -log -coverage -sqlitedbs -cache diff --git a/Gemfile.lock b/Gemfile.lock index 62258c0c6..492c855c3 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -266,7 +266,7 @@ GEM nokogiri (>= 1.2.0) rack (>= 1.0) rack-test (>= 0.5.3) - will_paginate (3.0.4) + will_paginate (3.0.5) xapian-full-alaveteli (1.2.9.5) xml-simple (1.1.2) zip (2.0.2) diff --git a/app/controllers/admin_general_controller.rb b/app/controllers/admin_general_controller.rb index 196616ed6..753208c9a 100644 --- a/app/controllers/admin_general_controller.rb +++ b/app/controllers/admin_general_controller.rb @@ -27,13 +27,16 @@ class AdminGeneralController < AdminController @comment_count = Comment.count # Tasks to do - @requires_admin_requests = InfoRequest.find(:all, :select => '*, ' + InfoRequest.last_event_time_clause + ' as last_event_time', :conditions => ["described_state = 'requires_admin'"], :order => "last_event_time") - @error_message_requests = InfoRequest.find(:all, :select => '*, ' + InfoRequest.last_event_time_clause + ' as last_event_time', :conditions => ["described_state = 'error_message'"], :order => "last_event_time") - @attention_requests = InfoRequest.find(:all, :select => '*, ' + InfoRequest.last_event_time_clause + ' as last_event_time', :conditions => ["described_state = 'attention_requested'"], :order => "last_event_time") - @blank_contacts = PublicBody.find(:all, :conditions => ["request_email = ''"], :order => "updated_at") + @requires_admin_requests = InfoRequest.find_in_state('requires_admin') + @error_message_requests = InfoRequest.find_in_state('error_message') + @attention_requests = InfoRequest.find_in_state('attention_requested') + @blank_contacts = PublicBody.find(:all, :conditions => ["request_email = ''"], + :order => "updated_at") @old_unclassified = InfoRequest.find_old_unclassified(:limit => 20, - :conditions => ["prominence = 'normal'"]) + :conditions => ["prominence = 'normal'"]) @holding_pen_messages = InfoRequest.holding_pen_request.incoming_messages + @new_body_requests = PublicBodyChangeRequest.new_body_requests.open + @body_update_requests = PublicBodyChangeRequest.body_update_requests.open end def timeline diff --git a/app/controllers/admin_public_body_change_requests_controller.rb b/app/controllers/admin_public_body_change_requests_controller.rb new file mode 100644 index 000000000..d76cdc0e5 --- /dev/null +++ b/app/controllers/admin_public_body_change_requests_controller.rb @@ -0,0 +1,15 @@ +class AdminPublicBodyChangeRequestsController < AdminController + + def edit + @change_request = PublicBodyChangeRequest.find(params[:id]) + end + + def update + @change_request = PublicBodyChangeRequest.find(params[:id]) + @change_request.close! + @change_request.send_response(params[:subject], params[:response]) + flash[:notice] = 'The change request has been closed and the user has been notified' + redirect_to admin_general_index_path + end + +end diff --git a/app/controllers/admin_public_body_controller.rb b/app/controllers/admin_public_body_controller.rb index 88e275960..120419a27 100644 --- a/app/controllers/admin_public_body_controller.rb +++ b/app/controllers/admin_public_body_controller.rb @@ -85,14 +85,33 @@ class AdminPublicBodyController < AdminController def new @public_body = PublicBody.new - render + if params[:change_request_id] + @change_request = PublicBodyChangeRequest.find(params[:change_request_id]) + end + if @change_request + @change_request_user_response = render_to_string(:template => "admin_public_body_change_requests/add_accepted", + :formats => [:txt]) + @public_body.name = @change_request.public_body_name + @public_body.request_email = @change_request.public_body_email + @public_body.last_edit_comment = @change_request.comment_for_public_body + end + render :formats => [:html] end def create I18n.with_locale(I18n.default_locale) do + if params[:change_request_id] + @change_request = PublicBodyChangeRequest.find(params[:change_request_id]) + end params[:public_body][:last_edit_editor] = admin_current_user() @public_body = PublicBody.new(params[:public_body]) if @public_body.save + if @change_request + response_text = params[:response].gsub(_("[Authority URL will be inserted here]"), + public_body_url(@public_body, :only_path => false)) + @change_request.close! + @change_request.send_response(params[:subject], response_text) + end flash[:notice] = 'PublicBody was successfully created.' redirect_to admin_body_show_url(@public_body) else @@ -103,15 +122,32 @@ class AdminPublicBodyController < AdminController def edit @public_body = PublicBody.find(params[:id]) - @public_body.last_edit_comment = "" - render + if params[:change_request_id] + @change_request = PublicBodyChangeRequest.find(params[:change_request_id]) + end + if @change_request + @change_request_user_response = render_to_string(:template => "admin_public_body_change_requests/update_accepted", + :formats => [:txt]) + @public_body.request_email = @change_request.public_body_email + @public_body.last_edit_comment = @change_request.comment_for_public_body + else + @public_body.last_edit_comment = "" + end + render :formats => [:html] end def update + if params[:change_request_id] + @change_request = PublicBodyChangeRequest.find(params[:change_request_id]) + end I18n.with_locale(I18n.default_locale) do params[:public_body][:last_edit_editor] = admin_current_user() @public_body = PublicBody.find(params[:id]) if @public_body.update_attributes(params[:public_body]) + if @change_request + @change_request.close! + @change_request.send_response(params[:subject], params[:response]) + end flash[:notice] = 'PublicBody was successfully updated.' redirect_to admin_body_show_url(@public_body) else diff --git a/app/controllers/admin_request_controller.rb b/app/controllers/admin_request_controller.rb index 4d45ced8b..fc291d998 100644 --- a/app/controllers/admin_request_controller.rb +++ b/app/controllers/admin_request_controller.rb @@ -26,15 +26,13 @@ class AdminRequestController < AdminController def show @info_request = InfoRequest.find(params[:id]) - # XXX is this *really* the only way to render a template to a - # variable, rather than to the response? - vars = OpenStruct.new(:name_to => @info_request.user_name, - :name_from => AlaveteliConfiguration::contact_name, - :info_request => @info_request, :reason => params[:reason], - :info_request_url => 'http://' + AlaveteliConfiguration::domain + request_url(@info_request), - :site_name => site_name) - template = File.read(File.join(File.dirname(__FILE__), "..", "views", "admin_request", "hidden_user_explanation.html.erb")) - @request_hidden_user_explanation = ERB.new(template).result(vars.instance_eval { binding }) + vars_for_explanation = {:reason => params[:reason], + :info_request => @info_request, + :name_to => @info_request.user_name, + :name_from => AlaveteliConfiguration::contact_name, + :info_request_url => request_url(@info_request, :only_path => false)} + @request_hidden_user_explanation = render_to_string(:template => "admin_request/hidden_user_explanation", + :locals => vars_for_explanation) end def resend @@ -281,7 +279,8 @@ class AdminRequestController < AdminController if ! info_request.is_external? ContactMailer.from_admin_message( - info_request.user, + info_request.user.name, + info_request.user.email, subject, params[:explanation].strip.html_safe ).deliver diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 161a82b26..7c122917d 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -428,100 +428,6 @@ class ApplicationController < ActionController::Base end end - def get_request_variety_from_params(params) - query = "" - sortby = "newest" - varieties = [] - if params[:request_variety] && !(query =~ /variety:/) - if params[:request_variety].include? "sent" - varieties -= ['variety:sent', 'variety:followup_sent', 'variety:response', 'variety:comment'] - varieties << ['variety:sent', 'variety:followup_sent'] - end - if params[:request_variety].include? "response" - varieties << ['variety:response'] - end - if params[:request_variety].include? "comment" - varieties << ['variety:comment'] - end - end - if !varieties.empty? - query = " (#{varieties.join(' OR ')})" - end - return query - end - - def get_status_from_params(params) - query = "" - if params[:latest_status] - statuses = [] - if params[:latest_status].class == String - params[:latest_status] = [params[:latest_status]] - end - if params[:latest_status].include?("recent") || params[:latest_status].include?("all") - query += " (variety:sent OR variety:followup_sent OR variety:response OR variety:comment)" - end - if params[:latest_status].include? "successful" - statuses << ['latest_status:successful', 'latest_status:partially_successful'] - end - if params[:latest_status].include? "unsuccessful" - statuses << ['latest_status:rejected', 'latest_status:not_held'] - end - if params[:latest_status].include? "awaiting" - statuses << ['latest_status:waiting_response', 'latest_status:waiting_clarification', 'waiting_classification:true', 'latest_status:internal_review','latest_status:gone_postal', 'latest_status:error_message', 'latest_status:requires_admin'] - end - if params[:latest_status].include? "internal_review" - statuses << ['status:internal_review'] - end - if params[:latest_status].include? "other" - statuses << ['latest_status:gone_postal', 'latest_status:error_message', 'latest_status:requires_admin', 'latest_status:user_withdrawn'] - end - if params[:latest_status].include? "gone_postal" - statuses << ['latest_status:gone_postal'] - end - if !statuses.empty? - query = " (#{statuses.join(' OR ')})" - end - end - return query - end - - def get_date_range_from_params(params) - query = "" - if params.has_key?(:request_date_after) && !params.has_key?(:request_date_before) - params[:request_date_before] = Time.now.strftime("%d/%m/%Y") - query += " #{params[:request_date_after]}..#{params[:request_date_before]}" - elsif !params.has_key?(:request_date_after) && params.has_key?(:request_date_before) - params[:request_date_after] = "01/01/2001" - end - if params.has_key?(:request_date_after) - query = " #{params[:request_date_after]}..#{params[:request_date_before]}" - end - return query - end - - def get_tags_from_params(params) - query = "" - tags = [] - if params.has_key?(:tags) - params[:tags].split().each do |tag| - tags << "tag:#{tag}" - end - end - if !tags.empty? - query = " (#{tags.join(' OR ')})" - end - return query - end - - def make_query_from_params(params) - query = params[:query] || "" if query.nil? - query += get_date_range_from_params(params) - query += get_request_variety_from_params(params) - query += get_status_from_params(params) - query += get_tags_from_params(params) - return query - end - def country_from_ip country = "" if !AlaveteliConfiguration::gaze_url.empty? diff --git a/app/controllers/general_controller.rb b/app/controllers/general_controller.rb index b01a67027..6f0d29889 100644 --- a/app/controllers/general_controller.rb +++ b/app/controllers/general_controller.rb @@ -103,7 +103,7 @@ class GeneralController < ApplicationController params[:query] = @query end if @variety_postfix != "all" && @requests - @query, _ = make_query_from_params(params) + @query = InfoRequestEvent.make_query_from_params(params) end @inputted_sortby = @sortby if @sortby.nil? diff --git a/app/controllers/public_body_change_requests_controller.rb b/app/controllers/public_body_change_requests_controller.rb new file mode 100644 index 000000000..4a6c5f5cb --- /dev/null +++ b/app/controllers/public_body_change_requests_controller.rb @@ -0,0 +1,28 @@ +class PublicBodyChangeRequestsController < ApplicationController + + def create + @change_request = PublicBodyChangeRequest.from_params(params[:public_body_change_request], @user) + if @change_request.save + @change_request.send_message + flash[:notice] = @change_request.thanks_notice + redirect_to frontpage_url + return + else + render :action => 'new' + end + end + + def new + @change_request = PublicBodyChangeRequest.new + if params[:body] + @change_request.public_body = PublicBody.find_by_url_name_with_historic(params[:body]) + end + if @change_request.public_body + @title = _('Ask us to update the email address for {{public_body_name}}', + :public_body_name => @change_request.public_body.name) + else + @title = _('Ask us to add an authority') + end + + end +end diff --git a/app/controllers/public_body_controller.rb b/app/controllers/public_body_controller.rb index 308d38e4c..862f4b318 100644 --- a/app/controllers/public_body_controller.rb +++ b/app/controllers/public_body_controller.rb @@ -40,7 +40,7 @@ class PublicBodyController < ApplicationController @searched_to_send_request = true end @view = params[:view] - query = make_query_from_params(params.merge(:latest_status => @view)) + query = InfoRequestEvent.make_query_from_params(params.merge(:latest_status => @view)) query += " requested_from:#{@public_body.url_name}" # Use search query for this so can collapse and paginate easily # XXX really should just use SQL query here rather than Xapian. diff --git a/app/controllers/request_controller.rb b/app/controllers/request_controller.rb index d982bd391..b79ead73d 100644 --- a/app/controllers/request_controller.rb +++ b/app/controllers/request_controller.rb @@ -141,7 +141,10 @@ class RequestController < ApplicationController def list medium_cache @view = params[:view] + @locale = self.locale_from_params() @page = get_search_page_from_params if !@page # used in cache case, as perform_search sets @page as side effect + @per_page = PER_PAGE + @max_results = MAX_RESULTS if @view == "recent" return redirect_to request_list_all_url(:action => "list", :view => "all", :page => @page), :status => :moved_permanently end @@ -151,16 +154,11 @@ class RequestController < ApplicationController raise ActiveRecord::RecordNotFound.new("Sorry. No pages after #{MAX_RESULTS / PER_PAGE}.") end - query = make_query_from_params(params.merge(:latest_status => @view)) + @filters = params.merge(:latest_status => @view) @title = _("View and search requests") - sortby = "newest" - xapian_object = perform_search([InfoRequestEvent], query, sortby, 'request_collapse') - @list_results = xapian_object.results.map { |r| r[:model] } - @matches_estimated = xapian_object.matches_estimated - @show_no_more_than = (@matches_estimated > MAX_RESULTS) ? MAX_RESULTS : @matches_estimated @title = @title + " (page " + @page.to_s + ")" if (@page > 1) - @track_thing = TrackThing.create_track_for_search_query(query) + @track_thing = TrackThing.create_track_for_search_query(InfoRequestEvent.make_query_from_params(@filters)) @feed_autodetect = [ { :url => do_track_url(@track_thing, 'feed'), :title => @track_thing.params[:title_in_rss], :has_json => true } ] # Don't let robots go more than 20 pages in @@ -326,7 +324,7 @@ class RequestController < ApplicationController if !authenticated?( :web => _("To send your FOI request").to_str, :email => _("Then your FOI request to {{public_body_name}} will be sent.",:public_body_name=>@info_request.public_body.name), - :email_subject => _("Confirm your FOI request to ") + @info_request.public_body.name + :email_subject => _("Confirm your FOI request to {{public_body_name}}",:public_body_name=>@info_request.public_body.name) ) # do nothing - as "authenticated?" has done the redirect to signin page for us return diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 4b603b064..154697377 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -123,5 +123,18 @@ module ApplicationHelper yield end end + + # We only want to cache request lists that have a reasonable chance of not expiring + # before they're requested again. Don't cache lists returned from specific searches + # or anything except the first page of results, just the first page of the default + # views + def request_list_cache_key + cacheable_param_list = ['controller', 'action', 'locale', 'view'] + if params.keys.all?{ |key| cacheable_param_list.include?(key) } + "request-list-#{@view}-#{@locale}" + else + nil + end + end end diff --git a/app/helpers/link_to_helper.rb b/app/helpers/link_to_helper.rb index 8df28f350..405886a85 100755 --- a/app/helpers/link_to_helper.rb +++ b/app/helpers/link_to_helper.rb @@ -121,24 +121,37 @@ module LinkToHelper end def user_link_absolute(user) - link_to h(user.name), user_url(user) + link_to user.name, user_url(user) end def user_link(user) - link_to h(user.name), user_path(user) + link_to user.name, user_path(user) end - def request_user_link_absolute(request) + def external_user_link(request, absolute, text) + if request.external_user_name + request.external_user_name + else + if absolute + url = help_privacy_url(:anchor => 'anonymous') + else + url = help_privacy_path(:anchor => 'anonymous') + end + link_to(text, url) + end + end + + def request_user_link_absolute(request, anonymous_text=_("Anonymous user")) if request.is_external? - request.external_user_name || _("Anonymous user") + external_user_link(request, absolute=true, anonymous_text) else user_link_absolute(request.user) end end - def request_user_link(request) + def request_user_link(request, anonymous_text=_("Anonymous user")) if request.is_external? - request.external_user_name || _("Anonymous user") + external_user_link(request, absolute=false, anonymous_text) else user_link(request.user) end diff --git a/app/mailers/contact_mailer.rb b/app/mailers/contact_mailer.rb index 4dc49bf8b..27e04ca4b 100644 --- a/app/mailers/contact_mailer.rb +++ b/app/mailers/contact_mailer.rb @@ -34,12 +34,31 @@ class ContactMailer < ApplicationMailer end # Send message to a user from the administrator - def from_admin_message(recipient_user, subject, message) - @message, @from_user, @recipient_user = message, contact_from_name_and_email, recipient_user - + def from_admin_message(recipient_name, recipient_email, subject, message) + @message, @from_user = message, contact_from_name_and_email + @recipient_name, @recipient_email = recipient_name, recipient_email mail(:from => contact_from_name_and_email, - :to => recipient_user.name_and_email, + :to => MailHandler.address_from_name_and_email(@recipient_name, @recipient_email), :bcc => AlaveteliConfiguration::contact_email, :subject => subject) end + + # Send a request to the administrator to add an authority + def add_public_body(change_request) + @change_request = change_request + mail(:from => MailHandler.address_from_name_and_email(@change_request.get_user_name, @change_request.get_user_email), + :to => contact_from_name_and_email, + :subject => _('Add authority - {{public_body_name}}', + :public_body_name => @change_request.get_public_body_name)) + end + + # Send a request to the administrator to update an authority email address + def update_public_body_email(change_request) + @change_request = change_request + mail(:from => MailHandler.address_from_name_and_email(@change_request.get_user_name, @change_request.get_user_email), + :to => contact_from_name_and_email, + :subject => _('Update email address - {{public_body_name}}', + :public_body_name => @change_request.get_public_body_name)) + end + end diff --git a/app/models/incoming_message.rb b/app/models/incoming_message.rb index bcf0b6ec9..59e61952e 100644 --- a/app/models/incoming_message.rb +++ b/app/models/incoming_message.rb @@ -385,6 +385,10 @@ class IncomingMessage < ActiveRecord::Base multiline_original_message = '(' + '''>>>.* \d\d/\d\d/\d\d\d\d\s+\d\d:\d\d(?::\d\d)?\s*>>>''' + ')' text.gsub!(/^(#{multiline_original_message}\n.*)$/m, replacement) + # On Thu, Nov 28, 2013 at 9:08 AM, A User + # <[1]request-7-skm40s2ls@xxx.xxxx> wrote: + text.gsub!(/^( On [^\n]+\n\s*\<[^>\n]+\> (wrote|said):\s*\n.*)$/m, replacement) + # Single line sections text.gsub!(/^(>.*\n)/, replacement) text.gsub!(/^(On .+ (wrote|said):\n)/, replacement) diff --git a/app/models/info_request.rb b/app/models/info_request.rb index 0a073dc79..def319ad4 100644 --- a/app/models/info_request.rb +++ b/app/models/info_request.rb @@ -113,8 +113,12 @@ class InfoRequest < ActiveRecord::Base # Possible reasons that a request could be reported for administrator attention def report_reasons - ["Contains defamatory material", "Not a valid request", "Request for personal information", - "Contains personal information", "Vexatious", "Other"] + [_("Contains defamatory material"), + _("Not a valid request"), + _("Request for personal information"), + _("Contains personal information"), + _("Vexatious"), + _("Other")] end def must_be_valid_state @@ -1228,6 +1232,23 @@ public return [xapian_similar, xapian_similar_more] end + def InfoRequest.request_list(filters, page, per_page, max_results) + xapian_object = ActsAsXapian::Search.new([InfoRequestEvent], + InfoRequestEvent.make_query_from_params(filters), + :offset => (page - 1) * per_page, + :limit => 25, + :sort_by_prefix => 'created_at', + :sort_by_ascending => true, + :collapse_by_prefix => 'request_collapse' + ) + list_results = xapian_object.results.map { |r| r[:model] } + matches_estimated = xapian_object.matches_estimated + show_no_more_than = [matches_estimated, max_results].min + return { :results => list_results, + :matches_estimated => matches_estimated, + :show_no_more_than => show_no_more_than } + end + def InfoRequest.recent_requests request_events = [] request_events_all_successful = false @@ -1275,6 +1296,12 @@ public return [request_events, request_events_all_successful] end + def InfoRequest.find_in_state(state) + find(:all, :select => '*, ' + last_event_time_clause + ' as last_event_time', + :conditions => ["described_state = ?", state], + :order => "last_event_time") + end + private def set_defaults diff --git a/app/models/info_request_event.rb b/app/models/info_request_event.rb index e268b28ca..5eed5ba83 100644 --- a/app/models/info_request_event.rb +++ b/app/models/info_request_event.rb @@ -21,6 +21,9 @@ # Email: hello@mysociety.org; WWW: http://www.mysociety.org/ class InfoRequestEvent < ActiveRecord::Base + + extend XapianQueries + belongs_to :info_request validates_presence_of :info_request @@ -416,4 +419,5 @@ class InfoRequestEvent < ActiveRecord::Base yield(column.human_name, self.send(column.name), column.type.to_s, column.name) end end + end diff --git a/app/models/profile_photo.rb b/app/models/profile_photo.rb index 322ebe53c..6c3b2cfa0 100644 --- a/app/models/profile_photo.rb +++ b/app/models/profile_photo.rb @@ -70,21 +70,25 @@ class ProfilePhoto < ActiveRecord::Base def data_and_draft_checks if self.data.nil? - errors.add(:data, N_("Please choose a file containing your photo.")) + errors.add(:data, _("Please choose a file containing your photo.")) return end if self.image.nil? - errors.add(:data, N_("Couldn't understand the image file that you uploaded. PNG, JPEG, GIF and many other common image file formats are supported.")) + errors.add(:data, _("Couldn't understand the image file that you uploaded. PNG, JPEG, GIF and many other common image file formats are supported.")) return end if self.image.format != 'PNG' - errors.add(:data, N_("Failed to convert image to a PNG")) + errors.add(:data, _("Failed to convert image to a PNG")) end if !self.draft && (self.image.columns != WIDTH || self.image.rows != HEIGHT) - errors.add(:data, N_("Failed to convert image to the correct size: at {{cols}}x{{rows}}, need {{width}}x{{height}}", :cols => self.image.columns, :rows => self.image.rows, :width => WIDTH, :height => HEIGHT)) + errors.add(:data, _("Failed to convert image to the correct size: at {{cols}}x{{rows}}, need {{width}}x{{height}}", + :cols => self.image.columns, + :rows => self.image.rows, + :width => WIDTH, + :height => HEIGHT)) end if self.draft && self.user_id diff --git a/app/models/public_body.rb b/app/models/public_body.rb index 933825d2a..a78a6677e 100644 --- a/app/models/public_body.rb +++ b/app/models/public_body.rb @@ -346,22 +346,26 @@ class PublicBody < ActiveRecord::Base # The "internal admin" is a special body for internal use. def PublicBody.internal_admin_body - I18n.with_locale(I18n.default_locale) do - pb = PublicBody.find_by_url_name("internal_admin_authority") - if pb.nil? - pb = PublicBody.new( - :name => 'Internal admin authority', - :short_name => "", - :request_email => AlaveteliConfiguration::contact_email, - :home_page => "", - :notes => "", - :publication_scheme => "", - :last_edit_editor => "internal_admin", - :last_edit_comment => "Made by PublicBody.internal_admin_body" - ) - pb.save! + # Use find_by_sql to avoid the search being specific to a + # locale, since url_name is a translated field: + sql = "SELECT * FROM public_bodies WHERE url_name = 'internal_admin_authority'" + matching_pbs = PublicBody.find_by_sql sql + case + when matching_pbs.empty? then + I18n.with_locale(I18n.default_locale) do + PublicBody.create!(:name => 'Internal admin authority', + :short_name => "", + :request_email => AlaveteliConfiguration::contact_email, + :home_page => "", + :notes => "", + :publication_scheme => "", + :last_edit_editor => "internal_admin", + :last_edit_comment => "Made by PublicBody.internal_admin_body") end - return pb + when matching_pbs.length == 1 then + matching_pbs[0] + else + raise "Multiple public bodies (#{matching_pbs.length}) found with url_name 'internal_admin_authority'" end end diff --git a/app/models/public_body_change_request.rb b/app/models/public_body_change_request.rb new file mode 100644 index 000000000..c1f395c0c --- /dev/null +++ b/app/models/public_body_change_request.rb @@ -0,0 +1,130 @@ +# == Schema Information +# +# Table name: public_body_change_requests +# +# id :integer not null, primary key +# user_email :string(255) +# user_name :string(255) +# user_id :integer +# public_body_name :text +# public_body_id :integer +# public_body_email :string(255) +# source_url :text +# notes :text +# is_open :boolean default(TRUE), not null +# created_at :datetime not null +# updated_at :datetime not null +# + +class PublicBodyChangeRequest < ActiveRecord::Base + + belongs_to :user + belongs_to :public_body + validates_presence_of :public_body_name, :message => N_("Please enter the name of the authority"), + :unless => proc{ |change_request| change_request.public_body } + validates_presence_of :user_name, :message => N_("Please enter your name"), + :unless => proc{ |change_request| change_request.user } + validates_presence_of :user_email, :message => N_("Please enter your email address"), + :unless => proc{ |change_request| change_request.user } + validate :user_email_format, :unless => proc{ |change_request| change_request.user_email.blank? } + validate :body_email_format, :unless => proc{ |change_request| change_request.public_body_email.blank? } + + scope :new_body_requests, :conditions => ['public_body_id IS NULL'], :order => 'created_at' + scope :body_update_requests, :conditions => ['public_body_id IS NOT NULL'], :order => 'created_at' + scope :open, :conditions => ['is_open = ?', true] + + def self.from_params(params, user) + change_request = new + change_request.update_from_params(params, user) + end + + def update_from_params(params, user) + if user + self.user_id = user.id + else + self.user_name = params[:user_name] + self.user_email = params[:user_email] + end + self.public_body_name = params[:public_body_name] + self.public_body_id = params[:public_body_id] + self.public_body_email = params[:public_body_email] + self.source_url = params[:source_url] + self.notes = params[:notes] + self + end + + def get_user_name + user ? user.name : user_name + end + + def get_user_email + user ? user.email : user_email + end + + def get_public_body_name + public_body ? public_body.name : public_body_name + end + + def send_message + if public_body + ContactMailer.update_public_body_email(self).deliver + else + ContactMailer.add_public_body(self).deliver + end + end + + def thanks_notice + if self.public_body + _("Your request to update the address for #{get_public_body_name} has been sent. Thank you for getting in touch! We'll get back to you soon.") + else + _("Your request to add an authority has been sent. Thank you for getting in touch! We'll get back to you soon.") + end + end + + def send_response(subject, response) + ContactMailer.from_admin_message(get_user_name, + get_user_email, + subject, + response.strip.html_safe).deliver + end + + def comment_for_public_body + comments = [_("Requested by: #{get_user_name} (#{get_user_email})")] + if !source_url.blank? + comments << _("Source URL: #{source_url}") + end + if !notes.blank? + comments << _("Notes: #{notes}") + end + comments.join("\n") + end + + def default_response_subject + if self.public_body + _("Your request to update {{public_body_name}} on {{site_name}}", :site_name => AlaveteliConfiguration::site_name, + :public_body_name => public_body.name) + else + _("Your request to add {{public_body_name}} to {{site_name}}", :site_name => AlaveteliConfiguration::site_name, + :public_body_name => public_body_name) + end + end + + def close! + self.is_open = false + self.save! + end + + private + + def body_email_format + unless MySociety::Validate.is_valid_email(self.public_body_email) + errors.add(:public_body_email, _("The authority email doesn't look like a valid address")) + end + end + + def user_email_format + unless MySociety::Validate.is_valid_email(self.user_email) + errors.add(:user_email, _("Your email doesn't look like a valid address")) + end + end +end diff --git a/app/views/admin_general/_change_request_summary.html.erb b/app/views/admin_general/_change_request_summary.html.erb new file mode 100644 index 000000000..bec49c12c --- /dev/null +++ b/app/views/admin_general/_change_request_summary.html.erb @@ -0,0 +1,60 @@ +<table class="table table-striped table-condensed"> + <tbody> + <tr> + <td> + <b>Authority</b> + </td> + <td> + <%= @change_request.get_public_body_name %> + </td> + </tr> + + <% if @change_request.public_body %> + <tr> + <td> + <b>Current address</b> + </td> + <td> + <%= @change_request.public_body.request_email %> + </td> + </tr> + <% end %> + + <tr> + <td> + <b>Suggested address</b> + </td> + <td> + <%= @change_request.public_body_email %> + </td> + </tr> + + <tr> + <td> + <b>Source</b> + </td> + <td> + <%= @change_request.source_url %> + </td> + </tr> + + <tr> + <td> + <b>Requested by</b> + </td> + <td> + <%= @change_request.get_user_name %> (<%= @change_request.get_user_email %>) + </td> + </tr> + + <tr> + <td> + <b>Requested on</b> + </td> + <td> + <%= I18n.l(@change_request.created_at, :format => "%e %B %Y %H:%M:%S") %> + (<%= "#{time_ago_in_words(@change_request.created_at)} ago" %>) + </td> + </tr> + </tbody> +</table> diff --git a/app/views/admin_general/index.html.erb b/app/views/admin_general/index.html.erb index 976860fa7..2202663be 100644 --- a/app/views/admin_general/index.html.erb +++ b/app/views/admin_general/index.html.erb @@ -174,9 +174,39 @@ </div> </div> <% end %> + + <% if @new_body_requests.size > 0 %> + <div class="accordion-group"> + <div class="accordion-heading"> + <a class="accordion-toggle" href="#new-authorities" data-toggle="collapse" data-parent="things-to-do"><span class="label label-important"><%= @new_body_requests.size %></span><%= chevron_right %> Add new authorities</a> + </div> + <div id="new-authorities" class="accordion-body collapse"> + <% for @change_request in @new_body_requests %> + <%= render :partial => 'change_request_summary'%> + <%= link_to("Close and respond", admin_change_request_edit_path(@change_request), :class => 'btn') %> + <%= link_to("Add authority", admin_body_new_path(:change_request_id => @change_request.id), :class => 'btn btn-primary') %> + <% end %> + </div> + </div> + <% end %> + + <% if @body_update_requests.size > 0 %> + <div class="accordion-group"> + <div class="accordion-heading"> + <a class="accordion-toggle" href="#update-authorities" data-toggle="collapse" data-parent="things-to-do"><span class="label label-important"><%= @body_update_requests.size %></span><%= chevron_right %> Update authorities</a> + </div> + <div id="update-authorities" class="accordion-body collapse"> + <% for @change_request in @body_update_requests %> + <%= render :partial => 'change_request_summary' %> + <%= link_to("Close and respond", admin_change_request_edit_path(@change_request), :class => 'btn') %> + <%= link_to("Make update", admin_body_edit_path(@change_request.public_body, :change_request_id => @change_request.id), :class => 'btn btn-primary') %> + <% end %> + </div> + </div> + <% end %> </div> -<% if @holding_pen_messages.size == 0 && @old_unclassified.size == 0 && @requires_admin_requests.size == 0 && @blank_contacts.size == 0 && @attention_requests.size == 0 %> +<% if @holding_pen_messages.size == 0 && @old_unclassified.size == 0 && @requires_admin_requests.size == 0 && @blank_contacts.size == 0 && @attention_requests.size == 0 && @new_body_requests.size == 0 && @body_update_requests.size == 0 %> <div class="row"> <div class="span12 alert alert-success"> No pending administration required. diff --git a/app/views/admin_public_body/_form.html.erb b/app/views/admin_public_body/_form.html.erb index 5a80386ec..2da13ab01 100644 --- a/app/views/admin_public_body/_form.html.erb +++ b/app/views/admin_public_body/_form.html.erb @@ -95,4 +95,8 @@ <p class="help-block">put URL or other source of new info</p> </div> </div> +<% if @change_request %> + <%= render :partial => 'admin_public_body_change_requests/response' %> + +<% end %> <!--[eoform:public_body]--> diff --git a/app/views/admin_public_body/new.html.erb b/app/views/admin_public_body/new.html.erb index 13e8238d6..24b27d7af 100644 --- a/app/views/admin_public_body/new.html.erb +++ b/app/views/admin_public_body/new.html.erb @@ -6,13 +6,15 @@ <div id="public_body_form"> <%= form_for @public_body, :as => :public_body, :url => admin_body_create_path, :html => {:class => "form form-horizontal"} do |f| %> <%= render :partial => 'form', :locals => {:f => f} %> + + <div class="form-actions"> <%= f.submit "Create", :class => "btn btn-primary" %> </div> <% end %> <div class="row"> <div class="span8 well"> - <%= link_to _('List all'), 'list' %> + <%= link_to 'List all', admin_body_list_path, :class => "btn" %> </div> </div> </div> diff --git a/app/views/admin_public_body/show.html.erb b/app/views/admin_public_body/show.html.erb index 8262287d5..f8161db26 100644 --- a/app/views/admin_public_body/show.html.erb +++ b/app/views/admin_public_body/show.html.erb @@ -67,7 +67,10 @@ </div> <% if i == versions.length - 1 %> <div class="span6"> - <%=_("This is the first version.")%> + <p>“<%= h(historic_public_body.last_edit_comment) %>”</p> + <ul> + <li><%=_("This is the first version.")%></li> + </ul> </div> <% else %> <div class="span6"> diff --git a/app/views/admin_public_body_change_requests/_response.html.erb b/app/views/admin_public_body_change_requests/_response.html.erb new file mode 100644 index 000000000..7fda8b7f8 --- /dev/null +++ b/app/views/admin_public_body_change_requests/_response.html.erb @@ -0,0 +1,15 @@ +<h3>Response to change request (will be emailed to user):</h3> +<%= hidden_field_tag 'change_request_id', @change_request.id %> +<div class="control-group" id="change_request_user_subject"> + <label for="change_request_user_subject_field" class="control-label">Subject of email:</label> + <div class="controls"> + <%= text_field_tag "subject", (params[:subject] || @change_request.default_response_subject), {:id => "change_request_user_subject_field", :class => "span6"} %> + </div> +</div> + +<div class="control-group" id="change_request_user_response"> + <label for="change_request_user_response_field" class="control-label">Response</label> + <div class="controls"> + <%= text_area_tag "response", (params[:response] || h(@change_request_user_response)), {:id => "change_request_user_response_field", :rows => 10, :class => 'span6'} %> + </div> +</div> diff --git a/app/views/admin_public_body_change_requests/add_accepted.txt.erb b/app/views/admin_public_body_change_requests/add_accepted.txt.erb new file mode 100644 index 000000000..fb22466b0 --- /dev/null +++ b/app/views/admin_public_body_change_requests/add_accepted.txt.erb @@ -0,0 +1,9 @@ +<%= _("Dear {{user_name}},", :user_name => @change_request.get_user_name) %> + +<%= _("Thanks for your suggestion to add {{public_body_name}}. It's been added to the site here:", :public_body_name => @change_request.public_body_name) %> + +<%= _("[Authority URL will be inserted here]")%> + +<%= _("Yours,") %> + +<%= _("The {{site_name}} team.", :site_name => site_name) %> diff --git a/app/views/admin_public_body_change_requests/edit.html.erb b/app/views/admin_public_body_change_requests/edit.html.erb new file mode 100644 index 000000000..cc9c5b5d9 --- /dev/null +++ b/app/views/admin_public_body_change_requests/edit.html.erb @@ -0,0 +1,8 @@ +<h1><%=@title%></h1> + +<%= form_tag admin_change_request_update_path(@change_request), :class => "form form-horizontal" do %> + <%= render :partial => 'admin_public_body_change_requests/response'%> + <div class="form-actions"> + <%= submit_tag 'Close', :accesskey => 'c', :class => "btn btn-primary" %> + </div> +<% end %> diff --git a/app/views/admin_public_body_change_requests/update_accepted.txt.erb b/app/views/admin_public_body_change_requests/update_accepted.txt.erb new file mode 100644 index 000000000..9c29c959b --- /dev/null +++ b/app/views/admin_public_body_change_requests/update_accepted.txt.erb @@ -0,0 +1,7 @@ +<%= _("Dear {{user_name}},", :user_name => @change_request.get_user_name) %> + +<%= _("Thanks for your suggestion to update the email address for {{public_body_name}} to {{public_body_email}}. This has now been done and any new requests will be sent to the new address.", :public_body_name => @change_request.public_body.name, :public_body_email => @change_request.public_body_email) %> + +<%= _("Yours,") %> + +<%= _("The {{site_name}} team.", :site_name => site_name) %> diff --git a/app/views/admin_request/_some_annotations.html.erb b/app/views/admin_request/_some_annotations.html.erb new file mode 100644 index 000000000..dfd46f828 --- /dev/null +++ b/app/views/admin_request/_some_annotations.html.erb @@ -0,0 +1,48 @@ +<% if comments.size > 0 %> + <div class="accordion" id="comments"> + <% for comment in comments %> + <div class="accordion-group"> + <div class="accordion-heading"> + <a href="#comment_<%=comment.id%>" data-toggle="collapse" data-parent="#comments"><%= chevron_right %></a> + <%= link_to admin_request_edit_comment_path(comment) do %> + #<%=comment.id%> + -- + <%=h(comment.user.name)%> + <%=admin_value(comment.created_at)%> + <% end %> + <blockquote class="incoming-message"> + <%= truncate(comment.body, :length => 400) %> + </blockquote> + </div> + <div id="comment_<%=comment.id%>" class="accordion-body collapse"> + <table class="table table-striped table-condensed"> + <tbody> + <tr> + <td colspan="2"> + By <%= user_both_links(comment.user) %> + </td> + </tr> + <% comment.for_admin_column do |name, value, type, column_name |%> + <tr> + <td> + <b><%=name%></b> + </td> + <td> + <% if column_name == 'body' && !comment.visible %> + <s><%=h comment.send(column_name) %></s> + <% else %> + <%=h comment.send(column_name) %> + <% end %> + </td> + </tr> + <% end %> + </tbody> + </table> + </div> + </div> + <% end %> + </div> +<% else %> + <p>None yet.</p> +<% end %> + diff --git a/app/views/admin_request/show.html.erb b/app/views/admin_request/show.html.erb index 83d9c3764..2589e52b4 100644 --- a/app/views/admin_request/show.html.erb +++ b/app/views/admin_request/show.html.erb @@ -325,53 +325,8 @@ <hr> <h2>Annotations</h2> -<% if @info_request.comments.size > 0 %> - <div class="accordion" id="comments"> - <% for comment in @info_request.comments %> - <div class="accordion-group"> - <div class="accordion-heading"> - <a href="#comment_<%=comment.id%>" data-toggle="collapse" data-parent="#comments"><%= chevron_right %></a> - <%= link_to admin_request_edit_comment_path(comment) do %> - #<%=comment.id%> - -- - <%=h(comment.user.name)%> - <%=admin_value(comment.created_at)%> - <% end %> - <blockquote class="incoming-message"> - <%= truncate(comment.body, :length => 400) %> - </blockquote> - </div> - <div id="comment_<%=comment.id%>" class="accordion-body collapse"> - <table class="table table-striped table-condensed"> - <tbody> - <tr> - <td colspan="2"> - By <%= user_both_links(comment.user) %> - </td> - </tr> - <% comment.for_admin_column do |name, value, type, column_name |%> - <tr> - <td> - <b><%=name%></b> - </td> - <td> - <% if column_name == 'body' && !comment.visible %> - <s><%=h comment.send(column_name) %></s> - <% else %> - <%=h comment.send(column_name) %> - <% end %> - </td> - </tr> - <% end %> - </tbody> - </table> - </div> - </div> - <% end %> - </div> -<% else %> - <p>None yet.</p> -<% end %> +<%= render :partial => 'admin_request/some_annotations' , :locals => { :comments => @info_request.comments } %> + <hr> <h2>Mail server delivery logs</h2> diff --git a/app/views/admin_user/show.html.erb b/app/views/admin_user/show.html.erb index c93c08e50..6d12aeff5 100644 --- a/app/views/admin_user/show.html.erb +++ b/app/views/admin_user/show.html.erb @@ -91,6 +91,12 @@ <hr> +<h2>Annotations</h2> + +<%= render :partial => 'admin_request/some_annotations' , :locals => { :comments => @admin_user.comments } %> + +<hr> + <h2>Censor rules</h2> <%= render :partial => 'admin_censor_rule/show', :locals => { :censor_rules => @admin_user.censor_rules, :user => @admin_user } %> diff --git a/app/views/contact_mailer/add_public_body.text.erb b/app/views/contact_mailer/add_public_body.text.erb new file mode 100644 index 000000000..5baa1fa1a --- /dev/null +++ b/app/views/contact_mailer/add_public_body.text.erb @@ -0,0 +1,19 @@ +<%= _("{{user_name}} would like a new authority added to {{site_name}}", :user_name => @change_request.get_user_name, :site_name => site_name) %> + +<%= _("Authority:") %> +<%= @change_request.get_public_body_name %> + +<%= _("Email:") %> +<%= @change_request.public_body_email %> + +<%= _("Source:") %> +<%= @change_request.source_url %> + +<%= _("Notes:") %> +<%= @change_request.notes %> + +<%= _('Add the authority:') %> +<%= admin_body_new_url(:change_request_id => @change_request.id, :only_path => false ) %> + +<%= _('Close the request and respond:') %> +<%= admin_change_request_edit_url(:id => @change_request.id, :only_path => false ) %> diff --git a/app/views/contact_mailer/update_public_body_email.text.erb b/app/views/contact_mailer/update_public_body_email.text.erb new file mode 100644 index 000000000..7d5a3dae0 --- /dev/null +++ b/app/views/contact_mailer/update_public_body_email.text.erb @@ -0,0 +1,16 @@ +<%= _("{{user_name}} would like the email address for {{public_body_name}} to be updated", :user_name => @change_request.get_user_name, :public_body_name => @change_request.get_public_body_name) %> + +<%= _("Email:") %> +<%= @change_request.public_body_email %> + +<%= _("Source:") %> +<%= @change_request.source_url %> + +<%= _("Notes:") %> +<%= @change_request.notes %> + +<%= _('Update the address:') %> +<%= admin_body_edit_path(@change_request.public_body, :change_request_id => @change_request.id, :only_path => false) %> + +<%= _('Close the request and respond:') %> +<%= admin_change_request_edit_url(:id => @change_request.id, :only_path => false ) %> diff --git a/app/views/general/_localised_datepicker.html.erb b/app/views/general/_localised_datepicker.html.erb index ec6593ea0..ee3206957 100644 --- a/app/views/general/_localised_datepicker.html.erb +++ b/app/views/general/_localised_datepicker.html.erb @@ -1,16 +1,16 @@ <script type="text/javascript"> $(function() { $(".use-datepicker").datepicker( - {closeText: '<%= _("Done") %>', - prevText: '<%= _("Prev") %>', - nextText: '<%= _("Next") %>', - currentText: '<%= _("Today") %>', + {closeText: '<%= j _("Done") %>', + prevText: '<%= j _("Prev") %>', + nextText: '<%= j _("Next") %>', + currentText: '<%= j _("Today") %>', monthNames: <%= raw I18n.translate('date.month_names')[1..-1].to_json %>, monthNamesShort: <%= raw I18n.translate('date.abbr_month_names')[1..-1].to_json %>, dayNames: <%= raw I18n.translate('date.day_names').to_json %>, dayNamesShort: <%= raw I18n.translate('date.abbr_day_names').to_json %>, dayNamesMin: <%= raw I18n.translate('date.abbr_day_names').collect{|x| x[0..0]}.to_json %>, - weekHeader: '<%= _("Wk") %>', + weekHeader: '<%= j _("Wk") %>', dateFormat: '<%= I18n.translate('date.formats.default').sub("%Y", "yy").sub("%m", "mm").sub("%d", "dd").gsub("-", "/") %>'} ); }); diff --git a/app/views/help/api.html.erb b/app/views/help/api.html.erb index df7bb30b6..c6488f93e 100644 --- a/app/views/help/api.html.erb +++ b/app/views/help/api.html.erb @@ -71,7 +71,15 @@ </p> </dd> - </dl> + + + <dt> 5. Write API </dt> + <dd> + <p> + The write API is designed to be used by authorities to create their own requests in the system. The API is currently used by mySociety's <a href="https://github.com/mysociety/foi-register">FOI Register software</a> to support using Alaveteli as a disclosure log for all FOI activity at a particular public body. More technical information about the write API is available on the <a href="https://github.com/mysociety/alaveteli/wiki/API#write-api">Alaveteli wiki</a>. + </p> + </dd> +</dl> <p>Please <a href="<%= help_contact_path %>">contact us</a> if you need an API feature that isn't there yet. It's very much a work in progress, and we do add things when people ask us to.</p> diff --git a/app/views/help/privacy.html.erb b/app/views/help/privacy.html.erb index 8e5293892..8ee7da385 100644 --- a/app/views/help/privacy.html.erb +++ b/app/views/help/privacy.html.erb @@ -84,6 +84,11 @@ ask a friend to. We don't have the resources to do this for everyone. </dd> +<dt id="anonymous">Why are there anonymous requests on the site? <a href="#anonymous">#</a> </dt> +<dd> +Some public authorities are using mySociety's <a href="https://github.com/mysociety/foi-register">FOI Register</a> software in order to use WhatDoTheyKnow as a disclosure log for all their FOI activity. When people make requests to the authority their names will usually be withheld from publication just as they would in an authority disclosure log on an authority website. +</dd> + <dt id="full_address">They've asked for my postal address! <a href="#full_address">#</a> </dt> <dd> diff --git a/app/views/help/requesting.html.erb b/app/views/help/requesting.html.erb index e7cfdd199..9de995435 100644 --- a/app/views/help/requesting.html.erb +++ b/app/views/help/requesting.html.erb @@ -30,7 +30,7 @@ <dt id="missing_body">You're missing the public authority that I want to request from! <a href="#missing_body">#</a> </dt> <dd> - <p>Please <a href="<%= help_contact_path %>">contact us</a> with the name of the public authority and, + <p>Please <a href="<%= new_change_request_path %>">contact us</a> with the name of the public authority and, if you can find it, their contact email address for Freedom of Information requests. </p> <p>If you'd like to help add a whole category of public authority to the site, we'd love diff --git a/app/views/outgoing_mailer/initial_request.text.erb b/app/views/outgoing_mailer/initial_request.text.erb index 5c418ecc7..b0f1dc9e8 100644 --- a/app/views/outgoing_mailer/initial_request.text.erb +++ b/app/views/outgoing_mailer/initial_request.text.erb @@ -6,7 +6,7 @@ <%= @info_request.incoming_email %> <%= _('Is {{email_address}} the wrong address for {{type_of_request}} requests to {{public_body_name}}? If so, please contact us using this form:', :email_address => @info_request.public_body.request_email, :type_of_request => @info_request.law_used_full, :public_body_name => @info_request.public_body.name)%> -<%= help_contact_url %> +<%= new_change_request_url(:body => @info_request.public_body.url_name) %> <%= render :partial => 'followup_footer' %> diff --git a/app/views/public_body/show.html.erb b/app/views/public_body/show.html.erb index b35e29eea..c36396149 100644 --- a/app/views/public_body/show.html.erb +++ b/app/views/public_body/show.html.erb @@ -32,6 +32,7 @@ <% end %> <% end %> <%= link_to _('View FOI email address'), view_public_body_email_path(@public_body.url_name) %><br> + <%= link_to _("Ask us to update FOI email"), new_change_request_path(:body => @public_body.url_name) %><br> </div> <div id="header_left"> diff --git a/app/views/public_body_change_requests/new.html.erb b/app/views/public_body_change_requests/new.html.erb new file mode 100644 index 000000000..7079cd868 --- /dev/null +++ b/app/views/public_body_change_requests/new.html.erb @@ -0,0 +1,61 @@ +<h1><%= @title %></h1> +<%= foi_error_messages_for :change_request %> + <%= form_for(@change_request, :url => change_request_path) do |f| %> +<% if not @user %> + <p> + <label class="form_label" for="user_name"> + <%= _("Your name:") %> + </label> + <%= f.text_field :user_name %> + <%= _('(or <a href="{{url}}">sign in</a>)', :url => signin_path(:r => request.fullpath)) %> + </p> + + <p> + <label class="form_label" for="user_email"> + <%= ("Your email:") %> + </label> + <%= f.text_field :user_email %> + </p> +<% end %> +<% if @change_request.public_body %> + <%= f.hidden_field :public_body_id %> +<% else %> + <p> + <label class="form_label" for="public_body_name"> + <%= _("Authority:") %> + </label> + <%= f.text_field :public_body_name %> + </p> +<% end %> + <p> + <label class="form_label" for="public_body_email"> + <%= _("Authority email:") %> + </label> + <%= f.text_field :public_body_email %> + <div class="form_item_note"> + <%= _("The contact email address for FOI requests to the authority.") %> + </div> + </p> + + <p> + <label class="form_label" for="source_url"> + <%= _("Source URL:") %> + </label> + <%= f.text_field :source_url %> + <div class="form_item_note"> + <%= _("The URL where you found the email address. This field is optional, but it would help us a lot if you can provide a link to a specific page on the authority's website that gives this address, as it will make it much easier for us to check.") %> + </div> + </p> + + <p> + <label class="form_label" for="notes"> + <%= _("Notes:") %> + </label> + <%= f.text_area :notes, :rows => 10, :cols => 60 %> + </p> + + <div class="form_button"> + <%= submit_tag _("Submit request") %> + </div> + +<% end %> diff --git a/app/views/reports/new.html.erb b/app/views/reports/new.html.erb index 7d558ab4e..1197b2255 100644 --- a/app/views/reports/new.html.erb +++ b/app/views/reports/new.html.erb @@ -4,17 +4,17 @@ <p><%= _("This request has already been reported for administrator attention") %></p> <% else %> <p> - Reporting a request notifies the site administrators. They will respond as soon as possible. + <%= _("Reporting a request notifies the site administrators. They will respond as soon as possible.") %> </p> - <p>Why specifically do you consider this request unsuitable?</p> + <p><%= _("Why specifically do you consider this request unsuitable?") %></p> <%= form_tag request_report_path(:request_id => @info_request.url_title) do %> <p> <label class="form_label" for="reason">Reason:</label> - <%= select_tag :reason, options_for_select(@info_request.report_reasons, @reason), :prompt => "Choose a reason" %> + <%= select_tag :reason, options_for_select(@info_request.report_reasons, @reason), :prompt => _("Choose a reason") %> </p> <p> - <label class="form_label" for="message">Please tell us more:</label> + <label class="form_label" for="message"><%= _("Please tell us more:") %></label> <%= text_area_tag :message, @message, :rows => 10, :cols => 60 %> </p> diff --git a/app/views/request/_list_results.html.erb b/app/views/request/_list_results.html.erb new file mode 100644 index 000000000..4da042816 --- /dev/null +++ b/app/views/request/_list_results.html.erb @@ -0,0 +1,12 @@ + <% @results = InfoRequest.request_list(@filters, @page, @per_page, @max_results) %> + <% if @results[:results].empty? %> + <p> <%= _('No requests of this sort yet.')%></p> + <% else %> + <h2 class="foi_results"><%= _('{{count}} FOI requests found', :count => @results[:matches_estimated]) %></h2> + <div class="results_block"> + <% @results[:results].each do |result| %> + <%= render :partial => 'request/request_listing_via_event', :locals => { :event => result, :info_request => result.info_request } %> + <% end %> + </div> + <% end %> + <%= will_paginate WillPaginate::Collection.new(@page, @per_page, @results[:show_no_more_than]) %> diff --git a/app/views/request/list.html.erb b/app/views/request/list.html.erb index 062b77c3e..a465f03ba 100644 --- a/app/views/request/list.html.erb +++ b/app/views/request/list.html.erb @@ -14,21 +14,11 @@ <div style="clear:both"></div> <div class="results_section"> - <% # TODO: Cache for 5 minutes %> - <% if @list_results.empty? %> - <p> <%= _('No requests of this sort yet.')%></p> - <% else %> - <h2 class="foi_results"><%= _('{{count}} FOI requests found', :count => @matches_estimated) %></h2> - <div class="results_block"> - <% for result in @list_results%> - <% if result.class.to_s == 'InfoRequestEvent' %> - <%= render :partial => 'request/request_listing_via_event', :locals => { :event => result, :info_request => result.info_request } %> - <% else %> - <p><strong><%= _('Unexpected search result type') %> <%=result.class.to_s%></strong></p> - <% end %> - <% end %> - </div> + <% if key = request_list_cache_key %> + <% cache_if_caching_fragments(key, :expires_in => 5.minutes) do %> + <%= render :partial => 'list_results' %> <% end %> - - <%= will_paginate WillPaginate::Collection.new(@page, @per_page, @show_no_more_than) %> + <% else %> + <%= render :partial => 'list_results' %> + <% end %> </div> diff --git a/app/views/request/show.html.erb b/app/views/request/show.html.erb index c520ce40c..153b0b861 100644 --- a/app/views/request/show.html.erb +++ b/app/views/request/show.html.erb @@ -34,14 +34,16 @@ <p class="subtitle"> <% if !@user.nil? && @user.admin_page_links? %> <%= _('{{user}} ({{user_admin_link}}) made this {{law_used_full}} request (<a href="{{request_admin_url}}">admin</a>) to {{public_body_link}} (<a href="{{public_body_admin_url}}">admin</a>)', - :user => @info_request.is_external? ? (@info_request.user_name || _('An anonymous user')) : user_link(@info_request.user), + :user => request_user_link(@info_request, _('An anonymous user')), :law_used_full => h(@info_request.law_used_full), :user_admin_link => user_admin_link_for_request(@info_request, _('external'), _('admin')), :request_admin_url => admin_request_show_url(@info_request), :public_body_link => public_body_link(@info_request.public_body), :public_body_admin_url => admin_body_show_url(@info_request.public_body)) %> <% else %> - <%= _('{{user}} made this {{law_used_full}} request',:user=>@info_request.is_external? ? (@info_request.user_name || _('An anonymous user')) : user_link(@info_request.user), :law_used_full=>h(@info_request.law_used_full)) %> + <%= _('{{user}} made this {{law_used_full}} request', + :user=>request_user_link(@info_request, _('An anonymous user')), + :law_used_full=>h(@info_request.law_used_full)) %> <%= _('to {{public_body}}',:public_body=>public_body_link(@info_request.public_body)) %> <% end %> </p> diff --git a/app/views/user/sign.html.erb b/app/views/user/sign.html.erb index 8291cdace..e8c5d5a58 100644 --- a/app/views/user/sign.html.erb +++ b/app/views/user/sign.html.erb @@ -13,7 +13,7 @@ </p> <% if @post_redirect.post_params["controller"] == "admin_general" %> <% unless AlaveteliConfiguration::disable_emergency_user %> - <p id="superuser_message">Don't have a superuser account yet? <%= link_to "Sign in as the emergency user", @post_redirect.uri + "?emergency=1" %></p> + <p id="superuser_message"><%= _("Don't have a superuser account yet?") %> <%= link_to _("Sign in as the emergency user"), @post_redirect.uri + "?emergency=1" %></p> <% end %> <% end %> diff --git a/app/views/user/wall.html.erb b/app/views/user/wall.html.erb index 190cc0a6d..6699c55fa 100644 --- a/app/views/user/wall.html.erb +++ b/app/views/user/wall.html.erb @@ -1,16 +1,19 @@ <% @title = h(@display_user.name) + _(" - wall") %> -<% if @is_you %> <div class="medium_column"> - <p><%= _('You can change the requests and users you are following on <a href="{{profile_url}}">your profile page</a>.', :profile_url => show_user_profile_path) %> - <%= render :partial => 'change_receive_email' %> + <% if @is_you %> + <h2><%= _("My wall") %></h2> + <p><%= _('You can change the requests and users you are following on <a href="{{profile_url}}">your profile page</a>.', :profile_url => show_user_profile_path) %></p> + <%= render :partial => 'change_receive_email' %> + <% else %> + <h2><%= _("This is <a href=\"{{profile_url}}\">{{user_name}}'s</a> wall", :profile_url => show_user_profile_path, :user_name => h(@display_user.name)) %></h2> + <% end %> </div> -<% end %> <div id="user_profile_search"> - <% if !@feed_results.nil? %> - <% for result in @feed_results %> - <%= render :partial => 'request/wall_listing', :locals => { :event => result, :info_request => result.info_request } %> - <% end %> - <% end %> - - + <% if !@feed_results.nil? and !@feed_results.empty? %> + <% for result in @feed_results %> + <%= render :partial => 'request/wall_listing', :locals => { :event => result, :info_request => result.info_request } %> + <% end %> + <% else %> + <p><%= _("There is nothing to display yet.") %></p> + <% end %> </div> diff --git a/commonlib b/commonlib -Subproject ad27f5409ef3ed1b800aa08c1d70a018443dcfd +Subproject 438003985e1bfb90fb83f5bbc5dce3da3fb21ee diff --git a/config/application.rb b/config/application.rb index 3c749a531..cbc7b9413 100644 --- a/config/application.rb +++ b/config/application.rb @@ -77,6 +77,12 @@ module Alaveteli require "#{Rails.root}/lib/whatdotheyknow/strip_empty_sessions" config.middleware.insert_before ::ActionDispatch::Cookies, WhatDoTheyKnow::StripEmptySessions, :key => '_wdtk_cookie_session', :path => "/", :httponly => true + # Allow the generation of full URLs in emails + config.action_mailer.default_url_options = { :host => AlaveteliConfiguration::domain } + if AlaveteliConfiguration::force_ssl + config.action_mailer.default_url_options[:protocol] = "https" + end + # Enable the asset pipeline config.assets.enabled = true diff --git a/config/initializers/alaveteli.rb b/config/initializers/alaveteli.rb index cabe96efa..6df1823a5 100644 --- a/config/initializers/alaveteli.rb +++ b/config/initializers/alaveteli.rb @@ -51,6 +51,7 @@ require 'alaveteli_file_types' require 'alaveteli_localization' require 'message_prominence' require 'theme' +require 'xapian_queries' AlaveteliLocalization.set_locales(AlaveteliConfiguration::available_locales, AlaveteliConfiguration::default_locale) diff --git a/config/routes.rb b/config/routes.rb index cadb7ec54..27311fab7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -108,6 +108,8 @@ Alaveteli::Application.routes.draw do match '/body_statistics' => 'public_body#statistics', :as => :public_bodies_statistics #### + resource :change_request, :only => [:new, :create], :controller => 'public_body_change_requests' + #### Comment controller match '/annotate/request/:url_title' => 'comment#new', :as => :new_comment, :type => 'request' #### @@ -172,6 +174,11 @@ Alaveteli::Application.routes.draw do match '/admin/body/mass_tag_add' => 'admin_public_body#mass_tag_add', :as => :admin_body_mass_tag_add #### + #### AdminPublicBodyChangeRequest controller + match '/admin/change_request/edit/:id' => 'admin_public_body_change_requests#edit', :as => :admin_change_request_edit + match '/admin/change_request/update/:id' => 'admin_public_body_change_requests#update', :as => :admin_change_request_update + #### + #### AdminGeneral controller match '/admin' => 'admin_general#index', :as => :admin_general_index match '/admin/timeline' => 'admin_general#timeline', :as => :admin_timeline diff --git a/db/migrate/20131211152641_create_public_body_change_requests.rb b/db/migrate/20131211152641_create_public_body_change_requests.rb new file mode 100644 index 000000000..e3fb560a6 --- /dev/null +++ b/db/migrate/20131211152641_create_public_body_change_requests.rb @@ -0,0 +1,20 @@ +class CreatePublicBodyChangeRequests < ActiveRecord::Migration + def up + create_table :public_body_change_requests do |t| + t.column :user_email, :string + t.column :user_name, :string + t.column :user_id, :integer + t.column :public_body_name, :text + t.column :public_body_id, :integer + t.column :public_body_email, :string + t.column :source_url, :text + t.column :notes, :text + t.column :is_open, :boolean, :null => false, :default => true + t.timestamps + end + end + + def down + drop_table :public_body_change_requests + end +end diff --git a/doc/CHANGES.md b/doc/CHANGES.md index 8f86f1c3a..70571dbfa 100644 --- a/doc/CHANGES.md +++ b/doc/CHANGES.md @@ -1,3 +1,19 @@ +# rails-3-develop + +## Highlighted features + +* There is a new form for users to request that a new authority should + be added, or to request an update to the contact email used for an + authority. Site admins are emailed about these requests, and can + resolve them from the admin interface. + +## Upgrade notes + +* If your theme overrides the help/requesting template, you should + update the link in the section on requesting new authorities so the + link points to `<%= new_change_request_path %>` instead of `<%= + help_contact_path %>`. + # Version 0.16 ## Highlighted features diff --git a/lib/alaveteli_external_command.rb b/lib/alaveteli_external_command.rb index fbdee8a62..086a461c8 100644 --- a/lib/alaveteli_external_command.rb +++ b/lib/alaveteli_external_command.rb @@ -44,7 +44,11 @@ module AlaveteliExternalCommand end xc.run(opts[:stdin_string] || "", opts[:env] || {}) - if xc.status != 0 + if !xc.exited + # Crash or timeout + $stderr.puts("#{program_name} #{args.join(' ')}:exited abnormally") + return nil + elsif xc.status != 0 # Error $stderr.puts("Error from #{program_name} #{args.join(' ')}:") $stderr.print(xc.err) diff --git a/lib/tasks/translation.rake b/lib/tasks/translation.rake index 6458d9268..b1f9d0b71 100644 --- a/lib/tasks/translation.rake +++ b/lib/tasks/translation.rake @@ -66,9 +66,10 @@ namespace :translation do 'Hello!') write_email(user_contact_email, 'Contact email (user to user)', output_file) - admin_contact_email = ContactMailer.from_admin_message(info_request.user, - 'A test message', - 'Hello!') + admin_contact_email = ContactMailer.from_admin_message(info_request.user.name, + info_request.user.email, + 'A test message', + 'Hello!') write_email(admin_contact_email, 'Contact email (admin to user)', output_file) # request mailer diff --git a/lib/xapian_queries.rb b/lib/xapian_queries.rb new file mode 100644 index 000000000..b3599740a --- /dev/null +++ b/lib/xapian_queries.rb @@ -0,0 +1,85 @@ +module XapianQueries + + # These methods take some filter criteria expressed in a hash and convert them + # into a xapian query referencing the terms and values stored by InfoRequestEvent. + # Note that the params are request params and may contain irrelevant keys + + def get_request_variety_from_params(params) + query = "" + sortby = "newest" + varieties = [] + if params[:request_variety] && !(query =~ /variety:/) + if params[:request_variety].include? "sent" + varieties -= ['variety:sent', 'variety:followup_sent', 'variety:response', 'variety:comment'] + varieties << ['variety:sent', 'variety:followup_sent'] + end + if params[:request_variety].include? "response" + varieties << ['variety:response'] + end + if params[:request_variety].include? "comment" + varieties << ['variety:comment'] + end + end + if !varieties.empty? + query = " (#{varieties.join(' OR ')})" + end + return query + end + + def get_status_from_params(params) + query = "" + if params[:latest_status] + statuses = [] + if params[:latest_status].class == String + params[:latest_status] = [params[:latest_status]] + end + if params[:latest_status].include?("recent") || params[:latest_status].include?("all") + query += " (variety:sent OR variety:followup_sent OR variety:response OR variety:comment)" + end + if params[:latest_status].include? "successful" + statuses << ['latest_status:successful', 'latest_status:partially_successful'] + end + if params[:latest_status].include? "unsuccessful" + statuses << ['latest_status:rejected', 'latest_status:not_held'] + end + if params[:latest_status].include? "awaiting" + statuses << ['latest_status:waiting_response', 'latest_status:waiting_clarification', 'waiting_classification:true', 'latest_status:internal_review','latest_status:gone_postal', 'latest_status:error_message', 'latest_status:requires_admin'] + end + if params[:latest_status].include? "internal_review" + statuses << ['status:internal_review'] + end + if params[:latest_status].include? "other" + statuses << ['latest_status:gone_postal', 'latest_status:error_message', 'latest_status:requires_admin', 'latest_status:user_withdrawn'] + end + if params[:latest_status].include? "gone_postal" + statuses << ['latest_status:gone_postal'] + end + if !statuses.empty? + query = " (#{statuses.join(' OR ')})" + end + end + return query + end + + def get_date_range_from_params(params) + query = "" + if params.has_key?(:request_date_after) && !params.has_key?(:request_date_before) + params[:request_date_before] = Time.now.strftime("%d/%m/%Y") + query += " #{params[:request_date_after]}..#{params[:request_date_before]}" + elsif !params.has_key?(:request_date_after) && params.has_key?(:request_date_before) + params[:request_date_after] = "01/01/2001" + end + if params.has_key?(:request_date_after) + query = " #{params[:request_date_after]}..#{params[:request_date_before]}" + end + return query + end + + def make_query_from_params(params) + query = params[:query] || "" if query.nil? + query += get_date_range_from_params(params) + query += get_request_variety_from_params(params) + query += get_status_from_params(params) + return query + end +end diff --git a/locale/cy/app.po b/locale/cy/app.po index 4a82d1f66..88f03a98c 100644 --- a/locale/cy/app.po +++ b/locale/cy/app.po @@ -20,7 +20,7 @@ msgstr "" "Project-Id-Version: alaveteli\n" "Report-Msgid-Bugs-To: \n" "POT-Creation-Date: 2013-11-08 12:10+0000\n" -"PO-Revision-Date: 2014-01-24 09:25+0000\n" +"PO-Revision-Date: 2014-01-27 11:01+0000\n" "Last-Translator: louisecrow <louise@mysociety.org>\n" "Language-Team: Welsh (http://www.transifex.com/projects/p/alaveteli/language/cy/)\n" "Language: cy\n" @@ -1273,7 +1273,7 @@ msgid "MailServerLog|Order" msgstr "MailServerLog|Archeb" msgid "Make a new<br/>\\n <strong>Freedom <span>of</span><br/>\\n Information<br/>\\n request</strong>" -msgstr "Gwnewch <strong>Cais Rhyddid Gwybodaeth</strong> newydd" +msgstr "Gwnewch <strong>Cais<br/>Rhyddid<br/>Gwybodaeth</strong><br/>newydd" msgid "Make a request" msgstr "Gwneud cais" @@ -2999,7 +2999,7 @@ msgid "You may be able to find\\n one on their website, or by phoning them up msgstr "Efallai y byddwch yn gallu dod o hyd i un ar eu gwefan, neu drwy eu ffonio nhw a gofyn. Os llwyddwch i ddod o hyd i un, <a href=\"{{url}}\"> anfonwch e atom</a>." msgid "You may be able to find\\none on their website, or by phoning them up and asking. If you manage\\nto find one, then please <a href=\"{{help_url}}\">send it to us</a>." -msgstr "Efallai y byddwch yn gallu dod o hyd i un ar eu gwefan, neu drwy eu ffonio nhw a gofyn. Os llwyddwch i ddod o hyd i un, <a href=\"{{url}}\"> anfonwch e atom</a>." +msgstr "Efallai y byddwch yn gallu dod o hyd i un ar eu gwefan, neu drwy eu ffonio nhw a gofyn. Os llwyddwch i ddod o hyd i un, <a href=\"{{help_url}}\"> anfonwch e atom</a>." msgid "You need to be logged in to change the text about you on your profile." msgstr "Rhaid i chi fod wedi mewngofnodi i newid y testun amdanoch chi ar eich proffil." diff --git a/script/site-specific-install.sh b/script/site-specific-install.sh index 8917fd577..7d47a5990 100755 --- a/script/site-specific-install.sh +++ b/script/site-specific-install.sh @@ -66,6 +66,11 @@ ensure_line_present \ /etc/postfix/main.cf 644 ensure_line_present \ + "^ *myhostname *=" \ + "myhostname = $(hostname --fqdn)" \ + /etc/postfix/main.cf 644 + +ensure_line_present \ "^do-not-reply" \ "do-not-reply-to-this-address: :blackhole:" \ /etc/aliases 644 diff --git a/spec/controllers/admin_public_body_change_requests_controller_spec.rb b/spec/controllers/admin_public_body_change_requests_controller_spec.rb new file mode 100644 index 000000000..b478e851d --- /dev/null +++ b/spec/controllers/admin_public_body_change_requests_controller_spec.rb @@ -0,0 +1,35 @@ +# -*- coding: utf-8 -*- +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +describe AdminPublicBodyChangeRequestsController, "editing a change request" do + + it "should render the edit template" do + change_request = FactoryGirl.create(:add_body_request) + get :edit, :id => change_request.id + response.should render_template("edit") + end + +end + +describe AdminPublicBodyChangeRequestsController, 'updating a change request' do + + before do + @change_request = FactoryGirl.create(:add_body_request) + post :update, { :id => @change_request.id, + :response => 'Thanks but no', + :subject => 'Your request' } + end + + it 'should close the change request' do + PublicBodyChangeRequest.find(@change_request.id).is_open.should == false + end + + it 'should send a response email to the user who requested the change' do + deliveries = ActionMailer::Base.deliveries + deliveries.size.should == 1 + mail = deliveries[0] + mail.subject.should == 'Your request' + mail.to.should == [@change_request.get_user_email] + mail.body.should =~ /Thanks but no/ + end +end diff --git a/spec/controllers/admin_public_body_controller_spec.rb b/spec/controllers/admin_public_body_controller_spec.rb index fe5087d7c..095d23245 100644 --- a/spec/controllers/admin_public_body_controller_spec.rb +++ b/spec/controllers/admin_public_body_controller_spec.rb @@ -1,6 +1,6 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') -describe AdminPublicBodyController, "when administering public bodies" do +describe AdminPublicBodyController, "when showing the index of public bodies" do render_views it "shows the index page" do @@ -12,28 +12,255 @@ describe AdminPublicBodyController, "when administering public bodies" do assigns[:public_bodies].should == [ public_bodies(:humpadink_public_body) ] end + it "searches for 'humpa' in another locale" do + get :index, {:query => "humpa", :locale => "es"} + assigns[:public_bodies].should == [ public_bodies(:humpadink_public_body) ] + end + +end + +describe AdminPublicBodyController, "when showing a public body" do + render_views + it "shows a public body" do get :show, :id => 2 end - it "creates a new public body" do + it "sets a using_admin flag" do + get :show, :id => 2 + session[:using_admin].should == 1 + end + + it "shows a public body in another locale" do + get :show, {:id => 2, :locale => "es" } + end + +end + +describe AdminPublicBodyController, 'when showing the form for a new public body' do + + it 'should assign a new public body to the view' do + get :new + assigns[:public_body].should be_a(PublicBody) + end + + context 'when passed a change request id as a param' do + render_views + + it 'should populate the name, email address and last edit comment on the public body' do + change_request = FactoryGirl.create(:add_body_request) + get :new, :change_request_id => change_request.id + assigns[:public_body].name.should == change_request.public_body_name + assigns[:public_body].request_email.should == change_request.public_body_email + assigns[:public_body].last_edit_comment.should match('Notes: Please') + end + + it 'should assign a default response text to the view' do + change_request = FactoryGirl.create(:add_body_request) + get :new, :change_request_id => change_request.id + assigns[:change_request_user_response].should match("Thanks for your suggestion to add A New Body") + end + end + +end + +describe AdminPublicBodyController, "when creating a public body" do + render_views + + it "creates a new public body in one locale" do + n = PublicBody.count + 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 == n + 1 + + body = PublicBody.find_by_name("New Quango") + response.should redirect_to(:controller=>'admin_public_body', :action=>'show', :id=>body.id) + end + + it "creates a new public body with multiple locales" do n = PublicBody.count - post :create, { :public_body => { :name => "New Quango", :short_name => "", :tag_string => "blah", :request_email => 'newquango@localhost', :last_edit_comment => 'From test code' } } + post :create, { + :public_body => { :name => "New Quango", + :short_name => "", + :tag_string => "blah", + :request_email => 'newquango@localhost', + :last_edit_comment => 'From test code', + :translated_versions => [{ :locale => "es", + :name => "Mi Nuevo Quango", + :short_name => "", + :request_email => 'newquango@localhost' }] + } + } PublicBody.count.should == n + 1 + + body = PublicBody.find_by_name("New Quango") + body.translations.map {|t| t.locale.to_s}.sort.should == ["en", "es"] + I18n.with_locale(:en) do + body.name.should == "New Quango" + body.url_name.should == "new_quango" + body.first_letter.should == "N" + end + I18n.with_locale(:es) do + body.name.should == "Mi Nuevo Quango" + body.url_name.should == "mi_nuevo_quango" + body.first_letter.should == "M" + end + + response.should redirect_to(:controller=>'admin_public_body', :action=>'show', :id=>body.id) + end + + context 'when the body is being created as a result of a change request' do + + before do + @change_request = FactoryGirl.create(:add_body_request) + post :create, { :public_body => { :name => "New Quango", + :short_name => "", + :tag_string => "blah", + :request_email => 'newquango@localhost', + :last_edit_comment => 'From test code' }, + :change_request_id => @change_request.id, + :subject => 'Adding a new body', + :response => 'The URL will be [Authority URL will be inserted here]'} + end + + it 'should send a response to the requesting user' do + deliveries = ActionMailer::Base.deliveries + deliveries.size.should == 1 + mail = deliveries[0] + mail.subject.should == 'Adding a new body' + mail.to.should == [@change_request.get_user_email] + mail.body.should =~ /The URL will be http:\/\/test.host\/body\/new_quango/ + end + + it 'should mark the change request as closed' do + PublicBodyChangeRequest.find(@change_request.id).is_open.should be_false + end + end +end + +describe AdminPublicBodyController, "when editing a public body" do + render_views + it "edits a public body" do get :edit, :id => 2 end + it "edits a public body in another locale" do + get :edit, {:id => 3, :locale => :en} + + # When editing a body, the controller returns all available translations + assigns[:public_body].find_translation_by_locale("es").name.should == 'El Department for Humpadinking' + assigns[:public_body].name.should == 'Department for Humpadinking' + response.should render_template('edit') + end + + context 'when passed a change request id as a param' do + render_views + + before do + @change_request = FactoryGirl.create(:update_body_request) + get :edit, :id => @change_request.public_body_id, :change_request_id => @change_request.id + end + + it 'should populate the email address and last edit comment on the public body' do + change_request = FactoryGirl.create(:update_body_request) + get :edit, :id => change_request.public_body_id, :change_request_id => change_request.id + assigns[:public_body].request_email.should == @change_request.public_body_email + assigns[:public_body].last_edit_comment.should match('Notes: Please') + end + + it 'should assign a default response text to the view' do + assigns[:change_request_user_response].should match("Thanks for your suggestion to update the email address") + end + end + +end + +describe AdminPublicBodyController, "when updating a public body" do + render_views + it "saves edits to a public body" do public_bodies(:humpadink_public_body).name.should == "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' } } + post :update, { :id => 3, :public_body => { :name => "Renamed", + :short_name => "", + :tag_string => "some tags", + :request_email => 'edited@localhost', + :last_edit_comment => 'From test code' } } request.flash[:notice].should include('successful') pb = PublicBody.find(public_bodies(:humpadink_public_body).id) pb.name.should == "Renamed" end + it "saves edits to a public body in another locale" do + I18n.with_locale(:es) do + pb = PublicBody.find(id=3) + pb.name.should == "El Department for Humpadinking" + post :update, { + :id => 3, + :public_body => { + :name => "Department for Humpadinking", + :short_name => "", + :tag_string => "some tags", + :request_email => 'edited@localhost', + :last_edit_comment => 'From test code', + :translated_versions => { + 3 => {:locale => "es", + :name => "Renamed", + :short_name => "", + :request_email => 'edited@localhost'} + } + } + } + request.flash[:notice].should include('successful') + end + + pb = PublicBody.find(public_bodies(:humpadink_public_body).id) + I18n.with_locale(:es) do + pb.name.should == "Renamed" + end + I18n.with_locale(:en) do + pb.name.should == "Department for Humpadinking" + end + end + + context 'when the body is being updated as a result of a change request' do + + before do + @change_request = FactoryGirl.create(:update_body_request) + post :update, { :id => @change_request.public_body_id, + :public_body => { :name => "New Quango", + :short_name => "", + :request_email => 'newquango@localhost', + :last_edit_comment => 'From test code' }, + :change_request_id => @change_request.id, + :subject => 'Body update', + :response => 'Done.'} + end + + it 'should send a response to the requesting user' do + deliveries = ActionMailer::Base.deliveries + deliveries.size.should == 1 + mail = deliveries[0] + mail.subject.should == 'Body update' + mail.to.should == [@change_request.get_user_email] + mail.body.should =~ /Done./ + end + + it 'should mark the change request as closed' do + PublicBodyChangeRequest.find(@change_request.id).is_open.should be_false + end + + end +end + +describe AdminPublicBodyController, "when destroying a public body" do + render_views + it "does not destroy a public body that has associated requests" do id = public_bodies(:humpadink_public_body).id n = PublicBody.count @@ -49,10 +276,10 @@ describe AdminPublicBodyController, "when administering public bodies" do PublicBody.count.should == n - 1 end - it "sets a using_admin flag" do - get :show, :id => 2 - session[:using_admin].should == 1 - end +end + +describe AdminPublicBodyController, "when assigning public body tags" do + render_views it "mass assigns tags" do condition = "public_body_translations.locale = ?" @@ -62,81 +289,81 @@ describe AdminPublicBodyController, "when administering public bodies" do response.should redirect_to(:action=>'list') PublicBody.find_by_tag("department").count.should == n end +end - describe 'import_csv' do +describe AdminPublicBodyController, "when importing a csv" do + render_views - describe 'when handling a GET request' do + describe 'when handling a GET request' do - it 'should get the page successfully' do - get :import_csv - response.should be_success - end + it 'should get the page successfully' do + get :import_csv + response.should be_success + end + end + + describe 'when handling a POST request' do + + before do + PublicBody.stub!(:import_csv).and_return([[],[]]) + @file_object = fixture_file_upload('/files/fake-authority-type.csv') end - describe 'when handling a POST request' do + it 'should handle a nil csv file param' do + post :import_csv, { :commit => 'Dry run' } + response.should be_success + end - before do - PublicBody.stub!(:import_csv).and_return([[],[]]) - @file_object = fixture_file_upload('/files/fake-authority-type.csv') + describe 'if there is a csv file param' do + + it 'should try to get the contents and original name of a csv file param' do + @file_object.should_receive(:read).and_return('some contents') + post :import_csv, { :csv_file => @file_object, + :commit => 'Dry run'} end - it 'should handle a nil csv file param' do - post :import_csv, { :commit => 'Dry run' } - response.should be_success + it 'should assign the original filename to the view' do + post :import_csv, { :csv_file => @file_object, + :commit => 'Dry run'} + assigns[:original_csv_file].should == 'fake-authority-type.csv' end - describe 'if there is a csv file param' do + end - it 'should try to get the contents and original name of a csv file param' do - @file_object.should_receive(:read).and_return('some contents') - post :import_csv, { :csv_file => @file_object, - :commit => 'Dry run'} - end + describe 'if there is no csv file param, but there are temporary_csv_file and + original_csv_file params' do - it 'should assign the original filename to the view' do - post :import_csv, { :csv_file => @file_object, - :commit => 'Dry run'} - assigns[:original_csv_file].should == 'fake-authority-type.csv' - end + it 'should try and get the file contents from a temporary file whose name + is passed as a param' do + @controller.should_receive(:retrieve_csv_data).with('csv_upload-2046-12-31-394') + post :import_csv, { :temporary_csv_file => 'csv_upload-2046-12-31-394', + :original_csv_file => 'original_contents.txt', + :commit => 'Dry run'} + end + it 'should raise an error on an invalid temp file name' do + params = { :temporary_csv_file => 'bad_name', + :original_csv_file => 'original_contents.txt', + :commit => 'Dry run'} + expected_error = "Invalid filename in upload_csv: bad_name" + lambda{ post :import_csv, params }.should raise_error(expected_error) end - describe 'if there is no csv file param, but there are temporary_csv_file and - original_csv_file params' do - - it 'should try and get the file contents from a temporary file whose name - is passed as a param' do - @controller.should_receive(:retrieve_csv_data).with('csv_upload-2046-12-31-394') - post :import_csv, { :temporary_csv_file => 'csv_upload-2046-12-31-394', - :original_csv_file => 'original_contents.txt', - :commit => 'Dry run'} - end - - it 'should raise an error on an invalid temp file name' do - params = { :temporary_csv_file => 'bad_name', - :original_csv_file => 'original_contents.txt', - :commit => 'Dry run'} - expected_error = "Invalid filename in upload_csv: bad_name" - lambda{ post :import_csv, params }.should raise_error(expected_error) - end - - it 'should raise an error if the temp file does not exist' do - temp_name = "csv_upload-20461231-394" - params = { :temporary_csv_file => temp_name, - :original_csv_file => 'original_contents.txt', - :commit => 'Dry run'} - expected_error = "Missing file in upload_csv: csv_upload-20461231-394" - lambda{ post :import_csv, params }.should raise_error(expected_error) - end - - it 'should assign the temporary filename to the view' do - post :import_csv, { :csv_file => @file_object, - :commit => 'Dry run'} - temporary_filename = assigns[:temporary_csv_file] - temporary_filename.should match(/csv_upload-#{Time.now.strftime("%Y%m%d")}-\d{1,5}/) - end + it 'should raise an error if the temp file does not exist' do + temp_name = "csv_upload-20461231-394" + params = { :temporary_csv_file => temp_name, + :original_csv_file => 'original_contents.txt', + :commit => 'Dry run'} + expected_error = "Missing file in upload_csv: csv_upload-20461231-394" + lambda{ post :import_csv, params }.should raise_error(expected_error) + end + it 'should assign the temporary filename to the view' do + post :import_csv, { :csv_file => @file_object, + :commit => 'Dry run'} + temporary_filename = assigns[:temporary_csv_file] + temporary_filename.should match(/csv_upload-#{Time.now.strftime("%Y%m%d")}-\d{1,5}/) end end @@ -266,103 +493,3 @@ describe AdminPublicBodyController, "when administering public bodies and paying end end -describe AdminPublicBodyController, "when administering public bodies with i18n" do - render_views - - it "shows the index page" do - get :index - end - - it "searches for 'humpa'" do - get :index, {:query => "humpa", :locale => "es"} - assigns[:public_bodies].should == [ public_bodies(:humpadink_public_body) ] - end - - it "shows a public body" do - get :show, {:id => 2, :locale => "es" } - end - - it "edits a public body" do - get :edit, {:id => 3, :locale => :en} - - # When editing a body, the controller returns all available translations - assigns[:public_body].find_translation_by_locale("es").name.should == 'El Department for Humpadinking' - assigns[:public_body].name.should == 'Department for Humpadinking' - response.should render_template('edit') - end - - it "saves edits to a public body" do - I18n.with_locale(:es) do - pb = PublicBody.find(id=3) - pb.name.should == "El Department for Humpadinking" - post :update, { - :id => 3, - :public_body => { - :name => "Department for Humpadinking", - :short_name => "", - :tag_string => "some tags", - :request_email => 'edited@localhost', - :last_edit_comment => 'From test code', - :translated_versions => { - 3 => {:locale => "es", :name => "Renamed",:short_name => "", :request_email => 'edited@localhost'} - } - } - } - request.flash[:notice].should include('successful') - end - - pb = PublicBody.find(public_bodies(:humpadink_public_body).id) - I18n.with_locale(:es) do - pb.name.should == "Renamed" - end - I18n.with_locale(:en) do - pb.name.should == "Department for Humpadinking" - end - end - - it "destroy a public body" do - n = PublicBody.count - post :destroy, { :id => public_bodies(:forlorn_public_body).id } - PublicBody.count.should == n - 1 - end - -end - -describe AdminPublicBodyController, "when creating public bodies with i18n" do - render_views - - it "creates a new public body in one locale" do - n = PublicBody.count - 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 == n + 1 - - body = PublicBody.find_by_name("New Quango") - response.should redirect_to(:controller=>'admin_public_body', :action=>'show', :id=>body.id) - end - - it "creates a new public body with multiple locales" do - n = PublicBody.count - post :create, { - :public_body => { - :name => "New Quango", :short_name => "", :tag_string => "blah", :request_email => 'newquango@localhost', :last_edit_comment => 'From test code', - :translated_versions => [{ :locale => "es", :name => "Mi Nuevo Quango", :short_name => "", :request_email => 'newquango@localhost' }] - } - } - PublicBody.count.should == n + 1 - - body = PublicBody.find_by_name("New Quango") - body.translations.map {|t| t.locale.to_s}.sort.should == ["en", "es"] - I18n.with_locale(:en) do - body.name.should == "New Quango" - body.url_name.should == "new_quango" - body.first_letter.should == "N" - end - I18n.with_locale(:es) do - body.name.should == "Mi Nuevo Quango" - body.url_name.should == "mi_nuevo_quango" - body.first_letter.should == "M" - end - - response.should redirect_to(:controller=>'admin_public_body', :action=>'show', :id=>body.id) - end -end diff --git a/spec/controllers/api_controller_spec.rb b/spec/controllers/api_controller_spec.rb index 2b1c515f7..6b02bd5b4 100644 --- a/spec/controllers/api_controller_spec.rb +++ b/spec/controllers/api_controller_spec.rb @@ -1,18 +1,6 @@ # coding: utf-8 require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') -def normalise_whitespace(s) - s = s.gsub(/\A\s+|\s+\Z/, "") - s = s.gsub(/\s+/, " ") - return s -end - -RSpec::Matchers.define :be_equal_modulo_whitespace_to do |expected| - match do |actual| - normalise_whitespace(actual) == normalise_whitespace(expected) - end -end - describe ApiController, "when using the API" do describe 'checking API keys' do diff --git a/spec/controllers/public_body_change_requests_controller_spec.rb b/spec/controllers/public_body_change_requests_controller_spec.rb new file mode 100644 index 000000000..7b878b893 --- /dev/null +++ b/spec/controllers/public_body_change_requests_controller_spec.rb @@ -0,0 +1,99 @@ +# -*- coding: utf-8 -*- +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +describe PublicBodyChangeRequestsController, "making a new change request" do + + it "should show the form" do + get :new + response.should render_template("new") + end + +end + +describe PublicBodyChangeRequestsController, "creating a change request" do + + context 'when handling a request for a new authority' do + + before do + @email = "test@example.com" + name = "Test User" + @change_request_params = {:user_email => @email, + :user_name => name, + :public_body_name => 'New Body', + :public_body_email => 'new_body@example.com', + :notes => 'Please', + :source => 'http://www.example.com'} + end + + it "should send an email to the site contact address" do + post :create, {:public_body_change_request => @change_request_params} + deliveries = ActionMailer::Base.deliveries + deliveries.size.should == 1 + mail = deliveries[0] + mail.subject.should =~ /Add authority - New Body/ + mail.from.should include(@email) + mail.to.should include('postmaster@localhost') + mail.body.should include('new_body@example.com') + mail.body.should include('New Body') + mail.body.should include("Please") + mail.body.should include('http://test.host/admin/body/new?change_request_id=') + mail.body.should include('http://test.host/admin/change_request/edit/') + end + + it 'should show a notice' do + post :create, {:public_body_change_request => @change_request_params} + expected_text = "Your request to add an authority has been sent. Thank you for getting in touch! We'll get back to you soon." + flash[:notice].should == expected_text + end + + it 'should redirect to the frontpage' do + post :create, {:public_body_change_request => @change_request_params} + response.should redirect_to frontpage_url + end + + end + + context 'when handling a request for an update to an existing authority' do + + before do + @email = "test@example.com" + name = "Test User" + @public_body = FactoryGirl.create(:public_body) + @change_request_params = {:user_email => @email, + :user_name => name, + :public_body_id => @public_body.id, + :public_body_email => 'new_body@example.com', + :notes => 'Please', + :source => 'http://www.example.com'} + end + + it 'should send an email to the site contact address' do + post :create, {:public_body_change_request => @change_request_params} + deliveries = ActionMailer::Base.deliveries + deliveries.size.should == 1 + mail = deliveries[0] + mail.subject.should =~ /Update email address - #{@public_body.name}/ + mail.from.should include(@email) + mail.to.should include('postmaster@localhost') + mail.body.should include('new_body@example.com') + mail.body.should include(@public_body.name) + mail.body.should include("Please") + mail.body.should include("http://test.host/admin/body/edit/#{@public_body.id}?change_request_id=") + mail.body.should include('http://test.host/admin/change_request/edit/') + end + + it 'should show a notice' do + post :create, {:public_body_change_request => @change_request_params} + expected_text = "Your request to update the address for #{@public_body.name} has been sent. Thank you for getting in touch! We'll get back to you soon." + flash[:notice].should == expected_text + end + + it 'should redirect to the frontpage' do + post :create, {:public_body_change_request => @change_request_params} + response.should redirect_to frontpage_url + end + + end + + +end diff --git a/spec/controllers/request_controller_spec.rb b/spec/controllers/request_controller_spec.rb index def9dfc7e..23f19f389 100644 --- a/spec/controllers/request_controller_spec.rb +++ b/spec/controllers/request_controller_spec.rb @@ -17,92 +17,6 @@ describe RequestController, "when listing recent requests" do response.should render_template('list') end - it "should filter requests" do - get :list, :view => 'all' - assigns[:list_results].map(&:info_request).should =~ InfoRequest.all - - # default sort order is the request with the most recently created event first - assigns[:list_results].map(&:info_request).should == InfoRequest.all( - :order => "(select max(info_request_events.created_at) from info_request_events where info_request_events.info_request_id = info_requests.id) DESC") - - get :list, :view => 'successful' - assigns[:list_results].map(&:info_request).should =~ InfoRequest.all( - :conditions => "id in ( - select info_request_id - from info_request_events - where not exists ( - select * - from info_request_events later_events - where later_events.created_at > info_request_events.created_at - and later_events.info_request_id = info_request_events.info_request_id - and later_events.described_state is not null - ) - and info_request_events.described_state in ('successful', 'partially_successful') - )") - end - - it "should filter requests by date" do - # The semantics of the search are that it finds any InfoRequest - # that has any InfoRequestEvent created in the specified range - - get :list, :view => 'all', :request_date_before => '13/10/2007' - assigns[:list_results].map(&:info_request).should =~ InfoRequest.all( - :conditions => "id in (select info_request_id from info_request_events where created_at < '2007-10-13'::date)") - - get :list, :view => 'all', :request_date_after => '13/10/2007' - assigns[:list_results].map(&:info_request).should =~ InfoRequest.all( - :conditions => "id in (select info_request_id from info_request_events where created_at > '2007-10-13'::date)") - - get :list, :view => 'all', :request_date_after => '13/10/2007', :request_date_before => '01/11/2007' - assigns[:list_results].map(&:info_request).should =~ InfoRequest.all( - :conditions => "id in (select info_request_id from info_request_events where created_at between '2007-10-13'::date and '2007-11-01'::date)") - end - - it "should list internal_review requests as unresolved ones" do - get :list, :view => 'awaiting' - - # This doesn’t precisely duplicate the logic of the actual - # query, but it is close enough to give the same result with - # the current set of test data. - assigns[:list_results].should =~ InfoRequestEvent.all( - :conditions => "described_state in ( - 'waiting_response', 'waiting_clarification', - 'internal_review', 'gone_postal', 'error_message', 'requires_admin' - ) and not exists ( - select * - from info_request_events later_events - where later_events.created_at > info_request_events.created_at - and later_events.info_request_id = info_request_events.info_request_id - )") - - - get :list, :view => 'awaiting' - assigns[:list_results].map(&:info_request).include?(info_requests(:fancy_dog_request)).should == false - - event = info_request_events(:useless_incoming_message_event) - event.described_state = event.calculated_state = "internal_review" - event.save! - rebuild_xapian_index - - get :list, :view => 'awaiting' - assigns[:list_results].map(&:info_request).include?(info_requests(:fancy_dog_request)).should == true - end - - it "should assign the first page of results" do - xap_results = mock(ActsAsXapian::Search, - :results => (1..25).to_a.map { |m| { :model => m } }, - :matches_estimated => 1000000) - - ActsAsXapian::Search.should_receive(:new). - with([InfoRequestEvent]," (variety:sent OR variety:followup_sent OR variety:response OR variety:comment)", - :sort_by_prefix => "created_at", :offset => 0, :limit => 25, :sort_by_ascending => true, - :collapse_by_prefix => "request_collapse"). - and_return(xap_results) - get :list, :view => 'all' - assigns[:list_results].size.should == 25 - assigns[:show_no_more_than].should == RequestController::MAX_RESULTS - end - it "should return 404 for pages we don't want to serve up" do xap_results = mock(ActsAsXapian::Search, :results => (1..25).to_a.map { |m| { :model => m } }, diff --git a/spec/factories.rb b/spec/factories.rb index 7d8f94ac1..8b724ae37 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -144,4 +144,16 @@ FactoryGirl.define do track_query 'Example Query' end + factory :public_body_change_request do + user + source_url 'http://www.example.com' + notes 'Please' + public_body_email 'new@example.com' + factory :add_body_request do + public_body_name 'A New Body' + end + factory :update_body_request do + public_body + end + end end diff --git a/spec/fixtures/files/email-folding-example-1.txt.expected b/spec/fixtures/files/email-folding-example-1.txt.expected index 801542288..45dabf156 100644 --- a/spec/fixtures/files/email-folding-example-1.txt.expected +++ b/spec/fixtures/files/email-folding-example-1.txt.expected @@ -8,3 +8,10 @@ On behalf of James Hall, Chief Executive Identity and Passport Service <<9032 C Pollard final response.doc>> +FOLDED_QUOTED_SECTION + +FOLDED_QUOTED_SECTION + + +FOLDED_QUOTED_SECTION + diff --git a/spec/fixtures/files/email-folding-example-10.txt b/spec/fixtures/files/email-folding-example-10.txt index 0fabb7f9c..a0773e6ff 100644 --- a/spec/fixtures/files/email-folding-example-10.txt +++ b/spec/fixtures/files/email-folding-example-10.txt @@ -3,13 +3,13 @@ Department of Health, please visit the 'Contact us' page on the Department’s website. ----------------------------------------------------------------------------------------- - - Apologies that you were not able to read our previous response of 4 - October. Please find the text of that email below. - + + Apologies that you were not able to read our previous response of 4 + October. Please find the text of that email below. + Our ref: DE00000642471 -Dear Ms Peters Rock, +Dear Ms Peters Rock, You requested your correspondence to be treated under the Freedom of Information Act. However, as your correspondence asked for general @@ -19,7 +19,7 @@ correspondence under the provisions of the Act. I am sorry I cannot be more helpful. -Yours sincerely, +Yours sincerely, Simon Dove Customer Service Centre Department of Health diff --git a/spec/fixtures/files/email-folding-example-10.txt.expected b/spec/fixtures/files/email-folding-example-10.txt.expected index e4f704c0e..5b609dc12 100644 --- a/spec/fixtures/files/email-folding-example-10.txt.expected +++ b/spec/fixtures/files/email-folding-example-10.txt.expected @@ -3,13 +3,13 @@ Department of Health, please visit the 'Contact us' page on the Department’s website. ----------------------------------------------------------------------------------------- - - Apologies that you were not able to read our previous response of 4 - October. Please find the text of that email below. - + + Apologies that you were not able to read our previous response of 4 + October. Please find the text of that email below. + Our ref: DE00000642471 -Dear Ms Peters Rock, +Dear Ms Peters Rock, You requested your correspondence to be treated under the Freedom of Information Act. However, as your correspondence asked for general @@ -19,7 +19,8 @@ correspondence under the provisions of the Act. I am sorry I cannot be more helpful. -Yours sincerely, +Yours sincerely, Simon Dove Customer Service Centre Department of Health +FOLDED_QUOTED_SECTION diff --git a/spec/fixtures/files/email-folding-example-11.txt b/spec/fixtures/files/email-folding-example-11.txt new file mode 100644 index 000000000..635d7aa4f --- /dev/null +++ b/spec/fixtures/files/email-folding-example-11.txt @@ -0,0 +1,45 @@ + Hello Example, + + This is a reply to your test request Nov 28. + + Regards. + + On Thu, Nov 28, 2013 at 9:08 AM, Example User + <[1]request-x-xxx@xxx.com> wrote: + + Dear Test Authority, + + This is the request body. + + Yours faithfully, + + Example User + + ------------------------------------------------------------------- + + Please use this email address for all replies to this request: + [2]request-x-xxx@xxx.com + + Is [3]testauthority@example.com the wrong address for Freedom of + Information requests to AYG Test Authority? If so, please contact us + using this form: + [4]http://example.com/help/contact + + Disclaimer: This message and any reply that you make will be published + on the internet. Our privacy and copyright policies: + [5]http://example.com/help/officers + + If you find this service useful as an FOI officer, please ask your web + manager to link to us from your organisation's FOI page. + + ------------------------------------------------------------------- + +References + + Visible links + 1. mailto:request-x-xxx@xxx.com + 2. mailto:request-x-xxx@xxx.com + 3. mailto:testauthority@example.com + 4. http://example.com/help/contact + 5. http://example.com/help/officers + diff --git a/spec/fixtures/files/email-folding-example-11.txt.expected b/spec/fixtures/files/email-folding-example-11.txt.expected new file mode 100644 index 000000000..e336062c7 --- /dev/null +++ b/spec/fixtures/files/email-folding-example-11.txt.expected @@ -0,0 +1,8 @@ + Hello Example, + + This is a reply to your test request Nov 28. + + Regards. + + +FOLDED_QUOTED_SECTION diff --git a/spec/fixtures/files/email-folding-example-2.txt.expected b/spec/fixtures/files/email-folding-example-2.txt.expected index e52fbe443..df578390d 100644 --- a/spec/fixtures/files/email-folding-example-2.txt.expected +++ b/spec/fixtures/files/email-folding-example-2.txt.expected @@ -2,3 +2,4 @@ Preface to the message which we are not interested in ----------------------------------------------------------------------------------------- Important message about cheese +FOLDED_QUOTED_SECTION diff --git a/spec/fixtures/files/email-folding-example-3.txt.expected b/spec/fixtures/files/email-folding-example-3.txt.expected index e2cca4933..accb40150 100644 --- a/spec/fixtures/files/email-folding-example-3.txt.expected +++ b/spec/fixtures/files/email-folding-example-3.txt.expected @@ -3,3 +3,10 @@ Reference : T3241/8 Thank you for your e-mail enquiry of 12th February. A reply is attached. +FOLDED_QUOTED_SECTION + +FOLDED_QUOTED_SECTION + + +FOLDED_QUOTED_SECTION + diff --git a/spec/fixtures/files/email-folding-example-5.txt.expected b/spec/fixtures/files/email-folding-example-5.txt.expected index fbb0f0f50..46d7f731a 100644 --- a/spec/fixtures/files/email-folding-example-5.txt.expected +++ b/spec/fixtures/files/email-folding-example-5.txt.expected @@ -1,11 +1,11 @@ Hi Simon -My apologies for timescale of response. The data forwarded is a public -register, and is updated on a frequent and regular basis; your request -unfortunately coincided with annual leave and a monthly update of the -spreadsheet. As the definition of an HMO under the Housing Act 2004 -differs to that under planning legislation, I have forwarded this and -your original request on to Andy England, Development Control Manager to +My apologies for timescale of response. The data forwarded is a public +register, and is updated on a frequent and regular basis; your request +unfortunately coincided with annual leave and a monthly update of the +spreadsheet. As the definition of an HMO under the Housing Act 2004 +differs to that under planning legislation, I have forwarded this and +your original request on to Andy England, Development Control Manager to respond independantly. If I can be of further assistance please contact me diff --git a/spec/fixtures/files/email-folding-example-7.txt.expected b/spec/fixtures/files/email-folding-example-7.txt.expected index 0ef8fd82b..cb6961038 100644 --- a/spec/fixtures/files/email-folding-example-7.txt.expected +++ b/spec/fixtures/files/email-folding-example-7.txt.expected @@ -13,4 +13,5 @@ Telephone +44 (0) 116 2222222 Extn 5221 VM No. 8035 Fax + 44 (0) 116 2485217 -<<0001_00035908_Resp_12RESPONSE LETTER_20080408_112311_01.TIF>>
\ No newline at end of file +<<0001_00035908_Resp_12RESPONSE LETTER_20080408_112311_01.TIF>> +FOLDED_QUOTED_SECTION diff --git a/spec/fixtures/files/email-folding-example-8.txt.expected b/spec/fixtures/files/email-folding-example-8.txt.expected index b5dc10c0d..e8c08f4ca 100644 --- a/spec/fixtures/files/email-folding-example-8.txt.expected +++ b/spec/fixtures/files/email-folding-example-8.txt.expected @@ -3,4 +3,5 @@ I will be out of the office starting 11/04/2008 and will not return until I will respond to your message when I return. If you have any urgent queries please ring 02085419088 for Legal Business Support queries or -contact Eileen Perren for FOI or DP queries
\ No newline at end of file +contact Eileen Perren for FOI or DP queries +FOLDED_QUOTED_SECTION diff --git a/spec/fixtures/files/email-folding-example-9.txt.expected b/spec/fixtures/files/email-folding-example-9.txt.expected index 2d2381a34..d222e9438 100644 --- a/spec/fixtures/files/email-folding-example-9.txt.expected +++ b/spec/fixtures/files/email-folding-example-9.txt.expected @@ -7,3 +7,9 @@ Yours sincerely MICHAEL HEGARTY FOI Officer +FOLDED_QUOTED_SECTION + +FOLDED_QUOTED_SECTION + + +FOLDED_QUOTED_SECTION diff --git a/spec/helpers/link_to_helper_spec.rb b/spec/helpers/link_to_helper_spec.rb index b29419ef3..2259db6c2 100644 --- a/spec/helpers/link_to_helper_spec.rb +++ b/spec/helpers/link_to_helper_spec.rb @@ -20,6 +20,45 @@ describe LinkToHelper do end + describe 'when displaying a user link for a request' do + + context "for external requests" do + before do + @info_request = mock_model(InfoRequest, :external_user_name => nil, + :is_external? => true) + end + + it 'should return the text "Anonymous user" with a link to the privacy help pages when there is no external username' do + request_user_link(@info_request).should == '<a href="/help/privacy#anonymous">Anonymous user</a>' + end + + it 'should return a link with an alternative text if requested' do + request_user_link(@info_request, 'other text').should == '<a href="/help/privacy#anonymous">other text</a>' + end + + it 'should display an absolute link if requested' do + request_user_link_absolute(@info_request).should == '<a href="http://test.host/help/privacy#anonymous">Anonymous user</a>' + end + end + + context "for normal requests" do + + before do + @info_request = FactoryGirl.build(:info_request) + end + + it 'should display a relative link by default' do + request_user_link(@info_request).should == '<a href="/user/example_user">Example User</a>' + end + + it 'should display an absolute link if requested' do + request_user_link_absolute(@info_request).should == '<a href="http://test.host/user/example_user">Example User</a>' + end + + end + + end + describe 'when displaying a user admin link for a request' do it 'should return the text "An anonymous user (external)" in the case where there is no external username' do diff --git a/spec/lib/alaveteli_external_command.rb b/spec/lib/alaveteli_external_command.rb new file mode 100644 index 000000000..18afeda33 --- /dev/null +++ b/spec/lib/alaveteli_external_command.rb @@ -0,0 +1,23 @@ +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +require 'alaveteli_external_command' + +script_dir = File.join(File.dirname(__FILE__), 'alaveteli_external_command_scripts') +segfault_script = File.join(script_dir, 'segfault.sh') +error_script = File.join(script_dir, 'error.sh') + +describe "when running external commands" do + + it "should detect a non-zero exit status" do + $stderr.should_receive(:puts).with(/Error from/) + t = AlaveteliExternalCommand.run(error_script) + assert_nil t + end + + it "should detect when an external command crashes" do + $stderr.should_receive(:puts).with(/exited abnormally/) + t = AlaveteliExternalCommand.run(segfault_script) + assert_nil t + end + +end diff --git a/spec/lib/alaveteli_external_command_scripts/error.sh b/spec/lib/alaveteli_external_command_scripts/error.sh new file mode 100755 index 000000000..65e74b3c6 --- /dev/null +++ b/spec/lib/alaveteli_external_command_scripts/error.sh @@ -0,0 +1,4 @@ +#!/bin/sh + +echo "this is my error message" >&1 +exit 1 diff --git a/spec/lib/alaveteli_external_command_scripts/segfault.sh b/spec/lib/alaveteli_external_command_scripts/segfault.sh new file mode 100755 index 000000000..f96ba5be8 --- /dev/null +++ b/spec/lib/alaveteli_external_command_scripts/segfault.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +kill -11 $$ diff --git a/spec/models/incoming_message_spec.rb b/spec/models/incoming_message_spec.rb index c0a7e5340..c27870afc 100644 --- a/spec/models/incoming_message_spec.rb +++ b/spec/models/incoming_message_spec.rb @@ -165,7 +165,7 @@ describe IncomingMessage, " when dealing with incoming mail" do message = File.read(file) parsed = IncomingMessage.remove_quoted_sections(message) expected = File.read("#{file}.expected") - parsed.should include(expected) + parsed.should be_equal_modulo_whitespace_to expected end end @@ -462,7 +462,7 @@ describe IncomingMessage, " when censoring data" do data.should == "His email was x\000x\000x\000@\000x\000x\000x\000.\000x\000x\000x\000, indeed" end - it 'should handle multibyte characters correctly', :focus => true do + it 'should handle multibyte characters correctly' do orig_data = 'á' data = orig_data.dup @regex_censor_rule = CensorRule.new() diff --git a/spec/models/info_request_spec.rb b/spec/models/info_request_spec.rb index dcc94e967..ade75e2cc 100644 --- a/spec/models/info_request_spec.rb +++ b/spec/models/info_request_spec.rb @@ -1177,4 +1177,100 @@ describe InfoRequest do request_events.map(&:info_request).select{|x|x.url_title =~ /^spam/}.length.should == 1 end end + + describe InfoRequest, "when constructing a list of requests by query" do + + before(:each) do + get_fixtures_xapian_index + end + + def apply_filters(filters) + results = InfoRequest.request_list(filters, page=1, per_page=100, max_results=100) + results[:results].map(&:info_request) + end + + it "should filter requests" do + apply_filters(:latest_status => 'all').should =~ InfoRequest.all + + # default sort order is the request with the most recently created event first + apply_filters(:latest_status => 'all').should == InfoRequest.all( + :order => "(SELECT max(info_request_events.created_at) + FROM info_request_events + WHERE info_request_events.info_request_id = info_requests.id) + DESC") + + apply_filters(:latest_status => 'successful').should =~ InfoRequest.all( + :conditions => "id in ( + SELECT info_request_id + FROM info_request_events + WHERE NOT EXISTS ( + SELECT * + FROM info_request_events later_events + WHERE later_events.created_at > info_request_events.created_at + AND later_events.info_request_id = info_request_events.info_request_id + AND later_events.described_state IS NOT null + ) + AND info_request_events.described_state IN ('successful', 'partially_successful') + )") + + end + + it "should filter requests by date" do + # The semantics of the search are that it finds any InfoRequest + # that has any InfoRequestEvent created in the specified range + filters = {:latest_status => 'all', :request_date_before => '13/10/2007'} + apply_filters(filters).should =~ InfoRequest.all( + :conditions => "id IN (SELECT info_request_id + FROM info_request_events + WHERE created_at < '2007-10-13'::date)") + + filters = {:latest_status => 'all', :request_date_after => '13/10/2007'} + apply_filters(filters).should =~ InfoRequest.all( + :conditions => "id IN (SELECT info_request_id + FROM info_request_events + WHERE created_at > '2007-10-13'::date)") + + filters = {:latest_status => 'all', + :request_date_after => '13/10/2007', + :request_date_before => '01/11/2007'} + apply_filters(filters).should =~ InfoRequest.all( + :conditions => "id IN (SELECT info_request_id + FROM info_request_events + WHERE created_at BETWEEN '2007-10-13'::date + AND '2007-11-01'::date)") + end + + + it "should list internal_review requests as unresolved ones" do + + # This doesn’t precisely duplicate the logic of the actual + # query, but it is close enough to give the same result with + # the current set of test data. + results = apply_filters(:latest_status => 'awaiting') + results.should =~ InfoRequest.all( + :conditions => "id IN (SELECT info_request_id + FROM info_request_events + WHERE described_state in ( + 'waiting_response', 'waiting_clarification', + 'internal_review', 'gone_postal', 'error_message', 'requires_admin' + ) and not exists ( + select * + from info_request_events later_events + where later_events.created_at > info_request_events.created_at + and later_events.info_request_id = info_request_events.info_request_id + ))") + + + results.include?(info_requests(:fancy_dog_request)).should == false + + event = info_request_events(:useless_incoming_message_event) + event.described_state = event.calculated_state = "internal_review" + event.save! + rebuild_xapian_index + results = apply_filters(:latest_status => 'awaiting') + results.include?(info_requests(:fancy_dog_request)).should == true + end + + + end end diff --git a/spec/models/profile_photo_spec.rb b/spec/models/profile_photo_spec.rb index 0e157e2c5..e70f474a0 100644 --- a/spec/models/profile_photo_spec.rb +++ b/spec/models/profile_photo_spec.rb @@ -10,12 +10,12 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') -describe ProfilePhoto, "when constructing a new photo" do +describe ProfilePhoto, "when constructing a new photo" do - before do + before do @mock_user = mock_model(User) end - + it 'should take no image as invalid' do profile_photo = ProfilePhoto.new(:data => nil, :user => @mock_user) profile_photo.valid?.should == false @@ -26,7 +26,15 @@ describe ProfilePhoto, "when constructing a new photo" do profile_photo.valid?.should == false end - it 'should accept and convert a PNG to right size' do + it 'should translate a no image error message' do + I18n.with_locale(:es) do + profile_photo = ProfilePhoto.new(:data => nil, :user => @mock_user) + profile_photo.valid?.should == false + profile_photo.errors[:data].should == ['Por favor elige el fichero que contiene tu foto'] + end + end + + it 'should accept and convert a PNG to right size' do data = load_file_fixture("parrot.png") profile_photo = ProfilePhoto.new(:data => data, :user => @mock_user) profile_photo.valid?.should == true @@ -35,7 +43,7 @@ describe ProfilePhoto, "when constructing a new photo" do profile_photo.image.rows.should == 96 end - it 'should accept and convert a JPEG to right format and size' do + it 'should accept and convert a JPEG to right format and size' do data = load_file_fixture("parrot.jpg") profile_photo = ProfilePhoto.new(:data => data, :user => @mock_user) profile_photo.valid?.should == true @@ -44,7 +52,7 @@ describe ProfilePhoto, "when constructing a new photo" do profile_photo.image.rows.should == 96 end - it 'should accept a draft PNG and not resize it' do + it 'should accept a draft PNG and not resize it' do data = load_file_fixture("parrot.png") profile_photo = ProfilePhoto.new(:data => data, :draft => true) profile_photo.valid?.should == true @@ -53,6 +61,6 @@ describe ProfilePhoto, "when constructing a new photo" do profile_photo.image.rows.should == 289 end - + end diff --git a/spec/models/public_body_change_request_spec.rb b/spec/models/public_body_change_request_spec.rb new file mode 100644 index 000000000..0c4cea67b --- /dev/null +++ b/spec/models/public_body_change_request_spec.rb @@ -0,0 +1,139 @@ +# == Schema Information +# +# Table name: public_body_change_requests +# +# id :integer not null, primary key +# user_email :string(255) +# user_name :string(255) +# user_id :integer +# public_body_name :text +# public_body_id :integer +# public_body_email :string(255) +# source_url :text +# notes :text +# is_open :boolean default(TRUE), not null +# created_at :datetime not null +# updated_at :datetime not null +# + +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +describe PublicBodyChangeRequest, 'when validating' do + + it 'should not be valid without a public body name' do + change_request = PublicBodyChangeRequest.new() + change_request.valid?.should be_false + change_request.errors[:public_body_name].should == ['Please enter the name of the authority'] + end + + it 'should not be valid without a user name if there is no user' do + change_request = PublicBodyChangeRequest.new(:public_body_name => 'New Body') + change_request.valid?.should be_false + change_request.errors[:user_name].should == ['Please enter your name'] + end + + it 'should not be valid without a user email address if there is no user' do + change_request = PublicBodyChangeRequest.new(:public_body_name => 'New Body') + change_request.valid?.should be_false + change_request.errors[:user_email].should == ['Please enter your email address'] + end + + it 'should be valid with a user and no name or email address' do + user = FactoryGirl.build(:user) + change_request = PublicBodyChangeRequest.new(:user => user, + :public_body_name => 'New Body') + change_request.valid?.should be_true + end + + it 'should validate the format of a user email address entered' do + change_request = PublicBodyChangeRequest.new(:public_body_name => 'New Body', + :user_email => '@example.com') + change_request.valid?.should be_false + change_request.errors[:user_email].should == ["Your email doesn't look like a valid address"] + end + + it 'should validate the format of a public body email address entered' do + change_request = PublicBodyChangeRequest.new(:public_body_name => 'New Body', + :public_body_email => '@example.com') + change_request.valid?.should be_false + change_request.errors[:public_body_email].should == ["The authority email doesn't look like a valid address"] + end + +end + +describe PublicBodyChangeRequest, 'get_user_name' do + + it 'should return the user_name field if there is no user association' do + change_request = PublicBodyChangeRequest.new(:user_name => 'Test User') + change_request.get_user_name.should == 'Test User' + end + + it 'should return the name of the associated user if there is one' do + user = FactoryGirl.build(:user) + change_request = PublicBodyChangeRequest.new(:user => user) + change_request.get_user_name.should == user.name + end + +end + + +describe PublicBodyChangeRequest, 'get_user_email' do + + it 'should return the user_email field if there is no user association' do + change_request = PublicBodyChangeRequest.new(:user_email => 'user@example.com') + change_request.get_user_email.should == 'user@example.com' + end + + it 'should return the email of the associated user if there is one' do + user = FactoryGirl.build(:user) + change_request = PublicBodyChangeRequest.new(:user => user) + change_request.get_user_email.should == user.email + end + +end + + +describe PublicBodyChangeRequest, 'get_public_body_name' do + + it 'should return the public_body_name field if there is no public body association' do + change_request = PublicBodyChangeRequest.new(:public_body_name => 'Test Authority') + change_request.get_public_body_name.should == 'Test Authority' + end + + it 'should return the name of the associated public body if there is one' do + public_body = FactoryGirl.build(:public_body) + change_request = PublicBodyChangeRequest.new(:public_body => public_body) + change_request.get_public_body_name.should == public_body.name + end + +end + +describe PublicBodyChangeRequest, 'when creating a comment for the associated public body' do + + it 'should include requesting user, source_url and notes' do + change_request = PublicBodyChangeRequest.new(:user_name => 'Test User', + :user_email => 'test@example.com', + :source_url => 'http://www.example.com', + :notes => 'Some notes') + expected = "Requested by: Test User (test@example.com)\nSource URL: http://www.example.com\nNotes: Some notes" + change_request.comment_for_public_body.should == expected + end + +end + +describe PublicBodyChangeRequest, 'when creating a default subject for a response email' do + + it 'should create an appropriate subject for a request to add a body' do + change_request = PublicBodyChangeRequest.new(:public_body_name => 'Test Body') + change_request.default_response_subject.should == 'Your request to add Test Body to Alaveteli' + end + + it 'should create an appropriate subject for a request to update an email address' do + public_body = FactoryGirl.build(:public_body) + change_request = PublicBodyChangeRequest.new(:public_body => public_body) + change_request.default_response_subject.should == "Your request to update #{public_body.name} on Alaveteli" + + end + +end + diff --git a/spec/models/public_body_spec.rb b/spec/models/public_body_spec.rb index ed24ced52..dc09bdfa6 100644 --- a/spec/models/public_body_spec.rb +++ b/spec/models/public_body_spec.rb @@ -300,6 +300,37 @@ describe PublicBody, "when searching" do end end +describe PublicBody, "when asked for the internal_admin_body" do + before(:each) do + # Make sure that there's no internal_admin_body before each of + # these tests: + PublicBody.connection.delete("DELETE FROM public_bodies WHERE url_name = 'internal_admin_body'") + PublicBody.connection.delete("DELETE FROM public_body_translations WHERE url_name = 'internal_admin_body'") + end + + it "should create the internal_admin_body if it didn't exist" do + iab = PublicBody.internal_admin_body + iab.should_not be_nil + end + + it "should find the internal_admin_body even if the default locale has changed since it was created" do + with_default_locale("en") do + I18n.with_locale(:en) do + iab = PublicBody.internal_admin_body + iab.should_not be_nil + end + end + with_default_locale("es") do + I18n.with_locale(:es) do + iab = PublicBody.internal_admin_body + iab.should_not be_nil + end + end + end + +end + + describe PublicBody, " when dealing public body locales" do it "shouldn't fail if it internal_admin_body was created in a locale other than the default" do # first time, do it with the non-default locale diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 1eeb8603b..dc5a0d6eb 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -187,11 +187,16 @@ Spork.prefork do end end + # Reset the default locale, making sure that the previous default locale + # is also cleared from the fallbacks def with_default_locale(locale) original_default_locale = I18n.default_locale + original_fallbacks = I18n.fallbacks + I18n.fallbacks = nil I18n.default_locale = locale yield ensure + I18n.fallbacks = original_fallbacks I18n.default_locale = original_default_locale end @@ -215,3 +220,16 @@ Spork.each_run do FactoryGirl.reload # This code will be run each time you run your specs. end + +def normalise_whitespace(s) + s = s.gsub(/\A\s+|\s+\Z/, "") + s = s.gsub(/\s+/, " ") + return s +end + +RSpec::Matchers.define :be_equal_modulo_whitespace_to do |expected| + match do |actual| + normalise_whitespace(actual) == normalise_whitespace(expected) + end +end + diff --git a/spec/views/request/list.html.erb_spec.rb b/spec/views/request/list.html.erb_spec.rb deleted file mode 100644 index 521d946bc..000000000 --- a/spec/views/request/list.html.erb_spec.rb +++ /dev/null @@ -1,49 +0,0 @@ -require File.expand_path(File.join('..', '..', '..', 'spec_helper'), __FILE__) - -describe "request/list" do - - before do - assign :page, 1 - assign :per_page, 10 - end - - def make_mock_event - return mock_model(InfoRequestEvent, - :info_request => mock_model(InfoRequest, - :title => 'Title', - :url_title => 'title', - :display_status => 'awaiting_response', - :calculate_status => 'awaiting_response', - :public_body => mock_model(PublicBody, :name => 'Test Quango', :url_name => 'testquango'), - :user => mock_model(User, :name => 'Test User', :url_name => 'testuser'), - :is_external? => false - ), - :incoming_message => nil, :is_incoming_message? => false, - :outgoing_message => nil, :is_outgoing_message? => false, - :comment => nil, :is_comment? => false, - :event_type => 'sent', - :created_at => Time.now - 4.days, - :search_text_main => '' - ) - end - - it "should be successful" do - assign :list_results, [ make_mock_event, make_mock_event ] - assign :matches_estimated, 2 - assign :show_no_more_than, 100 - render - response.should have_selector("div.request_listing") - response.should_not have_selector("p", :content => "No requests of this sort yet") - end - - it "should cope with no results" do - assign :list_results, [ ] - assign :matches_estimated, 0 - assign :show_no_more_than, 0 - render - response.should have_selector("p", :content => "No requests of this sort yet") - response.should_not have_selector("div.request_listing") - end - -end - |