Remove contacts with empty type from console GET /contacts response (#2052)

This commit is contained in:
Pavlo Tkach 2023-06-09 15:11:05 -04:00 committed by GitHub
parent b319eff7cd
commit a9aaa11801
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 11 deletions

View file

@ -92,9 +92,12 @@ public class ContactAction implements JsonGetAction {
ImmutableList<RegistrarPoc> am =
tm().transact(
() ->
tm().createQueryComposer(RegistrarPoc.class)
tm()
.createQueryComposer(RegistrarPoc.class)
.where("registrarId", Comparator.EQ, registrarId)
.list());
.stream()
.filter(r -> !r.getTypes().isEmpty())
.collect(toImmutableList()));
response.setStatus(HttpStatusCodes.STATUS_CODE_OK);
response.setPayload(gson.toJson(am));

View file

@ -74,7 +74,7 @@ class ContactActionTest {
private final HttpServletRequest request = mock(HttpServletRequest.class);
private RegistrarPoc testRegistrarPoc;
private static final Gson GSON = UtilsModule.provideGson();
private static final FakeResponse RESPONSE = new FakeResponse();
private FakeResponse response;
@RegisterExtension
final JpaTestExtensions.JpaIntegrationTestExtension jpa =
@ -82,6 +82,7 @@ class ContactActionTest {
@BeforeEach
void beforeEach() {
response = new FakeResponse();
testRegistrar = saveRegistrar("registrarId");
testRegistrarPoc =
new RegistrarPoc.Builder()
@ -110,8 +111,26 @@ class ContactActionTest {
testRegistrar.getRegistrarId(),
null);
action.run();
assertThat(RESPONSE.getStatus()).isEqualTo(HttpStatusCodes.STATUS_CODE_OK);
assertThat(RESPONSE.getPayload()).isEqualTo("[" + jsonRegistrar1 + "]");
assertThat(response.getStatus()).isEqualTo(HttpStatusCodes.STATUS_CODE_OK);
assertThat(response.getPayload()).isEqualTo("[" + jsonRegistrar1 + "]");
}
@Test
void testSuccess_onlyContactsWithNonEmptyType() throws IOException {
testRegistrarPoc = testRegistrarPoc.asBuilder().setTypes(ImmutableSet.of()).build();
insertInDb(testRegistrarPoc);
ContactAction action =
createAction(
Action.Method.GET,
AuthResult.create(
AuthLevel.USER,
UserAuthInfo.create(
createUser(new UserRoles.Builder().setGlobalRole(GlobalRole.FTE).build()))),
testRegistrar.getRegistrarId(),
null);
action.run();
assertThat(response.getStatus()).isEqualTo(HttpStatusCodes.STATUS_CODE_OK);
assertThat(response.getPayload()).isEqualTo("[]");
}
@Test
@ -126,7 +145,7 @@ class ContactActionTest {
testRegistrar.getRegistrarId(),
"[" + jsonRegistrar1 + "," + jsonRegistrar2 + "]");
action.run();
assertThat(RESPONSE.getStatus()).isEqualTo(HttpStatusCodes.STATUS_CODE_OK);
assertThat(response.getStatus()).isEqualTo(HttpStatusCodes.STATUS_CODE_OK);
assertThat(
loadAllOf(RegistrarPoc.class).stream()
.filter(r -> r.registrarId.equals(testRegistrar.getRegistrarId()))
@ -149,7 +168,7 @@ class ContactActionTest {
testRegistrar.getRegistrarId(),
"[" + jsonRegistrar1 + "," + jsonRegistrar2 + "]");
action.run();
assertThat(RESPONSE.getStatus()).isEqualTo(HttpStatusCodes.STATUS_CODE_OK);
assertThat(response.getStatus()).isEqualTo(HttpStatusCodes.STATUS_CODE_OK);
HashMap<String, String> testResult = new HashMap<>();
loadAllOf(RegistrarPoc.class).stream()
.filter(r -> r.registrarId.equals(testRegistrar.getRegistrarId()))
@ -175,7 +194,7 @@ class ContactActionTest {
testRegistrar.getRegistrarId(),
"[" + jsonRegistrar2 + "]");
action.run();
assertThat(RESPONSE.getStatus()).isEqualTo(HttpStatusCodes.STATUS_CODE_OK);
assertThat(response.getStatus()).isEqualTo(HttpStatusCodes.STATUS_CODE_OK);
assertThat(
loadAllOf(RegistrarPoc.class).stream()
.filter(r -> r.registrarId.equals(testRegistrar.getRegistrarId()))
@ -202,7 +221,7 @@ class ContactActionTest {
testRegistrar.getRegistrarId(),
"[" + jsonRegistrar2 + "]");
action.run();
assertThat(RESPONSE.getStatus()).isEqualTo(HttpStatusCodes.STATUS_CODE_FORBIDDEN);
assertThat(response.getStatus()).isEqualTo(HttpStatusCodes.STATUS_CODE_FORBIDDEN);
}
private User createUser(UserRoles userRoles) {
@ -218,14 +237,14 @@ class ContactActionTest {
throws IOException {
when(request.getMethod()).thenReturn(method.toString());
if (method.equals(Action.Method.GET)) {
return new ContactAction(request, authResult, RESPONSE, GSON, registrarId, Optional.empty());
return new ContactAction(request, authResult, response, GSON, registrarId, Optional.empty());
} else {
when(request.getReader())
.thenReturn(new BufferedReader(new StringReader("{\"contacts\":" + contacts + "}")));
Optional<ImmutableSet<RegistrarPoc>> maybeContacts =
RegistrarConsoleModule.provideContacts(
GSON, RequestModule.provideJsonBody(request, GSON));
return new ContactAction(request, authResult, RESPONSE, GSON, registrarId, maybeContacts);
return new ContactAction(request, authResult, response, GSON, registrarId, maybeContacts);
}
}
}