snipe-it/tests/api/ApiComponentsAssetsCest.php
Andrea Bergamasco c0c02eebd2 REST API - Refactoring of routes file, more tests added (#3345)
* Toggles the disabled state of auto_increment_prefix

To insert a prefix you had to toggle the checkbox, save the settings and reload. With this script it is immediate. Fixes #1390

* Delete asset image: made checkbox more visible

Related to #3153

* Added personal-access-token component

* Created basic API testing configuration

* First version of /components endpoind cest

* On-the-fly bearer token generation

* Completed testing of PATCH and PUT methods

* Added /components/{id}/assets route with tests

* Updated route and dataTable in view

* Completed test assertion

* Added links to assets in ComponentsAssets view

* Linked Company in AssetView page

* Fixed purchase_cost format expectation in ApiComponentsCest

* Refactored api routes file

Sorted all prefixes in alphabetical order, removed duplicate routes. For every prefix I placed first Route::resource and then any additional route in a Route::group. Expanded arrays for readability and consistency. Removed useless calls as create and edit everywhere.

* Refactored and added one more test to ApiComponentsAssetsCest

* Marked one test as incomplete, 404 response should return json

* Fixed value expectation

* Refactored getToken()

* Added API debugging routes

* Added more information to ValidationException reporting

Now the payload contains the validation errors for each invalid
attribute.

* /apitests: refactored expectations in component assertions

* Created ApiAssetsCest

* /apitests: Cleanup in Exceptions/Handler

* Reverted change to use

* Marked two tests as incomplete, looking for solutions
2017-02-23 16:32:35 -08:00

81 lines
2.8 KiB
PHP

<?php
class ApiComponentsAssetsCest
{
protected $faker;
protected $user;
public function _before(ApiTester $I)
{
$this->faker = \Faker\Factory::create();
$this->user = \App\Models\User::find(1);
$I->amBearerAuthenticated($I->getToken($this->user));
}
/** @test */
public function indexComponentsAssets(ApiTester $I)
{
$I->wantTo('Get a list of assets related to a component');
// generate component
$component = factory(\App\Models\Component::class, 'component')
->create(['user_id' => $this->user->id, 'qty' => 20]);
// generate assets and associate component
$assets = factory(\App\Models\Asset::class, 'asset', 2)
->create(['user_id' => $this->user->id])
->each(function ($asset) use ($component) {
$component->assets()->attach($component->id, [
'component_id' => $component->id,
'user_id' => $this->user->id,
'created_at' => date('Y-m-d H:i:s'),
'assigned_qty' => 2,
'asset_id' => $asset->id
]);
});
// verify
$I->sendGET('/components/' . $component->id . '/assets/');
$I->seeResponseIsJson();
$I->seeResponseCodeIs(200);
$response = json_decode($I->grabResponse());
$I->assertEquals(2, $response->total);
$I->assertInstanceOf(\Illuminate\Database\Eloquent\Collection::class, $assets);
$I->seeResponseContainsJson(['rows' => [
0 => [
'name' => $assets[0]->name,
'id' => $assets[0]->id,
'created_at' => $assets[0]->created_at->format('Y-m-d'),
],
1 => [
'name' => $assets[1]->name,
'id' => $assets[1]->id,
'created_at' => $assets[1]->created_at->format('Y-m-d'),
],
]
]);
}
/** @test */
public function expectEmptyResponseWithoutAssociatedAssets(ApiTester $I, $scenario)
{
$I->wantTo('See an empty response when there are no associated assets to a component');
$component = factory(\App\Models\Component::class, 'component')
->create(['user_id' => $this->user->id, 'qty' => 20]);
$I->sendGET('/components/' . $component->id . '/assets');
$I->seeResponseCodeIs(200);
$I->seeResponseIsJson();
$response = json_decode($I->grabResponse());
$I->assertEquals(0, $response->total);
$I->assertEquals([], $response->rows);
$I->seeResponseContainsJson(['total' => 0, 'rows' => []]);
}
}