Skip to content

Commit

Permalink
Server func rename
Browse files Browse the repository at this point in the history
  • Loading branch information
themanyfaceddemon committed Aug 11, 2024
1 parent 5f6e1c7 commit 5b5342b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
14 changes: 7 additions & 7 deletions DMBotNetwork/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ async def _call_net_method(cls, method_name: str, **kwargs) -> Any:
return method(cls, **valid_kwargs)

# DB work
async def _db_login_user(self, login: str, password: str) -> Optional[str]:
async def db_login_user(self, login: str, password: str) -> Optional[str]:
try:
async with self._connection.execute("SELECT password FROM users WHERE username = ?", (login,)) as cursor:
row = await cursor.fetchone()
Expand All @@ -101,7 +101,7 @@ async def _db_login_user(self, login: str, password: str) -> Optional[str]:
logging.error(f"Error logging in user {login}: {e}")
return None

async def _db_add_user(self, username: str, password: str, access: Dict[str, bool]) -> None:
async def db_add_user(self, username: str, password: str, access: Dict[str, bool]) -> None:
hashed_password = await self._hash_password(password)
packed_access = msgpack.packb(access)
try:
Expand All @@ -113,7 +113,7 @@ async def _db_add_user(self, username: str, password: str, access: Dict[str, boo
except Exception as e:
logging.error(f"Error adding user {username}: {e}")

async def _db_get_access(self, username: str) -> Optional[Dict[str, bool]]:
async def db_get_access(self, username: str) -> Optional[Dict[str, bool]]:
try:
async with self._connection.execute("SELECT access FROM users WHERE username = ?", (username,)) as cursor:
row = await cursor.fetchone()
Expand All @@ -124,22 +124,22 @@ async def _db_get_access(self, username: str) -> Optional[Dict[str, bool]]:
logging.error(f"Error getting access for user {username}: {e}")
return None

async def _db_delete_user(self, username: str) -> None:
async def db_delete_user(self, username: str) -> None:
try:
await self._connection.execute("DELETE FROM users WHERE username = ?", (username,))
await self._connection.commit()
except Exception as e:
logging.error(f"Error deleting user {username}: {e}")

async def _db_change_password(self, username: str, new_password: str) -> None:
async def db_change_password(self, username: str, new_password: str) -> None:
hashed_password = await self._hash_password(new_password)
try:
await self._connection.execute("UPDATE users SET password = ? WHERE username = ?", (hashed_password, username))
await self._connection.commit()
except Exception as e:
logging.error(f"Error changing password for user {username}: {e}")

async def _db_change_access(self, username: str, new_access: Optional[Dict[str, bool]] = None) -> None:
async def db_change_access(self, username: str, new_access: Optional[Dict[str, bool]] = None) -> None:
if username == "owner":
new_access = {"full_access": True}

Expand Down Expand Up @@ -169,7 +169,7 @@ async def _req_auth(self, reader: StreamReader, writer: StreamWriter) -> str:
await self.send_data(writer, {"action": "log", "log_type": "error", "msg": "Data must contain 'login' and 'password'."})
return None

return await self._db_login_user(user_data['login'], user_data['password'])
return await self.db_login_user(user_data['login'], user_data['password'])

except asyncio.TimeoutError:
await self.send_data(writer, {"action": "log", "log_type": "error", "msg": "Timeout error."})
Expand Down
30 changes: 15 additions & 15 deletions Tests/Server.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,45 +21,45 @@ async def asyncTearDown(self):
shutil.rmtree(self.db_path)

async def test_init_db(self):
access = await self.server._db_get_access('owner')
access = await self.server.db_get_access('owner')
self.assertIsNotNone(access)
self.assertIn('full_access', access)
self.assertTrue(access['full_access'])

async def test_db_add_user(self):
await self.server._db_add_user('test_user', 'password123', {'read': True})
access = await self.server._db_get_access('test_user')
await self.server.db_add_user('test_user', 'password123', {'read': True})
access = await self.server.db_get_access('test_user')
self.assertIsNotNone(access)
self.assertIn('read', access)
self.assertTrue(access['read'])

async def test_db_login_user(self):
await self.server._db_add_user('test_user', 'password123', {'read': True})
login_result = await self.server._db_login_user('test_user', 'password123')
await self.server.db_add_user('test_user', 'password123', {'read': True})
login_result = await self.server.db_login_user('test_user', 'password123')
self.assertEqual(login_result, 'test_user')

wrong_login_result = await self.server._db_login_user('test_user', 'wrongpassword')
wrong_login_result = await self.server.db_login_user('test_user', 'wrongpassword')
self.assertIsNone(wrong_login_result)

async def test_db_change_password(self):
await self.server._db_add_user('test_user', 'password123', {'read': True})
await self.server._db_change_password('test_user', 'newpassword123')
login_result = await self.server._db_login_user('test_user', 'newpassword123')
await self.server.db_add_user('test_user', 'password123', {'read': True})
await self.server.db_change_password('test_user', 'newpassword123')
login_result = await self.server.db_login_user('test_user', 'newpassword123')
self.assertEqual(login_result, 'test_user')

async def test_db_change_access(self):
await self.server._db_add_user('test_user', 'password123', {'read': True})
await self.server._db_change_access('test_user', {'write': True})
access = await self.server._db_get_access('test_user')
await self.server.db_add_user('test_user', 'password123', {'read': True})
await self.server.db_change_access('test_user', {'write': True})
access = await self.server.db_get_access('test_user')
self.assertIsNotNone(access)
self.assertIn('write', access)
self.assertTrue(access['write'])
self.assertNotIn('read', access)

async def test_db_delete_user(self):
await self.server._db_add_user('test_user', 'password123', {'read': True})
await self.server._db_delete_user('test_user')
access = await self.server._db_get_access('test_user')
await self.server.db_add_user('test_user', 'password123', {'read': True})
await self.server.db_delete_user('test_user')
access = await self.server.db_get_access('test_user')
self.assertIsNone(access)

async def test_send_receive_data(self):
Expand Down

0 comments on commit 5b5342b

Please sign in to comment.