Skip to content

Commit bb6d49c

Browse files
committed
updated tests
1 parent 8fda710 commit bb6d49c

File tree

6 files changed

+54
-73
lines changed

6 files changed

+54
-73
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,17 @@ return [
9797
| app engine or compute engine will be used.
9898
|
9999
*/
100-
'service' => ''
100+
'service' => [
101+
/*
102+
| Enable service account auth or not.
103+
*/
104+
'enabled' => false,
105+
106+
/*
107+
| Path to service account json file
108+
*/
109+
'file' => '',
110+
],
101111
];
102112

103113
```

src/Client.php

Lines changed: 15 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Client
2020
* @param array $config
2121
* @param string $userEmail
2222
*/
23-
public function __construct(array $config, $userEmail='')
23+
public function __construct(array $config, $userEmail = '')
2424
{
2525
$this->config = $config;
2626

@@ -85,56 +85,37 @@ public function make($service)
8585
* @param $userEmail
8686
* @return void
8787
*/
88-
protected function auth($userEmail='')
88+
protected function auth($userEmail = '')
8989
{
9090
// see (and use) if user has set Credentials
9191
if ($this->useAssertCredentials($userEmail)) {
9292
return;
9393
}
9494

95-
// check (and use) if running on app engine
96-
if ($this->useAppEngine()) {
97-
return;
98-
}
99-
100-
// fallback to compute engine
95+
// fallback to compute engine or app engine
10196
$this->client->useApplicationDefaultCredentials();
10297
}
10398

10499
/**
105100
* Determine and use credentials if user has set them.
106101
* @param $userEmail
107-
* @return boolean used or not
102+
* @return bool used or not
108103
*/
109-
protected function useAssertCredentials($userEmail='')
104+
protected function useAssertCredentials($userEmail = '')
110105
{
111-
$serviceJsonUrl = array_get($this->config, 'service', '');
112-
if (!empty($serviceJsonUrl)) {
113-
$this->client->setAuthConfig($serviceJsonUrl);
114-
if ($userEmail) {
115-
$this->client->setSubject($userEmail);
116-
}
117-
return true;
118-
}
106+
$serviceJsonUrl = array_get($this->config, 'service.file', '');
119107

120-
return false;
121-
}
108+
if (empty($serviceJsonUrl)) {
109+
return false;
110+
}
122111

123-
/**
124-
* Determine and use app engine credentials
125-
* if running on app engine.
126-
*
127-
* @return boolean used or not
128-
*/
129-
protected function useAppEngine()
130-
{
131-
// if running on app engine
132-
if ($this->client->isAppEngine()) {
133-
$this->client->useApplicationDefaultCredentials();
134-
return true;
112+
$this->client->setAuthConfig($serviceJsonUrl);
113+
114+
if ($userEmail) {
115+
$this->client->setSubject($userEmail);
135116
}
136117

137-
return false;
118+
return true;
138119
}
139120

140121
/**
@@ -155,4 +136,4 @@ public function __call($method, $parameters)
155136

156137
throw new \BadMethodCallException(sprintf('Method [%s] does not exist.', $method));
157138
}
158-
}
139+
}

src/config/config.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,15 @@
4444
| app engine or compute engine will be used.
4545
|
4646
*/
47-
'service' => env('GOOGLE_SERVICE_ACCOUNT_JSON_LOCATION', '')
47+
'service' => [
48+
/*
49+
| Enable service account auth or not.
50+
*/
51+
'enable' => env('GOOGLE_SERVICE_ENABLED', false),
52+
53+
/*
54+
| Path to service account json file
55+
*/
56+
'file' => env('GOOGLE_SERVICE_ACCOUNT_JSON_LOCATION', '')
57+
],
4858
];

tests/ClientTest.php

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -44,54 +44,22 @@ public function testMagicMethodException()
4444
$client->getAuthTest();
4545
}
4646

47-
/*
48-
* TODO: Rewrite these tests
49-
*/
50-
51-
/*public function testDefaultAuth()
47+
public function testNoCredentials()
5248
{
5349
$client = new \PulkitJalan\Google\Client([]);
5450

55-
$this->assertInstanceOf('Google_Auth_OAuth2', $client->getAuth());
56-
}
57-
58-
public function testAssertCredentials()
59-
{
60-
$client = new \PulkitJalan\Google\Client([
61-
'service' => [
62-
'enable' => true,
63-
'account' => 'name',
64-
'scopes' => ['scope'],
65-
'key' => __DIR__.'/data/cert.p12',
66-
],
67-
]);
68-
69-
$this->assertInstanceOf('Google_Auth_OAuth2', $client->getAuth());
51+
$this->assertFalse($client->isUsingApplicationDefaultCredentials());
7052
}
7153

72-
public function testAppEngineCredentials()
54+
public function testDefaultCredentials()
7355
{
74-
$_SERVER['SERVER_SOFTWARE'] = 'Google App Engine';
75-
7656
$client = new \PulkitJalan\Google\Client([
7757
'service' => [
7858
'enable' => true,
59+
'file' => __DIR__.'/data/test.json',
7960
],
8061
]);
8162

82-
$this->assertInstanceOf('Google_Auth_AppIdentity', $client->getAuth());
83-
84-
unset($_SERVER['SERVER_SOFTWARE']);
63+
$this->assertTrue($client->isUsingApplicationDefaultCredentials());
8564
}
86-
87-
public function testComputeEngineCredentials()
88-
{
89-
$client = new \PulkitJalan\Google\Client([
90-
'service' => [
91-
'enable' => true,
92-
],
93-
]);
94-
95-
$this->assertInstanceOf('Google_Auth_ComputeEngine', $client->getAuth());
96-
}*/
9765
}

tests/data/cert.p12

-2.14 KB
Binary file not shown.

tests/data/test.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"type": "service_account",
3+
"project_id": "12345",
4+
"private_key_id": "12345",
5+
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC4Oj1gs8sOUm43\n325pnsaW0tkk6kNSWd0z2OQop7QIyXpmEYXLA2yZEqfsEyaCZey5WC4EE69AdkHZ\nDu+RpoKl4gc46elqOzxFSjaY3T3VY0nBbCFdtuHfED8+1VPGbGBe/XqVDWlrLs4o\n4/AZTA2gzt6+rXZIs0IIyvCLJdtP34uViOh/orjdnEZas9aBwzqcjUgDtj6G8UGi\nKpa4b2suyaA30/c7PJCqNciPwc1rpqXnl0Mrg/fmv9A97mpJdCgfvN2dE6CeTjnQ\nqG/zM5pNrv/dwAjf6GwFJK0LUta6tLv6r4WrSUAJCk9lGy2sCALsFEUm2SGaMNab\njBB1S4qvAgMBAAECggEBAJhJHD95mdxNa2qoOT75zrMSLAlF1MyYGDApk8C/kCTL\nV3BT/8LnW7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gbrfVMLa2Go1uPc7yfHxHZd/Bo9C\n30gOYduKt5+/d6OkQgzITOczNfKtIOodjjSzwnQPICDNwjjxzQ47MlHfKraO/ADJJ\ngeJTZGxlf1Oyx+mSWHKda7eX\n-----END PRIVATE KEY-----\n",
6+
"client_email": "[email protected]",
7+
"client_id": "12345",
8+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
9+
"token_uri": "https://accounts.google.com/o/oauth2/token",
10+
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
11+
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/12345%40api-project-12345.apps.iam.gserviceaccount.com"
12+
}

0 commit comments

Comments
 (0)