Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fogelito committed Apr 18, 2024
1 parent 59f88d7 commit 49c2a11
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 63 deletions.
61 changes: 28 additions & 33 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: '3'

services:
mariadb:
image: mariadb:10.6
image: mariadb:10.11
environment:
- MYSQL_ROOT_PASSWORD=password
networks:
Expand All @@ -18,6 +18,10 @@ services:
- abuse
depends_on:
- mariadb
volumes:
- ./phpunit.xml:/code/phpunit.xml
- ./src:/code/src
- ./tests:/code/tests

networks:
abuse:
55 changes: 26 additions & 29 deletions src/Audit/Audit.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,38 @@

namespace Utopia\Audit;

use Exception;
use Throwable;
use Utopia\Database\Database;
use Utopia\Database\DateTime;
use Utopia\Database\Document;
use Utopia\Database\Exception\Authorization as AuthorizationException;
use Utopia\Database\Exception\Duplicate as DuplicateException;
use Utopia\Database\Exception\Structure as StructureException;
use Utopia\Database\Query;
use Utopia\Database\Validator\Authorization;
use Utopia\Exception;

class Audit
{
public const COLLECTION = 'audit';

private Database $db;
private Authorization $auth;

public function __construct(Database $db, Authorization $auth)
public function __construct(Database $db)
{
$this->db = $db;
$this->auth = $auth;
}

/**
* Setup database structure.
*
* @return void
*
* @throws DuplicateException
* @throws \Exception
* @throws Exception
*/
public function setup(): void
{
if (! $this->db->exists($this->db->getDatabase())) {
throw new \Exception('You need to create the database before running Audit setup');
if (!$this->db->exists($this->db->getDatabase())) {
throw new Exception('You need to create the database before running Audit setup');
}

$attributes = [
Expand Down Expand Up @@ -167,12 +164,12 @@ public function setup(): void
*
* @throws AuthorizationException
* @throws StructureException
* @throws \Exception
* @throws \Throwable
* @throws Exception
* @throws Throwable
*/
public function log(string $userId, string $event, string $resource, string $userAgent, string $ip, string $location, array $data = []): bool
{
$this->auth->skip(function () use ($userId, $event, $resource, $userAgent, $ip, $location, $data) {
$this->db->getAuthorization()->skip(function () use ($userId, $event, $resource, $userAgent, $ip, $location, $data) {
$this->db->createDocument(Audit::COLLECTION, new Document([
'$permissions' => [],
'userId' => $userId,
Expand All @@ -198,12 +195,12 @@ public function log(string $userId, string $event, string $resource, string $use
* @param Document|null $orderAfter
* @return array<Document>
*
* @throws \Exception
* @throws Exception
*/
public function getLogsByUser(string $userId, ?int $limit = null, ?int $offset = null, ?Document $orderAfter = null): array
{
/** @var array<Document> $result */
$result = $this->auth->skip(function () use ($userId, $limit, $offset, $orderAfter) {
$result = $this->db->getAuthorization()->skip(function () use ($userId, $limit, $offset, $orderAfter) {
$queries[] = Query::equal('userId', [$userId]);
$queries[] = Query::orderDesc('');

Expand Down Expand Up @@ -235,7 +232,7 @@ public function getLogsByUser(string $userId, ?int $limit = null, ?int $offset =
public function countLogsByUser(string $userId): int
{
/** @var int $count */
$count = $this->auth->skip(function () use ($userId) {
$count = $this->db->getAuthorization()->skip(function () use ($userId) {
return $this->db->count(
collection: Audit::COLLECTION,
queries: [Query::equal('userId', [$userId])]
Expand All @@ -254,12 +251,12 @@ public function countLogsByUser(string $userId): int
* @param Document|null $orderAfter
* @return array<Document>
*
* @throws \Exception
* @throws Exception
*/
public function getLogsByResource(string $resource, ?int $limit = 25, ?int $offset = null, ?Document $orderAfter = null): array
{
/** @var array<Document> $result */
$result = $this->auth->skip(function () use ($resource, $limit, $offset, $orderAfter) {
$result = $this->db->getAuthorization()->skip(function () use ($resource, $limit, $offset, $orderAfter) {
$queries[] = Query::equal('resource', [$resource]);
$queries[] = Query::orderDesc('');

Expand Down Expand Up @@ -288,12 +285,12 @@ public function getLogsByResource(string $resource, ?int $limit = 25, ?int $offs
* @param string $resource
* @return int
*
* @throws \Exception
* @throws Exception
*/
public function countLogsByResource(string $resource): int
{
/** @var int $count */
$count = $this->auth->skip(function () use ($resource) {
$count = $this->db->getAuthorization()->skip(function () use ($resource) {
return $this->db->count(
collection: Audit::COLLECTION,
queries: [Query::equal('resource', [$resource])]
Expand All @@ -313,12 +310,12 @@ public function countLogsByResource(string $resource): int
* @param Document|null $orderAfter
* @return array<Document>
*
* @throws \Exception
* @throws Exception
*/
public function getLogsByUserAndEvents(string $userId, array $events, ?int $limit = null, ?int $offset = null, ?Document $orderAfter = null): array
{
/** @var array<Document> $result */
$result = $this->auth->skip(function () use ($userId, $events, $limit, $offset, $orderAfter) {
$result = $this->db->getAuthorization()->skip(function () use ($userId, $events, $limit, $offset, $orderAfter) {
$queries[] = Query::equal('userId', [$userId]);
$queries[] = Query::equal('event', $events);
$queries[] = Query::orderDesc('');
Expand Down Expand Up @@ -349,12 +346,12 @@ public function getLogsByUserAndEvents(string $userId, array $events, ?int $limi
* @param array<int,string> $events
* @return int
*
* @throws \Exception
* @throws Exception
*/
public function countLogsByUserAndEvents(string $userId, array $events): int
{
/** @var int $count */
$count = $this->auth->skip(function () use ($userId, $events) {
$count = $this->db->getAuthorization()->skip(function () use ($userId, $events) {
return $this->db->count(
collection: Audit::COLLECTION,
queries: [
Expand All @@ -377,12 +374,12 @@ public function countLogsByUserAndEvents(string $userId, array $events): int
* @param Document|null $orderAfter
* @return array<Document>
*
* @throws \Exception
* @throws Exception
*/
public function getLogsByResourceAndEvents(string $resource, array $events, ?int $limit = null, ?int $offset = null, ?Document $orderAfter = null): array
{
/** @var array<Document> $result */
$result = $this->auth->skip(function () use ($resource, $events, $limit, $offset, $orderAfter) {
$result = $this->db->getAuthorization()->skip(function () use ($resource, $events, $limit, $offset, $orderAfter) {
$queries[] = Query::equal('resource', [$resource]);
$queries[] = Query::equal('event', $events);
$queries[] = Query::orderDesc('');
Expand Down Expand Up @@ -413,12 +410,12 @@ public function getLogsByResourceAndEvents(string $resource, array $events, ?int
* @param array<int,string> $events
* @return int
*
* @throws \Exception
* @throws Exception
*/
public function countLogsByResourceAndEvents(string $resource, array $events): int
{
/** @var int $count */
$count = $this->auth->skip(function () use ($resource, $events) {
$count = $this->db->getAuthorization()->skip(function () use ($resource, $events) {
return $this->db->count(
collection: Audit::COLLECTION,
queries: [
Expand All @@ -438,11 +435,11 @@ public function countLogsByResourceAndEvents(string $resource, array $events): i
* @return bool
*
* @throws AuthorizationException
* @throws \Exception
* @throws Throwable
*/
public function cleanup(string $datetime): bool
{
$this->auth->skip(function () use ($datetime) {
$this->db->getAuthorization()->skip(function () use ($datetime) {
do {
$documents = $this->db->find(
collection: Audit::COLLECTION,
Expand Down

0 comments on commit 49c2a11

Please sign in to comment.