|
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 |
|
|
Aurélien Bompard |
626417 |
from __future__ import unicode_literals
|
|
Aurélien Bompard |
626417 |
|
|
Pierre-Yves Chibon |
2e4286 |
__requires__ = ['SQLAlchemy >= 0.8']
|
|
Pierre-Yves Chibon |
2e4286 |
import pkg_resources
|
|
Pierre-Yves Chibon |
2e4286 |
|
|
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 |
2e4286 |
sys.path.insert(0, os.path.join(os.path.dirname(
|
|
Pierre-Yves Chibon |
2e4286 |
os.path.abspath(__file__)), '..'))
|
|
Pierre-Yves Chibon |
2e4286 |
|
|
Pierre-Yves Chibon |
2e4286 |
import pagure.lib
|
|
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 |
2e4286 |
''' Test pagure.lib.login.id_generator. '''
|
|
Pierre-Yves Chibon |
2e4286 |
self.assertEqual(
|
|
Pierre-Yves Chibon |
2e4286 |
pagure.lib.login.id_generator(size=3, chars=['a']),
|
|
Pierre-Yves Chibon |
2e4286 |
'aaa'
|
|
Pierre-Yves Chibon |
2e4286 |
)
|
|
Pierre-Yves Chibon |
2e4286 |
|
|
Pierre-Yves Chibon |
2e4286 |
def test_get_session_by_visitkey(self):
|
|
Pierre-Yves Chibon |
2e4286 |
''' Test pagure.lib.login.get_session_by_visitkey. '''
|
|
Pierre-Yves Chibon |
2e4286 |
|
|
Pierre-Yves Chibon |
2e4286 |
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 |
3e58b0 |
''' Test pagure.lib.login.generate_hashed_value. '''
|
|
Aurélien Bompard |
626417 |
password = pagure.lib.login.generate_hashed_value('foo')
|
|
Pierre-Yves Chibon |
29b1ab |
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 |
3e58b0 |
''' Test pagure.lib.login.check_password. '''
|
|
Pierre-Yves Chibon |
3e58b0 |
|
|
Pierre-Yves Chibon |
a1412f |
# Version 2
|
|
Aurélien Bompard |
626417 |
password = pagure.lib.login.generate_hashed_value('foo')
|
|
Pierre-Yves Chibon |
3e58b0 |
self.assertTrue(
|
|
Aurélien Bompard |
626417 |
pagure.lib.login.check_password('foo', password))
|
|
Pierre-Yves Chibon |
a1412f |
self.assertFalse(
|
|
Aurélien Bompard |
626417 |
pagure.lib.login.check_password('bar', password))
|
|
Pierre-Yves Chibon |
a1412f |
|
|
Pierre-Yves Chibon |
a1412f |
# Version 1
|
|
Aurélien Bompard |
626417 |
password = '%s%s' % ('foo', pagure.config.config.get('PASSWORD_SEED', None))
|
|
Aurélien Bompard |
619e2a |
if isinstance(password, six.text_type):
|
|
Aurélien Bompard |
626417 |
password = password.encode('utf-8')
|
|
farhaanbukhsh |
355681 |
password = '$1$' + hashlib.sha512(password).hexdigest()
|
|
Aurélien Bompard |
626417 |
password = password.encode("utf-8")
|
|
Aurélien Bompard |
626417 |
self.assertTrue(pagure.lib.login.check_password('foo', password))
|
|
Aurélien Bompard |
626417 |
self.assertFalse(pagure.lib.login.check_password('bar', password))
|
|
Pierre-Yves Chibon |
a1412f |
|
|
Pierre-Yves Chibon |
a1412f |
# Invalid password - No version
|
|
Aurélien Bompard |
626417 |
password = '%s%s' % ('foo', pagure.config.config.get('PASSWORD_SEED', None))
|
|
Aurélien Bompard |
619e2a |
if isinstance(password, six.text_type):
|
|
Aurélien Bompard |
626417 |
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 |
a1412f |
PagureException,
|
|
Pierre-Yves Chibon |
a1412f |
pagure.lib.login.check_password,
|
|
Aurélien Bompard |
626417 |
'foo', password
|
|
Pierre-Yves Chibon |
a1412f |
)
|
|
Pierre-Yves Chibon |
a1412f |
|
|
Pierre-Yves Chibon |
a1412f |
# Invalid password - Invalid version
|
|
Aurélien Bompard |
626417 |
password = b'$3$' + password
|
|
Pierre-Yves Chibon |
a1412f |
self.assertRaises(
|
|
Pierre-Yves Chibon |
a1412f |
PagureException,
|
|
Pierre-Yves Chibon |
a1412f |
pagure.lib.login.check_password,
|
|
Aurélien Bompard |
626417 |
'foo',
|
|
Pierre-Yves Chibon |
a1412f |
password
|
|
Pierre-Yves Chibon |
a1412f |
)
|
|
Aurélien Bompard |
626417 |
password = '%s%s' % ('foo', pagure.config.config.get('PASSWORD_SEED', None))
|
|
Aurélien Bompard |
619e2a |
if isinstance(password, six.text_type):
|
|
Aurélien Bompard |
626417 |
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 |
375407 |
PagureException,
|
|
Pierre-Yves Chibon |
375407 |
pagure.lib.login.check_password,
|
|
Aurélien Bompard |
626417 |
'foo', password
|
|
Pierre-Yves Chibon |
375407 |
)
|
|
Pierre-Yves Chibon |
375407 |
|
|
Pierre-Yves Chibon |
375407 |
# Invalid password - Invalid version
|
|
Aurélien Bompard |
626417 |
password = b'$3$' + password
|
|
Pierre-Yves Chibon |
375407 |
self.assertRaises(
|
|
Pierre-Yves Chibon |
375407 |
PagureException,
|
|
Pierre-Yves Chibon |
375407 |
pagure.lib.login.check_password,
|
|
Aurélien Bompard |
626417 |
'foo',
|
|
Pierre-Yves Chibon |
375407 |
password
|
|
Pierre-Yves Chibon |
375407 |
)
|
|
Pierre-Yves Chibon |
2e4286 |
|
|
Farhaan Bukhsh |
9d34ee |
def test_unicode_required(self):
|
|
Farhaan Bukhsh |
9d34ee |
''' Test to check for non-ascii password
|
|
Farhaan Bukhsh |
9d34ee |
'''
|
|
Farhaan Bukhsh |
9d34ee |
self.assertRaises(
|
|
Farhaan Bukhsh |
9d34ee |
ValueError,
|
|
Farhaan Bukhsh |
9d34ee |
pagure.lib.login.generate_hashed_value,
|
|
Aurélien Bompard |
626417 |
'hunter2'.encode('utf-8')
|
|
Farhaan Bukhsh |
9d34ee |
)
|
|
Aurélien Bompard |
626417 |
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,
|
|
Aurélien Bompard |
626417 |
'foo'.encode('utf-8'),
|
|
Farhaan Bukhsh |
9d34ee |
password
|
|
Farhaan Bukhsh |
9d34ee |
)
|
|
Farhaan Bukhsh |
9d34ee |
|
|
Farhaan Bukhsh |
9d34ee |
|
|
Pierre-Yves Chibon |
2e4286 |
if __name__ == '__main__':
|
|
Pierre-Yves Chibon |
393f31 |
unittest.main(verbosity=2)
|