Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Installs whose number has been re-used as an NN shouldn't be able to get an NN of their own #802

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 20 additions & 18 deletions src/meshapi/tests/test_nn.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,25 +429,26 @@ def test_nn_valid_low_install_number_used_nn(self):
content_type="application/json",
)

code = 201
code = 409
self.assertEqual(
code,
response.status_code,
f"status code incorrect for test_nn_valid_install_number. Should be {code}, but got {response.status_code}",
)

resp_nn = json.loads(response.content.decode("utf-8"))["network_number"]
expected_nn = 101
self.assertEqual(
expected_nn,
resp_nn,
f"nn incorrect for test_nn_valid_install_number. Should be {expected_nn}, but got {resp_nn}",
json.loads(response.content.decode("utf-8")),
{
"detail": "Invalid install status for NN Assignment. "
"Re-submit the join form to create a new install number"
},
)

node_object = Node.objects.get(network_number=expected_nn)
self.assertEqual(1, len(Node.objects.all()))

self.install_obj_low.refresh_from_db()
self.assertEqual(self.install_obj_low.node, node_object)
self.assertEqual(node_object.status, Node.NodeStatus.PLANNED)
self.assertEqual(self.install_obj_low.node, None)
self.assertEqual(self.install_obj_low.status, Install.InstallStatus.NN_REASSIGNED)

@parameterized.expand(
[
Expand All @@ -469,25 +470,26 @@ def test_nn_valid_low_install_number_reassigned_but_unused_nn(self, status_to_ev
content_type="application/json",
)

code = 201
code = 409
self.assertEqual(
code,
response.status_code,
f"status code incorrect for test_nn_valid_install_number. Should be {code}, but got {response.status_code}",
)

resp_nn = json.loads(response.content.decode("utf-8"))["network_number"]
expected_nn = 101
self.assertEqual(
expected_nn,
resp_nn,
f"nn incorrect for test_nn_valid_install_number. Should be {expected_nn}, but got {resp_nn}",
json.loads(response.content.decode("utf-8")),
{
"detail": "Invalid install status for NN Assignment. "
"Re-submit the join form to create a new install number"
},
)

node_object = Node.objects.get(network_number=expected_nn)
self.assertEqual(0, len(Node.objects.all()))

self.install_obj_low.refresh_from_db()
self.assertEqual(self.install_obj_low.node, node_object)
self.assertEqual(node_object.status, Node.NodeStatus.PLANNED)
self.assertEqual(self.install_obj_low.node, None)
self.assertEqual(self.install_obj_low.status, status_to_eval)

@parameterized.expand(
[
Expand Down
13 changes: 13 additions & 0 deletions src/meshapi/views/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ class Meta:
),
"403": OpenApiResponse(form_err_response_schema, description="Incorrect or missing password"),
"404": OpenApiResponse(form_err_response_schema, description="Requested install number could not be found"),
"409": OpenApiResponse(form_err_response_schema, description="Requested install is in an invalid state"),
"500": OpenApiResponse(form_err_response_schema, description="Unexpected internal error"),
},
),
Expand Down Expand Up @@ -516,6 +517,18 @@ def network_number_assignment(request: Request) -> Response:

# First, we try to identify a Node object for this install if it doesn't already have one
if not nn_install.node:
# Don't allow old recycled or closed installs to have a network number assigned to them,
# since this would be very confusing. Installs in this status should re-submit the join
# form and try again with a new install number
if nn_install.status in [Install.InstallStatus.CLOSED, Install.InstallStatus.NN_REASSIGNED]:
return Response(
{
"detail": "Invalid install status for NN Assignment. "
"Re-submit the join form to create a new install number"
},
status=status.HTTP_409_CONFLICT,
)

# If the building on this install has a primary_node, then use that one
if nn_building.primary_node is not None:
logging.info(f"Reusing existing node ({nn_building.primary_node.id}) from building ({nn_building.id})")
Expand Down
Loading