|
1 | 1 | package com.linkedin.datahub.graphql.resolvers.incident; |
2 | 2 |
|
| 3 | +import static com.linkedin.datahub.graphql.TestUtils.getMockAllowContextForResource; |
3 | 4 | import static org.testng.Assert.*; |
4 | 5 |
|
5 | 6 | import com.linkedin.common.AuditStamp; |
6 | 7 | import com.linkedin.common.urn.Urn; |
7 | 8 | import com.linkedin.common.urn.UrnUtils; |
| 9 | +import com.linkedin.datahub.graphql.QueryContext; |
8 | 10 | import com.linkedin.datahub.graphql.generated.IncidentPriority; |
9 | 11 | import com.linkedin.datahub.graphql.generated.IncidentStatusInput; |
10 | 12 | import com.linkedin.incident.IncidentAssigneeArray; |
11 | 13 | import com.linkedin.incident.IncidentStage; |
12 | 14 | import com.linkedin.incident.IncidentState; |
13 | 15 | import com.linkedin.incident.IncidentStatus; |
| 16 | +import com.linkedin.metadata.authorization.PoliciesConfig; |
| 17 | +import com.linkedin.metadata.utils.SchemaFieldUtils; |
14 | 18 | import java.util.Arrays; |
15 | 19 | import java.util.Collections; |
16 | 20 | import java.util.List; |
|
19 | 23 | public class IncidentUtilsTest { |
20 | 24 |
|
21 | 25 | private static final Urn TEST_USER_URN = UrnUtils.getUrn("urn:li:corpuser:test"); |
| 26 | + private static final Urn TEST_DATASET_URN = |
| 27 | + UrnUtils.getUrn("urn:li:dataset:(urn:li:dataPlatform:hive,SampleTable,PROD)"); |
| 28 | + private static final Urn TEST_OTHER_DATASET_URN = |
| 29 | + UrnUtils.getUrn("urn:li:dataset:(urn:li:dataPlatform:hive,OtherTable,PROD)"); |
| 30 | + private static final Urn TEST_CHART_URN = UrnUtils.getUrn("urn:li:chart:(looker,baz)"); |
| 31 | + private static final Urn TEST_SCHEMA_FIELD_URN = |
| 32 | + SchemaFieldUtils.generateSchemaFieldUrn(TEST_DATASET_URN, "user_id"); |
| 33 | + private static final String EDIT_INCIDENTS = |
| 34 | + PoliciesConfig.EDIT_ENTITY_INCIDENTS_PRIVILEGE.getType(); |
22 | 35 |
|
23 | 36 | @Test |
24 | 37 | public void testMapIncidentPriorityWithNull() { |
@@ -103,4 +116,57 @@ public void testMapIncidentStatusWithValidInput() { |
103 | 116 | assertEquals(status.getStage(), IncidentStage.INVESTIGATION); |
104 | 117 | assertEquals(status.getMessage(), "Issue resolved"); |
105 | 118 | } |
| 119 | + |
| 120 | + @Test |
| 121 | + public void testGetIncidentAuthorizationUrnRemapsSchemaFieldToParent() { |
| 122 | + assertEquals( |
| 123 | + IncidentUtils.getIncidentAuthorizationUrn(TEST_SCHEMA_FIELD_URN), TEST_DATASET_URN); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void testGetIncidentAuthorizationUrnLeavesOtherEntitiesAlone() { |
| 128 | + assertEquals(IncidentUtils.getIncidentAuthorizationUrn(TEST_DATASET_URN), TEST_DATASET_URN); |
| 129 | + assertEquals(IncidentUtils.getIncidentAuthorizationUrn(TEST_CHART_URN), TEST_CHART_URN); |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + public void testGetIncidentAuthorizationUrnFallsBackWhenFieldUrnDoesNotParse() { |
| 134 | + // A schemaField URN missing its field path cannot yield a parent, so the check stays on the |
| 135 | + // URN it was given rather than failing open. |
| 136 | + Urn malformed = UrnUtils.getUrn("urn:li:schemaField:notAFieldUrn"); |
| 137 | + assertEquals(IncidentUtils.getIncidentAuthorizationUrn(malformed), malformed); |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + public void testEditIncidentOnSchemaFieldAllowedByParentDatasetPolicy() { |
| 142 | + QueryContext context = |
| 143 | + getMockAllowContextForResource(TEST_USER_URN.toString(), EDIT_INCIDENTS, TEST_DATASET_URN); |
| 144 | + assertTrue(IncidentUtils.isAuthorizedToEditIncidentForResource(TEST_SCHEMA_FIELD_URN, context)); |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + public void testEditIncidentOnSchemaFieldDeniedWhenParentPolicyDoesNotMatch() { |
| 149 | + QueryContext context = |
| 150 | + getMockAllowContextForResource( |
| 151 | + TEST_USER_URN.toString(), EDIT_INCIDENTS, TEST_OTHER_DATASET_URN); |
| 152 | + assertFalse( |
| 153 | + IncidentUtils.isAuthorizedToEditIncidentForResource(TEST_SCHEMA_FIELD_URN, context)); |
| 154 | + } |
| 155 | + |
| 156 | + @Test |
| 157 | + public void testEditIncidentOnSchemaFieldUsesNonDatasetParent() { |
| 158 | + Urn chartFieldUrn = SchemaFieldUtils.generateSchemaFieldUrn(TEST_CHART_URN, "user_id"); |
| 159 | + QueryContext context = |
| 160 | + getMockAllowContextForResource(TEST_USER_URN.toString(), EDIT_INCIDENTS, TEST_CHART_URN); |
| 161 | + assertTrue(IncidentUtils.isAuthorizedToEditIncidentForResource(chartFieldUrn, context)); |
| 162 | + } |
| 163 | + |
| 164 | + @Test |
| 165 | + public void testEditIncidentOnOtherEntitiesIsUnchanged() { |
| 166 | + QueryContext context = |
| 167 | + getMockAllowContextForResource(TEST_USER_URN.toString(), EDIT_INCIDENTS, TEST_DATASET_URN); |
| 168 | + assertTrue(IncidentUtils.isAuthorizedToEditIncidentForResource(TEST_DATASET_URN, context)); |
| 169 | + // The remap is scoped to schemaField: a dataset policy still does not reach other entities. |
| 170 | + assertFalse(IncidentUtils.isAuthorizedToEditIncidentForResource(TEST_CHART_URN, context)); |
| 171 | + } |
106 | 172 | } |
0 commit comments