Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_AuthEncoding.py 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Copyright (c) 2002, 2015 Zope Foundation and Contributors.
  5. #
  6. # This software is subject to the provisions of the Zope Public License,
  7. # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
  8. # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
  9. # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  10. # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
  11. # FOR A PARTICULAR PURPOSE
  12. #
  13. ##############################################################################
  14. """Test of AuthEncoding
  15. """
  16. from AuthEncoding import AuthEncoding
  17. from ..compat import b, u
  18. import pytest
  19. def testListSchemes():
  20. assert len(AuthEncoding.listSchemes()) > 0 # At least one must exist!
  21. @pytest.mark.parametrize('schema_id', AuthEncoding.listSchemes())
  22. @pytest.mark.parametrize('password', [u'good_pw', u'gööd_pw', b(u'gööd_pw')])
  23. def testGoodPassword(schema_id, password):
  24. enc = AuthEncoding.pw_encrypt(password, schema_id)
  25. assert enc != password
  26. assert AuthEncoding.pw_validate(enc, password)
  27. assert AuthEncoding.pw_validate(u(enc), password)
  28. assert AuthEncoding.is_encrypted(enc)
  29. assert not AuthEncoding.is_encrypted(password)
  30. @pytest.mark.parametrize('schema_id', AuthEncoding.listSchemes())
  31. @pytest.mark.parametrize(
  32. 'password', [u'OK_pa55w0rd \n', u'OK_pä55w0rd \n', b(u'OK_pä55w0rd \n')])
  33. def testBadPassword(schema_id, password):
  34. enc = AuthEncoding.pw_encrypt(password, schema_id)
  35. assert enc != password
  36. assert not AuthEncoding.pw_validate(enc, u'xxx')
  37. assert not AuthEncoding.pw_validate(enc, b'xxx')
  38. assert not AuthEncoding.pw_validate(u(enc), u'xxx')
  39. assert not AuthEncoding.pw_validate(enc, enc)
  40. if schema_id != u'CRYPT':
  41. # crypt truncates passwords and would fail this test.
  42. assert not AuthEncoding.pw_validate(enc, password[:-1])
  43. assert not AuthEncoding.pw_validate(enc, password[1:])
  44. assert AuthEncoding.pw_validate(enc, password)
  45. @pytest.mark.parametrize('schema_id', AuthEncoding.listSchemes())
  46. def testShortPassword(schema_id):
  47. pw = u'1'
  48. enc = AuthEncoding.pw_encrypt(pw, schema_id)
  49. assert AuthEncoding.pw_validate(enc, pw)
  50. assert not AuthEncoding.pw_validate(enc, enc)
  51. assert not AuthEncoding.pw_validate(enc, u'xxx')
  52. @pytest.mark.parametrize('schema_id', AuthEncoding.listSchemes())
  53. def testLongPassword(schema_id):
  54. pw = u'Pw' * 2000
  55. enc = AuthEncoding.pw_encrypt(pw, schema_id)
  56. assert AuthEncoding.pw_validate(enc, pw)
  57. assert not AuthEncoding.pw_validate(enc, enc)
  58. assert not AuthEncoding.pw_validate(enc, u'xxx')
  59. if u'CRYPT' not in schema_id:
  60. # crypt and bcrypt truncates passwords and would fail these tests.
  61. assert not AuthEncoding.pw_validate(enc, pw[:-2])
  62. assert not AuthEncoding.pw_validate(enc, pw[2:])
  63. @pytest.mark.parametrize('schema_id', AuthEncoding.listSchemes())
  64. def testBlankPassword(schema_id):
  65. pw = u''
  66. enc = AuthEncoding.pw_encrypt(pw, schema_id)
  67. assert enc != pw
  68. assert AuthEncoding.pw_validate(enc, pw)
  69. assert not AuthEncoding.pw_validate(enc, enc)
  70. assert not AuthEncoding.pw_validate(enc, u'xxx')
  71. def testUnencryptedPassword():
  72. # Sanity check
  73. pw = u'my-password'
  74. assert AuthEncoding.pw_validate(pw, pw)
  75. assert not AuthEncoding.pw_validate(pw, pw + u'asdf')
  76. def testEncryptWithNotSupportedScheme():
  77. with pytest.raises(ValueError) as err:
  78. AuthEncoding.pw_encrypt(u'asdf', 'MD1')
  79. assert 'Not supported: MD1' == str(err.value)
  80. def testEncryptAcceptsTextAndBinaryEncodingNames():
  81. assert (AuthEncoding.pw_encrypt(u'asdf', b'SHA') ==
  82. AuthEncoding.pw_encrypt(u'asdf', u'SHA'))