Skip to content

Commit

Permalink
feat: install catch2, add basic testing for password set creation
Browse files Browse the repository at this point in the history
  • Loading branch information
ni-jessica committed Oct 22, 2023
1 parent e7eedf4 commit 1b0f7e4
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 5 deletions.
20 changes: 20 additions & 0 deletions backend/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
cmake_minimum_required(VERSION 3.27)

## application
project(backend)

find_package(Crow REQUIRED)
Expand All @@ -7,6 +9,7 @@ find_package(SQLite3 REQUIRED)
set(SOURCES
src/main.cpp
src/database.cpp
src/passwords.cpp
)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
Expand All @@ -16,3 +19,20 @@ add_executable(${PROJECT_NAME} ${SOURCES})
target_link_libraries(${PROJECT_NAME} Crow::Crow SQLite::SQLite3)

target_include_directories(${PROJECT_NAME} PRIVATE src)

## tests
project(tests)

find_package(Catch2 REQUIRED)

set (TEST_SOURCE
tests/test.cpp
)

add_executable(${PROJECT_NAME} ${TEST_SOURCE})

target_link_libraries(${PROJECT_NAME} PRIVATE Catch2::Catch2WithMain)

target_include_directories(${PROJECT_NAME} PRIVATE tests)

include(Catch)
6 changes: 6 additions & 0 deletions backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,9 @@ To fix VS Code import errors with Crow, try adding the following line to your `s
```

[Source](https://stackoverflow.com/questions/58077908/linking-conan-include-to-vs-code)

## Testing
After building, you can run tests from `/backend`:
```bash
cd build && ./tests
```
1 change: 1 addition & 0 deletions backend/conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class BackendRecipe(ConanFile):
def requirements(self):
self.requires("crowcpp-crow/1.0+5")
self.requires("sqlite3/3.42.0")
self.requires("catch2/3.4.0")

def build_requirements(self):
self.tool_requires("cmake/3.22.6")
3 changes: 2 additions & 1 deletion backend/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
#include "crow.h"
#include <sqlite3.h>
#include "database.hpp"
#include "passwords.hpp"

int main()
{
{
database::Database db = database::Database("passwords.db");
db.execute("CREATE TABLE passwords (password TEXT);");
db.execute("INSERT INTO passwords (password) VALUES ('chocolate1');");
Expand Down
30 changes: 26 additions & 4 deletions backend/tests/test.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,29 @@
#include <cassert>
#include <iostream>
#define CATCH_CONFIG_MAIN
#include <regex>
#include <catch2/catch_all.hpp>
#include "../src/passwords.cpp"

int main()
std::unordered_set<std::string> passwordSet = passwords::generatePasswords(5, 24);

bool hasLettersAndDigit(std::string str)
{
const std::regex pattern("^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d).+$");
return regex_match(str, pattern);
}

bool hasSymbol(std::string str)
{
return str.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890_") != std::string::npos;
}

TEST_CASE("Test basic password requirements")
{
return 0;
// function generated 5 passwords
REQUIRE(passwordSet.size() == 5);
// a password has more than 10 characters
auto password = *(passwordSet.begin());
REQUIRE(password.length() >= 10);
// a password meets all char requirements
REQUIRE(hasLettersAndDigit(password));
REQUIRE(hasSymbol(password));
}

0 comments on commit 1b0f7e4

Please sign in to comment.