-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #88 from CUMGroup/builder
Builder
- Loading branch information
Showing
10 changed files
with
629 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using PWManager.Application.Exceptions; | ||
using PWManager.Domain.Services.Interfaces; | ||
using PWManager.Domain.ValueObjects; | ||
|
||
namespace PWManager.Application.Services; | ||
|
||
public class PasswordBuilder { | ||
|
||
private IPasswordGeneratorService _generatorService; | ||
|
||
private bool _includeLowerCase = false; | ||
private bool _includeUpperCase = false; | ||
private bool _includeSpecial = false; | ||
private bool _includeSpaces = false; | ||
private bool _includeNumeric = false; | ||
private bool _includeBrackets = false; | ||
private int _minLength = 5; | ||
private int _maxLength = 8; | ||
|
||
private PasswordBuilder() : this(new PasswordGeneratorService(null)) {} | ||
|
||
private PasswordBuilder(IPasswordGeneratorService generatorService) { | ||
_generatorService = generatorService; | ||
} | ||
|
||
public PasswordBuilder SetMinLength(int minLength) { | ||
if (minLength <= 0) { | ||
throw new PasswordGenerationException(MessageStrings.MIN_LENGTH_TO_SMALL); | ||
} | ||
_minLength = minLength; | ||
return this; | ||
} | ||
|
||
public PasswordBuilder SetMaxLength(int maxLength) { | ||
if (maxLength < _minLength) { | ||
throw new PasswordGenerationException(MessageStrings.MAX_LENGTH_TO_SMALL); | ||
} | ||
_maxLength = maxLength; | ||
return this; | ||
} | ||
|
||
public PasswordBuilder IncludeUppercase() { | ||
_includeUpperCase = true; | ||
return this; | ||
} | ||
public PasswordBuilder IncludeLowercase() { | ||
_includeLowerCase = true; | ||
return this; | ||
} | ||
public PasswordBuilder IncludeSpecialChars() { | ||
_includeSpecial = true; | ||
return this; | ||
} | ||
public PasswordBuilder IncludeSpaces() { | ||
_includeSpaces = true; | ||
return this; | ||
} | ||
public PasswordBuilder IncludeNumeric() { | ||
_includeNumeric = true; | ||
return this; | ||
} | ||
public PasswordBuilder IncludeBrackets() { | ||
_includeBrackets = true; | ||
return this; | ||
} | ||
|
||
public string BuildPassword() { | ||
var criteria = BuildCriteria(); | ||
return _generatorService.GeneratePasswordWith(criteria); | ||
} | ||
|
||
internal PasswordGeneratorCriteria BuildCriteria() { | ||
return new PasswordGeneratorCriteria(_includeLowerCase, _includeUpperCase, _includeNumeric, | ||
_includeSpecial, _includeBrackets, _includeSpaces, _minLength, _maxLength); | ||
} | ||
|
||
public static PasswordBuilder Create() { | ||
return new PasswordBuilder(); | ||
} | ||
|
||
internal static PasswordBuilder Create(IPasswordGeneratorService service) { | ||
return new PasswordBuilder(service); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
using NSubstitute; | ||
using PWManager.Application.Exceptions; | ||
using PWManager.Application.Services; | ||
using PWManager.Domain.Services.Interfaces; | ||
using PWManager.Domain.ValueObjects; | ||
|
||
namespace PWManager.UnitTests.Application; | ||
|
||
public class PasswordBuilderTest { | ||
|
||
[Fact] | ||
public void PasswordBuilder_Should_SetMinLength() { | ||
var criteria = PasswordBuilder.Create() | ||
.IncludeUppercase() | ||
.SetMinLength(8) | ||
.BuildCriteria(); | ||
|
||
Assert.Equal(8, criteria.MinLength); | ||
} | ||
|
||
[Fact] | ||
public void PasswordBuilder_Should_SetMaxLength() { | ||
var criteria = PasswordBuilder.Create() | ||
.IncludeUppercase() | ||
.SetMaxLength(80) | ||
.BuildCriteria(); | ||
|
||
Assert.Equal(80, criteria.MaxLength); | ||
} | ||
|
||
[Fact] | ||
public void PasswordBuilder_Should_IncludeLowercase() { | ||
var criteria = PasswordBuilder.Create() | ||
.IncludeLowercase() | ||
.BuildCriteria(); | ||
|
||
Assert.True(criteria.IncludeLowerCase); | ||
} | ||
|
||
[Fact] | ||
public void PasswordBuilder_Should_IncludeUppercase() { | ||
var criteria = PasswordBuilder.Create() | ||
.IncludeUppercase() | ||
.BuildCriteria(); | ||
|
||
Assert.True(criteria.IncludeUpperCase); | ||
} | ||
|
||
[Fact] | ||
public void PasswordBuilder_Should_IncludeSpecialChars() { | ||
var criteria = PasswordBuilder.Create() | ||
.IncludeSpecialChars() | ||
.BuildCriteria(); | ||
|
||
Assert.True(criteria.IncludeSpecial); | ||
} | ||
|
||
[Fact] | ||
public void PasswordBuilder_Should_IncludeSpacesAndUppercase() { | ||
var criteria = PasswordBuilder.Create() | ||
.IncludeUppercase() | ||
.IncludeSpaces() | ||
.BuildCriteria(); | ||
|
||
Assert.True(criteria.IncludeSpaces); | ||
} | ||
|
||
[Fact] | ||
public void PasswordBuilder_Should_IncludeBrackets() { | ||
var criteria = PasswordBuilder.Create() | ||
.IncludeBrackets() | ||
.BuildCriteria(); | ||
|
||
Assert.True(criteria.IncludeBrackets); | ||
} | ||
|
||
[Fact] | ||
public void PasswordBuilder_Should_IncludeNumeric() { | ||
var criteria = PasswordBuilder.Create() | ||
.IncludeNumeric() | ||
.BuildCriteria(); | ||
|
||
Assert.True(criteria.IncludeNumeric); | ||
} | ||
|
||
[Fact] | ||
public void PasswordBuilder_Should_IncludeAll() { | ||
var criteria = PasswordBuilder.Create() | ||
.IncludeUppercase() | ||
.IncludeLowercase() | ||
.IncludeSpaces() | ||
.IncludeSpecialChars() | ||
.IncludeBrackets() | ||
.IncludeNumeric() | ||
.SetMinLength(10) | ||
.SetMaxLength(100) | ||
.BuildCriteria(); | ||
|
||
Assert.True(criteria.IncludeUpperCase); | ||
Assert.True(criteria.IncludeLowerCase); | ||
Assert.True(criteria.IncludeSpaces); | ||
Assert.True(criteria.IncludeSpecial); | ||
Assert.True(criteria.IncludeBrackets); | ||
Assert.True(criteria.IncludeNumeric); | ||
Assert.Equal(10, criteria.MinLength); | ||
Assert.Equal(100, criteria.MaxLength); | ||
} | ||
|
||
[Fact] | ||
public void PasswordBuilder_Should_BuildPassword() { | ||
var generator = Substitute.For<IPasswordGeneratorService>(); | ||
generator.GeneratePasswordWith(Arg.Any<PasswordGeneratorCriteria>()).Returns("GeneratedPassword"); | ||
var password = PasswordBuilder.Create(generator) | ||
.IncludeUppercase() | ||
.BuildPassword(); | ||
|
||
Assert.Equal("GeneratedPassword", password); | ||
} | ||
|
||
[Fact] | ||
public void PasswordBuilder_ShouldNot_SetMinLengthWrong() { | ||
var ex = Assert.Throws<PasswordGenerationException>(() => { | ||
PasswordBuilder.Create() | ||
.SetMinLength(-23) | ||
.BuildCriteria(); | ||
}); | ||
|
||
Assert.Equal(MessageStrings.MIN_LENGTH_TO_SMALL, ex.Message); | ||
} | ||
|
||
[Fact] | ||
public void PasswordBuilder_ShouldNot_SetMaxLengthWrong() { | ||
var ex = Assert.Throws<PasswordGenerationException>(() => { | ||
PasswordBuilder.Create() | ||
.SetMaxLength(-23) | ||
.BuildCriteria(); | ||
}); | ||
|
||
Assert.Equal(MessageStrings.MAX_LENGTH_TO_SMALL, ex.Message); | ||
} | ||
} |
Oops, something went wrong.