feat(pages): open the three public landing-page reads in the gateway
Only GET of a published page, a page image and an active company's branding, each anchored to its exact shape. Writes still need a token. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
This commit is contained in:
1 parent
dcbe8071b5
commit
bf6f2c8696
3 files changed
+87
-1
No files matched your search
+82
@@ -0,0 +1,82 @@
|
||||
package com.solvway.gateway.filter;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.solvway.gateway.model.AuthAppKey;
|
||||
import com.solvway.gateway.model.AuthAppKeyMap;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
|
||||
import org.springframework.mock.web.server.MockServerWebExchange;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/** Public landing pages open three reads without a user token: a published page, an image and a company's branding. Writes stay closed. */
|
||||
class JWTAuthFilterPagesPathTest {
|
||||
|
||||
private static final String KEY = "gateway-test-key";
|
||||
|
||||
private JWTAuthFilter filter;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() throws Exception {
|
||||
AuthAppKey key = new ObjectMapper().readValue(
|
||||
"{\"key\":\"" + KEY + "\",\"type\":\"BO\",\"ipList\":[\"0.0.0.0/0\"],\"allowList\":[\"/\"]}",
|
||||
AuthAppKey.class);
|
||||
AuthAppKeyMap map = new AuthAppKeyMap();
|
||||
map.add(KEY, key);
|
||||
filter = new JWTAuthFilter();
|
||||
ReflectionTestUtils.setField(filter, "authAppKeyMap", map);
|
||||
}
|
||||
|
||||
private MockServerWebExchange call(HttpMethod method, String path) {
|
||||
return MockServerWebExchange.from(MockServerHttpRequest.method(method, path)
|
||||
.header("api-key", KEY)
|
||||
.remoteAddress(new InetSocketAddress("127.0.0.1", 40000)));
|
||||
}
|
||||
|
||||
private boolean passes(MockServerWebExchange exchange) {
|
||||
AtomicBoolean called = new AtomicBoolean(false);
|
||||
GatewayFilterChain chain = ex -> {
|
||||
called.set(true);
|
||||
return Mono.empty();
|
||||
};
|
||||
filter.filter(exchange, chain).block();
|
||||
return called.get();
|
||||
}
|
||||
|
||||
@Test
|
||||
void publicPageReadsNeedNoUserToken() {
|
||||
for (String path : new String[] {"/counter/cmsPage/findPublished/1/hotel-technology", "/counter/pageImage/42", "/company/branding/INS", "/company/branding/cv-2"}) {
|
||||
assertThat(passes(call(HttpMethod.GET, path))).as(path).isTrue();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void pageWritesAndOtherReadsStillNeedAToken() {
|
||||
for (String[] c : new String[][] {
|
||||
{"POST", "/counter/cmsPage/save"},
|
||||
{"POST", "/counter/cmsPage/search"},
|
||||
{"POST", "/counter/cmsPage/delete"},
|
||||
{"GET", "/counter/cmsPage/findById/3"},
|
||||
{"POST", "/counter/pageImage/upload"},
|
||||
{"GET", "/counter/pageImage/findCompany/42"},
|
||||
{"POST", "/counter/pageImage/42"},
|
||||
{"GET", "/counter/pageImage/42/x"},
|
||||
{"GET", "/counter/cmsPage/findPublished/1/hotel-technology/x"},
|
||||
{"GET", "/counter/cmsPage/findPublished/x/hotel-technology"},
|
||||
{"POST", "/company/1/branding"},
|
||||
{"GET", "/company/branding/INS/x"} }) {
|
||||
MockServerWebExchange exchange = call(HttpMethod.valueOf(c[0]), c[1]);
|
||||
assertThat(passes(exchange)).as(c[0] + " " + c[1]).isFalse();
|
||||
assertThat(exchange.getResponse().getStatusCode()).as(c[0] + " " + c[1]).isEqualTo(HttpStatus.UNAUTHORIZED);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user