def test_datarequest_delete(self, organization_id, accepted_dataset_id):
        # Configure the mock
        datarequest = test_data._generate_basic_datarequest()
        datarequest.organization_id = organization_id
        datarequest.accepted_dataset_id = accepted_dataset_id
        actions.db.DataRequest.get.return_value = [datarequest]

        default_pkg = {"pkg": 1}
        default_org = {"org": 2}
        default_user = {"user": 3}
        test_data._initialize_basic_actions(actions, default_user, default_org, default_pkg)

        # Call the function
        expected_data_dict = test_data.delete_request_data.copy()
        result = actions.datarequest_delete(self.context, test_data.delete_request_data)

        # Assertions
        actions.db.init_db.assert_called_once_with(self.context["model"])
        actions.tk.check_access.assert_called_once_with(constants.DATAREQUEST_DELETE, self.context, expected_data_dict)
        self.context["session"].delete.assert_called_once_with(datarequest)
        self.context["session"].commit.assert_called_once_with()

        org = default_org if organization_id else None
        pkg = default_pkg if accepted_dataset_id else None
        self._check_basic_response(datarequest, result, default_user, org, pkg)
Пример #2
0
    def test_datarequest_delete(self, organization_id, accepted_dataset_id):
        # Configure the mock
        datarequest = test_data._generate_basic_datarequest()
        datarequest.organization_id = organization_id
        datarequest.accepted_dataset_id = accepted_dataset_id
        actions.db.DataRequest.get.return_value = [datarequest]

        default_pkg = {'pkg': 1}
        default_org = {'org': 2}
        default_user = {'user': 3}
        test_data._initialize_basic_actions(actions, default_user, default_org, default_pkg)

        # Call the function
        expected_data_dict = test_data.delete_request_data.copy()
        result = actions.datarequest_delete(self.context, test_data.delete_request_data)

        # Assertions
        actions.db.init_db.assert_called_once_with(self.context['model'])
        actions.tk.check_access.assert_called_once_with(constants.DATAREQUEST_DELETE, self.context, expected_data_dict)
        self.context['session'].delete.assert_called_once_with(datarequest)
        self.context['session'].commit.assert_called_once_with()

        org = default_org if organization_id else None
        pkg = default_pkg if accepted_dataset_id else None
        self._check_basic_response(datarequest, result, default_user, org, pkg)
Пример #3
0
    def test_datarequest_update(self,
                                title_checked,
                                organization_id=None,
                                accepted_dataset_id=None):
        # Configure the mock
        datarequest = test_data._generate_basic_datarequest()
        datarequest.organization_id = organization_id
        datarequest.accepted_dataset_id = accepted_dataset_id
        # Title should not be checked when it does not change
        datarequest.title = test_data.create_request_data[
            'title'] + 'a' if title_checked else test_data.create_request_data[
                'title']
        actions.db.DataRequest.get.return_value = [datarequest]

        # Mock actions
        default_pkg = {'pkg': 1}
        default_org = {'org': 2}
        default_user = {'user': 3}
        test_data._initialize_basic_actions(actions, default_user, default_org,
                                            default_pkg)

        # Store previous user (needed to check that it has not been modified)
        previous_user_id = datarequest.user_id

        # Call the action
        result = actions.datarequest_update(self.context,
                                            test_data.update_request_data)

        # Assertions
        actions.db.init_db.assert_called_once_with(self.context['model'])
        actions.tk.check_access.assert_called_once_with(
            constants.DATAREQUEST_UPDATE, self.context,
            test_data.update_request_data)
        actions.db.DataRequest.get.assert_called_once_with(
            id=test_data.update_request_data['id'])
        expected_context = self.context.copy()
        expected_context['avoid_existing_title_check'] = not title_checked
        actions.validator.validate_datarequest.assert_called_once_with(
            expected_context, test_data.update_request_data)

        self.context['session'].add.assert_called_once_with(datarequest)
        self.context['session'].commit.assert_called_once()

        # Check the object stored in the database
        self.assertEquals(previous_user_id, datarequest.user_id)
        self.assertEquals(test_data.update_request_data['title'],
                          datarequest.title)
        self.assertEquals(test_data.update_request_data['description'],
                          datarequest.description)
        self.assertEquals(test_data.update_request_data['organization_id'],
                          datarequest.organization_id)

        # Check the result
        org = default_org if organization_id else None
        pkg = default_pkg if accepted_dataset_id else None
        self._check_basic_response(datarequest, result, default_user, org, pkg)
Пример #4
0
    def test_datarequest_show_found_closed(self, organization_id, accepted_dataset_id):
        datarequest = test_data._generate_basic_datarequest()
        datarequest.organization_id = organization_id
        datarequest.accepted_dataset_id = 'example_uuidv4_package'
        datarequest.close_time = datetime.datetime.now()
        datarequest.closed = True

        org_checked = True if organization_id else False
        pkg_checked = True if accepted_dataset_id else False

        self._test_datarequest_show_found(datarequest, org_checked, pkg_checked)
    def test_datarequest_show_found_closed(self, organization_id, accepted_dataset_id):
        datarequest = test_data._generate_basic_datarequest()
        datarequest.organization_id = organization_id
        datarequest.accepted_dataset_id = "example_uuidv4_package"
        datarequest.close_time = datetime.datetime.now()
        datarequest.closed = True

        org_checked = True if organization_id else False
        pkg_checked = True if accepted_dataset_id else False

        self._test_datarequest_show_found(datarequest, org_checked, pkg_checked)
    def test_close_datarequest(self, data, expected_accepted_ds, organization_id):
        # Configure the mock
        current_time = self._datetime.datetime.now()
        actions.datetime.datetime.now = MagicMock(return_value=current_time)
        datarequest = test_data._generate_basic_datarequest()
        datarequest.organization_id = organization_id
        datarequest.accepted_dataset_id = None
        actions.db.DataRequest.get.return_value = [datarequest]

        send_mail_patch = patch('ckanext.datarequests.actions._send_mail')
        send_mail_mock = send_mail_patch.start()
        self.addCleanup(send_mail_patch.stop)

        get_datarequest_involved_users_patch = patch('ckanext.datarequests.actions._get_datarequest_involved_users')
        get_datarequest_involved_users_mock = get_datarequest_involved_users_patch.start()
        self.addCleanup(get_datarequest_involved_users_patch.stop)

        # Mock actions
        default_pkg = {'pkg': 1}

        default_org = {'org': 2}

        default_user = {'user': 3}
        test_data._initialize_basic_actions(actions, default_user, default_org, default_pkg)

        # Call the function
        expected_data_dict = data.copy()
        result = actions.close_datarequest(self.context, data)

        # Assertions
        actions.db.init_db.assert_called_once_with(self.context['model'])
        actions.tk.check_access.assert_called_once_with(constants.CLOSE_DATAREQUEST, self.context, expected_data_dict)
        self.context['session'].add.assert_called_once_with(datarequest)
        self.context['session'].commit.assert_called_once_with()

        # The data object returned by the database has been modified appropriately
        self.assertTrue(datarequest.closed)
        self.assertEquals(datarequest.close_time, current_time)
        if expected_accepted_ds:
            self.assertEquals(datarequest.accepted_dataset_id, data['accepted_dataset_id'])
        else:
            self.assertIsNone(datarequest.accepted_dataset_id)

        org = default_org if organization_id else None
        pkg = default_pkg if expected_accepted_ds else None
        self._check_basic_response(datarequest, result, default_user, org, pkg)

        send_mail_mock.assert_called_once_with(get_datarequest_involved_users_mock.return_value, 'close_datarequest', result)
        get_datarequest_involved_users_mock.assert_called_once_with(self.context, result)
    def test_update_datarequest(self, title_checked, organization_id=None, accepted_dataset_id=None):
        # Configure the mock
        datarequest = test_data._generate_basic_datarequest()
        datarequest.organization_id = organization_id
        datarequest.accepted_dataset_id = accepted_dataset_id
        # Title should not be checked when it does not change
        datarequest.title = (test_data.create_request_data['title'] + 'a'
                             if title_checked
                             else test_data.create_request_data['title'])
        actions.db.DataRequest.get.return_value = [datarequest]

        # Mock actions
        default_pkg = {'pkg': 1}

        default_org = {'org': 2}

        default_user = {'user': 3}
        test_data._initialize_basic_actions(actions, default_user, default_org, default_pkg)

        # Store previous user (needed to check that it has not been modified)
        previous_user_id = datarequest.user_id

        # Call the action
        result = actions.update_datarequest(self.context, test_data.update_request_data)

        # Assertions
        actions.db.init_db.assert_called_once_with(self.context['model'])
        actions.tk.check_access.assert_called_once_with(constants.UPDATE_DATAREQUEST,
                                                        self.context,
                                                        test_data.update_request_data)
        actions.db.DataRequest.get.assert_called_once_with(id=test_data.update_request_data['id'])
        expected_context = self.context.copy()
        expected_context['avoid_existing_title_check'] = not title_checked
        actions.validator.validate_datarequest.assert_called_once_with(expected_context, test_data.update_request_data)

        self.context['session'].add.assert_called_once_with(datarequest)
        self.context['session'].commit.assert_called_once()

        # Check the object stored in the database
        self.assertEquals(previous_user_id, datarequest.user_id)
        self.assertEquals(test_data.update_request_data['title'], datarequest.title)
        self.assertEquals(test_data.update_request_data['description'], datarequest.description)
        self.assertEquals(test_data.update_request_data['organization_id'], datarequest.organization_id)

        # Check the result
        org = default_org if organization_id else None
        pkg = default_pkg if accepted_dataset_id else None
        self._check_basic_response(datarequest, result, default_user, org, pkg)
Пример #8
0
    def test_datarequest_close(self, data, expected_accepted_ds,
                               organization_id):
        # Configure the mock
        current_time = self._datetime.datetime.now()
        actions.datetime.datetime.now = MagicMock(return_value=current_time)
        datarequest = test_data._generate_basic_datarequest()
        datarequest.organization_id = organization_id
        datarequest.accepted_dataset_id = None
        actions.db.DataRequest.get.return_value = [datarequest]

        # Mock actions
        default_pkg = {'pkg': 1}
        default_org = {'org': 2}
        default_user = {'user': 3}
        test_data._initialize_basic_actions(actions, default_user, default_org,
                                            default_pkg)

        # Call the function
        expected_data_dict = data.copy()
        result = actions.datarequest_close(self.context, data)

        # Assertions
        actions.db.init_db.assert_called_once_with(self.context['model'])
        actions.tk.check_access.assert_called_once_with(
            constants.DATAREQUEST_CLOSE, self.context, expected_data_dict)
        self.context['session'].add.assert_called_once_with(datarequest)
        self.context['session'].commit.assert_called_once_with()

        # The data object returned by the database has been modified appropriately
        self.assertTrue(datarequest.closed)
        self.assertEquals(datarequest.close_time, current_time)
        if expected_accepted_ds:
            self.assertEquals(datarequest.accepted_dataset_id,
                              data['accepted_dataset_id'])
        else:
            self.assertIsNone(datarequest.accepted_dataset_id)

        org = default_org if organization_id else None
        pkg = default_pkg if expected_accepted_ds else None
        self._check_basic_response(datarequest, result, default_user, org, pkg)
    def test_datarequest_close(self, data, expected_accepted_ds, organization_id):
        # Configure the mock
        current_time = self._datetime.datetime.now()
        actions.datetime.datetime.now = MagicMock(return_value=current_time)
        datarequest = test_data._generate_basic_datarequest()
        datarequest.organization_id = organization_id
        datarequest.accepted_dataset_id = None
        actions.db.DataRequest.get.return_value = [datarequest]

        # Mock actions
        default_pkg = {"pkg": 1}
        default_org = {"org": 2}
        default_user = {"user": 3}
        test_data._initialize_basic_actions(actions, default_user, default_org, default_pkg)

        # Call the function
        expected_data_dict = data.copy()
        result = actions.datarequest_close(self.context, data)

        # Assertions
        actions.db.init_db.assert_called_once_with(self.context["model"])
        actions.tk.check_access.assert_called_once_with(constants.DATAREQUEST_CLOSE, self.context, expected_data_dict)
        self.context["session"].add.assert_called_once_with(datarequest)
        self.context["session"].commit.assert_called_once_with()

        # The data object returned by the database has been modified appropriately
        self.assertTrue(datarequest.closed)
        self.assertEquals(datarequest.close_time, current_time)
        if expected_accepted_ds:
            self.assertEquals(datarequest.accepted_dataset_id, data["accepted_dataset_id"])
        else:
            self.assertIsNone(datarequest.accepted_dataset_id)

        org = default_org if organization_id else None
        pkg = default_pkg if expected_accepted_ds else None
        self._check_basic_response(datarequest, result, default_user, org, pkg)
Пример #10
0
 def test_datarequest_show_found_no_org_open(self):
     datarequest = test_data._generate_basic_datarequest()
     datarequest['organization_id'] = None
     self._test_datarequest_show_found(datarequest, False, False)
Пример #11
0
 def test_datarequest_show_found_org_open(self):
     datarequest = test_data._generate_basic_datarequest()
     self._test_datarequest_show_found(datarequest, True, False)
Пример #12
0
 def test_datarequest_show_found_no_org_open(self):
     datarequest = test_data._generate_basic_datarequest()
     datarequest["organization_id"] = None
     self._test_datarequest_show_found(datarequest, False, False)
Пример #13
0
 def test_datarequest_show_found_org_open(self):
     datarequest = test_data._generate_basic_datarequest()
     self._test_datarequest_show_found(datarequest, True, False)