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

fix(posts): add deletePost GraphQL endpoint #6813

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import com.datahub.authentication.AuthenticationConfiguration;
import com.datahub.authentication.group.GroupService;
import com.datahub.authentication.invite.InviteTokenService;
import com.datahub.authentication.post.PostService;
import com.datahub.authentication.token.StatefulTokenService;
import com.datahub.authentication.user.NativeUserService;
import com.datahub.authorization.AuthorizationConfiguration;
import com.datahub.authorization.role.RoleService;
import com.datahub.authentication.post.PostService;
import com.google.common.collect.ImmutableList;
import com.linkedin.common.VersionedUrn;
import com.linkedin.common.urn.Urn;
Expand Down Expand Up @@ -179,13 +179,13 @@
import com.linkedin.datahub.graphql.resolvers.mutate.UpdateNameResolver;
import com.linkedin.datahub.graphql.resolvers.mutate.UpdateParentNodeResolver;
import com.linkedin.datahub.graphql.resolvers.mutate.UpdateUserSettingResolver;
import com.linkedin.datahub.graphql.resolvers.settings.user.UpdateCorpUserViewsSettingsResolver;
import com.linkedin.datahub.graphql.resolvers.operation.ReportOperationResolver;
import com.linkedin.datahub.graphql.resolvers.policy.DeletePolicyResolver;
import com.linkedin.datahub.graphql.resolvers.policy.GetGrantedPrivilegesResolver;
import com.linkedin.datahub.graphql.resolvers.policy.ListPoliciesResolver;
import com.linkedin.datahub.graphql.resolvers.policy.UpsertPolicyResolver;
import com.linkedin.datahub.graphql.resolvers.post.CreatePostResolver;
import com.linkedin.datahub.graphql.resolvers.post.DeletePostResolver;
import com.linkedin.datahub.graphql.resolvers.post.ListPostsResolver;
import com.linkedin.datahub.graphql.resolvers.recommendation.ListRecommendationsResolver;
import com.linkedin.datahub.graphql.resolvers.role.AcceptRoleResolver;
Expand All @@ -198,10 +198,11 @@
import com.linkedin.datahub.graphql.resolvers.search.SearchAcrossEntitiesResolver;
import com.linkedin.datahub.graphql.resolvers.search.SearchAcrossLineageResolver;
import com.linkedin.datahub.graphql.resolvers.search.SearchResolver;
import com.linkedin.datahub.graphql.resolvers.step.BatchGetStepStatesResolver;
import com.linkedin.datahub.graphql.resolvers.step.BatchUpdateStepStatesResolver;
import com.linkedin.datahub.graphql.resolvers.settings.user.UpdateCorpUserViewsSettingsResolver;
import com.linkedin.datahub.graphql.resolvers.settings.view.GlobalViewsSettingsResolver;
import com.linkedin.datahub.graphql.resolvers.settings.view.UpdateGlobalViewsSettingsResolver;
import com.linkedin.datahub.graphql.resolvers.step.BatchGetStepStatesResolver;
import com.linkedin.datahub.graphql.resolvers.step.BatchUpdateStepStatesResolver;
import com.linkedin.datahub.graphql.resolvers.tag.CreateTagResolver;
import com.linkedin.datahub.graphql.resolvers.tag.DeleteTagResolver;
import com.linkedin.datahub.graphql.resolvers.tag.SetTagColorResolver;
Expand Down Expand Up @@ -854,6 +855,7 @@ private void configureMutationResolvers(final RuntimeWiring.Builder builder) {
.dataFetcher("createInviteToken", new CreateInviteTokenResolver(this.inviteTokenService))
.dataFetcher("acceptRole", new AcceptRoleResolver(this.roleService, this.inviteTokenService))
.dataFetcher("createPost", new CreatePostResolver(this.postService))
.dataFetcher("deletePost", new DeletePostResolver(this.postService))
.dataFetcher("batchUpdateStepStates", new BatchUpdateStepStatesResolver(this.entityClient))
.dataFetcher("createView", new CreateViewResolver(this.viewService))
.dataFetcher("updateView", new UpdateViewResolver(this.viewService))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.linkedin.datahub.graphql.resolvers.post;

import com.datahub.authentication.Authentication;
import com.datahub.authentication.post.PostService;
import com.linkedin.common.urn.Urn;
import com.linkedin.common.urn.UrnUtils;
import com.linkedin.datahub.graphql.QueryContext;
import com.linkedin.datahub.graphql.authorization.AuthorizationUtils;
import com.linkedin.datahub.graphql.exception.AuthorizationException;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import java.util.concurrent.CompletableFuture;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;


@Slf4j
@RequiredArgsConstructor
public class DeletePostResolver implements DataFetcher<CompletableFuture<Boolean>> {
private final PostService _postService;

@Override
public CompletableFuture<Boolean> get(final DataFetchingEnvironment environment) throws Exception {
final QueryContext context = environment.getContext();

if (!AuthorizationUtils.canCreateGlobalAnnouncements(context)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the future, once the scope of Posts expands, we will need to change this permission. Maybe it's better to check the owner of the post.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This addresses the use case for now, and we can get it in immediately. But I'd like to discuss this one offline a bit more.

throw new AuthorizationException(
"Unauthorized to delete posts. Please contact your DataHub administrator if this needs corrective action.");
}

final Urn postUrn = UrnUtils.getUrn(environment.getArgument("urn"));
final Authentication authentication = context.getAuthentication();

return CompletableFuture.supplyAsync(() -> {
try {
return _postService.deletePost(postUrn, authentication);
} catch (Exception e) {
throw new RuntimeException("Failed to create a new post", e);
}
});
}
}
5 changes: 5 additions & 0 deletions datahub-graphql-core/src/main/resources/entity.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,11 @@ type Mutation {
"""
createPost(input: CreatePostInput!): Boolean

"""
Delete a post
"""
deletePost(urn: String!): Boolean

"""
Create a new DataHub View (Saved Filter)
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.linkedin.datahub.graphql.resolvers.post;

import com.datahub.authentication.Authentication;
import com.datahub.authentication.post.PostService;
import com.linkedin.common.urn.Urn;
import com.linkedin.common.urn.UrnUtils;
import com.linkedin.datahub.graphql.QueryContext;
import graphql.schema.DataFetchingEnvironment;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import static com.linkedin.datahub.graphql.TestUtils.*;
import static org.mockito.Mockito.*;
import static org.testng.Assert.*;


public class DeletePostResolverTest {
private static final String POST_URN_STRING = "urn:li:post:123";
private PostService _postService;
private DeletePostResolver _resolver;
private DataFetchingEnvironment _dataFetchingEnvironment;
private Authentication _authentication;

@BeforeMethod
public void setupTest() throws Exception {
_postService = mock(PostService.class);
_dataFetchingEnvironment = mock(DataFetchingEnvironment.class);
_authentication = mock(Authentication.class);

_resolver = new DeletePostResolver(_postService);
}

@Test
public void testNotAuthorizedFails() {
QueryContext mockContext = getMockDenyContext();
when(_dataFetchingEnvironment.getContext()).thenReturn(mockContext);

assertThrows(() -> _resolver.get(_dataFetchingEnvironment).join());
}

@Test
public void testDeletePost() throws Exception {
Urn postUrn = UrnUtils.getUrn(POST_URN_STRING);

QueryContext mockContext = getMockAllowContext();
when(_dataFetchingEnvironment.getContext()).thenReturn(mockContext);
when(mockContext.getAuthentication()).thenReturn(_authentication);
when(_dataFetchingEnvironment.getArgument(eq("urn"))).thenReturn(POST_URN_STRING);
when(_postService.deletePost(eq(postUrn), eq(_authentication))).thenReturn(true);

assertTrue(_resolver.get(_dataFetchingEnvironment).join());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.linkedin.common.Media;
import com.linkedin.common.MediaType;
import com.linkedin.common.url.Url;
import com.linkedin.common.urn.Urn;
import com.linkedin.entity.client.EntityClient;
import com.linkedin.metadata.key.PostKey;
import com.linkedin.mxe.MetadataChangeProposal;
Expand Down Expand Up @@ -68,4 +69,13 @@ public boolean createPost(@Nonnull String postType, @Nonnull PostContent postCon

return true;
}

public boolean deletePost(@Nonnull Urn postUrn, @Nonnull Authentication authentication)
throws RemoteInvocationException {
if (!_entityClient.exists(postUrn, authentication)) {
throw new RuntimeException("Post does not exist");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: I'd prefer to see an DataHubGraphQLException with a NOT_FOUND code (404)

}
_entityClient.deleteEntity(postUrn, authentication);
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import com.linkedin.common.Media;
import com.linkedin.common.MediaType;
import com.linkedin.common.url.Url;
import com.linkedin.common.urn.Urn;
import com.linkedin.common.urn.UrnUtils;
import com.linkedin.entity.client.EntityClient;
import com.linkedin.post.PostContent;
import com.linkedin.post.PostContentType;
Expand All @@ -19,6 +21,7 @@


public class PostServiceTest {
private static final Urn POST_URN = UrnUtils.getUrn("urn:li:post:123");
private static final MediaType POST_MEDIA_TYPE = MediaType.IMAGE;
private static final String POST_MEDIA_LOCATION =
"https://datahubproject.io/img/datahub-logo-color-light-horizontal.svg";
Expand Down Expand Up @@ -63,4 +66,16 @@ public void testCreatePost() throws RemoteInvocationException {
_postService.createPost(POST_TYPE.toString(), POST_CONTENT, SYSTEM_AUTHENTICATION);
verify(_entityClient, times(1)).ingestProposal(any(), eq(SYSTEM_AUTHENTICATION));
}

@Test
public void testDeletePostDoesNotExist() throws RemoteInvocationException {
when(_entityClient.exists(eq(POST_URN), eq(SYSTEM_AUTHENTICATION))).thenReturn(false);
assertThrows(() -> _postService.deletePost(POST_URN, SYSTEM_AUTHENTICATION));
}

@Test
public void testDeletePost() throws RemoteInvocationException {
when(_entityClient.exists(eq(POST_URN), eq(SYSTEM_AUTHENTICATION))).thenReturn(true);
assertTrue(_postService.deletePost(POST_URN, SYSTEM_AUTHENTICATION));
}
}