Skip to content

Commit

Permalink
Added/updated tests
Browse files Browse the repository at this point in the history
Signed-off-by: snipe <[email protected]>
  • Loading branch information
snipe committed Feb 11, 2025
1 parent a9d6a5f commit 3de5f58
Show file tree
Hide file tree
Showing 19 changed files with 230 additions and 16 deletions.
2 changes: 2 additions & 0 deletions tests/Feature/Categories/Api/CreateCategoriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function testCanCreateCategoryWithValidCategoryType()
'name' => 'Test Category',
'eula_text' => 'Test EULA',
'category_type' => 'accessory',
'notes' => 'Test Note',
])
->assertOk()
->assertStatusMessageIs('success')
Expand All @@ -38,6 +39,7 @@ public function testCanCreateCategoryWithValidCategoryType()
$category = Category::find($response['payload']['id']);
$this->assertEquals('Test Category', $category->name);
$this->assertEquals('Test EULA', $category->eula_text);
$this->assertEquals('Test Note', $category->notes);
$this->assertEquals('accessory', $category->category_type);
}

Expand Down
4 changes: 4 additions & 0 deletions tests/Feature/Categories/Api/UpdateCategoriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function testCanUpdateCategoryViaPatchWithoutCategoryType()
->patchJson(route('api.categories.update', $category), [
'name' => 'Test Category',
'eula_text' => 'Test EULA',
'notes' => 'Test Note',
])
->assertOk()
->assertStatusMessageIs('success')
Expand All @@ -27,6 +28,7 @@ public function testCanUpdateCategoryViaPatchWithoutCategoryType()
$category->refresh();
$this->assertEquals('Test Category', $category->name, 'Name was not updated');
$this->assertEquals('Test EULA', $category->eula_text, 'EULA was not updated');
$this->assertEquals('Test Note', $category->notes, 'Note was not updated');

}

Expand All @@ -39,6 +41,7 @@ public function testCannotUpdateCategoryViaPatchWithCategoryType()
'name' => 'Test Category',
'eula_text' => 'Test EULA',
'category_type' => 'accessory',
'note' => 'Test Note',
])
->assertOk()
->assertStatusMessageIs('error')
Expand All @@ -48,6 +51,7 @@ public function testCannotUpdateCategoryViaPatchWithCategoryType()
$category->refresh();
$this->assertNotEquals('Test Category', $category->name, 'Name was not updated');
$this->assertNotEquals('Test EULA', $category->eula_text, 'EULA was not updated');
$this->assertNotEquals('Test Note', $category->notes, 'Note was not updated');
$this->assertNotEquals('accessory', $category->category_type, 'EULA was not updated');

}
Expand Down
7 changes: 4 additions & 3 deletions tests/Feature/Categories/Ui/CreateCategoriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ public function testUserCanCreateCategories()
$this->actingAs(User::factory()->superuser()->create())
->post(route('categories.store'), [
'name' => 'Test Category',
'category_type' => 'asset'
'category_type' => 'asset',
'notes' => 'Test Note',
])
->assertRedirect(route('categories.index'));

$this->assertTrue(Category::where('name', 'Test Category')->exists());
$this->assertTrue(Category::where('name', 'Test Category')->where('notes', 'Test Note')->exists());
}

public function testUserCannotCreateCategoriesWithInvalidType()
Expand All @@ -48,7 +49,7 @@ public function testUserCannotCreateCategoriesWithInvalidType()
->from(route('categories.create'))
->post(route('categories.store'), [
'name' => 'Test Category',
'category_type' => 'invalid'
'category_type' => 'invalid',
])
->assertRedirect(route('categories.create'));

Expand Down
11 changes: 7 additions & 4 deletions tests/Feature/Categories/Ui/UpdateCategoriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function testUserCanCreateCategories()
$this->actingAs(User::factory()->superuser()->create())
->post(route('categories.store'), [
'name' => 'Test Category',
'category_type' => 'asset'
'category_type' => 'asset',
])
->assertStatus(302)
->assertSessionHasNoErrors()
Expand All @@ -49,13 +49,14 @@ public function testUserCanEditAssetCategory()
$response = $this->actingAs(User::factory()->superuser()->create())
->put(route('categories.update', ['category' => $category]), [
'name' => 'Test Category Edited',
'notes' => 'Test Note Edited',
])
->assertStatus(302)
->assertSessionHasNoErrors()
->assertRedirect(route('categories.index'));

$this->followRedirects($response)->assertSee('Success');
$this->assertTrue(Category::where('name', 'Test Category Edited')->exists());
$this->assertTrue(Category::where('name', 'Test Category Edited')->where('notes', 'Test Note Edited')->exists());

}

Expand All @@ -69,13 +70,14 @@ public function testUserCanChangeCategoryTypeIfNoAssetsAssociated()
->put(route('categories.update', ['category' => $category]), [
'name' => 'Test Category Edited',
'category_type' => 'accessory',
'notes' => 'Test Note Edited',
])
->assertSessionHasNoErrors()
->assertStatus(302)
->assertRedirect(route('categories.index'));

$this->followRedirects($response)->assertSee('Success');
$this->assertTrue(Category::where('name', 'Test Category Edited')->exists());
$this->assertTrue(Category::where('name', 'Test Category Edited')->where('notes', 'Test Note Edited')->exists());

}

Expand All @@ -89,14 +91,15 @@ public function testUserCannotChangeCategoryTypeIfAssetsAreAssociated()
->put(route('categories.update', ['category' => $category]), [
'name' => 'Test Category Edited',
'category_type' => 'accessory',
'notes' => 'Test Note Edited',
])
->assertSessionHasErrors(['category_type'])
->assertInvalid(['category_type'])
->assertStatus(302)
->assertRedirect(route('categories.edit', ['category' => $category->id]));

$this->followRedirects($response)->assertSee(trans('general.error'));
$this->assertFalse(Category::where('name', 'Test Category Edited')->exists());
$this->assertFalse(Category::where('name', 'Test Category Edited')->where('notes', 'Test Note Edited')->exists());

}

Expand Down
19 changes: 19 additions & 0 deletions tests/Feature/Departments/Api/CreateDepartmentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,23 @@ public function testRequiresPermissionToCreateDepartment()
->assertForbidden();
}

public function testCanCreateDepartment()
{
$response = $this->actingAsForApi(User::factory()->superuser()->create())
->postJson(route('api.departments.store'), [
'name' => 'Test Department',
'notes' => 'Test Note',
])
->assertOk()
->assertStatusMessageIs('success')
->assertStatus(200)
->json();

$this->assertTrue(Department::where('name', 'Test Department')->exists());

$department = Department::find($response['payload']['id']);
$this->assertEquals('Test Department', $department->name);
$this->assertEquals('Test Note', $department->notes);
}

}
2 changes: 2 additions & 0 deletions tests/Feature/Departments/Api/UpdateDepartmentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function testCanUpdateDepartmentViaPatch()
$this->actingAsForApi(User::factory()->superuser()->create())
->patchJson(route('api.departments.update', $department), [
'name' => 'Test Department',
'notes' => 'Test Note',
])
->assertOk()
->assertStatusMessageIs('success')
Expand All @@ -33,6 +34,7 @@ public function testCanUpdateDepartmentViaPatch()

$department->refresh();
$this->assertEquals('Test Department', $department->name, 'Name was not updated');
$this->assertEquals('Test Note', $department->notes, 'Note was not updated');

}

Expand Down
5 changes: 3 additions & 2 deletions tests/Feature/Departments/Ui/CreateDepartmentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ public function testUserCanCreateDepartments()
$this->actingAs(User::factory()->superuser()->create())
->post(route('departments.store'), [
'name' => 'Test Department',
'company_id' => Company::factory()->create()->id
'company_id' => Company::factory()->create()->id,
'notes' => 'Test Note',
])
->assertRedirect(route('departments.index'));

$this->assertTrue(Department::where('name', 'Test Department')->exists());
$this->assertTrue(Department::where('name', 'Test Department')->where('notes', 'Test Note')->exists());
}


Expand Down
3 changes: 2 additions & 1 deletion tests/Feature/Departments/Ui/UpdateDepartmentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ public function testUserCanEditDepartments()
$response = $this->actingAs(User::factory()->superuser()->create())
->put(route('departments.update', ['department' => $department]), [
'name' => 'Test Department Edited',
'notes' => 'Test Note Edited',
])
->assertStatus(302)
->assertSessionHasNoErrors()
->assertRedirect(route('departments.index'));

$this->followRedirects($response)->assertSee('Success');
$this->assertTrue(Department::where('name', 'Test Department Edited')->exists());
$this->assertTrue(Department::where('name', 'Test Department Edited')->where('notes', 'Test Note Edited')->exists());

}

Expand Down
3 changes: 2 additions & 1 deletion tests/Feature/Groups/Api/StoreGroupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function testCanStoreGroupWithPermissionsPassed()
$this->actingAsForApi(User::factory()->superuser()->create())
->postJson(route('api.groups.store'), [
'name' => 'My Awesome Group',
'notes' => 'My Awesome Note',
'permissions' => [
'admin' => '1',
'import' => '1',
Expand All @@ -29,7 +30,7 @@ public function testCanStoreGroupWithPermissionsPassed()
])
->assertOk();

$group = Group::where('name', 'My Awesome Group')->first();
$group = Group::where('name', 'My Awesome Group')->where('notes', 'My Awesome Note')->first();

$this->assertNotNull($group);
$this->assertEquals('1', $group->decodePermissions()['admin']);
Expand Down
15 changes: 15 additions & 0 deletions tests/Feature/Groups/Ui/CreateGroupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Tests\Feature\Groups\Ui;

use App\Models\Group;
use App\Models\User;
use Tests\TestCase;

Expand All @@ -13,4 +14,18 @@ public function testPageRenders()
->get(route('groups.create'))
->assertOk();
}

public function testUserCanCreateGroup()
{
$this->assertFalse(Group::where('name', 'Test Group')->exists());

$this->actingAs(User::factory()->superuser()->create())
->post(route('groups.store'), [
'name' => 'Test Group',
'notes' => 'Test Note',
])
->assertRedirect(route('groups.index'));

$this->assertTrue(Group::where('name', 'Test Group')->where('notes', 'Test Note')->exists());
}
}
18 changes: 18 additions & 0 deletions tests/Feature/Groups/Ui/UpdateGroupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,22 @@ public function testPageRenders()
->get(route('groups.edit', Group::factory()->create()->id))
->assertOk();
}

public function testUserCanEditGroups()
{
$group = Group::factory()->create(['name' => 'Test Group']);
$this->assertTrue(Group::where('name', 'Test Group')->exists());

$response = $this->actingAs(User::factory()->superuser()->create())
->put(route('groups.update', ['group' => $group]), [
'name' => 'Test Group Edited',
'notes' => 'Test Note Edited',
])
->assertStatus(302)
->assertSessionHasNoErrors()
->assertRedirect(route('groups.index'));

$this->followRedirects($response)->assertSee('Success');
$this->assertTrue(Group::where('name', 'Test Group Edited')->where('notes', 'Test Note Edited')->exists());
}
}
20 changes: 20 additions & 0 deletions tests/Feature/Locations/Api/CreateLocationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,26 @@ public function testRequiresPermissionToCreateLocation()
->assertForbidden();
}


public function testCanCreateLocation()
{
$response = $this->actingAsForApi(User::factory()->superuser()->create())
->postJson(route('api.locations.store'), [
'name' => 'Test Location',
'notes' => 'Test Note',
])
->assertOk()
->assertStatusMessageIs('success')
->assertStatus(200)
->json();

$this->assertTrue(Location::where('name', 'Test Location')->exists());

$department = Location::find($response['payload']['id']);
$this->assertEquals('Test Location', $department->name);
$this->assertEquals('Test Note', $department->notes);
}

public function testCannotCreateNewLocationsWithTheSameName()
{
$location = Location::factory()->create();
Expand Down
6 changes: 4 additions & 2 deletions tests/Feature/Locations/Api/UpdateLocationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,17 @@ public function testCanUpdateLocationViaPatch()

$this->actingAsForApi(User::factory()->superuser()->create())
->patchJson(route('api.locations.update', $location), [
'name' => 'Test Location',
'name' => 'Test Updated Location',
'notes' => 'Test Updated Note',
])
->assertOk()
->assertStatusMessageIs('success')
->assertStatus(200)
->json();

$location->refresh();
$this->assertEquals('Test Location', $location->name, 'Name was not updated');
$this->assertEquals('Test Updated Location', $location->name, 'Name was not updated');
$this->assertEquals('Test Updated Note', $location->notes, 'Note was not updated');

}

Expand Down
4 changes: 2 additions & 2 deletions tests/Feature/Locations/Ui/CreateLocationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ public function testUserCanCreateLocations()
$this->actingAs(User::factory()->superuser()->create())
->post(route('locations.store'), [
'name' => 'Test Location',
'company_id' => Company::factory()->create()->id
'notes' => 'Test Note',
])
->assertRedirect(route('locations.index'));

$this->assertTrue(Location::where('name', 'Test Location')->exists());
$this->assertTrue(Location::where('name', 'Test Location')->where('notes', 'Test Note')->exists());
}

public function testUserCannotCreateLocationsWithInvalidParent()
Expand Down
3 changes: 2 additions & 1 deletion tests/Feature/Locations/Ui/UpdateLocationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@ public function testUserCanEditLocations()
$response = $this->actingAs(User::factory()->superuser()->create())
->put(route('locations.update', ['location' => $location]), [
'name' => 'Test Location Edited',
'notes' => 'Test Note Edited',
])
->assertStatus(302)
->assertSessionHasNoErrors()
->assertRedirect(route('locations.index'));

$this->followRedirects($response)->assertSee('Success');
$this->assertTrue(Location::where('name', 'Test Location Edited')->exists());
$this->assertTrue(Location::where('name', 'Test Location Edited')->where('notes', 'Test Note Edited')->exists());
}

public function testUserCannotEditLocationsToMakeThemTheirOwnParent()
Expand Down
39 changes: 39 additions & 0 deletions tests/Feature/Manufacturers/Api/CreateManufacturersTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Tests\Feature\Manufacturers\Api;

use App\Models\Manufacturer;
use App\Models\User;
use Tests\TestCase;

class CreateManufacturersTest extends TestCase
{


public function testRequiresPermissionToCreateDepartment()
{
$this->actingAsForApi(User::factory()->create())
->postJson(route('api.departments.store'))
->assertForbidden();
}

public function testCanCreateManufacturer()
{
$response = $this->actingAsForApi(User::factory()->superuser()->create())
->postJson(route('api.manufacturers.store'), [
'name' => 'Test Manufacturer',
'notes' => 'Test Note',
])
->assertOk()
->assertStatusMessageIs('success')
->assertStatus(200)
->json();

$this->assertTrue(Manufacturer::where('name', 'Test Manufacturer')->where('notes', 'Test Note')->exists());

$manufacturer = Manufacturer::find($response['payload']['id']);
$this->assertEquals('Test Manufacturer', $manufacturer->name);
$this->assertEquals('Test Note', $manufacturer->notes);
}

}
Loading

0 comments on commit 3de5f58

Please sign in to comment.