def test_duplicate_pid_sets_error_data(
        self,
        mock_get_dict_value_from_key_list,
        mock_curate_data_structure_api_get_by_id,
        mock_send_get_request,
    ):
        mock_data_1 = {"id": "mock_id_1", "dict_content": {}}
        mock_data_2 = {"id": "mock_id_2", "dict_content": {}}
        mock_send_get_request.return_value = MockResponse(
            text=json.dumps(mock_data_1), status_code=200)
        mock_curate_data_structure_api_get_by_id.return_value = {
            "data": mock_data_2
        }
        mock_get_dict_value_from_key_list.return_value = str(
            MockPID(prefix="mock_prefix_2", value="mock_record_2"))

        mock_data = MockPID()
        mock_curate_data_structure_id = ObjectId()
        mock_user = create_mock_user("1")

        self.module._init_prefix_and_record(str(mock_data),
                                            mock_curate_data_structure_id,
                                            mock_user)

        self.assertEquals(self.module.error_data, str(mock_data))
    def test_invalid_format_keeps_value(self):
        mock_data = str(MockPID(value="$invalid_value"))
        mock_curate_data_structure_id = ObjectId()
        mock_user = create_mock_user("1")

        self.module._init_prefix_and_record(mock_data,
                                            mock_curate_data_structure_id,
                                            mock_user)
        self.assertIsNotNone(self.module.default_value)
    def test_invalid_format_sets_error_data(self):
        mock_data = str(MockPID(value="$invalid_value"))
        mock_curate_data_structure_id = ObjectId()
        mock_user = create_mock_user("1")

        self.module._init_prefix_and_record(mock_data,
                                            mock_curate_data_structure_id,
                                            mock_user)
        self.assertEquals(self.module.error_data, mock_data)
    def test_settings_host_url_uses_default_system(self):
        mock_data = str(MockPID(provider="mock_not_default_provider"))
        mock_curate_data_structure_id = ObjectId()
        mock_user = create_mock_user("1")

        self.module._init_prefix_and_record(mock_data,
                                            mock_curate_data_structure_id,
                                            mock_user)
        self.assertEquals(self.module.error_data, mock_data)
    def test_settings_host_url_not_equals_to_record_host_url_sets_value_to_none(
            self):
        mock_data = str(MockPID(provider="mock_not_default_provider"))
        mock_curate_data_structure_id = ObjectId()
        mock_user = create_mock_user("1")

        self.module._init_prefix_and_record(mock_data,
                                            mock_curate_data_structure_id,
                                            mock_user)
        self.assertIsNone(self.module.default_value)
    def test_settings_host_url_does_not_end_with_slash(self):
        mock_data = str(MockPID())
        mock_data = "%s/" % mock_data if mock_data[-1] != "/" else mock_data

        mock_curate_data_structure_id = ObjectId()
        mock_user = create_mock_user("1")

        self.module._init_prefix_and_record(mock_data,
                                            mock_curate_data_structure_id,
                                            mock_user)
        self.assertEquals(self.module.error_data, mock_data)
    def test_correct_url_sets_correct_value(self,
                                            mock_curate_data_structure_api):
        mock_curate_data_structure_api.return_value = None
        mock_data = MockPID()
        mock_curate_data_structure_id = ObjectId()
        mock_user = create_mock_user("1")

        self.module._init_prefix_and_record(str(mock_data),
                                            mock_curate_data_structure_id,
                                            mock_user)
        self.assertEquals(self.module.default_value, mock_data.value)
    def test_editing_existing_record_sets_error_data_to_none(
        self,
        mock_get_dict_value_from_key_list,
        mock_curate_data_structure_api_get_by_id,
        mock_send_get_request,
    ):
        mock_data = {"id": "mock_id", "dict_content": {}}
        mock_send_get_request.return_value = MockResponse(
            text=json.dumps(mock_data), status_code=200)
        mock_curate_data_structure_api_get_by_id.return_value = {
            "data": mock_data
        }
        mock_get_dict_value_from_key_list.return_value = str(MockPID())

        self.module._init_prefix_and_record(*self.setDefaultTestData(
            as_string=True))

        self.assertIsNone(self.module.error_data)
    def setDefaultTestData(as_string=False):
        mock_data = MockPID() if not as_string else str(MockPID())
        mock_curate_data_structure_id = ObjectId()
        mock_user = create_mock_user("1")

        return [mock_data, mock_curate_data_structure_id, mock_user]