Skip to content

Commit

Permalink
compensate for some schema requests and use utf8 for the charset beca…
Browse files Browse the repository at this point in the history
…use that's all auth0 accepts
  • Loading branch information
hunterjackson committed Jun 27, 2024
1 parent a1a6660 commit e95f2cf
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 31 deletions.
18 changes: 16 additions & 2 deletions src/main/java/com/meta/cp4m/plugin/AuthRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.meta.cp4m.utils.BlockingExpiringValue;
import java.io.IOException;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Collection;
Expand Down Expand Up @@ -120,10 +121,23 @@ public OauthRequest(

private OauthRefreshResponse refreshToken() throws IOException {
return Request.post(oauthTenantUrl)
.bodyForm(refreshParams)
.bodyForm(refreshParams, StandardCharsets.UTF_8)
.execute()
.handleResponse(
r -> MAPPER.readValue(r.getEntity().getContent(), OauthRefreshResponse.class));
r -> {
String body = new String(r.getEntity().getContent().readAllBytes());
try {
return MAPPER.readValue(body, OauthRefreshResponse.class);
} catch (Exception e) {
LOGGER
.atError()
.setCause(e)
.setMessage("failed to parse OAuth response")
.addKeyValue("response_body", body)
.log();
throw new RuntimeException(e);
}
});
}

private void scheduleRefresh(long timeSeconds) {
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/com/meta/cp4m/plugin/GenericPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.base.Preconditions;
import com.meta.cp4m.message.Message;
import com.meta.cp4m.message.Payload;
import com.meta.cp4m.message.ThreadState;
import com.meta.cp4m.message.UserData;
import com.meta.cp4m.message.*;
import java.io.IOException;
import java.net.URI;
import java.time.Instant;
Expand All @@ -43,6 +40,11 @@ public GenericPlugin(URI url) {
@Override
public T handle(ThreadState<T> threadState) throws IOException {
ObjectNode postPayload = MAPPER.createObjectNode();
switch (threadState.tail()) {
case WAMessage ignored -> postPayload.put("source_client", "whatsapp");
case FBMessage ignored -> postPayload.put("source_client", "messenger");
default -> postPayload.put("source_client", "unknown");
}
postPayload.put("timestamp", Instant.now().toString());
ObjectNode userObj = postPayload.putObject("user");
userObj.put("id", threadState.userId().toString());
Expand Down
3 changes: 3 additions & 0 deletions src/test/java/com/meta/cp4m/plugin/AuthRequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ void oauth() throws URISyntaxException, InterruptedException, IOException {
"audience");
DummyWebServer.ReceivedRequest request = webserver.take(300);
String params = URLDecoder.decode(request.body(), StandardCharsets.UTF_8);
assertThat(request.contentType())
.isEqualTo(
ContentType.APPLICATION_FORM_URLENCODED.withCharset(StandardCharsets.UTF_8).toString());
assertThat(params)
.contains("client_id=client_id")
.contains("client_secret=client_secret")
Expand Down
59 changes: 34 additions & 25 deletions src/test/java/com/meta/cp4m/plugin/GenericPluginTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

package com.meta.cp4m.plugin;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static com.meta.cp4m.message.MessageFactory.FACTORY_MAP;
import static org.assertj.core.api.Assertions.*;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
Expand All @@ -21,10 +21,7 @@
import com.meta.cp4m.Identifier;
import com.meta.cp4m.configuration.ConfigurationUtils;
import com.meta.cp4m.configuration.RootConfiguration;
import com.meta.cp4m.message.Message;
import com.meta.cp4m.message.Payload;
import com.meta.cp4m.message.ThreadState;
import com.meta.cp4m.message.WAMessage;
import com.meta.cp4m.message.*;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
Expand Down Expand Up @@ -117,30 +114,35 @@ void configWAuth() throws JsonProcessingException {
.allSatisfy(p -> assertThat(p.plugin()).isOfAnyClassIn(GenericPlugin.class));
}

static Stream<ThreadState<WAMessage>> statesWithUserData() {
static Stream<ThreadState<?>> statesWithUserData() {
Identifier businessId = Identifier.random();
Identifier userId = Identifier.random();
Instant messageTime = Instant.now();
ThreadState<WAMessage> ts =
ThreadState.of(
new WAMessage(
messageTime,
Identifier.random(),
userId,
businessId,
new Payload.Text("hello world!"),
Message.Role.USER));
return Stream.of(
ts,
ts.withUserData(ts.userData().withName("test name")),
ts.withUserData(ts.userData().withPhoneNumber("1234567890")),
ts.withUserData(ts.userData().withPhoneNumber("1234567899")),
ts.withUserData(ts.userData().withName("test name").withPhoneNumber("1234567899")));
return FACTORY_MAP.values().stream()
.map(
f ->
ThreadState.of(
f.newMessage(
messageTime,
new Payload.Text("hello world!"),
userId,
businessId,
Identifier.random(),
Message.Role.USER)))
.flatMap(
ts ->
Stream.of(
ts,
ts.withUserData(ts.userData().withName("test name")),
ts.withUserData(ts.userData().withPhoneNumber("1234567890")),
ts.withUserData(ts.userData().withPhoneNumber("1234567899")),
ts.withUserData(
ts.userData().withName("test name").withPhoneNumber("1234567899"))));
}

@ParameterizedTest
@MethodSource("statesWithUserData")
void sanity(ThreadState<WAMessage> ts)
<T extends Message> void sanity(ThreadState<T> ts)
throws URISyntaxException, IOException, InterruptedException {
GenericPlugin.GenericPluginThreadUpdateResponse res =
new GenericPlugin.GenericPluginThreadUpdateResponse("text", "hello from the plugin!");
Expand All @@ -153,14 +155,21 @@ void sanity(ThreadState<WAMessage> ts)
.setScheme("http")
.setPort(webServer.port())
.build();
GenericPlugin<WAMessage> plugin = new GenericPlugin<>(url);
WAMessage response = plugin.handle(ts);
GenericPlugin<T> plugin = new GenericPlugin<>(url);
T response = plugin.handle(ts);
ReceivedRequest received = webServer.take(500);
assertThat(received)
.satisfies(
r -> assertThat(r.contentType()).isEqualTo(ContentType.APPLICATION_JSON.toString()))
.satisfies(r -> assertThat(r.path()).isEqualTo(path));
ObjectNode body = (ObjectNode) MAPPER.readTree(received.body());
switch (ts.tail()) {
case WAMessage ignored ->
assertThat(body.get("source_client").textValue()).isEqualTo("whatsapp");
case FBMessage ignored ->
assertThat(body.get("source_client").textValue()).isEqualTo("messenger");
default -> fail("all cases should be covered");
}
ObjectNode user = (ObjectNode) body.get("user");
assertThat(user.get("id").textValue()).isEqualTo(ts.userId().toString());
if (ts.userData().phoneNumber().isPresent()) {
Expand Down

0 comments on commit e95f2cf

Please sign in to comment.