Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge commit 'cc282b5f87dd7d2ad23ed2700be3210e0e04aeb2' into HEAD

  • Loading branch information...
commit 18bae355b31a812b7d4eab10af1a5dc5058d613f 2 parents 07ca760 + cc282b5
Jenkins authored
View
20 lib/validations.rb
@@ -4,6 +4,11 @@ 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 = {}
@@ -167,7 +172,7 @@ def self.doesFieldHaveValue(field_value)
end
def self.validate_upload_file(field_name, upload_file)
- if upload_file[:tempfile].size > MAX_UPLOAD_FILE_SIZE_IN_BYTE
+ 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
@@ -195,4 +200,17 @@ def self.validate_not_before_date_is_equal_or_greater_than_need_by(not_before, n
@@errors["Not before"] = message
end
end
+
+ def self.validate_file_type(field_name, file_type)
+ @@errors ||= {}
+ valid = false
+ type = file_type.split("/")
+
+ if @valid_file_type[type[0]] && (type[1] =~ @valid_file_type[type[0]])
+ valid = true
+ else
+ @@errors[field_name] = "Only text, word and pdf file allowed."
+ end
+ valid
+ end
end
View
7 lib/zendesk_ticket.rb
@@ -130,7 +130,12 @@ def format_comment_for_amend_content(params)
end
end
- comments.join("\n\n")
+ if !comments.join.empty?
+ comments.join("\n\n")
+ else
+ comments.join
+ end
+
end
def remove_space_from_phone_number(number)
View
52 spec/file_type_spec.rb
@@ -0,0 +1,52 @@
+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
Please sign in to comment.
Something went wrong with that request. Please try again.