Skip to content

Commit

Permalink
Refactoring tests (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
carusogabriel authored and bshaffer committed Dec 6, 2017
1 parent 8baf334 commit f3fc99f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 37 deletions.
8 changes: 3 additions & 5 deletions tests/Credentials/IAMCredentialsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,15 @@ public function testUpdateMetadataFunc()
);

$update_metadata = $iam->getUpdateMetadataFunc();
$this->assertTrue(is_callable($update_metadata));
$this->assertInternalType('callable', $update_metadata);

$actual_metadata = call_user_func($update_metadata,
$metadata = array('foo' => 'bar'));
$this->assertTrue(
isset($actual_metadata[IAMCredentials::SELECTOR_KEY]));
$this->assertArrayHasKey(IAMCredentials::SELECTOR_KEY, $actual_metadata);
$this->assertEquals(
$actual_metadata[IAMCredentials::SELECTOR_KEY],
$selector);
$this->assertTrue(
isset($actual_metadata[IAMCredentials::TOKEN_KEY]));
$this->assertArrayHasKey(IAMCredentials::TOKEN_KEY, $actual_metadata);
$this->assertEquals(
$actual_metadata[IAMCredentials::TOKEN_KEY],
$token);
Expand Down
74 changes: 43 additions & 31 deletions tests/Credentials/ServiceAccountCredentialsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,14 +294,16 @@ public function testUpdateMetadataFunc()
$testJson
);
$update_metadata = $sa->getUpdateMetadataFunc();
$this->assertTrue(is_callable($update_metadata));
$this->assertInternalType('callable', $update_metadata);

$actual_metadata = call_user_func($update_metadata,
$metadata = array('foo' => 'bar'),
$authUri = null,
$httpHandler);
$this->assertTrue(
isset($actual_metadata[CredentialsLoader::AUTH_METADATA_KEY]));
$this->assertArrayHasKey(
CredentialsLoader::AUTH_METADATA_KEY,
$actual_metadata
);
$this->assertEquals(
$actual_metadata[CredentialsLoader::AUTH_METADATA_KEY],
array('Bearer ' . $access_token));
Expand Down Expand Up @@ -383,13 +385,15 @@ public function testAuthUriIsNotSet()
$this->assertNotNull($sa);

$update_metadata = $sa->getUpdateMetadataFunc();
$this->assertTrue(is_callable($update_metadata));
$this->assertInternalType('callable', $update_metadata);

$actual_metadata = call_user_func($update_metadata,
$metadata = array('foo' => 'bar'),
$authUri = null);
$this->assertTrue(
!isset($actual_metadata[CredentialsLoader::AUTH_METADATA_KEY]));
$this->assertArrayNotHasKey(
CredentialsLoader::AUTH_METADATA_KEY,
$actual_metadata
);
}

public function testUpdateMetadataFunc()
Expand All @@ -401,36 +405,40 @@ public function testUpdateMetadataFunc()
$this->assertNotNull($sa);

$update_metadata = $sa->getUpdateMetadataFunc();
$this->assertTrue(is_callable($update_metadata));
$this->assertInternalType('callable', $update_metadata);

$actual_metadata = call_user_func($update_metadata,
$metadata = array('foo' => 'bar'),
$authUri = 'https://example.com/service');
$this->assertTrue(
isset($actual_metadata[CredentialsLoader::AUTH_METADATA_KEY]));
$this->assertArrayHasKey(
CredentialsLoader::AUTH_METADATA_KEY,
$actual_metadata
);

$authorization = $actual_metadata[CredentialsLoader::AUTH_METADATA_KEY];
$this->assertTrue(is_array($authorization));
$this->assertInternalType('array', $authorization);

$bearer_token = current($authorization);
$this->assertTrue(is_string($bearer_token));
$this->assertTrue(strpos($bearer_token, 'Bearer ') == 0);
$this->assertTrue(strlen($bearer_token) > 30);
$this->assertInternalType('string', $bearer_token);
$this->assertEquals(0, strpos($bearer_token, 'Bearer '));
$this->assertGreaterThan(30, strlen($bearer_token));

$actual_metadata2 = call_user_func($update_metadata,
$metadata = array('foo' => 'bar'),
$authUri = 'https://example.com/anotherService');
$this->assertTrue(
isset($actual_metadata2[CredentialsLoader::AUTH_METADATA_KEY]));
$this->assertArrayHasKey(
CredentialsLoader::AUTH_METADATA_KEY,
$actual_metadata2
);

$authorization2 = $actual_metadata2[CredentialsLoader::AUTH_METADATA_KEY];
$this->assertTrue(is_array($authorization2));
$this->assertInternalType('array', $authorization2);

$bearer_token2 = current($authorization2);
$this->assertTrue(is_string($bearer_token2));
$this->assertTrue(strpos($bearer_token2, 'Bearer ') == 0);
$this->assertTrue(strlen($bearer_token2) > 30);
$this->assertTrue($bearer_token != $bearer_token2);
$this->assertInternalType('string', $bearer_token2);
$this->assertEquals(0, strpos($bearer_token2, 'Bearer '));
$this->assertGreaterThan(30, strlen($bearer_token2));
$this->assertNotEquals($bearer_token2, $bearer_token);
}
}

Expand Down Expand Up @@ -465,21 +473,23 @@ public function testNoScopeUseJwtAccess()
$this->assertNotNull($sa);

$update_metadata = $sa->getUpdateMetadataFunc();
$this->assertTrue(is_callable($update_metadata));
$this->assertInternalType('callable', $update_metadata);

$actual_metadata = call_user_func($update_metadata,
$metadata = array('foo' => 'bar'),
$authUri = 'https://example.com/service');
$this->assertTrue(
isset($actual_metadata[CredentialsLoader::AUTH_METADATA_KEY]));
$this->assertArrayHasKey(
CredentialsLoader::AUTH_METADATA_KEY,
$actual_metadata
);

$authorization = $actual_metadata[CredentialsLoader::AUTH_METADATA_KEY];
$this->assertTrue(is_array($authorization));
$this->assertInternalType('array', $authorization);

$bearer_token = current($authorization);
$this->assertTrue(is_string($bearer_token));
$this->assertTrue(strpos($bearer_token, 'Bearer ') == 0);
$this->assertTrue(strlen($bearer_token) > 30);
$this->assertInternalType('string', $bearer_token);
$this->assertEquals(0, strpos($bearer_token, 'Bearer '));
$this->assertGreaterThan(30, strlen($bearer_token));
}

public function testNoScopeAndNoAuthUri()
Expand All @@ -495,15 +505,17 @@ public function testNoScopeAndNoAuthUri()
$this->assertNotNull($sa);

$update_metadata = $sa->getUpdateMetadataFunc();
$this->assertTrue(is_callable($update_metadata));
$this->assertInternalType('callable', $update_metadata);

$actual_metadata = call_user_func($update_metadata,
$metadata = array('foo' => 'bar'),
$authUri = null);
// no access_token is added to the metadata hash
// but also, no error should be thrown
$this->assertTrue(is_array($actual_metadata));
$this->assertTrue(
!isset($actual_metadata[CredentialsLoader::AUTH_METADATA_KEY]));
$this->assertInternalType('array', $actual_metadata);
$this->assertArrayNotHasKey(
CredentialsLoader::AUTH_METADATA_KEY,
$actual_metadata
);
}
}
2 changes: 1 addition & 1 deletion tests/OAuth2Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ public function testGeneratesAssertionRequests()
$this->assertEquals('POST', $req->getMethod());
$fields = Psr7\parse_query((string)$req->getBody());
$this->assertEquals(OAuth2::JWT_URN, $fields['grant_type']);
$this->assertTrue(array_key_exists('assertion', $fields));
$this->assertArrayHasKey('assertion', $fields);
}

public function testGeneratesExtendedRequests()
Expand Down

0 comments on commit f3fc99f

Please sign in to comment.