snipe-it/tests/api/ApiCompaniesCest.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

134 lines
4.1 KiB
PHP

<?php
use App\Helpers\Helper;
use App\Http\Transformers\CompaniesTransformer;
use App\Models\Company;
use App\Models\Setting;
use Illuminate\Support\Facades\Auth;
class ApiCompaniesCest
{
protected $user;
protected $timeFormat;
public function _before(ApiTester $I)
{
$this->user = \App\Models\User::find(1);
$I->haveHttpHeader('Accept', 'application/json');
$I->amBearerAuthenticated($I->getToken($this->user));
}
/** @test */
public function indexCompanys(ApiTester $I)
{
$I->wantTo('Get a list of companies');
// call
$I->sendGET('/companies?order_by=id&limit=10');
$I->seeResponseIsJson();
$I->seeResponseCodeIs(200);
$response = json_decode($I->grabResponse(), true);
// dd($response);
// sample verify
$company = App\Models\Company::withCount('assets','licenses','accessories','consumables','components','users')
->orderByDesc('created_at')->take(10)->get()->shuffle()->first();
$I->seeResponseContainsJson((new CompaniesTransformer)->transformCompany($company));
}
/** @test */
public function createCompany(ApiTester $I, $scenario)
{
$I->wantTo('Create a new company');
$temp_company = factory(\App\Models\Company::class)->make([
'name' => "Test Company Tag",
]);
// setup
$data = [
'name' => $temp_company->name,
];
// create
$I->sendPOST('/companies', $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 updateCompanyWithPatch(ApiTester $I, $scenario)
{
$I->wantTo('Update an company with PATCH');
// create
$company = factory(\App\Models\Company::class)->create([
'name' => 'Original Company Name',
]);
$I->assertInstanceOf(\App\Models\Company::class, $company);
$temp_company = factory(\App\Models\Company::class)->make([
'name' => "updated company name",
]);
$data = [
'name' => $temp_company->name,
];
$I->assertNotEquals($company->name, $data['name']);
// update
$I->sendPATCH('/companies/' . $company->id, $data);
$I->seeResponseIsJson();
$I->seeResponseCodeIs(200);
$response = json_decode($I->grabResponse());
$I->assertEquals('success', $response->status);
$I->assertEquals(trans('admin/companies/message.update.success'), $response->messages);
$I->assertEquals($company->id, $response->payload->id); // company id does not change
$I->assertEquals($temp_company->name, $response->payload->name); // company name updated
// Some manual copying to compare against
$temp_company->created_at = Carbon::parse($response->payload->created_at->datetime);
$temp_company->updated_at = Carbon::parse($response->payload->updated_at->datetime);
$temp_company->id = $company->id;
// verify
$I->sendGET('/companies/' . $company->id);
$I->seeResponseIsJson();
$I->seeResponseCodeIs(200);
$I->seeResponseContainsJson((new CompaniesTransformer)->transformCompany($temp_company));
}
/** @test */
public function deleteCompanyTest(ApiTester $I, $scenario)
{
$I->wantTo('Delete an company');
// create
$company = factory(\App\Models\Company::class)->create([
'name' => "Soon to be deleted"
]);
$I->assertInstanceOf(\App\Models\Company::class, $company);
// delete
$I->sendDELETE('/companies/' . $company->id);
$I->seeResponseIsJson();
$I->seeResponseCodeIs(200);
$response = json_decode($I->grabResponse());
$I->assertEquals('success', $response->status);
$I->assertEquals(trans('admin/companies/message.delete.success'), $response->messages);
// verify, expect a 200
$I->sendGET('/companies/' . $company->id);
$I->seeResponseCodeIs(200);
$I->seeResponseIsJson();
}
}