test_pagure_lib_login.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2016 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. Farhaan Bukhsh <farhaan.bukhsh@gmail.com>
  7. """
  8. from __future__ import unicode_literals, absolute_import
  9. import unittest
  10. import shutil
  11. import sys
  12. import os
  13. import hashlib
  14. import six
  15. from mock import patch
  16. sys.path.insert(
  17. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  18. )
  19. import pagure.lib.login
  20. from pagure.exceptions import PagureException
  21. import tests
  22. class PagureLibLogintests(tests.Modeltests):
  23. """Tests for pagure.lib.login"""
  24. def test_id_generator(self):
  25. """Test pagure.lib.login.id_generator."""
  26. self.assertEqual(
  27. pagure.lib.login.id_generator(size=3, chars=["a"]), "aaa"
  28. )
  29. def test_get_session_by_visitkey(self):
  30. """Test pagure.lib.login.get_session_by_visitkey."""
  31. session = pagure.lib.login.get_session_by_visitkey(self.session, "foo")
  32. self.assertEqual(session, None)
  33. def test_generate_hashed_value(self):
  34. """Test pagure.lib.login.generate_hashed_value."""
  35. password = pagure.lib.login.generate_hashed_value("foo")
  36. self.assertTrue(password.startswith("$2$"))
  37. self.assertEqual(len(password), 63)
  38. def test_check_password(self):
  39. """Test pagure.lib.login.check_password."""
  40. # Version 2
  41. password = pagure.lib.login.generate_hashed_value("foo")
  42. self.assertTrue(pagure.lib.login.check_password("foo", password))
  43. self.assertFalse(pagure.lib.login.check_password("bar", password))
  44. # Version 1
  45. password = "%s%s" % (
  46. "foo",
  47. pagure.config.config.get("PASSWORD_SEED", None),
  48. )
  49. if isinstance(password, six.text_type):
  50. password = password.encode("utf-8")
  51. password = "$1$" + hashlib.sha512(password).hexdigest()
  52. password = password.encode("utf-8")
  53. self.assertTrue(pagure.lib.login.check_password("foo", password))
  54. self.assertFalse(pagure.lib.login.check_password("bar", password))
  55. # Invalid password - No version
  56. password = "%s%s" % (
  57. "foo",
  58. pagure.config.config.get("PASSWORD_SEED", None),
  59. )
  60. if isinstance(password, six.text_type):
  61. password = password.encode("utf-8")
  62. password = hashlib.sha512(password).hexdigest()
  63. password = password.encode("utf-8")
  64. self.assertRaises(
  65. PagureException, pagure.lib.login.check_password, "foo", password
  66. )
  67. # Invalid password - Invalid version
  68. password = b"$3$" + password
  69. self.assertRaises(
  70. PagureException, pagure.lib.login.check_password, "foo", password
  71. )
  72. password = "%s%s" % (
  73. "foo",
  74. pagure.config.config.get("PASSWORD_SEED", None),
  75. )
  76. if isinstance(password, six.text_type):
  77. password = password.encode("utf-8")
  78. password = hashlib.sha512(password).hexdigest()
  79. password = password.encode("utf-8")
  80. self.assertRaises(
  81. PagureException, pagure.lib.login.check_password, "foo", password
  82. )
  83. # Invalid password - Invalid version
  84. password = b"$3$" + password
  85. self.assertRaises(
  86. PagureException, pagure.lib.login.check_password, "foo", password
  87. )
  88. def test_unicode_required(self):
  89. """Test to check for non-ascii password"""
  90. self.assertRaises(
  91. ValueError,
  92. pagure.lib.login.generate_hashed_value,
  93. "hunter2".encode("utf-8"),
  94. )
  95. password = pagure.lib.login.generate_hashed_value("foo")
  96. self.assertRaises(
  97. ValueError,
  98. pagure.lib.login.check_password,
  99. "foo".encode("utf-8"),
  100. password,
  101. )
  102. if __name__ == "__main__":
  103. unittest.main(verbosity=2)