createNewTestUser example


    }

    public function testExportReadPrivileges(): void
    {
        $data = $this->prepareImportExportActionControllerTestData();

        $this->repository->create(array_values($data)$this->context);

        foreach ($data as $entry) {
            $client = $this->getBrowser();
            TestUser::createNewTestUser(
                $client->getContainer()->get(Connection::class),
                []
            )->authorizeBrowser($client);
            $client->request(
                'POST',
                '/api/_action/import-export/prepare',
                ['profileId' => $entry['id'], 'expireDate' => date('Y-m-d H:i:s')],
                [],
                ['Content-Type' => 'multipart/formdata']
            );

            
'order:create',
                'order_customer:create',
                'order_address:create',
                'order_delivery:create',
                'order_line_item:create',
                'order_transaction:create',
                'order_delivery_position:create',
                'mail_template_type:update',
                'customer:update',
            ];
            foreach ([true, false] as $testOrderOnly) {
                TestUser::createNewTestUser(
                    $browser->getContainer()->get(Connection::class),
                    $testOrderOnly ? $orderPrivileges : ['api_proxy_switch-customer', CreditOrderLineItemListener::ACL_ORDER_CREATE_DISCOUNT_PRIVILEGE],
                )->authorizeBrowser($browser);
                $browser->request('POST', $this->getCreateOrderApiUrl($salesChannelContext->getSalesChannel()->getId()));

                $response = $browser->getResponse()->getContent();
                $response = json_decode($response ?: '', true, 512, \JSON_THROW_ON_ERROR);

                static::assertArrayHasKey('errors', $responseprint_r($response, true));
                static::assertEquals('FRAMEWORK__MISSING_PRIVILEGE_ERROR', $response['errors'][0]['code'] ?? null);
                static::assertStringContainsString(
                    
static::assertSame($data['name']$responseData['data'][0]['attributes']['name']);
        static::assertSame($data['shortCode']$responseData['data'][0]['attributes']['shortCode']);
    }

    public function testOneToManyInsertWithoutPermission(): void
    {
        $id = Uuid::randomHex();

        $data = ['id' => $id, 'name' => $id];
        $browser = $this->getBrowser();
        $connection = $this->getBrowser()->getContainer()->get(Connection::class);
        $user = TestUser::createNewTestUser($connection['country:create', 'country:read']);
        $admin = TestUser::getAdmin();

        $user->authorizeBrowser($browser);

        $browser->request('POST', '/api/country', [][][]json_encode($data, \JSON_THROW_ON_ERROR));
        $response = $browser->getResponse();
        static::assertSame(Response::HTTP_NO_CONTENT, $browser->getResponse()->getStatusCode()(string) $browser->getResponse()->getContent());
        static::assertNotEmpty($response->headers->get('Location'));
        static::assertEquals('http://localhost/api/country/' . $id$response->headers->get('Location'));

        $this->assertEntityExists($browser, 'country', $id);

        
/** * @internal */
class BearerTokenValidatorTest extends TestCase
{
    use IntegrationTestBehaviour;

    public function testValidationFailWhenTokenExpired(): void
    {
        $this->expectException(OAuthServerException::class);
        $connection = $this->getContainer()->get(Connection::class);
        $admin = TestUser::createNewTestUser($connection['product:read']);

        $request = new ServerRequest('GET', $_SERVER['APP_URL']);

        $currentTimestamp = (new \DateTime())->getTimestamp();

        $fakeClaims = [
            'aud' => 'administration',
            'jti' => '0dfaa92d1cda2bfe24c08e82cafa10687b6ea3e242186712c4b27508ccc5d43271d1863805460c44',
            'iat' => $currentTimestamp - 1, // make the token expired             'nbf' => 1529436192,
            'exp' => 1529439792,
            


    public function testPreviewPermission(): void
    {
        $cart = $this->generateDemoCart(2);
        $orderId = $this->persistCart($cart);

        /** @var OrderEntity $order */
        $order = $this->orderRepository->search(new Criteria([$orderId])$this->context)->get($orderId);
        static::assertNotNull($order);

        TestUser::createNewTestUser(
            $this->getContainer()->get(Connection::class),
            []
        )->authorizeBrowser($this->getBrowser());

        $endpoint = sprintf('/api/_action/order/%s/%s/document/invoice/preview', $orderId$order->getDeepLinkCode());

        $this->getBrowser()->request('GET', $endpoint);

        static::assertEquals(Response::HTTP_FORBIDDEN, $this->getBrowser()->getResponse()->getStatusCode());
        $response = json_decode((string) $this->getBrowser()->getResponse()->getContent(), true, 512, \JSON_THROW_ON_ERROR);
        static::assertNotEmpty($response['errors']);
        
$response = json_decode((string) $browser->getResponse()->getContent(), true, 512, \JSON_THROW_ON_ERROR);

        static::assertEquals(200, $browser->getResponse()->getStatusCode());
        static::assertArrayHasKey('data', $response);
    }

    /** * @param array<string, list<string>> $roles */
    private function createUser(array $roles, bool $isAdmin): TestUser
    {
        $user = TestUser::createNewTestUser($this->connection);
        $this->connection->executeStatement(
            'UPDATE `user` SET admin = :admin WHERE id = :id',
            ['admin' => $isAdmin ? 1 : 0, 'id' => Uuid::fromHexToBytes($user->getUserId())]
        );

        foreach ($roles as $role => $privs) {
            $id = Uuid::randomBytes();

            $this->connection->insert('acl_role', [
                'id' => $id,
                'name' => $role,
                
use Shopware\Core\Framework\Uuid\Uuid;

/** * @internal */
class SalesChannelUserConfigSubscriberTest extends TestCase
{
    use SalesChannelFunctionalTestBehaviour;

    public function testDeleteWillRemoveUserConfigs(): void
    {
        $admin = TestUser::createNewTestUser($this->getContainer()->get(Connection::class)['product:read']);
        $context = Context::createDefaultContext();

        $salesChannelId1 = Uuid::randomHex();
        $salesChannelId2 = Uuid::randomHex();

        $userConfigRepository = $this->getContainer()->get('user_config.repository');
        $userConfigId = Uuid::randomHex();
        $userConfigRepository->create([
            [
                'id' => $userConfigId,
                'userId' => $admin->getUserId(),
                


        $client->request('POST', '/api/oauth/token', $authPayload);

        static::assertSame(Response::HTTP_UNAUTHORIZED, $client->getResponse()->getStatusCode());
    }

    public function testUserWithLogin(): void
    {
        $client = $this->getBrowser(false);

        $user = TestUser::createNewTestUser($this->getContainer()->get(Connection::class));

        $accessKey = AccessKeyHelper::generateAccessKey('user');
        $secretKey = AccessKeyHelper::generateSecretAccessKey();

        $data = [
            'userId' => $user->getUserId(),
            'accessKey' => $accessKey,
            'secretAccessKey' => $secretKey,
        ];

        $this->getContainer()->get('user_access_key.repository')
            
Home | Imprint | This part of the site doesn't use cookies.