mirror of
https://github.com/signalapp/Signal-Server
synced 2026-04-23 07:08:04 +01:00
Migrate service tests to JUnit 5
This commit is contained in:
@@ -5,12 +5,11 @@ import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.post;
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.verify;
|
||||
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
|
||||
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
|
||||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import com.github.tomakehurst.wiremock.junit.WireMockRule;
|
||||
import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
|
||||
import java.net.http.HttpClient;
|
||||
import java.time.Duration;
|
||||
import java.util.Collections;
|
||||
@@ -18,19 +17,17 @@ import java.util.List;
|
||||
import java.util.Locale.LanguageRange;
|
||||
import java.util.Optional;
|
||||
import javax.annotation.Nullable;
|
||||
import junitparams.JUnitParamsRunner;
|
||||
import junitparams.Parameters;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
import org.whispersystems.textsecuregcm.configuration.TwilioConfiguration;
|
||||
import org.whispersystems.textsecuregcm.http.FaultTolerantHttpClient;
|
||||
import org.whispersystems.textsecuregcm.util.ExecutorUtils;
|
||||
|
||||
@SuppressWarnings("OptionalGetWithoutIsPresent")
|
||||
@RunWith(JUnitParamsRunner.class)
|
||||
public class TwilioVerifySenderTest {
|
||||
class TwilioVerifySenderTest {
|
||||
|
||||
private static final String ACCOUNT_ID = "test_account_id";
|
||||
private static final String ACCOUNT_TOKEN = "test_account_token";
|
||||
@@ -43,13 +40,15 @@ public class TwilioVerifySenderTest {
|
||||
|
||||
private static final String VERIFICATION_SID = "verification";
|
||||
|
||||
@Rule
|
||||
public WireMockRule wireMockRule = new WireMockRule(options().dynamicPort().dynamicHttpsPort());
|
||||
@RegisterExtension
|
||||
private final WireMockExtension wireMock = WireMockExtension.newInstance()
|
||||
.options(wireMockConfig().dynamicPort().dynamicHttpsPort())
|
||||
.build();
|
||||
|
||||
private TwilioVerifySender sender;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
final TwilioConfiguration twilioConfiguration = createTwilioConfiguration();
|
||||
|
||||
final FaultTolerantHttpClient httpClient = FaultTolerantHttpClient.newBuilder()
|
||||
@@ -62,7 +61,7 @@ public class TwilioVerifySenderTest {
|
||||
.withName("twilio")
|
||||
.build();
|
||||
|
||||
sender = new TwilioVerifySender("http://localhost:" + wireMockRule.port(), httpClient, twilioConfiguration);
|
||||
sender = new TwilioVerifySender("http://localhost:" + wireMock.getPort(), httpClient, twilioConfiguration);
|
||||
}
|
||||
|
||||
private TwilioConfiguration createTwilioConfiguration() {
|
||||
@@ -82,16 +81,16 @@ public class TwilioVerifySenderTest {
|
||||
}
|
||||
|
||||
private void setupSuccessStubForVerify() {
|
||||
wireMockRule.stubFor(post(urlEqualTo("/v2/Services/" + VERIFY_SERVICE_SID + "/Verifications"))
|
||||
wireMock.stubFor(post(urlEqualTo("/v2/Services/" + VERIFY_SERVICE_SID + "/Verifications"))
|
||||
.withBasicAuth(ACCOUNT_ID, ACCOUNT_TOKEN)
|
||||
.willReturn(aResponse()
|
||||
.withHeader("Content-Type", "application/json")
|
||||
.withBody("{\"sid\": \"" + VERIFICATION_SID + "\", \"status\": \"pending\"}")));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Parameters(method = "argumentsForDeliverSmsVerificationWithVerify")
|
||||
public void deliverSmsVerificationWithVerify(@Nullable final String client, @Nullable final String languageRange,
|
||||
@ParameterizedTest
|
||||
@MethodSource("argumentsForDeliverSmsVerificationWithVerify")
|
||||
void deliverSmsVerificationWithVerify(@Nullable final String client, @Nullable final String languageRange,
|
||||
final boolean expectAppHash, @Nullable final String expectedLocale) throws Exception {
|
||||
|
||||
setupSuccessStubForVerify();
|
||||
@@ -106,7 +105,7 @@ public class TwilioVerifySenderTest {
|
||||
|
||||
assertEquals(VERIFICATION_SID, verificationSid.get());
|
||||
|
||||
verify(1, postRequestedFor(urlEqualTo("/v2/Services/" + VERIFY_SERVICE_SID + "/Verifications"))
|
||||
wireMock.verify(1, postRequestedFor(urlEqualTo("/v2/Services/" + VERIFY_SERVICE_SID + "/Verifications"))
|
||||
.withHeader("Content-Type", equalTo("application/x-www-form-urlencoded"))
|
||||
.withRequestBody(equalTo(
|
||||
(expectedLocale == null ? "" : "Locale=" + expectedLocale + "&")
|
||||
@@ -115,7 +114,7 @@ public class TwilioVerifySenderTest {
|
||||
)));
|
||||
}
|
||||
|
||||
private static Object argumentsForDeliverSmsVerificationWithVerify() {
|
||||
private static Object[] argumentsForDeliverSmsVerificationWithVerify() {
|
||||
return new Object[][]{
|
||||
// client, languageRange, expectAppHash, expectedLocale
|
||||
{"ios", "fr-CA, en", false, "fr"},
|
||||
@@ -124,9 +123,9 @@ public class TwilioVerifySenderTest {
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
@Parameters(method = "argumentsForDeliverVoxVerificationWithVerify")
|
||||
public void deliverVoxVerificationWithVerify(@Nullable final String languageRange,
|
||||
@ParameterizedTest
|
||||
@MethodSource("argumentsForDeliverVoxVerificationWithVerify")
|
||||
void deliverVoxVerificationWithVerify(@Nullable final String languageRange,
|
||||
@Nullable final String expectedLocale) throws Exception {
|
||||
|
||||
setupSuccessStubForVerify();
|
||||
@@ -140,7 +139,7 @@ public class TwilioVerifySenderTest {
|
||||
|
||||
assertEquals(VERIFICATION_SID, verificationSid.get());
|
||||
|
||||
verify(1, postRequestedFor(urlEqualTo("/v2/Services/" + VERIFY_SERVICE_SID + "/Verifications"))
|
||||
wireMock.verify(1, postRequestedFor(urlEqualTo("/v2/Services/" + VERIFY_SERVICE_SID + "/Verifications"))
|
||||
.withHeader("Content-Type", equalTo("application/x-www-form-urlencoded"))
|
||||
.withRequestBody(equalTo(
|
||||
(expectedLocale == null ? "" : "Locale=" + expectedLocale + "&")
|
||||
@@ -148,7 +147,7 @@ public class TwilioVerifySenderTest {
|
||||
+ "&CustomCode=123456")));
|
||||
}
|
||||
|
||||
private static Object argumentsForDeliverVoxVerificationWithVerify() {
|
||||
private static Object[] argumentsForDeliverVoxVerificationWithVerify() {
|
||||
return new Object[][]{
|
||||
// languageRange, expectedLocale
|
||||
{"fr-CA, en", "fr"},
|
||||
@@ -159,8 +158,8 @@ public class TwilioVerifySenderTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSmsFiveHundred() throws Exception {
|
||||
wireMockRule.stubFor(post(urlEqualTo("/v2/Services/" + VERIFY_SERVICE_SID + "/Verifications"))
|
||||
void testSmsFiveHundred() throws Exception {
|
||||
wireMock.stubFor(post(urlEqualTo("/v2/Services/" + VERIFY_SERVICE_SID + "/Verifications"))
|
||||
.withBasicAuth(ACCOUNT_ID, ACCOUNT_TOKEN)
|
||||
.willReturn(aResponse()
|
||||
.withStatus(500)
|
||||
@@ -172,15 +171,15 @@ public class TwilioVerifySenderTest {
|
||||
|
||||
assertThat(verificationSid).isEmpty();
|
||||
|
||||
verify(3, postRequestedFor(urlEqualTo("/v2/Services/" + VERIFY_SERVICE_SID + "/Verifications"))
|
||||
wireMock.verify(3, postRequestedFor(urlEqualTo("/v2/Services/" + VERIFY_SERVICE_SID + "/Verifications"))
|
||||
.withHeader("Content-Type", equalTo("application/x-www-form-urlencoded"))
|
||||
.withRequestBody(equalTo("Channel=sms&To=%2B14153333333&CustomFriendlyName=" + SERVICE_FRIENDLY_NAME
|
||||
+ "&CustomCode=123456")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testVoxFiveHundred() throws Exception {
|
||||
wireMockRule.stubFor(post(urlEqualTo("/v2/Services/" + VERIFY_SERVICE_SID + "/Verifications"))
|
||||
void testVoxFiveHundred() throws Exception {
|
||||
wireMock.stubFor(post(urlEqualTo("/v2/Services/" + VERIFY_SERVICE_SID + "/Verifications"))
|
||||
.withBasicAuth(ACCOUNT_ID, ACCOUNT_TOKEN)
|
||||
.willReturn(aResponse()
|
||||
.withStatus(500)
|
||||
@@ -192,16 +191,16 @@ public class TwilioVerifySenderTest {
|
||||
|
||||
assertThat(verificationSid).isEmpty();
|
||||
|
||||
verify(3, postRequestedFor(urlEqualTo("/v2/Services/" + VERIFY_SERVICE_SID + "/Verifications"))
|
||||
wireMock.verify(3, postRequestedFor(urlEqualTo("/v2/Services/" + VERIFY_SERVICE_SID + "/Verifications"))
|
||||
.withHeader("Content-Type", equalTo("application/x-www-form-urlencoded"))
|
||||
.withRequestBody(equalTo("Channel=call&To=%2B14153333333&CustomFriendlyName=" + SERVICE_FRIENDLY_NAME
|
||||
+ "&CustomCode=123456")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void reportVerificationSucceeded() throws Exception {
|
||||
void reportVerificationSucceeded() throws Exception {
|
||||
|
||||
wireMockRule.stubFor(post(urlEqualTo("/v2/Services/" + VERIFY_SERVICE_SID + "/Verifications/" + VERIFICATION_SID))
|
||||
wireMock.stubFor(post(urlEqualTo("/v2/Services/" + VERIFY_SERVICE_SID + "/Verifications/" + VERIFICATION_SID))
|
||||
.withBasicAuth(ACCOUNT_ID, ACCOUNT_TOKEN)
|
||||
.willReturn(aResponse()
|
||||
.withStatus(200)
|
||||
@@ -212,7 +211,7 @@ public class TwilioVerifySenderTest {
|
||||
|
||||
assertThat(success).isTrue();
|
||||
|
||||
verify(1, postRequestedFor(urlEqualTo("/v2/Services/" + VERIFY_SERVICE_SID + "/Verifications/" + VERIFICATION_SID))
|
||||
wireMock.verify(1, postRequestedFor(urlEqualTo("/v2/Services/" + VERIFY_SERVICE_SID + "/Verifications/" + VERIFICATION_SID))
|
||||
.withHeader("Content-Type", equalTo("application/x-www-form-urlencoded"))
|
||||
.withRequestBody(equalTo("Status=approved")));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user