|
| 1 | +import os |
| 2 | +import sqlite3 |
| 3 | +from contextlib import contextmanager |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from libwardenpy.funtionality import ( |
| 8 | + AuthenticatedData, |
| 9 | + Entry, |
| 10 | + UnAuthData, |
| 11 | + add_password, |
| 12 | + authenticate_user, |
| 13 | + delete_passwod, |
| 14 | + get_password, |
| 15 | + list_passwords, |
| 16 | + register_user, |
| 17 | +) |
| 18 | + |
| 19 | +# Test database path |
| 20 | +TEST_DB = "test_db.sqlite3" |
| 21 | + |
| 22 | + |
| 23 | +@contextmanager |
| 24 | +def get_test_connection(): |
| 25 | + conn = sqlite3.connect(TEST_DB) |
| 26 | + try: |
| 27 | + yield conn |
| 28 | + except Exception as e: |
| 29 | + conn.rollback() |
| 30 | + raise e |
| 31 | + else: |
| 32 | + conn.commit() |
| 33 | + finally: |
| 34 | + conn.close() |
| 35 | + |
| 36 | + |
| 37 | +@pytest.fixture(autouse=True) |
| 38 | +def setup_teardown(): |
| 39 | + # Setup: Create test database and tables |
| 40 | + conn = sqlite3.connect(TEST_DB) |
| 41 | + conn.execute(""" |
| 42 | + CREATE TABLE IF NOT EXISTS users ( |
| 43 | + username TEXT PRIMARY KEY, |
| 44 | + password_hash TEXT NOT NULL, |
| 45 | + salt BLOB NOT NULL |
| 46 | + ) |
| 47 | + """) |
| 48 | + conn.execute(""" |
| 49 | + CREATE TABLE IF NOT EXISTS passwords ( |
| 50 | + id INTEGER PRIMARY KEY AUTOINCREMENT, |
| 51 | + username TEXT NOT NULL, |
| 52 | + site TEXT NOT NULL, |
| 53 | + encrypted_password BLOB NOT NULL, |
| 54 | + nonce BLOB NOT NULL, |
| 55 | + FOREIGN KEY (username) REFERENCES users(username) |
| 56 | + ) |
| 57 | + """) |
| 58 | + conn.close() |
| 59 | + |
| 60 | + yield |
| 61 | + |
| 62 | + os.remove(TEST_DB) |
| 63 | + |
| 64 | + |
| 65 | +def test_register_user(): |
| 66 | + test_user = UnAuthData("testuser", "testpassword123") |
| 67 | + with get_test_connection() as conn: |
| 68 | + register_user(conn, test_user) |
| 69 | + |
| 70 | + cursor = conn.execute( |
| 71 | + "SELECT username FROM users WHERE username = ?", (test_user.username,) |
| 72 | + ) |
| 73 | + result = cursor.fetchall() |
| 74 | + assert result is not None |
| 75 | + assert len(result) == 1 |
| 76 | + assert result[0][0] == test_user.username |
| 77 | + |
| 78 | + |
| 79 | +def test_register_duplicate_user(): |
| 80 | + test_user = UnAuthData("testuser", "testpassword123") |
| 81 | + with get_test_connection() as conn: |
| 82 | + register_user(conn, test_user) |
| 83 | + # Try to register the same user again |
| 84 | + register_user(conn, test_user) |
| 85 | + register_user(conn, test_user) |
| 86 | + |
| 87 | + cursor = conn.execute( |
| 88 | + "SELECT COUNT(*) FROM users WHERE username = ?", (test_user.username,) |
| 89 | + ) |
| 90 | + count = cursor.fetchone() |
| 91 | + assert count[0] == 1 |
| 92 | + |
| 93 | + |
| 94 | +def test_authenticate_user(): |
| 95 | + test_user = UnAuthData("testuser", "testpassword123") |
| 96 | + with get_test_connection() as conn: |
| 97 | + register_user(conn, test_user) |
| 98 | + key = authenticate_user(conn, test_user) |
| 99 | + assert key is not None |
| 100 | + |
| 101 | + |
| 102 | +def test_authenticate_wrong_password(): |
| 103 | + test_user = UnAuthData("testuser", "testpassword123") |
| 104 | + wrong_pass_user = UnAuthData("testuser", "wrongpassword") |
| 105 | + with get_test_connection() as conn: |
| 106 | + register_user(conn, test_user) |
| 107 | + key = authenticate_user(conn, wrong_pass_user) |
| 108 | + assert key is None |
| 109 | + |
| 110 | + |
| 111 | +def test_add_and_get_password(): |
| 112 | + test_user = UnAuthData("testuser", "testpassword123") |
| 113 | + with get_test_connection() as conn: |
| 114 | + register_user(conn, test_user) |
| 115 | + key = authenticate_user(conn, test_user) |
| 116 | + auth_data = AuthenticatedData(test_user.username, key) |
| 117 | + |
| 118 | + test_entry = Entry("example.com", "password123") |
| 119 | + add_password(conn, auth_data, test_entry) |
| 120 | + |
| 121 | + passwords = get_password(conn, auth_data, "example.com") |
| 122 | + assert passwords is not None |
| 123 | + assert len(passwords) == 1 |
| 124 | + assert passwords[0][1] == "example.com" |
| 125 | + assert passwords[0][2].decode() == "password123" |
| 126 | + |
| 127 | + |
| 128 | +def test_list_passwords(): |
| 129 | + test_user = UnAuthData("testuser", "testpassword123") |
| 130 | + with get_test_connection() as conn: |
| 131 | + register_user(conn, test_user) |
| 132 | + key = authenticate_user(conn, test_user) |
| 133 | + auth_data = AuthenticatedData(test_user.username, key) |
| 134 | + |
| 135 | + # Add multiple passwords |
| 136 | + add_password(conn, auth_data, Entry("site1.com", "pass1")) |
| 137 | + add_password(conn, auth_data, Entry("site2.com", "pass2")) |
| 138 | + |
| 139 | + passwords = list_passwords(conn, auth_data) |
| 140 | + assert passwords is not None |
| 141 | + assert len(passwords) == 2 |
| 142 | + assert any(p[0] == "site1.com" and p[1].decode() == "pass1" for p in passwords) |
| 143 | + assert any(p[0] == "site2.com" and p[1].decode() == "pass2" for p in passwords) |
| 144 | + |
| 145 | + |
| 146 | +def test_delete_password(): |
| 147 | + test_user = UnAuthData("testuser", "testpassword123") |
| 148 | + with get_test_connection() as conn: |
| 149 | + register_user(conn, test_user) |
| 150 | + key = authenticate_user(conn, test_user) |
| 151 | + auth_data = AuthenticatedData(test_user.username, key) |
| 152 | + |
| 153 | + add_password(conn, auth_data, Entry("example.com", "password123")) |
| 154 | + passwords = get_password(conn, auth_data, "example.com") |
| 155 | + if passwords is not None: |
| 156 | + password_id = str(passwords[0][0]) |
| 157 | + delete_passwod(conn, auth_data, password_id) |
| 158 | + |
| 159 | + passwords_after_delete = get_password(conn, auth_data, "example.com") |
| 160 | + assert passwords_after_delete is None |
0 commit comments