Skip to content

Commit

Permalink
Iterate
Browse files Browse the repository at this point in the history
1) Fix indentation
2) Simplify `runTestCase` method in AuthTokenMiddlewareTest
3) Improve testing.
  • Loading branch information
yash30201 committed Nov 15, 2023
1 parent 60a5d58 commit 850c41b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
8 changes: 4 additions & 4 deletions tests/FetchAuthTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ class_implements($fetcherClass)

if (is_a($fetcherClass, UpdateMetadataInterface::class, true)) {
$mockFetcher->updateMetadata(Argument::cetera())
->shouldBeCalledTimes(1)->will(function () use (&$httpHandlerCalled) {
$httpHandlerCalled = true;
return ['authorization' => ['Bearer xyz']];
});
->shouldBeCalledTimes(1)->will(function () use (&$httpHandlerCalled) {
$httpHandlerCalled = true;
return ['authorization' => ['Bearer xyz']];
});
} else {
$mockFetcher->fetchAuthToken(Argument::any())
->shouldBeCalledTimes(1)
Expand Down
39 changes: 25 additions & 14 deletions tests/Middleware/AuthTokenMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ public function testOnlyTouchesWhenAuthConfigScoped()
->willReturn([]);
$this->mockRequest->withHeader()->shouldNotBeCalled();

$this->runTestCase($this->mockFetcher->reveal(), false);
$middleware = new AuthTokenMiddleware($this->mockFetcher->reveal());
$mock = new MockHandler([new Response(200)]);
$callable = $middleware($mock);
$callable($this->mockRequest->reveal(), ['auth' => 'not_google_auth']);
}

public function testAddsTheTokenAsAnAuthorizationHeader()
Expand All @@ -63,7 +66,7 @@ public function testAddsTheTokenAsAnAuthorizationHeader()
->shouldBeCalledTimes(1)
->willReturn($this->mockRequest->reveal());

$this->runTestCase($this->mockFetcher->reveal(), true);
$this->runTestCase($this->mockFetcher->reveal());
}

public function testDoesNotAddAnAuthorizationHeaderOnNoAccessToken()
Expand All @@ -75,7 +78,7 @@ public function testDoesNotAddAnAuthorizationHeaderOnNoAccessToken()
$this->mockRequest->withHeader('authorization', 'Bearer ')
->willReturn($this->mockRequest->reveal());

$this->runTestCase($this->mockFetcher->reveal(), true);
$this->runTestCase($this->mockFetcher->reveal());
}

public function testUsesIdTokenWhenAccessTokenDoesNotExist()
Expand All @@ -89,7 +92,7 @@ public function testUsesIdTokenWhenAccessTokenDoesNotExist()
->shouldBeCalledTimes(1)
->willReturn($this->mockRequest->reveal());

$this->runTestCase($this->mockFetcher->reveal(), true);
$this->runTestCase($this->mockFetcher->reveal());

}

Expand Down Expand Up @@ -122,7 +125,7 @@ public function testUsesCachedAccessToken()
null,
$this->mockCache->reveal()
);
$this->runTestCase($cachedFetcher, true);
$this->runTestCase($cachedFetcher);
}

public function testUsesCachedIdToken()
Expand Down Expand Up @@ -154,7 +157,7 @@ public function testUsesCachedIdToken()
null,
$this->mockCache->reveal()
);
$this->runTestCase($cachedFetcher, true);
$this->runTestCase($cachedFetcher);
}

public function testGetsCachedAuthTokenUsingCacheOptions()
Expand Down Expand Up @@ -187,7 +190,7 @@ public function testGetsCachedAuthTokenUsingCacheOptions()
['prefix' => $prefix],
$this->mockCache->reveal()
);
$this->runTestCase($cachedFetcher, true);
$this->runTestCase($cachedFetcher);
}

public function testShouldSaveValueInCacheWithSpecifiedPrefix()
Expand Down Expand Up @@ -228,7 +231,7 @@ public function testShouldSaveValueInCacheWithSpecifiedPrefix()
['prefix' => $prefix, 'lifetime' => $lifetime],
$this->mockCache->reveal()
);
$this->runTestCase($cachedFetcher, true);
$this->runTestCase($cachedFetcher);
}

/**
Expand Down Expand Up @@ -301,9 +304,15 @@ public function testAddAuthHeadersFromUpdateMetadata()
$request = new Request('GET', 'http://foo.com');

$middleware = new AuthTokenMiddleware($this->mockFetcher->reveal());
$mock = new MockHandler([new Response(200)]);
$mockHandlerCalled = false;
$mock = new MockHandler([function ($request, $options) use ($authResult, &$mockHandlerCalled) {
$this->assertEquals($authResult['authorization'], $request->getHeaderLine('authorization'));
$mockHandlerCalled = true;
return new Response(200);
}]);
$callable = $middleware($mock);
$callable($request, ['auth' => 'google_auth']);
$this->assertTrue($mockHandlerCalled);
}

public function testOverlappingAddAuthHeadersFromUpdateMetadata()
Expand All @@ -314,7 +323,6 @@ public function testOverlappingAddAuthHeadersFromUpdateMetadata()
];

$request = new Request('GET', 'http://foo.com');
$request->withHeader('x-goog-api-client', 'default-value');

$this->mockFetcher->willImplement(UpdateMetadataInterface::class);
$this->mockFetcher->updateMetadata(Argument::cetera())
Expand All @@ -324,22 +332,25 @@ public function testOverlappingAddAuthHeadersFromUpdateMetadata()
->willReturn(['access_token' => '1/abcdef1234567890']);

$middleware = new AuthTokenMiddleware($this->mockFetcher->reveal());
$mock = new MockHandler([function ($request, $options) use ($authHeaders) {

$mockHandlerCalled = false;
$mock = new MockHandler([function ($request, $options) use ($authHeaders, &$mockHandlerCalled) {
$this->assertEquals($authHeaders['authorization'], $request->getHeaderLine('authorization'));
$this->assertArrayHasKey('x-goog-api-client', $request->getHeaders());
$mockHandlerCalled = true;
return new Response(200);
}]);
$callable = $middleware($mock);
$callable($request, ['auth' => 'google_auth']);
$this->assertTrue($mockHandlerCalled);
}

private function runTestCase($fetcher, bool $isGoogleAuth)
private function runTestCase($fetcher)
{
$authType = $isGoogleAuth ? 'google_auth' :'not_google_auth';
$middleware = new AuthTokenMiddleware($fetcher);
$mock = new MockHandler([new Response(200)]);
$callable = $middleware($mock);
$callable($this->mockRequest->reveal(), ['auth' => $authType]);
$callable($this->mockRequest->reveal(), ['auth' => 'google_auth']);
}

public function provideShouldNotifyTokenCallback()
Expand Down

0 comments on commit 850c41b

Please sign in to comment.