Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assert allow field during node field updates #975

Closed
Jotschi opened this issue Dec 18, 2019 · 5 comments
Closed

Assert allow field during node field updates #975

Jotschi opened this issue Dec 18, 2019 · 5 comments

Comments

@Jotschi
Copy link
Contributor

Jotschi commented Dec 18, 2019

I noticed that the allow field will currently not be validated strictly in the API.

We should add the same checks that have also been added via #431

@karowin
Copy link
Contributor

karowin commented May 15, 2020

HI, @Jotschi can you please add some more details to this, like steps to reproduce and test this.

@karowin
Copy link
Contributor

karowin commented May 19, 2020

HI, @Jotschi could you please respond to my query above.

@Jotschi
Copy link
Contributor Author

Jotschi commented May 19, 2020

@karowin

I quickly sketched a test that should help you. The test should assert for the …NotAllowed cases that an error is thrown when a node field gets updated with a node that uses a different schema compared to the allowed value in the schema of the node that gets updated.
The test currently passes and expects a pass - you would need to change the assertion for this case.

You can use the call function to expect a specific error. Example:

call(() -> client().createNode(PROJECT_NAME, request), BAD_REQUEST, "node_no_languagecode_specified");

I assume a new I18n entry also needs to be added with a meaningful message in case of failing allow checks.

Besides the test there are two places which need to be updated:

  1. Similar to this check a check on the node's schema name needs to be performed against the provided schema
  1. For lists this has to be checked for each individual entry:
    https://github.com/gentics/mesh/blob/dev/core/src/main/java/com/gentics/mesh/core/data/node/field/list/impl/NodeGraphFieldListImpl.java#L88
package com.gentics.mesh.core.schema;

import static com.gentics.mesh.test.TestDataProvider.PROJECT_NAME;
import static com.gentics.mesh.test.TestSize.FULL;
import static com.gentics.mesh.test.context.ElasticsearchTestMode.TRACKING;

import java.util.Collections;

import org.junit.Before;
import org.junit.Test;

import com.gentics.mesh.core.rest.node.FieldMapImpl;
import com.gentics.mesh.core.rest.node.NodeCreateRequest;
import com.gentics.mesh.core.rest.node.field.Field;
import com.gentics.mesh.core.rest.node.field.impl.NodeFieldImpl;
import com.gentics.mesh.core.rest.node.field.list.impl.NodeFieldListImpl;
import com.gentics.mesh.core.rest.node.field.list.impl.NodeFieldListItemImpl;
import com.gentics.mesh.core.rest.schema.FieldSchema;
import com.gentics.mesh.core.rest.schema.impl.ListFieldSchemaImpl;
import com.gentics.mesh.core.rest.schema.impl.NodeFieldSchemaImpl;
import com.gentics.mesh.core.rest.schema.impl.SchemaCreateRequest;
import com.gentics.mesh.core.rest.schema.impl.SchemaResponse;
import com.gentics.mesh.test.context.AbstractMeshTest;
import com.gentics.mesh.test.context.MeshTestSetting;

@MeshTestSetting(elasticsearch = TRACKING, testSize = FULL, startServer = true)
public class SchemaAllowNodeFieldTest extends AbstractMeshTest {
	private String nodeUuid;

	@Before
	public void setUp() throws Exception {
		nodeUuid = tx(() -> folder("2015").getUuid());
	}

	private void createSchema(FieldSchema field) {
		field.setName("testField");

		SchemaCreateRequest req = new SchemaCreateRequest();
		req.setName("test");
		req.setFields(Collections.singletonList(field));

		SchemaResponse response = client().createSchema(req).blockingGet();
		client().assignSchemaToProject(PROJECT_NAME, response.getUuid()).blockingAwait();
	}

	private void createNode(Field field) {
		NodeCreateRequest req = new NodeCreateRequest();
		req.setLanguage("en");
		req.setSchemaName("test");
		req.setParentNodeUuid(nodeUuid);
		FieldMapImpl fieldMap = new FieldMapImpl();
		fieldMap.put("testField", field);
		req.setFields(fieldMap);

		client().createNode(PROJECT_NAME, req).blockingAwait();
	}

	private void runTest(FieldSchema schemaField, Field nodeField) {
		createSchema(schemaField);
		createNode(nodeField);
	}

	@Test
	public void node() {
		runTest(
			new NodeFieldSchemaImpl().setAllowedSchemas("test"),
			new NodeFieldImpl().setUuid(nodeUuid));
	}

	@Test
	public void nodeNotAllowed() {
		runTest(
			new NodeFieldSchemaImpl().setAllowedSchemas("test2"),
			new NodeFieldImpl().setUuid(nodeUuid));
	}

	@Test
	public void nodeList() {
		runTest(
			new ListFieldSchemaImpl().setListType("node").setAllowedSchemas("test"),
			new NodeFieldListImpl().setItems(Collections.singletonList(new NodeFieldListItemImpl().setUuid(nodeUuid))));
	}

	@Test
	public void nodeListNotAllowed() {
		runTest(
			new ListFieldSchemaImpl().setListType("node").setAllowedSchemas("test2"),
			new NodeFieldListImpl().setItems(Collections.singletonList(new NodeFieldListItemImpl().setUuid(nodeUuid))));
	}

}

@karowin
Copy link
Contributor

karowin commented May 20, 2020

@Jotschi
Thanks for your input.
I have committed the code on my branch for now. We wanted to double-check whether our understanding is correct and we are on the right path.
karowin@f496083

We are not able to get the SchemaReference from the Nodefield.

@Jotschi
Copy link
Contributor Author

Jotschi commented May 20, 2020

@karowin Yes. That looks correct from a quick glance.

Regarding your question:
karowin@f496083#diff-e6c0eec23073a1cddf0310bd2ed5f0b9R102

I think you use the wrong object here. The nodeField is the REST POJO which was passed to this handler when the field gets updated. You however want to check whether the node that was loaded via the reference is allowed.

Node node = ac.getProject().getNodeRoot().findByUuid(nodeField.getUuid());

You can get the schema name via:

node.getSchemaContainer().getName();

Avoiding loading the node would be nice but is not possible at the moment. I hope this helps.

You can revert the changes to NodeFieldImp. If possible also avoid white space changes. Those sometimes happen when the IDE uses a different format compared to that which was previously used. I can handle those also when merging the PR. No big issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants