Skip to content

Commit

Permalink
Added test coverage for the "non active" user and non-existing user.
Browse files Browse the repository at this point in the history
  • Loading branch information
spetrov committed Jan 26, 2024
1 parent e724054 commit 8d749de
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public abstract class BaseDeviceBindingTest {
protected static Context context = ApplicationProvider.getApplicationContext();

// This test uses dynamic configuration with the following settings:
protected final static String AM_URL = "https://openam-sdks.forgeblocks.com/am";
protected final static String AM_URL = "https://openam-spetrov.forgeblocks.com/am";
protected final static String REALM = "alpha";
protected final static String OAUTH_CLIENT = "AndroidTest";
protected final static String OAUTH_REDIRECT_URI = "org.forgerock.demo:/oauth2redirect";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,58 @@ public void onException(Exception e) {
Assert.assertNotNull(FRSession.getCurrentSession());
Assert.assertNotNull(FRSession.getCurrentSession().getSessionToken());
}

/*
* Make sure that when user does NOT exist, the Device Binding node triggers the failure outcome (SDKS-2935)
*/
@Test
public void testDeviceBindingUnknownUser() throws ExecutionException, InterruptedException {
final int[] hit = {0};
final int[] failureOutcome = {0};
NodeListenerFuture<FRSession> nodeListenerFuture = new DeviceBindingNodeListener(context, "default")
{
@Override
public void onCallbackReceived(Node node)
{
if (node.getCallback(DeviceSigningVerifierCallback.class) != null) {
DeviceSigningVerifierCallback callback = node.getCallback(DeviceSigningVerifierCallback.class);

Assertions.fail("Test failed: Received unexpected DeviceSigningVerifierCallback! (see SDKS-2169)" );
return;
}
if (node.getCallback(NameCallback.class) != null) {
hit[0]++;
node.getCallback(NameCallback.class).setName("UNKNOWN-USER");
node.next(context, this);
return;
}
// Make sure that the "Failure" outcome has been triggered
if (node.getCallback(TextOutputCallback.class) != null) {
TextOutputCallback textOutputCallback = node.getCallback(TextOutputCallback.class);
assertThat(textOutputCallback.getMessage()).isEqualTo("Device Binding Failed");
failureOutcome[0]++;

node.next(context, this);
return;
}

super.onCallbackReceived(node);
}
};

FRSession.authenticate(context, TREE, nodeListenerFuture);

// Ensure that the journey finishes with failure
thrown.expect(java.util.concurrent.ExecutionException.class);
thrown.expectMessage("ApiException{statusCode=401, error='', description='{\"code\":401,\"reason\":\"Unauthorized\",\"message\":\"Login failure\"}'}");

Assert.assertNull(nodeListenerFuture.get());
Assert.assertNull(FRSession.getCurrentSession());
Assert.assertNull(FRSession.getCurrentSession().getSessionToken());

assertThat(hit[0]).isEqualTo(1);
assertThat(failureOutcome[0]).isEqualTo(1);
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import com.nimbusds.jwt.JWTParser;

import org.assertj.core.api.Assertions;
import org.assertj.core.api.ClassAssert;
import org.forgerock.android.auth.FRListener;
import org.forgerock.android.auth.FRSession;
import org.forgerock.android.auth.Logger;
Expand Down Expand Up @@ -92,9 +91,13 @@ public void onException(Exception e) {
}
}

/*
* When user does NOT exist in AM, the node should trigger the "Failure" outcome (SDKS-2935)
*/
@Test
public void testDeviceSigningVerifierUnknownUserError() throws ExecutionException, InterruptedException {
final int[] hit = {0};
final int[] failureOutcome = {0};
NodeListenerFuture<FRSession> nodeListenerFuture = new DeviceSigningVerifierNodeListener(context, "default")
{
@Override
Expand All @@ -112,22 +115,27 @@ public void onCallbackReceived(Node node)
node.next(context, this);
return;
}
// Make sure that the "Failure" outcome has been triggered
if (node.getCallback(TextOutputCallback.class) != null) {
TextOutputCallback textOutputCallback = node.getCallback(TextOutputCallback.class);
assertThat(textOutputCallback.getMessage()).isEqualTo("Failure");
failureOutcome[0]++;

node.next(context, this);
return;
}

super.onCallbackReceived(node);
}
};

FRSession.authenticate(context, TREE, nodeListenerFuture);

// Ensure that the journey finishes with failure
thrown.expect(java.util.concurrent.ExecutionException.class);
thrown.expectMessage("ApiException{statusCode=401, error='', description='{\"code\":401,\"reason\":\"Unauthorized\",\"message\":\"Login failure\"}'}");

Assert.assertNull(nodeListenerFuture.get());
Assert.assertNull(FRSession.getCurrentSession());
Assert.assertNull(FRSession.getCurrentSession().getSessionToken());

Assert.assertNotNull(nodeListenerFuture.get());
Assert.assertNotNull(FRSession.getCurrentSession());
Assert.assertNotNull(FRSession.getCurrentSession().getSessionToken());
assertThat(hit[0]).isEqualTo(1);
assertThat(failureOutcome[0]).isEqualTo(1);
}

@Test
Expand Down Expand Up @@ -891,7 +899,7 @@ public void onException(Exception e) {

return;
}
// Make sure that by default upon
// Make sure that the "Abort" outcome has been triggered
if (node.getCallback(TextOutputCallback.class) != null) {
TextOutputCallback textOutputCallback = node.getCallback(TextOutputCallback.class);
assertThat(textOutputCallback.getMessage()).isEqualTo("Abort");
Expand All @@ -913,4 +921,64 @@ public void onException(Exception e) {
Assert.assertNotNull(FRSession.getCurrentSession().getSessionToken());
}

/*
* Make sure that when user's account is not active,
* the Inactive User outcome is triggered (when enabled...) (SDKS-2935)
*/
@Test
public void testDeviceVerificationInactiveUser() throws ExecutionException, InterruptedException {
final int[] signCallback = {0};
final int[] inactiveUserOutcome = {0};

NodeListenerFuture<FRSession> nodeListenerFuture = new DeviceSigningVerifierNodeListener(context, "inactive-user")
{
final NodeListener<FRSession> nodeListener = this;

@Override
public void onCallbackReceived(Node node)
{
if (node.getCallback(DeviceSigningVerifierCallback.class) != null) {
DeviceSigningVerifierCallback callback = node.getCallback(DeviceSigningVerifierCallback.class);

signCallback[0]++;
callback.sign(context, new FRListener<Void>() {
@Override
public void onSuccess(Void result) {

node.next(context, nodeListener);
}
@Override
public void onException(Exception e) {
node.next(context, nodeListener);
}
});

return;
}
// Make sure that the "Inactive User" outcome has been triggered
if (node.getCallback(TextOutputCallback.class) != null) {
TextOutputCallback textOutputCallback = node.getCallback(TextOutputCallback.class);
assertThat(textOutputCallback.getMessage()).isEqualTo("Inactive User");
inactiveUserOutcome[0]++;

node.next(context, nodeListener);
return;
}

super.onCallbackReceived(node);
}
};

FRSession.authenticate(context, TREE, nodeListenerFuture);
Assert.assertNotNull(nodeListenerFuture.get());

// Make sure that the node didn't return a callback, and the "inactive user" outcome was triggered.
assertThat(signCallback[0]).isEqualTo(0);
assertThat(inactiveUserOutcome[0]).isEqualTo(1);

// Ensure that the journey finishes with success
Assert.assertNotNull(FRSession.getCurrentSession());
Assert.assertNotNull(FRSession.getCurrentSession().getSessionToken());

}
}

0 comments on commit 8d749de

Please sign in to comment.