Completed
Push — 1.4-password-hashing-2 ( b79be0...722ba0 )
by Kamil
16:49
created

UpdatingUserPasswordEncoderTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sylius\Tests\Functional;
6
7
use Doctrine\Common\Persistence\ObjectManager;
8
use Fidry\AliceDataFixtures\LoaderInterface;
9
use Fidry\AliceDataFixtures\Persistence\PurgeMode;
10
use PHPUnit\Framework\Assert;
11
use Sylius\Component\User\Repository\UserRepositoryInterface;
12
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
13
14
final class UpdatingUserPasswordEncoderTest extends WebTestCase
15
{
16
    protected function setUp(): void
17
    {
18
        static::bootKernel();
19
20
        /** @var LoaderInterface $fixtureLoader */
21
        $fixtureLoader = static::$container->get('fidry_alice_data_fixtures.loader.doctrine');
22
23
        $fixtureLoader->load(
24
            [
25
                __DIR__ . '/../DataFixtures/ORM/resources/channels.yml',
26
                __DIR__ . '/../DataFixtures/ORM/resources/customers.yml',
27
                __DIR__ . '/../DataFixtures/ORM/resources/admin_users.yml',
28
            ],
29
            [],
30
            [],
31
            PurgeMode::createDeleteMode()
32
        );
33
    }
34
35
    /** @test */
36
    public function it_updates_the_encoder_when_the_shop_user_logs_in(): void
37
    {
38
        $client = static::createClient();
39
        $client->followRedirects(true);
40
41
        /** @var UserRepositoryInterface $shopUserRepository */
42
        $shopUserRepository = static::$container->get('sylius.repository.shop_user');
43
44
        /** @var ObjectManager $shopUserManager */
45
        $shopUserManager = static::$container->get('sylius.manager.shop_user');
46
47
        $shopUser = $shopUserRepository->findOneByEmail('[email protected]');
48
        $shopUser->setPlainPassword('testpassword');
49
        $shopUser->setEncoderName('sha512');
50
51
        $shopUserManager->persist($shopUser);
52
        $shopUserManager->flush();
53
54
        $client->request('GET', '/en_US/login');
55
56
        $client->submitForm('Login', [
57
            '_username' => '[email protected]',
58
            '_password' => 'testpassword'
59
        ]);
60
61
        Assert::assertSame(200, $client->getResponse()->getStatusCode());
62
        Assert::assertSame('/en_US/', parse_url($client->getCrawler()->getUri(), \PHP_URL_PATH));
63
        Assert::assertSame('argon2i', $shopUserRepository->findOneByEmail('[email protected]')->getEncoderName());
64
    }
65
66
    /** @test */
67
    public function it_updates_the_encoder_when_the_admin_user_logs_in(): void
68
    {
69
        $client = static::createClient();
70
        $client->followRedirects(true);
71
72
        /** @var UserRepositoryInterface $adminUserRepository */
73
        $adminUserRepository = static::$container->get('sylius.repository.admin_user');
74
75
        /** @var ObjectManager $adminUserManager */
76
        $adminUserManager = static::$container->get('sylius.manager.admin_user');
77
78
        $adminUser = $adminUserRepository->findOneByEmail('[email protected]');
79
        $adminUser->setPlainPassword('testpassword');
80
        $adminUser->setEncoderName('sha512');
81
82
        $adminUserManager->persist($adminUser);
83
        $adminUserManager->flush();
84
85
        $client->request('GET', '/admin/login');
86
87
        $client->submitForm('Login', [
88
            '_username' => '[email protected]',
89
            '_password' => 'testpassword'
90
        ]);
91
92
        Assert::assertSame(200, $client->getResponse()->getStatusCode());
93
        Assert::assertSame('/admin/', parse_url($client->getCrawler()->getUri(), \PHP_URL_PATH));
94
        Assert::assertSame('argon2i', $adminUserRepository->findOneByEmail('[email protected]')->getEncoderName());
95
    }
96
}
97