Skip to content

Commit

Permalink
fix test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskapp committed Mar 17, 2024
1 parent 82b086b commit 3a67173
Show file tree
Hide file tree
Showing 34 changed files with 204 additions and 234 deletions.
3 changes: 1 addition & 2 deletions src/Backend/View/App.php
Expand Up @@ -85,9 +85,8 @@ public function getEntity(int $id, ContextInterface $context)
'scopes' => $builder->doColumn([$this->getTable(Table\App\Scope::class), 'getAvailableScopes'], [$context->getTenantId(), new Reference('id')], 'name'),
'tokens' => $builder->doCollection([$this->getTable(Table\Token::class), 'getTokensByApp'], [$context->getTenantId(), new Reference('id')], [
'id' => $builder->fieldInteger(Table\Generated\TokenTable::COLUMN_ID),
'userId' => $builder->fieldInteger(Table\Generated\TokenTable::COLUMN_USER_ID),
'status' => $builder->fieldInteger(Table\Generated\TokenTable::COLUMN_STATUS),
'token' => Table\Generated\TokenTable::COLUMN_TOKEN,
'name' => Table\Generated\TokenTable::COLUMN_NAME,
'scope' => $builder->fieldCsv(Table\Generated\TokenTable::COLUMN_SCOPE),
'ip' => Table\Generated\TokenTable::COLUMN_IP,
'expire' => Table\Generated\TokenTable::COLUMN_EXPIRE,
Expand Down
3 changes: 1 addition & 2 deletions src/Consumer/View/App.php
Expand Up @@ -92,9 +92,8 @@ public function getEntity(int $appId, ContextInterface $context)
'scopes' => $builder->doColumn([$this->getTable(Table\App\Scope::class), 'getAvailableScopes'], [$context->getTenantId(), new Reference('id')], 'name'),
'tokens' => $builder->doCollection([$this->getTable(Table\Token::class), 'getTokensByApp'], [$context->getTenantId(), new Reference('id')], [
'id' => $builder->fieldInteger(Table\Generated\TokenTable::COLUMN_ID),
'userId' => $builder->fieldInteger(Table\Generated\TokenTable::COLUMN_USER_ID),
'status' => $builder->fieldInteger(Table\Generated\TokenTable::COLUMN_STATUS),
'token' => Table\Generated\TokenTable::COLUMN_TOKEN,
'name' => Table\Generated\TokenTable::COLUMN_NAME,
'scope' => $builder->fieldCsv(Table\Generated\TokenTable::COLUMN_SCOPE),
'ip' => Table\Generated\TokenTable::COLUMN_IP,
'expire' => Table\Generated\TokenTable::COLUMN_EXPIRE,
Expand Down
9 changes: 3 additions & 6 deletions src/Consumer/View/Identity.php
Expand Up @@ -54,15 +54,12 @@ public function getCollection(?int $appId, QueryFilter $filter, ContextInterface
$sortBy = $filter->getSortBy(Table\Generated\IdentityTable::COLUMN_NAME);
$sortOrder = $filter->getSortOrder(OrderBy::ASC);

if (empty($appId)) {
// by default we use the consumer app
$appId = 2;
}

$condition = $filter->getCondition([QueryFilter::COLUMN_SEARCH => Table\Generated\IdentityTable::COLUMN_NAME]);
$condition->equals(Table\Generated\IdentityTable::COLUMN_TENANT_ID, $context->getTenantId());
$condition->equals(Table\Generated\IdentityTable::COLUMN_STATUS, Table\Event::STATUS_ACTIVE);
$condition->equals(Table\Generated\IdentityTable::COLUMN_APP_ID, $appId);
if (!empty($appId)) {
$condition->equals(Table\Generated\IdentityTable::COLUMN_APP_ID, $appId);
}

$builder = new Builder($this->connection);

Expand Down
3 changes: 2 additions & 1 deletion src/Installation/DataBag.php
Expand Up @@ -459,13 +459,14 @@ public function addPlanScope(string $plan, string $scope, ?string $tenantId = nu
];
}

public function addToken(string $app, string $user, string $token, string $refresh, string $scope, string $expire, ?string $date = null, ?string $tenantId = null): void
public function addToken(string $app, string $user, string $name, string $token, string $refresh, string $scope, string $expire, ?string $date = null, ?string $tenantId = null): void
{
$this->data['fusio_token'][] = [
'tenant_id' => $tenantId,
'app_id' => $this->getReference('fusio_app', $app, $tenantId),
'user_id' => $this->getReference('fusio_user', $user, $tenantId),
'status' => Table\Token::STATUS_ACTIVE,
'name' => $name,
'token' => $token,
'refresh' => $refresh,
'scope' => $scope,
Expand Down
8 changes: 1 addition & 7 deletions src/Service/Identity.php
Expand Up @@ -265,19 +265,13 @@ public function exchange(string $identityId, string $code, string $state, UserCo
// get scopes for user
$scopes = $this->userService->getAvailableScopes($userId, $context);

$appId = $existing->getAppId();
if (empty($appId)) {
// if the identity is not assigned to a specific app we use by default the consumer app
$appId = 2;
}

$userAgent = $_SERVER['HTTP_USER_AGENT'] ?? 'n/a';
$ip = $_SERVER['REMOTE_ADDR'] ?? '127.0.0.1';
$name = 'Identity Provider ' . $existing->getName() . ' by ' . $userAgent . ' (' . $ip . ')';

$accessToken = $this->tokenService->generate(
$context->getTenantId(),
$appId,
null,
$userId,
$name,
$scopes,
Expand Down
5 changes: 4 additions & 1 deletion src/Table/Token.php
Expand Up @@ -37,11 +37,14 @@ class Token extends Generated\TokenTable
const STATUS_ACTIVE = 0x1;
const STATUS_DELETED = 0x2;

public function findOneByTenantAndId(?string $tenantId, int $id): ?TokenRow
public function findOneByTenantAndId(?string $tenantId, int $id, ?int $userId = null): ?TokenRow
{
$condition = Condition::withAnd();
$condition->equals(self::COLUMN_TENANT_ID, $tenantId);
$condition->equals(self::COLUMN_ID, $id);
if ($userId !== null) {
$condition->equals(self::COLUMN_USER_ID, $userId);
}

return $this->findOneBy($condition);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Authorization/AuthorizationCodeTest.php
Expand Up @@ -68,7 +68,7 @@ public function testPost()
$row = $this->connection->fetchAssociative('SELECT app_id, user_id, status, token, refresh, scope, expire, date FROM fusio_token WHERE token = :token', ['token' => $data['access_token']]);

$this->assertEquals(3, $row['app_id']);
$this->assertEquals(4, $row['user_id']);
$this->assertEquals(2, $row['user_id']);
$this->assertEquals(Token::STATUS_ACTIVE, $row['status']);
$this->assertEquals($data['access_token'], $row['token']);
$this->assertEquals($data['refresh_token'], $row['refresh']);
Expand Down
6 changes: 3 additions & 3 deletions tests/Authorization/PasswordTest.php
Expand Up @@ -42,7 +42,7 @@ public function getDataSet(): array

public function testPost()
{
$body = 'grant_type=password&username=Developer&password=qf2vX10Ec3wFZHx0K1eL&scope=authorization,backend';
$body = 'grant_type=password&username=Consumer&password=qf2vX10Ec3wFZHx0K1eL&scope=authorization,backend';
$response = $this->sendRequest('/authorization/token', 'POST', [
'User-Agent' => 'Fusio TestCase',
'Authorization' => 'Basic ' . base64_encode('5347307d-d801-4075-9aaa-a21a29a448c5:342cefac55939b31cd0a26733f9a4f061c0829ed87dae7caff50feaa55aff23d'),
Expand All @@ -54,7 +54,7 @@ public function testPost()

public function testPostEmail()
{
$body = 'grant_type=password&username=developer@localhost.com&password=qf2vX10Ec3wFZHx0K1eL&scope=authorization,backend';
$body = 'grant_type=password&username=consumer@localhost.com&password=qf2vX10Ec3wFZHx0K1eL&scope=authorization,backend';
$response = $this->sendRequest('/authorization/token', 'POST', [
'User-Agent' => 'Fusio TestCase',
'Authorization' => 'Basic ' . base64_encode('5347307d-d801-4075-9aaa-a21a29a448c5:342cefac55939b31cd0a26733f9a4f061c0829ed87dae7caff50feaa55aff23d'),
Expand Down Expand Up @@ -136,7 +136,7 @@ private function assertAccessToken(ResponseInterface $response)
$row = $this->connection->fetchAssociative('SELECT app_id, user_id, status, token, refresh, scope, expire, date FROM fusio_token WHERE token = :token', ['token' => $data['access_token']]);

$this->assertEquals(3, $row['app_id']);
$this->assertEquals(4, $row['user_id']);
$this->assertEquals(2, $row['user_id']);
$this->assertEquals(Token::STATUS_ACTIVE, $row['status']);
$this->assertEquals($data['access_token'], $row['token']);
$this->assertEquals($data['refresh_token'], $row['refresh']);
Expand Down
2 changes: 1 addition & 1 deletion tests/Authorization/RefreshTokenTest.php
Expand Up @@ -85,7 +85,7 @@ public function testPost()

public function testPostExpiredToken()
{
$body = 'grant_type=refresh_token&refresh_token=b8f6f61bd22b440a3e4be2b7491066682bfcde611dbefa1b15d2e7f6522d77e2';
$body = 'grant_type=refresh_token&refresh_token=b8f6f61bd22b440a3e5be2b7491066682bfcde611dbefa1b15d2e7f6522d77e2';
$response = $this->sendRequest('/authorization/token', 'POST', [
'User-Agent' => 'Fusio TestCase',
'Authorization' => 'Basic ' . base64_encode('5347307d-d801-4075-9aaa-a21a29a448c5:342cefac55939b31cd0a26733f9a4f061c0829ed87dae7caff50feaa55aff23d'),
Expand Down
49 changes: 12 additions & 37 deletions tests/Authorization/WhoamiTest.php
Expand Up @@ -42,50 +42,22 @@ public function testGet()
{
$response = $this->sendRequest('/authorization/whoami', 'GET', array(
'User-Agent' => 'Fusio TestCase',
'Authorization' => 'Bearer da250526d583edabca8ac2f99e37ee39aa02a3c076c0edc6929095e20ca18dcf'
'Authorization' => 'Bearer b8f6f61bd22b440a3e4be2b7491066682bfcde611dbefa1b15d2e7f6522d77e2'
));

$body = (string) $response->getBody();
$body = Normalizer::normalize($body);

$expect = <<<'JSON'
{
"id": 1,
"roleId": 1,
"id": 2,
"roleId": 3,
"planId": 1,
"status": 1,
"name": "Administrator",
"email": "admin@localhost.com",
"name": "Consumer",
"email": "consumer@localhost.com",
"points": 100,
"scopes": [
"backend",
"backend.account",
"backend.action",
"backend.app",
"backend.audit",
"backend.category",
"backend.config",
"backend.connection",
"backend.cronjob",
"backend.dashboard",
"backend.event",
"backend.generator",
"backend.identity",
"backend.log",
"backend.marketplace",
"backend.operation",
"backend.page",
"backend.plan",
"backend.rate",
"backend.role",
"backend.schema",
"backend.scope",
"backend.sdk",
"backend.statistic",
"backend.tenant",
"backend.token",
"backend.transaction",
"backend.trash",
"backend.user",
"backend.webhook",
"consumer",
"consumer.account",
"consumer.app",
Expand All @@ -101,9 +73,9 @@ public function testGet()
"consumer.transaction",
"consumer.webhook",
"authorization",
"default",
"foo",
"bar"
"bar",
"plan_scope"
],
"plans": [
{
Expand All @@ -113,6 +85,9 @@ public function testGet()
"points": 1000
}
],
"metadata": {
"foo": "bar"
},
"date": "[datetime]"
}
JSON;
Expand Down
17 changes: 13 additions & 4 deletions tests/Backend/Api/App/EntityTest.php
Expand Up @@ -76,11 +76,21 @@ public function testGet()
"bar"
],
"tokens": [
{
"id": 7,
"status": 1,
"name": "Foo-App\/Expired",
"scope": [
"bar"
],
"ip": "127.0.0.1",
"expire": "[datetime]",
"date": "[datetime]"
},
{
"id": 4,
"userId": 4,
"status": 1,
"token": "e4a4d21e8ca88b215572b4d8635c492d8877fd8d3de6b98ba7c08d282adfb94f",
"name": "Foo-App\/Developer",
"scope": [
"bar"
],
Expand All @@ -90,9 +100,8 @@ public function testGet()
},
{
"id": 3,
"userId": 2,
"status": 1,
"token": "b41344388feed85bc362e518387fdc8c81b896bfe5e794131e1469770571d873",
"name": "Foo-App\/Consumer",
"scope": [
"bar"
],
Expand Down
2 changes: 1 addition & 1 deletion tests/Backend/Api/Dashboard/resource/dashboard_get.json
Expand Up @@ -415,7 +415,7 @@
"entry": [
{
"id": 1,
"user_id": 1,
"user_id": 2,
"plan_id": 2,
"transactionId": "[transaction_id]",
"amount": 39.99,
Expand Down
6 changes: 3 additions & 3 deletions tests/Backend/Api/Scope/CollectionTest.php
Expand Up @@ -213,16 +213,16 @@ public function testPost()
->orderBy('id', 'DESC')
->getSQL();

$scopeId = 49;
$scopeId = $row['id'];
$operations = $this->connection->fetchAllAssociative($sql, ['scope_id' => $scopeId]);

$this->assertEquals([[
'scope_id' => $scopeId,
'operation_id' => 188,
'operation_id' => 193,
'allow' => 1,
], [
'scope_id' => $scopeId,
'operation_id' => 186,
'operation_id' => 191,
'allow' => 1,
]], $operations);
}
Expand Down
15 changes: 6 additions & 9 deletions tests/Backend/Api/Token/CollectionTest.php
Expand Up @@ -53,10 +53,9 @@ public function testGet()
"itemsPerPage": 16,
"entry": [
{
"id": 3,
"appId": 3,
"userId": 2,
"id": 7,
"status": 1,
"name": "Foo-App\/Expired",
"scope": [
"bar"
],
Expand Down Expand Up @@ -87,10 +86,9 @@ public function testGetSearch()
"itemsPerPage": 16,
"entry": [
{
"id": 3,
"appId": 3,
"userId": 2,
"id": 7,
"status": 1,
"name": "Foo-App\/Expired",
"scope": [
"bar"
],
Expand Down Expand Up @@ -121,10 +119,9 @@ public function testGetCount()
"itemsPerPage": 80,
"entry": [
{
"id": 3,
"appId": 3,
"userId": 2,
"id": 7,
"status": 1,
"name": "Foo-App\/Expired",
"scope": [
"bar"
],
Expand Down
13 changes: 1 addition & 12 deletions tests/Backend/Api/Token/EntityTest.php
Expand Up @@ -51,19 +51,8 @@ public function testGet()
$expect = <<<'JSON'
{
"id": 1,
"app": {
"id": 1,
"userId": 1,
"status": 1,
"name": "Backend"
},
"user": {
"id": 1,
"status": 1,
"name": "Administrator"
},
"status": 1,
"token": "da250526d583edabca8ac2f99e37ee39aa02a3c076c0edc6929095e20ca18dcf",
"name": "Backend\/Administrator",
"scope": [
"backend",
"authorization"
Expand Down
2 changes: 1 addition & 1 deletion tests/Backend/Api/Transaction/CollectionTest.php
Expand Up @@ -56,7 +56,7 @@ public function testGet()
"entry": [
{
"id": 1,
"userId": 1,
"userId": 2,
"planId": 2,
"transactionId": "[transaction_id]",
"amount": 39.99,
Expand Down
2 changes: 1 addition & 1 deletion tests/Backend/Api/Transaction/EntityTest.php
Expand Up @@ -51,7 +51,7 @@ public function testGet()
$expect = <<<JSON
{
"id": 1,
"userId": 1,
"userId": 2,
"planId": 2,
"transactionId": "[transaction_id]",
"amount": 39.99,
Expand Down
8 changes: 8 additions & 0 deletions tests/Backend/Api/User/EntityTest.php
Expand Up @@ -89,6 +89,14 @@ public function testGet()
"foo",
"bar"
],
"plans": [
{
"id": 2,
"name": "Plan B",
"price": 49.99,
"points": 1000
}
],
"apps": [
{
"id": 5,
Expand Down

0 comments on commit 3a67173

Please sign in to comment.