Skip to content

A free open source IT asset/license management system

/api_tests: Completed tests for basic CRUD for Assets and Components (#3426)

Andrea Bergamasco7ee172888db6
diff --git a/.travis.yml b/.travis.yml
index 83db93506e..6caf0ca7f6 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -41,6 +41,7 @@ script:
# - ./vendor/bin/codecept run acceptance --env=testing-ci
- ./vendor/bin/codecept run functional --env=functional-travis
#script: ./vendor/bin/codecept run
+ - ./vendor/bin/codecept run api --env=testing-ci
after_success:
- codecov
diff --git a/app/Http/Controllers/Api/AssetsController.php b/app/Http/Controllers/Api/AssetsController.php
index 7a8b212e2d..b79030d2f6 100644
--- a/app/Http/Controllers/Api/AssetsController.php
+++ b/app/Http/Controllers/Api/AssetsController.php
@@ -287,23 +287,36 @@ class AssetsController extends Controller
if ($asset = Asset::find($id)) {
($request->has('model_id')) ?
$asset->model()->associate(AssetModel::find($request->get('model_id'))) : '';
- ($request->has('name')) ? $asset->name = $request->get('name') : '';
- ($request->has('serial')) ? $asset->serial = $request->get('serial') : '';
- ($request->has('model_id')) ? $asset->model_id = $request->get('model_id') : '';
- ($request->has('order_number')) ? $asset->order_number = $request->get('order_number') : '';
- ($request->has('notes')) ? $asset->notes = $request->get('notes') : '';
- ($request->has('asset_tag')) ? $asset->asset_tag = $request->input('asset_tag') : '';
- ($request->has('archived')) ? $asset->archived = $request->get('archived') : '';
- ($request->has('status_id')) ? $asset->status_id = $request->get('status_id') : '';
- ($request->has('warranty_months')) ? $asset->warranty_months = $request->get('warranty_months') : '';
+ ($request->has('name')) ?
+ $asset->name = $request->get('name') : '';
+ ($request->has('serial')) ?
+ $asset->serial = $request->get('serial') : '';
+ ($request->has('model_id')) ?
+ $asset->model_id = $request->get('model_id') : '';
+ ($request->has('order_number')) ?
+ $asset->order_number = $request->get('order_number') : '';
+ ($request->has('notes')) ?
+ $asset->notes = $request->get('notes') : '';
+ ($request->has('asset_tag')) ?
+ $asset->asset_tag = $request->get('asset_tag') : '';
+ ($request->has('archived')) ?
+ $asset->archived = $request->get('archived') : '';
+ ($request->has('status_id')) ?
+ $asset->status_id = $request->get('status_id') : '';
+ ($request->has('warranty_months')) ?
+ $asset->warranty_months = $request->get('warranty_months') : '';
($request->has('purchase_cost')) ?
$asset->purchase_cost = Helper::ParseFloat($request->get('purchase_cost')) : '';
- ($request->has('purchase_date')) ? $asset->purchase_date = $request->get('purchase_date') : '';
- ($request->has('assigned_to')) ? $asset->assigned_to = $request->get('assigned_to') : '';
- ($request->has('supplier_id')) ? $asset->supplier_id = $request->get('supplier_id') : '';
- ($request->has('requestable')) ? $asset->name = $request->get('requestable') : '';
- ($request->has('archived')) ? $asset->name = $request->get('archived') : '';
- ($request->has('rtd_location_id')) ? $asset->name = $request->get('rtd_location_id') : '';
+ ($request->has('purchase_date')) ?
+ $asset->purchase_date = $request->get('purchase_date') : '';
+ ($request->has('assigned_to')) ?
+ $asset->assigned_to = $request->get('assigned_to') : '';
+ ($request->has('supplier_id')) ?
+ $asset->supplier_id = $request->get('supplier_id') : '';
+ ($request->has('requestable')) ?
+ $asset->requestable = $request->get('requestable') : '';
+ ($request->has('rtd_location_id')) ?
+ $asset->rtd_location_id = $request->get('rtd_location_id') : '';
($request->has('company_id')) ?
$asset->company_id = Company::getIdForCurrentUser($request->get('company_id')) : '';
@@ -335,8 +348,6 @@ class AssetsController extends Controller
}
return response()->json(Helper::formatStandardApiResponse('error', null, $asset->getErrors()), 200);
}
-
-
return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/hardware/message.does_not_exist')), 200);
}
diff --git a/app/Http/Controllers/Api/ComponentsController.php b/app/Http/Controllers/Api/ComponentsController.php
index bd02ed720d..1ffb3d4e0e 100644
--- a/app/Http/Controllers/Api/ComponentsController.php
+++ b/app/Http/Controllers/Api/ComponentsController.php
@@ -135,7 +135,7 @@ class ComponentsController extends Controller
$component = Component::findOrFail($id);
$this->authorize('delete', $component);
$component->delete();
- return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/components/message.delete.success')));
+ return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/components/message.delete.success')));
}
/**
diff --git a/app/Models/Asset.php b/app/Models/Asset.php
index ecedaba35e..619a41b13f 100644
--- a/app/Models/Asset.php
+++ b/app/Models/Asset.php
@@ -54,18 +54,18 @@ class Asset extends Depreciable
use ValidatingTrait, UniqueUndeletedTrait;
protected $rules = [
- 'name' => 'max:255|nullable',
- 'model_id' => 'required|integer|exists:models,id',
- 'status_id' => 'required|integer|exists:status_labels,id',
- 'company_id' => 'integer|nullable',
- 'warranty_months' => 'numeric|nullable',
- 'physical' => 'numeric|max:1|nullable',
- 'checkout_date' => 'date|max:10|min:10|nullable',
- 'checkin_date' => 'date|max:10|min:10|nullable',
- 'supplier_id' => 'numeric|nullable',
- 'asset_tag' => 'required|min:1|max:255|unique_undeleted',
- 'status' => 'integer',
- 'purchase_cost' => 'numeric|nullable',
+ 'name' => 'max:255|nullable',
+ 'model_id' => 'required|integer|exists:models,id',
+ 'status_id' => 'required|integer|exists:status_labels,id',
+ 'company_id' => 'integer|nullable',
+ 'warranty_months' => 'numeric|nullable',
+ 'physical' => 'numeric|max:1|nullable',
+ 'checkout_date' => 'date|max:10|min:10|nullable',
+ 'checkin_date' => 'date|max:10|min:10|nullable',
+ 'supplier_id' => 'numeric|nullable',
+ 'asset_tag' => 'required|min:1|max:255|unique_undeleted',
+ 'status' => 'integer',
+ 'purchase_cost' => 'numeric|nullable',
];
/**
diff --git a/tests/api/ApiAssetsCest.php b/tests/api/ApiAssetsCest.php
index e51ed7ad72..47233c427a 100644
--- a/tests/api/ApiAssetsCest.php
+++ b/tests/api/ApiAssetsCest.php
@@ -121,7 +121,7 @@ class ApiAssetsCest
// setup
$data = [
- 'asset_tag' => $temp_asset->tag,
+ 'asset_tag' => $temp_asset->asset_tag,
'assigned_to' => $temp_asset->assigned_to,
'company_id' => $temp_asset->company->id,
'image' => $temp_asset->image,
@@ -148,14 +148,14 @@ class ApiAssetsCest
{
$I->wantTo('Update an asset with PATCH');
- // create and store an asset
+ // create
$asset = factory(\App\Models\Asset::class, 'asset')->create();
$I->assertInstanceOf(\App\Models\Asset::class, $asset);
- // create a temporary asset to grab new data
$temp_asset = factory(\App\Models\Asset::class, 'asset')->make();
+
$data = [
- 'asset_tag' => $temp_asset->tag,
+ 'asset_tag' => $temp_asset->asset_tag,
'assigned_to' => $temp_asset->assigned_to,
'company_id' => $temp_asset->company->id,
'image' => $temp_asset->image,
@@ -171,7 +171,6 @@ class ApiAssetsCest
'warranty_months' => $temp_asset->warranty_months,
];
- // the asset name should be different
$I->assertNotEquals($asset->name, $data['name']);
// update
@@ -182,24 +181,19 @@ class ApiAssetsCest
$response = json_decode($I->grabResponse());
$I->assertEquals('success', $response->status);
$I->assertEquals(trans('admin/hardware/message.update.success'), $response->messages);
-
- $I->assertEquals($asset->id, $response->payload->id);
- $I->assertEquals($data['name'], $response->payload->name);
- $I->assertEquals($data['asset_tag'], $response->payload->asset_tag);
- $I->assertEquals($data['model_id'], $response->payload->model_id);
-
- // dd($response, $asset->getAttributes());
+ $I->assertEquals($asset->id, $response->payload->id); // asset id does not change
+ $I->assertEquals($temp_asset->asset_tag, $response->payload->asset_tag); // asset tag updated
+ $I->assertEquals($temp_asset->name, $response->payload->name); // asset name updated
+ $I->assertEquals($temp_asset->rtd_location_id, $response->payload->rtd_location_id); // asset rtd_location_id updated
// verify
- // $scenario->incomplete('[BadMethodCallException] Call to undefined method Illuminate\Database\Query\Builder::detail() 🤔');
$I->sendGET('/hardware/' . $asset->id);
-
$I->seeResponseIsJson();
$I->seeResponseCodeIs(200);
$I->seeResponseContainsJson([
'id' => (int) $asset->id,
- 'name' => e($temp_asset->name), // TODO: name should change
- 'asset_tag' => e($temp_asset->asset_tag), // TODO: asset_tag should change
+ 'name' => e($temp_asset->name),
+ 'asset_tag' => e($temp_asset->asset_tag),
'serial' => e($temp_asset->serial),
'model' => ($temp_asset->model) ? [
'id' => (int) $temp_asset->model->id,
@@ -220,17 +214,17 @@ class ApiAssetsCest
] : null,
'notes' => e($temp_asset->notes),
'order_number' => e($asset->order_number),
- 'company' => ($temp_asset->company) ? [
+ 'company' => ($asset->company) ? [
'id' => (int) $temp_asset->company->id,
'name'=> e($temp_asset->company->name)
] : null,
- 'location' => ($asset->assetLoc) ? [
- 'id' => (int) $asset->assetLoc->id,
- 'name'=> e($asset->assetLoc->name)
+ 'location' => ($temp_asset->assetLoc) ? [
+ 'id' => (int) $temp_asset->assetLoc->id,
+ 'name'=> e($temp_asset->assetLoc->name)
] : null,
- 'rtd_location' => ($asset->defaultLoc) ? [
- 'id' => (int) $asset->defaultLoc->id,
- 'name'=> e($asset->defaultLoc->name)
+ 'rtd_location' => ($temp_asset->defaultLoc) ? [
+ 'id' => (int) $temp_asset->defaultLoc->id,
+ 'name'=> e($temp_asset->defaultLoc->name)
] : null,
'image' => ($asset->getImageUrl()) ? $asset->getImageUrl() : null,
'assigned_to' => ($temp_asset->assigneduser) ? [
@@ -284,11 +278,29 @@ class ApiAssetsCest
$asset = factory(\App\Models\Asset::class, 'asset')->create();
$I->assertInstanceOf(\App\Models\Asset::class, $asset);
- $company = \App\Models\Company::inRandomOrder()->first();
+ $temp_asset_tag = $this->faker->uuid;
+ $temp_asset = factory(\App\Models\Asset::class, 'asset')->make([
+ 'asset_tag' => $temp_asset_tag,
+ ]);
+
+ $I->assertNotNull($temp_asset->asset_tag);
+ $I->assertEquals($temp_asset_tag, $temp_asset->asset_tag);
$data = [
- 'name' => $this->faker->sentence(3),
- 'company_id' => $company->id,
+ 'asset_tag' => $temp_asset->asset_tag,
+ 'assigned_to' => $temp_asset->assigned_to,
+ 'company_id' => $temp_asset->company->id,
+ 'image' => $temp_asset->image,
+ 'model_id' => $temp_asset->model_id,
+ 'name' => $temp_asset->name,
+ 'notes' => $temp_asset->notes,
+ 'purchase_cost' => $temp_asset->purchase_cost,
+ 'purchase_date' => $temp_asset->purchase_date->format('Y-m-d'),
+ 'rtd_location_id' => $temp_asset->rtd_location_id,
+ 'serial' => $temp_asset->serial,
+ 'status_id' => $temp_asset->status_id,
+ 'supplier_id' => $temp_asset->supplier_id,
+ 'warranty_months' => $temp_asset->warranty_months,
];
$I->assertNotEquals($asset->name, $data['name']);
@@ -301,7 +313,10 @@ class ApiAssetsCest
$response = json_decode($I->grabResponse());
$I->assertEquals('success', $response->status);
$I->assertEquals(trans('admin/hardware/message.update.success'), $response->messages);
- $I->assertEquals($asset->id, $response->payload->id);
+ $I->assertEquals($asset->id, $response->payload->id); // asset id does not change
+ $I->assertEquals($temp_asset->asset_tag, $response->payload->asset_tag); // asset tag updated
+ $I->assertEquals($temp_asset->name, $response->payload->name); // asset name updated
+ $I->assertEquals($temp_asset->rtd_location_id, $response->payload->rtd_location_id); // asset rtd_location_id updated
// verify
$I->sendGET('/hardware/' . $asset->id);
@@ -309,46 +324,46 @@ class ApiAssetsCest
$I->seeResponseCodeIs(200);
$I->seeResponseContainsJson([
'id' => (int) $asset->id,
- 'name' => e($data['name']),
- 'asset_tag' => e($asset->asset_tag),
- 'serial' => e($asset->serial),
- 'model' => ($asset->model) ? [
- 'id' => (int) $asset->model->id,
- 'name'=> e($asset->model->name)
+ 'name' => e($temp_asset->name),
+ 'asset_tag' => e($temp_asset->asset_tag),
+ 'serial' => e($temp_asset->serial),
+ 'model' => ($temp_asset->model) ? [
+ 'id' => (int) $temp_asset->model->id,
+ 'name'=> e($temp_asset->model->name)
] : null,
- 'model_number' => ($asset->model) ? e($asset->model->model_number) : null,
- 'status_label' => ($asset->assetstatus) ? [
- 'id' => (int) $asset->assetstatus->id,
- 'name'=> e($asset->assetstatus->name)
+ 'model_number' => ($temp_asset->model) ? e($temp_asset->model->model_number) : null,
+ 'status_label' => ($temp_asset->assetstatus) ? [
+ 'id' => (int) $temp_asset->assetstatus->id,
+ 'name'=> e($temp_asset->assetstatus->name)
] : null,
- 'category' => ($asset->model->category) ? [
- 'id' => (int) $asset->model->category->id,
- 'name'=> e($asset->model->category->name)
+ 'category' => ($temp_asset->model->category) ? [
+ 'id' => (int) $temp_asset->model->category->id,
+ 'name'=> e($temp_asset->model->category->name)
] : null,
- 'manufacturer' => ($asset->model->manufacturer) ? [
- 'id' => (int) $asset->model->manufacturer->id,
- 'name'=> e($asset->model->manufacturer->name)
+ 'manufacturer' => ($temp_asset->model->manufacturer) ? [
+ 'id' => (int) $temp_asset->model->manufacturer->id,
+ 'name'=> e($temp_asset->model->manufacturer->name)
] : null,
- 'notes' => e($asset->notes),
+ 'notes' => e($temp_asset->notes),
'order_number' => e($asset->order_number),
'company' => ($asset->company) ? [
- 'id' => (int) $data['company_id'],
- 'name'=> e($company->name)
+ 'id' => (int) $temp_asset->company->id,
+ 'name'=> e($temp_asset->company->name)
] : null,
- 'location' => ($asset->assetLoc) ? [
- 'id' => (int) $asset->assetLoc->id,
- 'name'=> e($asset->assetLoc->name)
+ 'location' => ($temp_asset->assetLoc) ? [
+ 'id' => (int) $temp_asset->assetLoc->id,
+ 'name'=> e($temp_asset->assetLoc->name)
] : null,
- 'rtd_location' => ($asset->defaultLoc) ? [
- 'id' => (int) $asset->defaultLoc->id,
- 'name'=> e($asset->defaultLoc->name)
+ 'rtd_location' => ($temp_asset->defaultLoc) ? [
+ 'id' => (int) $temp_asset->defaultLoc->id,
+ 'name'=> e($temp_asset->defaultLoc->name)
] : null,
'image' => ($asset->getImageUrl()) ? $asset->getImageUrl() : null,
- 'assigned_to' => ($asset->assigneduser) ? [
- 'id' => (int) $asset->assigneduser->id,
- 'name' => e($asset->assigneduser->getFullNameAttribute()),
- 'first_name'=> e($asset->assigneduser->first_name),
- 'last_name'=> e($asset->assigneduser->last_name)
+ 'assigned_to' => ($temp_asset->assigneduser) ? [
+ 'id' => (int) $temp_asset->assigneduser->id,
+ 'name' => e($temp_asset->assigneduser->getFullNameAttribute()),
+ 'first_name'=> e($temp_asset->assigneduser->first_name),
+ 'last_name'=> e($temp_asset->assigneduser->last_name)
] : null,
'warranty' => ($asset->warranty_months > 0) ? e($asset->warranty_months . ' ' . trans('admin/hardware/form.months')) : null,
'warranty_expires' => ($asset->warranty_months > 0) ? [
@@ -376,7 +391,7 @@ class ApiAssetsCest
// 'formatted' => $asset->created_at->format('Y-m-d'),
// ] : null,
// 'purchase_cost' => (float) $asset->purchase_cost,
- 'user_can_checkout' => (bool) $asset->availableForCheckout(),
+ 'user_can_checkout' => (bool) $temp_asset->availableForCheckout(),
'available_actions' => [
'checkout' => (bool) Gate::allows('checkout', Asset::class),
'checkin' => (bool) Gate::allows('checkin', Asset::class),