PageRenderTime 25ms CodeModel.GetById 14ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Google/vendor/google/auth/tests/Credentials/GCECredentialsTest.php

https://gitlab.com/vangtrangbac123/zinkaiuit.tk
PHP | 124 lines | 93 code | 12 blank | 19 comment | 0 complexity | 11348dc49703cc34d5254548f7c90370 MD5 | raw file
  1<?php
  2/*
  3 * Copyright 2015 Google Inc.
  4 *
  5 * Licensed under the Apache License, Version 2.0 (the "License");
  6 * you may not use this file except in compliance with the License.
  7 * You may obtain a copy of the License at
  8 *
  9 *     http://www.apache.org/licenses/LICENSE-2.0
 10 *
 11 * Unless required by applicable law or agreed to in writing, software
 12 * distributed under the License is distributed on an "AS IS" BASIS,
 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14 * See the License for the specific language governing permissions and
 15 * limitations under the License.
 16 */
 17
 18namespace Google\Auth\Tests;
 19
 20use Google\Auth\Credentials\GCECredentials;
 21use GuzzleHttp\Psr7;
 22use GuzzleHttp\Psr7\Response;
 23
 24class GCECredentialsOnGCETest extends \PHPUnit_Framework_TestCase
 25{
 26    public function testIsFalseOnClientErrorStatus()
 27    {
 28        $httpHandler = getHandler([
 29            buildResponse(400),
 30        ]);
 31        $this->assertFalse(GCECredentials::onGCE($httpHandler));
 32    }
 33
 34    public function testIsFalseOnServerErrorStatus()
 35    {
 36        $httpHandler = getHandler([
 37            buildResponse(500),
 38        ]);
 39        $this->assertFalse(GCECredentials::onGCE($httpHandler));
 40    }
 41
 42    public function testIsFalseOnOkStatusWithoutExpectedHeader()
 43    {
 44        $httpHandler = getHandler([
 45            buildResponse(200),
 46        ]);
 47        $this->assertFalse(GCECredentials::onGCE($httpHandler));
 48    }
 49
 50    public function testIsOkIfGoogleIsTheFlavor()
 51    {
 52        $httpHandler = getHandler([
 53            buildResponse(200, [GCECredentials::FLAVOR_HEADER => 'Google']),
 54        ]);
 55        $this->assertTrue(GCECredentials::onGCE($httpHandler));
 56    }
 57}
 58
 59class GCECredentialsOnAppEngineFlexibleTest extends \PHPUnit_Framework_TestCase
 60{
 61    public function testIsFalseByDefault()
 62    {
 63        $this->assertFalse(GCECredentials::onAppEngineFlexible());
 64    }
 65
 66    public function testIsTrueWhenGaeVmIsTrue()
 67    {
 68        $_SERVER['GAE_VM'] = 'true';
 69        $this->assertTrue(GCECredentials::onAppEngineFlexible());
 70    }
 71}
 72
 73class GCECredentialsGetCacheKeyTest extends \PHPUnit_Framework_TestCase
 74{
 75    public function testShouldNotBeEmpty()
 76    {
 77        $g = new GCECredentials();
 78        $this->assertNotEmpty($g->getCacheKey());
 79    }
 80}
 81
 82class GCECredentialsFetchAuthTokenTest extends \PHPUnit_Framework_TestCase
 83{
 84    public function testShouldBeEmptyIfNotOnGCE()
 85    {
 86        $httpHandler = getHandler([
 87            buildResponse(500),
 88        ]);
 89        $g = new GCECredentials();
 90        $this->assertEquals(array(), $g->fetchAuthToken($httpHandler));
 91    }
 92
 93    /**
 94     * @expectedException Exception
 95     * @expectedExceptionMessage Invalid JSON response
 96     */
 97    public function testShouldFailIfResponseIsNotJson()
 98    {
 99        $notJson = '{"foo": , this is cannot be passed as json" "bar"}';
100        $httpHandler = getHandler([
101            buildResponse(200, [GCECredentials::FLAVOR_HEADER => 'Google']),
102            buildResponse(200, [], $notJson),
103        ]);
104        $g = new GCECredentials();
105        $g->fetchAuthToken($httpHandler);
106    }
107
108    public function testShouldReturnTokenInfo()
109    {
110        $wantedTokens = [
111            'access_token' => '1/abdef1234567890',
112            'expires_in' => '57',
113            'token_type' => 'Bearer',
114        ];
115        $jsonTokens = json_encode($wantedTokens);
116        $httpHandler = getHandler([
117            buildResponse(200, [GCECredentials::FLAVOR_HEADER => 'Google']),
118            buildResponse(200, [], Psr7\stream_for($jsonTokens)),
119        ]);
120        $g = new GCECredentials();
121        $this->assertEquals($wantedTokens, $g->fetchAuthToken($httpHandler));
122        $this->assertEquals(time() + 57, $g->getLastReceivedToken()['expires_at']);
123    }
124}