예제 #1
0
    def test_enabling(self):
        full = Policy.objects.get(name = "full")
        upon_request = Policy.objects.get(name = 'upon-request')

        self.device_1.policy = full
        self.device_1.save()

        self.assertTrue(self.device_1.is_allowed()) 

        # Test out partial unblocking
        self.device_1.policy = upon_request
        self.device_1.save()

        self.assertFalse(self.device_1.is_allowed()) 

        # Create an approval, and see that the device is enabled
        ta = TemporaryApproval(device=self.device_1)
        ta.set_parameters(10)
        ta.save()

        self.assertTrue(self.device_1.is_allowed()) 
예제 #2
0
def enable_device(request):
    """
    The user wants to temporarily enable a device
    """
    logger = logging.getLogger('dri.custom')
    output = {"success": True,
              "message": ""}
    if request.method != 'POST':
        output['success'] = False
        output["message"] = "Must use a POST"
        return output
    mac_address = request.POST.get("mac_address").upper()
    provided_duration = request.POST.get("duration", "30")
    logger.info("IN ENABLE-DEVICES: %s" % mac_address)
    try:
        duration = int(provided_duration)
        device = None
        logger.info("Duration: %s" % duration)
        try:
            device = Device.objects.get(mac_address=mac_address)
            logger.info("device found. Device name: %s" % device.name)
        except Device.DoesNotExist:
            return {"success": False,
                    "message": "can't look that record up: (%s)" % mac_address
                   }
        temporary_approval = TemporaryApproval()
        temporary_approval.device = device
        temporary_approval.set_parameters(duration)
        temporary_approval.save()
        output['message'] = "Saved"
        logger.info("Saving approval")
    except ValueError:
        msg = "Bad duration provided: (%s)" % provided_duration
        logger.info(msg)
        output["success"] = False
        output['message'] = msg
    log_object(output, "MY OUTPUT")
    return output