aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/com/juick/server
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2021-08-22 10:57:02 +0300
committerGravatar Vitaly Takmazov2021-08-22 10:57:02 +0300
commitd918967281652ead0130c5dbef663e82003d4393 (patch)
treeb4bdd09ee9a2f86e0aae7ee6cdf21672a2f0fd31 /src/test/java/com/juick/server
parent1d395b762746948c4ba9897c0ff1e5be0aaaf6db (diff)
ActivityPub: handle user deletion for suspended users
Diffstat (limited to 'src/test/java/com/juick/server')
-rw-r--r--src/test/java/com/juick/server/MockDeleteListener.java30
-rw-r--r--src/test/java/com/juick/server/tests/ServerTests.java40
2 files changed, 69 insertions, 1 deletions
diff --git a/src/test/java/com/juick/server/MockDeleteListener.java b/src/test/java/com/juick/server/MockDeleteListener.java
new file mode 100644
index 00000000..d060d865
--- /dev/null
+++ b/src/test/java/com/juick/server/MockDeleteListener.java
@@ -0,0 +1,30 @@
+/*
+ * Copyright (C) 2008-2021, Juick
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package com.juick.server;
+
+import com.juick.service.activities.DeleteUserEvent;
+import org.springframework.context.ApplicationListener;
+
+import javax.annotation.Nonnull;
+
+public class MockDeleteListener implements ApplicationListener<DeleteUserEvent> {
+ @Override
+ public void onApplicationEvent(@Nonnull DeleteUserEvent event) {
+
+ }
+}
diff --git a/src/test/java/com/juick/server/tests/ServerTests.java b/src/test/java/com/juick/server/tests/ServerTests.java
index 2f60f5be..db1449f2 100644
--- a/src/test/java/com/juick/server/tests/ServerTests.java
+++ b/src/test/java/com/juick/server/tests/ServerTests.java
@@ -48,6 +48,7 @@ import com.juick.www.api.webfinger.model.Account;
import com.juick.www.api.xnodeinfo2.model.NodeInfo;
import com.juick.www.WebApp;
import com.juick.service.*;
+import com.juick.service.activities.DeleteUserEvent;
import com.juick.service.activities.UpdateEvent;
import com.juick.service.component.SystemEvent;
import com.juick.test.util.MockUtils;
@@ -229,6 +230,10 @@ public class ServerTests {
private Resource testappResponse;
@Value("classpath:snapshots/activity/testfollow.json")
private Resource testfollowRequest;
+ @Value("classpath:snapshots/activity/testdelete.json")
+ private Resource testDeleteRequest;
+ @Value("classpath:snapshots/activity/test_suspended_user.json")
+ private Resource testSuspendedUserResponse;
@Value("classpath:snapshots/email/subscription.html")
private Resource testSubscriptionHtmlEmail;
@Value("classpath:snapshots/email/private.html")
@@ -2077,7 +2082,7 @@ public class ServerTests {
}
@Test
- public void federatedUserDeletionFlow() throws Exception {
+ public void federatedUserDeletionFlowWhenItIsGone() throws Exception {
String deleteJsonStr = IOUtils.toString(new ClassPathResource("delete_user.json").getURI(),
StandardCharsets.UTF_8);
Delete delete = jsonMapper.readValue(deleteJsonStr, Delete.class);
@@ -2096,6 +2101,39 @@ public class ServerTests {
apClient.setRequestFactory(originalRequestFactory);
}
+ @MockBean
+ private MockDeleteListener deleteListener;
+ @Captor
+ protected ArgumentCaptor<DeleteUserEvent> deleteEventCaptor;
+
+ @Test
+ public void federatedUserDeletionFlowWhenItIsSuspended() throws Exception {
+ String deleteJsonStr = IOUtils.toString(testDeleteRequest.getInputStream(), StandardCharsets.UTF_8);
+ Delete delete = jsonMapper.readValue(deleteJsonStr, Delete.class);
+ ClientHttpRequestFactory originalRequestFactory = apClient.getRequestFactory();
+ MockRestServiceServer restServiceServer = MockRestServiceServer.createServer(apClient);
+ restServiceServer.expect(times(2), requestTo(delete.getObject().getId()))
+ .andRespond(withSuccess(IOUtils.toString(testSuspendedUserResponse.getInputStream(), StandardCharsets.UTF_8), MediaType.APPLICATION_JSON));
+ Person testuser = (Person) signatureManager.getContext(URI.create(delete.getObject().getId())).get();
+ Instant now = Instant.now();
+ String testRequestDate = DateFormattersHolder.getHttpDateFormatter().format(now);
+ String inboxUri = "/api/inbox";
+ byte[] digest = MessageDigest.getInstance("SHA-256").digest(deleteJsonStr.getBytes());
+ String digestHeader = "SHA-256=" + new String(Base64.encodeBase64(digest));
+ String testSignatureString = signatureManager.addSignature(testuser, "localhost", "POST", inboxUri,
+ testRequestDate, digestHeader, testKeystoreManager);
+ mockMvc.perform(post(inboxUri).contentType(ACTIVITY_MEDIA_TYPE).content(deleteJsonStr)
+ .header("Host", "localhost")
+ .header("Date", testRequestDate)
+ .header("Digest", digestHeader)
+ .header("Signature", testSignatureString))
+ .andExpect(status().isAccepted());
+ apClient.setRequestFactory(originalRequestFactory);
+ Mockito.verify(deleteListener, Mockito.times(1)).onApplicationEvent(deleteEventCaptor.capture());
+ DeleteUserEvent receivedEvent = deleteEventCaptor.getValue();
+ assertThat(receivedEvent.getUserUri(), is(testuser.getId()));
+ }
+
@Test
@Order(2)
public void handleIncorrectCertificates() throws Exception {