33import time
44import unittest
55from unittest import TestCase
6- from unittest import mock
76from urllib .parse import quote
87from urllib .parse import unquote
98
109from freezegun import freeze_time
11- from fakeredis import FakeStrictRedis
1210
1311os .environ ['MOCK_REDIS' ] = 'true'
1412
2119
2220class SnapPassTestCase (TestCase ):
2321
24- @mock .patch ('redis.client.StrictRedis' , FakeStrictRedis )
2522 def test_get_password (self ):
2623 password = "melatonin overdose 1337!$"
2724 key = snappass .set_password (password , 30 )
@@ -46,13 +43,11 @@ def test_returned_token_format(self):
4643 # 16 bytes urlsafe base64 is 22 chars
4744 self .assertEqual (len (storage .REDIS_PREFIX ) + 22 , len (token ))
4845
49- @mock .patch ('redis.client.StrictRedis' , FakeStrictRedis )
5046 def test_password_before_expiration (self ):
5147 password = 'fidelio'
5248 key = snappass .set_password (password , 1 )
5349 self .assertEqual (password , snappass .get_password (key ))
5450
55- @mock .patch ('redis.client.StrictRedis' , FakeStrictRedis )
5651 def test_password_after_expiration (self ):
5752 password = 'open sesame'
5853 key = snappass .set_password (password , 1 )
@@ -92,29 +87,25 @@ def test_handle_password_missing_data(self):
9287 rv = self .app .post ('/' , data = {})
9388 self .assertEqual (rv .status_code , 500 )
9489
95- @mock .patch ('redis.client.StrictRedis' , FakeStrictRedis )
9690 def test_preview_password (self ):
9791 password = "I like novelty kitten statues!"
9892 key = snappass .set_password (password , 30 )
9993 rv = self .app .get ('/{0}' .format (key ))
10094 # The password payload should not be visible in preview
10195 self .assertNotIn (password , rv .get_data (as_text = True ))
10296
103- @mock .patch ('redis.client.StrictRedis' , FakeStrictRedis )
10497 def test_preview_password_not_found (self ):
10598 rv = self .app .get ('/invalid_key' )
10699 self .assertEqual (rv .status_code , 404 )
107100 self .assertIn ('Secret Not Found' , rv .get_data (as_text = True ))
108101
109- @mock .patch ('redis.client.StrictRedis' , FakeStrictRedis )
110102 def test_show_password (self ):
111103 password = "I like novelty kitten statues!"
112104 key = snappass .set_password (password , 30 )
113105 rv = self .app .post ('/{0}' .format (key ))
114106 # The payload (encrypted text) is shown in the text area
115107 self .assertIn (password , rv .get_data (as_text = True ))
116108
117- @mock .patch ('redis.client.StrictRedis' , FakeStrictRedis )
118109 def test_show_password_not_found (self ):
119110 rv = self .app .post ('/invalid_key' )
120111 self .assertEqual (rv .status_code , 404 )
@@ -126,7 +117,6 @@ def test_url_prefix(self):
126117 rv = self .app .post ('/' , data = {'password' : password , 'ttl' : 'hour' })
127118 self .assertIn ("localhost/test/prefix/" , rv .get_data (as_text = True ))
128119
129- @mock .patch ('redis.client.StrictRedis' , FakeStrictRedis )
130120 def test_set_password_json (self ):
131121 with freeze_time ("2020-05-08 12:00:00" ) as frozen_time :
132122 password = 'my name is my passport. verify me.'
@@ -147,7 +137,6 @@ def test_set_password_json(self):
147137 frozen_time .move_to ("2020-05-22 12:00:00" )
148138 self .assertIsNone (snappass .get_password (key ))
149139
150- @mock .patch ('redis.client.StrictRedis' , FakeStrictRedis )
151140 def test_set_password_api (self ):
152141 with freeze_time ("2020-05-08 12:00:00" ) as frozen_time :
153142 password = 'my name is my passport. verify me.'
@@ -176,7 +165,6 @@ def test_api_handle_password_missing_data(self):
176165 )
177166 self .assertEqual (rv .status_code , 500 )
178167
179- @mock .patch ('redis.client.StrictRedis' , FakeStrictRedis )
180168 def test_set_password_api_default_ttl (self ):
181169 with freeze_time ("2020-05-08 12:00:00" ) as frozen_time :
182170 password = 'my name is my passport. verify me.'
@@ -229,7 +217,6 @@ def test_uses_host_override_with_untrusted_host_header(self):
229217 json_content ['link' ].startswith ('https://snappass.example.org/' )
230218 )
231219
232- @mock .patch ('redis.client.StrictRedis' , FakeStrictRedis )
233220 def test_set_password_api_v2 (self ):
234221 with freeze_time ("2020-05-08 12:00:00" ) as frozen_time :
235222 password = 'my name is my passport. verify me.'
@@ -248,7 +235,6 @@ def test_set_password_api_v2(self):
248235 frozen_time .move_to ("2020-05-22 12:00:00" )
249236 self .assertIsNone (snappass .get_password (key ))
250237
251- @mock .patch ('redis.client.StrictRedis' , FakeStrictRedis )
252238 def test_set_password_api_v2_default_ttl (self ):
253239 with freeze_time ("2020-05-08 12:00:00" ) as frozen_time :
254240 password = 'my name is my passport. verify me.'
@@ -315,7 +301,6 @@ def test_set_password_api_v2_no_password_and_too_big_ttl(self):
315301 bad_ttl = invalid_params [1 ]
316302 self .assertEqual (bad_ttl ['name' ], 'ttl' )
317303
318- @mock .patch ('redis.client.StrictRedis' , FakeStrictRedis )
319304 def test_check_password_api_v2 (self ):
320305 password = 'my name is my passport. verify me.'
321306 rv = self .app .post (
@@ -330,7 +315,6 @@ def test_check_password_api_v2(self):
330315 rvc = self .app .head ('/api/v2/passwords/' + quote (key ))
331316 self .assertEqual (rvc .status_code , 200 )
332317
333- @mock .patch ('redis.client.StrictRedis' , FakeStrictRedis )
334318 def test_check_password_api_v2_bad_keys (self ):
335319 password = 'my name is my passport. verify me.'
336320 rv = self .app .post (
@@ -345,7 +329,6 @@ def test_check_password_api_v2_bad_keys(self):
345329 rvc = self .app .head ('/api/v2/passwords/' + quote (key [::- 1 ]))
346330 self .assertEqual (rvc .status_code , 404 )
347331
348- @mock .patch ('redis.client.StrictRedis' , FakeStrictRedis )
349332 def test_retrieve_password_api_v2 (self ):
350333 password = 'my name is my passport. verify me.'
351334 rv = self .app .post (
@@ -364,7 +347,6 @@ def test_retrieve_password_api_v2(self):
364347 retrieved_password = json_content_retrieved ['password' ]
365348 self .assertEqual (retrieved_password , password )
366349
367- @mock .patch ('redis.client.StrictRedis' , FakeStrictRedis )
368350 def test_retrieve_password_api_v2_bad_keys (self ):
369351 password = 'my name is my passport. verify me.'
370352 rv = self .app .post (
0 commit comments