-
Notifications
You must be signed in to change notification settings - Fork 241
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BATM-7170 Onfido: Allow webhook URL configuration via configuration f…
…ile (#979)
- Loading branch information
1 parent
e1127cc
commit c47f839
Showing
3 changed files
with
157 additions
and
52 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
.../batm/server/extensions/extra/identityverification/onfido/OnfidoConfigurationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package com.generalbytes.batm.server.extensions.extra.identityverification.onfido; | ||
|
||
import com.generalbytes.batm.server.extensions.IExtensionContext; | ||
import com.google.common.base.Strings; | ||
import lombok.AllArgsConstructor; | ||
|
||
import java.util.function.Supplier; | ||
|
||
@AllArgsConstructor | ||
public class OnfidoConfigurationService { | ||
|
||
/** | ||
* Configuration file for Onfido in '/batm/config' directory. | ||
*/ | ||
private static final String ONFIDO_CONFIG_FILE = "onfido"; | ||
|
||
private final IExtensionContext ctx; | ||
|
||
/** | ||
* Get callback URL for verification site webhook. | ||
* | ||
* @return URL from configuration file or default value based on configured hostname and port 7743 | ||
*/ | ||
public String getVerificationSiteCallbackUrl() { | ||
return getFromConfigOrDefault(getVerificationSiteWebhookConfig(), this::getMasterServerApiAddress); | ||
} | ||
|
||
/** | ||
* Get callback URL for Onfido webhook. | ||
* | ||
* @return URL from configuration file or default value based on configured hostname and port 8743 | ||
*/ | ||
public String getOnfidoCallbackUrl() { | ||
return getFromConfigOrDefault(getOnfidoWebhookConfig(), this::getMasterServerProxyAddress); | ||
} | ||
|
||
private String getMasterServerApiAddress() { | ||
return "https://" + getHostname() + ":" + 7743; | ||
} | ||
|
||
/** | ||
* @return this server address with hostname form /batm/config/hostname, | ||
* port number and path set to the one used by nginx proxy installed with {@code batm-manage install-reverse-proxy}. | ||
*/ | ||
private String getMasterServerProxyAddress() { | ||
return "https://" + getHostname() + ":" + 8743 + "/server"; | ||
} | ||
|
||
private String getHostname() { | ||
String hostname = Strings.nullToEmpty(ctx.getConfigFileContent("hostname")).trim(); | ||
if (hostname.isEmpty()) { | ||
throw new RuntimeException("Hostname not configured in /batm/config"); | ||
} | ||
return hostname; | ||
} | ||
|
||
private String getFromConfigOrDefault(String configValue, Supplier<String> defaultValue) { | ||
return configValue != null ? configValue : defaultValue.get(); | ||
} | ||
|
||
private String getOnfidoWebhookConfig() { | ||
return getConfiguredPropertyOrNull("webhook.onfido"); | ||
} | ||
|
||
private String getVerificationSiteWebhookConfig() { | ||
return getConfiguredPropertyOrNull("webhook.verificationSite"); | ||
} | ||
|
||
private String getConfiguredPropertyOrNull(String key) { | ||
return ctx.getConfigProperty(ONFIDO_CONFIG_FILE, key, null); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...m/server/extensions/extra/identityverification/onfido/OnfidoConfigurationServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.generalbytes.batm.server.extensions.extra.identityverification.onfido; | ||
|
||
import com.generalbytes.batm.server.extensions.IExtensionContext; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.Mockito.when; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class OnfidoConfigurationServiceTest { | ||
@Mock | ||
private IExtensionContext ctx; | ||
@InjectMocks | ||
private OnfidoConfigurationService configurationService; | ||
|
||
@Test | ||
void testGetVerificationSiteCallbackUrl_fromConfig() { | ||
String configuredUrl = "https://my.server.com:1234/verification"; | ||
when(ctx.getConfigProperty("onfido", "webhook.verificationSite", null)).thenReturn(configuredUrl); | ||
String verificationSiteCallbackUrl = configurationService.getVerificationSiteCallbackUrl(); | ||
assertEquals(configuredUrl, verificationSiteCallbackUrl); | ||
} | ||
|
||
@Test | ||
void testGetVerificationSiteCallbackUrl_defaultValue() { | ||
when(ctx.getConfigProperty("onfido", "webhook.verificationSite", null)).thenReturn(null); | ||
when(ctx.getConfigFileContent("hostname")).thenReturn("default.server.com"); | ||
String verificationSiteCallbackUrl = configurationService.getVerificationSiteCallbackUrl(); | ||
assertEquals("https://default.server.com:7743", verificationSiteCallbackUrl); | ||
} | ||
|
||
@Test | ||
void testGetOnfidoCallbackUrl_fromConfig() { | ||
String configuredUrl = "https://my.server.com:1234/onfido"; | ||
when(ctx.getConfigProperty("onfido", "webhook.onfido", null)).thenReturn(configuredUrl); | ||
String onfidoCallbackUrl = configurationService.getOnfidoCallbackUrl(); | ||
assertEquals(configuredUrl, onfidoCallbackUrl); | ||
} | ||
|
||
@Test | ||
void testGetOnfidoCallbackUrl_defaultValue() { | ||
when(ctx.getConfigProperty("onfido", "webhook.onfido", null)).thenReturn(null); | ||
when(ctx.getConfigFileContent("hostname")).thenReturn("default.server.com"); | ||
String onfidoCallbackUrl = configurationService.getOnfidoCallbackUrl(); | ||
assertEquals("https://default.server.com:8743/server", onfidoCallbackUrl); | ||
} | ||
|
||
@Test | ||
void testHostnameNotConfigured() { | ||
when(ctx.getConfigFileContent("hostname")).thenReturn(null); | ||
try { | ||
configurationService.getVerificationSiteCallbackUrl(); | ||
} catch (RuntimeException e) { | ||
assertEquals("Hostname not configured in /batm/config", e.getMessage()); | ||
} | ||
} | ||
} |