forked from laravel/framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request laravel#57 from eurides-eu/feature/add-organizatio…
…n-endpoints-tests Add organization & organization role endpoints tests
- Loading branch information
Showing
16 changed files
with
580 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ $finder = Finder::create()->in([ | |
'app', | ||
'config', | ||
'database', | ||
'tests', | ||
]); | ||
|
||
return Config::create() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
use App\Models\Organization; | ||
use App\Models\OrganizationBudget; | ||
use Faker\Generator as Faker; | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
| Model Factories | ||
|-------------------------------------------------------------------------- | ||
| | ||
| This directory should contain each of the model factory definitions for | ||
| your application. Factories provide a convenient way to generate new | ||
| model instances for testing / seeding your application's database. | ||
| | ||
*/ | ||
|
||
$factory->define(OrganizationBudget::class, function (Faker $faker) { | ||
return [ | ||
'name' => 'Default Budget', | ||
'reference' => str_random(10), | ||
'max_amount' => $faker->randomFloat(2, 0, 12000), | ||
'organization_id' => function () { | ||
return factory(Organization::class)->create()->id; | ||
}, | ||
]; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
use App\Models\Organization; | ||
use Faker\Generator as Faker; | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
| Model Factories | ||
|-------------------------------------------------------------------------- | ||
| | ||
| This directory should contain each of the model factory definitions for | ||
| your application. Factories provide a convenient way to generate new | ||
| model instances for testing / seeding your application's database. | ||
| | ||
*/ | ||
|
||
$factory->define(Organization::class, function (Faker $faker) { | ||
return [ | ||
'name' => $faker->company, | ||
'reference_code' => str_random(10), | ||
]; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
use Faker\Generator as Faker; | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
| Model Factories | ||
|-------------------------------------------------------------------------- | ||
| | ||
| This directory should contain each of the model factory definitions for | ||
| your application. Factories provide a convenient way to generate new | ||
| model instances for testing / seeding your application's database. | ||
| | ||
*/ | ||
|
||
$factory->define(App\Models\OrganizationRole::class, function (Faker $faker) { | ||
return [ | ||
'name' => $faker->jobTitle, | ||
]; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
use Faker\Generator as Faker; | ||
use Laravel\Passport\Client; | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
| Model Factories | ||
|-------------------------------------------------------------------------- | ||
| | ||
| This directory should contain each of the model factory definitions for | ||
| your application. Factories provide a convenient way to generate new | ||
| model instances for testing / seeding your application's database. | ||
| | ||
*/ | ||
|
||
$factory->define(Client::class, function (Faker $faker) { | ||
return [ | ||
'name' => 'Eurides Password Grant Client', | ||
'secret' => str_random(40), | ||
'role' => 'operator', | ||
'password_client' => 1, | ||
'personal_access_client' => 0, | ||
'revoked' => 0, | ||
'redirect' => url('callback'), | ||
]; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
<?php | ||
|
||
namespace Tests\Feature; | ||
|
||
use App\Models\Organization; | ||
use App\Models\OrganizationBudget; | ||
use Faker\Factory as Faker; | ||
use Tests\TestCase; | ||
|
||
class OrganizationsTest extends TestCase | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
protected $organizationJsonStructure = [ | ||
'data' => [ | ||
'id', | ||
'name', | ||
'reference', | ||
'services', | ||
'defaultBudget' => [ | ||
'id', | ||
'name', | ||
'reference', | ||
'maxAmount', | ||
'approverEmail', | ||
'autoApproveAmountLimit', | ||
'autoApproveMonthlyLimit', | ||
'autoApproveAfter', | ||
], | ||
], | ||
]; | ||
|
||
public function testOrganizationsAreCreatedCorrectly() | ||
{ | ||
$this->setupOperatorAccessToken(); | ||
|
||
$faker = Faker::create(); | ||
|
||
$data = [ | ||
'name' => $faker->company, | ||
'reference' => (string) $faker->randomNumber(5), | ||
'services' => config('eurides.services'), | ||
]; | ||
|
||
$response = $this->post('api/organizations', $data, $this->headers); | ||
|
||
$content = json_decode($response->getContent()); | ||
|
||
$response->assertStatus(201) | ||
->assertJsonStructure($this->organizationJsonStructure); | ||
|
||
$this->assertEquals($data['name'], $content->data->name); | ||
$this->assertEquals($data['reference'], $content->data->reference); | ||
$this->assertEquals(config('eurides.services'), $content->data->services); | ||
} | ||
|
||
public function testCreateRequiresName() | ||
{ | ||
$this->setupOperatorAccessToken(); | ||
|
||
$response = $this->post('api/organizations', [], $this->headers); | ||
|
||
$response->assertStatus(422) | ||
->assertJson([ | ||
'errors' => [ | ||
[ | ||
'name' => [ | ||
[ | ||
'code' => 'errors.required', | ||
'message' => trans('validation.required', ['attribute' => 'name']), | ||
], | ||
], | ||
], | ||
], | ||
]); | ||
} | ||
|
||
public function testNotFound() | ||
{ | ||
$this->setupOperatorAccessToken(); | ||
|
||
$response = $this->get('api/organizations/whatever', $this->headers); | ||
|
||
$response->assertStatus(404); | ||
} | ||
|
||
public function testOrganizationIsListedCorrectly() | ||
{ | ||
$this->setupOperatorAccessToken(); | ||
|
||
$organization = factory(Organization::class)->make(); | ||
|
||
$createResponse = $this->post('api/organizations', $organization->toArray(), $this->headers); | ||
|
||
$createResponseContent = json_decode($createResponse->getContent()); | ||
|
||
$response = $this->get("api/organizations/{$createResponseContent->data->id}", $this->headers); | ||
|
||
$response->assertStatus(200) | ||
->assertJsonStructure($this->organizationJsonStructure); | ||
} | ||
|
||
public function testOrganizationsAreUpdatedCorrectly() | ||
{ | ||
$this->setupOperatorAccessToken(); | ||
|
||
$organizationBudget = factory(OrganizationBudget::class)->create(); | ||
|
||
$payload = [ | ||
'name' => 'Company B', | ||
'reference' => 'BCD234', | ||
'services' => config('eurides.services'), | ||
'defaultBudgetId' => $organizationBudget->id, | ||
]; | ||
|
||
$response = $this->put("api/organizations/$organizationBudget->organization_id", $payload, $this->headers); | ||
|
||
$content = json_decode($response->getContent(), true); | ||
|
||
$response->assertSuccessful() | ||
->assertJsonStructure($this->organizationJsonStructure); | ||
|
||
$this->assertEquals($payload['name'], $content['data']['name']); | ||
$this->assertEquals($payload['reference'], $content['data']['reference']); | ||
$this->assertEquals($payload['services'], $content['data']['services']); | ||
$this->assertEquals($payload['defaultBudgetId'], $content['data']['defaultBudget']['id']); | ||
} | ||
|
||
public function testUpdateRequiresNameAndDefaultBudgetId() | ||
{ | ||
$this->setupOperatorAccessToken(); | ||
|
||
$organizationBudget = factory(OrganizationBudget::class)->create(); | ||
|
||
$response = $this->put("api/organizations/$organizationBudget->organization_id", [], $this->headers); | ||
|
||
$response->assertStatus(422) | ||
->assertJson([ | ||
'errors' => [ | ||
[ | ||
'name' => [ | ||
[ | ||
'code' => 'errors.required', | ||
'message' => trans('validation.required', ['attribute' => 'name']), | ||
], | ||
], | ||
], | ||
[ | ||
'defaultBudgetId' => [ | ||
[ | ||
'code' => 'errors.required', | ||
'message' => trans('validation.required', ['attribute' => 'default budget id']), | ||
], | ||
], | ||
], | ||
], | ||
]); | ||
} | ||
|
||
public function testOrganizationsAreDeletedCorrectly() | ||
{ | ||
$this->setupOperatorAccessToken(); | ||
|
||
$organization = factory(Organization::class)->create(); | ||
|
||
$response = $this->delete("api/organizations/$organization->id", [], $this->headers); | ||
|
||
$response->assertSuccessful(); | ||
|
||
$this->assertSoftDeleted((new Organization())->getTable(), ['id' => $organization->id]); | ||
} | ||
|
||
public function testDeleteNotFound() | ||
{ | ||
$this->setupOperatorAccessToken(); | ||
|
||
$response = $this->delete('api/organizations/notfound', [], $this->headers); | ||
|
||
$response->assertStatus(404); | ||
} | ||
} |
Oops, something went wrong.