aboutsummaryrefslogtreecommitdiffstats
path: root/spec/controllers/admin_request_controller_spec.rb
blob: ece1fe3895f44ed10cd43887cb9b6a5f9562baca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* 
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe AdminRequestController, "when administering requests" do
    integrate_views
    before { basic_auth_login @request }

    before(:each) do
        load_raw_emails_data
        @old_filters = ActionController::Routing::Routes.filters
        ActionController::Routing::Routes.filters = RoutingFilter::Chain.new
    end
    after do
        ActionController::Routing::Routes.filters = @old_filters
    end

    it "shows the index/list page" do
        get :index
    end

    it "shows a public body" do
        get :show, :id => info_requests(:fancy_dog_request)
    end

    it "edits a public body" do
        get :edit, :id => info_requests(:fancy_dog_request)
    end

    it "saves edits to a request" do
        info_requests(:fancy_dog_request).title.should == "Why do you have & such a fancy dog?"
        post :update, { :id => info_requests(:fancy_dog_request), :info_request => { :title => "Renamed", :prominence => "normal", :described_state => "waiting_response", :awaiting_description => false, :allow_new_responses_from => 'anybody', :handle_rejected_responses => 'bounce' } }
        response.flash[:notice].should include('successful')
        ir = InfoRequest.find(info_requests(:fancy_dog_request).id)
        ir.title.should == "Renamed"
    end

    it "edits an outgoing message" do
        get :edit_outgoing, :id => outgoing_messages(:useless_outgoing_message)
    end

    it "saves edits to an outgoing_message" do
        outgoing_messages(:useless_outgoing_message).body.should include("fancy dog")
        post :update_outgoing, { :id => outgoing_messages(:useless_outgoing_message), :outgoing_message => { :body => "Why do you have such a delicious cat?" } }
        response.flash[:notice].should include('successful')
        ir = OutgoingMessage.find(outgoing_messages(:useless_outgoing_message).id)
        ir.body.should include("delicious cat")
    end

end

describe AdminRequestController, "when administering the holding pen" do
    integrate_views
    before(:each) do
        basic_auth_login @request
        load_raw_emails_data
        @old_filters = ActionController::Routing::Routes.filters
        ActionController::Routing::Routes.filters = RoutingFilter::Chain.new
    end
    after do
        ActionController::Routing::Routes.filters = @old_filters
    end

    it "shows a rejection reason for an incoming message from an invalid address" do
        ir = info_requests(:fancy_dog_request)
        ir.allow_new_responses_from = 'authority_only'
        ir.handle_rejected_responses = 'holding_pen'
        ir.save!
        receive_incoming_mail('incoming-request-plain.email', ir.incoming_email, "frob@nowhere.com")
        get :show_raw_email, :id => InfoRequest.holding_pen_request.get_last_response.raw_email.id
        response.should have_text(/Only the authority can reply to this request/)
    end

    it "allows redelivery even to a closed request" do
        ir = info_requests(:fancy_dog_request)
        ir.allow_new_responses_from = 'nobody'
        ir.handle_rejected_responses = 'holding_pen'
        ir.save!
        InfoRequest.holding_pen_request.incoming_messages.length.should == 0
        ir.incoming_messages.length.should == 1
        receive_incoming_mail('incoming-request-plain.email', ir.incoming_email, "frob@nowhere.com")
        InfoRequest.holding_pen_request.incoming_messages.length.should == 1
        new_im = InfoRequest.holding_pen_request.incoming_messages[0]
        ir.incoming_messages.length.should == 1
        post :redeliver_incoming, :redeliver_incoming_message_id => new_im.id, :url_title => ir.url_title        
        ir = InfoRequest.find_by_url_title(ir.url_title)
        ir.incoming_messages.length.should == 2
        response.should redirect_to(:controller=>'admin_request', :action=>'show', :id=>101)
        InfoRequest.holding_pen_request.incoming_messages.length.should == 0
    end

    it "guesses a misdirected request" do
        ir = info_requests(:fancy_dog_request)
        ir.handle_rejected_responses = 'holding_pen'
        ir.allow_new_responses_from = 'authority_only'
        ir.save!
        mail_to = "request-#{ir.id}-asdfg@example.com"
        receive_incoming_mail('incoming-request-plain.email', mail_to)
        interesting_email = InfoRequest.holding_pen_request.get_last_response.raw_email.id
        # now we add another message to the queue, which we're not interested in
        receive_incoming_mail('incoming-request-plain.email', ir.incoming_email, "")
        InfoRequest.holding_pen_request.incoming_messages.length.should == 2
        get :show_raw_email, :id => interesting_email
        response.should have_text(/Could not identify the request/)
        assigns[:info_requests][0].should == ir
    end

    it "destroys an incoming message" do
        im = incoming_messages(:useless_incoming_message)        
        raw_email = im.raw_email.filepath
        post :destroy_incoming, :incoming_message_id => im.id
        assert_equal File.exists?(raw_email), false        
    end

end
an class="s1">'Hide pins', 'Dangos pinnau', 'Cuddio pinnau', "Vis nåler", "Gjem nåler" ]; for (var i=0; i<showhide.length; i+=2) { if (this.innerHTML == showhide[i]) { fixmystreet.markers.setVisibility(true); this.innerHTML = showhide[i+1]; } else if (this.innerHTML == showhide[i+1]) { fixmystreet.markers.setVisibility(false); this.innerHTML = showhide[i]; } } }); $('#all_pins_link').click(function(e) { e.preventDefault(); fixmystreet.markers.setVisibility(true); var welsh = 0; var texts = [ 'en', 'Include stale reports', 'Hide stale reports', 'cy', 'Cynnwys hen adroddiadau', 'Cuddio hen adroddiadau' ]; for (var i=0; i<texts.length; i+=3) { if (this.innerHTML == texts[i+1]) { this.innerHTML = texts[i+2]; fixmystreet.markers.protocol.options.params = { all_pins: 1 }; fixmystreet.markers.refresh( { force: true } ); lang = texts[i]; } else if (this.innerHTML == texts[i+2]) { this.innerHTML = texts[i+1]; fixmystreet.markers.protocol.options.params = { }; fixmystreet.markers.refresh( { force: true } ); lang = texts[i]; } } if (lang == 'cy') { document.getElementById('hide_pins_link').innerHTML = 'Cuddio pinnau'; } else { document.getElementById('hide_pins_link').innerHTML = 'Hide pins'; } }); } function fms_markers_list(pins, transform) { var cols = { 'red':'R', 'green':'G', 'blue':'B', 'purple':'P' }; var markers = []; for (var i=0; i<pins.length; i++) { var pin = pins[i]; var loc = new OpenLayers.Geometry.Point(pin[1], pin[0]); if (transform) { // The Strategy does this for us, so don't do it in that case. loc.transform( new OpenLayers.Projection("EPSG:4326"), fixmystreet.map.getProjectionObject() ); } var marker = new OpenLayers.Feature.Vector(loc, { type: cols[pin[2]], id: pin[3], title: pin[4] }); markers.push( marker ); } return markers; } /* Overridding the buttonDown function of PanZoom so that it does zoomTo(0) rather than zoomToMaxExtent() */ OpenLayers.Control.PanZoomFMS = OpenLayers.Class(OpenLayers.Control.PanZoom, { buttonDown: function (evt) { if (!OpenLayers.Event.isLeftClick(evt)) { return; } switch (this.action) { case "panup": this.map.pan(0, -this.getSlideFactor("h")); break; case "pandown": this.map.pan(0, this.getSlideFactor("h")); break; case "panleft": this.map.pan(-this.getSlideFactor("w"), 0); break; case "panright": this.map.pan(this.getSlideFactor("w"), 0); break; case "zoomin": this.map.zoomIn(); break; case "zoomout": this.map.zoomOut(); break; case "zoomworld": this.map.zoomTo(0); break; } OpenLayers.Event.stop(evt); } }); /* Overriding Permalink so that it can pass the correct zoom to OSM */ OpenLayers.Control.PermalinkFMS = OpenLayers.Class(OpenLayers.Control.Permalink, { updateLink: function() { var separator = this.anchor ? '#' : '?'; var href = this.base; if (href.indexOf(separator) != -1) { href = href.substring( 0, href.indexOf(separator) ); } href += separator + OpenLayers.Util.getParameterString(this.createParams(null, this.map.getZoom()+fixmystreet.zoomOffset)); // Could use mlat/mlon here as well if we are on a page with a marker if (this.anchor && !this.element) { window.location.href = href; } else { this.element.href = href; } } }); /* Pan data handler */ OpenLayers.Format.FixMyStreet = OpenLayers.Class(OpenLayers.Format.JSON, { read: function(json, filter) { if (typeof json == 'string') { obj = OpenLayers.Format.JSON.prototype.read.apply(this, [json, filter]); } else { obj = json; } if (typeof(obj.current) != 'undefined') document.getElementById('current').innerHTML = obj.current; if (typeof(obj.current_near) != 'undefined') document.getElementById('current_near').innerHTML = obj.current_near; var markers = fms_markers_list( obj.pins, false ); return markers; }, CLASS_NAME: "OpenLayers.Format.FixMyStreet" }); /* Click handler */ OpenLayers.Control.Click = OpenLayers.Class(OpenLayers.Control, { defaultHandlerOptions: { 'single': true, 'double': false, 'pixelTolerance': 0, 'stopSingle': false, 'stopDouble': false }, initialize: function(options) { this.handlerOptions = OpenLayers.Util.extend( {}, this.defaultHandlerOptions ); OpenLayers.Control.prototype.initialize.apply( this, arguments ); this.handler = new OpenLayers.Handler.Click( this, { 'click': this.trigger }, this.handlerOptions ); }, trigger: function(e) { var lonlat = fixmystreet.map.getLonLatFromViewPortPx(e.xy); if (fixmystreet.page == 'new') { /* Already have a purple pin */ fixmystreet.markers.features[0].move(lonlat); } else { var markers = fms_markers_list( [ [ lonlat.lat, lonlat.lon, 'purple' ] ], false ); fixmystreet.bbox_strategy.deactivate(); fixmystreet.markers.removeAllFeatures(); fixmystreet.markers.addFeatures( markers ); fixmystreet_activate_drag(); } fixmystreet_update_pin(lonlat); if (fixmystreet.page == 'new') { return; } $.getJSON('/report/new/ajax', { latitude: $('#fixmystreet\\.latitude').val(), longitude: $('#fixmystreet\\.longitude').val() }, function(data) { $('#councils_text').html(data.councils_text); $('#form_category_row').html(data.category); }); $('#side-form').show(); $('#side').hide(); $('#sub_map_links').hide(); fixmystreet.page = 'new'; location.hash = 'report'; } }); // This function might be passed either an OpenLayers.LonLat (so has // lon and lat) or an OpenLayers.Geometry.Point (so has x and y) function fixmystreet_update_pin(lonlat) { lonlat.transform( fixmystreet.map.getProjectionObject(), new OpenLayers.Projection("EPSG:4326") ); document.getElementById('fixmystreet.latitude').value = lonlat.lat || lonlat.y; document.getElementById('fixmystreet.longitude').value = lonlat.lon || lonlat.x; } function fixmystreet_activate_drag() { fixmystreet.drag = new OpenLayers.Control.DragFeature( fixmystreet.markers, { onComplete: function(feature, e) { fixmystreet_update_pin( feature.geometry.clone() ); } } ); fixmystreet.map.addControl( fixmystreet.drag ); fixmystreet.drag.activate(); }