Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,14 @@ public void bind(final String eventName, final SubscriptionEventListener listene
private String authorize() {
try {
final AuthResponse authResponse = GSON.fromJson(getAuthorizationResponse(), AuthResponse.class);
channelData = authResponse.getChannelData();

if (authResponse.getAuth() == null) {
// Gson returns null for a null or empty response
if (authResponse == null || authResponse.getAuth() == null) {
throw new AuthorizationFailureException(
"Didn't receive all the fields expected " +
"from the ChannelAuthorizer, expected an auth and shared_secret."
);
} else {
channelData = authResponse.getChannelData();
return authResponse.getAuth();
}
} catch (JsonSyntaxException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ public String toSubscribeMessage() {
private String authenticate() {
try {
final AuthResponse authResponse = GSON.fromJson(getAuthorizationResponse(), AuthResponse.class);
if (authResponse.getAuth() == null || authResponse.getSharedSecret() == null) {
// Gson returns null for a null or empty response
if (authResponse == null || authResponse.getAuth() == null || authResponse.getSharedSecret() == null) {
throw new AuthorizationFailureException(
"Didn't receive all the fields expected " +
"from the ChannelAuthorizer, expected an auth and shared_secret."
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/com/pusher/client/user/impl/InternalUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.google.gson.JsonSyntaxException;
import com.pusher.client.AuthenticationFailureException;
import com.pusher.client.UserAuthenticator;
import com.pusher.client.channel.ChannelState;
import com.pusher.client.channel.PusherEvent;
import com.pusher.client.channel.SubscriptionEventListener;
import com.pusher.client.channel.impl.ChannelManager;
Expand Down Expand Up @@ -107,7 +108,10 @@ private AuthenticationResponse getAuthenticationResponse() throws Authentication
String response = userAuthenticator.authenticate(connection.getSocketId());
try {
AuthenticationResponse authenticationResponse = GSON.fromJson(response, AuthenticationResponse.class);
if (authenticationResponse.getAuth() == null || authenticationResponse.getUserData() == null) {
// Gson returns null for a null or empty response
if (authenticationResponse == null
|| authenticationResponse.getAuth() == null
|| authenticationResponse.getUserData() == null) {
throw new AuthenticationFailureException(
"Didn't receive all the fields expected from the UserAuthenticator. Expected auth and user_data"
);
Expand Down Expand Up @@ -135,8 +139,11 @@ private void onSigninSuccess(PusherEvent event) {
}

private void disconnect() {
if (serverToUserChannel.isSubscribed()) {
// disconnect() runs on every CONNECTING and DISCONNECTED state change, so it
// can run again after userId has been cleared. getName() needs the userId.
if (userId != null && serverToUserChannel.isSubscribed()) {
channelManager.unsubscribeFrom(serverToUserChannel.getName());
serverToUserChannel.updateState(ChannelState.UNSUBSCRIBED);
}
userId = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,18 @@ public void testThrowsAuthorizationFailureExceptionIfAuthorizerReturnsBasicStrin
channel.toSubscribeMessage();
}

@Test(expected = AuthorizationFailureException.class)
public void testThrowsAuthorizationFailureExceptionIfAuthorizerReturnsNull() {
when(mockChannelAuthorizer.authorize(eq(getChannelName()), anyString())).thenReturn(null);
channel.toSubscribeMessage();
}

@Test(expected = AuthorizationFailureException.class)
public void testThrowsAuthorizationFailureExceptionIfAuthorizerReturnsEmptyString() {
when(mockChannelAuthorizer.authorize(eq(getChannelName()), anyString())).thenReturn("");
channel.toSubscribeMessage();
}

@Test(expected = AuthorizationFailureException.class)
public void testThrowsAuthorizationFailureExceptionIfAuthorizerReturnsInvalidJSON() {
when(mockChannelAuthorizer.authorize(eq(getChannelName()), anyString())).thenReturn("{\"auth\":\"");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,16 @@ public void authenticationThrowsExceptionIfNoSharedSecret() {
channel.toSubscribeMessage();
}

@Test(expected = AuthorizationFailureException.class)
public void authenticationThrowsExceptionIfAuthorizerReturnsNull() {
when(mockChannelAuthorizer.authorize(Matchers.anyString(), Matchers.anyString()))
.thenReturn(null);

PrivateEncryptedChannelImpl channel = newInstance();

channel.toSubscribeMessage();
}

@Test(expected = AuthorizationFailureException.class)
public void authenticationThrowsExceptionIfMalformedJson() {
when(mockChannelAuthorizer.authorize(Matchers.anyString(), Matchers.anyString()))
Expand Down
33 changes: 33 additions & 0 deletions src/test/java/com/pusher/client/user/impl/InternalUserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@

import com.pusher.client.AuthenticationFailureException;
import com.pusher.client.UserAuthenticator;
import com.pusher.client.channel.ChannelState;
import com.pusher.client.channel.PusherEvent;
import com.pusher.client.channel.SubscriptionEventListener;
import com.pusher.client.channel.impl.ChannelManager;
import com.pusher.client.connection.ConnectionEventListener;
import com.pusher.client.connection.ConnectionState;
import com.pusher.client.connection.ConnectionStateChange;
import com.pusher.client.connection.impl.InternalConnection;
import com.pusher.client.util.Factory;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

Expand Down Expand Up @@ -83,6 +87,35 @@ public void testSigninMalformedResponse() {
user.signin();
}

@Test(expected = AuthenticationFailureException.class)
public void testSigninNullResponse() {
when(mockConnection.getState()).thenReturn(ConnectionState.CONNECTED);
when(mockUserAuthenticator.authenticate(socketId)).thenReturn(null);
user.signin();
}

@Test
public void testRepeatedReconnectsAfterSigninDoNotThrow() {
final ArgumentCaptor<ConnectionEventListener> connectionListener =
ArgumentCaptor.forClass(ConnectionEventListener.class);
verify(mockConnection).bind(eq(ConnectionState.ALL), connectionListener.capture());

user.handleEvent(PusherEvent.fromJson(signinSuccessEvent));
final ArgumentCaptor<ServerToUserChannel> serverToUserChannel =
ArgumentCaptor.forClass(ServerToUserChannel.class);
verify(mockChannelManager).subscribeTo(serverToUserChannel.capture(), eq(null));
serverToUserChannel.getValue().updateState(ChannelState.SUBSCRIBED);

// Each reconnect attempt moves the connection through CONNECTING again
connectionListener.getValue()
.onConnectionStateChange(new ConnectionStateChange(ConnectionState.RECONNECTING, ConnectionState.CONNECTING));
connectionListener.getValue()
.onConnectionStateChange(new ConnectionStateChange(ConnectionState.RECONNECTING, ConnectionState.CONNECTING));

verify(mockChannelManager).unsubscribeFrom("#server-to-user-1");
assertNull(user.userId());
}

@Test
public void testHandleEventSigninSuccessEvent() {
user.handleEvent(PusherEvent.fromJson(signinSuccessEvent));
Expand Down
Loading