Blame tests/test_pagure_lib_login.py

Pierre-Yves Chibon 2e4286
# -*- coding: utf-8 -*-
Pierre-Yves Chibon 2e4286
Pierre-Yves Chibon 2e4286
"""
Pierre-Yves Chibon 2e4286
 (c) 2016 - Copyright Red Hat Inc
Pierre-Yves Chibon 2e4286
Pierre-Yves Chibon 2e4286
 Authors:
Pierre-Yves Chibon 2e4286
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
farhaanbukhsh 96d198
   Farhaan Bukhsh <farhaan.bukhsh@gmail.com></farhaan.bukhsh@gmail.com>
Pierre-Yves Chibon 2e4286
Pierre-Yves Chibon 2e4286
"""
Pierre-Yves Chibon 2e4286
Pierre-Yves Chibon 67d1cc
from __future__ import unicode_literals, absolute_import
Aurélien Bompard 626417
Pierre-Yves Chibon 2e4286
import unittest
Pierre-Yves Chibon 2e4286
import shutil
Pierre-Yves Chibon 2e4286
import sys
Pierre-Yves Chibon 2e4286
import os
Pierre-Yves Chibon 2e4286
Aurélien Bompard 626417
import hashlib
Aurélien Bompard 626417
import six
Pierre-Yves Chibon 2e4286
from mock import patch
Pierre-Yves Chibon 2e4286
Pierre-Yves Chibon 73d120
sys.path.insert(
Pierre-Yves Chibon 73d120
    0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
Pierre-Yves Chibon 73d120
)
Pierre-Yves Chibon 2e4286
Pierre-Yves Chibon 2e4286
import pagure.lib.login
Aurélien Bompard 626417
from pagure.exceptions import PagureException
Pierre-Yves Chibon 2e4286
import tests
Pierre-Yves Chibon 2e4286
Pierre-Yves Chibon 2e4286
Pierre-Yves Chibon 2e4286
class PagureLibLogintests(tests.Modeltests):
Pierre-Yves Chibon 2e4286
    """ Tests for pagure.lib.login """
Pierre-Yves Chibon 2e4286
Pierre-Yves Chibon 2e4286
    def test_id_generator(self):
Pierre-Yves Chibon 73d120
        """ Test pagure.lib.login.id_generator. """
Pierre-Yves Chibon 2e4286
        self.assertEqual(
Pierre-Yves Chibon 73d120
            pagure.lib.login.id_generator(size=3, chars=["a"]), "aaa"
Pierre-Yves Chibon 2e4286
        )
Pierre-Yves Chibon 2e4286
Pierre-Yves Chibon 2e4286
    def test_get_session_by_visitkey(self):
Pierre-Yves Chibon 73d120
        """ Test pagure.lib.login.get_session_by_visitkey. """
Pierre-Yves Chibon 2e4286
Pierre-Yves Chibon 73d120
        session = pagure.lib.login.get_session_by_visitkey(self.session, "foo")
Pierre-Yves Chibon 2e4286
        self.assertEqual(session, None)
Pierre-Yves Chibon 2e4286
Pierre-Yves Chibon 3e58b0
    def test_generate_hashed_value(self):
Pierre-Yves Chibon 73d120
        """ Test pagure.lib.login.generate_hashed_value. """
Pierre-Yves Chibon 73d120
        password = pagure.lib.login.generate_hashed_value("foo")
Pierre-Yves Chibon 73d120
        self.assertTrue(password.startswith("$2$"))
Pierre-Yves Chibon 3e58b0
        self.assertEqual(len(password), 63)
Pierre-Yves Chibon 3e58b0
Pierre-Yves Chibon 3e58b0
    def test_check_password(self):
Pierre-Yves Chibon 73d120
        """ Test pagure.lib.login.check_password. """
Pierre-Yves Chibon 3e58b0
Pierre-Yves Chibon a1412f
        # Version 2
Pierre-Yves Chibon 73d120
        password = pagure.lib.login.generate_hashed_value("foo")
Pierre-Yves Chibon 73d120
        self.assertTrue(pagure.lib.login.check_password("foo", password))
Pierre-Yves Chibon 73d120
        self.assertFalse(pagure.lib.login.check_password("bar", password))
Pierre-Yves Chibon a1412f
Pierre-Yves Chibon a1412f
        # Version 1
Pierre-Yves Chibon 73d120
        password = "%s%s" % (
Pierre-Yves Chibon 73d120
            "foo",
Pierre-Yves Chibon 73d120
            pagure.config.config.get("PASSWORD_SEED", None),
Pierre-Yves Chibon 73d120
        )
Aurélien Bompard 619e2a
        if isinstance(password, six.text_type):
Pierre-Yves Chibon 73d120
            password = password.encode("utf-8")
Pierre-Yves Chibon 73d120
        password = "$1$" + hashlib.sha512(password).hexdigest()
Aurélien Bompard 626417
        password = password.encode("utf-8")
Pierre-Yves Chibon 73d120
        self.assertTrue(pagure.lib.login.check_password("foo", password))
Pierre-Yves Chibon 73d120
        self.assertFalse(pagure.lib.login.check_password("bar", password))
Pierre-Yves Chibon a1412f
Pierre-Yves Chibon a1412f
        # Invalid password  -  No version
Pierre-Yves Chibon 73d120
        password = "%s%s" % (
Pierre-Yves Chibon 73d120
            "foo",
Pierre-Yves Chibon 73d120
            pagure.config.config.get("PASSWORD_SEED", None),
Pierre-Yves Chibon 73d120
        )
Aurélien Bompard 619e2a
        if isinstance(password, six.text_type):
Pierre-Yves Chibon 73d120
            password = password.encode("utf-8")
farhaanbukhsh 355681
        password = hashlib.sha512(password).hexdigest()
Aurélien Bompard 626417
        password = password.encode("utf-8")
Pierre-Yves Chibon a1412f
        self.assertRaises(
Pierre-Yves Chibon 73d120
            PagureException, pagure.lib.login.check_password, "foo", password
Pierre-Yves Chibon a1412f
        )
Pierre-Yves Chibon a1412f
Pierre-Yves Chibon a1412f
        # Invalid password  -  Invalid version
Pierre-Yves Chibon 73d120
        password = b"$3$" + password
Pierre-Yves Chibon a1412f
        self.assertRaises(
Pierre-Yves Chibon 73d120
            PagureException, pagure.lib.login.check_password, "foo", password
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        password = "%s%s" % (
Pierre-Yves Chibon 73d120
            "foo",
Pierre-Yves Chibon 73d120
            pagure.config.config.get("PASSWORD_SEED", None),
Pierre-Yves Chibon a1412f
        )
Aurélien Bompard 619e2a
        if isinstance(password, six.text_type):
Pierre-Yves Chibon 73d120
            password = password.encode("utf-8")
farhaanbukhsh 0fc6df
        password = hashlib.sha512(password).hexdigest()
Aurélien Bompard 626417
        password = password.encode("utf-8")
Pierre-Yves Chibon 375407
        self.assertRaises(
Pierre-Yves Chibon 73d120
            PagureException, pagure.lib.login.check_password, "foo", password
Pierre-Yves Chibon 375407
        )
Pierre-Yves Chibon 375407
Pierre-Yves Chibon 375407
        # Invalid password  -  Invalid version
Pierre-Yves Chibon 73d120
        password = b"$3$" + password
Pierre-Yves Chibon 375407
        self.assertRaises(
Pierre-Yves Chibon 73d120
            PagureException, pagure.lib.login.check_password, "foo", password
Pierre-Yves Chibon 375407
        )
Pierre-Yves Chibon 2e4286
Farhaan Bukhsh 9d34ee
    def test_unicode_required(self):
Pierre-Yves Chibon 73d120
        """ Test to check for non-ascii password
Pierre-Yves Chibon 73d120
        """
Farhaan Bukhsh 9d34ee
        self.assertRaises(
Farhaan Bukhsh 9d34ee
            ValueError,
Farhaan Bukhsh 9d34ee
            pagure.lib.login.generate_hashed_value,
Pierre-Yves Chibon 73d120
            "hunter2".encode("utf-8"),
Farhaan Bukhsh 9d34ee
        )
Pierre-Yves Chibon 73d120
        password = pagure.lib.login.generate_hashed_value("foo")
Farhaan Bukhsh 9d34ee
        self.assertRaises(
Farhaan Bukhsh 9d34ee
            ValueError,
Farhaan Bukhsh 9d34ee
            pagure.lib.login.check_password,
Pierre-Yves Chibon 73d120
            "foo".encode("utf-8"),
Pierre-Yves Chibon 73d120
            password,
Farhaan Bukhsh 9d34ee
        )
Farhaan Bukhsh 9d34ee
Farhaan Bukhsh 9d34ee
Pierre-Yves Chibon 73d120
if __name__ == "__main__":
Pierre-Yves Chibon 393f31
    unittest.main(verbosity=2)