diff options
| author | Simeon Simeonov | 2023-01-08 23:39:45 +0100 |
|---|---|---|
| committer | Simeon Simeonov | 2023-01-08 23:39:45 +0100 |
| commit | e9f45a50dd3f92a4082bec6dc33c4aa0f919138a (patch) | |
| tree | dcfc89aa5aacaaf97072da9d0d0517c3b367176b /test/test_main.py | |
| parent | b8fa40b48feb8709d27b1c50f1ea6be2ce081692 (diff) | |
Include the tests in the sdist packagev1.2.1
Diffstat (limited to 'test/test_main.py')
| -rw-r--r-- | test/test_main.py | 265 |
1 files changed, 265 insertions, 0 deletions
diff --git a/test/test_main.py b/test/test_main.py new file mode 100644 index 0000000..35bd2a2 --- /dev/null +++ b/test/test_main.py | |||
| @@ -0,0 +1,265 @@ | |||
| 1 | # -*- coding: utf-8 -*- | ||
| 2 | # SPDX-License-Identifier: BSD-2-Clause-FreeBSD | ||
| 3 | # | ||
| 4 | # Copyright (c) 2020-2023, Simeon Simeonov | ||
| 5 | # All rights reserved. | ||
| 6 | # | ||
| 7 | # Redistribution and use in source and binary forms, with or without | ||
| 8 | # modification, are permitted provided that the following conditions | ||
| 9 | # are met: | ||
| 10 | # 1. Redistributions of source code must retain the above copyright | ||
| 11 | # notice, this list of conditions and the following disclaimer. | ||
| 12 | # 2. Redistributions in binary form must reproduce the above copyright notice, | ||
| 13 | # this list of conditions and the following disclaimer in the documentation | ||
| 14 | # and/or other materials provided with the distribution. | ||
| 15 | # | ||
| 16 | # THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR | ||
| 17 | # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | ||
| 18 | # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | ||
| 19 | # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
| 20 | # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
| 21 | # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
| 22 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
| 23 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| 24 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | ||
| 25 | # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 26 | """Tests for otp2289.__main__""" | ||
| 27 | import os | ||
| 28 | import unittest.mock | ||
| 29 | |||
| 30 | import pytest | ||
| 31 | |||
| 32 | from otp2289.__main__ import main | ||
| 33 | |||
| 34 | |||
| 35 | def test_main_generate_otp_response(capsys): | ||
| 36 | """tests main""" | ||
| 37 | args = [ | ||
| 38 | '--generate-otp-response', | ||
| 39 | '-a', | ||
| 40 | 'sha1', | ||
| 41 | '-i', | ||
| 42 | '99', | ||
| 43 | '-s', | ||
| 44 | 'TesT', | ||
| 45 | '-p', | ||
| 46 | 'This is a test.', | ||
| 47 | ] | ||
| 48 | with pytest.raises(SystemExit) as exit_info: | ||
| 49 | main(args) | ||
| 50 | captured = capsys.readouterr() | ||
| 51 | assert captured.out == ( | ||
| 52 | f'Seed: TesT, Step: 99, Hash: sha1{os.linesep}' | ||
| 53 | f'0x87fec7768b73ccf9{os.linesep}' | ||
| 54 | ) | ||
| 55 | assert exit_info.type == SystemExit | ||
| 56 | assert exit_info.value.code == 0 | ||
| 57 | args.extend(['-f', 'token']) | ||
| 58 | with pytest.raises(SystemExit) as exit_info: | ||
| 59 | main(args) | ||
| 60 | captured = capsys.readouterr() | ||
| 61 | assert captured.out == ( | ||
| 62 | f'Seed: TesT, Step: 99, Hash: sha1{os.linesep}' | ||
| 63 | f'GAFF WAIT SKID GIG SKY EYED{os.linesep}' | ||
| 64 | ) | ||
| 65 | assert exit_info.type == SystemExit | ||
| 66 | assert exit_info.value.code == 0 | ||
| 67 | args.append('-q') | ||
| 68 | with pytest.raises(SystemExit) as exit_info: | ||
| 69 | main(args) | ||
| 70 | captured = capsys.readouterr() | ||
| 71 | assert captured.out == f'GAFF WAIT SKID GIG SKY EYED{os.linesep}' | ||
| 72 | assert exit_info.type == SystemExit | ||
| 73 | assert exit_info.value.code == 0 | ||
| 74 | |||
| 75 | |||
| 76 | def test_main_generate_otp_response_env_passwd(capsys): | ||
| 77 | """tests main by fetching password from the env. var. 'OTP2289_PASSWORD'""" | ||
| 78 | args = [ | ||
| 79 | '--generate-otp-response', | ||
| 80 | '-a', | ||
| 81 | 'sha1', | ||
| 82 | '-i', | ||
| 83 | '99', | ||
| 84 | '-s', | ||
| 85 | 'TesT', | ||
| 86 | ] | ||
| 87 | with unittest.mock.patch.dict( | ||
| 88 | os.environ, {'OTP2289_PASSWORD': 'This is a test.'} | ||
| 89 | ): | ||
| 90 | with pytest.raises(SystemExit) as exit_info: | ||
| 91 | main(args) | ||
| 92 | captured = capsys.readouterr() | ||
| 93 | assert captured.out == ( | ||
| 94 | f'Seed: TesT, Step: 99, Hash: sha1{os.linesep}' | ||
| 95 | f'0x87fec7768b73ccf9{os.linesep}' | ||
| 96 | ) | ||
| 97 | assert exit_info.type == SystemExit | ||
| 98 | assert exit_info.value.code == 0 | ||
| 99 | args.extend(['-f', 'token']) | ||
| 100 | with pytest.raises(SystemExit) as exit_info: | ||
| 101 | main(args) | ||
| 102 | captured = capsys.readouterr() | ||
| 103 | assert captured.out == ( | ||
| 104 | f'Seed: TesT, Step: 99, Hash: sha1{os.linesep}' | ||
| 105 | f'GAFF WAIT SKID GIG SKY EYED{os.linesep}' | ||
| 106 | ) | ||
| 107 | assert exit_info.type == SystemExit | ||
| 108 | assert exit_info.value.code == 0 | ||
| 109 | args.append('-q') | ||
| 110 | with pytest.raises(SystemExit) as exit_info: | ||
| 111 | main(args) | ||
| 112 | captured = capsys.readouterr() | ||
| 113 | assert captured.out == f'GAFF WAIT SKID GIG SKY EYED{os.linesep}' | ||
| 114 | assert exit_info.type == SystemExit | ||
| 115 | assert exit_info.value.code == 0 | ||
| 116 | |||
| 117 | |||
| 118 | def test_main_generate_otp_range(capsys): | ||
| 119 | """tests main""" | ||
| 120 | args = [ | ||
| 121 | '--generate-otp-range', | ||
| 122 | '-i', | ||
| 123 | '2', | ||
| 124 | '-s', | ||
| 125 | 'TesT', | ||
| 126 | '-r', | ||
| 127 | '5', | ||
| 128 | '-p', | ||
| 129 | 'This is a test.', | ||
| 130 | ] | ||
| 131 | with pytest.raises(SystemExit) as exit_info: | ||
| 132 | main(args) | ||
| 133 | captured = capsys.readouterr() | ||
| 134 | assert captured.out == ( | ||
| 135 | f'Seed: TesT, Step: 2, Hash: md5, Range: 3' | ||
| 136 | f'{os.linesep}' | ||
| 137 | f'2: 0x4049f8b161669b7b{os.linesep}' | ||
| 138 | f'1: 0x7965e05436f5029f{os.linesep}' | ||
| 139 | f'0: 0x9e876134d90499dd{os.linesep}' | ||
| 140 | ) | ||
| 141 | assert exit_info.type == SystemExit | ||
| 142 | assert exit_info.value.code == 0 | ||
| 143 | args.append('-q') | ||
| 144 | with pytest.raises(SystemExit) as exit_info: | ||
| 145 | main(args) | ||
| 146 | captured = capsys.readouterr() | ||
| 147 | assert captured.out == ( | ||
| 148 | f'2: 0x4049f8b161669b7b{os.linesep}' | ||
| 149 | f'1: 0x7965e05436f5029f{os.linesep}' | ||
| 150 | f'0: 0x9e876134d90499dd{os.linesep}' | ||
| 151 | ) | ||
| 152 | assert exit_info.type == SystemExit | ||
| 153 | assert exit_info.value.code == 0 | ||
| 154 | args.extend(['-f', 'token']) | ||
| 155 | with pytest.raises(SystemExit) as exit_info: | ||
| 156 | main(args) | ||
| 157 | captured = capsys.readouterr() | ||
| 158 | assert captured.out == ( | ||
| 159 | f'2: THY AVON NO NECK COKE MOLL{os.linesep}' | ||
| 160 | f'1: EASE OIL FUM CURE AWRY AVIS{os.linesep}' | ||
| 161 | f'0: INCH SEA ANNE LONG AHEM TOUR{os.linesep}' | ||
| 162 | ) | ||
| 163 | assert exit_info.type == SystemExit | ||
| 164 | assert exit_info.value.code == 0 | ||
| 165 | |||
| 166 | |||
| 167 | @pytest.mark.parametrize( | ||
| 168 | 'args', | ||
| 169 | [ | ||
| 170 | [ | ||
| 171 | '--generate-otp-range', | ||
| 172 | '-i', | ||
| 173 | '2', | ||
| 174 | '-s', | ||
| 175 | 'TesT', | ||
| 176 | '-r', | ||
| 177 | '5', | ||
| 178 | ], | ||
| 179 | [ | ||
| 180 | '--generate-otp-range', | ||
| 181 | '-i', | ||
| 182 | '2', | ||
| 183 | '-s', | ||
| 184 | 'TesT', | ||
| 185 | '-r', | ||
| 186 | '5', | ||
| 187 | '-P', | ||
| 188 | ], | ||
| 189 | ], | ||
| 190 | ) | ||
| 191 | @unittest.mock.patch('getpass.getpass', lambda *args: 'This is a test.') | ||
| 192 | def test_main_generate_otp_range_passwd_prompt(capsys, args): | ||
| 193 | """tests main by prompting for password (with or without -P)""" | ||
| 194 | args = [ | ||
| 195 | '--generate-otp-range', | ||
| 196 | '-i', | ||
| 197 | '2', | ||
| 198 | '-s', | ||
| 199 | 'TesT', | ||
| 200 | '-r', | ||
| 201 | '5', | ||
| 202 | ] | ||
| 203 | with pytest.raises(SystemExit) as exit_info: | ||
| 204 | main(args) | ||
| 205 | captured = capsys.readouterr() | ||
| 206 | assert captured.out == ( | ||
| 207 | f'Seed: TesT, Step: 2, Hash: md5, Range: 3' | ||
| 208 | f'{os.linesep}' | ||
| 209 | f'2: 0x4049f8b161669b7b{os.linesep}' | ||
| 210 | f'1: 0x7965e05436f5029f{os.linesep}' | ||
| 211 | f'0: 0x9e876134d90499dd{os.linesep}' | ||
| 212 | ) | ||
| 213 | assert exit_info.type == SystemExit | ||
| 214 | assert exit_info.value.code == 0 | ||
| 215 | args.append('-q') | ||
| 216 | with pytest.raises(SystemExit) as exit_info: | ||
| 217 | main(args) | ||
| 218 | captured = capsys.readouterr() | ||
| 219 | assert captured.out == ( | ||
| 220 | f'2: 0x4049f8b161669b7b{os.linesep}' | ||
| 221 | f'1: 0x7965e05436f5029f{os.linesep}' | ||
| 222 | f'0: 0x9e876134d90499dd{os.linesep}' | ||
| 223 | ) | ||
| 224 | assert exit_info.type == SystemExit | ||
| 225 | assert exit_info.value.code == 0 | ||
| 226 | args.extend(['-f', 'token']) | ||
| 227 | with pytest.raises(SystemExit) as exit_info: | ||
| 228 | main(args) | ||
| 229 | captured = capsys.readouterr() | ||
| 230 | assert captured.out == ( | ||
| 231 | f'2: THY AVON NO NECK COKE MOLL{os.linesep}' | ||
| 232 | f'1: EASE OIL FUM CURE AWRY AVIS{os.linesep}' | ||
| 233 | f'0: INCH SEA ANNE LONG AHEM TOUR{os.linesep}' | ||
| 234 | ) | ||
| 235 | assert exit_info.type == SystemExit | ||
| 236 | assert exit_info.value.code == 0 | ||
| 237 | |||
| 238 | |||
| 239 | def test_main_initiate(capsys): | ||
| 240 | """tests main""" | ||
| 241 | args = [ | ||
| 242 | '--initiate-new-sequence', | ||
| 243 | '-i', | ||
| 244 | '500', | ||
| 245 | '-s', | ||
| 246 | 'TesT', | ||
| 247 | '-p', | ||
| 248 | 'This is a test.', | ||
| 249 | ] | ||
| 250 | with pytest.raises(SystemExit) as exit_info: | ||
| 251 | main(args) | ||
| 252 | captured = capsys.readouterr() | ||
| 253 | assert captured.out == ( | ||
| 254 | f'Seed: TesT, Step: 500, Hash: md5{os.linesep}' | ||
| 255 | f'0x2b8d82b6ac14346c{os.linesep}' | ||
| 256 | ) | ||
| 257 | assert exit_info.type == SystemExit | ||
| 258 | assert exit_info.value.code == 0 | ||
| 259 | args.append('-q') | ||
| 260 | with pytest.raises(SystemExit) as exit_info: | ||
| 261 | main(args) | ||
| 262 | captured = capsys.readouterr() | ||
| 263 | assert captured.out == f'0x2b8d82b6ac14346c{os.linesep}' | ||
| 264 | assert exit_info.type == SystemExit | ||
| 265 | assert exit_info.value.code == 0 | ||
