From 8abdac5ade33c71125eb3f7409cb6dafe8aff278 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sun, 8 Jan 2023 16:52:52 +0300 Subject: Fix ActivityPub redirects --- src/main/java/com/juick/www/controllers/Site.java | 11 ++++++----- src/test/java/com/juick/server/tests/ServerTests.java | 19 ++++++++++++------- 2 files changed, 18 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/main/java/com/juick/www/controllers/Site.java b/src/main/java/com/juick/www/controllers/Site.java index cbc14496..1dc4c338 100644 --- a/src/main/java/com/juick/www/controllers/Site.java +++ b/src/main/java/com/juick/www/controllers/Site.java @@ -43,6 +43,7 @@ import org.springframework.security.web.WebAttributes; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.*; +import org.springframework.web.servlet.view.RedirectView; import javax.inject.Inject; import java.net.URLEncoder; @@ -477,14 +478,14 @@ public class Site { return "views/pm_sent"; } - @GetMapping(value = "/{uname}/{mid}", produces = { Context.LD_JSON_MEDIA_TYPE, Context.ACTIVITY_MEDIA_TYPE, - Context.ACTIVITYSTREAMS_PROFILE_MEDIA_TYPE, MediaType.APPLICATION_JSON_VALUE }) - public String threadRedirect(@PathVariable int mid) { + @GetMapping(value = "/{uname}/{mid}", produces = { MediaType.APPLICATION_JSON_VALUE, Context.LD_JSON_MEDIA_TYPE, Context.ACTIVITY_MEDIA_TYPE, + Context.ACTIVITYSTREAMS_PROFILE_MEDIA_TYPE }) + public RedirectView threadRedirect(@PathVariable String uname, @PathVariable int mid) { String linkedDataLocation = "/n/" + mid + "-0"; - return "redirect:" + linkedDataLocation; + return new RedirectView(linkedDataLocation); } - @GetMapping(value = "/{uname}/{mid}", consumes = MediaType.ALL_VALUE, produces = MediaType.TEXT_HTML_VALUE) + @GetMapping(value = "/{uname}/{mid}", produces = { MediaType.TEXT_HTML_VALUE, MediaType.ALL_VALUE }) protected String threadAction(@ModelAttribute User visitor, ModelMap model, @PathVariable String uname, @PathVariable int mid, @CookieValue(name = "sape_cookie", required = false, defaultValue = StringUtils.EMPTY) String sapeCookie) { diff --git a/src/test/java/com/juick/server/tests/ServerTests.java b/src/test/java/com/juick/server/tests/ServerTests.java index eff78c1d..80627275 100644 --- a/src/test/java/com/juick/server/tests/ServerTests.java +++ b/src/test/java/com/juick/server/tests/ServerTests.java @@ -46,7 +46,6 @@ import com.juick.www.WebApp; import com.juick.www.ad.models.Site; import com.juick.www.api.SystemActivity; import com.juick.www.api.Users; -import com.juick.www.api.activity.Profile; import com.juick.www.api.activity.helpers.ProfileUriBuilder; import com.juick.www.api.activity.model.Context; import com.juick.www.api.activity.model.activities.*; @@ -64,7 +63,6 @@ import io.pebbletemplates.pebble.PebbleEngine; import io.pebbletemplates.pebble.error.PebbleException; import io.pebbletemplates.pebble.template.PebbleTemplate; import jakarta.servlet.http.Cookie; -import jakarta.servlet.http.HttpServletRequest; import jakarta.xml.bind.JAXBContext; import jakarta.xml.bind.JAXBException; import jakarta.xml.bind.Marshaller; @@ -92,8 +90,6 @@ import org.springframework.core.io.Resource; import org.springframework.http.*; import org.springframework.http.client.ClientHttpRequestFactory; import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.mock.http.client.MockClientHttpRequest; -import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpSession; import org.springframework.mock.web.MockMultipartFile; import org.springframework.test.context.TestPropertySource; @@ -1651,7 +1647,8 @@ public class ServerTests { @Test public void accountUrlShouldBeExposedOverWebfinger() throws Exception { - mockMvc.perform(get("/.well-known/webfinger?resource=acct:ugnich@localhost")).andExpect(status().isOk()) + mockMvc.perform(get("/.well-known/webfinger?resource=acct:ugnich@localhost") + .accept("application/jrd+json")).andExpect(status().isOk()) .andExpect(jsonPath("$.subject", is("acct:ugnich@localhost"))) .andExpect(jsonPath("$.links", hasSize(1))) .andExpect(jsonPath("$.links[0].href", is("http://localhost:8080/u/ugnich"))); @@ -2097,7 +2094,8 @@ public class ServerTests { String signatureString = signatureService.addSignature(ugnichPerson, testHost, "GET", meUri, requestDate, StringUtils.EMPTY); - MvcResult me = mockMvc.perform(get("/api/me").header("Host", testHost).header("Date", requestDate) + MvcResult me = mockMvc.perform(get("/api/me") + .header("Host", testHost).header("Date", requestDate) .header( "Signature", signatureString)).andExpect(status().isOk()).andReturn(); User meUser = jsonMapper.readValue(me.getResponse().getContentAsString(), User.class); assertThat(meUser, is(ugnich)); @@ -2651,7 +2649,7 @@ public class ServerTests { @Test public void invalidMediaTypeTest() throws Exception { - mockMvc.perform(get("/api/messages") + mockMvc.perform(get("/api/messages/discussions") .header("Accept", "application/xml")).andExpect(status().isBadRequest()) .andExpect(content().string("Invalid media type")); } @@ -2683,6 +2681,13 @@ public class ServerTests { .andExpect(redirectedUrl(ldUri)); mockMvc.perform(get(htmlUri).accept(MediaType.APPLICATION_JSON)).andExpect(status().is3xxRedirection()) .andExpect(redirectedUrl(ldUri)); + var mastodonAcceptHeader = "application/activity+json, application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\", text/html;q=0.1"; + mockMvc.perform(get(htmlUri).accept(mastodonAcceptHeader)).andExpect(status().is3xxRedirection()) + .andExpect(redirectedUrl(ldUri)); + var webclientHeader = "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"; + mockMvc.perform(get(htmlUri).accept(webclientHeader)).andExpect(status().isOk()); + mockMvc.perform(get(htmlUri)).andExpect(status().isOk()); + mockMvc.perform(get(ldUri)).andExpect(status().isOk()); } @Test -- cgit v1.2.3