Skip to content

Commit

Permalink
Get the integration tests up and running again
Browse files Browse the repository at this point in the history
- The messenger transactional wrap is not working with the tests, as it conflicts with the DoctrineTestBundle
- Fix some type problems (DateTime vs DateTimeImmutable, nulls and initialization errors)
- `SearchManager::findActivityPubActorsByUsername` now converts `!user@domain` into `@user@domain` so searching for a magazine by `!mag@domain` works from the API as well
- The `StatsContentRepository` had a sign error
- add a new docker compose for the test dependencies, as well as oauth keys for the tests
- remove the purge user test, as this is no longer an option
- change the .env.test to fit the new dependencies
  • Loading branch information
BentiGorlich committed Dec 17, 2024
1 parent 656f721 commit 82db8d1
Show file tree
Hide file tree
Showing 244 changed files with 4,189 additions and 4,910 deletions.
10 changes: 9 additions & 1 deletion .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@ PANTHER_APP_ENV=panther
PANTHER_ERROR_SCREENSHOT_DIR=./var/error-screenshots

# Mbin variables
DATABASE_URL="postgresql://kbin:ChangeMe@127.0.0.1:5432/kbin_test?serverVersion=13&charset=utf8"
DATABASE_URL="postgresql://mbin:ChangeThisPostgresPass@127.0.0.1:5433/mbin?serverVersion=16&charset=utf8"
REDIS_DNS=redis://123456789@127.0.0.1:6380
MAILER_DSN=null://default
KBIN_JS_ENABLED=false
KBIN_DEFAULT_LANG=en
KBIN_DOMAIN=kbin.test
ELASTICSEARCH_ENABLED=false
KBIN_API_ITEMS_PER_PAGE=2
KBIN_FEDERATION_ENABLED=false

###> league/oauth2-server-bundle ###
OAUTH_PRIVATE_KEY=%kernel.project_dir%/config/oauth2/tests/private.pem
OAUTH_PUBLIC_KEY=%kernel.project_dir%/config/oauth2/tests/public.pem
OAUTH_ENCRYPTION_KEY=MF3Sks6dIHhMvk1SALv68e8LydunZopLennBuF9jALk=
OAUTH_PASSPHRASE=null
###< league/oauth2-server-bundle ###
54 changes: 54 additions & 0 deletions .github/workflows/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,60 @@ jobs:
SYMFONY_DEPRECATIONS_HELPER: disabled
run: php bin/phpunit tests/Unit

integration-test:
runs-on: ubuntu-latest
container:
image: danger89/mbin-pipeline:1.2.0
steps:
- uses: actions/checkout@v4

- name: Get Composer Cache Directory
id: composer-cache
run: |
echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
- name: Composer install
run: composer install --no-scripts --no-progress

- uses: actions/cache@v4
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-composer-

- name: Run integration tests
env:
COMPOSER_CACHE_DIR: ${{ steps.composer-cache.outputs.dir }}
SYMFONY_DEPRECATIONS_HELPER: disabled
run: php bin/phpunit tests/Functional
services:
postgres:
# Docker Hub image
image: postgres
# Provide the password for postgres
env:
POSTGRES_DB: mbin_test
POSTGRES_USER: mbin
POSTGRES_PASSWORD: ChangeThisPostgresPass
# Set health checks to wait until postgres has started
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
redis:
# Docker Hub image
image: redis
# Set health checks to wait until redis has started
options: >-
--health-cmd "redis-cli ping"
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
# Maps port 6379 on service container to the host
- 6380:6379

audit-check:
runs-on: ubuntu-latest
container:
Expand Down
28 changes: 28 additions & 0 deletions config/oauth2/tests/private.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-----BEGIN PRIVATE KEY-----
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDwTLg0F+MPOubO
8QXiNBAzWVFdP8LKemjKjUWembvcSIkeKhyIvk5YSbsX56vc7ArJnX8ikdnhVw5u
wK7K9vwU8+FJBFStHypexOQGJchSpV76kWYK4J6zFVy11bCbsHPrlwM0chM7pBgU
JSJ2c5KPaGhKUwZ/DzsP+qzLh1Us/LKtuLc5uPPEN1824Up7DFr6RiM0dWKjyvEB
bNJmDAFr/F4j6K7zZLv52U49kdWetHNMLXQxWB8WM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q/F3MKm0VYp8rSsN4YISViNmAx5KjoBgfbgPFfLz+95r24Hvd/
8mAVL2ssBXFPQWZDC7FJVPvaFR15tv3t4BqymC7tAoGBAKR9e/VLI2cajpBdXzK4
JhaQ59R/ybxPdg5JhyGC3n9WN5lK71pdbR04vNVAT3+LwpNu49DKEYkiSCfo3tAl
iJPnKe8mGgQ4f1W7bvbW9GxMYWp/SGQRE3MZbH1ChUlCiqlzhkmwq1LgOFURCO+3
GyQt5+0mF/tTqsFwt+n1VOtI
-----END PRIVATE KEY-----
9 changes: 9 additions & 0 deletions config/oauth2/tests/public.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA8Ey4NBfjDzrmzvEF4jQQ
M1lRXT/Cynpoyo1Fnpm73EiJHiociL5OWEm7F+er3OwKyZ1/IpHZ4VcObsCuyvb8
FPPhSQRUrR8qXsTkBiXIUqVe+pFmCuCesxVctdWwm7Bz65cDNHITO6QYFCUidnOS
j2hoSlMGfw87D/qsy4dVLPyyrbi3ObjzxDdfNuFKewxa+kYjNHVio8rxAWzSZgwB
a/xeI+iu82S7+dlOPZHVnrRzTC10MVgfFjFOoSR0RqoM/KySPoHKshywwnipTCRK
55wPZAwt6Sk4w7oFBUlUt24Iu7b9avvyomB1H9Uk1eaGO+CQuCX0T2JqAfmZin/Y
CwIDAQAB
-----END PUBLIC KEY-----
20 changes: 20 additions & 0 deletions docker/tests/compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
services:
db:
image: postgres:${POSTGRES_VERSION:-16}-alpine
container_name: mbin-tests-db
restart: unless-stopped
ports:
- "5433:5432"
environment:
- POSTGRES_DB=mbin_test
- POSTGRES_USER=mbin
- POSTGRES_PASSWORD=ChangeThisPostgresPass
redis:
image: redis:alpine
container_name: mbin-tests-redis
restart: unless-stopped
command: /bin/sh -c "redis-server --requirepass 123456789"
ports:
- "6380:6379"
healthcheck:
test: ["CMD", "redis-cli", "ping"]
22 changes: 19 additions & 3 deletions docs/03-contributing/01-getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,15 +192,17 @@ _Optionally:_ If you want to start federating, you will also need to messenger j

More info: [Contributing guide](https://github.com/MbinOrg/mbin/blob/main/CONTRIBUTING.md), [Admin guide](../02-admin/README.md) and [Symfony Local Web Server](https://symfony.com/doc/current/setup/symfony_server.html)

## Unit tests
## Testing

When fixing a bug or implementing a new feature or improvement, we expect that test code will also be included with every delivery of production code. There are three levels of tests that we distinguish between:

- Unit Tests: test a specific unit (SUT), mock external functions/classes/database calls, etc. Unit-tests are fast, isolated and repeatable
- Integration Tests: test larger part of the code, combining multiple units together (classes, services or alike).
- Application Tests: test high-level functionality, APIs or web calls.

### Prepare for unit tests
For more info read: [Symfony Testing guide](https://symfony.com/doc/current/testing.html).

### Prepare testing

1. First increase execution time in your PHP config file: `/etc/php/8.3/fpm/php.ini`:

Expand All @@ -226,7 +228,21 @@ Running the unit tests can be done by executing:
SYMFONY_DEPRECATIONS_HELPER=disabled ./bin/phpunit tests/Unit
```

For more info read: [Symfony Testing guide](https://symfony.com/doc/current/testing.html).
### Running integration tests

Our integration tests depend on a database and a caching server (Valkey / KeyDB / Redis).
The database and cache are cleared / dumped every test run.
To start the services:

```sh
cd docker/tests && docker compose up -d
```

To run the integration tests:

```sh
SYMFONY_DEPRECATIONS_HELPER=disabled ./bin/phpunit tests/Functional
```

## Linting

Expand Down
3 changes: 2 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
<server name="APP_ENV" value="test" force="true" />
<server name="SHELL_VERBOSITY" value="-1" />
<server name="SYMFONY_PHPUNIT_REMOVE" value="" />
<server name="SYMFONY_PHPUNIT_VERSION" value="9.6" />
<server name="SYMFONY_PHPUNIT_VERSION" value="11.4.3" />
<server name="BOOTSTRAP_DB" value="1" />
</php>

<testsuites>
Expand Down
4 changes: 2 additions & 2 deletions src/Controller/Api/Instance/InstanceRetrieveStatsApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,14 +229,14 @@ public function content(
if (null === $startString) {
$start = null;
} else {
$start = new \DateTime($startString);
$start = new \DateTimeImmutable($startString);
}

$endString = $request->get('end');
if (null === $endString) {
$end = null;
} else {
$end = new \DateTime($endString);
$end = new \DateTimeImmutable($endString);
}
} catch (\Exception $e) {
throw new BadRequestHttpException('Failed to parse start or end time');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,14 +284,14 @@ public function content(
if (null === $startString) {
$start = null;
} else {
$start = new \DateTime($startString);
$start = new \DateTimeImmutable($startString);
}

$endString = $request->get('end');
if (null === $endString) {
$end = null;
} else {
$end = new \DateTime($endString);
$end = new \DateTimeImmutable($endString);
}
} catch (\Exception $e) {
throw new BadRequestHttpException('Failed to parse start or end time');
Expand Down
1 change: 1 addition & 0 deletions src/DTO/EntryRequestDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public function mergeIntoDto(EntryDto $dto): EntryDto
$dto->isOc = $this->isOc ?? $dto->isOc;
$dto->lang = $this->lang ?? $dto->lang ?? SettingsManager::getValue('KBIN_DEFAULT_LANG');
$dto->url = $this->url ?? $dto->url;
$dto->tags = $this->tags ?? $dto->tags;

return $dto;
}
Expand Down
1 change: 1 addition & 0 deletions src/Entity/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ public function __construct(
$this->favourites = new ArrayCollection();
$this->notifications = new ArrayCollection();
$this->badges = new ArrayCollection();
$this->hashtags = new ArrayCollection();

$user->addEntry($this);

Expand Down
2 changes: 2 additions & 0 deletions src/Entity/Magazine.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ public function __construct(
$this->reports = new ArrayCollection();
$this->badges = new ArrayCollection();
$this->logs = new ArrayCollection();
$this->moderatorRequests = new ArrayCollection();
$this->ownershipRequests = new ArrayCollection();

if (null !== $user) {
$this->addModerator(new Moderator($this, $user, null, true, true));
Expand Down
4 changes: 3 additions & 1 deletion src/MessageHandler/ActivityPub/Inbox/ActivityHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use App\Service\SettingsManager;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
use Symfony\Component\Messenger\MessageBusInterface;
Expand All @@ -39,6 +40,7 @@ class ActivityHandler extends MbinMessageHandler
{
public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly KernelInterface $kernel,
private readonly SignatureValidator $signatureValidator,
private readonly SettingsManager $settingsManager,
private readonly MessageBusInterface $bus,
Expand All @@ -48,7 +50,7 @@ public function __construct(
private readonly RemoteInstanceManager $remoteInstanceManager,
private readonly LoggerInterface $logger
) {
parent::__construct($this->entityManager);
parent::__construct($this->entityManager, $this->kernel);
}

public function __invoke(ActivityMessage $message): void
Expand Down
4 changes: 3 additions & 1 deletion src/MessageHandler/ActivityPub/Inbox/AddHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use App\Service\SettingsManager;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Component\Messenger\MessageBusInterface;

Expand All @@ -30,6 +31,7 @@ class AddHandler extends MbinMessageHandler
{
public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly KernelInterface $kernel,
private readonly ActivityPubManager $activityPubManager,
private readonly ApHttpClient $apHttpClient,
private readonly ApActivityRepository $apActivityRepository,
Expand All @@ -41,7 +43,7 @@ public function __construct(
private readonly EntryManager $entryManager,
private readonly SettingsManager $settingsManager,
) {
parent::__construct($this->entityManager);
parent::__construct($this->entityManager, $this->kernel);
}

public function __invoke(AddMessage $message): void
Expand Down
4 changes: 3 additions & 1 deletion src/MessageHandler/ActivityPub/Inbox/AnnounceHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use App\Service\VoteManager;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Component\Messenger\MessageBusInterface;

Expand All @@ -24,11 +25,12 @@ public function __construct(
private readonly ActivityPubManager $activityPubManager,
private readonly EntityManagerInterface $entityManager,
private readonly MessageBusInterface $bus,
private readonly KernelInterface $kernel,
private readonly VoteManager $manager,
private readonly VoteHandleSubscriber $voteHandleSubscriber,
private readonly LoggerInterface $logger,
) {
parent::__construct($this->entityManager);
parent::__construct($this->entityManager, $this->kernel);
}

public function __invoke(AnnounceMessage $message): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use App\Service\SettingsManager;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Component\Messenger\MessageBusInterface;

Expand All @@ -34,6 +35,7 @@ class ChainActivityHandler extends MbinMessageHandler
{
public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly KernelInterface $kernel,
private readonly LoggerInterface $logger,
private readonly ApHttpClient $client,
private readonly MessageBusInterface $bus,
Expand All @@ -42,7 +44,7 @@ public function __construct(
private readonly Page $page,
private readonly SettingsManager $settingsManager,
) {
parent::__construct($this->entityManager);
parent::__construct($this->entityManager, $this->kernel);
}

public function __invoke(ChainActivityMessage $message): void
Expand Down
4 changes: 3 additions & 1 deletion src/MessageHandler/ActivityPub/Inbox/CreateHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use App\Service\MessageManager;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Component\Messenger\MessageBusInterface;

Expand All @@ -34,6 +35,7 @@ class CreateHandler extends MbinMessageHandler
{
public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly KernelInterface $kernel,
private readonly Note $note,
private readonly Page $page,
private readonly MessageBusInterface $bus,
Expand All @@ -42,7 +44,7 @@ public function __construct(
private readonly ActivityPubManager $activityPubManager,
private readonly ApActivityRepository $repository
) {
parent::__construct($this->entityManager);
parent::__construct($this->entityManager, $this->kernel);
}

/**
Expand Down
Loading

0 comments on commit 82db8d1

Please sign in to comment.