<commit_before>#include <iostream>

#include "models/Users.h" 
#include "generics/test.h"

#define TEST_NAME "test"
#define SQL_PATH "../"
#define BASIC_SQL "basic.sql"
#define TEST_PATH "tests/models/Users/add/"

//TODO copy paste of "is_admin.cpp"

class Test : public cppcmsskel::models::Users {

    private:
        const std::string newName = "new user";
        const std::string newEmail = "<EMAIL>";
        const std::string password = "<PASSWORD>";

        const std::string alreadyTakenName = "old user";
        const std::string alreadyTakenEmail = "<EMAIL>";

        // 
        bool test_add_new_user(
            const std::string &name,
            const std::string &email
        ) {


            int userId = add(
                name,
                password,
                email
            );

            return test_user_created(
                userId,
                name,
                email,
                password
            );
        }
        // performs various test that should fail if the user
        // does not exist, and will success if the success if the
        // user has been correctly added
        bool test_user_created(
            const int userId,
            const std::string &name,
            const std::string &email,
            const std::string &newPassword
        ) {
            if (userId == USERS_NOT_ADDED_ERROR) {
                std::cout << "not added" << std::endl;
                return false;
            }
            if (!username_exists(name)) {
                std::cout << "no name" << std::endl;
                return false;
            }

            if (!email_exists(email)) {

                std::cout << "no email" << std::endl;
                return false;
            }

            if (!username_exists(name)) {
                std::cout << "no name" << std::endl;
                return false;
            }

            if (!is_login_correct(name, newPassword)) {
                std::cout << "login wrong" << std::endl;
                return false;
            }

            return true;
        }
    public:
        Test() : cppcmsskel::models::Users(TEST_PATH TEST_NAME ".db") {

        }

        bool test_add_real_new_user() {
            return test_add_new_user(
                newName,
                newEmail
            );
        }

        bool test_email_taken() {
            return test_add_new_user(
                newName,
                alreadyTakenEmail
            );
        }

        bool test_username_taken() {
            return test_add_new_user(
                alreadyTakenName,
                newEmail
            );
        }

        bool test_username_email_taken() {
            return test_add_new_user(
                alreadyTakenName,
                alreadyTakenEmail
            );
        }

};
        

int main() {

    bool nothingFailed = true;
    Test model;

    std::cout << SQL_PATH TEST_PATH BASIC_SQL << std::endl;
    CPPCMSSKEL_TEST_RESULT_WORK(
        "Try to create the tables ... " ,
        model.import_sql_file(
          SQL_PATH TEST_PATH BASIC_SQL
        ),
        nothingFailed
    );


    CPPCMSSKEL_TEST_RESULT_WORK(
        "if brand new user => true ... " ,
        model.test_add_real_new_user(),
        nothingFailed
    );

    CPPCMSSKEL_TEST_RESULT_NOT_WORK(
        "If email already taken => false ... " ,
        model.test_email_taken(),
        nothingFailed
    );

    CPPCMSSKEL_TEST_RESULT_NOT_WORK(
        "If username is already used => false ... " ,
        model.test_username_taken(),
        nothingFailed
    );

    CPPCMSSKEL_TEST_RESULT_NOT_WORK(
        "If both username and email are taken => false ... " ,
        model.test_username_email_taken(),
        nothingFailed
    );



    if (nothingFailed) {
        return 0;
    } else {
        return 1;
    }
}
<commit_msg>modifed slightly the test for models::users::add, the test for 'only username already taken' and the same for 'only email already taken' were actually reusing the 'new username' / 'new email' used to test if we can add normally a user, which made that actually this new email/new user were also taken at the time of the test<commit_after>#include <iostream>

#include "models/Users.h" 
#include "generics/test.h"

#define TEST_NAME "test"
#define SQL_PATH "../"
#define BASIC_SQL "basic.sql"
#define TEST_PATH "tests/models/Users/add/"

//TODO copy paste of "is_admin.cpp"

class Test : public cppcmsskel::models::Users {

    private:
        const std::string newName = "new user";
        const std::string newEmail = "<EMAIL>";
        const std::string password = "<PASSWORD>";

        const std::string alreadyTakenName = "old user";
        const std::string alreadyTakenEmail = "<EMAIL>";

        // 
        bool test_add_new_user(
            const std::string &name,
            const std::string &email
        ) {


            int userId = add(
                name,
                password,
                email
            );

            return test_user_created(
                userId,
                name,
                email,
                password
            );
        }
        // performs various test that should fail if the user
        // does not exist, and will success if the success if the
        // user has been correctly added
        bool test_user_created(
            const int userId,
            const std::string &name,
            const std::string &email,
            const std::string &newPassword
        ) {
            if (userId == USERS_NOT_ADDED_ERROR) {
                std::cout << "not added" << std::endl;
                return false;
            }
            if (!username_exists(name)) {
                std::cout << "no name" << std::endl;
                return false;
            }

            if (!email_exists(email)) {

                std::cout << "no email" << std::endl;
                return false;
            }

            if (!username_exists(name)) {
                std::cout << "no name" << std::endl;
                return false;
            }

            if (!is_login_correct(name, newPassword)) {
                std::cout << "login wrong" << std::endl;
                return false;
            }

            return true;
        }
    public:
        Test() : cppcmsskel::models::Users(TEST_PATH TEST_NAME ".db") {

        }

        bool test_add_real_new_user() {
            return test_add_new_user(
                newName,
                newEmail
            );
        }

        bool test_email_taken() {
            return test_add_new_user(
                // just to make sure we use a non-used
                // username
                newName + "emailtaken",
                alreadyTakenEmail
            );
        }

        bool test_username_taken() {
            return test_add_new_user(
                alreadyTakenName,
                // just to make sure newEmail is not taken
                "usernametaken" + newEmail
            );
        }

        bool test_username_email_taken() {
            return test_add_new_user(
                alreadyTakenName,
                alreadyTakenEmail
            );
        }

};
        

int main() {

    bool nothingFailed = true;
    Test model;

    std::cout << SQL_PATH TEST_PATH BASIC_SQL << std::endl;
    CPPCMSSKEL_TEST_RESULT_WORK(
        "Try to create the tables ... " ,
        model.import_sql_file(
          SQL_PATH TEST_PATH BASIC_SQL
        ),
        nothingFailed
    );


    CPPCMSSKEL_TEST_RESULT_WORK(
        "if brand new user => true ... " ,
        model.test_add_real_new_user(),
        nothingFailed
    );

    CPPCMSSKEL_TEST_RESULT_NOT_WORK(
        "If email already taken => false ... " ,
        model.test_email_taken(),
        nothingFailed
    );

    CPPCMSSKEL_TEST_RESULT_NOT_WORK(
        "If username is already used => false ... " ,
        model.test_username_taken(),
        nothingFailed
    );

    CPPCMSSKEL_TEST_RESULT_NOT_WORK(
        "If both username and email are taken => false ... " ,
        model.test_username_email_taken(),
        nothingFailed
    );



    if (nothingFailed) {
        return 0;
    } else {
        return 1;
    }
}

{'fraction_non_alphanumeric': 0.0939540507859734, 'fraction_numerical': 0.0004836759371221282, 'mean_word_length': 1.4778310365488316, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5427021', 'n_tokens_mistral': 2343, 'n_tokens_neox': 2214, 'n_words': 568}