Permalink
Browse files

Merge pull request #24 from alphagov/drying_functional_tests

DRYing the functional tests
  • Loading branch information...
2 parents d59d353 + e906e28 commit dbbc930bc1c7369760e0f1f06fa648ca6504506a @mnowster mnowster committed Dec 11, 2012
@@ -1,49 +0,0 @@
-require "test_helper"
-
-class ContentChangeRequestsControllerTest < ActionController::TestCase
- include TestData
-
- setup do
- login_as_stub_user
- @zendesk_api = ZenDeskAPIClientDouble.new
- ZendeskClient.stubs(:get_client).returns(@zendesk_api)
- end
-
- context "a new content change request" do
- should "render the form" do
- get :new
- assert_select "h1", /Request a change to existing GOV.UK content/i
- end
- end
-
- context "a submitted content change request" do
- should "reject invalid change requests" do
- params = valid_content_change_request_params.tap {|p| p["content_change_request"]["requester_attributes"].merge!("email" => "")}
- post :create, params
- assert_response 400
- assert_template "new"
- assert_select ".help-inline", /can't be blank/
- end
-
- should "submit it to ZenDesk" do
- params = valid_content_change_request_params
- post :create, params
-
- assert_equal ['content_amend'], @zendesk_api.ticket.tags
-
- assert_redirected_to "/acknowledge"
- end
-
- context "concerning Inside Government" do
- should "tag the ticket with an inside_government tag" do
- params = valid_content_change_request_params.tap {|p| p["content_change_request"].merge!("request_context" => "inside_government")}
-
- post :create, params
-
- assert_includes @zendesk_api.ticket.tags, 'inside_government'
-
- assert_redirected_to "/acknowledge"
- end
- end
- end
-end
@@ -9,22 +9,7 @@ class CreateNewUserRequestsControllerTest < ActionController::TestCase
ZendeskClient.stubs(:get_client).returns(@zendesk_api)
end
- context "new user creation request" do
- should "render the form" do
- get :new
- assert_select "h1", /Create a new user account/i
- end
- end
-
context "submitted user creation request" do
- should "reject invalid requests" do
- params = valid_create_new_user_request_params.tap {|p| p["create_new_user_request"]["requester_attributes"].merge!("email" => "")}
- post :create, params
- assert_response 400
- assert_template "new"
- assert_select ".help-inline", /can't be blank/
- end
-
should "submit it to ZenDesk" do
params = valid_create_new_user_request_params
@@ -9,33 +9,7 @@ class GeneralRequestsControllerTest < ActionController::TestCase
ZendeskClient.stubs(:get_client).returns(@zendesk_api)
end
- context "a new general request" do
- should "render the form" do
- get :new
- assert_select "h1", /Report a problem, request GDS support, or make a suggestion/i
- end
- end
-
context "a submitted general request" do
- should "reject invalid requests" do
- params = valid_general_request_params.tap {|p| p["general_request"]["requester_attributes"].merge!("email" => "")}
-
- post :create, params
-
- assert_response 400
- assert_template :new
- assert_select ".help-inline", /can't be blank/
- end
-
- should "submit it to ZenDesk" do
- params = valid_general_request_params
-
- post :create, params
-
- assert_equal ['govt_agency_general'], @zendesk_api.ticket.tags
- assert_redirected_to "/acknowledge"
- end
-
should "add the user agent to the ticket in the comments" do
request.user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2)"
params = valid_general_request_params
@@ -1,49 +0,0 @@
-require 'test_helper'
-
-class RemoveUserRequestsControllerTest < ActionController::TestCase
- include TestData
-
- setup do
- login_as_stub_user
- @zendesk_api = ZenDeskAPIClientDouble.new
- ZendeskClient.stubs(:get_client).returns(@zendesk_api)
- end
-
- context "new remove user request" do
- should "render the form" do
- get :new
- assert_select "h1", /Remove User/i
- end
- end
-
- context "submitted remove user request" do
- should "reject invalid requests" do
- params = valid_remove_user_request_params.tap {|p| p["remove_user_request"]["requester_attributes"].merge!("email" => "")}
- post :create, params
- assert_response 400
- assert_template "new"
- assert_select ".help-inline", /can't be blank/
- end
-
- should "submit it to ZenDesk" do
- params = valid_remove_user_request_params
-
- post :create, params
-
- assert_equal ['remove_user'], @zendesk_api.ticket.tags
- assert_redirected_to "/acknowledge"
- end
-
- context "concerning Inside Government" do
- should "tag the ticket with an inside_government tag" do
- params = valid_remove_user_request_params.tap {|p| p["remove_user_request"].merge!("tool_role" => "inside_government_editor")}
-
- post :create, params
-
- assert_includes @zendesk_api.ticket.tags, 'inside_government'
-
- assert_redirected_to "/acknowledge"
- end
- end
- end
-end
@@ -0,0 +1,99 @@
+require 'test_helper'
+require 'tableless_model'
+require 'zendesk_ticket'
+
+class TestRequest < TablelessModel
+ include WithRequester
+
+ attr_accessor :a, :b
+ validates_presence_of :a
+end
+
+class TestZendeskTicket < ZendeskTicket
+ def subject
+ "Test request"
+ end
+
+ def tags
+ ["tag_a", "tag_b"]
+ end
+
+ def comment_snippets
+ []
+ end
+end
+
+class TestRequestsController < RequestsController
+ def new_request
+ TestRequest.new(:requester => Requester.new)
+ end
+
+ def zendesk_ticket_class
+ TestZendeskTicket
+ end
+
+ def parse_request_from_params
+ TestRequest.new(params[:test_request])
+ end
+end
+
+def valid_params_for_test_request
+ {
+ "test_request" => {
+ "a" => "A string", "b" => "Another string",
+ "requester_attributes" => {"email" => "abc@d.com"}
+ }
+ }
+end
+
+class RequestsControllerTest < ActionController::TestCase
+ setup do
+ login_as_stub_user
+ @zendesk_api = ZenDeskAPIClientDouble.new
+ ZendeskClient.stubs(:get_client).returns(@zendesk_api)
+
+ Rails.application.routes.draw do
+ match 'new' => "test_requests#new"
+ match 'create' => "test_requests#create"
+ end
+
+ @controller = TestRequestsController.new
+ prevent_implicit_rendering
+ end
+
+ teardown do
+ Rails.application.reload_routes!
+ end
+
+ context "a new general request" do
+ should "render the form" do
+ @controller.expects(:default_render)
+ get :new
+ end
+ end
+
+ def prevent_implicit_rendering
+ # we're not testing view rendering here,
+ # so prevent rendering by stubbing out default_render
+ @controller.stubs(:default_render)
+ end
+
+ context "a submission of a test request" do
+ should "reject invalid parameters" do
+ params = valid_params_for_test_request.tap {|p| p["test_request"].merge!("a" => "")}
+
+ @controller.expects(:render).with(:new, has_entry(:status => 400))
+
+ post :create, params
+ end
+
+ should "submit it to ZenDesk" do
+ params = valid_params_for_test_request
+
+ post :create, params
+
+ assert_equal ['tag_a', 'tag_b'], @zendesk_api.ticket.tags
+ assert_redirected_to "/acknowledge"
+ end
+ end
+end
View
@@ -1,14 +1,4 @@
module TestData
- def valid_content_change_request_params
- {"content_change_request" =>
- { "requester_attributes" => valid_requester_params,
- "time_constraint_attributes" => valid_time_constraint_params,
- "url1" => "https://www.gov.uk",
- "details_of_change" => "Content is wrong",
- "request_context" => "mainstream" }
- }
- end
-
def valid_create_new_user_request_params
{ "create_new_user_request" =>
{ "requester_attributes" => valid_requester_params,
@@ -18,17 +8,6 @@ def valid_create_new_user_request_params
}
end
- def valid_remove_user_request_params
- { "remove_user_request" =>
- { "requester_attributes" => valid_requester_params,
- "time_constraint_attributes" => {"not_before_date" => "01-12-2022"},
- "user_name"=>"subject",
- "user_email"=>"subject@digital.cabinet-office.gov.uk",
- "tool_role" => "govt_form",
- "additional_comments"=>"" }
- }
- end
-
def valid_general_request_params
{"general_request" =>
{ "requester_attributes" => valid_requester_params,
@@ -49,11 +28,4 @@ def valid_requested_user_params
"phone"=>"12345",
}
end
-
- def valid_time_constraint_params
- { "needed_by_date" => "01-01-2023",
- "not_before_date" => "01-12-2022",
- "time_constraint_reason" => "Legal requirement"
- }
- end
end

0 comments on commit dbbc930

Please sign in to comment.