Skip to content

Commit

Permalink
[#59157] server: tests: test-groups: Add testing multiple groups per …
Browse files Browse the repository at this point in the history
…device
  • Loading branch information
msobkowski committed Jun 7, 2024
1 parent 4f85922 commit 7815b7a
Showing 1 changed file with 65 additions and 15 deletions.
80 changes: 65 additions & 15 deletions server/tests/test-groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
GROUPS_ENDPOINT = f"{SERVER}/api/v1/groups"
DUMMY_DEVICE_ID = 1
DBPATH = "test-db.db"

GROUP_DEFAULT_PRIORITY = 25

@pytest.fixture()
def process():
Expand All @@ -36,18 +36,35 @@ def test_groups(process: subprocess.Popen):
assert resp.status_code == 200, "fetching all groups works"
assert resp.content == b"[]", "empty database returns no groups"

# Creating a group
test_json = {
# Creating groups
test_json_group_default_priority = {
"metadata": {
"description": "An example group"
},
"priority": "25"

}
resp = requests.post(GROUPS_ENDPOINT, json=test_json)

resp = requests.post(GROUPS_ENDPOINT, json=test_json_group_default_priority)
assert resp.status_code == 200, "creating a group works"
group_data = resp.json()
assert group_data["metadata"] == test_json["metadata"], "metadata was added to the group"
assert group_data["metadata"] == test_json_group_default_priority["metadata"], "metadata was added to the group"
assert group_data["priority"] == GROUP_DEFAULT_PRIORITY, "priority was set to the default value"

resp = requests.post(GROUPS_ENDPOINT, json=test_json_group_default_priority)
assert resp.status_code == 200, "creating a group with the same priority as an existing group works"
group_data = resp.json()
assert group_data["priority"] == GROUP_DEFAULT_PRIORITY, "priority was set to the default value"

test_json_group_high_priority = {
"metadata": {
"description": "An example group"
},
"priority": 0
}

resp = requests.post(GROUPS_ENDPOINT, json=test_json_group_high_priority)
assert resp.status_code == 200, "creating a group with an explicitly set priority works"
group_data = resp.json()
assert group_data["priority"] == test_json_group_high_priority["priority"], "priority was set to the requested value"

# Fetching information about a single group
resp = requests.get(f"{GROUPS_ENDPOINT}/{group_data['id']}")
Expand All @@ -61,35 +78,68 @@ def test_groups(process: subprocess.Popen):
resp = requests.get(f"{GROUPS_ENDPOINT}/{group_data['id']}")
assert resp.status_code == 404, "deleted group no longer exists"

# Create a group that will be used for testing package assignment
test_group = requests.post(GROUPS_ENDPOINT, json=test_json).json()
# Create groups that will be used for testing package assignment
test_group_default_priority_1 = requests.post(GROUPS_ENDPOINT, json=test_json_group_default_priority).json()
test_group_default_priority_2 = requests.post(GROUPS_ENDPOINT, json=test_json_group_default_priority).json()
test_group_high_priority = requests.post(GROUPS_ENDPOINT, json=test_json_group_high_priority).json()

# Assign a device to the group (created in test-specific DB file `test.db`)
resp = requests.patch(f"{GROUPS_ENDPOINT}/{test_group['id']}/devices", json={
resp = requests.patch(f"{GROUPS_ENDPOINT}/{test_group_default_priority_1['id']}/devices", json={
"add": [DUMMY_DEVICE_ID],
"remove": []
})
assert resp.status_code == 200, "modified device assignment successfully"
resp = requests.get(f"{GROUPS_ENDPOINT}/{test_group['id']}").json()
resp = requests.get(f"{GROUPS_ENDPOINT}/{test_group_default_priority_1['id']}").json()
assert DUMMY_DEVICE_ID in resp["devices"], "device ID appears in fetched group information"

# Trying to assign a second time should return an error
resp = requests.patch(f"{GROUPS_ENDPOINT}/{test_group['id']}/devices", json={
resp = requests.patch(f"{GROUPS_ENDPOINT}/{test_group_default_priority_1['id']}/devices", json={
"add": [DUMMY_DEVICE_ID],
"remove": []
})
assert resp.status_code == 409, "signal trying to re-add an already existing device"

# Assign a device to a group with different priority
resp = requests.patch(f"{GROUPS_ENDPOINT}/{test_group_high_priority['id']}/devices", json={
"add": [DUMMY_DEVICE_ID],
"remove": []
})
assert resp.status_code == 200, "modified device assignment successfully"
resp = requests.get(f"{GROUPS_ENDPOINT}/{test_group_high_priority['id']}").json()
assert DUMMY_DEVICE_ID in resp["devices"], "device ID appears in fetched group information"

# Assign a device to a group with the same priority as other group the device is already assigned to should fail
resp = requests.patch(f"{GROUPS_ENDPOINT}/{test_group_default_priority_2['id']}/devices", json={
"add": [DUMMY_DEVICE_ID],
"remove": []
})
assert resp.status_code == 409, "signal trying to assign a device to a group with the same priority as other group the device is already assigned to"

# Changing a group priority
resp = requests.post(f"{GROUPS_ENDPOINT}/{test_group_default_priority_1['id']}/priority", json={
"priority": 10
})
assert resp.status_code == 200, "changing priority of a group works"

# Changing a group priority to the same value as priority of another group assigned to the same device should fail
resp = requests.post(f"{GROUPS_ENDPOINT}/{test_group_default_priority_1['id']}/priority", json={
"priority": 0
})
assert resp.status_code == 409, "signal trying to set group priority to the same value as priority of another group assigned to the same device"

# Deleting a group should fail, because a device is assigned to the group
resp = requests.delete(f"{GROUPS_ENDPOINT}/{test_group['id']}")
resp = requests.delete(f"{GROUPS_ENDPOINT}/{test_group_default_priority_1['id']}")
assert resp.status_code == 409, "signal trying to remove group while a device is assigned"

# Now remove the device from the group
resp = requests.patch(f"{GROUPS_ENDPOINT}/{test_group['id']}/devices", json={
resp = requests.patch(f"{GROUPS_ENDPOINT}/{test_group_default_priority_1['id']}/devices", json={
"add": [],
"remove": [DUMMY_DEVICE_ID]
})
assert resp.status_code == 200, "removing devices from group works"
resp = requests.get(f"{GROUPS_ENDPOINT}/{test_group['id']}").json()
resp = requests.get(f"{GROUPS_ENDPOINT}/{test_group_default_priority_1['id']}").json()
assert DUMMY_DEVICE_ID not in resp["devices"], "device ID no longer appears in fetched group information"

# Deleting a group
resp = requests.delete(f"{GROUPS_ENDPOINT}/{test_group_default_priority_1['id']}")
assert resp.status_code == 200, "deleting a group with no devices works"

0 comments on commit 7815b7a

Please sign in to comment.