Skip to content

Commit

Permalink
Merge pull request #88 from CUMGroup/builder
Browse files Browse the repository at this point in the history
Builder
  • Loading branch information
AlexMi-Ha authored Apr 12, 2024
2 parents 4f12a77 + 164b94a commit 616e704
Show file tree
Hide file tree
Showing 10 changed files with 629 additions and 5 deletions.
9 changes: 8 additions & 1 deletion .github/workflows/test_on_pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,11 @@ jobs:
- name: Run Unit Tests
run: |
cd PWManager.UnitTests
dotnet test --logger "console;verbosity=detailed"
rm -rf coverage
rm -rf TestResults
dotnet tool install -g dotnet-reportgenerator-globaltool
dotnet test --logger "console;verbosity=detailed" --collect:"XPlat Code Coverage"
reportgenerator "-reports:TestResults/*/coverage*" "-targetdir:coverage" "-reporttypes:TextSummary"
cat coverage/Summary.txt
rm -rf coverage
rm -rf TestResults
4 changes: 4 additions & 0 deletions PWManager.Application/Exceptions/MessageStrings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,8 @@ public static class MessageStrings {
public const string PATH_ERROR = "An unknown error occured! Could not determine execution path!";
public const string DIRECTORY_ERROR = "An unknown error occured! Execution path is not a directory!";
// ----------------------------------------

// PasswordBuilder
public const string MIN_LENGTH_TO_SMALL = "MinLength cannot be smaller than 0";
public const string MAX_LENGTH_TO_SMALL = "MaxLength cannot be smaller than MinLength";
}
2 changes: 2 additions & 0 deletions PWManager.Application/Services/AccountService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public void AddNewAccount(string identifier, string loginname, string password)

var saved = _groupRepo.AddAccountToGroup(account, _environment.CurrentGroup);
if (!saved) {
_environment.CurrentGroup.RemoveAccount(account);
throw new UserFeedbackException(MessageStrings.FAILED_ADDING_ACCOUNT);
}
}
Expand Down Expand Up @@ -87,6 +88,7 @@ public void DeleteAccount(string identifier) {
throw new UserFeedbackException(MessageStrings.ACCOUNT_NOT_FOUND);
}

_environment.CurrentGroup.RemoveAccount(acc);
_groupRepo.DeleteAccountInGroup(acc, _environment.CurrentGroup);
}

Expand Down
84 changes: 84 additions & 0 deletions PWManager.Application/Services/PasswordBuilder.cs
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);
}
}
6 changes: 3 additions & 3 deletions PWManager.Application/Services/PasswordGeneratorService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace PWManager.Application.Services;

public class PasswordGeneratorService : IPasswordGeneratorService {

private readonly PasswordGeneratorCriteria _userSettings;
private readonly ISettingsRepository _settingsRepo;

private const string Lowercase = "abcdefghijklmnopqrstuvwxyz";
private const string Uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Expand All @@ -23,7 +23,7 @@ public class PasswordGeneratorService : IPasswordGeneratorService {
}

public PasswordGeneratorService(ISettingsRepository settingsRepository, Random rng) {
_userSettings = settingsRepository.GetSettings().PwGenCriteria;
_settingsRepo = settingsRepository;
_rng = rng;
}

Expand All @@ -44,7 +44,7 @@ public string GeneratePasswordWith(PasswordGeneratorCriteria criteria) {
}

public string GeneratePassword() {
return GeneratePasswordWith(_userSettings);
return GeneratePasswordWith(_settingsRepo.GetSettings().PwGenCriteria);
}

private static char[] BuildPossibleChars(PasswordGeneratorCriteria criteria) {
Expand Down
2 changes: 1 addition & 1 deletion PWManager.Domain/ValueObjects/PasswordGeneratorCriteria.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class PasswordGeneratorCriteria : ValueObject {
public bool IncludeBrackets { get; }
public int MinLength { get; }
public int MaxLength { get; }

public PasswordGeneratorCriteria(bool includeLowerCase, bool includeUpperCase, bool includeNumeric, bool includeSpecial, bool includeBrackets, bool includeSpaces, int minLength, int maxLength) {
if (minLength <= 0) {
throw new ArgumentException("MinLength cannot be less than or equal to 0");
Expand Down
141 changes: 141 additions & 0 deletions PWManager.UnitTests/Application/PasswordBuilderTest.cs
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);
}
}
Loading

0 comments on commit 616e704

Please sign in to comment.