Skip to content

Commit 883a5ea

Browse files
author
Alexandre Vincent
committed
[change] add TestCommandsApiNonAdmin to reveal bug on commands org field
1 parent 4ea68be commit 883a5ea

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

openwisp_controller/connection/tests/test_api.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,61 @@ def test_create_command_without_connection(self):
387387
)
388388

389389

390+
# The same tests, but with a normal user
391+
class TestCommandsApiNonAdmin(TestCommandsAPI):
392+
def setUp(self):
393+
# Organisation to manage devices
394+
org1 = self._get_org()
395+
# Admin for this organisation
396+
self.administrator = self._create_administrator(organizations=[org1])
397+
self.client.force_login(self.administrator)
398+
# Credentials for the same organisation
399+
cred1 = self._create_credentials(organization=org1)
400+
# Connection to a device with these credentials
401+
self.device_conn = self._create_device_connection(credentials=cred1)
402+
self.device_id = self.device_conn.device.id
403+
404+
def test_bearer_authentication(self):
405+
self.client.logout()
406+
command_obj = self._create_command(device_conn=self.device_conn)
407+
token = self._obtain_auth_token(username="administrator", password="tester")
408+
409+
with self.subTest("Test creating command"):
410+
url = self._get_path("device_command_list", self.device_id)
411+
payload = {
412+
"type": "custom",
413+
"input": {"command": "echo test"},
414+
}
415+
response = self.client.post(
416+
url,
417+
data=payload,
418+
content_type="application/json",
419+
HTTP_AUTHORIZATION=f"Bearer {token}",
420+
)
421+
self.assertEqual(response.status_code, 201)
422+
self.assertIn("id", response.data)
423+
424+
with self.subTest("Test retrieving command"):
425+
url = self._get_path(
426+
"device_command_details", self.device_id, command_obj.id
427+
)
428+
response = self.client.get(
429+
url,
430+
HTTP_AUTHORIZATION=f"Bearer {token}",
431+
)
432+
self.assertEqual(response.status_code, 200)
433+
self.assertIn("id", response.data)
434+
435+
with self.subTest("Test listing command"):
436+
url = self._get_path("device_command_list", self.device_id)
437+
response = self.client.get(
438+
url,
439+
HTTP_AUTHORIZATION=f"Bearer {token}",
440+
)
441+
self.assertEqual(response.status_code, 200)
442+
self.assertEqual(len(response.data["results"]), 2)
443+
444+
390445
class TestConnectionApi(
391446
TestAdminMixin, AuthenticationMixin, TestCase, CreateConnectionsMixin
392447
):

0 commit comments

Comments
 (0)