Skip to content

Commit

Permalink
Base test cover. Part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
vtsykun committed Mar 5, 2023
1 parent b349bdd commit 40c2f9c
Show file tree
Hide file tree
Showing 15 changed files with 508 additions and 23 deletions.
4 changes: 4 additions & 0 deletions .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ APP_SECRET='$ecretf0rt3st'
SYMFONY_DEPRECATIONS_HELPER=999999
PANTHER_APP_ENV=panther
PANTHER_ERROR_SCREENSHOT_DIR=./var/error-screenshots

DATABASE_URL="sqlite:///%kernel.project_dir%/var/test.db"
MAILER_DSN=null://null
REDIS_URL=redis://localhost/14
43 changes: 43 additions & 0 deletions .github/workflows/run_tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: "Run Tests"

on:
push:
paths:
- 'src/**'
- 'tests/**'
branches:
- master
pull_request:

env:
APP_ENV: test

jobs:
tests:
name: "Tests"
runs-on: ubuntu-20.04

steps:
- name: "Checkout"
uses: "actions/checkout@v3"

- name: "Install PHP"
uses: "shivammathur/setup-php@v2"
with:
coverage: "none"
extensions: "intl, zip, redis"
php-version: "8.1"
tools: composer

- name: "Install dependencies"
uses: "ramsey/composer-install@v2"
with:
composer-options: "--ansi --no-interaction"

- name: Start Redis
uses: supercharge/[email protected]
with:
redis-version: 6

- name: "Run tests"
run: "composer tests"
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@
],
"post-update-cmd": [
"@auto-scripts"
]
],
"tests": "tests/phpunit"
},
"conflict": {
"symfony/symfony": "*"
Expand Down
5 changes: 3 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
<ini name="error_reporting" value="-1" />
<server name="APP_ENV" value="test" force="true" />
<server name="SHELL_VERBOSITY" value="-1" />
<env name="SYMFONY_DEPRECATIONS_HELPER" value="disabled" />
</php>

<testsuites>
<testsuite name="Project Test Suite">
<directory>tests</directory>
<testsuite name="functional">
<directory>tests/Functional</directory>
</testsuite>
</testsuites>

Expand Down
7 changes: 5 additions & 2 deletions src/Command/UserManagerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int

if ($input->hasOption('password')) {
$io->writeln("Creating a user {$username}...");
$password = $io->askHidden('Enter password');
$input->setOption('password', $password);

if (empty($input->getOption('password'))) {
$password = $io->askHidden('Enter password');
$input->setOption('password', $password);
}
}

$password = $input->getOption('password') ?: hash('sha512', random_bytes(50));
Expand Down
2 changes: 1 addition & 1 deletion src/Controller/WebController.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public function statsAction(\Redis $redis): Response
*/
protected function getFilteredOrderedBys(Request $req)
{
$orderBys = $req->query->get('orderBys', []);
$orderBys = $req->query->get('orderBys') ?: [];
if (!$orderBys) {
$orderBys = $req->query->get('search_query');
$orderBys = $orderBys['orderBys'] ?? [];
Expand Down
2 changes: 1 addition & 1 deletion src/Security/Api/ApiUsernamePasswordToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function __construct($user, $providerKey, array $roles = [])
$this->setUser($user);
$this->providerKey = $providerKey;

parent::setAuthenticated(count($roles) > 0);
parent::setAuthenticated(count($roles) > 0, false);
}

/**
Expand Down
16 changes: 0 additions & 16 deletions tests/Controller/ProviderControllerTest.php

This file was deleted.

93 changes: 93 additions & 0 deletions tests/Functional/Controller/BaseAclControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

declare(strict_types=1);

namespace Packeton\Tests\Functional\Controller;

use Packeton\Tests\Phpunit\PacketonTestTrait;
use PHPUnit\Framework\Attributes\DataProvider;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class BaseAclControllerTest extends WebTestCase
{
use PacketonTestTrait;

#[DataProvider('aclUserUrlProvider')]
public function testACLAccess(string $user, string $url, int $code): void
{
$client = static::createClient();

$user = $this->getUser($user);
$client->loginUser($user);

$client->request('GET', $url);
static::assertResponseStatusCodeSame($code);
}

#[DataProvider('adminUrlProvider')]
public function testAdminAccess(string $url): void
{
$client = static::createClient();

$admin = $this->getUser('admin');

$client->loginUser($admin);

$client->request('GET', $url);
static::assertResponseIsSuccessful();
}

public function testAnonymousAccess(): void
{
$client = static::createClient();
$client->request('GET', '/');
static::assertResponseRedirects();
}

public static function aclUserUrlProvider(): array
{
return [
['dev', '/', 200],
['dev', '/', 200],
['dev', '/packages/okvpn/cron-bundle', 200],
['dev', '/packages/okvpn/satis-api', 200],
['dev', '/packages/okvpn/cron-bundle/stats', 200],
['dev', '/packages/submit', 200],
['dev', '/groups', 403],

['user1', '/', 200],
['user1', '/profile', 200],
['user1', '/packages/okvpn/cron-bundle', 200],
['user1', '/packages/okvpn/satis-api', 404],
['user1', '/about', 403],

['user2', '/packages/okvpn/satis-api', 200],
];
}

public static function adminUrlProvider(): array
{
return [
['/'],
['/packages/okvpn/cron-bundle'],
['/packages/okvpn/cron-bundle/stats'],
['/packages/submit'],
['/users/'],
['/users/dev'],
['/users/dev/update'],
['/groups'],
['/groups/1/update'],
['/users/sshkey'],
['/profile'],
['/statistics'],
['/explore'],
['/about'],
['/users/admin/packages'],
['/users/admin/favorites'],
['/apidoc'],
['/proxies'],
['/feeds/'],
['/feeds/releases.rss'],
];
}
}
111 changes: 111 additions & 0 deletions tests/Functional/Controller/ProviderControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

namespace Packeton\Tests\Functional\Controller;

use Packeton\Tests\Phpunit\PacketonTestTrait;
use PHPUnit\Framework\Attributes\DataProvider;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class ProviderControllerTest extends WebTestCase
{
use PacketonTestTrait;

public function testRootPackages(): void
{
$client = static::createClient();

$client->request('GET', '/packages.json');
static::assertResponseStatusCodeSame(401);

$this->basicLogin('admin', $client);
$client->request('GET', '/packages.json');
static::assertResponseStatusCodeSame(200);;
}

#[DataProvider('aclUserProvider')]
public function testAvailablePackages(string $user, int $count): void
{
$client = static::createClient();

$this->basicLogin($user, $client);
$client->request('GET', '/packages.json');

$content = $this->getJsonResponse($client);
static::assertCount($count, $content['available-packages']);
}

public function testV2Packages(): void
{
$client = static::createClient();

$this->basicLogin('admin', $client);

$client->request('GET', '/p2/okvpn/cron-bundle.json');
$content = $this->getJsonResponse($client);
static::assertNotEmpty($content['packages']['okvpn/cron-bundle']);

$client->request('GET', '/p2/okvpn/cron-bundle~dev.json');
$content = $this->getJsonResponse($client);
static::assertNotEmpty($content['packages']['okvpn/cron-bundle']);

$client->request('GET', '/p2/okvpn/cron-bundle222.json');
static::assertResponseStatusCodeSame(404);
}

public function testV2CustomerPackages(): void
{
$client = static::createClient();

$this->basicLogin('user1', $client);

$client->request('GET', '/p2/okvpn/cron-bundle.json');
$content = $this->getJsonResponse($client);
static::assertNotEmpty($content['packages']['okvpn/cron-bundle']);

$client->request('GET', '/p2/okvpn/cron-bundle~dev.json');
$content = $this->getJsonResponse($client);
static::assertNotEmpty($content['packages']['okvpn/cron-bundle']);

$client->request('GET', '/p2/okvpn/satis-api.json');
static::assertResponseStatusCodeSame(404);
}

public function testProviderIncludesV1(): void
{
$client = static::createClient();

$this->basicLogin('admin', $client);

$server = ['HTTP_USER_AGENT' => 'Composer/1.10.2 (Windows NT; 10.0; PHP 8.1.0)'];
$client->request('GET', '/packages.json', server: $server);
$content = $this->getJsonResponse($client);
static::assertNotEmpty($content['provider-includes']);

$hash = reset($content['provider-includes'])['sha256'];
$client->request('GET', "/p/providers$$hash.json", server: $server);
static::assertEquals($hash, hash('sha256', $client->getResponse()->getContent()));

$content = $this->getJsonResponse($client);
static::assertNotEmpty($content['providers']);

$hash = $content['providers']['okvpn/cron-bundle']['sha256'];
$client->request('GET', "/p/okvpn/cron-bundle$$hash.json", server: $server);
static::assertEquals($hash, hash('sha256', $client->getResponse()->getContent()));

$content = $this->getJsonResponse($client);
static::assertNotEmpty($content['packages']);
static::assertNotEmpty($content['packages']['okvpn/cron-bundle']);
$versions = array_column($content['packages']['okvpn/cron-bundle'], 'version_normalized');
static::assertTrue(in_array('9999999-dev', $versions));
}

public static function aclUserProvider(): array
{
return [
['admin', 3],
['dev', 3],
['user1', 1],
['user2', 3],
];
}
}
Loading

0 comments on commit 40c2f9c

Please sign in to comment.