summaryrefslogtreecommitdiff
path: root/tests/test_main.py
diff options
context:
space:
mode:
authorSimeon Simeonov2022-03-29 20:44:03 +0200
committerSimeon Simeonov2022-03-29 20:44:03 +0200
commitf19fde48c6b3f65c164c931286e72a0d254c1fb3 (patch)
tree5b6c1bf6cef51c852c49abff3a0e4626e8a33146 /tests/test_main.py
parent12c5e38691b24d12a8fc9605e3b93fac86d461ae (diff)
Add the -P param and improve tests
Diffstat (limited to 'tests/test_main.py')
-rw-r--r--tests/test_main.py207
1 files changed, 170 insertions, 37 deletions
diff --git a/tests/test_main.py b/tests/test_main.py
index b48f625..a331274 100644
--- a/tests/test_main.py
+++ b/tests/test_main.py
@@ -1,7 +1,7 @@
1# -*- coding: utf-8 -*- 1# -*- coding: utf-8 -*-
2# SPDX-License-Identifier: BSD-2-Clause-FreeBSD 2# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3# 3#
4# Copyright (c) 2020, Simeon Simeonov 4# Copyright (c) 2020-2022, Simeon Simeonov
5# All rights reserved. 5# All rights reserved.
6# 6#
7# Redistribution and use in source and binary forms, with or without 7# Redistribution and use in source and binary forms, with or without
@@ -25,6 +25,7 @@
25# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26"""Tests for otp2289.__main__""" 26"""Tests for otp2289.__main__"""
27import os 27import os
28import unittest.mock
28 29
29import pytest 30import pytest
30 31
@@ -33,28 +34,34 @@ from otp2289.__main__ import main
33 34
34def test_main_generate_otp_response(capsys): 35def test_main_generate_otp_response(capsys):
35 """tests main""" 36 """tests main"""
36 args = ['--generate-otp-response', 37 args = [
37 '-a', 38 '--generate-otp-response',
38 'sha1', 39 '-a',
39 '-i', 40 'sha1',
40 '99', 41 '-i',
41 '-s', 42 '99',
42 'TesT', 43 '-s',
43 '-p', 44 'TesT',
44 'This is a test.'] 45 '-p',
46 'This is a test.',
47 ]
45 with pytest.raises(SystemExit) as exit_info: 48 with pytest.raises(SystemExit) as exit_info:
46 main(args) 49 main(args)
47 captured = capsys.readouterr() 50 captured = capsys.readouterr()
48 assert captured.out == (f'Seed: TesT, Step: 99, Hash: sha1{os.linesep}' 51 assert captured.out == (
49 f'0x87fec7768b73ccf9{os.linesep}') 52 f'Seed: TesT, Step: 99, Hash: sha1{os.linesep}'
53 f'0x87fec7768b73ccf9{os.linesep}'
54 )
50 assert exit_info.type == SystemExit 55 assert exit_info.type == SystemExit
51 assert exit_info.value.code == 0 56 assert exit_info.value.code == 0
52 args.extend(['-f', 'token']) 57 args.extend(['-f', 'token'])
53 with pytest.raises(SystemExit) as exit_info: 58 with pytest.raises(SystemExit) as exit_info:
54 main(args) 59 main(args)
55 captured = capsys.readouterr() 60 captured = capsys.readouterr()
56 assert captured.out == (f'Seed: TesT, Step: 99, Hash: sha1{os.linesep}' 61 assert captured.out == (
57 f'GAFF WAIT SKID GIG SKY EYED{os.linesep}') 62 f'Seed: TesT, Step: 99, Hash: sha1{os.linesep}'
63 f'GAFF WAIT SKID GIG SKY EYED{os.linesep}'
64 )
58 assert exit_info.type == SystemExit 65 assert exit_info.type == SystemExit
59 assert exit_info.value.code == 0 66 assert exit_info.value.code == 0
60 args.append('-q') 67 args.append('-q')
@@ -66,61 +73,187 @@ def test_main_generate_otp_response(capsys):
66 assert exit_info.value.code == 0 73 assert exit_info.value.code == 0
67 74
68 75
76def 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
69def test_main_generate_otp_range(capsys): 118def test_main_generate_otp_range(capsys):
70 """tests main""" 119 """tests main"""
71 args = ['--generate-otp-range', 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',
72 '-i', 181 '-i',
73 '2', 182 '2',
74 '-s', 183 '-s',
75 'TesT', 184 'TesT',
76 '-r', 185 '-r',
77 '5', 186 '5',
78 '-p', 187 '-P',
79 'This is a test.'] 188 ],
189 ],
190)
191@unittest.mock.patch('getpass.getpass', lambda *args: 'This is a test.')
192def 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 ]
80 with pytest.raises(SystemExit) as exit_info: 203 with pytest.raises(SystemExit) as exit_info:
81 main(args) 204 main(args)
82 captured = capsys.readouterr() 205 captured = capsys.readouterr()
83 assert captured.out == (f'Seed: TesT, Step: 2, Hash: md5, Range: 3' 206 assert captured.out == (
84 f'{os.linesep}' 207 f'Seed: TesT, Step: 2, Hash: md5, Range: 3'
85 f'2: 0x4049f8b161669b7b{os.linesep}' 208 f'{os.linesep}'
86 f'1: 0x7965e05436f5029f{os.linesep}' 209 f'2: 0x4049f8b161669b7b{os.linesep}'
87 f'0: 0x9e876134d90499dd{os.linesep}') 210 f'1: 0x7965e05436f5029f{os.linesep}'
211 f'0: 0x9e876134d90499dd{os.linesep}'
212 )
88 assert exit_info.type == SystemExit 213 assert exit_info.type == SystemExit
89 assert exit_info.value.code == 0 214 assert exit_info.value.code == 0
90 args.append('-q') 215 args.append('-q')
91 with pytest.raises(SystemExit) as exit_info: 216 with pytest.raises(SystemExit) as exit_info:
92 main(args) 217 main(args)
93 captured = capsys.readouterr() 218 captured = capsys.readouterr()
94 assert captured.out == (f'2: 0x4049f8b161669b7b{os.linesep}' 219 assert captured.out == (
95 f'1: 0x7965e05436f5029f{os.linesep}' 220 f'2: 0x4049f8b161669b7b{os.linesep}'
96 f'0: 0x9e876134d90499dd{os.linesep}') 221 f'1: 0x7965e05436f5029f{os.linesep}'
222 f'0: 0x9e876134d90499dd{os.linesep}'
223 )
97 assert exit_info.type == SystemExit 224 assert exit_info.type == SystemExit
98 assert exit_info.value.code == 0 225 assert exit_info.value.code == 0
99 args.extend(['-f', 'token']) 226 args.extend(['-f', 'token'])
100 with pytest.raises(SystemExit) as exit_info: 227 with pytest.raises(SystemExit) as exit_info:
101 main(args) 228 main(args)
102 captured = capsys.readouterr() 229 captured = capsys.readouterr()
103 assert captured.out == (f'2: THY AVON NO NECK COKE MOLL{os.linesep}' 230 assert captured.out == (
104 f'1: EASE OIL FUM CURE AWRY AVIS{os.linesep}' 231 f'2: THY AVON NO NECK COKE MOLL{os.linesep}'
105 f'0: INCH SEA ANNE LONG AHEM TOUR{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 )
106 assert exit_info.type == SystemExit 235 assert exit_info.type == SystemExit
107 assert exit_info.value.code == 0 236 assert exit_info.value.code == 0
108 237
109 238
110def test_main_initiate(capsys): 239def test_main_initiate(capsys):
111 """tests main""" 240 """tests main"""
112 args = ['--initiate-new-sequence', 241 args = [
113 '-i', 242 '--initiate-new-sequence',
114 '500', 243 '-i',
115 '-s', 244 '500',
116 'TesT', 245 '-s',
117 '-p', 246 'TesT',
118 'This is a test.'] 247 '-p',
248 'This is a test.',
249 ]
119 with pytest.raises(SystemExit) as exit_info: 250 with pytest.raises(SystemExit) as exit_info:
120 main(args) 251 main(args)
121 captured = capsys.readouterr() 252 captured = capsys.readouterr()
122 assert captured.out == (f'Seed: TesT, Step: 500, Hash: md5{os.linesep}' 253 assert captured.out == (
123 f'0x2b8d82b6ac14346c{os.linesep}') 254 f'Seed: TesT, Step: 500, Hash: md5{os.linesep}'
255 f'0x2b8d82b6ac14346c{os.linesep}'
256 )
124 assert exit_info.type == SystemExit 257 assert exit_info.type == SystemExit
125 assert exit_info.value.code == 0 258 assert exit_info.value.code == 0
126 args.append('-q') 259 args.append('-q')