From ddc56426c28ea057c62a83dee3cf73d51779967c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Fri, 26 Jun 2020 00:02:36 +0200 Subject: [PATCH] MAPG-177 add test for GoogleOAuth --- tests/OAuth/GoogleOAuthTest.php | 90 +++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 tests/OAuth/GoogleOAuthTest.php diff --git a/tests/OAuth/GoogleOAuthTest.php b/tests/OAuth/GoogleOAuthTest.php new file mode 100644 index 0000000..a40fc66 --- /dev/null +++ b/tests/OAuth/GoogleOAuthTest.php @@ -0,0 +1,90 @@ +getMockBuilder(IRequest::class) + ->setMethods(['setUrl', 'setMethod', 'setQuery', 'setHeaders', 'send']) + ->getMock(); + $googleOAuth = new GoogleOAuth($requestMock); + + $dialogUrl = $googleOAuth->getDialogUrl($state, $redirectUrl); + $dialogUrlParsed = explode('?', $dialogUrl); + + $this->assertEquals('https://accounts.google.com/o/oauth2/v2/auth', $dialogUrlParsed[0]); + + parse_str($dialogUrlParsed[1], $dialogUrlQueryParams); + + $expectedQueryParams = [ + 'response_type' => 'code', + 'client_id' => $_ENV['GOOGLE_OAUTH_CLIENT_ID'], + 'scope' => 'openid email', + 'redirect_uri' => $redirectUrl, + 'state' => $state, + 'nonce' => hash('sha256', random_bytes(10) . microtime()), + ]; + + $this->assertEquals($expectedQueryParams['response_type'], $dialogUrlQueryParams['response_type']); + $this->assertEquals($expectedQueryParams['client_id'], $dialogUrlQueryParams['client_id']); + $this->assertEquals($expectedQueryParams['scope'], $dialogUrlQueryParams['scope']); + $this->assertEquals($expectedQueryParams['redirect_uri'], $dialogUrlQueryParams['redirect_uri']); + $this->assertEquals($expectedQueryParams['state'], $dialogUrlQueryParams['state']); + $this->assertMatchesRegularExpression('/^[a-f0-9]{64}$/', $dialogUrlQueryParams['nonce']); + } + + public function testCanRequestToken(): void + { + $_ENV['GOOGLE_OAUTH_CLIENT_ID'] = 'abc'; + $_ENV['GOOGLE_OAUTH_CLIENT_SECRET'] = 'xxx'; + $code = 'code_from_google'; + $redirectUrl = 'http://example.com/oauth'; + + $requestMock = $this->getMockBuilder(IRequest::class) + ->setMethods(['setUrl', 'setMethod', 'setQuery', 'setHeaders', 'send']) + ->getMock(); + $responseMock = $this->getMockBuilder(IResponse::class) + ->setMethods(['getBody', 'getHeaders']) + ->getMock(); + $googleOAuth = new GoogleOAuth($requestMock); + + $expectedQueryParams = [ + 'code' => $code, + 'client_id' => $_ENV['GOOGLE_OAUTH_CLIENT_ID'], + 'client_secret' => $_ENV['GOOGLE_OAUTH_CLIENT_SECRET'], + 'redirect_uri' => $redirectUrl, + 'grant_type' => 'authorization_code', + ]; + + $requestMock->expects($this->once()) + ->method('setUrl') + ->with($this->equalTo('https://oauth2.googleapis.com/token')); + $requestMock->expects($this->once()) + ->method('setMethod') + ->with($this->equalTo(IRequest::HTTP_POST)); + $requestMock->expects($this->once()) + ->method('setQuery') + ->with($this->equalTo($expectedQueryParams)); + $requestMock->expects($this->once()) + ->method('send') + ->will($this->returnValue($responseMock)); + $responseMock->expects($this->once()) + ->method('getBody') + ->will($this->returnValue('{"test":"json"}')); + + $token = $googleOAuth->getToken($code, $redirectUrl); + + $this->assertEquals(['test' => 'json'], $token); + } +}