A free open source IT asset/license management system
scaffold test cases around sharing templates
akemidxdbd9a844dc3e
diff --git a/app/Http/Controllers/ReportTemplatesController.php b/app/Http/Controllers/ReportTemplatesController.phpindex 4709f398d6..b2da839208 100644--- a/app/Http/Controllers/ReportTemplatesController.php+++ b/app/Http/Controllers/ReportTemplatesController.php@@ -46,6 +46,12 @@ class ReportTemplatesController extends Controller{$this->authorize('reports.view');+ if ($reportTemplate->created_by != auth()->id()){+ return redirect()+ ->route('report-templates.show', $reportTemplate)+ ->withError(trans('general.report_not_editable'));+ }+return view('reports/custom', ['customfields' => CustomField::get(),'template' => $reportTemplate,@@ -56,6 +62,12 @@ class ReportTemplatesController extends Controller{$this->authorize('reports.view');+ if ($reportTemplate->created_by != auth()->id()){+ return redirect()+ ->route('report-templates.show', $reportTemplate)+ ->withError(trans('general.report_not_editable'));+ }+$properties = ['name' => $request->input('name'),'options' => $request->except(['_token', 'name','share_report_template']),diff --git a/app/Http/Requests/UpdateSharedTemplateRequest.php b/app/Http/Requests/UpdateSharedTemplateRequest.phpindex ada9ca3c92..ac9891f514 100644--- a/app/Http/Requests/UpdateSharedTemplateRequest.php+++ b/app/Http/Requests/UpdateSharedTemplateRequest.php@@ -29,10 +29,10 @@ class UpdateSharedTemplateRequest extends CustomAssetReportRequest$rules = array_merge(parent::rules(),(new ReportTemplate)->getRules(),- [- if ($this->created_by == auth()->id()) {- } //so how do we apply this rule. is it allow or restrict?- ],+// [+// if ($this->created_by == auth()->id()) {+// } //so how do we apply this rule. is it allow or restrict?+// ],);return $rules;}diff --git a/app/Models/ReportTemplate.php b/app/Models/ReportTemplate.phpindex 9d388d2cdb..0948450eb6 100644--- a/app/Models/ReportTemplate.php+++ b/app/Models/ReportTemplate.php@@ -17,6 +17,8 @@ class ReportTemplate extends Modeluse UniqueUndeletedTrait;use ValidatingTrait;+ protected $table = 'report_templates';+protected $casts = ['options' => 'array',];diff --git a/database/factories/ReportTemplateFactory.php b/database/factories/ReportTemplateFactory.phpindex 7044af6342..bc5d24c724 100644--- a/database/factories/ReportTemplateFactory.php+++ b/database/factories/ReportTemplateFactory.php@@ -20,6 +20,14 @@ class ReportTemplateFactory extends Factory'id' => '1',],'created_by' => User::factory(),+ 'share_report_template' => 0,];}++ public function shared()+ {+ return $this->state(function () {+ return['share_report_template' => 1];+ });+ }}diff --git a/resources/lang/en-US/general.php b/resources/lang/en-US/general.phpindex 9bc1ead4d4..73288e043d 100644--- a/resources/lang/en-US/general.php+++ b/resources/lang/en-US/general.php@@ -691,6 +691,7 @@ return ['select_all_none' => 'Select/Unselect All','generic_model_not_found' => 'That :model was not found or you do not have permission to access it',+ 'report_not_editable' => 'You do not have permission to edit this report template','deleted_models' => 'Deleted Asset Models','deleted_users' => 'Deleted Users','cost_each' => ':amount each',diff --git a/tests/Feature/ReportTemplates/DeleteReportTemplateTest.php b/tests/Feature/ReportTemplates/DeleteReportTemplateTest.phpindex cebc8156ad..e9b6632820 100644--- a/tests/Feature/ReportTemplates/DeleteReportTemplateTest.php+++ b/tests/Feature/ReportTemplates/DeleteReportTemplateTest.php@@ -33,6 +33,11 @@ class DeleteReportTemplateTest extends TestCase implements TestsPermissionsRequi$this->assertModelExists($reportTemplate);}+ public function testCannotDeleteAnotherUsersSharedReportTemplate()+ {+ $this->markTestIncomplete();+ }+public function testCanDeleteAReportTemplate(){$user = User::factory()->canViewReports()->create();diff --git a/tests/Feature/ReportTemplates/EditReportTemplateTest.php b/tests/Feature/ReportTemplates/EditReportTemplateTest.phpindex d359baddde..576d0e5ffa 100644--- a/tests/Feature/ReportTemplates/EditReportTemplateTest.php+++ b/tests/Feature/ReportTemplates/EditReportTemplateTest.php@@ -28,6 +28,16 @@ class EditReportTemplateTest extends TestCase implements TestsPermissionsRequire->assertStatus(302);}+ public function testCannotLoadEditPageForAnotherUsersSharedReportTemplate()+ {+ $user = User::factory()->canViewReports()->create();+ $reportTemplate = ReportTemplate::factory()->shared()->create();++ $this->actingAs($user)+ ->get(route('report-templates.edit', $reportTemplate))+ ->assertStatus(302);+ }+public function testCanLoadEditReportTemplatePage(){$user = User::factory()->canViewReports()->create();diff --git a/tests/Feature/ReportTemplates/ShowReportTemplateTest.php b/tests/Feature/ReportTemplates/ShowReportTemplateTest.phpindex 748df288b5..0e6e93b0cd 100644--- a/tests/Feature/ReportTemplates/ShowReportTemplateTest.php+++ b/tests/Feature/ReportTemplates/ShowReportTemplateTest.php@@ -32,7 +32,7 @@ class ShowReportTemplateTest extends TestCase implements TestsPermissionsRequire}]);}- public function testCannotLoadAnotherUsersSavedReportTemplate()+ public function testCannotLoadAnotherUsersSavedReportTemplateIfNotShared(){$reportTemplate = ReportTemplate::factory()->create();@@ -40,4 +40,9 @@ class ShowReportTemplateTest extends TestCase implements TestsPermissionsRequire->get(route('report-templates.show', $reportTemplate))->assertStatus(302);}++ public function testCanLoadAnotherUsersSavedReportTemplateIfShared()+ {+ $this->markTestIncomplete();+ }}diff --git a/tests/Feature/ReportTemplates/UpdateReportTemplateTest.php b/tests/Feature/ReportTemplates/UpdateReportTemplateTest.phpindex 09c168b27e..5f07235494 100644--- a/tests/Feature/ReportTemplates/UpdateReportTemplateTest.php+++ b/tests/Feature/ReportTemplates/UpdateReportTemplateTest.php@@ -20,9 +20,31 @@ class UpdateReportTemplateTest extends TestCase implements TestsPermissionsRequipublic function testCannotUpdateAnotherUsersReportTemplate(){+ $reportTemplate = ReportTemplate::factory()->create(['name' => 'Original']);+$this->actingAs(User::factory()->canViewReports()->create())- ->post(route('report-templates.update', ReportTemplate::factory()->create()))+ ->post(route('report-templates.update', $reportTemplate), [+ 'name' => 'Changed',+ 'options' => $reportTemplate->options,+ ])->assertStatus(302);++ $this->assertEquals('Original', $reportTemplate->fresh()->name);+ }++ public function testCannotUpdateAnotherUsersSharedReportTemplate()+ {+ $reportTemplate = ReportTemplate::factory()->shared()->create(['name' => 'Original']);++ $this->actingAs(User::factory()->canViewReports()->create())+ ->post(route('report-templates.update', $reportTemplate), [+ 'name' => 'Changed',+ 'options' => $reportTemplate->options,+ ])+ ->assertStatus(302)+ ->assertSessionHas('error', trans('general.report_not_editable'));++ $this->assertEquals('Original', $reportTemplate->fresh()->name);}public function testUpdatingReportTemplateRequiresValidFields()