From b606779f57f715fde951ca815199402cac83295e Mon Sep 17 00:00:00 2001 From: Connor <78193645+Connor-Bernard@users.noreply.github.com> Date: Wed, 17 Apr 2024 16:28:32 -0700 Subject: [PATCH] [FEAT] Validation Helper (#33) * feat: add validation helper * test: add test coverage for validation helper * chore: rename 'Errors' dir to 'errors' --- app/errors/validation_error.rb | 11 ++ app/helpers/canvas_validation_helper.rb | 63 ++++++++++ spec/Helpers/canvas_validation_helper_spec.rb | 112 ++++++++++++++++++ 3 files changed, 186 insertions(+) create mode 100644 app/errors/validation_error.rb create mode 100644 app/helpers/canvas_validation_helper.rb create mode 100644 spec/Helpers/canvas_validation_helper_spec.rb diff --git a/app/errors/validation_error.rb b/app/errors/validation_error.rb new file mode 100644 index 0000000..0feb998 --- /dev/null +++ b/app/errors/validation_error.rb @@ -0,0 +1,11 @@ +## +# Base class for a validation error. +class ValidationError < StandardError + ## + # Constructor for error. + # + # @param message the error message (defaults to "Validation Error"). + def initialize(message="Validation Error") + super(message) + end +end diff --git a/app/helpers/canvas_validation_helper.rb b/app/helpers/canvas_validation_helper.rb new file mode 100644 index 0000000..663db84 --- /dev/null +++ b/app/helpers/canvas_validation_helper.rb @@ -0,0 +1,63 @@ +module CanvasValidationHelper + OVERRIDE_TITLE_MAX_CHARACTERS = 40 + + ## + # Checks if the provided course id is valid. + # + # @param [Integer] courseId the course id to check. + # @return [Boolean] whether the provided id is valid. + def is_valid_course_id(courseId) + courseId.is_a?(Integer) && courseId > 0 + end + + ## + # Checks if the provided assignment id is valid. + # + # @param [Integer] assignmentId the assignment id to check. + # @return [Boolean] whether the assignment id is valid. + def is_valid_assignment_id(assignmentId) + assignmentId.is_a?(Integer) && assignmentId > 0 + end + + ## + # Checks if the student id is valid. + # + # @param [Integer] studentId the student id to check. + # @return [Boolean] whether the student id is valid. + def is_valid_student_id(studentId) + studentId.is_a?(Integer) && studentId > 0 + end + + ## + # Checks if the list of student ids is valid. + # + # @param [Enumerable] the list of student ids to validate. + # @return [Boolean] whether all student ids in the list are valid. + def is_valid_student_ids(studentIds) + studentIds.all? do |studentId| + is_valid_student_id(studentId) + end + end + + ## + # Checks to see if the provided override title is valid. + # + # @param [String] title the title to check. + # @return [Boolean] whether the title is valid. + def is_valid_title(title) + /^[A-Za-z0-9\-_ ]*$/.match?(title) && title.length < OVERRIDE_TITLE_MAX_CHARACTERS + end + + ## + # Checks whether the provided date is a valid Canvas date. + # TODO: maybe want to refine this to actually valid dates not just format? + # + # @param [String] date the date string to validate. + # @return [Boolean] whether the date string is valid. + def is_valid_date(date) + if (date == nil) + return true + end + /^[0-9]{4}\-[0-9]{2}\-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$/.match?(date) + end +end diff --git a/spec/Helpers/canvas_validation_helper_spec.rb b/spec/Helpers/canvas_validation_helper_spec.rb new file mode 100644 index 0000000..cbfe889 --- /dev/null +++ b/spec/Helpers/canvas_validation_helper_spec.rb @@ -0,0 +1,112 @@ +require 'rails_helper' +require_relative '../../app/helpers/canvas_validation_helper' + +describe 'CanvasValidationHelper', type: :helper do + class Helper + include CanvasValidationHelper + end + attr_reader :helper + + before do + @helper = Helper.new + end + + describe 'is_valid_course_id' do + it 'returns true on positive integer input' do + expect(helper.is_valid_course_id(16)).to be(true) + end + + it 'returns false on negative integer input' do + expect(helper.is_valid_course_id(-16)).to be(false) + end + + it 'returns false on boolean input' do + expect(helper.is_valid_course_id(0.1)).to be(false) + end + + it 'returns false on alphabetical input' do + expect(helper.is_valid_course_id('abc')).to be(false) + end + end + + describe 'is_valid_assignment_id' do + it 'returns true on positive integer input' do + expect(helper.is_valid_assignment_id(16)).to be(true) + end + + it 'returns false on negative integer input' do + expect(helper.is_valid_assignment_id(-16)).to be(false) + end + + it 'returns false on boolean input' do + expect(helper.is_valid_assignment_id(0.1)).to be(false) + end + + it 'returns false on alphabetical input' do + expect(helper.is_valid_assignment_id('abc')).to be(false) + end + end + + describe 'is_valid_student_id' do + it 'returns true on positive integer input' do + expect(helper.is_valid_student_id(16)).to be(true) + end + + it 'returns false on negative integer input' do + expect(helper.is_valid_student_id(-16)).to be(false) + end + + it 'returns false on boolean input' do + expect(helper.is_valid_student_id(0.1)).to be(false) + end + + it 'returns false on alphabetical input' do + expect(helper.is_valid_student_id('abc')).to be(false) + end + end + + describe 'is_valid_student_ids' do + it 'returns true on positive integral input' do + expect(helper.is_valid_student_ids([16, 18])).to be(true) + end + + it 'returns false on negative integer input' do + expect(helper.is_valid_student_ids([-16, 16])).to be(false) + end + + it 'returns false on boolean input' do + expect(helper.is_valid_student_ids([16, 0.1])).to be(false) + end + + it 'returns false on alphabetical input' do + expect(helper.is_valid_student_ids(['abc', 16])).to be(false) + end + end + describe 'is_valid_title' do + it 'returns true on valid input' do + expect(helper.is_valid_title('hello world')).to be(true) + end + + it 'returns false invalid characters' do + expect(helper.is_valid_title('**')).to be(false) + end + + it 'returns false on too long input' do + expect(helper.is_valid_title('A' * 50)).to be(false) + end + end + + describe 'is_valid_date' do + it 'returns true on properly formatted dates' do + expect(helper.is_valid_date('2002-03-16T12:00:00Z')).to be(true) + end + + it 'returns false on inproperly formatted dates' do + expect(helper.is_valid_date('March 16th 2002')).to be(false) + end + + it 'returns false on strings with too much text' do + expect(helper.is_valid_date('2002-03-16T12:00:00Z*')).to be(false) + end + end +end