Skip to content

Commit

Permalink
Reorder tests to match order of SDK functions
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenclouston committed Dec 1, 2024
1 parent 1ef8ff5 commit 495fb9b
Showing 1 changed file with 116 additions and 187 deletions.
303 changes: 116 additions & 187 deletions test/AuthsignalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,81 +22,85 @@ static function tearDownAfterClass(): void {
self::$server->stop();
}

public function testSetApiKey() {
$this->assertEquals('secret', Authsignal::getApiKey());
}

public function testTrackAction() {
// Mock response
$mockedResponse = array("state" => "ALLOW",
"idempotencyKey" => "5924a649-b5d3-4baf-a4ab-4b812dde97a0",
"ruleIds" => []);
public function testGetUser() {
$mockedResponse = array("isEnrolled" => false,
"accessToken" => "xxxx",
"url" => "wwwww");

self::$server->setResponseOfPath('/v1/users/123%3Atest/actions/signIn', new Response(json_encode($mockedResponse)));
self::$server->setResponseOfPath("/v1/users/123%3Atest", new Response(json_encode($mockedResponse)));

$params = array(
"userId" => "123:test",
"action" => "signIn",
"attributes" => array(
"redirectUrl" => "https://www.yourapp.com/back_to_your_app",
"email" => "test@email",
"deviceId" => "123",
"userAgent" => "Mozilla/5.0 (platform; rv:geckoversion) Gecko/geckotrail Firefox/firefoxversion",
"ipAddress" => "1.1.1.1",
"custom" => array(
"yourCustomBoolean" => true,
"yourCustomString" => true,
"yourCustomNumber" => 1.12
)
)
"redirectUrl" => "https://www.example.com/"
);

$response = Authsignal::track($params);

$this->assertEquals($response["state"], "ALLOW");
$this->assertEquals($response["idempotencyKey"], $mockedResponse["idempotencyKey"]);
$response = Authsignal::getUser($params);
$this->assertEquals($response["isEnrolled"], $mockedResponse["isEnrolled"]);
$this->assertEquals($response["url"], $mockedResponse["url"]);
}

public function testGetAction() {
// Mock response
$mockedResponse = array("state" => "ALLOW",
"idempotencyKey" => "5924a649-b5d3-4baf-a4ab-4b812dde97a0",
"stateUpdatedAt" => "2022-07-25T03:19:00.316Z",
"createdAt" => "2022-07-25T03:19:00.316Z",
"ruleIds" => []);

self::$server->setResponseOfPath("/v1/users/123%3Atest/actions/signIn/5924a649-b5d3-4baf-a4ab-4b812dde97a04", new Response(json_encode($mockedResponse)));

public function testUpdateUser() {
$mockedResponse = array(
"userId" => "550e8400-e29b-41d4-a716-446655440000",
"email" => "updated_email",
);

self::$server->setResponseOfPath("/v1/users/550e8400-e29b-41d4-a716-446655440000", new Response(json_encode($mockedResponse)));

$params = array(
"userId" => "123:test",
"action" => "signIn",
"idempotencyKey" => "5924a649-b5d3-4baf-a4ab-4b812dde97a04"
"userId" => "550e8400-e29b-41d4-a716-446655440000",
"attributes" => array(
"email" => "updated_email",
)
);

$response = Authsignal::updateUser($params);

$this->assertEquals($response["userId"], $mockedResponse["userId"]);
$this->assertEquals($response["email"], $mockedResponse["email"]);
}

$response = Authsignal::getAction($params);

$this->assertEquals($response["state"], "ALLOW");
$this->assertEquals($response["idempotencyKey"], $mockedResponse["idempotencyKey"]);
$this->assertEquals($response["stateUpdatedAt"], $mockedResponse["stateUpdatedAt"]);
public function testDeleteUser() {
$mockedResponse = array("success" => true);

self::$server->setResponseOfPath("/v1/users/1234", new Response(json_encode($mockedResponse), [], 200));

$params = array("userId" => "1234");
$response = Authsignal::deleteUser($params);

$this->assertEquals($response["success"], true);
}

public function testGetUser() {
$mockedResponse = array("isEnrolled" => false,
"accessToken" => "xxxx",
"url" => "wwwww");
public function testGetAuthenticators() {
$mockedResponse = array(
array(
"userAuthenticatorId" => "authenticator_id_1",
"authenticatorType" => "SMS",
"isDefault" => true,
"phoneNumber" => "+6427000000"
),
array(
"userAuthenticatorId" => "authenticator_id_2",
"authenticatorType" => "EMAIL",
"isDefault" => false,
"email" => "[email protected]"
)
);

self::$server->setResponseOfPath("/v1/users/123%3Atest", new Response(json_encode($mockedResponse)));
self::$server->setResponseOfPath("/v1/users/123%3Atest/authenticators", new Response(json_encode($mockedResponse)));

$params = array(
"userId" => "123:test",
"redirectUrl" => "https://www.example.com/"
"userId" => "123:test"
);

$response = Authsignal::getUser($params);

$this->assertEquals($response["isEnrolled"], $mockedResponse["isEnrolled"]);
$this->assertEquals($response["url"], $mockedResponse["url"]);
}
$response = Authsignal::getAuthenticators($params);

$this->assertIsArray($response);
$this->assertCount(2, $response);
$this->assertEquals($response[0]["userAuthenticatorId"], $mockedResponse[0]["userAuthenticatorId"]);
$this->assertEquals($response[1]["userAuthenticatorId"], $mockedResponse[1]["userAuthenticatorId"]);
}

public function testEnrollVerifiedAuthenticator() {
$mockedResponse = array(
Expand All @@ -123,47 +127,57 @@ public function testEnrollVerifiedAuthenticator() {
$this->assertEquals($response["authenticator"]["userAuthenticatorId"], $mockedResponse["authenticator"]["userAuthenticatorId"]);
}

public function testValidateChallenge() {
$mockedResponse = array("state" => "CHALLENGE_SUCCEEDED",
"idempotencyKey" => "5924a649-b5d3-4baf-a4ab-4b812dde97a0",
"stateUpdatedAt" => "2022-07-25T03:19:00.316Z",
"userId" => "123:test",
"isValid" => "true",
"action" => "signIn",
"verificationMethod" => "AUTHENTICATOR_APP");
public function testDeleteAuthenticator() {
$mockedResponse = array("success" => true);

self::$server->setResponseOfPath("/v1/users/123%3Atest/authenticators/456%3Atest", new Response(json_encode($mockedResponse), [], 200));

$params = array(
"userId" => "123:test",
"userAuthenticatorId" => "456:test"
);
$response = Authsignal::deleteAuthenticator($params);

$this->assertArrayHasKey("success", $response);
$this->assertEquals($response["success"], true);
}

self::$server->setResponseOfPath("/v1/validate", new Response(json_encode($mockedResponse)));
public function testTrackAction() {
// Mock response
$mockedResponse = array("state" => "ALLOW",
"idempotencyKey" => "5924a649-b5d3-4baf-a4ab-4b812dde97a0",
"ruleIds" => []);

$key = "secret";
$testTokenPayload = [
'iss' => 'http://example.org',
'aud' => 'http://example.com',
'iat' => 1356999524,
'nbf' => 1357000000,
'other' => [
'userId' => "123:test",
'state' => "CHALLENGE_SUCCEEDED",
'action' => 'signIn',
'idempotencyKey' => "5924a649-b5d3-4baf-a4ab-4b812dde97a0",
]
];
$token = JWT::encode($testTokenPayload, $key, 'HS256');
self::$server->setResponseOfPath('/v1/users/123%3Atest/actions/signIn', new Response(json_encode($mockedResponse)));

$params = array(
"userId" => "123:test",
"token" => $token
"action" => "signIn",
"attributes" => array(
"redirectUrl" => "https://www.yourapp.com/back_to_your_app",
"email" => "test@email",
"deviceId" => "123",
"userAgent" => "Mozilla/5.0 (platform; rv:geckoversion) Gecko/geckotrail Firefox/firefoxversion",
"ipAddress" => "1.1.1.1",
"custom" => array(
"yourCustomBoolean" => true,
"yourCustomString" => true,
"yourCustomNumber" => 1.12
)
)
);

$response = Authsignal::validateChallenge($params);
$response = Authsignal::track($params);

$this->assertEquals($response['isValid'], "true");
$this->assertEquals($response["state"], "ALLOW");
$this->assertEquals($response["idempotencyKey"], $mockedResponse["idempotencyKey"]);
}

public function testValidateChallengeOptionalUserId() {
public function testValidateChallenge() {
$mockedResponse = array("state" => "CHALLENGE_SUCCEEDED",
"idempotencyKey" => "5924a649-b5d3-4baf-a4ab-4b812dde97a0",
"stateUpdatedAt" => "2022-07-25T03:19:00.316Z",
"userId" => null,
"userId" => "123:test",
"isValid" => "true",
"action" => "signIn",
"verificationMethod" => "AUTHENTICATOR_APP");
Expand All @@ -177,6 +191,7 @@ public function testValidateChallengeOptionalUserId() {
'iat' => 1356999524,
'nbf' => 1357000000,
'other' => [
'userId' => "123:test",
'state' => "CHALLENGE_SUCCEEDED",
'action' => 'signIn',
'idempotencyKey' => "5924a649-b5d3-4baf-a4ab-4b812dde97a0",
Expand All @@ -185,122 +200,36 @@ public function testValidateChallengeOptionalUserId() {
$token = JWT::encode($testTokenPayload, $key, 'HS256');

$params = array(
"userId" => "123:test",
"token" => $token
);

$response = Authsignal::validateChallenge($params);

$this->assertEquals($response["isValid"], "true");
$this->assertEquals($response['isValid'], "true");
}

public function testValidateChallengeInvalidAction() {
$mockedResponse = array(
"isValid" => false,
"error" => "Action is invalid."
);

self::$server->setResponseOfPath("/v1/validate", new Response(json_encode($mockedResponse)));

$key = "secret";
$testTokenPayload = [
'iss' => 'http://example.org',
'aud' => 'http://example.com',
'iat' => 1356999524,
'nbf' => 1357000000,
'other' => [
'state' => "CHALLENGE_SUCCEEDED",
'action' => 'signIn',
'idempotencyKey' => "5924a649-b5d3-4baf-a4ab-4b812dde97a0",
]
];
$token = JWT::encode($testTokenPayload, $key, 'HS256');

$params = array(
"token" => $token,
"action" => "malicious_action"
);

$response = Authsignal::validateChallenge($params);

$this->assertEquals($response["isValid"], false);
$this->assertEquals($response["error"], "Action is invalid.");
}
public function testGetAction() {
// Mock response
$mockedResponse = array("state" => "ALLOW",
"idempotencyKey" => "5924a649-b5d3-4baf-a4ab-4b812dde97a0",
"stateUpdatedAt" => "2022-07-25T03:19:00.316Z",
"createdAt" => "2022-07-25T03:19:00.316Z",
"ruleIds" => []);

public function testDeleteUser() {
$mockedResponse = array("success" => true);

self::$server->setResponseOfPath("/v1/users/1234", new Response(json_encode($mockedResponse), [], 200));

$params = array("userId" => "1234");
$response = Authsignal::deleteUser($params);

$this->assertEquals($response["success"], true);
}
self::$server->setResponseOfPath("/v1/users/123%3Atest/actions/signIn/5924a649-b5d3-4baf-a4ab-4b812dde97a04", new Response(json_encode($mockedResponse)));

public function testDeleteAuthenticator() {
$mockedResponse = array("success" => true);

self::$server->setResponseOfPath("/v1/users/123%3Atest/authenticators/456%3Atest", new Response(json_encode($mockedResponse), [], 200));

$params = array(
"userId" => "123:test",
"userAuthenticatorId" => "456:test"
);
$response = Authsignal::deleteAuthenticator($params);

$this->assertArrayHasKey("success", $response);
$this->assertEquals($response["success"], true);
}

public function testUpdateUser() {
$mockedResponse = array(
"userId" => "550e8400-e29b-41d4-a716-446655440000",
"email" => "updated_email",
);

self::$server->setResponseOfPath("/v1/users/550e8400-e29b-41d4-a716-446655440000", new Response(json_encode($mockedResponse)));

$params = array(
"userId" => "550e8400-e29b-41d4-a716-446655440000",
"attributes" => array(
"email" => "updated_email",
)
);

$response = Authsignal::updateUser($params);

$this->assertEquals($response["userId"], $mockedResponse["userId"]);
$this->assertEquals($response["email"], $mockedResponse["email"]);
}

public function testGetAuthenticators() {
$mockedResponse = array(
array(
"userAuthenticatorId" => "authenticator_id_1",
"authenticatorType" => "SMS",
"isDefault" => true,
"phoneNumber" => "+6427000000"
),
array(
"userAuthenticatorId" => "authenticator_id_2",
"authenticatorType" => "EMAIL",
"isDefault" => false,
"email" => "[email protected]"
)
);

self::$server->setResponseOfPath("/v1/users/123%3Atest/authenticators", new Response(json_encode($mockedResponse)));

$params = array(
"userId" => "123:test"
"action" => "signIn",
"idempotencyKey" => "5924a649-b5d3-4baf-a4ab-4b812dde97a04"
);

$response = Authsignal::getAuthenticators($params);
$response = Authsignal::getAction($params);

$this->assertIsArray($response);
$this->assertCount(2, $response);
$this->assertEquals($response[0]["userAuthenticatorId"], $mockedResponse[0]["userAuthenticatorId"]);
$this->assertEquals($response[1]["userAuthenticatorId"], $mockedResponse[1]["userAuthenticatorId"]);
$this->assertEquals($response["state"], "ALLOW");
$this->assertEquals($response["idempotencyKey"], $mockedResponse["idempotencyKey"]);
$this->assertEquals($response["stateUpdatedAt"], $mockedResponse["stateUpdatedAt"]);
}

public function testUpdateAction() {
Expand Down

0 comments on commit 495fb9b

Please sign in to comment.