diff --git a/app/Console/Commands/CleanSquawkAllocations.php b/app/Console/Commands/CleanSquawkAllocations.php index 694b65890..79fae507a 100644 --- a/app/Console/Commands/CleanSquawkAllocations.php +++ b/app/Console/Commands/CleanSquawkAllocations.php @@ -24,7 +24,7 @@ public function handle() DB::table('squawk_allocation')->where( 'allocated_at', '<', - Carbon::now()->subMinutes(env('APP_SQUAWK_ALLOCATION_MIN'))->format('Y-m-d H:i:s') + Carbon::now()->subMinutes(config('squawk.allocation_min'))->format('Y-m-d H:i:s') )->delete(); Log::Info('Squawk allocations cleaned successfully'); diff --git a/app/Helpers/User/UserConfig.php b/app/Helpers/User/UserConfig.php index 5945cced7..5a9d3c3a0 100644 --- a/app/Helpers/User/UserConfig.php +++ b/app/Helpers/User/UserConfig.php @@ -44,7 +44,7 @@ public function apiKey() : string */ public function apiUrl() : string { - return env('APP_URL'); + return config('app.url'); } /** @@ -56,7 +56,7 @@ public function apiUrl() : string public function jsonSerialize() { return [ - 'api-url' => env('APP_URL'), + 'api-url' => config('app.url'), 'api-key' => $this->accessToken, ]; } diff --git a/app/Http/Controllers/DependencyController.php b/app/Http/Controllers/DependencyController.php index cb99ca1aa..6b6c71f4a 100644 --- a/app/Http/Controllers/DependencyController.php +++ b/app/Http/Controllers/DependencyController.php @@ -2,7 +2,9 @@ namespace App\Http\Controllers; use App\Models\Dependency\Dependency; +use Carbon\Carbon; use Illuminate\Http\JsonResponse; +use Illuminate\Support\Facades\Auth; class DependencyController extends BaseController { @@ -13,11 +15,32 @@ class DependencyController extends BaseController */ public function getAllDependencies() : JsonResponse { - $dependencies = Dependency::all()->map(function (Dependency $dependency) { + $dependencies = Dependency::with('user')->get()->map(function (Dependency $dependency) { + $updatedAt = $dependency->updated_at; + if ($dependency->per_user) { + if (!$dependency->user->first()) { + $dependency->user()->attach( + $dependency->id, + [ + 'user_id' => Auth::user()->id, + 'updated_at' => Carbon::now(), + ] + ); + $dependency->load('user'); + } + + $updatedAt = $dependency->user->first()->pivot->updated_at; + } elseif (!$updatedAt) { + $dependency->updated_at = Carbon::now(); + $dependency->save(); + $updatedAt = $dependency->updated_at; + } + return [ 'key' => $dependency->key, 'uri' => sprintf('%s/%s', config('app.url'), $dependency->uri), 'local_file' => $dependency->local_file, + 'updated_at' => $updatedAt->timestamp, ]; }); return response()->json($dependencies); diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php index 1cf1ec89e..b407306d6 100644 --- a/app/Http/Kernel.php +++ b/app/Http/Kernel.php @@ -5,6 +5,7 @@ use App\Http\Middleware\Authenticate; use App\Http\Middleware\GithubAuth; use App\Http\Middleware\LogAdminAction; +use App\Http\Middleware\UpdateDependency; use App\Http\Middleware\UserIsBanned; use App\Http\Middleware\UserIsDisabled; use App\Http\Middleware\UserLastLogin; @@ -23,6 +24,7 @@ class Kernel extends HttpKernel 'auth' => Authenticate::class, 'auth.github' => GithubAuth::class, 'admin.log' => LogAdminAction::class, + 'dependency.update' => UpdateDependency::class, 'user.banned' => UserIsBanned::class, 'user.disabled' => UserIsDisabled::class, 'user.lastlogin' => UserLastLogin::class, diff --git a/app/Http/Middleware/UpdateDependency.php b/app/Http/Middleware/UpdateDependency.php new file mode 100644 index 000000000..6ace1dd66 --- /dev/null +++ b/app/Http/Middleware/UpdateDependency.php @@ -0,0 +1,27 @@ + 'boolean', + ]; + + public function user(): BelongsToMany + { + return $this->belongsToMany(User::class)->withTimestamps(); + } } diff --git a/app/Models/User/User.php b/app/Models/User/User.php index cf363d358..3c12768cf 100644 --- a/app/Models/User/User.php +++ b/app/Models/User/User.php @@ -1,11 +1,13 @@ save(); return $this; } - + /** * Marks the user as disabled * @@ -136,4 +138,9 @@ public function jsonSerialize() : array 'tokens' => $this->tokens, ]; } + + public function dependencies(): BelongsToMany + { + return $this->belongsToMany(Dependency::class)->withTimestamps(); + } } diff --git a/app/Providers/RegionalPressureServiceProvider.php b/app/Providers/RegionalPressureServiceProvider.php index b79ea804f..d16e31dc2 100644 --- a/app/Providers/RegionalPressureServiceProvider.php +++ b/app/Providers/RegionalPressureServiceProvider.php @@ -24,7 +24,7 @@ public function register() { $this->app->bind(RegionalPressureService::class, function (Application $app) { // Create dependencies - $metarUri = env('APP_REGIONAL_PRESSURES_URL', ''); + $metarUri = config('metar.regional_url'); $http = new Client(); $metarParser = $app->make(MetarService::class); return new RegionalPressureService($http, $metarUri, $metarParser); diff --git a/app/Services/DependencyService.php b/app/Services/DependencyService.php new file mode 100644 index 000000000..ff6ab5ab3 --- /dev/null +++ b/app/Services/DependencyService.php @@ -0,0 +1,47 @@ +first(); + + if (!$dependency) { + Log::error(sprintf('Dependency %s not found to update', $key)); + return; + } + + if ($dependency->per_user && $user === null) { + Log::error(sprintf('Dependency %s is per user but user was not specifieid', $key)); + return; + } + + if ($dependency->per_user) { + self::touchUserDependency($dependency, $user); + } else { + self::touchGlobalDependency($dependency); + } + } + + public static function touchGlobalDependency(Dependency $dependency): void + { + $dependency->touch(); + } + + public static function touchUserDependency(Dependency $dependency, User $user): void + { + if (!$dependency->per_user) { + throw new LogicException(sprintf('Dependency %s is not a per-user dependency', $dependency->key)); + } + + $user->dependencies()->updateExistingPivot($dependency->id, ['updated_at' => Carbon::now()]); + } +} diff --git a/app/Services/MetarService.php b/app/Services/MetarService.php index 73196ab3f..f9552f469 100644 --- a/app/Services/MetarService.php +++ b/app/Services/MetarService.php @@ -71,7 +71,7 @@ public function getQnhFromVatsimMetar(string $icao) : ?int } $metar = $this->httpClient->get( - env('VATSIM_METAR_URL'), + config('metar.vatsim_url'), [ RequestOptions::ALLOW_REDIRECTS => true, RequestOptions::HTTP_ERRORS => false, diff --git a/config/metar.php b/config/metar.php new file mode 100644 index 000000000..2de399edf --- /dev/null +++ b/config/metar.php @@ -0,0 +1,7 @@ + env('VATSIM_METAR_URL', 'metar.vatsim.net'), + 'regional_url' => env('APP_REGIONAL_PRESSURES_URL', 'https://www.aviationweather.gov/adds/dataserver_current/httpparam?dataSource=metars&requesttype=retrieve&format=xml&hoursBeforeNow=3&mostRecentForEachStation=constraint&stationString=~gb&fields=raw_text,station_id'), +]; diff --git a/config/squawk.php b/config/squawk.php new file mode 100644 index 000000000..878a7fd66 --- /dev/null +++ b/config/squawk.php @@ -0,0 +1,5 @@ + env('APP_SQUAWK_ALLOCATION_MIN', 45), +]; diff --git a/database/migrations/2020_04_03_160614_add_user_dependency_column.php b/database/migrations/2020_04_03_160614_add_user_dependency_column.php new file mode 100644 index 000000000..2daa0906d --- /dev/null +++ b/database/migrations/2020_04_03_160614_add_user_dependency_column.php @@ -0,0 +1,35 @@ +boolean('per_user') + ->default(false) + ->after('local_file') + ->comment('If the dependency is per-user'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('dependencies', function (Blueprint $table) { + $table->dropColumn('per_user'); + }); + } +} diff --git a/database/migrations/2020_04_03_161131_add_per_user_dependencies.php b/database/migrations/2020_04_03_161131_add_per_user_dependencies.php new file mode 100644 index 000000000..103406b8d --- /dev/null +++ b/database/migrations/2020_04_03_161131_add_per_user_dependencies.php @@ -0,0 +1,30 @@ +update(['per_user' => 1]); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Dependency::where('key', 'DEPENDENCY_HOLD_PROFILE') + ->update(['per_user' => 0]); + } +} diff --git a/database/migrations/2020_04_03_161652_add_dependency_user_table.php b/database/migrations/2020_04_03_161652_add_dependency_user_table.php new file mode 100644 index 000000000..524b39f2c --- /dev/null +++ b/database/migrations/2020_04_03_161652_add_dependency_user_table.php @@ -0,0 +1,36 @@ +unsignedMediumInteger('dependency_id')->comment('The dependency'); + $table->unsignedInteger('user_id')->comment('The user the dependency belongs to'); + $table->timestamps(); + + $table->primary(['dependency_id', 'user_id']); + $table->foreign('dependency_id')->references('id')->on('dependencies')->onDelete('cascade'); + $table->foreign('user_id')->references('id')->on('user')->onDelete('cascade'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('dependency_user'); + } +} diff --git a/database/seeds/DatabaseSeeder.php b/database/seeds/DatabaseSeeder.php index d8b8bbf08..36399299c 100644 --- a/database/seeds/DatabaseSeeder.php +++ b/database/seeds/DatabaseSeeder.php @@ -62,6 +62,7 @@ class DatabaseSeeder extends Seeder 'sid_prenotes', ], DependencyTableSeeder::class => [ + 'dependency_user', 'dependencies', ], AircraftTableSeeder::class => [ diff --git a/database/seeds/DependencyTableSeeder.php b/database/seeds/DependencyTableSeeder.php index 50e2f2c81..a16d3ad23 100644 --- a/database/seeds/DependencyTableSeeder.php +++ b/database/seeds/DependencyTableSeeder.php @@ -2,6 +2,7 @@ use App\Models\Dependency\Dependency; use Illuminate\Database\Seeder; +use Illuminate\Support\Facades\DB; class DependencyTableSeeder extends Seeder { @@ -13,11 +14,58 @@ public function run() 'key' => 'DEPENDENCY_ONE', 'uri' => 'dependency/one', 'local_file' => 'one.json', + 'updated_at' => '2020-04-02 21:00:00', + 'per_user' => false, ], [ 'key' => 'DEPENDENCY_TWO', 'uri' => 'dependency/two', 'local_file' => 'two.json', + 'updated_at' => '2020-04-03 21:00:00', + 'per_user' => false, + ], + [ + 'key' => 'USER_DEPENDENCY_ONE', + 'uri' => 'dependency/userone', + 'local_file' => 'userone.json', + 'updated_at' => '2020-04-02 21:00:00', + 'per_user' => true, + ], + [ + 'key' => 'USER_DEPENDENCY_TWO', + 'uri' => 'dependency/usertwo', + 'local_file' => 'usertwo.json', + 'updated_at' => '2020-04-03 21:00:00', + 'per_user' => true, + ], + [ + 'key' => 'USER_DEPENDENCY_THREE', + 'uri' => 'dependency/userthree', + 'local_file' => 'userthree.json', + 'updated_at' => '2020-04-01 21:00:00', + 'per_user' => true, + ], + [ + 'key' => 'DEPENDENCY_THREE', + 'uri' => 'dependency/three', + 'local_file' => 'three.json', + 'updated_at' => null, + 'per_user' => false, + ], + ] + ); + + DB::table('dependency_user')->insert( + [ + [ + 'dependency_id' => 3, + 'user_id' => UserTableSeeder::ACTIVE_USER_CID, + 'updated_at' => '2020-04-04 21:00:00', + ], + [ + 'dependency_id' => 4, + 'user_id' => UserTableSeeder::ACTIVE_USER_CID, + 'updated_at' => '2020-04-05 21:00:00', ] ] ); diff --git a/database/seeds/SquawkAllocationTableSeeder.php b/database/seeds/SquawkAllocationTableSeeder.php index f330ce3b7..90b082c1b 100644 --- a/database/seeds/SquawkAllocationTableSeeder.php +++ b/database/seeds/SquawkAllocationTableSeeder.php @@ -27,21 +27,21 @@ public function run() 'callsign' => 'BAW456', 'squawk' => '2321', 'allocated_by' => 1203533, - 'allocated_at' => Carbon::now()->subMinutes(env('APP_SQUAWK_ALLOCATION_MIN') - 1), + 'allocated_at' => Carbon::now()->subMinutes(config('squawk.allocation_min') - 1), ], [ 'id' => 3, 'callsign' => 'NAX1431', 'squawk' => '4325', 'allocated_by' => 1203533, - 'allocated_at' => Carbon::now()->subMinutes(env('APP_SQUAWK_ALLOCATION_MIN')), + 'allocated_at' => Carbon::now()->subMinutes(config('squawk.allocation_min')), ], [ 'id' => 4, 'callsign' => 'TOM43E', 'squawk' => '5436', 'allocated_by' => 1203533, - 'allocated_at' => Carbon::now()->subMinutes(env('APP_SQUAWK_ALLOCATION_MIN') + 1), + 'allocated_at' => Carbon::now()->subMinutes(config('squawk.allocation_min') + 1), ], ] ); diff --git a/routes/api.php b/routes/api.php index 233ae07e4..d3df72a3d 100644 --- a/routes/api.php +++ b/routes/api.php @@ -26,13 +26,20 @@ ] ); + // Dependencies + Route::get('dependency', 'DependencyController@getAllDependencies'); + // Holds Route::get('hold/profile', 'HoldController@getUserHoldProfiles'); - Route::put('hold/profile', 'HoldController@createUserHoldProfile'); - Route::put('hold/profile/{profile_id}', 'HoldController@updateUserHoldProfile') - ->where('profile_id', '\d+'); - Route::delete('hold/profile/{profile_id}', 'HoldController@deleteUserHoldProfile') - ->where('profile_id', '\d+'); + + // Whenever a hold profile is updated, we need to reset the dependency + Route::middleware('dependency.update:DEPENDENCY_HOLD_PROFILE')->group(function () { + Route::put('hold/profile', 'HoldController@createUserHoldProfile'); + Route::put('hold/profile/{profile_id}', 'HoldController@updateUserHoldProfile') + ->where('profile_id', '\d+'); + Route::delete('hold/profile/{profile_id}', 'HoldController@deleteUserHoldProfile') + ->where('profile_id', '\d+'); + }); // Squawks Route::get('squawk-assignment/{callsign}', 'SquawkController@getSquawkAssignment') @@ -131,11 +138,16 @@ Route::middleware('admin.dependency')->group(function () { // Initial altitudes and sids - Route::delete('sid/{id}', 'SidController@deleteSid') - ->where('sid', 'd+'); - Route::put('sid', 'SidController@createSid'); - Route::put('sid/{id}', 'SidController@updateSid') - ->where('sid', 'd+'); + Route::middleware('dependency.update:DEPENDENCY_PRENOTE,DEPENDENCY_INITIAL_ALTITUDES,DEPENDENCY_SID_HANDOFF') + ->group( + function () { + Route::delete('sid/{id}', 'SidController@deleteSid') + ->where('sid', 'd+'); + Route::put('sid', 'SidController@createSid'); + Route::put('sid/{id}', 'SidController@updateSid') + ->where('sid', 'd+'); + } + ); }); Route::middleware('admin.github')->group(function () { @@ -168,9 +180,6 @@ ] )->where('version', '[A-Za-z0-9\.\-]+'); - // Dependencies - Route::get('dependency', 'DependencyController@getAllDependencies'); - // Controller positions Route::get('controller', 'ControllerPositionController@getAllControllers'); Route::get('controller-positions', 'ControllerPositionController@getControllerPositionsDependency'); diff --git a/tests/app/Console/Commands/CleanSquawkAllocationsTest.php b/tests/app/Console/Commands/CleanSquawkAllocationsTest.php index 646ad6afd..b780b024b 100644 --- a/tests/app/Console/Commands/CleanSquawkAllocationsTest.php +++ b/tests/app/Console/Commands/CleanSquawkAllocationsTest.php @@ -21,7 +21,7 @@ public function testItCleansAllocations() 'callsign' => 'VIR25E', 'squawk' => '2321', 'allocated_by' => self::ACTIVE_USER_CID, - 'allocated_at' => Carbon::now()->subMinutes(env('APP_SQUAWK_ALLOCATION_MIN') - 1), + 'allocated_at' => Carbon::now()->subMinutes(config('squawk.allocation_min') - 1), ] ); Allocation::create( @@ -29,7 +29,7 @@ public function testItCleansAllocations() 'callsign' => 'UAL242', 'squawk' => '4325', 'allocated_by' => self::ACTIVE_USER_CID, - 'allocated_at' => Carbon::now()->subMinutes(env('APP_SQUAWK_ALLOCATION_MIN') + 10), + 'allocated_at' => Carbon::now()->subMinutes(config('squawk.allocation_min') + 10), ] ); Allocation::create( @@ -37,10 +37,10 @@ public function testItCleansAllocations() 'callsign' => 'TCX125', 'squawk' => '5436', 'allocated_by' => self::ACTIVE_USER_CID, - 'allocated_at' => Carbon::now()->subMinutes(env('APP_SQUAWK_ALLOCATION_MIN') + 1), + 'allocated_at' => Carbon::now()->subMinutes(config('squawk.allocation_min') + 1), ] ); - + $this->assertDatabaseHas('squawk_allocation', ['callsign' => 'VIR25E']); $this->assertDatabaseHas('squawk_allocation', ['callsign' => 'UAL242']); $this->assertDatabaseHas('squawk_allocation', ['callsign' => 'TCX125']); diff --git a/tests/app/Helpers/User/UserConfigTest.php b/tests/app/Helpers/User/UserConfigTest.php index db467cf8c..0ad398c10 100644 --- a/tests/app/Helpers/User/UserConfigTest.php +++ b/tests/app/Helpers/User/UserConfigTest.php @@ -31,13 +31,13 @@ public function testItHasApiKey() public function testItHasAnApiUrl() { - $this->assertEquals(env('APP_URL'), $this->userConfig->apiUrl()); + $this->assertEquals(config('app.url'), $this->userConfig->apiUrl()); } public function testItSerializesToJson() { $expected = [ - 'api-url' => env('APP_URL'), + 'api-url' => config('app.url'), 'api-key' => 'user-api-key', ]; diff --git a/tests/app/Http/Controllers/DependencyControllerTest.php b/tests/app/Http/Controllers/DependencyControllerTest.php index d78abf7b6..09f8f54fc 100644 --- a/tests/app/Http/Controllers/DependencyControllerTest.php +++ b/tests/app/Http/Controllers/DependencyControllerTest.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers; use App\BaseApiTestCase; +use Carbon\Carbon; class DependencyControllerTest extends BaseApiTestCase { @@ -13,20 +14,48 @@ public function testItConstructs() public function testItReturnsDependencies() { + Carbon::setTestNow(Carbon::now()); + $expected = [ [ 'key' => 'DEPENDENCY_ONE', 'uri' => sprintf('%s/dependency/one', config('app.url')), 'local_file' => 'one.json', + 'updated_at' => Carbon::parse('2020-04-02 21:00:00')->timestamp, ], [ 'key' => 'DEPENDENCY_TWO', 'uri' => sprintf('%s/dependency/two', config('app.url')), 'local_file' => 'two.json', - ] + 'updated_at' => Carbon::parse('2020-04-03 21:00:00')->timestamp, + ], + [ + 'key' => 'USER_DEPENDENCY_ONE', + 'uri' => sprintf('%s/dependency/userone', config('app.url')), + 'local_file' => 'userone.json', + 'updated_at' => Carbon::parse('2020-04-04 21:00:00')->timestamp, + ], + [ + 'key' => 'USER_DEPENDENCY_TWO', + 'uri' => sprintf('%s/dependency/usertwo', config('app.url')), + 'local_file' => 'usertwo.json', + 'updated_at' => Carbon::parse('2020-04-05 21:00:00')->timestamp, + ], + [ + 'key' => 'USER_DEPENDENCY_THREE', + 'uri' => sprintf('%s/dependency/userthree', config('app.url')), + 'local_file' => 'userthree.json', + 'updated_at' => Carbon::now()->timestamp, + ], + [ + 'key' => 'DEPENDENCY_THREE', + 'uri' => sprintf('%s/dependency/three', config('app.url')), + 'local_file' => 'three.json', + 'updated_at' => Carbon::now()->timestamp, + ], ]; - $this->makeUnauthenticatedApiRequest(self::METHOD_GET, 'dependency') + $this->makeAuthenticatedApiRequest(self::METHOD_GET, 'dependency') ->assertJson($expected) ->assertStatus(200); } diff --git a/tests/app/Http/Middleware/UpdateDependencyTest.php b/tests/app/Http/Middleware/UpdateDependencyTest.php new file mode 100644 index 000000000..c5cef2432 --- /dev/null +++ b/tests/app/Http/Middleware/UpdateDependencyTest.php @@ -0,0 +1,66 @@ +middleware = $this->app->make(UpdateDependency::class); + } + + public function testItPassesToTheNextMiddleware() + { + $request = Mockery::mock(Request::class); + + $expected = 418; + $actual = $this->middleware->handle($request, function () { + return new Response('', 418); + })->getStatusCode(); + + $this->assertEquals($expected, $actual); + } + + public function testItUpdatesAllDependenciesSpecified() + { + $request = Mockery::mock(Request::class); + $this->middleware->handle( + $request, + function () { + return new Response('', 418); + }, + 'DEPENDENCY_ONE', + 'DEPENDENCY_TWO' + ); + + $now = Carbon::now(); + Date::setTestNow($now); + + DependencyService::touchGlobalDependency(Dependency::where('key', 'DEPENDENCY_ONE')->first()); + $this->assertEquals( + $now->timestamp, + Dependency::where('key', 'DEPENDENCY_ONE')->first()->updated_at->timestamp + ); + $this->assertEquals( + $now->timestamp, + Dependency::where('key', 'DEPENDENCY_TWO')->first()->updated_at->timestamp + ); + } +} diff --git a/tests/app/Services/DependencyServiceTest.php b/tests/app/Services/DependencyServiceTest.php new file mode 100644 index 000000000..a19ed6b4e --- /dev/null +++ b/tests/app/Services/DependencyServiceTest.php @@ -0,0 +1,111 @@ +assertEquals( + '2020-04-02 21:00:00', + Dependency::where('key', self::GLOBAL_DEPENDENCY)->first()->updated_at + ); + } + + public function testItDoesntTouchUserDependenciesIfNoUser() + { + DependencyService::touchDependencyByKey(self::USER_DEPENDENCY, null); + + $timestamp = User::find(self::ACTIVE_USER_CID) + ->dependencies() + ->where('key', self::USER_DEPENDENCY) + ->first() + ->pivot + ->updated_at; + + $this->assertGreaterThanOrEqual('2020-04-02 21:00:00', $timestamp); + } + + public function testItTouchesGlobalDependenciesByKey() + { + $now = Carbon::now(); + Date::setTestNow($now); + + DependencyService::touchDependencyByKey(self::GLOBAL_DEPENDENCY, User::find(self::ACTIVE_USER_CID)); + $this->assertEquals( + $now->timestamp, + Dependency::where('key', self::GLOBAL_DEPENDENCY)->first()->updated_at->timestamp + ); + } + + public function testItTouchesUserDependenciesByKey() + { + $now = Carbon::now(); + Date::setTestNow($now); + + DependencyService::touchDependencyByKey(self::USER_DEPENDENCY, User::find(self::ACTIVE_USER_CID)); + + $timestamp = User::find(self::ACTIVE_USER_CID) + ->dependencies() + ->where('key', self::USER_DEPENDENCY) + ->first() + ->pivot + ->updated_at + ->timestamp; + + $this->assertEquals($now->timestamp, $timestamp); + } + + public function testItTouchesGlobalDependencies() + { + $now = Carbon::now(); + Date::setTestNow($now); + + DependencyService::touchGlobalDependency(Dependency::where('key', self::GLOBAL_DEPENDENCY)->first()); + $this->assertEquals( + $now->timestamp, + Dependency::where('key', self::GLOBAL_DEPENDENCY)->first()->updated_at->timestamp + ); + } + + public function testItThrowsExceptionIfDependencyIsNotPerUser() + { + $this->expectException(LogicException::class); + DependencyService::touchUserDependency( + Dependency::where('key', self::GLOBAL_DEPENDENCY)->first(), + User::find(self::ACTIVE_USER_CID) + ); + } + + public function testItTouchesUserDependencies() + { + $now = Carbon::now(); + Date::setTestNow($now); + + DependencyService::touchUserDependency( + Dependency::where('key', self::USER_DEPENDENCY)->first(), + User::find(self::ACTIVE_USER_CID) + ); + + $timestamp = User::find(self::ACTIVE_USER_CID) + ->dependencies() + ->where('key', self::USER_DEPENDENCY) + ->first() + ->pivot + ->updated_at + ->timestamp; + + $this->assertEquals($now->timestamp, $timestamp); + } +} diff --git a/tests/app/Services/MetarServiceTest.php b/tests/app/Services/MetarServiceTest.php index f6c77499a..9650be5da 100644 --- a/tests/app/Services/MetarServiceTest.php +++ b/tests/app/Services/MetarServiceTest.php @@ -61,7 +61,7 @@ public function testItReturnsNullIfVatsimMetarDownloadFails() $mockClient = Mockery::mock(Client::class); $mockClient->shouldReceive('get') ->with( - env('VATSIM_METAR_URL'), + config('metar.vatsim_url'), [ RequestOptions::ALLOW_REDIRECTS => true, RequestOptions::HTTP_ERRORS => false, @@ -83,7 +83,7 @@ public function testItReturnsNullIfNoMetarAvailable() $mockClient = Mockery::mock(Client::class); $mockClient->shouldReceive('get') ->with( - env('VATSIM_METAR_URL'), + config('metar.vatsim_url'), [ RequestOptions::ALLOW_REDIRECTS => true, RequestOptions::HTTP_ERRORS => false, @@ -105,7 +105,7 @@ public function testItReturnsNullIfMetarNotValid() $mockClient = Mockery::mock(Client::class); $mockClient->shouldReceive('get') ->with( - env('VATSIM_METAR_URL'), + config('metar.vatsim_url'), [ RequestOptions::ALLOW_REDIRECTS => true, RequestOptions::HTTP_ERRORS => false, @@ -127,7 +127,7 @@ public function testItReturnsQnhIfValidMetar() $mockClient = Mockery::mock(Client::class); $mockClient->shouldReceive('get') ->with( - env('VATSIM_METAR_URL'), + config('metar.vatsim_url'), [ RequestOptions::ALLOW_REDIRECTS => true, RequestOptions::HTTP_ERRORS => false, @@ -150,7 +150,7 @@ public function testItCachesMetars() $mockClient->shouldReceive('get') ->once() ->with( - env('VATSIM_METAR_URL'), + config('metar.vatsim_url'), [ RequestOptions::ALLOW_REDIRECTS => true, RequestOptions::HTTP_ERRORS => false, diff --git a/tests/app/Services/UserConfigServiceTest.php b/tests/app/Services/UserConfigServiceTest.php index dd76ce10a..e21c4a6f6 100644 --- a/tests/app/Services/UserConfigServiceTest.php +++ b/tests/app/Services/UserConfigServiceTest.php @@ -34,7 +34,7 @@ public function testcreateThrowsExceptionIfUserNotFound() public function testcreateReturnsConfig() { $config = $this->service->create(1203533); - $this->assertEquals(env('APP_URL'), $config->apiUrl()); + $this->assertEquals(config('app.url'), $config->apiUrl()); $this->assertNotNull($config->apiKey()); } }