Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RSN-31] - Create validators according to constraints #32

Merged
merged 27 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
423326f
Created basic validators for all models in db
mkoper02 May 6, 2024
4f39e38
Created regex check for all models
mkoper02 May 11, 2024
b2825e8
Added regex in validators, changed models into dtos, created unit tests
mkoper02 May 13, 2024
33a51b0
Revert "Added regex in validators, changed models into dtos, created …
mkoper02 May 13, 2024
444e194
Added tests, changed models in validators into dtos
mkoper02 May 13, 2024
a337026
Deleted EventDto changes
mkoper02 May 13, 2024
e794972
Unit test fix
mkoper02 May 13, 2024
c5dcd82
Restore ReasnAPI.csproj
mkoper02 May 14, 2024
be3fc47
Created const variables for regex strings and hardcoded numbers
mkoper02 May 15, 2024
740fdce
Created basic validators for all models in db
mkoper02 May 6, 2024
5c611e9
Created regex check for all models
mkoper02 May 11, 2024
3c3a10b
Added regex in validators, changed models into dtos, created unit tests
mkoper02 May 13, 2024
0ac564d
Revert "Added regex in validators, changed models into dtos, created …
mkoper02 May 13, 2024
d1e447c
Added tests, changed models in validators into dtos
mkoper02 May 13, 2024
4ace779
Unit test fix
mkoper02 May 13, 2024
934e445
Restore ReasnAPI.csproj
mkoper02 May 14, 2024
dee72d6
Created const variables for regex strings and hardcoded numbers
mkoper02 May 15, 2024
208050c
Updated validator after scaffold
mkoper02 May 20, 2024
9f0295a
Merge branch 'RSN-31' of https://github.com/wzarek/Reasn into RSN-31
mkoper02 May 20, 2024
9a151d0
Updated unit tests after scaffold
mkoper02 May 20, 2024
30a8666
feat: add jwt and exceptions handlers
raczu Jun 1, 2024
14e3333
test: add unit tests for jwt, handlers and validators
raczu Jun 1, 2024
26348f0
style: format code with dotnet format
raczu Jun 4, 2024
c6ad6fe
Merge remote-tracking branch 'origin/RSN-40' into RSN-31
raczu Jun 4, 2024
fe7e7be
refactor: rewrite validators to use fluent validation
raczu Jun 5, 2024
8a25fe8
refactor: make regexes and length variables as constants
raczu Jun 5, 2024
ffeb579
Merge remote-tracking branch 'origin/main' into RSN-31
raczu Jun 6, 2024
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
4 changes: 4 additions & 0 deletions Server/ReasnAPI/ReasnAPI.Tests/ReasnAPI.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ReasnAPI\ReasnAPI.csproj" />
</ItemGroup>

<ItemGroup>
<Using Include="Microsoft.Playwright.MSTest" />
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" />
Expand Down
12 changes: 0 additions & 12 deletions Server/ReasnAPI/ReasnAPI.Tests/UnitTest1.cs

This file was deleted.

336 changes: 336 additions & 0 deletions Server/ReasnAPI/ReasnAPI.Tests/Validators/AddressValidatorTests.cs
Original file line number Diff line number Diff line change
@@ -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());
}
}
}
Loading