Skip to content

Commit

Permalink
lib mpsks added session as parameter
Browse files Browse the repository at this point in the history
added the db session now as parameter
removed kargs for determining api calls
moved check for number of mpsks clients to api

Signed-off-by: Alex <robokatze182@gmx.de>
#744
  • Loading branch information
agmes4 committed Sep 21, 2024
1 parent 2335014 commit 3a6ef56
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 37 deletions.
26 changes: 12 additions & 14 deletions pycroft/lib/mpsk_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,19 @@
# This file is part of the Pycroft project and licensed under the terms of
# the Apache License, Version 2.0. See the LICENSE file for details
from pycroft.model.mpsk_client import MPSKClient
from pycroft.model.types import AmountExceededError
from pycroft.model.user import User
from pycroft.model.session import session
from pycroft.lib.logging import log_user_event
from pycroft.helpers.i18n import deferred_gettext


def mpsk_delete(host: MPSKClient, processor: User) -> None:
message = deferred_gettext("Deleted host '{}'.").format(host.name)
log_user_event(author=processor, user=host.owner, message=message.to_json())
def mpsk_delete(session, mpsk_client: MPSKClient, processor: User) -> None:

Check failure on line 10 in pycroft/lib/mpsk_client.py

View workflow job for this annotation

GitHub Actions / python-lint

Error

Function is missing a type annotation for one or more arguments [no-untyped-def]
message = deferred_gettext("Deleted mpsk client '{}'.").format(mpsk_client.name)
log_user_event(author=processor, user=mpsk_client.owner, message=message.to_json())

session.delete(host)
session.delete(mpsk_client)


def change_mac(client: MPSKClient, mac: str, processor: User) -> MPSKClient:
def change_mac(session, client: MPSKClient, mac: str, processor: User) -> MPSKClient:

Check failure on line 17 in pycroft/lib/mpsk_client.py

View workflow job for this annotation

GitHub Actions / python-lint

Error

Function is missing a type annotation for one or more arguments [no-untyped-def]
"""
This method will change the mac address of the given mpsks client to the new
mac address.
Expand All @@ -31,24 +29,21 @@ def change_mac(client: MPSKClient, mac: str, processor: User) -> MPSKClient:
message = deferred_gettext("Changed MAC address from {} to {}.").format(old_mac, mac)
if client.owner:
log_user_event(message.to_json(), processor, client.owner)
session.add(client)
return client


def mpsk_client_create(owner: User, name: str, mac: str, processor: User, api=False) -> MPSKClient:
def mpsk_client_create(session, owner: User, name: str, mac: str, processor: User) -> MPSKClient:

Check failure on line 36 in pycroft/lib/mpsk_client.py

View workflow job for this annotation

GitHub Actions / python-lint

Error

Function is missing a type annotation for one or more arguments [no-untyped-def]
"""
creates a mpsks client for a given user with a mac address.
:param session: session to use with the database.
:param owner: the user who initiated the mac address change.
:param name: the name of the mpsks client.
:param mac: the new mac address.
:param processor: the user who initiated the mac address change.
:param api: whether to create an api client or not. If set Ture checks rather a user exceeds the maximum of clients (set to 10).
"""
if len(owner.mpsks) >= 30 and api:
raise AmountExceededError(
"the limit of added mpsks clients is exceeded", limit=30, actual=len(owner.mpsks)
)

client = MPSKClient(name=name, owner_id=owner.id, mac=mac)

session.add(client)
Expand All @@ -63,7 +58,9 @@ def mpsk_client_create(owner: User, name: str, mac: str, processor: User, api=Fa
return client


def mpsk_edit(client: MPSKClient, owner: User, name: str, mac: str, processor: User) -> None:
def mpsk_edit(

Check failure on line 61 in pycroft/lib/mpsk_client.py

View workflow job for this annotation

GitHub Actions / python-lint

Error

Function is missing a type annotation for one or more arguments [no-untyped-def]
session, client: MPSKClient, owner: User, name: str, mac: str, processor: User
) -> None:
if client.name != name:
message = deferred_gettext("Changed name of client '{}' to '{}'.").format(client.name, name)
client.name = name
Expand All @@ -87,3 +84,4 @@ def mpsk_edit(client: MPSKClient, owner: User, name: str, mac: str, processor: U
)
log_user_event(author=processor, user=owner, message=message.to_json())
client.mac = mac
session.add(client)
18 changes: 2 additions & 16 deletions tests/model/test_mpsk.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from pycroft.lib.mpsk_client import mpsk_client_create
from pycroft.model.mpsk_client import MPSKClient
from pycroft.model.types import InvalidMACAddressException, AmountExceededError
from pycroft.model.types import InvalidMACAddressException
from .. import factories


Expand Down Expand Up @@ -70,26 +70,12 @@ def test_names(self, session, user, name):
mpsk_client = MPSKClient(mac="00:00:00:00:00:00", name=name, owner=user)
assert mpsk_client.name == name

def test_exceeds_max_api(self, session, user):
mac = "00:00:00:00:00:"
for j in range(1, 4):
for i in range(10):
mac_client = mac + hex(j)[2:] + hex(i)[2:]
c = mpsk_client_create(user, "Hallo", mac_client, user, api=True)
user.mpsks.append(c)
session.flush()

for i in range(15):
mac_client = mac + "0" + hex(i)[2:]
with pytest.raises(AmountExceededError):
mpsk_client_create(user, "Hallo", mac_client, user, api=True)

def test_admin_exceeds(self, session, user):
mac = "00:00:00:00:00:"
for j in range(0, 4):
for i in range(15):
mac_client = mac + hex(j)[2:] + hex(i)[2:]
c = mpsk_client_create(user, "Hallo", mac_client, user)
c = mpsk_client_create(session, user, "Hallo", mac_client, user)
user.mpsks.append(c)
session.flush()

Expand Down
14 changes: 10 additions & 4 deletions web/api/v0/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,14 @@ def post(self, user_id: int, password: str, mac: str, name: str) -> ResponseRetu
user = get_authenticated_user(user_id, password)

try:
mpsk_client_create(user, mac, name, user, api=True)
# checks rather the user has all settable mpsks clients created
if len(user.mpsks) > current_app.config.get("MAX_MPSKS", 30):
abort(400, message="User has the maximum count of mpsk clients.")

if not user.wifi_password:
abort(400, message="Legacy wifi password change of password is required.")

mpsk_client_create(session, user, mac, name, user, api=True)

Check failure on line 352 in web/api/v0/__init__.py

View workflow job for this annotation

GitHub Actions / python-lint

Error

Unexpected keyword argument "api" for "mpsk_client_create" [call-arg]
session.session.commit()
except InvalidMACAddressException:
abort(400, message="Invalid MAC address.")
Expand Down Expand Up @@ -372,7 +379,7 @@ def post(self, user_id: int, mpsk_id: int, password: str) -> ResponseReturnValue
if not user == mpsk.owner:
abort(401, message="You are not the owner of the mpsk.")

mpsk_delete(mpsk, user)
mpsk_delete(session, mpsk, user)
session.session.commit()

return "mpsk client was deleted"
Expand Down Expand Up @@ -400,8 +407,7 @@ def post(
abort(404, message=f"User {user_id} does not own the mpsk client with the id {mpsks_id}")

try:
mpsk_edit(mpsk, user, name, mac, user)
session.session.add(mpsk)
mpsk_edit(session, mpsk, user, name, mac, user)
session.session.commit()
except InvalidMACAddressException:
abort(400, message="Invalid MAC address.")
Expand Down
8 changes: 5 additions & 3 deletions web/blueprints/mpskclient/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def default_response() -> ResponseReturnValue:
owner = session.session.get(User, form.owner_id.data)
with abort_on_error(default_response), session.session.begin_nested():
host = lib_mpsk.mpsk_client_create(
owner, form.name.data, form.mac.data, processor=current_user
session.session, owner, form.name.data, form.mac.data, processor=current_user
)
session.session.commit()

Expand Down Expand Up @@ -92,7 +92,7 @@ def default_response() -> ResponseReturnValue:
return default_response()

with abort_on_error(default_response), session.session.begin_nested():
lib_mpsk.mpsk_delete(mpsk, current_user)
lib_mpsk.mpsk_delete(session.session, mpsk, current_user)
session.session.commit()

flash("MPSK Client erfolgreich gelöscht.", "success")
Expand All @@ -117,7 +117,9 @@ def default_response() -> ResponseReturnValue:
return default_response()

with abort_on_error(default_response), session.session.begin_nested():
lib_mpsk.mpsk_edit(mpsk, mpsk.owner, form.name.data, form.mac.data, current_user)
lib_mpsk.mpsk_edit(
session.session, mpsk, mpsk.owner, form.name.data, form.mac.data, current_user
)
session.session.add(mpsk)
session.session.commit()
flash("MPSK Client erfolgreich bearbeitet.", "success")
Expand Down

0 comments on commit 3a6ef56

Please sign in to comment.