Debian Codegraph @

tests/test_reset.py

raw
1 import pytest
2 from unittest.mock import patch
3 from flask import Flask
4 from a2d.routes.reset import reset_routes
5
6 @pytest.fixture
7 def app():
8 """Fixture to create a Flask app instance."""
9 app = Flask(__name__)
10 app.config['TESTING'] = True
11 app.register_blueprint(reset_routes)
12 app.secret_key = 'test_secret_key' # Set a secret key for session management
13 return app
14
15 @pytest.fixture
16 def client(app):
17 """Fixture to create a test client with app context."""
18 with app.test_client() as client:
19 with app.app_context():
20 yield client
21
22 def test_reset_account(client):
23 """Test case for resetting account with valid passphrase."""
24 with client.session_transaction() as sess:
25 sess['user_id'] = 1 # Example user ID
26
27 # Mock passphrase verification and form data
28 with patch('a2d.routes.reset.verify_passphrase', return_value=True):
29 data = {
30 'passphrase': 'testpassphrase',
31 'reset_confirm': 'delete',
32 'self_ssl_delete': 'true',
33 'ca_ssl_delete': 'false'
34 }
35 rv = client.post('/reset-portal', data=data)
36
37 assert rv.status_code == 302 # Expecting redirect upon successful reset
38
39 def test_reset_account_invalid_passphrase(client):
40 """Test case for resetting account with invalid passphrase."""
41 with client.session_transaction() as sess:
42 sess['user_id'] = 1 # Example user ID
43
44 # Mock invalid passphrase verification
45 with patch('a2d.routes.reset.verify_passphrase', return_value=False):
46 data = {
47 'passphrase': 'wrongpass',
48 'reset_confirm': 'delete',
49 'self_ssl_delete': 'true',
50 'ca_ssl_delete': 'false'
51 }
52 rv = client.post('/reset-portal', data=data)
53
54 assert rv.status_code == 302 # Expecting redirect upon failure due to invalid passphrase
55
56 def test_reset_account_no_confirmation(client):
57 """Test case for resetting account without confirmation."""
58 with client.session_transaction() as sess:
59 sess['user_id'] = 1 # Example user ID
60
61 # No confirmation provided
62 data = {
63 'passphrase': 'testpassphrase',
64 'reset_confirm': '',
65 'self_ssl_delete': 'false',
66 'ca_ssl_delete': 'false'
67 }
68 rv = client.post('/reset-portal', data=data)
69
70 assert rv.status_code == 302 # Expecting redirect or another appropriate status code
71
72 def test_reset_account_no_login(client):
73 """Test case for resetting account without user logged in."""
74 # No user logged in scenario
75 data = {
76 'passphrase': 'testpassphrase',
77 'reset_confirm': 'delete',
78 'self_ssl_delete': 'true',
79 'ca_ssl_delete': 'false'
80 }
81 rv = client.post('/reset-portal', data=data, follow_redirects=True)
82
83 assert rv.status_code == 404 # Expecting 404 when endpoint is not reachable without login