Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #2 from alphagov/content_change_request_refactoring

Content change request form refactoring
  • Loading branch information...
commit 2b93b880c8858880262420ce3e8759c14b75b4fc 2 parents 2ba5394 + 812552f
@jamiecobbett jamiecobbett authored
View
47 app/controllers/application_controller.rb
@@ -1,7 +1,54 @@
+require "zendesk_request"
+require "zendesk_client"
+
class ApplicationController < ActionController::Base
include GDS::SSO::ControllerMethods
before_filter :authenticate_user!
protect_from_forgery
+
+ private
+
+ def on_get(template)
+ begin
+ @client = ZendeskClient.get_client(logger)
+ @organisations = ZendeskRequest.get_organisations(@client)
+ rescue ZendeskError
+ return render :"support/zendesk_connection_error", :layout => "application"
+ end
+
+ @formdata = {}
+ render :"#{template}", :layout => "application"
+ end
+
+ def on_post(params, route)
+ begin
+ @client = ZendeskClient.get_client(logger)
+ @organisations = ZendeskRequest.get_organisations(@client)
+ rescue ZendeskError
+ return render :"support/zendesk_error", :layout => "application"
+ end
+ @formdata = params
+
+ if @errors.empty?
+ ticket = ZendeskRequest.raise_zendesk_request(@client, params, route)
+ if ticket
+ redirect_to '/acknowledge'
+ else
+ return render :"support/zendesk_error", :layout => "application"
+ end
+ else
+ render :"#{@template}", :layout => "application"
+ end
+ end
+
+ def prepopulate_organisation_list
+ begin
+ @client = ZendeskClient.get_client(logger)
+ @organisations = ZendeskRequest.get_organisations(@client)
+ rescue ZendeskError
+ return render :"support/zendesk_connection_error", :layout => "application"
+ end
+ end
end
View
13 app/controllers/content_change_requests_controller.rb
@@ -0,0 +1,13 @@
+class ContentChangeRequestsController < ApplicationController
+ def new
+ @formdata = {}
+ prepopulate_organisation_list
+ end
+
+ def create
+ @template = "content_change_requests/new"
+
+ @errors = Guard.validationsForAmendContent(params)
+ on_post(params, "amend-content")
+ end
+end
View
37 app/controllers/support_controller.rb
@@ -1,5 +1,3 @@
-require "zendesk_request"
-require "zendesk_client"
require "guard"
class SupportController < ApplicationController
@@ -78,39 +76,4 @@ def landing
def acknowledge
render :acknowledge, :layout => "application"
end
-
- private
-
- def on_get(template)
- begin
- @client = ZendeskClient.get_client(logger)
- @organisations = ZendeskRequest.get_organisations(@client)
- rescue ZendeskError
- return render :"zendesk_connection_error", :layout => "application"
- end
-
- @formdata = {}
- render :"#{template}", :layout => "application"
- end
-
- def on_post(params, route)
- begin
- @client = ZendeskClient.get_client(logger)
- @organisations = ZendeskRequest.get_organisations(@client)
- rescue ZendeskError
- return render :"zendesk_error", :layout => "application"
- end
- @formdata = params
-
- if @errors.empty?
- ticket = ZendeskRequest.raise_zendesk_request(@client, params, route)
- if ticket
- redirect_to '/acknowledge'
- else
- return render :"zendesk_error", :layout => "application"
- end
- else
- render :"#{@template}", :layout => "application"
- end
- end
end
View
18 app/views/content/amend.html.erb → app/views/content_change_requests/new.html.erb
@@ -7,21 +7,21 @@
</div>
<div class="well">
-<%= form_tag('/amend-content', :method => :post) do %>
+<%= form_tag(content_change_request_url, :method => :post) do %>
- <%= render "author" %>
+ <%= render "support/author" %>
<fieldset>
<legend>URL(s) of content to be changed</legend>
<label class="visuallyhidden" for="target_url1">URL 1</label>
- <input class="span6 input-block-level" id="target_url1" name="url1" type="text" placeholder="http://www.gov.uk/" <%= render "assignvalue", {:param => @formdata[:url1]} %> >
+ <input class="span6 input-block-level" id="target_url1" name="url1" type="text" placeholder="http://www.gov.uk/" <%= render "support/assignvalue", {:param => @formdata[:url1]} %> >
<label class="visuallyhidden" for="target_url2">URL 2</label>
- <input class="span6 input-block-level" id="target_url2" name="url2" type="text" placeholder="http://www.gov.uk/" <%= render "assignvalue", {:param => @formdata[:url2]} %> >
+ <input class="span6 input-block-level" id="target_url2" name="url2" type="text" placeholder="http://www.gov.uk/" <%= render "support/assignvalue", {:param => @formdata[:url2]} %> >
<label class="visuallyhidden" for="target_url3">URL 3</label>
- <input class="span6 input-block-level" id="target_url3" name="url3" type="text" placeholder="http://www.gov.uk/" <%= render "assignvalue", {:param => @formdata[:url3]} %> >
+ <input class="span6 input-block-level" id="target_url3" name="url3" type="text" placeholder="http://www.gov.uk/" <%= render "support/assignvalue", {:param => @formdata[:url3]} %> >
<div class="comments">
<label for="add_content">Details of what should be added, amended, or removed:</label>
@@ -30,11 +30,11 @@
</fieldset>
- <%= render "date", {:title => "When should this change be published?", :label => 'need_by', :day => @formdata[:need_by_day], :month => @formdata[:need_by_month], :year => @formdata[:need_by_year]} %>
- <%= render "errors", {errors: @errors, key: "Need by"} %>
+ <%= render "support/date", {:title => "When should this change be published?", :label => 'need_by', :day => @formdata[:need_by_day], :month => @formdata[:need_by_month], :year => @formdata[:need_by_year]} %>
+ <%= render "support/errors", {errors: @errors, key: "Need by"} %>
- <%= render "date", {:title => "Not to be amended before (optional)", :label => 'not_before', :day => @formdata[:not_before_day], :month => @formdata[:not_before_month], :year => @formdata[:not_before_year]} %>
- <%= render "errors", {errors: @errors, key: "Not before"} %>
+ <%= render "support/date", {:title => "Not to be amended before (optional)", :label => 'not_before', :day => @formdata[:not_before_day], :month => @formdata[:not_before_month], :year => @formdata[:not_before_year]} %>
+ <%= render "support/errors", {errors: @errors, key: "Not before"} %>
<fieldset>
<div class="comments">
View
2  app/views/layouts/application.html.erb
@@ -33,7 +33,7 @@
<div class="well sidebar-nav">
<ul class="nav nav-list">
<li class="nav-header">Content request</li>
- <%= nav_link 'Content change', '/amend-content' %>
+ <%= nav_link 'Content change', new_content_change_request_url %>
</ul>
<ul class="nav nav-list">
<li class="nav-header">User Access</li>
View
18 app/views/support/_author.erb
@@ -4,21 +4,21 @@
<div class="control-group">
<label for="name">Name (required)</label>
- <input id="name" name="name" type="text" class="span6" required aria-required="true" <%= render "assignvalue", {:param => @formdata[:name]} %> >
- <%= render "errors", {key: "Name", errors: @errors} %>
+ <input id="name" name="name" type="text" class="span6" required aria-required="true" <%= render "support/assignvalue", {:param => @formdata[:name]} %> >
+ <%= render "support/errors", {key: "Name", errors: @errors} %>
</div>
<label for="email">Email (required)</label>
- <input id="email" name="email" type="email" class="span6" required aria-required="true" <%= render "assignvalue", {:param => @formdata[:email]} %> >
- <%= render "errors", {key: "Email", errors: @errors} %>
+ <input id="email" name="email" type="email" class="span6" required aria-required="true" <%= render "support/assignvalue", {:param => @formdata[:email]} %> >
+ <%= render "support/errors", {key: "Email", errors: @errors} %>
<label for="job">Job title (required)</label>
- <input id="job" name="job" type="text" class="span6" required aria-required="true" <%= render "assignvalue", {:param => @formdata[:job]} %> >
- <%= render "errors", {key: "Job", errors: @errors} %>
+ <input id="job" name="job" type="text" class="span6" required aria-required="true" <%= render "support/assignvalue", {:param => @formdata[:job]} %> >
+ <%= render "support/errors", {key: "Job", errors: @errors} %>
<label for="phone">Phone number</label>
- <input class="span6" id="phone" name="phone" pattern="([\d\s]*)" type="tel" <%= render "assignvalue", {:param => @formdata[:phone]} %> >
- <%= render "errors", {key: "Phone", errors: @errors} %>
+ <input class="span6" id="phone" name="phone" pattern="([\d\s]*)" type="tel" <%= render "support/assignvalue", {:param => @formdata[:phone]} %> >
+ <%= render "support/errors", {key: "Phone", errors: @errors} %>
<label for="organisation_list">Organisation (required)</label>
<select id="organisation_list" class="span6" name="organisation">
@@ -30,7 +30,7 @@
<% end %>
<% end %>
</select>
- <%= render "errors", {key: "Organisation", errors: @errors} %>
+ <%= render "support/errors", {key: "Organisation", errors: @errors} %>
<label for="other_organisation">Please specify the organisation if you didn&rsquo;t find it in the list above</label>
<input class="span6" id="other_organisation" name="other_organisation" type="text">
View
3  config/routes.rb
@@ -1,5 +1,6 @@
Support::Application.routes.draw do
- match "amend-content" => "support#amend_content"
+ resource :content_change_request, :only => [:new, :create]
+
match "create-user" => "support#create_user"
match "remove-user" => "support#remove_user"
match "campaign" => "support#campaign"
View
0  test/functional/.gitkeep
No changes.
View
66 test/functional/content_change_requests_controller_test.rb
@@ -0,0 +1,66 @@
+require_relative "../test_helper"
+
+class ContentChangeRequestsControllerTest < ActionController::TestCase
+ include ZenDeskOrganisationListHelper
+
+ setup do
+ login_as_stub_user
+ end
+
+ VALID_CONTENT_CHANGE_REQUEST_PARAMS = {
+ "name"=>"Testing",
+ "email"=>"testing@digital.cabinet-office.gov.uk",
+ "job"=>"Dev",
+ "phone"=>"",
+ "organisation"=>"cabinet_office",
+ "other_organisation"=>"",
+ "url1"=>"",
+ "url2"=>"",
+ "url3"=>"",
+ "add_content"=>"",
+ "need_by_day"=>"",
+ "need_by_month"=>"",
+ "need_by_year"=>"",
+ "not_before_day"=>"",
+ "not_before_month"=>"",
+ "not_before_year"=>"",
+ "additional"=>""
+ }
+
+ context "GET amend_content" do
+ setup do
+ stub_zendesk_organisation_list
+ end
+
+ should "render the form" do
+ get :new
+ assert_select "h1", /Request a change to existing GOV.UK content/i
+ end
+
+ should "use ZenDesk to populate the organisation dropdown" do
+ get :new
+ assert_select "select#organisation_list option", "Advocate General for Scotland"
+ end
+ end
+
+ context "POST amend_content" do
+ setup do
+ stub_zendesk_organisation_list
+ end
+
+ should "reject invalid change requests" do
+ params = VALID_CONTENT_CHANGE_REQUEST_PARAMS.merge("organisation" => "")
+ post :create, params
+ assert_response 200 # should actually be an error status, but let's worry about that later
+ assert_template "new"
+ assert_select ".help-block", /Organisation information is required/
+ end
+
+ should "submit it to ZenDesk" do
+ params = VALID_CONTENT_CHANGE_REQUEST_PARAMS
+ ZendeskRequest.expects(:raise_zendesk_request).returns("not a null")
+ post :create, params
+ assert_redirected_to "/acknowledge"
+ end
+ end
+end
View
107 test/functional/support_controller_test.rb
@@ -1,43 +1,12 @@
require_relative "../test_helper"
class SupportControllerTest < ActionController::TestCase
+ include ZenDeskOrganisationListHelper
+
setup do
login_as_stub_user
end
- def stub_zendesk_organisation_list
- url = %r{https://.*@govuk.zendesk.com/api/v2/ticket_fields/21494928}
- body = {
- "ticket_field" => {
- "url" => "https://govuk.zendesk.com/api/v2/ticket_fields/21494928.json",
- "id"=>21494928,
- "type"=>"tagger",
- "title"=>"Department",
- "description"=>"",
- "position"=>9999,
- "active"=>true,
- "required"=>false,
- "collapsed_for_agents"=>false,
- "regexp_for_validation"=>nil,
- "title_in_portal"=>"Department",
- "visible_in_portal"=>false,
- "editable_in_portal"=>false,
- "required_in_portal"=>false,
- "tag"=>nil,
- "created_at"=>"2012-08-07 08:06:33 UTC",
- "updated_at"=>"2012-08-07 08:06:33 UTC",
- "custom_field_options"=>[
- {"name"=>"Advocate General for Scotland", "value"=>"advocate_general_for_scotland"},
- {"name"=>"Attorney General's Office", "value"=>"attorney_generals_office"},
- {"name"=>"Cabinet Office", "value"=>"cabinet_office"}
- ]
- }
- }
-
- stub_request(:get, url).
- to_return(:status => 200, :body => body.to_json, :headers => {"Content-Type" => "application/json"})
- end
-
context "GET landing" do
should "render the homepage" do
get :landing
@@ -45,78 +14,6 @@ def stub_zendesk_organisation_list
end
end
- context "GET amend_content" do
- setup do
- stub_zendesk_organisation_list
- end
-
- should "render the form" do
- get :amend_content
- assert_select "h1", /Request a change to existing GOV.UK content/i
- end
-
- should "use ZenDesk to populate the organisation dropdown" do
- get :amend_content
- assert_select "select#organisation_list option", "Advocate General for Scotland"
- end
- end
-
- context "POST amend_content" do
- setup do
- stub_zendesk_organisation_list
- end
-
- should "reject invalid change requests" do
- params = {
- "name"=>"Testing",
- "email"=>"testing@digital.cabinet-office.gov.uk",
- "job"=>"Dev",
- "organisation"=>"", # this has to be set
- "other_organisation"=>"",
- "url1"=>"",
- "url2"=>"",
- "url3"=>"",
- "add_content"=>"",
- "need_by_day"=>"",
- "need_by_month"=>"",
- "need_by_year"=>"",
- "not_before_day"=>"",
- "not_before_month"=>"",
- "not_before_year"=>"",
- "additional"=>""
- }
- post :amend_content, params
- assert_response 200 # should actually be an error status, but let's worry about that later
- assert_template "amend"
- assert_select ".help-block", /Organisation information is required/
- end
-
- should "submit it to ZenDesk" do
- params = {
- "name"=>"Testing",
- "email"=>"testing@digital.cabinet-office.gov.uk",
- "job"=>"Dev",
- "phone"=>"",
- "organisation"=>"cabinet_office",
- "other_organisation"=>"",
- "url1"=>"",
- "url2"=>"",
- "url3"=>"",
- "add_content"=>"",
- "need_by_day"=>"",
- "need_by_month"=>"",
- "need_by_year"=>"",
- "not_before_day"=>"",
- "not_before_month"=>"",
- "not_before_year"=>"",
- "additional"=>""
- }
- ZendeskRequest.expects(:raise_zendesk_request).returns("not a null")
- post :amend_content, params
- assert_redirected_to "/acknowledge"
- end
- end
-
context "GET create_user" do
setup do
stub_zendesk_organisation_list
View
33 test/test_helper.rb
@@ -19,4 +19,37 @@ def login_as_stub_user
end
end
+module ZenDeskOrganisationListHelper
+ def stub_zendesk_organisation_list
+ url = %r{https://.*@govuk.zendesk.com/api/v2/ticket_fields/21494928}
+ body = {
+ "ticket_field" => {
+ "url" => "https://govuk.zendesk.com/api/v2/ticket_fields/21494928.json",
+ "id"=>21494928,
+ "type"=>"tagger",
+ "title"=>"Department",
+ "description"=>"",
+ "position"=>9999,
+ "active"=>true,
+ "required"=>false,
+ "collapsed_for_agents"=>false,
+ "regexp_for_validation"=>nil,
+ "title_in_portal"=>"Department",
+ "visible_in_portal"=>false,
+ "editable_in_portal"=>false,
+ "required_in_portal"=>false,
+ "tag"=>nil,
+ "created_at"=>"2012-08-07 08:06:33 UTC",
+ "updated_at"=>"2012-08-07 08:06:33 UTC",
+ "custom_field_options"=>[
+ {"name"=>"Advocate General for Scotland", "value"=>"advocate_general_for_scotland"},
+ {"name"=>"Attorney General's Office", "value"=>"attorney_generals_office"},
+ {"name"=>"Cabinet Office", "value"=>"cabinet_office"}
+ ]
+ }
+ }
+ stub_request(:get, url).
+ to_return(:status => 200, :body => body.to_json, :headers => {"Content-Type" => "application/json"})
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.