Skip to content

Commit 42be3bd

Browse files
authored
Merge pull request #51 from packagist/f/improved-ux
Improve developer UX for token API
2 parents 42676ff + 5b2c130 commit 42be3bd

File tree

5 files changed

+52
-1
lines changed

5 files changed

+52
-1
lines changed

src/Api/Tokens.php

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ public function all()
2020

2121
public function create(array $tokenData)
2222
{
23+
if (isset($tokenData['teamId'], $tokenData['accessToAllPackages'])) {
24+
throw new InvalidArgumentException('Only set either "accessToAllPackages" or "teamId"');
25+
}
26+
2327
return $this->post('/tokens/', $tokenData);
2428
}
2529

src/HttpClient/Plugin/ExceptionThrower.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,15 @@ protected function doHandleRequest(RequestInterface $request, callable $next, ca
4747
}
4848
}
4949

50-
throw new HttpTransportException(isset($content['message']) ? $content['message'] : $content, $response->getStatusCode(), $request->getUri());
50+
if (isset($content['message'])) {
51+
$message = $content['message'];
52+
} elseif (is_string($content)) {
53+
$message = $content;
54+
} else {
55+
$message = json_encode($content);
56+
}
57+
58+
throw new HttpTransportException($message, $response->getStatusCode(), $request->getUri());
5159
});
5260
}
5361
}

src/HttpClient/Plugin/RequestSignature.php

+4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ class RequestSignature implements Plugin
2727
*/
2828
public function __construct($token, $secret)
2929
{
30+
if (!$token || !$secret) {
31+
throw new \InvalidArgumentException('$token and $secret must be set');
32+
}
33+
3034
$this->token = $token;
3135
$this->secret = $secret;
3236
}

tests/Api/TokensTest.php

+16
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,22 @@ public function testCreate()
6767
]));
6868
}
6969

70+
public function testCreateTeamIdAndAllAccess()
71+
{
72+
$this->expectException(\InvalidArgumentException::class);
73+
/** @var Tokens&MockObject $api */
74+
$api = $this->getApiMock();
75+
$api->expects($this->never())
76+
->method('post');
77+
78+
$api->create([
79+
'description' => 'Team Token',
80+
'access' => 'read',
81+
'teamId' => 1,
82+
'accessToAllPackages' => true,
83+
]);
84+
}
85+
7086
public function testRemove()
7187
{
7288
$expected = [];

tests/HttpClient/Plugin/RequestSignatureTest.php

+19
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,23 @@ public function testPrefixRequestPathSmoke()
6565

6666
$this->assertNotNull($promise->wait(true)->getHeader('Authorization')[0]);
6767
}
68+
69+
/**
70+
* @dataProvider tokenSecretProvider
71+
*/
72+
public function testMissingTokenOrSecret(string $token, string $secret): void
73+
{
74+
$this->expectException(\InvalidArgumentException::class);
75+
76+
new RequestSignature($token, $secret);
77+
}
78+
79+
public function tokenSecretProvider(): array
80+
{
81+
return [
82+
['', ''],
83+
['token', ''],
84+
['', 'secret'],
85+
];
86+
}
6887
}

0 commit comments

Comments
 (0)