-
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.
* feat: Adds Matchold Password Rule * test: Match Old Password Test
- Loading branch information
Showing
7 changed files
with
167 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace AchyutN\LaravelHelpers\Rules; | ||
|
||
use Closure; | ||
use Illuminate\Contracts\Validation\ValidationRule; | ||
use Illuminate\Support\Facades\Hash; | ||
|
||
class MatchOldPassword implements ValidationRule | ||
{ | ||
/** | ||
* Run the validation rule. | ||
* | ||
* @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail | ||
*/ | ||
public function validate(string $attribute, mixed $value, Closure $fail): void | ||
{ | ||
if (!Hash::check($value, auth()->user()->password)) { | ||
$fail('The old password does not match.'); | ||
} | ||
} | ||
} |
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,30 @@ | ||
<?php | ||
|
||
namespace AchyutN\LaravelHelpers\Tests\Factories; | ||
|
||
use AchyutN\LaravelHelpers\Tests\Models\User; | ||
use Illuminate\Database\Eloquent\Factories\Factory; | ||
|
||
class UserFactory extends Factory | ||
{ | ||
/** | ||
* The name of the factory's corresponding model. | ||
* | ||
* @var class-string<\Illuminate\Database\Eloquent\Model> | ||
*/ | ||
protected $model = User::class; | ||
|
||
/** | ||
* Define the model's default state. | ||
* | ||
* @return array<string, mixed> | ||
*/ | ||
public function definition(): array | ||
{ | ||
return [ | ||
'name' => $this->faker->name, | ||
'email' => $this->faker->email, | ||
'password' => bcrypt('password') | ||
]; | ||
} | ||
} |
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,36 @@ | ||
<?php | ||
|
||
namespace AchyutN\LaravelHelpers\Tests; | ||
|
||
use AchyutN\LaravelHelpers\Tests\Models\User; | ||
|
||
class MatchOldPasswordTest extends BaseTestCase | ||
{ | ||
|
||
public \Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model $user; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$this->user = User::factory()->create(); | ||
} | ||
|
||
public function test_call_password_change_route_with_valid_data() | ||
{ | ||
$response = $this->actingAs($this->user)->post('/change-password', [ | ||
'password' => 'password' | ||
]); | ||
$response->assertStatus(200); | ||
} | ||
|
||
public function test_call_password_change_route_with_invalid_data() | ||
{ | ||
$response = $this->actingAs($this->user)->post('/change-password', [ | ||
'password' => 'Password' | ||
]); | ||
$response->assertStatus(302); | ||
$response->assertSessionHasErrors(['password']); | ||
|
||
$this->assertEquals('The old password does not match.', session('errors')->get('password')[0]); | ||
} | ||
} |
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,21 @@ | ||
<?php | ||
|
||
namespace AchyutN\LaravelHelpers\Tests\Models; | ||
|
||
use AchyutN\LaravelHelpers\Tests\Factories\UserFactory; | ||
use Illuminate\Foundation\Auth\User as Authenticatable; | ||
use Illuminate\Database\Eloquent\Factories\Factory; | ||
|
||
class User extends Authenticatable | ||
{ | ||
protected $guarded = []; | ||
|
||
protected static function factory(int $count = 1): Factory | ||
{ | ||
if ($count && $count > 1) { | ||
return UserFactory::times($count); | ||
} else { | ||
return UserFactory::new(); | ||
} | ||
} | ||
} |
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 | ||
|
||
namespace AchyutN\LaravelHelpers\Tests\Routes; | ||
|
||
use AchyutN\LaravelHelpers\Rules\MatchOldPassword; | ||
use AchyutN\LaravelHelpers\Tests\BaseTestCase; | ||
use Illuminate\Http\Request; | ||
|
||
class PasswordMatchRoutes extends BaseTestCase | ||
{ | ||
public static function setupPasswordMatchRoutes($router): void | ||
{ | ||
$router->post('change-password', function (Request $request) { | ||
$validated = $request->validate([ | ||
'password' => new MatchOldPassword | ||
]); | ||
return response()->json($validated); | ||
}); | ||
} | ||
} |
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,34 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up(): void | ||
{ | ||
Schema::create("users", function (Blueprint $table) { | ||
$table->id(); | ||
$table->string("name"); | ||
$table->string('email'); | ||
$table->string("password"); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::dropIfExists("users"); | ||
} | ||
}; |