Magento1PasswordAttemptTest.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace Webkul\BagistoApi\Tests\Unit\Migration;
  3. use App\Auth\CustomerUserProvider;
  4. use App\Support\Magento1Password;
  5. use Illuminate\Support\Facades\Hash;
  6. use Illuminate\Support\Facades\Schema;
  7. use Webkul\BagistoApi\Tests\BagistoApiTestCase;
  8. use Webkul\Customer\Models\Customer;
  9. class Magento1PasswordAttemptTest extends BagistoApiTestCase
  10. {
  11. public function setUp(): void
  12. {
  13. parent::setUp();
  14. if (! Schema::hasColumn('customers', 'legacy_password')) {
  15. $this->markTestSkipped('Run php artisan migrate to add Asteria customer columns.');
  16. }
  17. $this->seedRequiredData();
  18. }
  19. public function test_attempt_upgrades_a_legacy_hash_to_bcrypt(): void
  20. {
  21. $customer = $this->createCustomer([
  22. 'password' => Hash::make('placeholder-secret'),
  23. 'legacy_password' => md5('abcsecret12').':abc',
  24. ]);
  25. $this->assertTrue(Magento1Password::attempt($customer, 'secret12'));
  26. $customer->refresh();
  27. $this->assertTrue(Hash::check('secret12', $customer->password));
  28. $this->assertNull($customer->legacy_password);
  29. }
  30. public function test_attempt_accepts_an_existing_bcrypt_password(): void
  31. {
  32. $customer = $this->createCustomer([
  33. 'password' => Hash::make('secret12'),
  34. 'legacy_password' => null,
  35. ]);
  36. $this->assertTrue(Magento1Password::attempt($customer, 'secret12'));
  37. $this->assertFalse(Magento1Password::attempt($customer, 'wrong-password'));
  38. }
  39. public function test_customer_user_provider_upgrades_legacy_password(): void
  40. {
  41. $customer = $this->createCustomer([
  42. 'password' => Hash::make('placeholder-secret'),
  43. 'legacy_password' => md5('abcsecret12').':abc',
  44. ]);
  45. $provider = new CustomerUserProvider(app('hash'), Customer::class);
  46. $this->assertTrue($provider->validateCredentials($customer, ['password' => 'secret12']));
  47. $customer->refresh();
  48. $this->assertTrue(Hash::check('secret12', $customer->password));
  49. $this->assertNull($customer->legacy_password);
  50. }
  51. }