From b2825e863a91296d5906b3b2bbaf0c7eb927a5db Mon Sep 17 00:00:00 2001 From: Maciej Koperdowski Date: Mon, 13 May 2024 19:49:10 +0200 Subject: [PATCH] Added regex in validators, changed models into dtos, created unit tests --- .../ReasnAPI.Tests/ReasnAPI.Tests.csproj | 4 + Server/ReasnAPI/ReasnAPI.Tests/UnitTest1.cs | 12 - .../Validators/AddressValidatorTests.cs | 336 ++++++++++++++++ .../Validators/CommentValidatorTests.cs | 65 +++ .../Validators/EventValidatorTests.cs | 257 ++++++++++++ .../Validators/InterestValidatorTests.cs | 62 +++ .../Validators/ObjectTypeValidatorTests.cs | 75 ++++ .../Validators/ParameterValidatorTests.cs | 109 +++++ .../Validators/RoleValidatorTests.cs | 75 ++++ .../Validators/StatusValidatorTests.cs | 101 +++++ .../Validators/TagValidatorTests.cs | 62 +++ .../Validators/UserValidatorTests.cs | 374 ++++++++++++++++++ .../ReasnAPI/ReasnAPI/Models/DTOs/EventDto.cs | 2 +- Server/ReasnAPI/ReasnAPI/ReasnAPI.csproj | 1 - .../ReasnAPI/Validators/AddressValidator.cs | 16 +- .../ReasnAPI/Validators/CommentValidator.cs | 24 +- .../ReasnAPI/Validators/EventValidator.cs | 41 +- .../ReasnAPI/Validators/ImageValidator.cs | 21 - .../ReasnAPI/Validators/InterestValidator.cs | 8 +- .../Validators/ObjectTypeValidator.cs | 2 +- .../ReasnAPI/Validators/ParameterValidator.cs | 10 +- .../ReasnAPI/Validators/RoleValidator.cs | 13 +- .../ReasnAPI/Validators/StatusValidator.cs | 18 +- .../ReasnAPI/Validators/TagValidator.cs | 8 +- .../ReasnAPI/Validators/UserValidator.cs | 43 +- 25 files changed, 1572 insertions(+), 167 deletions(-) delete mode 100644 Server/ReasnAPI/ReasnAPI.Tests/UnitTest1.cs create mode 100644 Server/ReasnAPI/ReasnAPI.Tests/Validators/AddressValidatorTests.cs create mode 100644 Server/ReasnAPI/ReasnAPI.Tests/Validators/CommentValidatorTests.cs create mode 100644 Server/ReasnAPI/ReasnAPI.Tests/Validators/EventValidatorTests.cs create mode 100644 Server/ReasnAPI/ReasnAPI.Tests/Validators/InterestValidatorTests.cs create mode 100644 Server/ReasnAPI/ReasnAPI.Tests/Validators/ObjectTypeValidatorTests.cs create mode 100644 Server/ReasnAPI/ReasnAPI.Tests/Validators/ParameterValidatorTests.cs create mode 100644 Server/ReasnAPI/ReasnAPI.Tests/Validators/RoleValidatorTests.cs create mode 100644 Server/ReasnAPI/ReasnAPI.Tests/Validators/StatusValidatorTests.cs create mode 100644 Server/ReasnAPI/ReasnAPI.Tests/Validators/TagValidatorTests.cs create mode 100644 Server/ReasnAPI/ReasnAPI.Tests/Validators/UserValidatorTests.cs delete mode 100644 Server/ReasnAPI/ReasnAPI/Validators/ImageValidator.cs diff --git a/Server/ReasnAPI/ReasnAPI.Tests/ReasnAPI.Tests.csproj b/Server/ReasnAPI/ReasnAPI.Tests/ReasnAPI.Tests.csproj index 1b46a3ed..44463a72 100644 --- a/Server/ReasnAPI/ReasnAPI.Tests/ReasnAPI.Tests.csproj +++ b/Server/ReasnAPI/ReasnAPI.Tests/ReasnAPI.Tests.csproj @@ -17,6 +17,10 @@ + + + + diff --git a/Server/ReasnAPI/ReasnAPI.Tests/UnitTest1.cs b/Server/ReasnAPI/ReasnAPI.Tests/UnitTest1.cs deleted file mode 100644 index 46dac6f2..00000000 --- a/Server/ReasnAPI/ReasnAPI.Tests/UnitTest1.cs +++ /dev/null @@ -1,12 +0,0 @@ -namespace ReasnAPI.Tests -{ - [TestClass] - public class UnitTest1 - { - [TestMethod] - public void TestAdd() - { - Assert.AreEqual(17, 17); - } - } -} diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Validators/AddressValidatorTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Validators/AddressValidatorTests.cs new file mode 100644 index 00000000..0ff447ab --- /dev/null +++ b/Server/ReasnAPI/ReasnAPI.Tests/Validators/AddressValidatorTests.cs @@ -0,0 +1,336 @@ +using ReasnAPI.Models.DTOs; +using ReasnAPI.Validators; + +namespace ReasnAPI.Tests.Validators +{ + [TestClass] + public class AddressValidatorTests + { + [TestMethod] + public void Validate_WhenCityIsEmpty_ReturnsValidationResult() + { + var address = new AddressDto + { + City = "", + Country = "Country", + Street = "Street", + State = "State", + ZipCode = "00-000" + }; + + var result = AddressValidator.Validate(address); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "City is required")); + Assert.IsTrue(result.Any(r => r.ErrorMessage == "City is invalid")); + } + + [TestMethod] + public void Validate_WhenCityIsTooLong_ReturnsValidationResult() + { + var address = new AddressDto + { + City = new string('a', 65), + Country = "Country", + Street = "Street", + State = "State", + ZipCode = "00-000" + }; + + var result = AddressValidator.Validate(address); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "City is too long")); + } + + [TestMethod] + public void Validate_WhenCityIsInvalid_ReturnsValidationResult() + { + var address = new AddressDto + { + City = "Invalid City", + Country = "Country", + Street = "Street", + State = "State", + ZipCode = "00-000" + }; + + var result = AddressValidator.Validate(address); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "City is invalid")); + } + + [TestMethod] + public void Validate_WhenCityIsValid_ReturnsNoValidationResult() + { + var address = new AddressDto + { + City = "Valid City", + Country = "Country", + Street = "Street", + State = "State", + ZipCode = "00-000" + }; + + var result = AddressValidator.Validate(address); + + Assert.IsFalse(result.Any()); + } + + [TestMethod] + public void Validate_WhenCountryIsEmpty_ReturnsValidationResult() + { + var address = new AddressDto + { + City = "City", + Country = "", + Street = "Street", + State = "State", + ZipCode = "00-000" + }; + + var result = AddressValidator.Validate(address); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Country is required")); + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Country is invalid")); + } + + [TestMethod] + public void Validate_WhenCountryIsTooLong_ReturnsValidationResult() + { + var address = new AddressDto + { + City = "City", + Country = new string('a', 65), + Street = "Street", + State = "State", + ZipCode = "00-000" + }; + + var result = AddressValidator.Validate(address); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Country is too long")); + } + + [TestMethod] + public void Validate_WhenCountryIsInvalid_ReturnsValidationResult() + { + var address = new AddressDto + { + City = "City", + Country = "Invalid Country", + Street = "Street", + State = "State", + ZipCode = "00-000" + }; + + var result = AddressValidator.Validate(address); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Country is invalid")); + } + + [TestMethod] + public void Validate_WhenCountryIsValid_ReturnsNoValidationResult() + { + var address = new AddressDto + { + City = "City", + Country = "Valid Country", + Street = "Street", + State = "State", + ZipCode = "00-000" + }; + + var result = AddressValidator.Validate(address); + + Assert.IsFalse(result.Any()); + } + + [TestMethod] + public void Validate_WhenStreetIsEmpty_ReturnsValidationResult() + { + var address = new AddressDto + { + City = "City", + Country = "Country", + Street = "", + State = "State", + ZipCode = "00-000" + }; + + var result = AddressValidator.Validate(address); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Street is required")); + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Street is invalid")); + } + + [TestMethod] + public void Validate_WhenStreetIsTooLong_ReturnsValidationResult() + { + var address = new AddressDto + { + City = "City", + Country = "Country", + Street = new string('a', 65), + State = "State", + ZipCode = "00-000" + }; + + var result = AddressValidator.Validate(address); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Street is too long")); + } + + [TestMethod] + public void Validate_WhenStreetIsInvalid_ReturnsValidationResult() + { + var address = new AddressDto + { + City = "City", + Country = "Country", + Street = "Invalid Street", + State = "State", + ZipCode = "00-000" + }; + + var result = AddressValidator.Validate(address); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Street is invalid")); + } + + [TestMethod] + public void Validate_WhenStreetIsValid_ReturnsNoValidationResult() + { + var address = new AddressDto + { + City = "City", + Country = "Country", + Street = "Valid Street", + State = "State", + ZipCode = "00-000" + }; + + var result = AddressValidator.Validate(address); + + Assert.IsFalse(result.Any()); + } + + [TestMethod] + public void Validate_WhenStateIsEmpty_ReturnsValidationResult() + { + var address = new AddressDto + { + City = "City", + Country = "Country", + Street = "Street", + State = "", + ZipCode = "00-000" + }; + + var result = AddressValidator.Validate(address); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "State is required")); + Assert.IsTrue(result.Any(r => r.ErrorMessage == "State is invalid")); + } + + [TestMethod] + public void Validate_WhenStateIsTooLong_ReturnsValidationResult() + { + var address = new AddressDto + { + City = "City", + Country = "Country", + Street = "Street", + State = new string('a', 65), + ZipCode = "00-000" + }; + + var result = AddressValidator.Validate(address); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "State is too long")); + } + + [TestMethod] + public void Validate_WhenStateIsInvalid_ReturnsValidationResult() + { + var address = new AddressDto + { + City = "City", + Country = "Country", + Street = "Street", + State = "Invalid State", + ZipCode = "00-000" + }; + + var result = AddressValidator.Validate(address); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "State is invalid")); + } + + [TestMethod] + public void Validate_WhenStateIsValid_ReturnsNoValidationResult() + { + var address = new AddressDto + { + City = "City", + Country = "Country", + Street = "Street", + State = "Valid State", + ZipCode = "00-000" + }; + + var result = AddressValidator.Validate(address); + + Assert.IsFalse(result.Any()); + } + + [TestMethod] + public void Valdate_WhenZipCodeIsTooLong_ReturnsValidationResult() + { + var address = new AddressDto + { + City = "City", + Country = "Country", + Street = "Street", + State = "State", + ZipCode = new string('0', 9) + }; + + var result = AddressValidator.Validate(address); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "ZipCode is too long")); + } + + [TestMethod] + public void Validate_WhenZipCodeIsInvalid_ReturnsValidationResult() + { + var address = new AddressDto + { + City = "City", + Country = "Country", + Street = "Street", + State = "State", + ZipCode = "Invalid_" + }; + + var result = AddressValidator.Validate(address); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "ZipCode is invalid")); + } + + [TestMethod] + public void Validate_WhenZipCodeIsValid_ReturnsNoValidationResult() + { + var address = new AddressDto + { + City = "City", + Country = "Country", + Street = "Street", + State = "State", + ZipCode = "00-000" + }; + + var result = AddressValidator.Validate(address); + + Assert.IsFalse(result.Any()); + } + } +} diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Validators/CommentValidatorTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Validators/CommentValidatorTests.cs new file mode 100644 index 00000000..49f64da6 --- /dev/null +++ b/Server/ReasnAPI/ReasnAPI.Tests/Validators/CommentValidatorTests.cs @@ -0,0 +1,65 @@ +using ReasnAPI.Models.DTOs; +using ReasnAPI.Validators; + +namespace ReasnAPI.Tests.Validators +{ + [TestClass] + public class CommentValidatorTests + { + [TestMethod] + public void Validate_WhenContentIsEmpty_ReturnsValidationResult() + { + var comment = new CommentDto + { + Content = "", + CreatedAt = DateTime.Now + }; + + var result = CommentValidator.Validate(comment); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Content is required")); + } + + [TestMethod] + public void Validate_WhenContentIsTooLong_ReturnsValidationResult() + { + var comment = new CommentDto + { + Content = new string('a', 1025), + CreatedAt = DateTime.Now + }; + + var result = CommentValidator.Validate(comment); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Content is too long")); + } + + [TestMethod] + public void Validate_WhenCreatedAtIsInTheFuture_ReturnsValidationResult() + { + var comment = new CommentDto + { + Content = "Content", + CreatedAt = DateTime.Now.AddMinutes(1) + }; + + var result = CommentValidator.Validate(comment); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "CreatedAt is in the future")); + } + + [TestMethod] + public void Validate_WhenCommentIsValid_ReturnsNoValidationResult() + { + var comment = new CommentDto + { + Content = "Content", + CreatedAt = DateTime.Now + }; + + var result = CommentValidator.Validate(comment); + + Assert.IsFalse(result.Any()); + } + } +} diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Validators/EventValidatorTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Validators/EventValidatorTests.cs new file mode 100644 index 00000000..ce6b68ff --- /dev/null +++ b/Server/ReasnAPI/ReasnAPI.Tests/Validators/EventValidatorTests.cs @@ -0,0 +1,257 @@ +using ReasnAPI.Models.DTOs; +using ReasnAPI.Validators; + +namespace ReasnAPI.Tests.Validators +{ + [TestClass] + public class EventValidatorTests + { + [TestMethod] + public void Validate_WhenNameIsEmpty_ReturnsValidationResult() + { + var event1 = new EventDto + { + Name = "", + Description = "Description", + StartAt = DateTime.Now, + EndAt = DateTime.Now, + CreatedAt = DateTime.Now, + UpdatedAt = DateTime.Now, + Slug = "slug" + }; + + var result = EventValidator.Validate(event1); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Name is required")); + } + + [TestMethod] + public void Validate_WhenNameIsTooLong_ReturnsValidationResult() + { + var event1 = new EventDto + { + Name = new string('a', 65), + Description = "Description", + StartAt = DateTime.Now, + EndAt = DateTime.Now, + CreatedAt = DateTime.Now, + UpdatedAt = DateTime.Now, + Slug = "slug" + }; + + var result = EventValidator.Validate(event1); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Name is too long")); + } + + [TestMethod] + public void Validate_WhenNameIsValid_ReturnsNoValidationResult() + { + var event1 = new EventDto + { + Name = "ValidName", + Description = "Description", + StartAt = DateTime.Now, + EndAt = DateTime.Now, + CreatedAt = DateTime.Now, + UpdatedAt = DateTime.Now, + Slug = "slug" + }; + + var result = EventValidator.Validate(event1); + + Assert.IsFalse(result.Any()); + } + + [TestMethod] + public void Validate_WhenDescriptionIsEmpty_ReturnsValidationResult() + { + var event1 = new EventDto + { + Name = "Name", + Description = "", + StartAt = DateTime.Now, + EndAt = DateTime.Now, + CreatedAt = DateTime.Now, + UpdatedAt = DateTime.Now, + Slug = "slug" + }; + + var result = EventValidator.Validate(event1); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Description is required")); + } + + [TestMethod] + public void Validate_WhenDescriptionIsTooLong_ReturnsValidationResult() + { + var event1 = new EventDto + { + Name = "Name", + Description = new string('a', 4049), + StartAt = DateTime.Now, + EndAt = DateTime.Now, + CreatedAt = DateTime.Now, + UpdatedAt = DateTime.Now, + Slug = "slug" + }; + + var result = EventValidator.Validate(event1); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Description is too long")); + } + + [TestMethod] + public void Validate_WhenDescriptionIsValid_ReturnsNoValidationResult() + { + var event1 = new EventDto + { + Name = "Name", + Description = "Valid Description", + StartAt = DateTime.Now, + EndAt = DateTime.Now, + CreatedAt = DateTime.Now, + UpdatedAt = DateTime.Now, + Slug = "slug" + }; + + var result = EventValidator.Validate(event1); + + Assert.IsFalse(result.Any()); + } + + [TestMethod] + public void Validate_WhenStartAtIsAfterEndAt_ReturnsValidationResult() + { + var event1 = new EventDto + { + Name = "Name", + Description = "Description", + StartAt = DateTime.Now.AddMinutes(1), + EndAt = DateTime.Now, + CreatedAt = DateTime.Now, + UpdatedAt = DateTime.Now, + Slug = "slug" + }; + + var result = EventValidator.Validate(event1); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "StartTime is after EndTime")); + } + + [TestMethod] + public void Validate_WhenCreatedAtIsInTheFuture_ReturnsValidationResult() + { + var event1 = new EventDto + { + Name = "Name", + Description = "Description", + StartAt = DateTime.Now, + EndAt = DateTime.Now, + CreatedAt = DateTime.Now.AddMinutes(1), + UpdatedAt = DateTime.Now, + Slug = "slug" + }; + + var result = EventValidator.Validate(event1); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "CreatedAt is in the future")); + } + + [TestMethod] + public void Validate_WhenUpdatedAtIsInTheFuture_ReturnsValidationResult() + { + var event1 = new EventDto + { + Name = "Name", + Description = "Description", + StartAt = DateTime.Now, + EndAt = DateTime.Now, + CreatedAt = DateTime.Now, + UpdatedAt = DateTime.Now.AddMinutes(1), + Slug = "slug" + }; + + var result = EventValidator.Validate(event1); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "UpdatedAt is in the future")); + } + + [TestMethod] + public void Validate_WhenSlugIsEmpty_ReturnsValidationResult() + { + var event1 = new EventDto + { + Name = "Name", + Description = "Description", + StartAt = DateTime.Now, + EndAt = DateTime.Now, + CreatedAt = DateTime.Now, + UpdatedAt = DateTime.Now, + Slug = "" + }; + + var result = EventValidator.Validate(event1); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Slug is required")); + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Slug is invalid")); + } + + [TestMethod] + public void Validate_WhenSlugIsTooLong_ReturnsValidationResult() + { + var event1 = new EventDto + { + Name = "Name", + Description = "Description", + StartAt = DateTime.Now, + EndAt = DateTime.Now, + CreatedAt = DateTime.Now, + UpdatedAt = DateTime.Now, + Slug = new string('a', 129) + }; + + var result = EventValidator.Validate(event1); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Slug is too long")); + } + + [TestMethod] + public void Validate_WhenSlugIsIsInvalid_ReturnsValidationResult() + { + var event1 = new EventDto + { + Name = "Name", + Description = "Description", + StartAt = DateTime.Now, + EndAt = DateTime.Now, + CreatedAt = DateTime.Now, + UpdatedAt = DateTime.Now, + Slug = "invalid slug" + }; + + var result = EventValidator.Validate(event1); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Slug is invalid")); + } + + [TestMethod] + public void Validate_WhenSlugIsValid_ReturnsNoValidationResult() + { + var event1 = new EventDto + { + Name = "Name", + Description = "Description", + StartAt = DateTime.Now, + EndAt = DateTime.Now, + CreatedAt = DateTime.Now, + UpdatedAt = DateTime.Now, + Slug = "valid-slug" + }; + + var result = EventValidator.Validate(event1); + + Assert.IsFalse(result.Any()); + } + } +} diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Validators/InterestValidatorTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Validators/InterestValidatorTests.cs new file mode 100644 index 00000000..4d64732a --- /dev/null +++ b/Server/ReasnAPI/ReasnAPI.Tests/Validators/InterestValidatorTests.cs @@ -0,0 +1,62 @@ +using ReasnAPI.Models.DTOs; +using ReasnAPI.Validators; + +namespace ReasnAPI.Tests.Validators +{ + [TestClass] + public class InterestValidatorTests + { + [TestMethod] + public void Validate_WhenNameIsEmpty_ReturnsValidationResult() + { + var interest = new IntrestDto + { + Name = "" + }; + + var result = InterestValidator.Validate(interest); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Name is required")); + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Name is invalid")); + } + + [TestMethod] + public void Validate_WhenNameIsTooLong_ReturnsValidationResult() + { + var interest = new IntrestDto + { + Name = new string('a', 33) + }; + + var result = InterestValidator.Validate(interest); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Name is too long")); + } + + [TestMethod] + public void Validate_WhenNameIsInvalid_ReturnsValidationResult() + { + var interest = new IntrestDto + { + Name = "Invalid Name" + }; + + var result = InterestValidator.Validate(interest); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Name is invalid")); + } + + [TestMethod] + public void Validate_WhenNameIsValid_ReturnsNoValidationResult() + { + var interest = new IntrestDto + { + Name = "Valid Name" + }; + + var result = InterestValidator.Validate(interest); + + Assert.IsFalse(result.Any()); + } + } +} diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Validators/ObjectTypeValidatorTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Validators/ObjectTypeValidatorTests.cs new file mode 100644 index 00000000..5c1480ee --- /dev/null +++ b/Server/ReasnAPI/ReasnAPI.Tests/Validators/ObjectTypeValidatorTests.cs @@ -0,0 +1,75 @@ +using ReasnAPI.Models.Database; +using ReasnAPI.Validators; + +namespace ReasnAPI.Tests.Validators +{ + [TestClass] + public class ObjectTypeValidatorTests + { + [TestMethod] + public void Validate_WhenNameIsEmpty_ReturnsValidationResult() + { + var objectType = new ObjectType + { + Name = "" + }; + + var result = ObjectTypeValidator.Validate(objectType); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Name is required")); + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Name is invalid")); + } + + [TestMethod] + public void Validate_WhenNameIsTooLong_ReturnsValidationResult() + { + var objectType = new ObjectType + { + Name = new string('a', 33) + }; + + var result = ObjectTypeValidator.Validate(objectType); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Name is too long")); + } + + [TestMethod] + public void Validate_WhenNameIsInvalid_ReturnsValidationResult() + { + var objectType = new ObjectType + { + Name = "Invalid Name" + }; + + var result = ObjectTypeValidator.Validate(objectType); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Name is invalid")); + } + + [TestMethod] + public void Validate_WhenNameIsEvent_ReturnsNoValidationResult() + { + var objectType = new ObjectType + { + Name = "Event" + }; + + var result = ObjectTypeValidator.Validate(objectType); + + Assert.IsFalse(result.Any()); + } + + [TestMethod] + public void Validate_WhenNameIsUser_ReturnsNoValidationResult() + { + var objectType = new ObjectType + { + Name = "User" + }; + + var result = ObjectTypeValidator.Validate(objectType); + + Assert.IsFalse(result.Any()); + } + } +} diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Validators/ParameterValidatorTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Validators/ParameterValidatorTests.cs new file mode 100644 index 00000000..424f987b --- /dev/null +++ b/Server/ReasnAPI/ReasnAPI.Tests/Validators/ParameterValidatorTests.cs @@ -0,0 +1,109 @@ +using ReasnAPI.Models.DTOs; +using ReasnAPI.Validators; + +namespace ReasnAPI.Tests.Validators +{ + [TestClass] + public class ParameterValidatorTests + { + [TestMethod] + public void Validate_WhenKeyIsEmpty_ReturnsKeyIsRequired() + { + var parameter = new ParameterDto + { + Key = "", + Value = "value" + }; + + var result = ParameterValidator.Validate(parameter); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Key is required")); + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Key is invalid")); + } + + [TestMethod] + public void Validate_WhenKeyIsTooLong_ReturnsKeyIsTooLong() + { + var parameter = new ParameterDto + { + Key = new string('a', 33), + Value = "value" + }; + + var result = ParameterValidator.Validate(parameter); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Key is too long")); + } + + [TestMethod] + public void Validate_WhenKeyIsInvalid_ReturnsKeyIsInvalid() + { + var parameter = new ParameterDto + { + Key = "Invalid Key", + Value = "value" + }; + + var result = ParameterValidator.Validate(parameter); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Key is invalid")); + } + + [TestMethod] + public void Validate_WhenValueIsEmpty_ReturnsValueIsRequired() + { + var parameter = new ParameterDto + { + Key = "key", + Value = "" + }; + + var result = ParameterValidator.Validate(parameter); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Value is required")); + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Value is invalid")); + } + + [TestMethod] + public void Validate_WhenValueIsTooLong_ReturnsValueIsTooLong() + { + var parameter = new ParameterDto + { + Key = "key", + Value = new string('a', 65) + }; + + var result = ParameterValidator.Validate(parameter); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Value is too long")); + } + + [TestMethod] + public void Validate_WhenValueIsInvalid_ReturnsValueIsInvalid() + { + var parameter = new ParameterDto + { + Key = "key", + Value = "Invalid Value" + }; + + var result = ParameterValidator.Validate(parameter); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Value is invalid")); + } + + [TestMethod] + public void Validate_WhenValueAndKeyAreValid_ReturnsNoValidationResult() + { + var parameter = new ParameterDto + { + Key = "key", + Value = "value" + }; + + var result = ParameterValidator.Validate(parameter); + + Assert.IsFalse(result.Any()); + } + } +} diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Validators/RoleValidatorTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Validators/RoleValidatorTests.cs new file mode 100644 index 00000000..a662254a --- /dev/null +++ b/Server/ReasnAPI/ReasnAPI.Tests/Validators/RoleValidatorTests.cs @@ -0,0 +1,75 @@ +using ReasnAPI.Models.DTOs; +using ReasnAPI.Validators; + +namespace ReasnAPI.Tests.Validators +{ + [TestClass] + public class RoleValidatorTests + { + [TestMethod] + public void Validate_WhenRoleNameIsEmpty_ReturnsValidationResult() + { + var role = new RoleDto + { + Name = "" + }; + + var result = RoleValidator.Validate(role); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Role name is required")); + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Role name is invalid")); + } + + [TestMethod] + public void Validate_WhenRoleNameIsInvalid_ReturnsValidationResult() + { + var role = new RoleDto + { + Name = "Invalid Role" + }; + + var result = RoleValidator.Validate(role); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Role name is invalid")); + } + + [TestMethod] + public void Validate_WhenRoleNameIsOrganizer_ReturnsNoValidationResult() + { + var role = new RoleDto + { + Name = "Organizer" + }; + + var result = RoleValidator.Validate(role); + + Assert.IsFalse(result.Any()); + } + + [TestMethod] + public void Validate_WhenRoleNameIsAdmin_ReturnsNoValidationResult() + { + var role = new RoleDto + { + Name = "Admin" + }; + + var result = RoleValidator.Validate(role); + + Assert.IsFalse(result.Any()); + } + + [TestMethod] + public void Validate_WhenRoleNameIsUser_ReturnsNoValidationResult() + { + var role = new RoleDto + { + Name = "User" + }; + + var result = RoleValidator.Validate(role); + + Assert.IsFalse(result.Any()); + } + } +} diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Validators/StatusValidatorTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Validators/StatusValidatorTests.cs new file mode 100644 index 00000000..c3438cdf --- /dev/null +++ b/Server/ReasnAPI/ReasnAPI.Tests/Validators/StatusValidatorTests.cs @@ -0,0 +1,101 @@ +using ReasnAPI.Models.DTOs; +using ReasnAPI.Validators; + +namespace ReasnAPI.Tests.Validators +{ + [TestClass] + public class StatusValidatorTests + { + [TestMethod] + public void Validate_WhenNameIsEmpty_ReturnsValidationResult() + { + var status = new StatusDto + { + Name = "" + }; + + var result = StatusValidator.Validate(status); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Name is required")); + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Name is invalid")); + } + + [TestMethod] + public void Validate_WhenNameIsInvalid_ReturnsValidationResult() + { + var status = new StatusDto + { + Name = "Invalid Name" + }; + + var result = StatusValidator.Validate(status); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Name is invalid")); + } + + [TestMethod] + public void Validate_WhenNameIsInterested_ReturnsValidationResult() + { + var status = new StatusDto + { + Name = "Interested" + }; + + var result = StatusValidator.Validate(status); + + Assert.IsFalse(result.Any(r => r.ErrorMessage == "Name is invalid")); + } + + [TestMethod] + public void Validate_WhenNameIsParticipating_ReturnsValidationResult() + { + var status = new StatusDto + { + Name = "Participating" + }; + + var result = StatusValidator.Validate(status); + + Assert.IsFalse(result.Any(r => r.ErrorMessage == "Name is invalid")); + } + + [TestMethod] + public void Validate_WhenNameIsCompleted_ReturnsValidationResult() + { + var status = new StatusDto + { + Name = "Completed" + }; + + var result = StatusValidator.Validate(status); + + Assert.IsFalse(result.Any(r => r.ErrorMessage == "Name is invalid")); + } + + [TestMethod] + public void Validate_WhenNameIsInProgress_ReturnsValidationResult() + { + var status = new StatusDto + { + Name = "In progress" + }; + + var result = StatusValidator.Validate(status); + + Assert.IsFalse(result.Any(r => r.ErrorMessage == "Name is invalid")); + } + + [TestMethod] + public void Validate_WhenNameIsWaitingForApproval_ReturnsValidationResult() + { + var status = new StatusDto + { + Name = "Waiting for approval" + }; + + var result = StatusValidator.Validate(status); + + Assert.IsFalse(result.Any(r => r.ErrorMessage == "Name is invalid")); + } + } +} diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Validators/TagValidatorTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Validators/TagValidatorTests.cs new file mode 100644 index 00000000..4a935bdd --- /dev/null +++ b/Server/ReasnAPI/ReasnAPI.Tests/Validators/TagValidatorTests.cs @@ -0,0 +1,62 @@ +using ReasnAPI.Models.DTOs; +using ReasnAPI.Validators; + +namespace ReasnAPI.Tests.Validators +{ + [TestClass] + public class TagValidatorTests + { + [TestMethod] + public void Validate_WhenNameIsEmpty_ReturnsValidationResult() + { + var tag = new TagDto + { + Name = "" + }; + + var result = TagValidator.Validate(tag); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Name is required")); + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Name is invalid")); + } + + [TestMethod] + public void Validate_WhenNameIsTooLong_ReturnsValidationResult() + { + var tag = new TagDto + { + Name = new string('a', 65) + }; + + var result = TagValidator.Validate(tag); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Name is too long")); + } + + [TestMethod] + public void Validate_WhenNameIsInvalid_ReturnsValidationResult() + { + var tag = new TagDto + { + Name = "Invalid Name" + }; + + var result = TagValidator.Validate(tag); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Name is invalid")); + } + + [TestMethod] + public void Validate_WhenNameIsValid_ReturnsValidationResult() + { + var tag = new TagDto + { + Name = "Valid Name" + }; + + var result = TagValidator.Validate(tag); + + Assert.IsFalse(result.Any(r => r.ErrorMessage == "Name is invalid")); + } + } +} diff --git a/Server/ReasnAPI/ReasnAPI.Tests/Validators/UserValidatorTests.cs b/Server/ReasnAPI/ReasnAPI.Tests/Validators/UserValidatorTests.cs new file mode 100644 index 00000000..a5de2ecf --- /dev/null +++ b/Server/ReasnAPI/ReasnAPI.Tests/Validators/UserValidatorTests.cs @@ -0,0 +1,374 @@ +using ReasnAPI.Models.DTOs; +using ReasnAPI.Validators; + +namespace ReasnAPI.Tests.Validators +{ + [TestClass] + public class UserValidatorTests + { + [TestMethod] + public void Validate_WhenNameIsEmpty_ReturnsValidationResult() + { + var user = new UserDto + { + Name = "", + Surname = "Surname", + Username = "Username", + RoleId = 1, + Email = "email@email.com", + AddressId = 1, + Phone = "+48 123456789" + }; + + var result = UserValidator.Validate(user); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Name is required")); + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Name is invalid")); + } + + [TestMethod] + public void Validate_WhenNameIsTooLong_ReturnsValidationResult() + { + var user = new UserDto + { + Name = new string('a', 65), + Surname = "Surname", + Username = "Username", + RoleId = 1, + Email = "email@email.com", + AddressId = 1, + Phone = "+48 123456789" + }; + + var result = UserValidator.Validate(user); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Name is too long")); + } + + [TestMethod] + public void Validate_WhenNameIsInvalid_ReturnsValidationResult() + { + var user = new UserDto + { + Name = "Invalid Name", + Surname = "Surname", + Username = "Username", + RoleId = 1, + Email = "email@email.com", + AddressId = 1, + Phone = "+48 123456789" + }; + + var result = UserValidator.Validate(user); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Name is invalid")); + } + + [TestMethod] + public void Validate_WhenNameIsValid_ReturnsNoValidationResult() + { + var user = new UserDto + { + Name = "Validname", + Surname = "Surname", + Username = "Username", + RoleId = 1, + Email = "email@email.com", + AddressId = 1, + Phone = "+48 123456789" + }; + + var result = UserValidator.Validate(user); + + Assert.IsFalse(result.Any()); + } + + [TestMethod] + public void Validate_WhenSurnameIsEmpty_ReturnsValidationResult() + { + var user = new UserDto + { + Name = "Name", + Surname = "", + Username = "Username", + RoleId = 1, + Email = "email@email.com", + AddressId = 1, + Phone = "+48 123456789" + }; + + var result = UserValidator.Validate(user); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Surname is required")); + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Surname is invalid")); + } + + [TestMethod] + public void Validate_WhenSurnameIsTooLong_ReturnsValidationResult() + { + var user = new UserDto + { + Name = "Name", + Surname = new string('a', 65), + Username = "Username", + RoleId = 1, + Email = "email@email.com", + AddressId = 1, + Phone = "+48 123456789" + }; + + var result = UserValidator.Validate(user); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Surname is too long")); + } + + [TestMethod] + public void Validate_WhenSurnameIsInvalid_ReturnsValidationResult() + { + var user = new UserDto + { + Name = "Name", + Surname = "Invalid Surname", + Username = "Username", + RoleId = 1, + Email = "email@email.com", + AddressId = 1, + Phone = "+48 123456789" + }; + + var result = UserValidator.Validate(user); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Surname is invalid")); + } + + [TestMethod] + public void Validate_WhenSurnameIsValid_ReturnsNoValidationResult() + { + var user = new UserDto + { + Name = "Name", + Surname = "Validsurname", + Username = "Username", + RoleId = 1, + Email = "email@email.com", + AddressId = 1, + Phone = "+48 123456789" + }; + + var result = UserValidator.Validate(user); + + Assert.IsFalse(result.Any()); + } + + [TestMethod] + public void Validate_WhenUsernameIsEmpty_ReturnsValidationResult() + { + var user = new UserDto + { + Name = "Name", + Surname = "Surname", + Username = "", + RoleId = 1, + Email = "email@email.com", + AddressId = 1, + Phone = "+48 123456789" + }; + + var result = UserValidator.Validate(user); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Username is required")); + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Username is invalid")); + } + + [TestMethod] + public void Validate_WhenUsernameIsTooLong_ReturnsValidationResult() + { + var user = new UserDto + { + Name = "Name", + Surname = "Surname", + Username = new string('a', 65), + RoleId = 1, + Email = "email@email.com", + AddressId = 1, + Phone = "+48 123456789" + }; + + var result = UserValidator.Validate(user); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Username is too long")); + } + + [TestMethod] + public void Validate_WhenUsernameIsInvalid_ReturnsValidationResult() + { + var user = new UserDto + { + Name = "Name", + Surname = "Surname", + Username = "Invalid Username", + RoleId = 1, + Email = "email@email.com", + AddressId = 1, + Phone = "+48 123456789" + }; + + var result = UserValidator.Validate(user); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Username is invalid")); + } + + [TestMethod] + public void Validate_WhenUsernameIsValid_ReturnsValidationResult() + { + var user = new UserDto + { + Name = "Name", + Surname = "Surname", + Username = "ValidUsername", + RoleId = 1, + Email = "email@email.com", + AddressId = 1, + Phone = "+48 123456789" + }; + + var result = UserValidator.Validate(user); + + Assert.IsFalse(result.Any()); + } + + [TestMethod] + public void Validate_WhenEmailIsEmpty_ReturnsValidationResult() + { + var user = new UserDto + { + Name = "Name", + Surname = "Surname", + Username = "Username", + RoleId = 1, + Email = "", + AddressId = 1, + Phone = "+48 123456789" + }; + + var result = UserValidator.Validate(user); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Email is required")); + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Email is invalid")); + } + + [TestMethod] + public void Validate_WhenEmailIsTooLong_ReturnsValidationResult() + { + var user = new UserDto + { + Name = "Name", + Surname = "Surname", + Username = "Username", + RoleId = 1, + Email = new string('a', 256), + AddressId = 1, + Phone = "+48 123456789" + }; + + var result = UserValidator.Validate(user); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Email is too long")); + } + + [TestMethod] + public void Validate_WhenEmailIsInvalid_ReturnsValidationResult() + { + var user = new UserDto + { + Name = "Name", + Surname = "Surname", + Username = "Username", + RoleId = 1, + Email = "Invalid Email", + AddressId = 1, + Phone = "+48 123456789" + }; + + var result = UserValidator.Validate(user); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Email is invalid")); + } + + [TestMethod] + public void Validate_WhenEmailIsValid_ReturnsNoValidationResult() + { + var user = new UserDto + { + Name = "Name", + Surname = "Surname", + Username = "Username", + RoleId = 1, + Email = "valid@email.com", + AddressId = 1, + Phone = "+48 123456789" + }; + + var result = UserValidator.Validate(user); + + Assert.IsFalse(result.Any()); + } + + [TestMethod] + public void Validate_WhenPhoneIsTooLong_ReturnsValidationResult() + { + var user = new UserDto + { + Name = "Name", + Surname = "Surname", + Username = "Username", + RoleId = 1, + Email = "email@email.com", + AddressId = 1, + Phone = new string('a', 20) + }; + + var result = UserValidator.Validate(user); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Phone is too long")); + } + + [TestMethod] + public void Validate_WhenPhoneIsInvalid_ReturnsValidationResult() + { + var user = new UserDto + { + Name = "Name", + Surname = "Surname", + Username = "Username", + RoleId = 1, + Email = "email@email.com", + AddressId = 1, + Phone = "Invalid Phone" + }; + + var result = UserValidator.Validate(user); + + Assert.IsTrue(result.Any(r => r.ErrorMessage == "Phone is invalid")); + } + + [TestMethod] + public void Validate_WhenPhoneIsValid_ReturnsNoValidationResult() + { + var user = new UserDto + { + Name = "Name", + Surname = "Surname", + Username = "Username", + RoleId = 1, + Email = "email@email.com", + AddressId = 1, + Phone = "+48 123456789" + }; + + var result = UserValidator.Validate(user); + + Assert.IsFalse(result.Any()); + } + } +} diff --git a/Server/ReasnAPI/ReasnAPI/Models/DTOs/EventDto.cs b/Server/ReasnAPI/ReasnAPI/Models/DTOs/EventDto.cs index d35c8120..00c00ff5 100644 --- a/Server/ReasnAPI/ReasnAPI/Models/DTOs/EventDto.cs +++ b/Server/ReasnAPI/ReasnAPI/Models/DTOs/EventDto.cs @@ -10,7 +10,7 @@ public class EventDto { public DateTime EndAt { get; set; } public DateTime CreatedAt { get; set; } public DateTime UpdatedAt { get; set; } - public string? Slug { get; set; } + public string Slug { get; set; } = null!; public int StatusId { get; set; } public List? Tags { get; set; } } diff --git a/Server/ReasnAPI/ReasnAPI/ReasnAPI.csproj b/Server/ReasnAPI/ReasnAPI/ReasnAPI.csproj index 02ce3550..a5564cb9 100644 --- a/Server/ReasnAPI/ReasnAPI/ReasnAPI.csproj +++ b/Server/ReasnAPI/ReasnAPI/ReasnAPI.csproj @@ -23,7 +23,6 @@ - diff --git a/Server/ReasnAPI/ReasnAPI/Validators/AddressValidator.cs b/Server/ReasnAPI/ReasnAPI/Validators/AddressValidator.cs index 08aa4c60..e6c9897c 100644 --- a/Server/ReasnAPI/ReasnAPI/Validators/AddressValidator.cs +++ b/Server/ReasnAPI/ReasnAPI/Validators/AddressValidator.cs @@ -1,12 +1,12 @@ -using ReasnAPI.Models.Database; +using ReasnAPI.Models.DTOs; using System.ComponentModel.DataAnnotations; using System.Text.RegularExpressions; namespace ReasnAPI.Validators { - public class AddressValidator : IValidator
+ public class AddressValidator : IValidator { - public static IEnumerable Validate(Address address) + public static IEnumerable Validate(AddressDto address) { if (string.IsNullOrWhiteSpace(address.City)) { @@ -18,7 +18,7 @@ public static IEnumerable Validate(Address address) yield return new ValidationResult("City is too long", [nameof(address.City)]); } - if (new Regex("^\\p{Lu}\\p{Ll}+(?:(\\s|-)\\p{Lu}\\p{Ll}+)*$").IsMatch(address.City) is false) + if (!new Regex("^\\p{Lu}\\p{Ll}+(?:(\\s|-)\\p{Lu}\\p{Ll}+)*$").IsMatch(address.City)) { yield return new ValidationResult("City is invalid", [nameof(address.City)]); } @@ -33,7 +33,7 @@ public static IEnumerable Validate(Address address) yield return new ValidationResult("Country is too long", [nameof(address.Country)]); } - if (new Regex("^\\p{Lu}\\p{Ll}+(?:(\\s|-)(\\p{Lu}\\p{Ll}+|i|of|and|the)){0,5}$").IsMatch(address.Country) is false) + if (!new Regex("^\\p{Lu}\\p{Ll}+(?:(\\s|-)(\\p{Lu}\\p{Ll}+|i|of|and|the)){0,5}$").IsMatch(address.Country)) { yield return new ValidationResult("Country is invalid", [nameof(address.Country)]); } @@ -48,7 +48,7 @@ public static IEnumerable Validate(Address address) yield return new ValidationResult("Street is too long", [nameof(address.Street)]); } - if (new Regex("^[\\p{L}\\d]+(?:(\\s)\\p{L}+)*(\\s(?:(\\d+\\p{L}?(/\\d*\\p{L}?)?)))?$").IsMatch(address.Street) is false) + if (!new Regex("^[\\p{L}\\d]+(?:(\\s)\\p{L}+)*(\\s(?:(\\d+\\p{L}?(/\\d*\\p{L}?)?)))?$").IsMatch(address.Street)) { yield return new ValidationResult("Street is invalid", [nameof(address.Street)]); } @@ -63,7 +63,7 @@ public static IEnumerable Validate(Address address) yield return new ValidationResult("State is too long", [nameof(address.State)]); } - if (new Regex("^\\p{Lu}\\p{Ll}+(?:(\\s|-)\\p{L}+)*$").IsMatch(address.State) is false) + if (!new Regex("^\\p{Lu}\\p{Ll}+(?:(\\s|-)\\p{L}+)*$").IsMatch(address.State)) { yield return new ValidationResult("State is invalid", [nameof(address.State)]); } @@ -75,7 +75,7 @@ public static IEnumerable Validate(Address address) yield return new ValidationResult("ZipCode is too long", [nameof(address.ZipCode)]); } - if (new Regex("^[\\p{L}\\d\\s-]{3,}$").IsMatch(address.ZipCode) is false) + if (!new Regex("^[\\p{L}\\d\\s-]{3,}$").IsMatch(address.ZipCode)) { yield return new ValidationResult("ZipCode is invalid", [nameof(address.ZipCode)]); } diff --git a/Server/ReasnAPI/ReasnAPI/Validators/CommentValidator.cs b/Server/ReasnAPI/ReasnAPI/Validators/CommentValidator.cs index e4fd2264..54a68c45 100644 --- a/Server/ReasnAPI/ReasnAPI/Validators/CommentValidator.cs +++ b/Server/ReasnAPI/ReasnAPI/Validators/CommentValidator.cs @@ -1,18 +1,12 @@ -using ReasnAPI.Models.Database; +using ReasnAPI.Models.DTOs; using System.ComponentModel.DataAnnotations; -using System.Numerics; namespace ReasnAPI.Validators { - public class CommentValidator : IValidator + public class CommentValidator : IValidator { - public static IEnumerable Validate(Comment comment) + public static IEnumerable Validate(CommentDto comment) { - if (string.IsNullOrWhiteSpace(comment.EventId.ToString())) - { - yield return new ValidationResult("EventId is required", [nameof(comment.EventId)]); - } - if (string.IsNullOrWhiteSpace(comment.Content)) { yield return new ValidationResult("Content is required", [nameof(comment.Content)]); @@ -23,20 +17,10 @@ public static IEnumerable Validate(Comment comment) yield return new ValidationResult("Content is too long", [nameof(comment.Content)]); } - if (string.IsNullOrWhiteSpace(comment.CreatedAt.ToString())) - { - yield return new ValidationResult("CreatedAt is required", [nameof(comment.CreatedAt)]); - } - - if (comment.CreatedAt > DateTime.Now) + if (comment.CreatedAt >= DateTime.Now) { yield return new ValidationResult("CreatedAt is in the future", [nameof(comment.CreatedAt)]); } - - if (string.IsNullOrWhiteSpace(comment.UserId.ToString())) - { - yield return new ValidationResult("UserId is required", [nameof(comment.UserId)]); - } } } } diff --git a/Server/ReasnAPI/ReasnAPI/Validators/EventValidator.cs b/Server/ReasnAPI/ReasnAPI/Validators/EventValidator.cs index 87de07e9..f86f601c 100644 --- a/Server/ReasnAPI/ReasnAPI/Validators/EventValidator.cs +++ b/Server/ReasnAPI/ReasnAPI/Validators/EventValidator.cs @@ -1,12 +1,12 @@ -using ReasnAPI.Models.Database; +using ReasnAPI.Models.DTOs; using System.ComponentModel.DataAnnotations; using System.Text.RegularExpressions; namespace ReasnAPI.Validators { - public class EventValidator : IValidator + public class EventValidator : IValidator { - public static IEnumerable Validate(Event eventData) + public static IEnumerable Validate(EventDto eventData) { if (string.IsNullOrWhiteSpace(eventData.Name)) { @@ -18,11 +18,6 @@ public static IEnumerable Validate(Event eventData) yield return new ValidationResult("Name is too long", [nameof(eventData.Name)]); } - if (string.IsNullOrWhiteSpace(eventData.AddressId.ToString())) - { - yield return new ValidationResult("AddressId is required", [nameof(eventData.AddressId)]); - } - if (string.IsNullOrWhiteSpace(eventData.Description)) { yield return new ValidationResult("Description is required", [nameof(eventData.Description)]); @@ -33,34 +28,19 @@ public static IEnumerable Validate(Event eventData) yield return new ValidationResult("Description is too long", [nameof(eventData.Description)]); } - if (string.IsNullOrWhiteSpace(eventData.OrganizerId.ToString())) - { - yield return new ValidationResult("OrganizerId is required", [nameof(eventData.OrganizerId)]); - } - - if (string.IsNullOrWhiteSpace(eventData.StartAt.ToString())) - { - yield return new ValidationResult("StartTime is required", [nameof(eventData.StartAt)]); - } - - if (string.IsNullOrWhiteSpace(eventData.EndAt.ToString())) - { - yield return new ValidationResult("EndTime is required", [nameof(eventData.EndAt)]); - } - if (eventData.StartAt > eventData.EndAt) { yield return new ValidationResult("StartTime is after EndTime", [nameof(eventData.StartAt)]); } - if (string.IsNullOrWhiteSpace(eventData.CreatedAt.ToString())) + if (eventData.CreatedAt >= DateTime.Now) { - yield return new ValidationResult("CreatedAt is required", [nameof(eventData.CreatedAt)]); + yield return new ValidationResult("CreatedAt is in the future", [nameof(eventData.CreatedAt)]); } - if (string.IsNullOrWhiteSpace(eventData.UpdatedAt.ToString())) + if (eventData.UpdatedAt >= DateTime.Now) { - yield return new ValidationResult("UpdatedAt is required", [nameof(eventData.UpdatedAt)]); + yield return new ValidationResult("UpdatedAt is in the future", [nameof(eventData.UpdatedAt)]); } if (string.IsNullOrWhiteSpace(eventData.Slug)) @@ -73,15 +53,10 @@ public static IEnumerable Validate(Event eventData) yield return new ValidationResult("Slug is too long", [nameof(eventData.Slug)]); } - if (new Regex("^[\\p{L}\\d]+[\\p{L}\\d-]*$").IsMatch(eventData.Slug)) + if (!new Regex("^[\\p{L}\\d]+[\\p{L}\\d-]*$").IsMatch(eventData.Slug)) { yield return new ValidationResult("Slug is invalid", [nameof(eventData.Name)]); } - - if (string.IsNullOrWhiteSpace(eventData.StatusId.ToString())) - { - yield return new ValidationResult("StatusId is required", [nameof(eventData.StatusId)]); - } } } } diff --git a/Server/ReasnAPI/ReasnAPI/Validators/ImageValidator.cs b/Server/ReasnAPI/ReasnAPI/Validators/ImageValidator.cs deleted file mode 100644 index 15de2bfc..00000000 --- a/Server/ReasnAPI/ReasnAPI/Validators/ImageValidator.cs +++ /dev/null @@ -1,21 +0,0 @@ -using ReasnAPI.Models.Database; -using System.ComponentModel.DataAnnotations; - -namespace ReasnAPI.Validators -{ - public class ImageValidator : IValidator - { - public static IEnumerable Validate(Image image) - { - if (string.IsNullOrWhiteSpace(image.ObjectTypeId.ToString())) - { - yield return new ValidationResult("ObjectTypeId is required", [nameof(image.ObjectTypeId)]); - } - - if (string.IsNullOrWhiteSpace(image.ObjectId.ToString())) - { - yield return new ValidationResult("ObjectId is required", [nameof(image.ObjectId)]); - } - } - } -} diff --git a/Server/ReasnAPI/ReasnAPI/Validators/InterestValidator.cs b/Server/ReasnAPI/ReasnAPI/Validators/InterestValidator.cs index ce4704ed..d6068f0b 100644 --- a/Server/ReasnAPI/ReasnAPI/Validators/InterestValidator.cs +++ b/Server/ReasnAPI/ReasnAPI/Validators/InterestValidator.cs @@ -1,12 +1,12 @@ -using ReasnAPI.Models.Database; +using ReasnAPI.Models.DTOs; using System.ComponentModel.DataAnnotations; using System.Text.RegularExpressions; namespace ReasnAPI.Validators { - public class InterestValidator : IValidator + public class InterestValidator : IValidator { - public static IEnumerable Validate(Interest interest) + public static IEnumerable Validate(IntrestDto interest) { if (string.IsNullOrWhiteSpace(interest.Name)) { @@ -18,7 +18,7 @@ public static IEnumerable Validate(Interest interest) yield return new ValidationResult("Name is too long", [nameof(interest.Name)]); } - if (new Regex("^\\p{Lu}\\p{Ll}+(?:\\s\\p{L}+)*$").IsMatch(interest.Name)) + if (!new Regex("^\\p{Lu}\\p{Ll}+(?:\\s\\p{L}+)*$").IsMatch(interest.Name)) { yield return new ValidationResult("Name is invalid", [nameof(interest.Name)]); } diff --git a/Server/ReasnAPI/ReasnAPI/Validators/ObjectTypeValidator.cs b/Server/ReasnAPI/ReasnAPI/Validators/ObjectTypeValidator.cs index c26bddaf..e2e965ce 100644 --- a/Server/ReasnAPI/ReasnAPI/Validators/ObjectTypeValidator.cs +++ b/Server/ReasnAPI/ReasnAPI/Validators/ObjectTypeValidator.cs @@ -18,7 +18,7 @@ public static IEnumerable Validate(ObjectType objectType) yield return new ValidationResult("Name is too long", [nameof(objectType.Name)]); } - if (new Regex("^(Event|User)$").IsMatch(objectType.Name) is false) + if (!new Regex("^(Event|User)$").IsMatch(objectType.Name)) { yield return new ValidationResult("Name is invalid", [nameof(objectType.Name)]); } diff --git a/Server/ReasnAPI/ReasnAPI/Validators/ParameterValidator.cs b/Server/ReasnAPI/ReasnAPI/Validators/ParameterValidator.cs index 8a06fe21..42ad28fa 100644 --- a/Server/ReasnAPI/ReasnAPI/Validators/ParameterValidator.cs +++ b/Server/ReasnAPI/ReasnAPI/Validators/ParameterValidator.cs @@ -1,12 +1,12 @@ -using ReasnAPI.Models.Database; +using ReasnAPI.Models.DTOs; using System.ComponentModel.DataAnnotations; using System.Text.RegularExpressions; namespace ReasnAPI.Validators { - public class ParameterValidator : IValidator + public class ParameterValidator : IValidator { - public static IEnumerable Validate(Parameter parameter) + public static IEnumerable Validate(ParameterDto parameter) { if (string.IsNullOrWhiteSpace(parameter.Key)) { @@ -18,7 +18,7 @@ public static IEnumerable Validate(Parameter parameter) yield return new ValidationResult("Key is too long", [nameof(parameter.Key)]); } - if (new Regex("^\\p{L}+(?:\\s\\p{L}+)*$").IsMatch(parameter.Key)) + if (!new Regex("^\\p{L}+(?:\\s\\p{L}+)*$").IsMatch(parameter.Key)) { yield return new ValidationResult("Key is invalid", [nameof(parameter.Key)]); } @@ -33,7 +33,7 @@ public static IEnumerable Validate(Parameter parameter) yield return new ValidationResult("Value is too long", [nameof(parameter.Value)]); } - if (new Regex("^[\\p{L}\\d]+(?:\\s[\\p{L}\\d]+)*$").IsMatch(parameter.Value)) + if (!new Regex("^[\\p{L}\\d]+(?:\\s[\\p{L}\\d]+)*$").IsMatch(parameter.Value)) { yield return new ValidationResult("Value is invalid", [nameof(parameter.Value)]); } diff --git a/Server/ReasnAPI/ReasnAPI/Validators/RoleValidator.cs b/Server/ReasnAPI/ReasnAPI/Validators/RoleValidator.cs index 362a9fc9..83712151 100644 --- a/Server/ReasnAPI/ReasnAPI/Validators/RoleValidator.cs +++ b/Server/ReasnAPI/ReasnAPI/Validators/RoleValidator.cs @@ -1,24 +1,19 @@ -using ReasnAPI.Models.Database; +using ReasnAPI.Models.DTOs; using System.ComponentModel.DataAnnotations; using System.Text.RegularExpressions; namespace ReasnAPI.Validators { - public class RoleValidator : IValidator + public class RoleValidator : IValidator { - public static IEnumerable Validate(Role role) + public static IEnumerable Validate(RoleDto role) { if (string.IsNullOrWhiteSpace(role.Name)) { yield return new ValidationResult("Role name is required", [nameof(role.Name)]); } - if (role.Name.Length > 32) - { - yield return new ValidationResult("Role name is too long", [nameof(role.Name)]); - } - - if (new Regex("^(Organizer|Admin|User)$").IsMatch(role.Name) is false) + if (!new Regex("^(Organizer|Admin|User)$").IsMatch(role.Name)) { yield return new ValidationResult("Role name is invalid", [nameof(role.Name)]); } diff --git a/Server/ReasnAPI/ReasnAPI/Validators/StatusValidator.cs b/Server/ReasnAPI/ReasnAPI/Validators/StatusValidator.cs index a28e7bb1..680a6e0d 100644 --- a/Server/ReasnAPI/ReasnAPI/Validators/StatusValidator.cs +++ b/Server/ReasnAPI/ReasnAPI/Validators/StatusValidator.cs @@ -1,29 +1,19 @@ -using ReasnAPI.Models.Database; +using ReasnAPI.Models.DTOs; using System.ComponentModel.DataAnnotations; using System.Text.RegularExpressions; namespace ReasnAPI.Validators { - public class StatusValidator : IValidator + public class StatusValidator : IValidator { - public static IEnumerable Validate(Status status) + public static IEnumerable Validate(StatusDto status) { if (string.IsNullOrWhiteSpace(status.Name)) { yield return new ValidationResult("Name is required", [nameof(status.Name)]); } - if (status.Name.Length > 32) - { - yield return new ValidationResult("Name is too long", [nameof(status.Name)]); - } - - if (string.IsNullOrWhiteSpace(status.ObjectTypeId.ToString())) - { - yield return new ValidationResult("ObjectTypeId is required", [nameof(status.ObjectTypeId)]); - } - - if (new Regex("^(Interested|Participating|Completed|In progress|Waiting for approval)$").IsMatch(status.Name) is false) + if (!new Regex("^(Interested|Participating|Completed|In progress|Waiting for approval)$").IsMatch(status.Name)) { yield return new ValidationResult("Name is invalid", [nameof(status.Name)]); } diff --git a/Server/ReasnAPI/ReasnAPI/Validators/TagValidator.cs b/Server/ReasnAPI/ReasnAPI/Validators/TagValidator.cs index ff7ee67c..10c45944 100644 --- a/Server/ReasnAPI/ReasnAPI/Validators/TagValidator.cs +++ b/Server/ReasnAPI/ReasnAPI/Validators/TagValidator.cs @@ -1,12 +1,12 @@ -using ReasnAPI.Models.Database; +using ReasnAPI.Models.DTOs; using System.ComponentModel.DataAnnotations; using System.Text.RegularExpressions; namespace ReasnAPI.Validators { - public class TagValidator : IValidator + public class TagValidator : IValidator { - public static IEnumerable Validate(Tag tag) + public static IEnumerable Validate(TagDto tag) { if (string.IsNullOrWhiteSpace(tag.Name)) { @@ -18,7 +18,7 @@ public static IEnumerable Validate(Tag tag) yield return new ValidationResult("Name is too long", [nameof(tag.Name)]); } - if (new Regex("^\\p{L}+(?:\\s\\p{L}+)*$").IsMatch(tag.Name)) + if (!new Regex("^\\p{L}+(?:\\s\\p{L}+)*$").IsMatch(tag.Name)) { yield return new ValidationResult("Name is invalid", [nameof(tag.Name)]); } diff --git a/Server/ReasnAPI/ReasnAPI/Validators/UserValidator.cs b/Server/ReasnAPI/ReasnAPI/Validators/UserValidator.cs index b6dfb0d0..2e153655 100644 --- a/Server/ReasnAPI/ReasnAPI/Validators/UserValidator.cs +++ b/Server/ReasnAPI/ReasnAPI/Validators/UserValidator.cs @@ -1,12 +1,12 @@ -using ReasnAPI.Models.Database; +using ReasnAPI.Models.DTOs; using System.ComponentModel.DataAnnotations; using System.Text.RegularExpressions; namespace ReasnAPI.Validators { - public class UserValidator : IValidator + public class UserValidator : IValidator { - public static IEnumerable Validate(User user) + public static IEnumerable Validate(UserDto user) { if (string.IsNullOrWhiteSpace(user.Name)) { @@ -18,7 +18,7 @@ public static IEnumerable Validate(User user) yield return new ValidationResult("Name is too long", [nameof(user.Name)]); } - if (new Regex("^\\p{Lu}\\p{Ll}+$").IsMatch(user.Name) is false) + if (!new Regex("^\\p{Lu}\\p{Ll}+$").IsMatch(user.Name)) { yield return new ValidationResult("Name is invalid", [nameof(user.Name)]); } @@ -33,7 +33,7 @@ public static IEnumerable Validate(User user) yield return new ValidationResult("Surname is too long", [nameof(user.Surname)]); } - if (new Regex("^\\p{L}+(?:[\\s'-]?\\p{L}+)*$").IsMatch(user.Surname) is false) + if (!new Regex("^\\p{L}+(?:[\\s'-]?\\p{L}+)*$").IsMatch(user.Surname)) { yield return new ValidationResult("Surname is invalid", [nameof(user.Surname)]); } @@ -48,31 +48,11 @@ public static IEnumerable Validate(User user) yield return new ValidationResult("Username is too long", [nameof(user.Username)]); } - if (new Regex("^[\\p{L}\\d._%+-]+$").IsMatch(user.Username) is false) + if (!new Regex("^[\\p{L}\\d._%+-]+$").IsMatch(user.Username)) { yield return new ValidationResult("Username is invalid", [nameof(user.Username)]); } - if (string.IsNullOrWhiteSpace(user.Password)) - { - yield return new ValidationResult("Password is required", [nameof(user.Password)]); - } - - if (string.IsNullOrWhiteSpace(user.CreatedAt.ToString())) - { - yield return new ValidationResult("CreatedAt is required", [nameof(user.CreatedAt)]); - } - - if (string.IsNullOrWhiteSpace(user.UpdatedAt.ToString())) - { - yield return new ValidationResult("UpdatedAt is required", [nameof(user.UpdatedAt)]); - } - - if (string.IsNullOrWhiteSpace(user.RoleId.ToString())) - { - yield return new ValidationResult("RoleId is required", [nameof(user.RoleId)]); - } - if (string.IsNullOrWhiteSpace(user.Email)) { yield return new ValidationResult("Email is required", [nameof(user.Email)]); @@ -83,24 +63,19 @@ public static IEnumerable Validate(User user) yield return new ValidationResult("Email is too long", [nameof(user.Email)]); } - if (new Regex("^[a-zA-Z\\d._%+-]+@[a-zA-Z\\d.-]+\\.[a-zA-Z]{2,6}$").IsMatch(user.Email) is false) + if (!new Regex("^[a-zA-Z\\d._%+-]+@[a-zA-Z\\d.-]+\\.[a-zA-Z]{2,6}$").IsMatch(user.Email)) { yield return new ValidationResult("Email is invalid", [nameof(user.Email)]); } - if (string.IsNullOrWhiteSpace(user.AddressId.ToString())) - { - yield return new ValidationResult("AddressId is required", [nameof(user.AddressId)]); - } - if (!string.IsNullOrWhiteSpace(user.Phone)) { - if (!string.IsNullOrWhiteSpace(user.Phone) && user.Phone.Length > 16) + if (user.Phone.Length > 16) { yield return new ValidationResult("Phone is too long", [nameof(user.Phone)]); } - if (new Regex("^\\+\\d{1,3}\\s\\d{1,15}$").IsMatch(user.Phone) is false) + if (!new Regex("^\\+\\d{1,3}\\s\\d{1,15}$").IsMatch(user.Phone)) { yield return new ValidationResult("Phone is invalid", [nameof(user.Phone)]); }