@@ -387,6 +387,61 @@ def test_create_command_without_connection(self):
387
387
)
388
388
389
389
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
+
390
445
class TestConnectionApi (
391
446
TestAdminMixin , AuthenticationMixin , TestCase , CreateConnectionsMixin
392
447
):
0 commit comments