This repository has been archived by the owner on Jan 2, 2025. It is now read-only.
forked from TwilioDevEd/account-security-quickstart-flask
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwofa_tests.py
174 lines (144 loc) · 5.28 KB
/
twofa_tests.py
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
import os
import twofa
import unittest
import tempfile
from twofa.database import db_session
from twofa.models import User
try:
from urllib.parse import urlparse
except ImportError:
from urlparse import urlparse
try:
from unittest.mock import patch, MagicMock
except ImportError:
from unittest.mock import patch, MagicMock
class TwoFATestCase(unittest.TestCase):
def setUp(self):
self.db_fd, twofa.app.config["DATABASE"] = tempfile.mkstemp()
twofa.app.testing = True
twofa.app.config["WTF_CSRF_METHODS"] = [] # This is the magic
twofa.app.config["WTF_CSRF_ENABLED"] = False
self.app = twofa.app.test_client()
with twofa.app.app_context():
twofa.init_db()
self.user = User(
username="test",
password="test",
email="[email protected]",
authy_id="fake_id",
)
db_session.add(self.user)
db_session.commit()
def tearDown(self):
db_session.delete(self.user)
db_session.commit()
os.close(self.db_fd)
os.unlink(twofa.app.config["DATABASE"])
def test_protected_redirect_anonymous_to_login(self):
# Arrange
# Act
response = self.app.get("/protected")
# Assert
assert response.status_code == 302
assert urlparse(response.location).path == "/login"
def test_protected_logged_in_user_redirected_to_2fa(self):
# Arrange
# Act
with self.app:
response = self.app.post(
"/login",
data={"username": "test", "password": "test"},
follow_redirects=False,
)
response = self.app.get("/protected")
# Assert
assert response.status_code == 302
assert urlparse(response.location).path == "/2fa"
def test_protected_displays_to_authy_user(self):
# Arrange
# Act
with self.app:
response = self.app.post(
"/login",
data={"username": "test", "password": "test"},
follow_redirects=False,
)
with self.app.session_transaction() as sess:
sess["authy"] = True
response = self.app.get("/protected")
# Assert
assert response.status_code == 200
@patch("twofa.views.authy_api")
def test_token_sms_success(self, authy_api):
# Arrange
request_sms_response = MagicMock()
request_sms_response.ok.return_value = True
authy_api.users.request_sms.return_value = request_sms_response
# Act
with self.app:
response = self.app.post(
"/login",
data={"username": "test", "password": "test"},
follow_redirects=False,
)
response = self.app.post("/token/sms")
# Assert
assert response.status_code == 200
authy_api.users.request_sms.assert_called_once_with("fake_id", {"force": True})
request_sms_response.ok.assert_called_once()
@patch("twofa.views.authy_api")
def test_token_sms_failure(self, authy_api):
# Arrange
request_sms_response = MagicMock()
request_sms_response.ok.return_value = False
authy_api.users.request_sms.return_value = request_sms_response
# Act
with self.app:
response = self.app.post(
"/login",
data={"username": "test", "password": "test"},
follow_redirects=False,
)
response = self.app.post("/token/sms")
# Assert
assert response.status_code == 503
authy_api.users.request_sms.assert_called_once_with("fake_id", {"force": True})
request_sms_response.ok.assert_called_once()
@patch("twofa.views.authy_api")
def test_token_voice_success(self, authy_api):
# Arrange
request_call_response = MagicMock()
request_call_response.ok.return_value = True
authy_api.users.request_call.return_value = request_call_response
# Act
with self.app:
response = self.app.post(
"/login",
data={"username": "test", "password": "test"},
follow_redirects=False,
)
response = self.app.post("/token/voice")
# Assert
assert response.status_code == 200
authy_api.users.request_call.assert_called_once_with("fake_id", {"force": True})
request_call_response.ok.assert_called_once()
@patch("twofa.views.authy_api")
def test_token_voice_failure(self, authy_api):
# Arrange
request_call_response = MagicMock()
request_call_response.ok.return_value = False
authy_api.users.request_call.return_value = request_call_response
# Act
with self.app:
response = self.app.post(
"/login",
data={"username": "test", "password": "test"},
follow_redirects=False,
)
response = self.app.post("/token/voice")
# Assert
assert response.status_code == 503
authy_api.users.request_call.assert_called_once_with("fake_id", {"force": True})
request_call_response.ok.assert_called_once()
if __name__ == "__main__":
unittest.main()