Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
snipe committed Nov 21, 2024
2 parents d10f118 + 2a21cce commit b99b56b
Show file tree
Hide file tree
Showing 21 changed files with 511 additions and 134 deletions.
2 changes: 2 additions & 0 deletions .env.testing.example
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ DB_PORT=3306
DB_DATABASE=null
DB_USERNAME=null
DB_PASSWORD=null

[email protected]
5 changes: 4 additions & 1 deletion .github/workflows/tests-sqlite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ jobs:
cp -v .env.testing.example .env
cp -v .env.testing.example .env.testing
- name: Create database file
run: touch database/database.sqlite

- name: Install Dependencies
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist

Expand All @@ -57,5 +60,5 @@ jobs:

- name: Execute tests (Unit and Feature tests) via PHPUnit
env:
DB_CONNECTION: sqlite_testing
DB_CONNECTION: sqlite
run: php artisan test
336 changes: 217 additions & 119 deletions app/Http/Controllers/Api/AssetsController.php

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion app/Http/Controllers/Components/ComponentsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public function destroy($componentId)
$this->authorize('delete', $component);

// Remove the image if one exists
if (Storage::disk('public')->exists('components/'.$component->image)) {
if ($component->image && Storage::disk('public')->exists('components/' . $component->image)) {
try {
Storage::disk('public')->delete('components/'.$component->image);
} catch (\Exception $e) {
Expand Down
2 changes: 1 addition & 1 deletion app/Listeners/CheckoutableListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ private function getNotifiables($event){
return $event->checkedOutTo->manager?->email ?? '';
}
else{
return $event->checkedOutTo->email;
return $event->checkedOutTo?->email ?? '';
}
}

Expand Down
2 changes: 1 addition & 1 deletion app/Mail/CheckinAccessoryMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function __construct(Accessory $accessory, $checkedOutTo, User $checkedIn
*/
public function envelope(): Envelope
{
$from = new Address(env('MAIL_FROM_ADDR','[email protected]'));
$from = new Address(config('mail.from.address'));

return new Envelope(
from: $from,
Expand Down
2 changes: 1 addition & 1 deletion app/Mail/CheckinAssetMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function __construct(Asset $asset, $checkedOutTo, User $checkedInBy, $not
*/
public function envelope(): Envelope
{
$from = new Address(env('MAIL_FROM_ADDR','[email protected]'));
$from = new Address(config('mail.from.address'));

return new Envelope(
from: $from,
Expand Down
2 changes: 1 addition & 1 deletion app/Mail/CheckinLicenseMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function __construct(LicenseSeat $licenseSeat, $checkedOutTo, User $check
*/
public function envelope(): Envelope
{
$from = new Address(env('MAIL_FROM_ADDR','[email protected]'));
$from = new Address(config('mail.from.address'));

return new Envelope(
from: $from,
Expand Down
2 changes: 1 addition & 1 deletion app/Mail/CheckoutAccessoryMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function __construct(Accessory $accessory, $checkedOutTo, User $checkedOu
*/
public function envelope(): Envelope
{
$from = new Address(env('MAIL_FROM_ADDR','[email protected]'));
$from = new Address(config('mail.from.address'));

return new Envelope(
from: $from,
Expand Down
2 changes: 1 addition & 1 deletion app/Mail/CheckoutAssetMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function __construct(Asset $asset, $checkedOutTo, User $checkedOutBy, $no
*/
public function envelope(): Envelope
{
$from = new Address(env('MAIL_FROM_ADDR', '[email protected]'));
$from = new Address(config('mail.from.address'));

return new Envelope(
from: $from,
Expand Down
2 changes: 1 addition & 1 deletion app/Mail/CheckoutConsumableMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __construct(Consumable $consumable, $checkedOutTo, User $checked
*/
public function envelope(): Envelope
{
$from = new Address(env('MAIL_FROM_ADDR','[email protected]'));
$from = new Address(config('mail.from.address'));

return new Envelope(
from: $from,
Expand Down
2 changes: 1 addition & 1 deletion app/Mail/CheckoutLicenseMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function __construct(LicenseSeat $licenseSeat, $checkedOutTo, User $check
*/
public function envelope(): Envelope
{
$from = new Address(env('MAIL_FROM_ADDR','[email protected]'));
$from = new Address(config('mail.from.address'));

return new Envelope(
from: $from,
Expand Down
13 changes: 8 additions & 5 deletions resources/lang/en-US/admin/hardware/message.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

return [

'undeployable' => '<strong>Warning: </strong> This asset has been marked as currently undeployable. If this status has changed, please update the asset status.',
'does_not_exist' => 'Asset does not exist.',
'does_not_exist_var'=> 'Asset with tag :asset_tag not found.',
'no_tag' => 'No asset tag provided.',
'undeployable' => '<strong>Warning: </strong> This asset has been marked as currently undeployable. If this status has changed, please update the asset status.',
'does_not_exist' => 'Asset does not exist.',
'does_not_exist_var' => 'Asset with tag :asset_tag not found.',
'no_tag' => 'No asset tag provided.',
'does_not_exist_or_not_requestable' => 'That asset does not exist or is not requestable.',
'assoc_users' => 'This asset is currently checked out to a user and cannot be deleted. Please check the asset in first, and then try deleting again. ',
'assoc_users' => 'This asset is currently checked out to a user and cannot be deleted. Please check the asset in first, and then try deleting again. ',
'warning_audit_date_mismatch' => 'This asset\'s next audit date (:next_audit_date) is before the last audit date (:last_audit_date). Please update the next audit date.',
'labels_generated' => 'Labels were successfully generated.',
'error_generating_labels' => 'Error while generating labels.',
'no_assets_selected' => 'No assets selected.',

'create' => [
'error' => 'Asset was not created, please try again. :(',
Expand Down
3 changes: 3 additions & 0 deletions resources/views/mail/markdown/checkin-asset.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
@if (($item->name!=$item->asset_tag))
| **{{ trans('mail.asset_tag') }}** | {{ $item->asset_tag }} |
@endif
@if (isset($item->model->category))
| **{{ trans('general.category') }}** | {{ $item->model->category->name }} |
@endif
@if (isset($item->manufacturer))
| **{{ trans('general.manufacturer') }}** | {{ $item->manufacturer->name }} |
@endif
Expand Down
3 changes: 3 additions & 0 deletions resources/views/mail/markdown/checkout-asset.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
@if (($item->name!=$item->asset_tag))
| **{{ trans('mail.asset_tag') }}** | {{ $item->asset_tag }} |
@endif
@if (isset($item->model->category))
| **{{ trans('general.category') }}** | {{ $item->model->category->name }} |
@endif
@if (isset($item->manufacturer))
| **{{ trans('general.manufacturer') }}** | {{ $item->manufacturer->name }} |
@endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
@if ((isset($item_tag)) && ($item_tag!=''))
| **{{ trans('mail.asset_tag') }}** | {{ $item_tag }} |
@endif
@if (isset($item->model->category))
| **{{ trans('general.category') }}** | {{ $item->model->category->name }} |
@endif
@if ((isset($item_model)) && ($item_model!=''))
| **{{ trans('mail.asset_name') }}** | {{ $item_model }} |
@endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
@if ((isset($item->asset_tag)) && ($item->asset_tag!=''))
| **{{ trans('mail.asset_tag') }}** | {{ $item->asset_tag }} |
@endif
@if (isset($item->model->category))
| **{{ trans('general.category') }}** | {{ $item->model->category->name }} |
@endif
@if ((isset($item->name)) && ($item->name!=''))
| **{{ trans('mail.asset_name') }}** | {{ $item->name }} |
@endif
Expand Down
10 changes: 10 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -1282,4 +1282,14 @@
], 404);
}); // end fallback routes

/**
* Generate label routes
*/
Route::post('hardware/labels', [
Api\AssetsController::class,
'getLabels'
])->name('api.assets.labels');
// end generate label routes


}); // end API routes
140 changes: 140 additions & 0 deletions tests/Feature/Checkouts/Api/ComponentCheckoutTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?php

namespace Tests\Feature\Checkouts\Api;

use App\Models\Asset;
use App\Models\Company;
use App\Models\Component;
use App\Models\Location;
use App\Models\User;
use Carbon\Carbon;
use Tests\Concerns\TestsFullMultipleCompaniesSupport;
use Tests\Concerns\TestsPermissionsRequirement;
use Tests\TestCase;

class ComponentCheckoutTest extends TestCase implements TestsFullMultipleCompaniesSupport, TestsPermissionsRequirement
{
public function testRequiresPermission()
{
$component = Component::factory()->create();

$this->actingAsForApi(User::factory()->create())
->postJson(route('api.components.checkout', $component->id))
->assertForbidden();
}

public function testCannotCheckoutNonExistentComponent()
{
$this->actingAsForApi(User::factory()->checkoutComponents()->create())
->postJson(route('api.components.checkout', 1000))
->assertOk()
->assertStatusMessageIs('error')
->assertMessagesAre('Component does not exist.');
}

public function testCheckingOutComponentRequiresValidFields()
{
$component = Component::factory()->create();

$this->actingAsForApi(User::factory()->checkoutComponents()->create())
->postJson(route('api.components.checkout', $component->id), [
//
])
->assertOk()
->assertStatusMessageIs('error')
->assertPayloadContains('assigned_to')
->assertPayloadContains('assigned_qty');
}

public function testCannotCheckoutComponentIfRequestedAmountIsMoreThanComponentQuantity()
{
$asset = Asset::factory()->create();
$component = Component::factory()->create(['qty' => 2]);

$this->actingAsForApi(User::factory()->checkoutComponents()->create())
->postJson(route('api.components.checkout', $component->id), [
'assigned_to' => $asset->id,
'assigned_qty' => 3,
])
->assertOk()
->assertStatusMessageIs('error')
->assertMessagesAre(trans('admin/components/message.checkout.unavailable', ['remaining' => 2, 'requested' => 3]));
}

public function testCannotCheckoutComponentIfRequestedAmountIsMoreThanWhatIsRemaining()
{
$asset = Asset::factory()->create();
$component = Component::factory()->create(['qty' => 2]);
$component->assets()->attach($component->id, [
'component_id' => $component->id,
'created_at' => Carbon::now(),
'assigned_qty' => 1,
'asset_id' => $asset->id,
]);

$this->actingAsForApi(User::factory()->checkoutComponents()->create())
->postJson(route('api.components.checkout', $component->id), [
'assigned_to' => $asset->id,
'assigned_qty' => 3,
])
->assertOk()
->assertStatusMessageIs('error');
}

public function testCanCheckoutComponent()
{
$user = User::factory()->checkoutComponents()->create();
$asset = Asset::factory()->create();
$component = Component::factory()->create();

$this->actingAsForApi($user)
->postJson(route('api.components.checkout', $component->id), [
'assigned_to' => $asset->id,
'assigned_qty' => 1,
])
->assertOk()
->assertStatusMessageIs('success');

$this->assertTrue($component->assets->first()->is($asset));
}

public function testComponentCheckoutIsLogged()
{
$user = User::factory()->checkoutComponents()->create();
$location = Location::factory()->create();
$asset = Asset::factory()->create(['location_id' => $location->id]);
$component = Component::factory()->create();

$this->actingAsForApi($user)
->postJson(route('api.components.checkout', $component->id), [
'assigned_to' => $asset->id,
'assigned_qty' => 1,
]);

$this->assertDatabaseHas('action_logs', [
'created_by' => $user->id,
'action_type' => 'checkout',
'target_id' => $asset->id,
'target_type' => Asset::class,
'location_id' => $location->id,
'item_type' => Component::class,
'item_id' => $component->id,
]);
}

public function testAdheresToFullMultipleCompaniesSupportScoping()
{
[$companyA, $companyB] = Company::factory()->count(2)->create();

$userForCompanyA = User::factory()->for($companyA)->create();
$assetForCompanyB = Asset::factory()->for($companyB)->create();
$componentForCompanyB = Component::factory()->for($companyB)->create();

$this->actingAsForApi($userForCompanyA)
->postJson(route('api.components.checkout', $componentForCompanyB->id), [
'assigned_to' => $assetForCompanyB->id,
'assigned_qty' => 1,
])
->assertForbidden();
}
}
Loading

0 comments on commit b99b56b

Please sign in to comment.