Permalink
Browse files

Merge commit '5992b1b105648e44d71d188022e2c12ad7c8e08e' into HEAD

  • Loading branch information...
2 parents 978c1aa + 5992b1b commit cc897f5a9057c473ab157ba9cb2838706b41e755 Jenkins committed Oct 16, 2012
View
@@ -2,13 +2,6 @@
class Guard
- MAX_UPLOAD_FILE_SIZE_IN_BYTE = 20971520 #20MB
-
- @valid_file_type = {
- "text" => /.*/,
- "application" => /-officedocument|pdf/
- }
-
#Content validations
def self.validationsForAmendContent(form_data)
@@errors = {}
@@ -22,48 +15,29 @@ def self.validationsForAmendContent(form_data)
self.validate_date_is_equal_or_greater_than_today("Need by", need_by, "Changes can only be made after today.")
self.validate_not_before_date_is_equal_or_greater_than_need_by(not_before, need_by, "Not before date should be the same or later than the date which the changes are required to be made on.")
- if form_data[:uploaded_data] && self.doesFieldHaveValue(form_data[:uploaded_data][:filename])
- validate_upload_file("uploaded_data", form_data[:uploaded_data])
- end
@@errors
end
#User validations
def self.validationsForCreateUser(form_data)
@@errors = {}
- if form_data[:uploaded_data] && self.doesFieldHaveValue(form_data[:uploaded_data][:filename])
- required = ["name", "email", "job"]
- else
- required = ["name", "email", "job", "user_name", "user_email"]
- end
+ required = ["name", "email", "job", "user_name", "user_email"]
validate(form_data, required, {"phone" => form_data["phone"]}, {"email" => form_data["email"]})
- if form_data[:uploaded_data] && self.doesFieldHaveValue(form_data[:uploaded_data][:filename])
- validate_upload_file("uploaded_data", form_data[:uploaded_data])
- end
-
@@errors
end
def self.validationsForDeleteUser(form_data)
@@errors = {}
- if form_data[:uploaded_data] && self.doesFieldHaveValue(form_data[:uploaded_data][:filename])
- required = ["name", "email", "job"]
- else
- required = ["name", "email", "job", "user_name", "user_email"]
- end
+ required = ["name", "email", "job", "user_name", "user_email"]
validate(form_data, required, {"phone" => form_data["phone"]}, {"email" => form_data["email"]})
self.checkOptionalDateFieldsAreComplete(form_data, [["Not before", "not_before_day", "not_before_month", "not_before_year"]])
not_before = validate_date_in_valid_range("Not_before", "not_before_day", "not_before_month", "not_before_year", form_data)
self.validate_date_is_equal_or_greater_than_today("Not before", not_before, "Not before date should be the same or later than today.")
- if form_data[:uploaded_data] && self.doesFieldHaveValue(form_data[:uploaded_data][:filename])
- validate_upload_file("uploaded_data", form_data[:uploaded_data])
- end
-
@@errors
end
@@ -163,12 +137,6 @@ def self.doesFieldHaveValue(field_value)
field_value && !field_value.strip.empty?
end
- def self.validate_upload_file(field_name, upload_file)
- if validate_file_type(field_name, upload_file[:type]) && upload_file[:tempfile].size > MAX_UPLOAD_FILE_SIZE_IN_BYTE
- @@errors[field_name] = "The attached file, #{upload_file[:filename]}, is bigger than 20MB size limitation."
- end
- end
-
def self.validate_date_in_valid_range(date_field_name, day, month, year, form_data)
if !form_data[day].empty? && !form_data[month].empty? && !form_data[year].empty?
date_to_validate = form_data[day] + "-" + form_data[month] + "-" + form_data[year]
@@ -197,7 +165,7 @@ def self.validate_organisation_or_other_is_entered(form_data)
other_organisation_is_required = form_data[:organisation].empty? || form_data[:organisation] == "other_organisation"
if other_organisation_is_required &&
- (form_data[:other_organisation] && form_data[:other_organisation].strip.empty?)
+ (form_data[:other_organisation] && form_data[:other_organisation].strip.empty?)
@@errors["Organisation"] = "Organisation information is required for a valid request."
end
@@ -20,15 +20,6 @@
<div style="clear:both;"></div>
- <fieldset>
- <div class="upload">
- <label for="file-added">Supporting documentation (optional)</label>
- <br/>
- <input id="file-added" name="uploaded_data" type="file" accept="text/*, application/pdf, .xls, .xlsx"/>
- <%= erb :errors, :locals => {"key" => "uploaded_data"} %>
- </div>
- </fieldset>
-
<%= erb :date, :locals => {:title => "When should this change be made?", :label => 'need_by', :day => @formdata[:need_by_day], :month => @formdata[:need_by_month], :year => @formdata[:need_by_year]} %>
<%= erb :errors, :locals => {"key" => "Need by"} %>
View
@@ -1,14 +0,0 @@
-<fieldset>
- <fieldset>
- <label style="height:20px;">Alternatively</label>
- </fieldset>
-
- <div class="line"></div>
-
- <div class="upload">
- <label for="<%= id %>"><%= uploadmessage %> (20MB limit) </label>
- <br/>
- <input id="<%= id %>" name="<%= name %>" type="file" accept="text/*, application/pdf, .xls, .xlsx"/>
- <%= erb :errors, :locals => {"key" => name} %>
- </div>
-</fieldset>
@@ -19,6 +19,4 @@
</div>
</fieldset>
- <%= erb :upload, :locals => {"id" => "file-input", "name" => "uploaded_data", "uploadmessage" => "If you have multiple users to be added, you can upload a file here"}%>
-
</section>
@@ -20,6 +20,4 @@
</div>
</fieldset>
- <%= erb :upload, :locals => {"id" => "file-input", "name" => "uploaded_data", "uploadmessage" => "If you have multiple users to be removed, you can upload a file here"}%>
-
</section>
View
@@ -17,37 +17,18 @@ def self.get_organisations(client)
def self.raise_zendesk_request(client, params, from_route)
ticket_to_raise = ZendeskTicket.new(client, params, from_route)
- if ticket_to_raise.has_attachments
- create_ticket_with_attachment(client, ticket_to_raise)
- else
- client.ticket.create(
- :subject => ticket_to_raise.subject,
- :description => "Created via Govt API",
- :priority => "normal",
- :requester => {"locale_id" => 1, "name" => ticket_to_raise.name, "email" => ticket_to_raise.email},
- :fields => [{"id" => "21494928", "value" => ticket_to_raise.organisation},
- {"id" => "21487987", "value" => ticket_to_raise.job},
- {"id" => "21471291", "value" => ticket_to_raise.phone},
- {"id" => "21485833", "value" => ticket_to_raise.need_by_date},
- {"id" => "21502036", "value" => ticket_to_raise.not_before_date}],
- :tags => [ticket_to_raise.tag],
- :comment => {:value => ticket_to_raise.comment})
- end
- end
-
- def self.create_ticket_with_attachment(client, ticket_to_raise)
client.ticket.create(
- :subject => ticket_to_raise.subject,
- :description => "Created via Govt API",
- :priority => "normal",
- :requester => {"locale_id" => 1, "name" => ticket_to_raise.name, "email" => ticket_to_raise.email},
- :fields => [{"id" => "21494928", "value" => ticket_to_raise.organisation},
- {"id" => "21487987", "value" => ticket_to_raise.job},
- {"id" => "21471291", "value" => ticket_to_raise.phone},
- {"id" => "21485833", "value" => ticket_to_raise.need_by_date},
- {"id" => "21502036", "value" => ticket_to_raise.not_before_date}],
- :tags => [ticket_to_raise.tag],
- :comment => {:value => ticket_to_raise.comment, :uploads => ticket_to_raise.file_token})
+ :subject => ticket_to_raise.subject,
+ :description => "Created via Govt API",
+ :priority => "normal",
+ :requester => {"locale_id" => 1, "name" => ticket_to_raise.name, "email" => ticket_to_raise.email},
+ :fields => [{"id" => "21494928", "value" => ticket_to_raise.organisation},
+ {"id" => "21487987", "value" => ticket_to_raise.job},
+ {"id" => "21471291", "value" => ticket_to_raise.phone},
+ {"id" => "21485833", "value" => ticket_to_raise.need_by_date},
+ {"id" => "21502036", "value" => ticket_to_raise.not_before_date}],
+ :tags => [ticket_to_raise.tag],
+ :comment => {:value => ticket_to_raise.comment})
end
end
View
@@ -56,15 +56,8 @@ def initialize(client, params, from_route)
@not_before_date = params[:not_before_day] + "/" + params[:not_before_month] + "/" + params[:not_before_year]
end
- check_for_attachments(client, params)
end
-
- def has_attachments
- @file_token.length > 0
- end
-
-
private
def has_value(param)
@@ -152,33 +145,6 @@ def build_full_url_path(partial_path)
end
end
-# attachments
- def upload_file_to_create_file_token(client, tempfile, filename)
- directory = "./tmp"
- path = File.join(directory, filename)
- File.open(path, "wb") { |f| f.write(tempfile.read) }
- file_token = upload_file(client, path)
- File.delete(path)
- file_token
- end
-
- def upload_file(client, path)
- upload = ZendeskAPI::Upload.create(client, :file => File.open(path))
- upload.token
- end
-
- def check_for_attachments(client, params)
- @file_token = []
- if params[:uploaded_data] && doesFieldHaveValue(params[:uploaded_data][:filename])
- tempfile = params[:uploaded_data][:tempfile]
- filename = params[:uploaded_data][:filename]
- @file_token << upload_file_to_create_file_token(client, tempfile, filename)
- end
-
- if !@file_token.empty? && @comment.empty?
- @comment = "[Attachment(s)]"
- end
- end
def doesFieldHaveValue(field_value)
field_value && !field_value.strip.empty?
View
@@ -1,52 +0,0 @@
-require "test/unit"
-require "rack/test"
-
-require_relative "../lib/validations"
-
-class ValidationTest < Test::Unit::TestCase
- include Rack::Test::Methods
-
- def test_should_return_true_when_file_type_is_valid
- #Given
- file_type_text = "text/csv"
- file_type_word = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
- file_type_pdf = "application/pdf"
- field_name = "Uploaded data"
-
- #When
- is_valid_text = Guard.validate_file_type(field_name,file_type_text)
- is_valid_word = Guard.validate_file_type(field_name, file_type_word)
- is_valid_pdf = Guard.validate_file_type(field_name, file_type_pdf)
-
- #Then
- assert(is_valid_text)
- assert(is_valid_word)
- assert(is_valid_pdf)
- end
-
- def test_should_return_false_with_wrong_category
- #Given
- expected_error_message = "Only text, word and pdf file allowed."
- file_type_image = "image/png"
- field_name = "Uploaded data"
-
- #When
- is_valid = Guard.validate_file_type(field_name, file_type_image)
-
- #Then
- assert(!is_valid)
- end
-
- def test_should_return_false_unsupported_type_in_application_category
- #Given
- expected_error_message = "Only text, word and pdf file allowed."
- file_type_image = "application/atom+xml"
- field_name = "Uploaded data"
-
- #When
- is_valid = Guard.validate_file_type(field_name, file_type_image)
-
- #Then
- assert(!is_valid)
- end
-end

0 comments on commit cc897f5

Please sign in to comment.