def test_close_valid(self):
        context = {}
        accepted_ds_id = 'accepted_ds_uuidv4'
        package_validator = validator.tk.get_validator.return_value

        # Call the function
        validator.validate_datarequest_closing(context, {'id': 'dr_id', 'accepted_dataset_id': accepted_ds_id})

        # Check that the correct validator is called
        validator.tk.get_validator.assert_called_once_with('package_name_exists')

        # Check that the package existence has been checked
        package_validator.assert_called_once_with(accepted_ds_id, context)
    def test_close_valid(self):
        context = {}
        accepted_ds_id = "accepted_ds_uuidv4"
        package_validator = validator.tk.get_validator.return_value

        # Call the function
        validator.validate_datarequest_closing(
            context, {"id": "dr_id", "accepted_dataset_id": accepted_ds_id}
        )

        # Check that the correct validator is called
        validator.tk.get_validator.assert_called_once_with("package_name_exists")

        # Check that the package existence has been checked
        package_validator.assert_called_once_with(accepted_ds_id, context)
    def test_close_invalid_accepted_dataset(self):
        context = {}
        accepted_ds_id = 'accepted_ds_uuidv4'
        package_validator = validator.tk.get_validator.return_value
        package_validator.side_effect = self._tk.ValidationError({'Dataset': 'Invalid Dataset'})

        # Call the function (exception expected)
        with self.assertRaises(self._tk.ValidationError) as c:
            validator.validate_datarequest_closing(context, {'id': 'dr_id', 'accepted_dataset_id': accepted_ds_id})

        # Check that the correct validator is called
        validator.tk.get_validator.assert_called_once_with('package_name_exists')

        # Check that the validator has been properly called
        package_validator.assert_called_once_with(accepted_ds_id, context)
        self.assertEquals({'Accepted Dataset': ['Dataset not found']},
                          c.exception.error_dict)
    def test_close_invalid_accepted_dataset(self):
        context = {}
        accepted_ds_id = "accepted_ds_uuidv4"
        package_validator = validator.tk.get_validator.return_value
        package_validator.side_effect = self._tk.ValidationError(
            {"Dataset": "Invalid Dataset"}
        )

        # Call the function (exception expected)
        with self.assertRaises(self._tk.ValidationError) as c:
            validator.validate_datarequest_closing(
                context, {"id": "dr_id", "accepted_dataset_id": accepted_ds_id}
            )

        # Check that the correct validator is called
        validator.tk.get_validator.assert_called_once_with("package_name_exists")

        # Check that the validator has been properly called
        package_validator.assert_called_once_with(accepted_ds_id, context)
        self.assertEquals(
            {"Accepted Dataset": ["Dataset not found"]}, c.exception.error_dict
        )
Example #5
0
def close_datarequest(original_action, context, data_dict):
    """
    Action to close a data request. Access rights will be checked before
    closing the data request. If the user is not allowed, a NotAuthorized
    exception will be risen.

    Data QLD modification
    Will send email notification to the data request creator

    :param id: The ID of the data request to be closed
    :type id: string

    :param accepted_dataset_id: The ID of the dataset accepted as solution
        for this data request
    :type accepted_dataset_id: string

    :returns: A dict with the data request (id, user_id, title, description,
        organization_id, open_time, accepted_dataset, close_time, closed,
        followers)
    :rtype: dict

    """

    model = context['model']
    session = context['session']
    datarequest_id = data_dict.get('id', '')

    # Check id
    if not datarequest_id:
        raise tk.ValidationError(tk._('Data Request ID has not been included'))

    # Init the data base
    db.init_db(model)

    # Check access
    tk.check_access(constants.CLOSE_DATAREQUEST, context, data_dict)

    # Get the data request
    result = db.DataRequest.get(id=datarequest_id)
    if not result:
        raise tk.ObjectNotFound(
            tk._('Data Request %s not found in the data base') %
            datarequest_id)

    # Validate data
    validator.validate_datarequest_closing(context, data_dict)

    data_req = result[0]

    # Was the data request previously closed?
    if data_req.closed:
        raise tk.ValidationError([tk._('This Data Request is already closed')])

    data_req.closed = True
    data_req.accepted_dataset_id = data_dict.get('accepted_dataset_id') or None
    data_req.close_time = datetime.datetime.utcnow()
    _undictize_datarequest_closing_circumstances(data_req, data_dict)

    session.add(data_req)
    session.commit()

    datarequest_dict = _dictize_datarequest(data_req)

    # Mailing
    users = [data_req.user_id]
    _send_mail(users, 'close_datarequest_creator', datarequest_dict,
               'Data Request Closed Send Email')

    return datarequest_dict