Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/custom_wizard/validators/update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,10 @@ def validate_answer(field, field_id, value, label)
end

def validate_file_type(value, file_types)
file_types
.split(",")
.map { |t| t.gsub(".", "") }
.include?(File.extname(value["original_filename"])[1..-1])
extension = File.extname(value["original_filename"].to_s).delete_prefix(".").downcase
return false if extension.blank?

file_types.split(",").map { |t| t.strip.delete_prefix(".").downcase }.include?(extension)
end

def validate_date(value, format)
Expand Down
2 changes: 1 addition & 1 deletion plugin.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true
# name: discourse-custom-wizard
# about: Forms for Discourse. Better onboarding, structured posting, data enrichment, automated actions and much more.
# version: 2.18.16
# version: 2.18.17
# authors: Angus McLeod, Faizaan Gagan, Robert Barrow, Keegan George, Kaitlin Maddever, Marcos Gutierrez
# url: https://github.com/paviliondev/discourse-custom-wizard
# contact_emails: development@pavilion.tech
Expand Down
43 changes: 43 additions & 0 deletions spec/components/custom_wizard/update_validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,49 @@ def perform_validation(step_id, submission)
end
end

context "upload fields" do
let(:upload_field) { get_wizard_fixture("field/upload") }

def add_upload_field(file_types)
template[:steps][1][:fields] << upload_field.merge(file_types: file_types)
CustomWizard::Template.save(template)
end

def upload_errors(filename)
updater = perform_validation("step_2", step_2_field_7: { "original_filename" => filename })
updater.errors.messages[:step_2_field_7]
end

it "accepts files with an allowed extension" do
add_upload_field(".jpg,.jpeg,.png")
expect(upload_errors("photo.jpg")).to be_blank
end

it "rejects files with an extension that isn't allowed" do
add_upload_field(".jpg,.jpeg,.png")
expect(upload_errors("document.pdf").first).to eq(
I18n.t("wizard.field.invalid_file", label: "Upload", types: ".jpg,.jpeg,.png"),
)
expect(upload_errors("photo")).to be_present
end

it "ignores the case of the extension" do
add_upload_field(".jpg,.jpeg,.png")
expect(upload_errors("PHOTO.JPG")).to be_blank
end

it "ignores the case of the allowed file types" do
add_upload_field(".JPG,.PNG")
expect(upload_errors("photo.Png")).to be_blank
end

it "accepts file types written without a dot or with spaces" do
add_upload_field(".jpg, .png,webp")
expect(upload_errors("image.webp")).to be_blank
expect(upload_errors("photo.png")).to be_blank
end
end

context "answer validation" do
def set_answer_validation(opts)
@template[:steps][0][:fields][0][:validations] = { answer: { status: true }.merge(opts) }
Expand Down
Loading