-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reorder tests to match order of SDK functions
- Loading branch information
1 parent
1ef8ff5
commit 495fb9b
Showing
1 changed file
with
116 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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( | ||
|
@@ -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"); | ||
|
@@ -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", | ||
|
@@ -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() { | ||
|