Skip to content

Commit

Permalink
feat: Match Old Password Rule (#19)
Browse files Browse the repository at this point in the history
* feat: Adds Matchold Password Rule

* test: Match Old Password Test
  • Loading branch information
Dipesh79 authored Feb 9, 2024
1 parent c742ce5 commit 46636b6
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 1 deletion.
22 changes: 22 additions & 0 deletions src/Rules/MatchOldPassword.php
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.');
}
}
}
5 changes: 4 additions & 1 deletion tests/BaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace AchyutN\LaravelHelpers\Tests;

use AchyutN\LaravelHelpers\Tests\Routes\LatLongRoutes;
use AchyutN\LaravelHelpers\Tests\Routes\PasswordMatchRoutes;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Orchestra\Testbench\TestCase as Orchestra;

Expand All @@ -18,6 +19,8 @@ public function setUp(): void
$this->artisan('migrate')->run();

LatLongRoutes::setupLatLongRoutes($this->app->get('router'));
PasswordMatchRoutes::setupPasswordMatchRoutes($this->app->get('router'));

}

protected function getEnvironmentSetUp($app): void
Expand Down Expand Up @@ -55,4 +58,4 @@ public function getPackageProviders($app): array
\AchyutN\LaravelHelpers\LaravelHelperProvider::class,
];
}
}
}
30 changes: 30 additions & 0 deletions tests/Factories/UserFactory.php
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')
];
}
}
36 changes: 36 additions & 0 deletions tests/MatchOldPasswordTest.php
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]);
}
}
21 changes: 21 additions & 0 deletions tests/Models/User.php
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();
}
}
}
20 changes: 20 additions & 0 deletions tests/Routes/PasswordMatchRoutes.php
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);
});
}
}
34 changes: 34 additions & 0 deletions tests/migrations/users_table.php
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");
}
};

0 comments on commit 46636b6

Please sign in to comment.