snipe-it/tests/api/ApiLocationsCest.php
Daniel Meltzer 7de8f71f58 Api tests (#5096)
* Use the formated date helper to clean up verifications.

* Add Checkin/Checkout api tests.

* Accessories api test

* Add Companies API Test.

* Return ModelNotFound as a 404.

* Cleanups/simplficiations/updates.

* Locations api test.

* currency and image should be fillable on location.

* Update components api test.

* Use findOrFail so we return a 404 instead of a 200.  Matches other item types.

* order_number should be fillable in component.

* Add updated_at and permissions to information returned from api for a user.

* Add users test and flesh out factory and fillable fields.

* Add test for assets method

* API status label test.

* Disable php7.2 for now on travis until the count(null) issues are remedied

* Add serial to update.

* API model not found should return a 200
2018-02-24 19:01:34 -08:00

146 lines
4.9 KiB
PHP

<?php
use App\Helpers\Helper;
use App\Http\Transformers\LocationsTransformer;
use App\Models\Location;
use App\Models\Setting;
use Illuminate\Support\Facades\Auth;
class ApiLocationsCest
{
protected $user;
protected $timeFormat;
public function _before(ApiTester $I)
{
$this->user = \App\Models\User::find(1);
$this->timeFormat = Setting::getSettings()->date_display_format .' '. Setting::getSettings()->time_display_format;
$this->dateFormat = Setting::getSettings()->date_display_format;
$I->haveHttpHeader('Accept', 'application/json');
$I->amBearerAuthenticated($I->getToken($this->user));
}
/** @test */
public function indexLocations(ApiTester $I)
{
$I->wantTo('Get a list of locations');
// call
$I->sendGET('/locations?limit=1');
$I->seeResponseIsJson();
$I->seeResponseCodeIs(200);
$response = json_decode($I->grabResponse(), true);
// sample verify
$location = App\Models\Location::orderByDesc('created_at')
->withCount('assignedAssets', 'assets', 'users')
->take(1)->get()->shuffle()->first();
$I->seeResponseContainsJson((new LocationsTransformer)->transformLocation($location));
}
/** @test */
public function createLocation(ApiTester $I, $scenario)
{
$I->wantTo('Create a new location');
$temp_location = factory(\App\Models\Location::class)->make([
'name' => "Test Location Tag",
]);
// setup
$data = [
'name' => $temp_location->name,
];
// create
$I->sendPOST('/locations', $data);
$I->seeResponseIsJson();
$I->seeResponseCodeIs(200);
}
// Put is routed to the same method in the controller
// DO we actually need to test both?
/** @test */
public function updateLocationWithPatch(ApiTester $I, $scenario)
{
$I->wantTo('Update an location with PATCH');
// create
$location = factory(\App\Models\Location::class)->create([
'name' => 'Original Location Name',
]);
$I->assertInstanceOf(\App\Models\Location::class, $location);
$temp_location = factory(\App\Models\Location::class)->make([
'name' => "updated location name",
]);
$data = [
'name' => $temp_location->name,
'image' => $temp_location->image,
'address' => $temp_location->address,
'address2' => $temp_location->address2,
'city' => $temp_location->city,
'state' => $temp_location->state,
'country' => $temp_location->country,
'zip' => $temp_location->zip,
'parent_id' => $temp_location->parent_id,
'parent_id' => $temp_location->parent_id,
'manager_id' => $temp_location->manager_id,
'currency' => $temp_location->currency
];
$I->assertNotEquals($location->name, $data['name']);
// update
$I->sendPATCH('/locations/' . $location->id, $data);
$I->seeResponseIsJson();
$I->seeResponseCodeIs(200);
$response = json_decode($I->grabResponse());
$I->assertEquals('success', $response->status);
$I->assertEquals(trans('admin/locations/message.update.success'), $response->messages);
$I->assertEquals($location->id, $response->payload->id); // location id does not change
$I->assertEquals($temp_location->name, $response->payload->name); // location name updated
// Some necessary manual copying
$temp_location->created_at = Carbon::parse($response->payload->created_at->datetime);
$temp_location->updated_at = Carbon::parse($response->payload->updated_at->datetime);
$temp_location->id = $location->id;
// verify
$I->sendGET('/locations/' . $location->id);
$I->seeResponseIsJson();
$I->seeResponseCodeIs(200);
$I->seeResponseContainsJson((new LocationsTransformer)->transformLocation($temp_location));
// $I->seeResponseContainsJson($this->generateJsonResponse($temp_location, $location));
}
/** @test */
public function deleteLocationTest(ApiTester $I, $scenario)
{
$I->wantTo('Delete an location');
// create
$location = factory(\App\Models\Location::class)->create([
'name' => "Soon to be deleted"
]);
$I->assertInstanceOf(\App\Models\Location::class, $location);
// delete
$I->sendDELETE('/locations/' . $location->id);
$I->seeResponseIsJson();
$I->seeResponseCodeIs(200);
$response = json_decode($I->grabResponse());
$I->assertEquals('success', $response->status);
$I->assertEquals(trans('admin/locations/message.delete.success'), $response->messages);
// verify, expect a 200
$I->sendGET('/locations/' . $location->id);
$I->seeResponseCodeIs(200);
$I->seeResponseIsJson();
}
}