askrobots / dbbasic-accounts
Public
Unix-style user accounts (passwd, groups, permissions) with a web-friendly Python API.
test_accounts.py · 8.9 KB · 267 lines
| 1 | """ |
| 2 | Tests for dbbasic_accounts.Accounts (web-friendly API) |
| 3 | """ |
| 4 | |
| 5 | import pytest |
| 6 | import tempfile |
| 7 | import shutil |
| 8 | from pathlib import Path |
| 9 | |
| 10 | from dbbasic_accounts import Accounts, User |
| 11 | |
| 12 | |
| 13 | class TestAccounts: |
| 14 | """Test Accounts class (web-friendly API)""" |
| 15 | |
| 16 | @pytest.fixture |
| 17 | def temp_dir(self): |
| 18 | """Create temporary directory for testing""" |
| 19 | tmpdir = tempfile.mkdtemp() |
| 20 | yield tmpdir |
| 21 | shutil.rmtree(tmpdir) |
| 22 | |
| 23 | @pytest.fixture |
| 24 | def accounts(self, temp_dir): |
| 25 | """Create Accounts instance""" |
| 26 | return Accounts(temp_dir, domain='example.com') |
| 27 | |
| 28 | def test_init_creates_directories(self, temp_dir): |
| 29 | """Test that init creates required directories""" |
| 30 | accounts = Accounts(temp_dir, domain='test.com') |
| 31 | |
| 32 | assert (Path(temp_dir) / 'etc').exists() |
| 33 | assert (Path(temp_dir) / 'home').exists() |
| 34 | assert (Path(temp_dir) / 'var').exists() |
| 35 | assert (Path(temp_dir) / 'var' / 'mail').exists() |
| 36 | |
| 37 | def test_init_creates_tsv_files(self, temp_dir): |
| 38 | """Test that init creates TSV files""" |
| 39 | accounts = Accounts(temp_dir, domain='test.com') |
| 40 | |
| 41 | assert (Path(temp_dir) / 'etc' / 'passwd.tsv').exists() |
| 42 | assert (Path(temp_dir) / 'etc' / 'shadow.tsv').exists() |
| 43 | assert (Path(temp_dir) / 'etc' / 'group.tsv').exists() |
| 44 | |
| 45 | def test_register_creates_user(self, accounts): |
| 46 | """Test registering a new user""" |
| 47 | user = accounts.register('john@example.com', 'secret123', name='John Doe') |
| 48 | |
| 49 | assert isinstance(user, User) |
| 50 | assert user.username == 'john' |
| 51 | assert user.fullname == 'John Doe <john@example.com>' |
| 52 | assert user.uid == 1000 |
| 53 | |
| 54 | def test_register_creates_home_directory(self, accounts, temp_dir): |
| 55 | """Test that register creates home directory""" |
| 56 | accounts.register('john@example.com', 'secret123') |
| 57 | |
| 58 | home_dir = Path(temp_dir) / 'home' / 'john' |
| 59 | assert home_dir.exists() |
| 60 | assert (home_dir / 'uploads').exists() |
| 61 | |
| 62 | def test_register_creates_mailbox(self, accounts, temp_dir): |
| 63 | """Test that register creates mailbox""" |
| 64 | accounts.register('john@example.com', 'secret123') |
| 65 | |
| 66 | mailbox = Path(temp_dir) / 'var' / 'mail' / 'john' |
| 67 | assert mailbox.exists() |
| 68 | |
| 69 | def test_register_duplicate_fails(self, accounts): |
| 70 | """Test that registering duplicate user fails""" |
| 71 | accounts.register('john@example.com', 'secret123') |
| 72 | |
| 73 | with pytest.raises(ValueError, match="already exists"): |
| 74 | accounts.register('john@example.com', 'secret456') |
| 75 | |
| 76 | def test_login_valid_credentials(self, accounts): |
| 77 | """Test login with valid credentials""" |
| 78 | accounts.register('john@example.com', 'secret123', name='John Doe') |
| 79 | |
| 80 | user = accounts.login('john@example.com', 'secret123') |
| 81 | |
| 82 | assert user is not None |
| 83 | assert user.username == 'john' |
| 84 | |
| 85 | def test_login_invalid_password(self, accounts): |
| 86 | """Test login with invalid password""" |
| 87 | accounts.register('john@example.com', 'secret123') |
| 88 | |
| 89 | user = accounts.login('john@example.com', 'wrongpassword') |
| 90 | |
| 91 | assert user is None |
| 92 | |
| 93 | def test_login_nonexistent_user(self, accounts): |
| 94 | """Test login with nonexistent user""" |
| 95 | user = accounts.login('nobody@example.com', 'password') |
| 96 | |
| 97 | assert user is None |
| 98 | |
| 99 | def test_get_user_by_email(self, accounts): |
| 100 | """Test getting user by email""" |
| 101 | accounts.register('john@example.com', 'secret123', name='John Doe') |
| 102 | |
| 103 | user = accounts.get_user(email='john@example.com') |
| 104 | |
| 105 | assert user is not None |
| 106 | assert user.username == 'john' |
| 107 | |
| 108 | def test_get_user_by_username(self, accounts): |
| 109 | """Test getting user by username""" |
| 110 | accounts.register('john@example.com', 'secret123', name='John Doe') |
| 111 | |
| 112 | user = accounts.get_user(username='john') |
| 113 | |
| 114 | assert user is not None |
| 115 | assert user.username == 'john' |
| 116 | |
| 117 | def test_get_user_by_id(self, accounts): |
| 118 | """Test getting user by ID""" |
| 119 | registered = accounts.register('john@example.com', 'secret123') |
| 120 | |
| 121 | user = accounts.get_user(user_id=registered.uid) |
| 122 | |
| 123 | assert user is not None |
| 124 | assert user.username == 'john' |
| 125 | assert user.uid == registered.uid |
| 126 | |
| 127 | def test_get_user_not_found(self, accounts): |
| 128 | """Test getting nonexistent user""" |
| 129 | user = accounts.get_user(email='nobody@example.com') |
| 130 | |
| 131 | assert user is None |
| 132 | |
| 133 | def test_add_role(self, accounts): |
| 134 | """Test adding role to user""" |
| 135 | accounts.register('john@example.com', 'secret123') |
| 136 | |
| 137 | result = accounts.add_role('john@example.com', 'editor') |
| 138 | |
| 139 | assert result is True |
| 140 | roles = accounts.get_roles('john@example.com') |
| 141 | assert 'editor' in roles |
| 142 | |
| 143 | def test_remove_role(self, accounts): |
| 144 | """Test removing role from user""" |
| 145 | accounts.register('john@example.com', 'secret123') |
| 146 | accounts.add_role('john@example.com', 'editor') |
| 147 | |
| 148 | result = accounts.remove_role('john@example.com', 'editor') |
| 149 | |
| 150 | assert result is True |
| 151 | roles = accounts.get_roles('john@example.com') |
| 152 | assert 'editor' not in roles |
| 153 | |
| 154 | def test_get_roles(self, accounts): |
| 155 | """Test getting user roles""" |
| 156 | accounts.register('john@example.com', 'secret123') |
| 157 | accounts.add_role('john@example.com', 'editor') |
| 158 | accounts.add_role('john@example.com', 'admin') |
| 159 | |
| 160 | roles = accounts.get_roles('john@example.com') |
| 161 | |
| 162 | assert 'users' in roles # default group |
| 163 | assert 'editor' in roles |
| 164 | assert 'admin' in roles |
| 165 | |
| 166 | def test_change_password(self, accounts): |
| 167 | """Test changing password""" |
| 168 | accounts.register('john@example.com', 'secret123') |
| 169 | |
| 170 | result = accounts.change_password('john@example.com', 'newsecret456') |
| 171 | |
| 172 | assert result is True |
| 173 | |
| 174 | # Old password should not work |
| 175 | user = accounts.login('john@example.com', 'secret123') |
| 176 | assert user is None |
| 177 | |
| 178 | # New password should work |
| 179 | user = accounts.login('john@example.com', 'newsecret456') |
| 180 | assert user is not None |
| 181 | |
| 182 | def test_delete_user(self, accounts): |
| 183 | """Test deleting user""" |
| 184 | accounts.register('john@example.com', 'secret123') |
| 185 | |
| 186 | result = accounts.delete_user('john@example.com') |
| 187 | |
| 188 | assert result is True |
| 189 | |
| 190 | # User should not exist anymore |
| 191 | user = accounts.get_user(email='john@example.com') |
| 192 | assert user is None |
| 193 | |
| 194 | def test_delete_nonexistent_user(self, accounts): |
| 195 | """Test deleting nonexistent user""" |
| 196 | result = accounts.delete_user('nobody@example.com') |
| 197 | |
| 198 | assert result is False |
| 199 | |
| 200 | def test_list_users(self, accounts): |
| 201 | """Test listing all users""" |
| 202 | accounts.register('john@example.com', 'secret123', name='John Doe') |
| 203 | accounts.register('jane@example.com', 'pass456', name='Jane Smith') |
| 204 | |
| 205 | users = accounts.list_users() |
| 206 | |
| 207 | assert len(users) == 2 |
| 208 | usernames = [u.username for u in users] |
| 209 | assert 'john' in usernames |
| 210 | assert 'jane' in usernames |
| 211 | |
| 212 | def test_get_home_directory(self, accounts, temp_dir): |
| 213 | """Test getting home directory path""" |
| 214 | accounts.register('john@example.com', 'secret123') |
| 215 | |
| 216 | home = accounts.get_home_directory('john@example.com') |
| 217 | |
| 218 | assert home is not None |
| 219 | assert home == Path(temp_dir) / 'home' / 'john' |
| 220 | assert home.exists() |
| 221 | |
| 222 | def test_get_home_directory_nonexistent_user(self, accounts): |
| 223 | """Test getting home directory for nonexistent user""" |
| 224 | home = accounts.get_home_directory('nobody@example.com') |
| 225 | |
| 226 | assert home is None |
| 227 | |
| 228 | def test_get_mailbox(self, accounts, temp_dir): |
| 229 | """Test getting mailbox path""" |
| 230 | accounts.register('john@example.com', 'secret123') |
| 231 | |
| 232 | mailbox = accounts.get_mailbox('john@example.com') |
| 233 | |
| 234 | assert mailbox is not None |
| 235 | assert mailbox == Path(temp_dir) / 'var' / 'mail' / 'john' |
| 236 | assert mailbox.exists() |
| 237 | |
| 238 | def test_get_mailbox_nonexistent_user(self, accounts): |
| 239 | """Test getting mailbox for nonexistent user""" |
| 240 | mailbox = accounts.get_mailbox('nobody@example.com') |
| 241 | |
| 242 | assert mailbox is None |
| 243 | |
| 244 | def test_email_to_username_extraction(self, accounts): |
| 245 | """Test username extraction from email""" |
| 246 | user = accounts.register('john.doe@example.com', 'secret123') |
| 247 | |
| 248 | assert user.username == 'john.doe' |
| 249 | |
| 250 | def test_multiple_users_different_uids(self, accounts): |
| 251 | """Test that multiple users get different UIDs""" |
| 252 | user1 = accounts.register('john@example.com', 'secret123') |
| 253 | user2 = accounts.register('jane@example.com', 'pass456') |
| 254 | |
| 255 | assert user1.uid != user2.uid |
| 256 | assert user1.uid == 1000 |
| 257 | assert user2.uid == 1001 |
| 258 | |
| 259 | def test_unix_layer_accessible(self, accounts): |
| 260 | """Test that underlying PasswdDB layer is accessible""" |
| 261 | accounts.register('john@example.com', 'secret123') |
| 262 | |
| 263 | # Access Unix layer directly |
| 264 | user = accounts.passwd.getuser('john') |
| 265 | |
| 266 | assert user is not None |
| 267 | assert user.username == 'john' |