Skip to content

Commit

Permalink
improve password generation (#229)
Browse files Browse the repository at this point in the history
  • Loading branch information
cieslarmichal authored Nov 10, 2023
1 parent 9ee5465 commit 05f306d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/modules/internet/Internet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ std::string Internet::exampleEmail(std::optional<std::string> firstName, std::op

std::string Internet::password(int length)
{
const std::string passwordCharacters = "0123456789!@#$%^&*abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
const std::string passwordCharacters =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789~`!@#$%^&*()_-+={[}]|:;\"'<,>.?/";

std::string password;

Expand Down
10 changes: 4 additions & 6 deletions src/modules/internet/InternetTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ using namespace faker;

namespace
{
const std::string passwordCharacters =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789~`!@#$%^&*()_-+={[}]|:;\"'<,>.?/";
const std::vector<std::string> webProtocols{"http", "https"};
const std::vector<std::string> httpMethodNames{"GET", "POST", "DELETE", "PATCH", "PUT"};
const std::vector<unsigned> httpStatusInformationalCodes{100, 101, 102, 103};
Expand Down Expand Up @@ -359,25 +361,21 @@ TEST_F(InternetTest, shouldGenerateExampleEmailWithFullName)

TEST_F(InternetTest, shouldGeneratePassword)
{
const std::string passwordCharacters = "0123456789!@#$%^&*abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

const auto password = Internet::password();

ASSERT_EQ(password.size(), 15);
ASSERT_TRUE(std::ranges::all_of(password, [passwordCharacters](char passwordCharacter)
ASSERT_TRUE(std::ranges::all_of(password, [&](char passwordCharacter)
{ return passwordCharacters.find(passwordCharacter) != std::string::npos; }));
}

TEST_F(InternetTest, shouldGeneratePasswordWithSpecifiedLength)
{
const auto passwordLength = 25;

const std::string passwordCharacters = "0123456789!@#$%^&*abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

const auto password = Internet::password(passwordLength);

ASSERT_EQ(password.size(), passwordLength);
ASSERT_TRUE(std::ranges::all_of(password, [passwordCharacters](char passwordCharacter)
ASSERT_TRUE(std::ranges::all_of(password, [&](char passwordCharacter)
{ return passwordCharacters.find(passwordCharacter) != std::string::npos; }));
}

Expand Down

0 comments on commit 05f306d

Please sign in to comment.