Skip to content

Commit

Permalink
helpful defaults for fake UserPasswordDB to make fakes slightly less …
Browse files Browse the repository at this point in the history
…tedious
  • Loading branch information
glyph committed Feb 14, 2023
1 parent 633341b commit 2983d64
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions src/twisted/python/fakepwd.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
L{twisted.python.fakepwd} provides a fake implementation of the L{pwd} API.
"""

from typing import List
from typing import List, Optional

__all__ = ["UserDatabase", "ShadowDatabase"]

Expand Down Expand Up @@ -61,19 +61,21 @@ class UserDatabase:
"""

_users: List[_UserRecord]
_lastUID: int = 10101
_lastGID: int = 20202

def __init__(self) -> None:
self._users = []

def addUser(
self,
username: str,
password: str,
uid: int,
gid: int,
gecos: str,
home: str,
shell: str,
password: str = "password",
uid: Optional[int] = None,
gid: Optional[int] = None,
gecos: str = "",
home: str = "",
shell: str = "/bin/sh",
) -> None:
"""
Add a new user record to this database.
Expand All @@ -99,9 +101,14 @@ def addUser(
@param shell: The value for the C{pw_shell} field of the user record to
add.
"""
self._users.append(
_UserRecord(username, password, uid, gid, gecos, home, shell)
)
if uid is None:
uid = self._lastUID
self._lastUID += 1
if gid is None:
gid = self._lastGID
self._lastGID += 1
newUser = _UserRecord(username, password, uid, gid, gecos, home, shell)
self._users.append(newUser)

def getpwuid(self, uid: int) -> _UserRecord:
"""
Expand Down

0 comments on commit 2983d64

Please sign in to comment.