Skip to content
This repository
Browse code

Merge commit '5992b1b105648e44d71d188022e2c12ad7c8e08e' into HEAD

  • Loading branch information...
commit cc897f5a9057c473ab157ba9cb2838706b41e755 2 parents 978c1aa + 5992b1b
authored October 16, 2012
38  lib/validations.rb
@@ -2,13 +2,6 @@
2 2
 
3 3
 class Guard
4 4
 
5  
-  MAX_UPLOAD_FILE_SIZE_IN_BYTE = 20971520 #20MB
6  
-
7  
-  @valid_file_type = {
8  
-      "text" => /.*/,
9  
-      "application" => /-officedocument|pdf/
10  
-  }
11  
-
12 5
   #Content validations
13 6
   def self.validationsForAmendContent(form_data)
14 7
     @@errors = {}
@@ -22,37 +15,22 @@ def self.validationsForAmendContent(form_data)
22 15
     self.validate_date_is_equal_or_greater_than_today("Need by", need_by, "Changes can only be made after today.")
23 16
     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.")
24 17
 
25  
-    if form_data[:uploaded_data] && self.doesFieldHaveValue(form_data[:uploaded_data][:filename])
26  
-      validate_upload_file("uploaded_data", form_data[:uploaded_data])
27  
-    end
28 18
     @@errors
29 19
   end
30 20
 
31 21
   #User validations
32 22
   def self.validationsForCreateUser(form_data)
33 23
     @@errors = {}
34  
-    if form_data[:uploaded_data] && self.doesFieldHaveValue(form_data[:uploaded_data][:filename])
35  
-      required = ["name", "email", "job"]
36  
-    else
37  
-      required = ["name", "email", "job", "user_name", "user_email"]
38  
-    end
  24
+    required = ["name", "email", "job", "user_name", "user_email"]
39 25
     validate(form_data, required, {"phone" => form_data["phone"]}, {"email" => form_data["email"]})
40 26
 
41  
-    if form_data[:uploaded_data] && self.doesFieldHaveValue(form_data[:uploaded_data][:filename])
42  
-      validate_upload_file("uploaded_data", form_data[:uploaded_data])
43  
-    end
44  
-
45 27
     @@errors
46 28
   end
47 29
 
48 30
   def self.validationsForDeleteUser(form_data)
49 31
     @@errors = {}
50 32
 
51  
-    if form_data[:uploaded_data] && self.doesFieldHaveValue(form_data[:uploaded_data][:filename])
52  
-      required = ["name", "email", "job"]
53  
-    else
54  
-      required = ["name", "email", "job", "user_name", "user_email"]
55  
-    end
  33
+    required = ["name", "email", "job", "user_name", "user_email"]
56 34
 
57 35
     validate(form_data, required, {"phone" => form_data["phone"]}, {"email" => form_data["email"]})
58 36
     self.checkOptionalDateFieldsAreComplete(form_data, [["Not before", "not_before_day", "not_before_month", "not_before_year"]])
@@ -60,10 +38,6 @@ def self.validationsForDeleteUser(form_data)
60 38
     not_before = validate_date_in_valid_range("Not_before", "not_before_day", "not_before_month", "not_before_year", form_data)
61 39
     self.validate_date_is_equal_or_greater_than_today("Not before", not_before, "Not before date should be the same or later than today.")
62 40
 
63  
-    if form_data[:uploaded_data] && self.doesFieldHaveValue(form_data[:uploaded_data][:filename])
64  
-      validate_upload_file("uploaded_data", form_data[:uploaded_data])
65  
-    end
66  
-
67 41
     @@errors
68 42
   end
69 43
 
@@ -163,12 +137,6 @@ def self.doesFieldHaveValue(field_value)
163 137
     field_value && !field_value.strip.empty?
164 138
   end
165 139
 
166  
-  def self.validate_upload_file(field_name, upload_file)
167  
-    if validate_file_type(field_name, upload_file[:type]) && upload_file[:tempfile].size > MAX_UPLOAD_FILE_SIZE_IN_BYTE
168  
-      @@errors[field_name] = "The attached file, #{upload_file[:filename]}, is bigger than 20MB size limitation."
169  
-    end
170  
-  end
171  
-
172 140
   def self.validate_date_in_valid_range(date_field_name, day, month, year, form_data)
173 141
     if !form_data[day].empty? && !form_data[month].empty? && !form_data[year].empty?
174 142
       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)
197 165
     other_organisation_is_required = form_data[:organisation].empty? || form_data[:organisation] == "other_organisation"
198 166
 
199 167
     if other_organisation_is_required &&
200  
-        (form_data[:other_organisation] && form_data[:other_organisation].strip.empty?)
  168
+      (form_data[:other_organisation] && form_data[:other_organisation].strip.empty?)
201 169
       @@errors["Organisation"] = "Organisation information is required for a valid request."
202 170
     end
203 171
 
9  lib/views/content/amend.erb
@@ -20,15 +20,6 @@
20 20
 
21 21
   <div style="clear:both;"></div>
22 22
 
23  
-  <fieldset>
24  
-    <div class="upload">
25  
-      <label for="file-added">Supporting documentation (optional)</label>
26  
-      <br/>
27  
-      <input id="file-added" name="uploaded_data" type="file" accept="text/*, application/pdf, .xls, .xlsx"/>
28  
-      <%= erb :errors, :locals => {"key" => "uploaded_data"} %>
29  
-    </div>
30  
-  </fieldset>
31  
-
32 23
   <%= 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]} %>
33 24
   <%= erb :errors, :locals => {"key" => "Need by"} %>
34 25
 
14  lib/views/upload.erb
... ...
@@ -1,14 +0,0 @@
1  
-<fieldset>
2  
-  <fieldset>
3  
-    <label style="height:20px;">Alternatively</label>
4  
-  </fieldset>
5  
-
6  
-  <div class="line"></div>
7  
-
8  
-  <div class="upload">
9  
-    <label for="<%= id %>"><%= uploadmessage %> (20MB limit) </label>
10  
-    <br/>
11  
-    <input id="<%= id %>" name="<%= name %>" type="file" accept="text/*, application/pdf, .xls, .xlsx"/>
12  
-    <%= erb :errors, :locals => {"key" => name} %>
13  
-  </div>
14  
-</fieldset>
2  lib/views/useraccess/user.erb
@@ -19,6 +19,4 @@
19 19
     </div>
20 20
   </fieldset>
21 21
 
22  
-  <%= erb :upload, :locals => {"id" => "file-input", "name" => "uploaded_data", "uploadmessage" => "If you have multiple users to be added, you can upload a file here"}%>
23  
-
24 22
 </section>
2  lib/views/useraccess/userremove.erb
@@ -20,6 +20,4 @@
20 20
     </div>
21 21
   </fieldset>
22 22
 
23  
-  <%= erb :upload, :locals => {"id" => "file-input", "name" => "uploaded_data", "uploadmessage" => "If you have multiple users to be removed, you can upload a file here"}%>
24  
-
25 23
 </section>
41  lib/zendesk_request.rb
@@ -17,37 +17,18 @@ def self.get_organisations(client)
17 17
   def self.raise_zendesk_request(client, params, from_route)
18 18
     ticket_to_raise = ZendeskTicket.new(client, params, from_route)
19 19
 
20  
-    if ticket_to_raise.has_attachments
21  
-      create_ticket_with_attachment(client, ticket_to_raise)
22  
-    else
23  
-      client.ticket.create(
24  
-          :subject => ticket_to_raise.subject,
25  
-          :description => "Created via Govt API",
26  
-          :priority => "normal",
27  
-          :requester => {"locale_id" => 1, "name" => ticket_to_raise.name, "email" => ticket_to_raise.email},
28  
-          :fields => [{"id" => "21494928", "value" => ticket_to_raise.organisation},
29  
-                      {"id" => "21487987", "value" => ticket_to_raise.job},
30  
-                      {"id" => "21471291", "value" => ticket_to_raise.phone},
31  
-                      {"id" => "21485833", "value" => ticket_to_raise.need_by_date},
32  
-                      {"id" => "21502036", "value" => ticket_to_raise.not_before_date}],
33  
-          :tags => [ticket_to_raise.tag],
34  
-          :comment => {:value => ticket_to_raise.comment})
35  
-    end
36  
-  end
37  
-
38  
-  def self.create_ticket_with_attachment(client, ticket_to_raise)
39 20
     client.ticket.create(
40  
-        :subject => ticket_to_raise.subject,
41  
-        :description => "Created via Govt API",
42  
-        :priority => "normal",
43  
-        :requester => {"locale_id" => 1, "name" => ticket_to_raise.name, "email" => ticket_to_raise.email},
44  
-        :fields => [{"id" => "21494928", "value" => ticket_to_raise.organisation},
45  
-                    {"id" => "21487987", "value" => ticket_to_raise.job},
46  
-                    {"id" => "21471291", "value" => ticket_to_raise.phone},
47  
-                    {"id" => "21485833", "value" => ticket_to_raise.need_by_date},
48  
-                    {"id" => "21502036", "value" => ticket_to_raise.not_before_date}],
49  
-        :tags => [ticket_to_raise.tag],
50  
-        :comment => {:value => ticket_to_raise.comment, :uploads => ticket_to_raise.file_token})
  21
+      :subject => ticket_to_raise.subject,
  22
+      :description => "Created via Govt API",
  23
+      :priority => "normal",
  24
+      :requester => {"locale_id" => 1, "name" => ticket_to_raise.name, "email" => ticket_to_raise.email},
  25
+      :fields => [{"id" => "21494928", "value" => ticket_to_raise.organisation},
  26
+                  {"id" => "21487987", "value" => ticket_to_raise.job},
  27
+                  {"id" => "21471291", "value" => ticket_to_raise.phone},
  28
+                  {"id" => "21485833", "value" => ticket_to_raise.need_by_date},
  29
+                  {"id" => "21502036", "value" => ticket_to_raise.not_before_date}],
  30
+      :tags => [ticket_to_raise.tag],
  31
+      :comment => {:value => ticket_to_raise.comment})
51 32
   end
52 33
 
53 34
 end
34  lib/zendesk_ticket.rb
@@ -56,15 +56,8 @@ def initialize(client, params, from_route)
56 56
       @not_before_date = params[:not_before_day] + "/" + params[:not_before_month] + "/" + params[:not_before_year]
57 57
     end
58 58
 
59  
-    check_for_attachments(client, params)
60 59
   end
61 60
 
62  
-
63  
-  def has_attachments
64  
-    @file_token.length > 0
65  
-  end
66  
-
67  
-
68 61
   private
69 62
 
70 63
   def has_value(param)
@@ -152,33 +145,6 @@ def build_full_url_path(partial_path)
152 145
     end
153 146
   end
154 147
 
155  
-#  attachments
156  
-  def upload_file_to_create_file_token(client, tempfile, filename)
157  
-    directory = "./tmp"
158  
-    path = File.join(directory, filename)
159  
-    File.open(path, "wb") { |f| f.write(tempfile.read) }
160  
-    file_token = upload_file(client, path)
161  
-    File.delete(path)
162  
-    file_token
163  
-  end
164  
-
165  
-  def upload_file(client, path)
166  
-    upload = ZendeskAPI::Upload.create(client, :file => File.open(path))
167  
-    upload.token
168  
-  end
169  
-
170  
-  def check_for_attachments(client, params)
171  
-    @file_token = []
172  
-    if params[:uploaded_data] && doesFieldHaveValue(params[:uploaded_data][:filename])
173  
-      tempfile = params[:uploaded_data][:tempfile]
174  
-      filename = params[:uploaded_data][:filename]
175  
-      @file_token << upload_file_to_create_file_token(client, tempfile, filename)
176  
-    end
177  
-
178  
-    if !@file_token.empty? && @comment.empty?
179  
-      @comment = "[Attachment(s)]"
180  
-    end
181  
-  end
182 148
 
183 149
   def doesFieldHaveValue(field_value)
184 150
     field_value && !field_value.strip.empty?
52  spec/file_type_spec.rb
... ...
@@ -1,52 +0,0 @@
1  
-require "test/unit"
2  
-require "rack/test"
3  
-
4  
-require_relative "../lib/validations"
5  
-
6  
-class ValidationTest < Test::Unit::TestCase
7  
-  include Rack::Test::Methods
8  
-
9  
-  def test_should_return_true_when_file_type_is_valid
10  
-    #Given
11  
-    file_type_text = "text/csv"
12  
-    file_type_word = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
13  
-    file_type_pdf = "application/pdf"
14  
-    field_name = "Uploaded data"
15  
-
16  
-    #When
17  
-    is_valid_text = Guard.validate_file_type(field_name,file_type_text)
18  
-    is_valid_word = Guard.validate_file_type(field_name, file_type_word)
19  
-    is_valid_pdf = Guard.validate_file_type(field_name, file_type_pdf)
20  
-
21  
-    #Then
22  
-    assert(is_valid_text)
23  
-    assert(is_valid_word)
24  
-    assert(is_valid_pdf)
25  
-  end
26  
-
27  
-  def test_should_return_false_with_wrong_category
28  
-    #Given
29  
-    expected_error_message = "Only text, word and pdf file allowed."
30  
-    file_type_image = "image/png"
31  
-    field_name = "Uploaded data"
32  
-
33  
-    #When
34  
-    is_valid = Guard.validate_file_type(field_name, file_type_image)
35  
-
36  
-    #Then
37  
-    assert(!is_valid)
38  
-  end
39  
-
40  
-  def test_should_return_false_unsupported_type_in_application_category
41  
-    #Given
42  
-    expected_error_message = "Only text, word and pdf file allowed."
43  
-    file_type_image = "application/atom+xml"
44  
-    field_name = "Uploaded data"
45  
-
46  
-    #When
47  
-    is_valid = Guard.validate_file_type(field_name, file_type_image)
48  
-
49  
-    #Then
50  
-    assert(!is_valid)
51  
-  end
52  
-end

0 notes on commit cc897f5

Please sign in to comment.
Something went wrong with that request. Please try again.