<commit_before>#!/usr/bin/env python
#-*- coding:utf-8 -*-

import unittest
from lib import euemongo
from lib import eueuser

"""
Unit tests for user class module
"""


class TestUser(unittest.TestCase):

    host = "localhost"
    port = 27017
    database = "eue"
    collection = "users"

    mongo = None

    def setUp(self):
        self.mongo = euemongo.mongo(
            host=self.host,
            port=self.port,
            database=self.database)
        self.mongo.connect()
        self.user = eueuser.user(mongo=self.mongo, collection=self.collection)

    def tearDown(self):
        """ unset user instance """
        self.user = None
        """ cleanup everything for next test """
        self.mongo.db.drop_collection(self.collection)
        """ disconnect from mongo """
        self.mongo.disconnect()
        """ unset mongo instance """
        self.mongo = None

    """ test user creation """
    def test001_createUser(self):
        """
            > try to create a user with blank user and password : should fail
            > try to create a user with a blank password : should fail
            > try to create a user with a blank email : should fail
            > try to create a user with user and password : should success
            > try to create a user with email allready in database should fail
        """
        cases = [
            {"email": "",
                "password": "",
                "firstname": "",
                "lastname": "",
                "assert": False},
            {"email": "<EMAIL>",
                "password": "",
                "firstname": "",
                "lastname": "",
                "assert": False},
            {"email": "",
                "password": "<PASSWORD>",
                "firstname": "",
                "lastname": "",
                "assert": False},
            {"email": "<EMAIL>",
                "password": "<PASSWORD>",
                "firstname": "",
                "lastname": "",
                "assert": "notempty"},
            {"email": "<EMAIL>",
                "password": "<PASSWORD>",
                "firstname": "",
                "lastname": "",
                "assert": False}]

        for case in cases:
            if not case["assert"]:
                assert not self.user.new(case)
            if case["assert"]:
                assert self.user.new(case)
            if case["assert"] is "notempty":
                assert self.user.new(case) != ""

    """ test get user """
    def test002_getUser(self):
        id = self.user.new({
            "email": "<EMAIL>",
            "password": "<PASSWORD>"})
        assert id is not False
        result = self.user.get("<EMAIL>")
        assert result is not False
        assert result['_id'] == id
        assert result['email'] == '<EMAIL>'
        assert result['password'] == '<PASSWORD>'

    """ test user update """
    def test003_updateUser(self):
        """ create a user """
        assert self.user.new({
            "email": "<EMAIL>",
            "password": "<PASSWORD>",
            "firstname": "Eric",
            "lastname": "SORIANO"})
        """ update the password / firstname / lastname """
        assert self.user.update({
            "email": "<EMAIL>",
            "password": "<PASSWORD>",
            "firstname": "David",
            "lastname": "GUENAULT"})
        """ verify that everything is really updated """
        usr = self.user.get("<EMAIL>")
        assert usr["email"] == "<EMAIL>"<commit_msg>Added more granularity in user creation tests<commit_after>#!/usr/bin/env python
#-*- coding:utf-8 -*-

import unittest
from lib import euemongo
from lib import eueuser

"""
Unit tests for user class module
"""


class TestUser(unittest.TestCase):

    host = "localhost"
    port = 27017
    database = "eue"
    collection = "users"

    mongo = None

    def setUp(self):
        self.mongo = euemongo.mongo(
            host=self.host,
            port=self.port,
            database=self.database)
        self.mongo.connect()
        self.user = eueuser.user(mongo=self.mongo, collection=self.collection)

    def tearDown(self):
        """ unset user instance """
        self.user = None
        """ cleanup everything for next test """
        self.mongo.db.drop_collection(self.collection)
        """ disconnect from mongo """
        self.mongo.disconnect()
        """ unset mongo instance """
        self.mongo = None

    """ test user creation """
    def test001_createUser(self):
        """
            > try to create a user with blank user and password : should fail
            > try to create a user with a blank password : should fail
            > try to create a user with a blank email : should fail
            > try to create a user with user and password : should success
            > try to create a user with email allready in database should fail
        """
        user = {"email": "",
                "password": "",
                "firstname": "",
                "lastname": ""}
        assert not self.user.new(case)
        user = {"email": "<EMAIL>",
                "password": "",
                "firstname": "",
                "lastname": ""}
        assert not self.user.new(case)
        user = {"email": "",
                "password": "<PASSWORD>",
                "firstname": "",
                "lastname": ""}
        assert not self.user.new(case)
        user = {"email": "<EMAIL>",
                "password": "<PASSWORD>",
                "firstname": "",
                "lastname": ""}
        assert self.user.new(case) != ""
        user = {"email": "<EMAIL>",
                "password": "<PASSWORD>",
                "firstname": "",
                "lastname": ""}
        assert not self.user.new(case) != ""

    """ test non admin user creation """
    def test002_createAdminUser(self):
        """ default should be a non admin user """
        user = self.user.getUserStructure()
        user.email = "<EMAIL>"
        user.password = "<PASSWORD>"
        user.firstname = "David"
        user.lastname = "GUENAULT"

        """ user must return non empty """
        result = self.user.new(user)
        assert result != ""

        """ get created user """
        u = self.user.get(user.email)

        """ there should be an acl key and in acl """
        """ there should be an isAdmin key """
        """ user should not be admin by default """

        assert "acl" in u
        assert u["acl"] is dict
        assert "isAdmin" in u["acl"]
        assert not u["acl"]["isAdmin"]

    """ test get user """
    def test002_getUser(self):
        id = self.user.new({
            "email": "<EMAIL>",
            "password": "<PASSWORD>"})
        assert id is not False
        result = self.user.get("<EMAIL>")
        assert result is not False
        assert result['_id'] == id
        assert result['email'] == '<EMAIL>'
        assert result['password'] == '<PASSWORD>'

    """ test user update """
    def test003_updateUser(self):
        """ create a user """
        assert self.user.new({
            "email": "<EMAIL>",
            "password": "<PASSWORD>",
            "firstname": "Eric",
            "lastname": "SORIANO"})
        """ update the password / firstname / lastname """
        assert self.user.update({
            "email": "<EMAIL>",
            "password": "<PASSWORD>",
            "firstname": "David",
            "lastname": "GUENAULT"})
        """ verify that everything is really updated """
        usr = self.user.get("<EMAIL>")
        assert usr["email"] == "<EMAIL>"
{'fraction_non_alphanumeric': 0.14446922975987403, 'fraction_numerical': 0.00433014040152211, 'mean_word_length': 1.711490572749911, 'pattern_counts': {'":': 66, '<': 39, '<?xml version=': 0, '>': 49, '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': '5480280', 'n_tokens_mistral': 2093, 'n_tokens_neox': 2005, 'n_words': 586}