Skip to content

Commit

Permalink
Tests - SystemCredentialsTests + Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
nlogozzo committed Dec 11, 2023
1 parent 299bbf4 commit ff86f5f
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 54 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ if(NOT SKIP_TESTS)
enable_testing()
add_executable(${PROJECT_NAME}_test
tests/eventtests.cpp
tests/keyringtests.cpp
tests/main.cpp
tests/networktests.cpp
tests/stringtests.cpp
tests/systemcredentialstests.cpp
tests/updatertests.cpp
tests/versiontests.cpp
tests/webtests.cpp)
Expand Down
5 changes: 5 additions & 0 deletions include/keyring/credential.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef CREDENTIAL_H
#define CREDENTIAL_H

#include <iostream>
#include <string>
#include "passwordstrength.h"

Expand Down Expand Up @@ -98,6 +99,10 @@ namespace Nickvision::Aura::Keyring
* @return True if this != compare
*/
bool operator!=(const Credential& compare) const;
/**
* @brief Outputs the Credential object
*/
friend std::ostream& operator<<(std::ostream& os, const Credential& credential);

private:
int m_id;
Expand Down
10 changes: 10 additions & 0 deletions src/keyring/credential.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,14 @@ namespace Nickvision::Aura::Keyring
}
return (PasswordStrength)strength;
}

std::ostream& operator<<(std::ostream& os, const Credential& credential)
{
os << "[CRED: " << credential.getName() << "] " << std::endl;
os << "Uri: " << credential.getUri() << std::endl;
os << "Username: " << credential.getUsername() << std::endl;
os << "Password: " << credential.getPassword() << std::endl;
os << "Strength: " << (int)Credential::getPasswordStrength(credential.getPassword()) << std::endl;
return os;
}
}
7 changes: 5 additions & 2 deletions src/keyring/systemcredentials.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ namespace Nickvision::Aura::Keyring
{
if (cred->CredentialBlob)
{
Credential c{ cred->TargetName, cred->Comment, cred->UserName, LPCSTR(cred->CredentialBlob) };
std::string name{ cred->TargetName ? cred->TargetName : "" };
std::string url{ cred->Comment ? cred->Comment : "" };
std::string username{ cred->UserName ? cred->UserName : "" };
std::string password{ LPCSTR(cred->CredentialBlob) ? LPCSTR(cred->CredentialBlob) : "", cred->CredentialBlobSize };
CredFree(cred);
return c;
return { { name, url, username, password } };
}
}
#else
Expand Down
51 changes: 0 additions & 51 deletions tests/keyringtests.cpp

This file was deleted.

42 changes: 42 additions & 0 deletions tests/systemcredentialstests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <gtest/gtest.h>
#include "keyring/credential.h"
#include "keyring/systemcredentials.h"

using namespace Nickvision::Aura::Keyring;

static const std::string randCredentialName{ "RandomAuraTestCredential1" };

TEST(SystemCredentialsTests, AddRandCredential)
{
std::optional<Credential> cred{ SystemCredentials::addCredential(randCredentialName) };
std::cout << cred.value() << std::endl;
ASSERT_TRUE(cred.has_value());
ASSERT_TRUE(cred.value().getName() == randCredentialName);
ASSERT_TRUE(!cred.value().getPassword().empty());
}

TEST(SystemCredentialsTests, FetchRandCredential)
{
std::optional<Credential> cred{ SystemCredentials::getCredential(randCredentialName) };
std::cout << cred.value() << std::endl;
ASSERT_TRUE(cred.has_value());
ASSERT_TRUE(cred.value().getName() == randCredentialName);
ASSERT_TRUE(!cred.value().getPassword().empty());
}

TEST(SystemCredentialsTests, UpdateRandCredential)
{
Credential updatedCred{ randCredentialName, "", "TEST", "abc123" };
ASSERT_TRUE(SystemCredentials::updateCredential(updatedCred));
std::optional<Credential> cred{ SystemCredentials::getCredential(randCredentialName) };
std::cout << cred.value() << std::endl;
ASSERT_TRUE(cred.has_value());
ASSERT_TRUE(cred.value().getName() == randCredentialName);
ASSERT_TRUE(cred.value().getUsername() == "TEST");
ASSERT_TRUE(cred.value().getPassword() == "abc123");
}

TEST(SystemCredentialsTest, DeleteRandCredential)
{
ASSERT_TRUE(SystemCredentials::deleteCredential(randCredentialName));
}

0 comments on commit ff86f5f

Please sign in to comment.