Skip to content

Commit 57df433

Browse files
authored
Policies and testing (#52)
* Add OrganisationPolicy * Add tests to make sure users can't register or update with an email that already exists * Install Sass * Fix button styles
1 parent ad95f8a commit 57df433

File tree

13 files changed

+79
-46
lines changed

13 files changed

+79
-46
lines changed

app/Http/Requests/Organisation/OrganisationEdit.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
namespace App\Http\Requests\Organisation;
44

5-
use App\Enums\Permission;
65
use Illuminate\Foundation\Http\FormRequest;
6+
use Illuminate\Support\Facades\Gate;
77

88
class OrganisationEdit extends FormRequest
99
{
1010
public function authorize()
1111
{
12-
return $this->user()->can(Permission::EDIT_ORGANISATION->value);
12+
return Gate::allows('edit', $this->user()->currentOrganisation);
1313
}
1414
}

app/Http/Requests/Organisation/OrganisationUpdate.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
namespace App\Http\Requests\Organisation;
44

5-
use App\Enums\Permission;
65
use Illuminate\Foundation\Http\FormRequest;
6+
use Illuminate\Support\Facades\Gate;
77

88
class OrganisationUpdate extends FormRequest
99
{
1010
public function authorize()
1111
{
12-
return $this->user()->can(Permission::UPDATE_ORGANISATION->value);
12+
return Gate::allows('update', $this->user()->currentOrganisation);
1313
}
1414

1515
public function rules()

app/Policies/OrganisationPolicy.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace App\Policies;
4+
5+
use App\Enums\Permission;
6+
use App\Models\Organisation;
7+
use App\Models\User;
8+
9+
class OrganisationPolicy
10+
{
11+
public function edit(User $user, Organisation $organisation)
12+
{
13+
return $user->can(Permission::EDIT_ORGANISATION->value) && $user->currentOrganisation->is($organisation);
14+
}
15+
16+
public function update(User $user, Organisation $organisation)
17+
{
18+
return $user->can(Permission::UPDATE_ORGANISATION->value) && $user->currentOrganisation->is($organisation);
19+
}
20+
}

bun.lockb

755 Bytes
Binary file not shown.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"autoprefixer": "^10.4.16",
1313
"laravel-vite-plugin": "^0.7.8",
1414
"postcss": "^8.4.31",
15+
"sass": "^1.70.0",
1516
"tailwindcss": "^3.3.5",
1617
"vite": "^4.3.9",
1718
"vite-svg-loader": "^4.0.0",

resources/js/Components/Button.vue

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<template>
2-
<button class="rounded-md bg-slate-800 px-6 py-3 text-sm font-semibold text-white inline-flex shadow-sm hover:bg-slate-500">
2+
<button
3+
class="rounded-md bg-slate-800 px-6 py-3 text-sm font-semibold text-white inline-flex shadow-sm hover:bg-slate-500"
4+
>
35
<span v-text="text"></span>
46
</button>
57
</template>
@@ -11,25 +13,6 @@
1113
type: String,
1214
default: "Submit",
1315
},
14-
styles: String | Array,
15-
},
16-
17-
computed: {
18-
classes() {
19-
let classes = ["btn"];
20-
21-
if (this.styles) {
22-
if (this.styles.constructor.name === "String") {
23-
classes.push(`btn--${this.styles}`);
24-
} else if (this.styles.constructor.name === "Array") {
25-
this.styles.forEach((style) => {
26-
classes.push(`btn--${style}`);
27-
});
28-
}
29-
}
30-
31-
return classes.join(" ");
32-
},
3316
},
3417
};
3518
</script>

resources/js/Pages/Login/Show.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
<div class="col-span-full">
6262
<Button
6363
text="Log In"
64-
styles="full"
64+
class="w-full text-center justify-center"
6565
:disabled="loginForm.processing"
6666
/>
6767
</div>

resources/js/Pages/Register/Show.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
<div class="col-span-full">
9393
<Button
9494
text="Register"
95-
styles="full"
95+
class="w-full text-center justify-center"
9696
:disabled="registerForm.processing"
9797
/>
9898
</div>

tests/Feature/Controllers/AccountControllerTest.php

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use function Pest\Laravel\patch;
1010

1111
describe('Users', function () {
12-
test('Can edit their accounts', function () {
12+
test('Can access the edit page', function () {
1313
$user = User::factory()->create();
1414

1515
actingAs($user)
@@ -25,20 +25,13 @@
2525
});
2626

2727
test('Can update their details', function () {
28-
$user = User::factory()->create($oldData = [
28+
$user = User::factory()->create([
2929
'first_name' => 'Jim',
3030
'last_name' => 'Gordon',
3131
'email' => '[email protected]',
3232
'password' => 'oldPassword#123',
3333
]);
3434

35-
expect($user)
36-
->first_name->toBe($oldData['first_name'])
37-
->last_name->toBe($oldData['last_name'])
38-
->email->toBe($oldData['email']);
39-
40-
expect(Hash::check($oldData['password'], $user->password))->toBeTrue();
41-
4235
actingAs($user)
4336
->patch(route('account.update'), $newData = [
4437
'first_name' => 'Tim',
@@ -56,10 +49,28 @@
5649

5750
expect(Hash::check($newData['password'], $user->password))->toBeTrue();
5851
});
52+
53+
test("Can't update their email to one that already exists", function () {
54+
User::factory()->create([
55+
'email' => '[email protected]',
56+
]);
57+
58+
$user = User::factory()->create([
59+
'email' => '[email protected]',
60+
]);
61+
62+
actingAs($user)
63+
->patch(route('account.update'), $newData = [
64+
'email' => '[email protected]',
65+
])
66+
->assertSessionHasErrors('email');
67+
68+
expect($user->refresh()->email)->not()->toBe($newData['email']);
69+
});
5970
});
6071

6172
describe('Guests', function () {
62-
test("Can't edit accounts", function () {
73+
test("Can't access the edit page", function () {
6374
get(route('account.edit'))
6475
->assertRedirect(route('login'));
6576
});

tests/Feature/Controllers/HomeControllerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use function Pest\Laravel\get;
88

99
describe('Users', function () {
10-
test('Can access home page', function () {
10+
test('Can access the home page', function () {
1111
actingAs(User::factory()->create())
1212
->get(route('home'))
1313
->assertInertia(
@@ -18,7 +18,7 @@
1818
});
1919

2020
describe('Guests', function () {
21-
test("Can't access home page", function () {
21+
test("Can't access the home page", function () {
2222
get(route('home'))
2323
->assertRedirect(route('login'));
2424
});

tests/Feature/Controllers/LoginControllerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010
use function Pest\Laravel\post;
1111

1212
describe('Users', function () {
13-
test("Can't access login page", function () {
13+
test("Can't access the login page", function () {
1414
actingAs(User::factory()->create())
1515
->get(route('login'))
1616
->assertRedirect(route('home'));
1717
});
1818
});
1919

2020
describe('Guests', function () {
21-
test('Can access login page', function () {
21+
test('Can access the login page', function () {
2222
get(route('login'))
2323
->assertOk()
2424
->assertInertia(

tests/Feature/Controllers/OrganisationControllerTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,18 @@
1414
$this->adminUser->organisations()->create([
1515
'name' => 'GCPD',
1616
]);
17+
1718
$this->adminUser->currentOrganisation()->associate($this->adminUser->organisations->first())->save();
1819
});
1920

2021
describe('Admins', function () {
21-
test('Can see the edit page their organisation', function () {
22+
test('Can see the edit page for their current organisation', function () {
2223
actingAs($this->adminUser)
2324
->get(route('organisation.edit'))
2425
->assertOk();
2526
});
2627

27-
test('Can update their organisation name', function () {
28+
test("Can update their organisation's name", function () {
2829
expect($this->adminUser->currentOrganisation->name)->toBe('GCPD');
2930

3031
actingAs($this->adminUser)
@@ -38,7 +39,7 @@
3839
});
3940

4041
describe('Non-Admins', function () {
41-
test("Can't see the edit page their organisation", function () {
42+
test("Can't see the edit page for their organisation", function () {
4243
$user = User::factory()->create();
4344

4445
$user->organisations()->save($this->adminUser->currentOrganisation);
@@ -49,7 +50,7 @@
4950
->assertForbidden();
5051
});
5152

52-
test("Can't update their organisation name", function () {
53+
test("Can't update their organisation's name", function () {
5354
$user = User::factory()->create();
5455

5556
$user->organisations()->save($this->adminUser->currentOrganisation);

tests/Feature/Controllers/RegisterControllerTest.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111
use function Pest\Laravel\post;
1212

1313
describe('Users', function () {
14-
test("Can't access register page", function () {
14+
test("Can't access the register page", function () {
1515
actingAs(User::factory()->create())
1616
->get(route('register'))
1717
->assertRedirect(route('home'));
1818
});
1919
});
2020

2121
describe('Guests', function () {
22-
test('Can access register page', function () {
22+
test('Can access the register page', function () {
2323
get(route('register'))
2424
->assertOk()
2525
->assertInertia(
@@ -53,4 +53,21 @@
5353

5454
assertAuthenticated();
5555
});
56+
57+
test("Can't register with an email that already exists", function () {
58+
$email = '[email protected]';
59+
60+
User::factory()->create([
61+
'email' => $email,
62+
]);
63+
64+
post(route('register.store'), [
65+
'organisation_name' => fake()->company(),
66+
'first_name' => fake()->firstName(),
67+
'last_name' => fake()->lastName(),
68+
'email' => $email,
69+
'password' => fake()->password(),
70+
])
71+
->assertSessionHasErrors('email');
72+
});
5673
});

0 commit comments

Comments
 (0)