refactor: replace custom CRM with Monica fork
Some checks failed
Build & Push Monica Image to Gitea Registry / build-and-push (push) Failing after 9s
Some checks failed
Build & Push Monica Image to Gitea Registry / build-and-push (push) Failing after 9s
This commit is contained in:
104
tests/Unit/Services/Instance/AuditLog/LogAccountActionTest.php
Normal file
104
tests/Unit/Services/Instance/AuditLog/LogAccountActionTest.php
Normal file
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Instance\AuditLog;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Tests\TestCase;
|
||||
use App\Models\User\User;
|
||||
use App\Models\Contact\Contact;
|
||||
use App\Models\Instance\AuditLog;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use App\Services\Instance\AuditLog\LogAccountAction;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
|
||||
class LogAccountActionTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @test */
|
||||
public function it_logs_an_action(): void
|
||||
{
|
||||
$michael = factory(User::class)->create([]);
|
||||
|
||||
$date = Carbon::now();
|
||||
|
||||
$request = [
|
||||
'account_id' => $michael->account_id,
|
||||
'action' => 'account_created',
|
||||
'author_id' => $michael->id,
|
||||
'author_name' => $michael->name,
|
||||
'audited_at' => $date,
|
||||
'objects' => '{"user": 1}',
|
||||
];
|
||||
|
||||
$auditLog = (new LogAccountAction)->execute($request);
|
||||
|
||||
$this->assertDatabaseHas('audit_logs', [
|
||||
'id' => $auditLog->id,
|
||||
'account_id' => $michael->account_id,
|
||||
'about_contact_id' => null,
|
||||
'action' => 'account_created',
|
||||
'author_id' => $michael->id,
|
||||
'author_name' => $michael->name,
|
||||
'audited_at' => $date,
|
||||
'should_appear_on_dashboard' => false,
|
||||
'objects' => '{"user": 1}',
|
||||
]);
|
||||
|
||||
$this->assertInstanceOf(
|
||||
AuditLog::class,
|
||||
$auditLog
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_logs_an_action_about_a_contact(): void
|
||||
{
|
||||
$michael = factory(User::class)->create([]);
|
||||
$contact = factory(Contact::class)->create([
|
||||
'account_id' => $michael->account_id,
|
||||
]);
|
||||
|
||||
$date = Carbon::now();
|
||||
|
||||
$request = [
|
||||
'account_id' => $michael->account_id,
|
||||
'action' => 'account_created',
|
||||
'about_contact_id' => $contact->id,
|
||||
'author_id' => $michael->id,
|
||||
'author_name' => $michael->name,
|
||||
'audited_at' => $date,
|
||||
'objects' => '{"user": 1}',
|
||||
];
|
||||
|
||||
$auditLog = (new LogAccountAction)->execute($request);
|
||||
|
||||
$this->assertDatabaseHas('audit_logs', [
|
||||
'id' => $auditLog->id,
|
||||
'account_id' => $michael->account_id,
|
||||
'about_contact_id' => $contact->id,
|
||||
'action' => 'account_created',
|
||||
'author_id' => $michael->id,
|
||||
'author_name' => $michael->name,
|
||||
'audited_at' => $date,
|
||||
'should_appear_on_dashboard' => false,
|
||||
'objects' => '{"user": 1}',
|
||||
]);
|
||||
|
||||
$this->assertInstanceOf(
|
||||
AuditLog::class,
|
||||
$auditLog
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_fails_if_wrong_parameters_are_given(): void
|
||||
{
|
||||
$request = [
|
||||
'action' => 'account_created',
|
||||
];
|
||||
|
||||
$this->expectException(ValidationException::class);
|
||||
(new LogAccountAction)->execute($request);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Instance\Geolocalization;
|
||||
|
||||
use Tests\TestCase;
|
||||
use App\Models\Account\Place;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use App\Exceptions\RateLimitedSecondException;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use App\Exceptions\MissingEnvVariableException;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use App\Services\Instance\Geolocalization\GetGPSCoordinate;
|
||||
|
||||
class GetGPSCoordinateTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @test */
|
||||
public function it_returns_null_if_geolocation_is_disabled()
|
||||
{
|
||||
config(['monica.enable_geolocation' => false]);
|
||||
|
||||
$place = factory(Place::class)->create();
|
||||
|
||||
$request = [
|
||||
'account_id' => $place->account_id,
|
||||
'place_id' => $place->id,
|
||||
];
|
||||
|
||||
$this->expectException(MissingEnvVariableException::class);
|
||||
app(GetGPSCoordinate::class)->execute($request);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_gets_gps_coordinates()
|
||||
{
|
||||
config(['monica.enable_geolocation' => true]);
|
||||
config(['monica.location_iq_api_key' => 'test']);
|
||||
|
||||
$body = file_get_contents(base_path('tests/Fixtures/Services/Instance/Geolocalization/GetGPSCoordinateSampleResponse.json'));
|
||||
Http::fake([
|
||||
'us1.locationiq.com/v1/*' => Http::response($body, 200),
|
||||
]);
|
||||
|
||||
$place = factory(Place::class)->create();
|
||||
|
||||
$request = [
|
||||
'account_id' => $place->account_id,
|
||||
'place_id' => $place->id,
|
||||
];
|
||||
|
||||
$place = app(GetGPSCoordinate::class)->execute($request);
|
||||
|
||||
$this->assertDatabaseHas('places', [
|
||||
'id' => $place->id,
|
||||
]);
|
||||
|
||||
$this->assertInstanceOf(
|
||||
Place::class,
|
||||
$place
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_returns_null_if_address_is_garbage()
|
||||
{
|
||||
config(['monica.enable_geolocation' => true]);
|
||||
config(['monica.location_iq_api_key' => 'test']);
|
||||
|
||||
$body = file_get_contents(base_path('tests/Fixtures/Services/Instance/Geolocalization/GetGPSCoordinateGarbageResponse.json'));
|
||||
Http::fake([
|
||||
'us1.locationiq.com/v1/*' => Http::response($body, 404),
|
||||
]);
|
||||
|
||||
$place = factory(Place::class)->create([
|
||||
'country' => 'ewqr',
|
||||
'street' => '',
|
||||
'city' => 'sieklopekznqqq',
|
||||
'postal_code' => '',
|
||||
]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $place->account_id,
|
||||
'place_id' => $place->id,
|
||||
];
|
||||
|
||||
$place = app(GetGPSCoordinate::class)->execute($request);
|
||||
|
||||
$this->assertNull($place);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_fails_if_wrong_parameters_are_given()
|
||||
{
|
||||
config(['monica.enable_geolocation' => true]);
|
||||
config(['monica.location_iq_api_key' => 'test']);
|
||||
|
||||
$request = [
|
||||
'account_id' => 111,
|
||||
];
|
||||
|
||||
$this->expectException(ValidationException::class);
|
||||
|
||||
app(GetGPSCoordinate::class)->execute($request);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_release_the_job_if_rate_limited_second()
|
||||
{
|
||||
config(['monica.enable_geolocation' => true]);
|
||||
config(['monica.location_iq_api_key' => 'test']);
|
||||
|
||||
Http::fake([
|
||||
'us1.locationiq.com/v1/*' => Http::response('{"error":"Rate Limited Second"}', 429),
|
||||
]);
|
||||
|
||||
$place = factory(Place::class)->create([
|
||||
'country' => 'ewqr',
|
||||
'street' => '',
|
||||
'city' => 'sieklopekznqqq',
|
||||
'postal_code' => '',
|
||||
]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $place->account_id,
|
||||
'place_id' => $place->id,
|
||||
];
|
||||
|
||||
$this->expectException(RateLimitedSecondException::class);
|
||||
app(GetGPSCoordinate::class)->execute($request);
|
||||
}
|
||||
}
|
||||
88
tests/Unit/Services/Instance/TokenCleanTest.php
Normal file
88
tests/Unit/Services/Instance/TokenCleanTest.php
Normal file
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Instance;
|
||||
|
||||
use Tests\TestCase;
|
||||
use App\Models\User\User;
|
||||
use App\Models\User\SyncToken;
|
||||
use App\Models\Account\Account;
|
||||
use Sabre\VObject\PHPUnitAssertions;
|
||||
use App\Services\Instance\TokenClean;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
|
||||
class TokenCleanTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions,
|
||||
PHPUnitAssertions;
|
||||
|
||||
/** @test */
|
||||
public function tokenclean_left_one_token()
|
||||
{
|
||||
$account = factory(Account::class)->create();
|
||||
$user = factory(User::class)->create([
|
||||
'account_id' => $account->id,
|
||||
]);
|
||||
|
||||
SyncToken::create([
|
||||
'account_id' => $account->id,
|
||||
'user_id' => $user->id,
|
||||
'name' => 'contacts',
|
||||
'timestamp' => now(),
|
||||
]);
|
||||
app(TokenClean::class)->execute(['dryrun' => false]);
|
||||
|
||||
$this->assertDatabaseHas('synctoken', [
|
||||
'account_id' => $account->id,
|
||||
'user_id' => $user->id,
|
||||
'name' => 'contacts',
|
||||
]);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function tokenclean_left_all_token()
|
||||
{
|
||||
$account = factory(Account::class)->create();
|
||||
$user = factory(User::class)->create([
|
||||
'account_id' => $account->id,
|
||||
]);
|
||||
|
||||
$s1 = SyncToken::create([
|
||||
'account_id' => $account->id,
|
||||
'user_id' => $user->id,
|
||||
'name' => 'contacts',
|
||||
'timestamp' => now(),
|
||||
]);
|
||||
$s2 = SyncToken::create([
|
||||
'account_id' => $account->id,
|
||||
'user_id' => $user->id,
|
||||
'name' => 'contacts',
|
||||
'timestamp' => now()->addDays(-10),
|
||||
]);
|
||||
$s3 = SyncToken::create([
|
||||
'account_id' => $account->id,
|
||||
'user_id' => $user->id,
|
||||
'name' => 'contacts',
|
||||
'timestamp' => now()->addDays(-15),
|
||||
]);
|
||||
$s4 = SyncToken::create([
|
||||
'account_id' => $account->id,
|
||||
'user_id' => $user->id,
|
||||
'name' => 'contacts',
|
||||
'timestamp' => now()->addDays(-1),
|
||||
]);
|
||||
app(TokenClean::class)->execute(['dryrun' => false]);
|
||||
|
||||
$this->assertDatabaseHas('synctoken', [
|
||||
'id' => $s1->id,
|
||||
]);
|
||||
$this->assertDatabaseMissing('synctoken', [
|
||||
'id' => $s2->id,
|
||||
]);
|
||||
$this->assertDatabaseMissing('synctoken', [
|
||||
'id' => $s3->id,
|
||||
]);
|
||||
$this->assertDatabaseHas('synctoken', [
|
||||
'id' => $s4->id,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\Instance\Weather;
|
||||
|
||||
use Tests\TestCase;
|
||||
use App\Models\Account\Place;
|
||||
use App\Models\Account\Weather;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use App\Exceptions\NoCoordinatesException;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use App\Exceptions\MissingEnvVariableException;
|
||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||
use App\Services\Instance\Weather\GetWeatherInformation;
|
||||
|
||||
class GetWeatherInformationTest extends TestCase
|
||||
{
|
||||
use DatabaseTransactions;
|
||||
|
||||
/** @test */
|
||||
public function it_gets_weather_information_normal()
|
||||
{
|
||||
$place = factory(Place::class)->create([
|
||||
'latitude' => '34.112456',
|
||||
'longitude' => '-118.4270732',
|
||||
]);
|
||||
|
||||
config(['monica.enable_weather' => true]);
|
||||
config(['monica.weatherapi_key' => 'test']);
|
||||
|
||||
$body = file_get_contents(base_path('tests/Fixtures/Services/Instance/Weather/GetWeatherInformationSampleResponse.json'));
|
||||
Http::fake([
|
||||
'api.weatherapi.com/v1/*' => Http::response($body, 200),
|
||||
]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $place->account_id,
|
||||
'place_id' => $place->id,
|
||||
];
|
||||
|
||||
$weather = app(GetWeatherInformation::class)->execute($request);
|
||||
|
||||
$this->assertDatabaseHas('weather', [
|
||||
'id' => $weather->id,
|
||||
'account_id' => $place->account_id,
|
||||
'place_id' => $place->id,
|
||||
]);
|
||||
|
||||
$this->assertEquals(
|
||||
'Partly cloudy',
|
||||
$weather->summary
|
||||
);
|
||||
|
||||
$this->assertInstanceOf(
|
||||
Weather::class,
|
||||
$weather
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_cant_get_weather_info_if_weather_not_enabled()
|
||||
{
|
||||
$place = factory(Place::class)->create([
|
||||
'latitude' => '34.112456',
|
||||
'longitude' => '-118.4270732',
|
||||
]);
|
||||
|
||||
config(['monica.enable_weather' => false]);
|
||||
|
||||
$request = [
|
||||
'place_id' => $place->id,
|
||||
];
|
||||
|
||||
$this->expectException(MissingEnvVariableException::class);
|
||||
app(GetWeatherInformation::class)->execute($request);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_cant_get_weather_info_if_weatherapi_key_not_provided()
|
||||
{
|
||||
$place = factory(Place::class)->create([
|
||||
'latitude' => '34.112456',
|
||||
'longitude' => '-118.4270732',
|
||||
]);
|
||||
|
||||
config(['monica.enable_weather' => true]);
|
||||
config(['monica.weatherapi_key' => null]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $place->account_id,
|
||||
'place_id' => $place->id,
|
||||
];
|
||||
|
||||
$this->expectException(MissingEnvVariableException::class);
|
||||
app(GetWeatherInformation::class)->execute($request);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_cant_get_weather_info_if_latitude_longitude_are_null()
|
||||
{
|
||||
$place = factory(Place::class)->create([]);
|
||||
|
||||
config(['monica.enable_weather' => true]);
|
||||
config(['monica.weatherapi_key' => 'test']);
|
||||
config(['monica.enable_geolocation' => false]);
|
||||
|
||||
$request = [
|
||||
'account_id' => $place->account_id,
|
||||
'place_id' => $place->id,
|
||||
];
|
||||
|
||||
$this->expectException(NoCoordinatesException::class);
|
||||
app(GetWeatherInformation::class)->execute($request);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_fails_if_wrong_parameters_are_given()
|
||||
{
|
||||
config(['monica.enable_weather' => true]);
|
||||
config(['monica.weatherapi_key' => 'test']);
|
||||
|
||||
$request = [];
|
||||
|
||||
$this->expectException(ValidationException::class);
|
||||
app(GetWeatherInformation::class)->execute($request);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user