Browse Source

mobyt fix

Vincent GUFFON 3 years ago
parent
commit
04e82b94d9

+ 21 - 1
src/Service/Mobyt/MobytService.php

@@ -4,7 +4,9 @@ declare(strict_types=1);
 namespace App\Service\Mobyt;
 
 use App\Service\Rest\ApiRequestService;
+use App\Tests\Service\Mobyt\MobytServiceTest;
 use JetBrains\PhpStorm\Pure;
+use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
 use Symfony\Contracts\HttpClient\HttpClientInterface;
 
 /**
@@ -37,7 +39,7 @@ class MobytService extends ApiRequestService
     }
 
     /**
-     * Get a dolibarr society by its opentalent organization id
+     * Get a mobyt user status by its opentalent organization id
      *
      * @param int $organizationId
      * @param string $login
@@ -54,4 +56,22 @@ class MobytService extends ApiRequestService
             ]
         );
     }
+
+    /**
+     * Test if login and pass are correct
+     *
+     * @param string $login
+     * @param string $password
+     * @return bool
+     * @see MobytServiceTest::testIsCredentialsAreCorrect()
+     */
+    public function isCredentialsAreCorrect(string $login, string $password): bool
+    {
+        try{
+            $this->connect($login, $password);
+        }catch (NotFoundHttpException $exception){
+            return false;
+        }
+        return true;
+    }
 }

+ 17 - 0
src/Validator/Organization/Parameters/MobytCredentials.php

@@ -0,0 +1,17 @@
+<?php
+declare(strict_types=1);
+
+namespace App\Validator\Organization\Parameters;
+
+use Symfony\Component\Validator\Constraint;
+
+#[\Attribute]
+class MobytCredentials extends Constraint
+{
+    public $message = 'wrong_mobyt_credentials';
+
+    public function getTargets()
+    {
+        return self::CLASS_CONSTRAINT;
+    }
+}

+ 35 - 0
src/Validator/Organization/Parameters/MobytCredentialsValidator.php

@@ -0,0 +1,35 @@
+<?php
+declare(strict_types=1);
+
+namespace App\Validator\Organization\Parameters;
+
+use App\Entity\Organization\Parameters;
+use App\Service\Mobyt\MobytService;
+use Symfony\Component\Validator\Constraint;
+use Symfony\Component\Validator\ConstraintValidator;
+
+/**
+ * Classe control que les logins/mot de passe mobyt sont OK.
+ */
+class MobytCredentialsValidator extends ConstraintValidator
+{
+    public function __construct(private MobytService $mobytService){}
+
+    public function validate($value, Constraint $constraint)
+    {
+        /** @var Parameters $parameters */
+        $parameters = $value;
+
+        $userNameSms = $parameters->getUsernameSMS();
+        $passwordSms = $parameters->getPasswordSMS();
+
+        if(empty($userNameSms) || empty($passwordSms))
+            return;
+
+        if(!$this->mobytService->isCredentialsAreCorrect($userNameSms, $passwordSms)){
+            $this->context->buildViolation($constraint->message)
+                ->atPath('passwordSMS')
+                ->addViolation();
+        }
+    }
+}

+ 43 - 0
tests/Service/Mobyt/MobytServiceTest.php

@@ -4,6 +4,7 @@ namespace App\Tests\Service\Mobyt;
 
 use App\Service\Mobyt\MobytService;
 use PHPUnit\Framework\TestCase;
+use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
 use Symfony\Contracts\HttpClient\HttpClientInterface;
 use Symfony\Contracts\HttpClient\ResponseInterface;
 
@@ -62,4 +63,46 @@ class MobytServiceTest extends TestCase
             33.0
         );
     }
+
+    /**
+     * @see MobytService::isCredentialsAreCorrect()
+     */
+    public function testIsCredentialsAreCorrect(): void {
+
+        // Mock the response that will be returned by the connection request
+        $connectResponse = $this->getMockBuilder(ResponseInterface::class)
+            ->disableOriginalConstructor()
+            ->getMock();
+        $connectResponse->method('getContent')->willReturn('userId;sessionKey');
+
+        $this->client
+            ->method('request')
+            ->with("GET", "login?username=user&password=pwd")
+            ->willReturnOnConsecutiveCalls(
+                $connectResponse
+            );
+
+        $this->assertTrue($this->mobytService->isCredentialsAreCorrect('user', 'pwd'));
+    }
+
+    /**
+     * @see MobytService::isCredentialsAreCorrect()
+     */
+    public function testIsCredentialsUnCorrect(): void {
+
+        // Mock the response that will be returned by the connection request
+        $connectResponse = $this->getMockBuilder(ResponseInterface::class)
+            ->disableOriginalConstructor()
+            ->getMock();
+        $connectResponse->method('getContent')->willThrowException(new NotFoundHttpException());
+
+        $this->client
+            ->method('request')
+            ->with("GET", "login?username=user&password=pwd")
+            ->willReturnOnConsecutiveCalls(
+                $connectResponse
+            );
+
+        $this->assertFalse($this->mobytService->isCredentialsAreCorrect('user', 'pwd'));
+    }
 }