mirror of
https://github.com/signalapp/Signal-Server
synced 2026-04-25 03:28:06 +01:00
Upgrade to dropwizard 0.7.
This commit is contained in:
@@ -2,7 +2,8 @@ package org.whispersystems.textsecuregcm.tests.controllers;
|
||||
|
||||
import com.google.common.base.Optional;
|
||||
import com.sun.jersey.api.client.ClientResponse;
|
||||
import com.yammer.dropwizard.testing.ResourceTest;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.whispersystems.textsecuregcm.controllers.AccountController;
|
||||
import org.whispersystems.textsecuregcm.entities.AccountAttributes;
|
||||
@@ -16,11 +17,12 @@ import org.whispersystems.textsecuregcm.tests.util.AuthHelper;
|
||||
|
||||
import javax.ws.rs.core.MediaType;
|
||||
|
||||
import io.dropwizard.testing.junit.ResourceTestRule;
|
||||
import static org.fest.assertions.api.Assertions.assertThat;
|
||||
import static org.mockito.Matchers.anyString;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
public class AccountControllerTest extends ResourceTest {
|
||||
public class AccountControllerTest {
|
||||
|
||||
private static final String SENDER = "+14152222222";
|
||||
|
||||
@@ -30,26 +32,29 @@ public class AccountControllerTest extends ResourceTest {
|
||||
private RateLimiter rateLimiter = mock(RateLimiter.class );
|
||||
private SmsSender smsSender = mock(SmsSender.class );
|
||||
|
||||
@Override
|
||||
protected void setUpResources() throws Exception {
|
||||
addProvider(AuthHelper.getAuthenticator());
|
||||
@Rule
|
||||
public final ResourceTestRule resources = ResourceTestRule.builder()
|
||||
.addProvider(AuthHelper.getAuthenticator())
|
||||
.addResource(new AccountController(pendingAccountsManager,
|
||||
accountsManager,
|
||||
rateLimiters,
|
||||
smsSender))
|
||||
.build();
|
||||
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
when(rateLimiters.getSmsDestinationLimiter()).thenReturn(rateLimiter);
|
||||
when(rateLimiters.getVoiceDestinationLimiter()).thenReturn(rateLimiter);
|
||||
when(rateLimiters.getVerifyLimiter()).thenReturn(rateLimiter);
|
||||
|
||||
when(pendingAccountsManager.getCodeForNumber(SENDER)).thenReturn(Optional.of("1234"));
|
||||
|
||||
addResource(new AccountController(pendingAccountsManager,
|
||||
accountsManager,
|
||||
rateLimiters,
|
||||
smsSender));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSendCode() throws Exception {
|
||||
ClientResponse response =
|
||||
client().resource(String.format("/v1/accounts/sms/code/%s", SENDER))
|
||||
resources.client().resource(String.format("/v1/accounts/sms/code/%s", SENDER))
|
||||
.get(ClientResponse.class);
|
||||
|
||||
assertThat(response.getStatus()).isEqualTo(200);
|
||||
@@ -60,7 +65,7 @@ public class AccountControllerTest extends ResourceTest {
|
||||
@Test
|
||||
public void testVerifyCode() throws Exception {
|
||||
ClientResponse response =
|
||||
client().resource(String.format("/v1/accounts/code/%s", "1234"))
|
||||
resources.client().resource(String.format("/v1/accounts/code/%s", "1234"))
|
||||
.header("Authorization", AuthHelper.getAuthHeader(SENDER, "bar"))
|
||||
.entity(new AccountAttributes("keykeykeykey", false, false, 2222))
|
||||
.type(MediaType.APPLICATION_JSON_TYPE)
|
||||
@@ -68,13 +73,13 @@ public class AccountControllerTest extends ResourceTest {
|
||||
|
||||
assertThat(response.getStatus()).isEqualTo(204);
|
||||
|
||||
verify(accountsManager).create(isA(Account.class));
|
||||
verify(accountsManager, times(1)).create(isA(Account.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testVerifyBadCode() throws Exception {
|
||||
ClientResponse response =
|
||||
client().resource(String.format("/v1/accounts/code/%s", "1111"))
|
||||
resources.client().resource(String.format("/v1/accounts/code/%s", "1111"))
|
||||
.header("Authorization", AuthHelper.getAuthHeader(SENDER, "bar"))
|
||||
.entity(new AccountAttributes("keykeykeykey", false, false, 3333))
|
||||
.type(MediaType.APPLICATION_JSON_TYPE)
|
||||
|
||||
@@ -17,7 +17,8 @@
|
||||
package org.whispersystems.textsecuregcm.tests.controllers;
|
||||
|
||||
import com.google.common.base.Optional;
|
||||
import com.yammer.dropwizard.testing.ResourceTest;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.whispersystems.textsecuregcm.controllers.DeviceController;
|
||||
import org.whispersystems.textsecuregcm.entities.AccountAttributes;
|
||||
@@ -33,10 +34,11 @@ import org.whispersystems.textsecuregcm.util.VerificationCode;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
|
||||
import io.dropwizard.testing.junit.ResourceTestRule;
|
||||
import static org.fest.assertions.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
public class DeviceControllerTest extends ResourceTest {
|
||||
public class DeviceControllerTest {
|
||||
@Path("/v1/devices")
|
||||
static class DumbVerificationDeviceController extends DeviceController {
|
||||
public DumbVerificationDeviceController(PendingDevicesManager pendingDevices, AccountsManager accounts, RateLimiters rateLimiters) {
|
||||
@@ -55,10 +57,17 @@ public class DeviceControllerTest extends ResourceTest {
|
||||
private RateLimiter rateLimiter = mock(RateLimiter.class );
|
||||
private Account account = mock(Account.class );
|
||||
|
||||
@Override
|
||||
protected void setUpResources() throws Exception {
|
||||
addProvider(AuthHelper.getAuthenticator());
|
||||
@Rule
|
||||
public final ResourceTestRule resources = ResourceTestRule.builder()
|
||||
.addProvider(AuthHelper.getAuthenticator())
|
||||
.addResource(new DumbVerificationDeviceController(pendingDevicesManager,
|
||||
accountsManager,
|
||||
rateLimiters))
|
||||
.build();
|
||||
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
when(rateLimiters.getSmsDestinationLimiter()).thenReturn(rateLimiter);
|
||||
when(rateLimiters.getVoiceDestinationLimiter()).thenReturn(rateLimiter);
|
||||
when(rateLimiters.getVerifyLimiter()).thenReturn(rateLimiter);
|
||||
@@ -69,19 +78,17 @@ public class DeviceControllerTest extends ResourceTest {
|
||||
|
||||
when(pendingDevicesManager.getCodeForNumber(AuthHelper.VALID_NUMBER)).thenReturn(Optional.of("5678901"));
|
||||
when(accountsManager.get(AuthHelper.VALID_NUMBER)).thenReturn(Optional.of(account));
|
||||
|
||||
addResource(new DumbVerificationDeviceController(pendingDevicesManager, accountsManager, rateLimiters));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void validDeviceRegisterTest() throws Exception {
|
||||
VerificationCode deviceCode = client().resource("/v1/devices/provisioning_code")
|
||||
VerificationCode deviceCode = resources.client().resource("/v1/devices/provisioning_code")
|
||||
.header("Authorization", AuthHelper.getAuthHeader(AuthHelper.VALID_NUMBER, AuthHelper.VALID_PASSWORD))
|
||||
.get(VerificationCode.class);
|
||||
|
||||
assertThat(deviceCode).isEqualTo(new VerificationCode(5678901));
|
||||
|
||||
DeviceResponse response = client().resource("/v1/devices/5678901")
|
||||
DeviceResponse response = resources.client().resource("/v1/devices/5678901")
|
||||
.header("Authorization", AuthHelper.getAuthHeader(AuthHelper.VALID_NUMBER, "password1"))
|
||||
.entity(new AccountAttributes("keykeykeykey", false, true, 1234))
|
||||
.type(MediaType.APPLICATION_JSON_TYPE)
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package org.whispersystems.textsecuregcm.tests.controllers;
|
||||
|
||||
import com.sun.jersey.api.client.ClientResponse;
|
||||
import com.yammer.dropwizard.testing.ResourceTest;
|
||||
import org.junit.Before;
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.stubbing.Answer;
|
||||
@@ -17,33 +19,38 @@ import javax.ws.rs.core.MediaType;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import io.dropwizard.testing.junit.ResourceTestRule;
|
||||
import static org.fest.assertions.api.Assertions.assertThat;
|
||||
import static org.mockito.Matchers.anyList;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class DirectoryControllerTest extends ResourceTest {
|
||||
public class DirectoryControllerTest {
|
||||
|
||||
private RateLimiters rateLimiters = mock(RateLimiters.class );
|
||||
private RateLimiter rateLimiter = mock(RateLimiter.class );
|
||||
private DirectoryManager directoryManager = mock(DirectoryManager.class);
|
||||
private final RateLimiters rateLimiters = mock(RateLimiters.class );
|
||||
private final RateLimiter rateLimiter = mock(RateLimiter.class );
|
||||
private final DirectoryManager directoryManager = mock(DirectoryManager.class);
|
||||
|
||||
@Override
|
||||
protected void setUpResources() throws Exception {
|
||||
addProvider(AuthHelper.getAuthenticator());
|
||||
@Rule
|
||||
public final ResourceTestRule resources = ResourceTestRule.builder()
|
||||
.addProvider(AuthHelper.getAuthenticator())
|
||||
.addResource(new DirectoryController(rateLimiters,
|
||||
directoryManager))
|
||||
.build();
|
||||
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
when(rateLimiters.getContactsLimiter()).thenReturn(rateLimiter);
|
||||
when(directoryManager.get(anyList())).thenAnswer(new Answer<List<byte[]>>() {
|
||||
@Override
|
||||
public List<byte[]> answer(InvocationOnMock invocationOnMock) throws Throwable {
|
||||
List<byte[]> query = (List<byte[]>) invocationOnMock.getArguments()[0];
|
||||
List<byte[]> query = (List<byte[]>) invocationOnMock.getArguments()[0];
|
||||
List<byte[]> response = new LinkedList<>(query);
|
||||
response.remove(0);
|
||||
return response;
|
||||
}
|
||||
});
|
||||
|
||||
addResource(new DirectoryController(rateLimiters, directoryManager));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -58,14 +65,13 @@ public class DirectoryControllerTest extends ResourceTest {
|
||||
expectedResponse.remove(0);
|
||||
|
||||
ClientResponse response =
|
||||
client().resource("/v1/directory/tokens/")
|
||||
.entity(new ClientContactTokens(tokens))
|
||||
.type(MediaType.APPLICATION_JSON_TYPE)
|
||||
.header("Authorization",
|
||||
AuthHelper.getAuthHeader(AuthHelper.VALID_NUMBER,
|
||||
AuthHelper.VALID_PASSWORD))
|
||||
.put(ClientResponse.class);
|
||||
|
||||
resources.client().resource("/v1/directory/tokens/")
|
||||
.entity(new ClientContactTokens(tokens))
|
||||
.type(MediaType.APPLICATION_JSON_TYPE)
|
||||
.header("Authorization",
|
||||
AuthHelper.getAuthHeader(AuthHelper.VALID_NUMBER,
|
||||
AuthHelper.VALID_PASSWORD))
|
||||
.put(ClientResponse.class);
|
||||
|
||||
|
||||
assertThat(response.getStatus()).isEqualTo(200);
|
||||
|
||||
@@ -4,7 +4,8 @@ package org.whispersystems.textsecuregcm.tests.controllers;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.google.common.base.Optional;
|
||||
import com.sun.jersey.api.client.ClientResponse;
|
||||
import com.yammer.dropwizard.testing.ResourceTest;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.whispersystems.textsecuregcm.controllers.FederationController;
|
||||
import org.whispersystems.textsecuregcm.controllers.MessageController;
|
||||
@@ -23,17 +24,16 @@ import javax.ws.rs.core.MediaType;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import static com.yammer.dropwizard.testing.JsonHelpers.jsonFixture;
|
||||
import io.dropwizard.testing.junit.ResourceTestRule;
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.whispersystems.textsecuregcm.tests.util.JsonHelpers.jsonFixture;
|
||||
|
||||
public class FederatedControllerTest extends ResourceTest {
|
||||
public class FederatedControllerTest {
|
||||
|
||||
private static final String SINGLE_DEVICE_RECIPIENT = "+14151111111";
|
||||
private static final String MULTI_DEVICE_RECIPIENT = "+14152222222";
|
||||
@@ -46,10 +46,20 @@ public class FederatedControllerTest extends ResourceTest {
|
||||
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
@Override
|
||||
protected void setUpResources() throws Exception {
|
||||
addProvider(AuthHelper.getAuthenticator());
|
||||
private final MessageController messageController = new MessageController(rateLimiters, pushSender, accountsManager, federatedClientManager);
|
||||
|
||||
@Rule
|
||||
public final ResourceTestRule resources = ResourceTestRule.builder()
|
||||
.addProvider(AuthHelper.getAuthenticator())
|
||||
.addResource(new FederationController(accountsManager,
|
||||
null, null,
|
||||
messageController))
|
||||
.build();
|
||||
|
||||
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
List<Device> singleDeviceList = new LinkedList<Device>() {{
|
||||
add(new Device(1, "foo", "bar", "baz", "isgcm", null, false, 111));
|
||||
}};
|
||||
@@ -66,15 +76,12 @@ public class FederatedControllerTest extends ResourceTest {
|
||||
when(accountsManager.get(eq(MULTI_DEVICE_RECIPIENT))).thenReturn(Optional.of(multiDeviceAccount));
|
||||
|
||||
when(rateLimiters.getMessagesLimiter()).thenReturn(rateLimiter);
|
||||
|
||||
MessageController messageController = new MessageController(rateLimiters, pushSender, accountsManager, federatedClientManager);
|
||||
addResource(new FederationController(accountsManager, null, null, messageController));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSingleDeviceCurrent() throws Exception {
|
||||
ClientResponse response =
|
||||
client().resource(String.format("/v1/federation/messages/+14152223333/1/%s", SINGLE_DEVICE_RECIPIENT))
|
||||
resources.client().resource(String.format("/v1/federation/messages/+14152223333/1/%s", SINGLE_DEVICE_RECIPIENT))
|
||||
.header("Authorization", AuthHelper.getAuthHeader("cyanogen", "foofoo"))
|
||||
.entity(mapper.readValue(jsonFixture("fixtures/current_message_single_device.json"), IncomingMessageList.class))
|
||||
.type(MediaType.APPLICATION_JSON_TYPE)
|
||||
|
||||
@@ -2,7 +2,8 @@ package org.whispersystems.textsecuregcm.tests.controllers;
|
||||
|
||||
import com.google.common.base.Optional;
|
||||
import com.sun.jersey.api.client.ClientResponse;
|
||||
import com.yammer.dropwizard.testing.ResourceTest;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
@@ -24,16 +25,17 @@ import javax.ws.rs.core.MediaType;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import io.dropwizard.testing.junit.ResourceTestRule;
|
||||
import static org.fest.assertions.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
public class KeyControllerTest extends ResourceTest {
|
||||
public class KeyControllerTest {
|
||||
|
||||
private final String EXISTS_NUMBER = "+14152222222";
|
||||
private final String NOT_EXISTS_NUMBER = "+14152222220";
|
||||
private static final String EXISTS_NUMBER = "+14152222222";
|
||||
private static String NOT_EXISTS_NUMBER = "+14152222220";
|
||||
|
||||
private final int SAMPLE_REGISTRATION_ID = 999;
|
||||
private final int SAMPLE_REGISTRATION_ID2 = 1002;
|
||||
private static int SAMPLE_REGISTRATION_ID = 999;
|
||||
private static int SAMPLE_REGISTRATION_ID2 = 1002;
|
||||
|
||||
private final PreKey SAMPLE_KEY = new PreKey(1, EXISTS_NUMBER, Device.MASTER_ID, 1234, "test1", "test2", false);
|
||||
private final PreKey SAMPLE_KEY2 = new PreKey(2, EXISTS_NUMBER, 2, 5667, "test3", "test4,", false );
|
||||
@@ -42,14 +44,17 @@ public class KeyControllerTest extends ResourceTest {
|
||||
private final AccountsManager accounts = mock(AccountsManager.class);
|
||||
private final Account existsAccount = mock(Account.class );
|
||||
|
||||
private RateLimiters rateLimiters = mock(RateLimiters.class);
|
||||
private RateLimiter rateLimiter = mock(RateLimiter.class );
|
||||
|
||||
@Override
|
||||
protected void setUpResources() {
|
||||
addProvider(AuthHelper.getAuthenticator());
|
||||
|
||||
RateLimiters rateLimiters = mock(RateLimiters.class);
|
||||
RateLimiter rateLimiter = mock(RateLimiter.class );
|
||||
@Rule
|
||||
public final ResourceTestRule resources = ResourceTestRule.builder()
|
||||
.addProvider(AuthHelper.getAuthenticator())
|
||||
.addResource(new KeysController(rateLimiters, keys, accounts, null))
|
||||
.build();
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
Device sampleDevice = mock(Device.class );
|
||||
Device sampleDevice2 = mock(Device.class);
|
||||
Device sampleDevice3 = mock(Device.class);
|
||||
@@ -94,15 +99,12 @@ public class KeyControllerTest extends ResourceTest {
|
||||
});
|
||||
|
||||
when(keys.getCount(eq(AuthHelper.VALID_NUMBER), eq(1L))).thenReturn(5);
|
||||
|
||||
when(AuthHelper.VALID_ACCOUNT.getIdentityKey()).thenReturn(null);
|
||||
|
||||
addResource(new KeysController(rateLimiters, keys, accounts, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void validKeyStatusTest() throws Exception {
|
||||
PreKeyStatus result = client().resource("/v1/keys")
|
||||
PreKeyStatus result = resources.client().resource("/v1/keys")
|
||||
.header("Authorization",
|
||||
AuthHelper.getAuthHeader(AuthHelper.VALID_NUMBER, AuthHelper.VALID_PASSWORD))
|
||||
.get(PreKeyStatus.class);
|
||||
@@ -114,7 +116,7 @@ public class KeyControllerTest extends ResourceTest {
|
||||
|
||||
@Test
|
||||
public void validLegacyRequestTest() throws Exception {
|
||||
PreKey result = client().resource(String.format("/v1/keys/%s", EXISTS_NUMBER))
|
||||
PreKey result = resources.client().resource(String.format("/v1/keys/%s", EXISTS_NUMBER))
|
||||
.header("Authorization", AuthHelper.getAuthHeader(AuthHelper.VALID_NUMBER, AuthHelper.VALID_PASSWORD))
|
||||
.get(PreKey.class);
|
||||
|
||||
@@ -131,7 +133,7 @@ public class KeyControllerTest extends ResourceTest {
|
||||
|
||||
@Test
|
||||
public void validMultiRequestTest() throws Exception {
|
||||
UnstructuredPreKeyList results = client().resource(String.format("/v1/keys/%s/*", EXISTS_NUMBER))
|
||||
UnstructuredPreKeyList results = resources.client().resource(String.format("/v1/keys/%s/*", EXISTS_NUMBER))
|
||||
.header("Authorization", AuthHelper.getAuthHeader(AuthHelper.VALID_NUMBER, AuthHelper.VALID_PASSWORD))
|
||||
.get(UnstructuredPreKeyList.class);
|
||||
|
||||
@@ -163,27 +165,27 @@ public class KeyControllerTest extends ResourceTest {
|
||||
|
||||
@Test
|
||||
public void invalidRequestTest() throws Exception {
|
||||
ClientResponse response = client().resource(String.format("/v1/keys/%s", NOT_EXISTS_NUMBER))
|
||||
ClientResponse response = resources.client().resource(String.format("/v1/keys/%s", NOT_EXISTS_NUMBER))
|
||||
.header("Authorization", AuthHelper.getAuthHeader(AuthHelper.VALID_NUMBER, AuthHelper.VALID_PASSWORD))
|
||||
.get(ClientResponse.class);
|
||||
|
||||
assertThat(response.getClientResponseStatus().getStatusCode()).isEqualTo(404);
|
||||
assertThat(response.getStatusInfo().getStatusCode()).isEqualTo(404);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unauthorizedRequestTest() throws Exception {
|
||||
ClientResponse response =
|
||||
client().resource(String.format("/v1/keys/%s", NOT_EXISTS_NUMBER))
|
||||
resources.client().resource(String.format("/v1/keys/%s", NOT_EXISTS_NUMBER))
|
||||
.header("Authorization", AuthHelper.getAuthHeader(AuthHelper.VALID_NUMBER, AuthHelper.INVALID_PASSWORD))
|
||||
.get(ClientResponse.class);
|
||||
|
||||
assertThat(response.getClientResponseStatus().getStatusCode()).isEqualTo(401);
|
||||
assertThat(response.getStatusInfo().getStatusCode()).isEqualTo(401);
|
||||
|
||||
response =
|
||||
client().resource(String.format("/v1/keys/%s", NOT_EXISTS_NUMBER))
|
||||
resources.client().resource(String.format("/v1/keys/%s", NOT_EXISTS_NUMBER))
|
||||
.get(ClientResponse.class);
|
||||
|
||||
assertThat(response.getClientResponseStatus().getStatusCode()).isEqualTo(401);
|
||||
assertThat(response.getStatusInfo().getStatusCode()).isEqualTo(401);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -200,7 +202,7 @@ public class KeyControllerTest extends ResourceTest {
|
||||
preKeyList.setLastResortKey(lastResortKey);
|
||||
|
||||
ClientResponse response =
|
||||
client().resource("/v1/keys")
|
||||
resources.client().resource("/v1/keys")
|
||||
.header("Authorization", AuthHelper.getAuthHeader(AuthHelper.VALID_NUMBER, AuthHelper.VALID_PASSWORD))
|
||||
.type(MediaType.APPLICATION_JSON_TYPE)
|
||||
.put(ClientResponse.class, preKeyList);
|
||||
|
||||
@@ -3,10 +3,11 @@ package org.whispersystems.textsecuregcm.tests.controllers;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.google.common.base.Optional;
|
||||
import com.sun.jersey.api.client.ClientResponse;
|
||||
import com.yammer.dropwizard.testing.ResourceTest;
|
||||
import org.junit.Before;
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.whispersystems.textsecuregcm.controllers.MessageController;
|
||||
import org.whispersystems.textsecuregcm.entities.AccountAttributes;
|
||||
import org.whispersystems.textsecuregcm.entities.IncomingMessageList;
|
||||
import org.whispersystems.textsecuregcm.entities.MessageProtos;
|
||||
import org.whispersystems.textsecuregcm.entities.MismatchedDevices;
|
||||
@@ -24,35 +25,39 @@ import javax.ws.rs.core.MediaType;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import static com.yammer.dropwizard.testing.JsonHelpers.asJson;
|
||||
import static com.yammer.dropwizard.testing.JsonHelpers.jsonFixture;
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import io.dropwizard.testing.junit.ResourceTestRule;
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.whispersystems.textsecuregcm.tests.util.JsonHelpers.asJson;
|
||||
import static org.whispersystems.textsecuregcm.tests.util.JsonHelpers.jsonFixture;
|
||||
|
||||
public class MessageControllerTest extends ResourceTest {
|
||||
public class MessageControllerTest {
|
||||
|
||||
private static final String SINGLE_DEVICE_RECIPIENT = "+14151111111";
|
||||
private static final String MULTI_DEVICE_RECIPIENT = "+14152222222";
|
||||
|
||||
private PushSender pushSender = mock(PushSender.class );
|
||||
private FederatedClientManager federatedClientManager = mock(FederatedClientManager.class);
|
||||
private AccountsManager accountsManager = mock(AccountsManager.class );
|
||||
private RateLimiters rateLimiters = mock(RateLimiters.class );
|
||||
private RateLimiter rateLimiter = mock(RateLimiter.class );
|
||||
private final PushSender pushSender = mock(PushSender.class );
|
||||
private final FederatedClientManager federatedClientManager = mock(FederatedClientManager.class);
|
||||
private final AccountsManager accountsManager = mock(AccountsManager.class );
|
||||
private final RateLimiters rateLimiters = mock(RateLimiters.class );
|
||||
private final RateLimiter rateLimiter = mock(RateLimiter.class );
|
||||
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
@Override
|
||||
protected void setUpResources() throws Exception {
|
||||
addProvider(AuthHelper.getAuthenticator());
|
||||
@Rule
|
||||
public final ResourceTestRule resources = ResourceTestRule.builder()
|
||||
.addProvider(AuthHelper.getAuthenticator())
|
||||
.addResource(new MessageController(rateLimiters, pushSender, accountsManager,
|
||||
federatedClientManager))
|
||||
.build();
|
||||
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
List<Device> singleDeviceList = new LinkedList<Device>() {{
|
||||
add(new Device(1, "foo", "bar", "baz", "isgcm", null, false, 111));
|
||||
}};
|
||||
@@ -69,15 +74,12 @@ public class MessageControllerTest extends ResourceTest {
|
||||
when(accountsManager.get(eq(MULTI_DEVICE_RECIPIENT))).thenReturn(Optional.of(multiDeviceAccount));
|
||||
|
||||
when(rateLimiters.getMessagesLimiter()).thenReturn(rateLimiter);
|
||||
|
||||
addResource(new MessageController(rateLimiters, pushSender, accountsManager,
|
||||
federatedClientManager));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSingleDeviceLegacy() throws Exception {
|
||||
public synchronized void testSingleDeviceLegacy() throws Exception {
|
||||
ClientResponse response =
|
||||
client().resource("/v1/messages/")
|
||||
resources.client().resource("/v1/messages/")
|
||||
.header("Authorization", AuthHelper.getAuthHeader(AuthHelper.VALID_NUMBER, AuthHelper.VALID_PASSWORD))
|
||||
.entity(mapper.readValue(jsonFixture("fixtures/legacy_message_single_device.json"), IncomingMessageList.class))
|
||||
.type(MediaType.APPLICATION_JSON_TYPE)
|
||||
@@ -85,13 +87,13 @@ public class MessageControllerTest extends ResourceTest {
|
||||
|
||||
assertThat("Good Response", response.getStatus(), is(equalTo(200)));
|
||||
|
||||
verify(pushSender).sendMessage(any(Account.class), any(Device.class), any(MessageProtos.OutgoingMessageSignal.class));
|
||||
verify(pushSender, times(1)).sendMessage(any(Account.class), any(Device.class), any(MessageProtos.OutgoingMessageSignal.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSingleDeviceCurrent() throws Exception {
|
||||
public synchronized void testSingleDeviceCurrent() throws Exception {
|
||||
ClientResponse response =
|
||||
client().resource(String.format("/v1/messages/%s", SINGLE_DEVICE_RECIPIENT))
|
||||
resources.client().resource(String.format("/v1/messages/%s", SINGLE_DEVICE_RECIPIENT))
|
||||
.header("Authorization", AuthHelper.getAuthHeader(AuthHelper.VALID_NUMBER, AuthHelper.VALID_PASSWORD))
|
||||
.entity(mapper.readValue(jsonFixture("fixtures/current_message_single_device.json"), IncomingMessageList.class))
|
||||
.type(MediaType.APPLICATION_JSON_TYPE)
|
||||
@@ -99,13 +101,13 @@ public class MessageControllerTest extends ResourceTest {
|
||||
|
||||
assertThat("Good Response", response.getStatus(), is(equalTo(204)));
|
||||
|
||||
verify(pushSender).sendMessage(any(Account.class), any(Device.class), any(MessageProtos.OutgoingMessageSignal.class));
|
||||
verify(pushSender, times(1)).sendMessage(any(Account.class), any(Device.class), any(MessageProtos.OutgoingMessageSignal.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultiDeviceMissing() throws Exception {
|
||||
public synchronized void testMultiDeviceMissing() throws Exception {
|
||||
ClientResponse response =
|
||||
client().resource(String.format("/v1/messages/%s", MULTI_DEVICE_RECIPIENT))
|
||||
resources.client().resource(String.format("/v1/messages/%s", MULTI_DEVICE_RECIPIENT))
|
||||
.header("Authorization", AuthHelper.getAuthHeader(AuthHelper.VALID_NUMBER, AuthHelper.VALID_PASSWORD))
|
||||
.entity(mapper.readValue(jsonFixture("fixtures/current_message_single_device.json"), IncomingMessageList.class))
|
||||
.type(MediaType.APPLICATION_JSON_TYPE)
|
||||
@@ -121,9 +123,9 @@ public class MessageControllerTest extends ResourceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultiDeviceExtra() throws Exception {
|
||||
public synchronized void testMultiDeviceExtra() throws Exception {
|
||||
ClientResponse response =
|
||||
client().resource(String.format("/v1/messages/%s", MULTI_DEVICE_RECIPIENT))
|
||||
resources.client().resource(String.format("/v1/messages/%s", MULTI_DEVICE_RECIPIENT))
|
||||
.header("Authorization", AuthHelper.getAuthHeader(AuthHelper.VALID_NUMBER, AuthHelper.VALID_PASSWORD))
|
||||
.entity(mapper.readValue(jsonFixture("fixtures/current_message_extra_device.json"), IncomingMessageList.class))
|
||||
.type(MediaType.APPLICATION_JSON_TYPE)
|
||||
@@ -139,9 +141,9 @@ public class MessageControllerTest extends ResourceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultiDevice() throws Exception {
|
||||
public synchronized void testMultiDevice() throws Exception {
|
||||
ClientResponse response =
|
||||
client().resource(String.format("/v1/messages/%s", MULTI_DEVICE_RECIPIENT))
|
||||
resources.client().resource(String.format("/v1/messages/%s", MULTI_DEVICE_RECIPIENT))
|
||||
.header("Authorization", AuthHelper.getAuthHeader(AuthHelper.VALID_NUMBER, AuthHelper.VALID_PASSWORD))
|
||||
.entity(mapper.readValue(jsonFixture("fixtures/current_message_multi_device.json"), IncomingMessageList.class))
|
||||
.type(MediaType.APPLICATION_JSON_TYPE)
|
||||
@@ -153,9 +155,9 @@ public class MessageControllerTest extends ResourceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRegistrationIdMismatch() throws Exception {
|
||||
public synchronized void testRegistrationIdMismatch() throws Exception {
|
||||
ClientResponse response =
|
||||
client().resource(String.format("/v1/messages/%s", MULTI_DEVICE_RECIPIENT))
|
||||
resources.client().resource(String.format("/v1/messages/%s", MULTI_DEVICE_RECIPIENT))
|
||||
.header("Authorization", AuthHelper.getAuthHeader(AuthHelper.VALID_NUMBER, AuthHelper.VALID_PASSWORD))
|
||||
.entity(mapper.readValue(jsonFixture("fixtures/current_message_registration_id.json"), IncomingMessageList.class))
|
||||
.type(MediaType.APPLICATION_JSON_TYPE)
|
||||
|
||||
@@ -2,8 +2,10 @@ package org.whispersystems.textsecuregcm.tests.controllers;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.google.common.base.Optional;
|
||||
import com.yammer.dropwizard.auth.basic.BasicCredentials;
|
||||
import org.eclipse.jetty.websocket.WebSocket;
|
||||
import org.eclipse.jetty.websocket.api.CloseStatus;
|
||||
import org.eclipse.jetty.websocket.api.RemoteEndpoint;
|
||||
import org.eclipse.jetty.websocket.api.Session;
|
||||
import org.eclipse.jetty.websocket.api.UpgradeRequest;
|
||||
import org.junit.Test;
|
||||
import org.whispersystems.textsecuregcm.auth.AccountAuthenticator;
|
||||
import org.whispersystems.textsecuregcm.controllers.WebsocketController;
|
||||
@@ -15,12 +17,11 @@ import org.whispersystems.textsecuregcm.storage.PubSubManager;
|
||||
import org.whispersystems.textsecuregcm.storage.StoredMessageManager;
|
||||
import org.whispersystems.textsecuregcm.websocket.WebsocketAddress;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.fest.assertions.api.Assertions.assertThat;
|
||||
import io.dropwizard.auth.basic.BasicCredentials;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@@ -34,13 +35,13 @@ public class WebsocketControllerTest {
|
||||
private static final String VALID_PASSWORD = "secure";
|
||||
private static final String INVALID_PASSWORD = "insecure";
|
||||
|
||||
private final StoredMessageManager storedMessageManager = mock(StoredMessageManager.class);
|
||||
private final AccountAuthenticator accountAuthenticator = mock(AccountAuthenticator.class);
|
||||
private final PubSubManager pubSubManager = mock(PubSubManager.class );
|
||||
private final Account account = mock(Account.class );
|
||||
private final Device device = mock(Device.class );
|
||||
private final HttpServletRequest request = mock(HttpServletRequest.class );
|
||||
private final WebSocket.Connection connection = mock(WebSocket.Connection.class);
|
||||
private static final StoredMessageManager storedMessageManager = mock(StoredMessageManager.class);
|
||||
private static final AccountAuthenticator accountAuthenticator = mock(AccountAuthenticator.class);
|
||||
private static final PubSubManager pubSubManager = mock(PubSubManager.class );
|
||||
private static final Account account = mock(Account.class );
|
||||
private static final Device device = mock(Device.class );
|
||||
private static final UpgradeRequest upgradeRequest = mock(UpgradeRequest.class );
|
||||
private static final Session session = mock(Session.class );
|
||||
|
||||
@Test
|
||||
public void testCredentials() throws Exception {
|
||||
@@ -50,23 +51,35 @@ public class WebsocketControllerTest {
|
||||
when(accountAuthenticator.authenticate(eq(new BasicCredentials(INVALID_USER, INVALID_PASSWORD))))
|
||||
.thenReturn(Optional.<Account>absent());
|
||||
|
||||
WebsocketControllerFactory factory = new WebsocketControllerFactory(accountAuthenticator,
|
||||
storedMessageManager,
|
||||
pubSubManager);
|
||||
when(session.getUpgradeRequest()).thenReturn(upgradeRequest);
|
||||
|
||||
when(request.getParameter(eq("user"))).thenReturn(VALID_USER);
|
||||
when(request.getParameter(eq("password"))).thenReturn(VALID_PASSWORD);
|
||||
WebsocketController controller = new WebsocketController(accountAuthenticator, storedMessageManager, pubSubManager);
|
||||
|
||||
assertThat(factory.checkOrigin(request, "foobar")).isEqualTo(true);
|
||||
when(upgradeRequest.getParameterMap()).thenReturn(new HashMap<String, String[]>() {{
|
||||
put("login", new String[] {VALID_USER});
|
||||
put("password", new String[] {VALID_PASSWORD});
|
||||
}});
|
||||
|
||||
when(request.getParameter(eq("user"))).thenReturn(INVALID_USER);
|
||||
when(request.getParameter(eq("password"))).thenReturn(INVALID_PASSWORD);
|
||||
controller.onWebSocketConnect(session);
|
||||
|
||||
assertThat(factory.checkOrigin(request, "foobar")).isEqualTo(false);
|
||||
verify(session, never()).close();
|
||||
verify(session, never()).close(any(CloseStatus.class));
|
||||
verify(session, never()).close(anyInt(), anyString());
|
||||
|
||||
when(upgradeRequest.getParameterMap()).thenReturn(new HashMap<String, String[]>() {{
|
||||
put("login", new String[] {INVALID_USER});
|
||||
put("password", new String[] {INVALID_PASSWORD});
|
||||
}});
|
||||
|
||||
controller.onWebSocketConnect(session);
|
||||
|
||||
verify(session).close(any(CloseStatus.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOpen() throws Exception {
|
||||
RemoteEndpoint remote = mock(RemoteEndpoint.class);
|
||||
|
||||
List<String> outgoingMessages = new LinkedList<String>() {{
|
||||
add("first");
|
||||
add("second");
|
||||
@@ -76,29 +89,29 @@ public class WebsocketControllerTest {
|
||||
when(device.getId()).thenReturn(2L);
|
||||
when(account.getId()).thenReturn(31337L);
|
||||
when(account.getAuthenticatedDevice()).thenReturn(Optional.of(device));
|
||||
when(session.getRemote()).thenReturn(remote);
|
||||
when(session.getUpgradeRequest()).thenReturn(upgradeRequest);
|
||||
|
||||
when(request.getParameter(eq("user"))).thenReturn(VALID_USER);
|
||||
when(request.getParameter(eq("password"))).thenReturn(VALID_PASSWORD);
|
||||
when(upgradeRequest.getParameterMap()).thenReturn(new HashMap<String, String[]>() {{
|
||||
put("login", new String[] {VALID_USER});
|
||||
put("password", new String[] {VALID_PASSWORD});
|
||||
}});
|
||||
|
||||
when(accountAuthenticator.authenticate(eq(new BasicCredentials(VALID_USER, VALID_PASSWORD))))
|
||||
.thenReturn(Optional.of(account));
|
||||
|
||||
when(storedMessageManager.getOutgoingMessages(eq(account), eq(device))).thenReturn(outgoingMessages);
|
||||
|
||||
WebsocketControllerFactory factory = new WebsocketControllerFactory(accountAuthenticator,
|
||||
storedMessageManager,
|
||||
pubSubManager);
|
||||
WebsocketControllerFactory factory = new WebsocketControllerFactory(accountAuthenticator, storedMessageManager, pubSubManager);
|
||||
WebsocketController controller = (WebsocketController) factory.createWebSocket(null, null);
|
||||
|
||||
assertThat(factory.checkOrigin(request, "foobar")).isEqualTo(true);
|
||||
controller.onWebSocketConnect(session);
|
||||
|
||||
WebsocketController socket = (WebsocketController)factory.doWebSocketConnect(request, "foo");
|
||||
socket.onOpen(connection);
|
||||
verify(pubSubManager).subscribe(eq(new WebsocketAddress(31337L, 2L)), eq((controller)));
|
||||
verify(remote, times(3)).sendStringByFuture(anyString());
|
||||
|
||||
verify(pubSubManager).subscribe(eq(new WebsocketAddress(31337L, 2L)), eq((socket)));
|
||||
verify(connection, times(3)).sendMessage(anyString());
|
||||
|
||||
socket.onMessage(mapper.writeValueAsString(new AcknowledgeWebsocketMessage(1)));
|
||||
socket.onClose(1000, "Closed");
|
||||
controller.onWebSocketText(mapper.writeValueAsString(new AcknowledgeWebsocketMessage(1)));
|
||||
controller.onWebSocketClose(1000, "Closed");
|
||||
|
||||
List<String> pending = new LinkedList<String>() {{
|
||||
add("first");
|
||||
|
||||
Reference in New Issue
Block a user