summary refs log tree commit diff
path: root/pkgs/tools/security/pass/extensions/audit/0001-Make-it-possible-to-run-the-tests-offline.patch
blob: 36faf3140ccd0540fd9d4867343e61d25d6f9b53 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
From 37c2b4d2940476555aeec20fe1e5e3fa0492a94e Mon Sep 17 00:00:00 2001
From: Maximilian Bosch <maximilian@mbosch.me>
Date: Sun, 15 Mar 2020 19:58:53 +0100
Subject: [PATCH] Make it possible to run the tests offline

Helpful when developing without network access, also makes sure that
the test actually depend on the API's data like number of breaches
(which will change in time).
---
 tests/commons.py         | 25 +++++++++++++++++++++++++
 tests/test_audit.py      |  8 +++++---
 tests/test_pass_audit.py | 10 +++++++++-
 tests/test_pwned.py      |  8 +++++---
 4 files changed, 44 insertions(+), 7 deletions(-)

diff --git a/tests/commons.py b/tests/commons.py
index 13c4cb1..4f1ecd8 100644
--- a/tests/commons.py
+++ b/tests/commons.py
@@ -56,3 +56,28 @@ class TestPass(TestBase):
         for path in self.store.list(root):
             data[path] = self.store.show(path)
         return data
+
+
+def mock_request(*args, **kwargs):
+    class MockResponse:
+        def __init__(self):
+            data = [
+                "D5EE0CB1A41071812CCED2F1930E6E1A5D2:2",
+                "2DC183F740EE76F27B78EB39C8AD972A757:52579",
+                "CF164D7A51A1FD864B1BF9E1CE8A3EC171B:4",
+                "D0B910E7A3028703C0B30039795E908CEB2:7",
+                "AD6438836DBE526AA231ABDE2D0EEF74D42:3",
+                "EBAB0A7CE978E0194608B572E4F9404AA21:3",
+                "17727EAB0E800E62A776C76381DEFBC4145:120",
+                "5370372AC65308F03F6ED75EC6068C8E1BE:1386",
+                "1E4C9B93F3F0682250B6CF8331B7EE68FD8:3730471",
+                "437FAA5A7FCE15D1DDCB9EAEAEA377667B8:123422",
+                "944C22589AC652B0F47918D58CA0CDCCB63:411"
+            ]
+
+            self.text = "\r\n".join(data)
+
+        def raise_for_status(self):
+            pass
+
+    return MockResponse()
diff --git a/tests/test_audit.py b/tests/test_audit.py
index d8c7a9a..5e0a9cf 100644
--- a/tests/test_audit.py
+++ b/tests/test_audit.py
@@ -17,12 +17,13 @@
 #
 
 from .. import pass_audit
-from tests.commons import TestPass
-
+from tests.commons import TestPass, mock_request
+from unittest import mock
 
 class TestPassAudit(TestPass):
     passwords_nb = 7
 
+    @mock.patch('requests.get', mock_request)
     def test_password_notpwned(self):
         """Testing: pass audit for password not breached with K-anonymity method."""
         data = self._getdata("Password/notpwned")
@@ -30,9 +31,10 @@ class TestPassAudit(TestPass):
         breached = audit.password()
         self.assertTrue(len(breached) == 0)
 
+    @mock.patch('requests.get', mock_request)
     def test_password_pwned(self):
         """Testing: pass audit for password breached with K-anonymity method."""
-        ref_counts = [51259, 3, 114, 1352, 3645804, 78773, 396]
+        ref_counts = [52579, 3, 120, 1386, 3730471, 123422, 411]
         data = self._getdata("Password/pwned")
         audit = pass_audit.PassAudit(data)
         breached = audit.password()
diff --git a/tests/test_pass_audit.py b/tests/test_pass_audit.py
index 4c10f87..2c949f7 100644
--- a/tests/test_pass_audit.py
+++ b/tests/test_pass_audit.py
@@ -19,7 +19,8 @@
 import os
 
 from .. import pass_audit
-from tests.commons import TestPass
+from tests.commons import TestPass, mock_request
+from unittest import mock
 
 
 class TestPassAuditCMD(TestPass):
@@ -47,6 +48,7 @@ class TestPassAuditCMD(TestPass):
         cmd = ['--not-an-option', '-q']
         self._passaudit(cmd, 2)
 
+    @mock.patch('requests.get', mock_request)
     def test_pass_audit_StoreNotInitialized(self):
         """Testing: store not initialized."""
         cmd = ['Password/', '-v']
@@ -56,6 +58,7 @@ class TestPassAuditCMD(TestPass):
         os.rename(os.path.join(self.store.prefix, 'backup.gpg-id'),
                   os.path.join(self.store.prefix, '.gpg-id'))
 
+    @mock.patch('requests.get', mock_request)
     def test_pass_audit_InvalidID(self):
         """Testing: invalid user ID."""
         os.rename(os.path.join(self.store.prefix, '.gpg-id'),
@@ -66,26 +69,31 @@ class TestPassAuditCMD(TestPass):
         os.rename(os.path.join(self.store.prefix, 'backup.gpg-id'),
                   os.path.join(self.store.prefix, '.gpg-id'))
 
+    @mock.patch('requests.get', mock_request)
     def test_pass_audit_NotAFile(self):
         """Testing: pass audit not_a_file."""
         cmd = ['not_a_file']
         self._passaudit(cmd, 1)
 
+    @mock.patch('requests.get', mock_request)
     def test_pass_audit_passwords_notpwned(self):
         """Testing: pass audit Password/notpwned."""
         cmd = ['Password/notpwned']
         self._passaudit(cmd)
 
+    @mock.patch('requests.get', mock_request)
     def test_pass_audit_passwords_pwned(self):
         """Testing: pass audit Password/pwned."""
         cmd = ['Password/pwned']
         self._passaudit(cmd)
 
+    @mock.patch('requests.get', mock_request)
     def test_pass_audit_passwords_good(self):
         """Testing: pass audit Password/good."""
         cmd = ['Password/good']
         self._passaudit(cmd)
 
+    @mock.patch('requests.get', mock_request)
     def test_pass_audit_passwords_all(self):
         """Testing: pass audit ."""
         cmd = ['']
diff --git a/tests/test_pwned.py b/tests/test_pwned.py
index 5ce6bc6..c28939a 100644
--- a/tests/test_pwned.py
+++ b/tests/test_pwned.py
@@ -17,7 +17,8 @@
 #
 
 from .. import pass_audit
-from tests.commons import TestPass
+from tests.commons import TestPass, mock_request
+from unittest import mock
 
 
 class TestPwnedAPI(TestPass):
@@ -25,12 +26,13 @@ class TestPwnedAPI(TestPass):
     def setUp(self):
         self.api = pass_audit.PwnedAPI()
 
+    @mock.patch('requests.get', mock_request)
     def test_password_range(self):
         """Testing: https://api.haveibeenpwned.com/range API."""
         prefix = '21BD1'
         Hash = '21BD12DC183F740EE76F27B78EB39C8AD972A757'
         hashes, counts = self.api.password_range(prefix)
         self.assertIn(Hash, hashes)
-        self.assertTrue(counts[hashes.index(Hash)] == 51259)
+        self.assertTrue(counts[hashes.index(Hash)] == 52579)
         self.assertTrue(len(hashes) == len(counts))
-        self.assertTrue(len(hashes) == 527)
+        self.assertTrue(len(hashes) == 11)
-- 
2.25.0