diff options
| author | Simeon Simeonov | 2021-12-23 14:41:14 +0100 |
|---|---|---|
| committer | Simeon Simeonov | 2021-12-23 14:41:14 +0100 |
| commit | 4914b0c74ed669d6631897950ff1660ce91ef918 (patch) | |
| tree | 5bef79607256853f41fb3bb666e1c16604a7b258 /tests/test_envtoolkit_instance_static.py | |
Initial commit and release for Christmas with love1.0.0
Diffstat (limited to 'tests/test_envtoolkit_instance_static.py')
| -rw-r--r-- | tests/test_envtoolkit_instance_static.py | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/tests/test_envtoolkit_instance_static.py b/tests/test_envtoolkit_instance_static.py new file mode 100644 index 0000000..d7ac857 --- /dev/null +++ b/tests/test_envtoolkit_instance_static.py | |||
| @@ -0,0 +1,102 @@ | |||
| 1 | # etoolkit | ||
| 2 | # Copyright (C) 2021 Simeon Simeonov | ||
| 3 | |||
| 4 | # This program is free software: you can redistribute it and/or modify | ||
| 5 | # it under the terms of the GNU General Public License as published by | ||
| 6 | # the Free Software Foundation, either version 3 of the License, or | ||
| 7 | # (at your option) any later version. | ||
| 8 | |||
| 9 | # This program is distributed in the hope that it will be useful, | ||
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 12 | # GNU General Public License for more details. | ||
| 13 | |||
| 14 | # You should have received a copy of the GNU General Public License | ||
| 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 16 | """Tests for etoolkit.EtoolkitInstance static methods""" | ||
| 17 | import unittest.mock | ||
| 18 | |||
| 19 | import pytest | ||
| 20 | |||
| 21 | import etoolkit | ||
| 22 | |||
| 23 | |||
| 24 | @unittest.mock.patch('getpass.getpass') | ||
| 25 | def test_confirm_password_prompt(getpass, password_hash): | ||
| 26 | """Tests the static EtoolkitInstance.confirm_password_prompt method""" | ||
| 27 | getpass.return_value = 'the very secret passwd' | ||
| 28 | assert ( | ||
| 29 | etoolkit.EtoolkitInstance.confirm_password_prompt(password_hash) | ||
| 30 | == 'the very secret passwd' | ||
| 31 | ) | ||
| 32 | assert ( | ||
| 33 | etoolkit.EtoolkitInstance.confirm_password_prompt(password_hash, False) | ||
| 34 | == 'the very secret passwd' | ||
| 35 | ) | ||
| 36 | |||
| 37 | |||
| 38 | def test_decrypt(): | ||
| 39 | """Tests the static EtoolkitInstance.decrypt method""" | ||
| 40 | assert ( | ||
| 41 | etoolkit.EtoolkitInstance.decrypt( | ||
| 42 | 'the very secret passwd', | ||
| 43 | ( | ||
| 44 | 'enc-val$1$Y/TBb1F3siHTw6qZg9ERzZfA8PLPf2CwGSQLpu9jYWw=$FT5tS9' | ||
| 45 | 'o+ABvsxogIXpJim16Gz5SVtV8=' | ||
| 46 | ), | ||
| 47 | ) | ||
| 48 | == 'secret1' | ||
| 49 | ) | ||
| 50 | |||
| 51 | # now test with modified edata | ||
| 52 | with pytest.raises(etoolkit.EtoolkitInstanceError) as exc_info: | ||
| 53 | edata = ( | ||
| 54 | 'enc-val$1$Y/TBb1F3siHTw6qZg9ERzZfA8PLPf2CwGSQLpu9jYWw=$FT5tS9' | ||
| 55 | 'o+ABvsxogIXpJim17Gz5SVtV8=' | ||
| 56 | ) | ||
| 57 | etoolkit.EtoolkitInstance.decrypt( | ||
| 58 | 'the very secret passwd', edata | ||
| 59 | ) == 'secret1' | ||
| 60 | assert exc_info.type is etoolkit.EtoolkitInstanceError | ||
| 61 | assert exc_info.value.args[0] == f'Invalid tag when decrypting: {edata}' | ||
| 62 | |||
| 63 | |||
| 64 | def test_encrypt(): | ||
| 65 | """Tests the static EtoolkitInstance.encrypt method""" | ||
| 66 | edata = etoolkit.EtoolkitInstance.encrypt('foo', 'bar') | ||
| 67 | assert edata.startswith('enc-val$') | ||
| 68 | assert len(edata) == 83 | ||
| 69 | # the edata should always be different because of random salting | ||
| 70 | assert edata != etoolkit.EtoolkitInstance.encrypt('foo', 'bar') | ||
| 71 | |||
| 72 | |||
| 73 | def test_get_new_password_hash(): | ||
| 74 | """Tests the static EtoolkitInstance.get_new_password_hash method""" | ||
| 75 | new_hash = etoolkit.EtoolkitInstance.get_new_password_hash( | ||
| 76 | 'the very secret passwd' | ||
| 77 | ) | ||
| 78 | # all pbkdf2 params are the same / hardcoded for the time being | ||
| 79 | assert new_hash.startswith('pbkdf2_sha256$100000$') | ||
| 80 | assert len(new_hash) == 110 | ||
| 81 | # the hash should always be different because of random salting | ||
| 82 | assert new_hash != etoolkit.EtoolkitInstance.get_new_password_hash( | ||
| 83 | 'the very secret passwd' | ||
| 84 | ) | ||
| 85 | |||
| 86 | |||
| 87 | def test_parse_value(): | ||
| 88 | """Tests the static EtoolkitInstance.parse_value method""" | ||
| 89 | assert ( | ||
| 90 | etoolkit.EtoolkitInstance.parse_value('t%bs%t', {'%b': 'e', '%t': 't'}) | ||
| 91 | == 'test' | ||
| 92 | ) | ||
| 93 | |||
| 94 | |||
| 95 | def test_password_matches(password_hash): | ||
| 96 | """Tests the static EtoolkitInstance.password_matches method""" | ||
| 97 | assert etoolkit.EtoolkitInstance.password_matches( | ||
| 98 | 'the very secret passwd', password_hash | ||
| 99 | ) | ||
| 100 | assert not etoolkit.EtoolkitInstance.password_matches( | ||
| 101 | 'the very secret passwdo', password_hash | ||
| 102 | ) | ||
