Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 15 commits into from
Mar 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 28 additions & 17 deletions app/Http/Controllers/Api/AssetsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,23 +287,36 @@ public function update(Request $request, $id)
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')) : '';

Expand Down Expand Up @@ -335,8 +348,6 @@ public function update(Request $request, $id)
}
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);
}

Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Api/ComponentsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public function destroy($id)
$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')));
}

/**
Expand Down
24 changes: 12 additions & 12 deletions app/Models/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
];

/**
Expand Down
131 changes: 73 additions & 58 deletions tests/api/ApiAssetsCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public function createAsset(ApiTester $I, $scenario)

// 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,
Expand All @@ -148,14 +148,14 @@ public function updateAssetWithPatch(ApiTester $I, $scenario)
{
$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,
Expand All @@ -171,7 +171,6 @@ public function updateAssetWithPatch(ApiTester $I, $scenario)
'warranty_months' => $temp_asset->warranty_months,
];

// the asset name should be different
$I->assertNotEquals($asset->name, $data['name']);

// update
Expand All @@ -182,24 +181,19 @@ public function updateAssetWithPatch(ApiTester $I, $scenario)
$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,
Expand All @@ -220,17 +214,17 @@ public function updateAssetWithPatch(ApiTester $I, $scenario)
] : 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) ? [
Expand Down Expand Up @@ -284,11 +278,29 @@ public function updateAssetWithPut(ApiTester $I)
$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']);
Expand All @@ -301,54 +313,57 @@ public function updateAssetWithPut(ApiTester $I)
$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);
$I->seeResponseIsJson();
$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) ? [
Expand Down Expand Up @@ -376,7 +391,7 @@ public function updateAssetWithPut(ApiTester $I)
// '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),
Expand Down