From d073551767bb41f1ccafc36283fc0e9a60f7c31f Mon Sep 17 00:00:00 2001 From: Viktor Scharf Date: Wed, 28 May 2025 15:56:36 +0200 Subject: [PATCH] ApiTest: set user profile photo --- tests/acceptance/TestHelpers/GraphHelper.php | 105 +++++++++++++ tests/acceptance/bootstrap/GraphContext.php | 141 ++++++++++++++++++ .../features/apiAntivirus/antivirus.feature | 10 ++ .../userProfilePhoto.feature | 64 ++++++++ .../filesForUpload/broken-image-file.png | Bin 0 -> 500 bytes .../filesWithVirus/eicar-image.jpeg | 1 + 6 files changed, 321 insertions(+) create mode 100644 tests/acceptance/features/apiGraphUserGroup/userProfilePhoto.feature create mode 100644 tests/acceptance/filesForUpload/broken-image-file.png create mode 100644 tests/acceptance/filesForUpload/filesWithVirus/eicar-image.jpeg diff --git a/tests/acceptance/TestHelpers/GraphHelper.php b/tests/acceptance/TestHelpers/GraphHelper.php index ac38dc6bc4..e426c7e000 100644 --- a/tests/acceptance/TestHelpers/GraphHelper.php +++ b/tests/acceptance/TestHelpers/GraphHelper.php @@ -2468,4 +2468,109 @@ public static function getAllUsers( self::getRequestHeaders() ); } + + /** + * @param string $baseUrl + * @param string $xRequestId + * @param string $user + * @param string $password + * @param string $source + * + * @return ResponseInterface + * @throws GuzzleException + */ + public static function addUserPhoto( + string $baseUrl, + string $xRequestId, + string $user, + string $password, + string $source + ): ResponseInterface { + $url = self::getFullUrl($baseUrl, "me/photo/\$value"); + return HttpRequestHelper::put( + $url, + $xRequestId, + $user, + $password, + ['Content-Type' => 'image/jpeg'], + $source + ); + } + + /** + * @param string $baseUrl + * @param string $xRequestId + * @param string $user + * @param string $password + * + * @return ResponseInterface + * @throws GuzzleException + */ + public static function getUserPhoto( + string $baseUrl, + string $xRequestId, + string $user, + string $password + ): ResponseInterface { + $url = self::getFullUrl($baseUrl, "me/photo/\$value"); + return HttpRequestHelper::get( + $url, + $xRequestId, + $user, + $password + ); + } + + /** + * @param string $baseUrl + * @param string $xRequestId + * @param string $user + * @param string $password + * @param string $source + * + * @return ResponseInterface + * @throws GuzzleException + */ + public static function changeUserPhoto( + string $baseUrl, + string $xRequestId, + string $user, + string $password, + string $source + ): ResponseInterface { + $url = self::getFullUrl($baseUrl, "me/photo/\$value"); + return HttpRequestHelper::sendRequest( + $url, + $xRequestId, + "PATCH", + $user, + $password, + ['Content-Type' => 'image/jpeg'], + $source + ); + } + + /** + * @param string $baseUrl + * @param string $xRequestId + * @param string $user + * @param string $password + * + * @return ResponseInterface + * @throws GuzzleException + */ + public static function deleteUserPhoto( + string $baseUrl, + string $xRequestId, + string $user, + string $password + ): ResponseInterface { + $url = self::getFullUrl($baseUrl, "me/photo/\$value"); + return HttpRequestHelper::delete( + $url, + $xRequestId, + $user, + $password + ); + } } diff --git a/tests/acceptance/bootstrap/GraphContext.php b/tests/acceptance/bootstrap/GraphContext.php index 120b79831a..fdc7df04bc 100644 --- a/tests/acceptance/bootstrap/GraphContext.php +++ b/tests/acceptance/bootstrap/GraphContext.php @@ -3178,4 +3178,145 @@ public function theUserGetsAllUsersUsingTheGraphApi(?string $user = null): void $this->featureContext->setResponse($response); } + + /** + * @When /^user "([^"]*)" sets profile photo to "([^"]*)" using the Graph API$/ + * + * @param string $user + * @param string $photo + * + * @return void + * @throws GuzzleException + */ + public function userSetsUserProfilePhotoUsingTheGraphApi( + string $user, + string $photo + ): void { + $source = \file_get_contents( + $this->featureContext->acceptanceTestsDirLocation() . $photo + ); + $response = GraphHelper::addUserPhoto( + $this->featureContext->getBaseUrl(), + $this->featureContext->getStepLineRef(), + $user, + $this->featureContext->getPasswordForUser($user), + $source + ); + $this->featureContext->setResponse($response); + } + + /** + * @Given /^user "([^"]*)" has set the profile photo to "([^"]*)"$/ + * + * @param string $user + * @param string $photo + * + * @return void + * @throws GuzzleException + * @throws Exception + */ + public function theUserHasSetPhoto(string $user, string $photo): void { + $response = $this->userSetsUserProfilePhotoUsingTheGraphApi($user, $photo); + $this->featureContext->theHTTPStatusCodeShouldBe(200, '', $response); + } + + /** + * @When /^user "([^"]*)" (gets|tries to get) a profile photo using the Graph API$/ + * + * @param string $user + * + * @return void + * @throws GuzzleException + */ + public function userShouldHasAProfilePhotoUsingTheGraphApi(string $user): void { + $response = GraphHelper::getUserPhoto( + $this->featureContext->getBaseUrl(), + $this->featureContext->getStepLineRef(), + $user, + $this->featureContext->getPasswordForUser($user) + ); + $this->featureContext->setResponse($response); + } + + /** + * @Then /^the profile photo should contain file "([^"]*)"$/ + * + * @param string $file + * + * @return void + * @throws GuzzleException + */ + public function profilePhotoShouldContainFile(string $file): void { + $source = \file_get_contents( + $this->featureContext->acceptanceTestsDirLocation() . $file + ); + Assert::assertEquals( + $source, + $this->featureContext->getResponse()->getBody()->getContents(), + "The profile photo binary does not match expected content of $file" + ); + } + + /** + * @Then /^for user "([^"]*)" the profile photo should contain file "([^"]*)"$/ + * + * @param string $user + * @param string $file + * + * @return void + * @throws GuzzleException + */ + public function profilePhotoForUserShouldContainFile(string $user, string $file): void { + $this->featureContext->theHTTPStatusCodeShouldBe( + 200, + "Expected response status code should be 200", + $this->userShouldHasAProfilePhotoUsingTheGraphApi($user) + ); + $this->profilePhotoShouldContainFile($file); + } + + /** + * @When /^user "([^"]*)" changes the profile photo to "([^"]*)" using the Graph API$/ + * + * @param string $user + * @param string $photo + * + * @return void + * @throws GuzzleException + */ + public function userChangesUserProfilePhotoUsingTheGraphApi( + string $user, + string $photo + ): void { + $source = \file_get_contents( + $this->featureContext->acceptanceTestsDirLocation() . $photo + ); + $response = GraphHelper::changeUserPhoto( + $this->featureContext->getBaseUrl(), + $this->featureContext->getStepLineRef(), + $user, + $this->featureContext->getPasswordForUser($user), + $source + ); + $this->featureContext->setResponse($response); + } + + /** + * @When /^user "([^"]*)" deletes the profile photo using the Graph API$/ + * + * @param string $user + * + * @return void + * @throws GuzzleException + */ + public function userDeletesAProfilePhotoUsingTheGraphApi(string $user): void { + $response = GraphHelper::deleteUserPhoto( + $this->featureContext->getBaseUrl(), + $this->featureContext->getStepLineRef(), + $user, + $this->featureContext->getPasswordForUser($user) + ); + $this->featureContext->setResponse($response); + } + } diff --git a/tests/acceptance/features/apiAntivirus/antivirus.feature b/tests/acceptance/features/apiAntivirus/antivirus.feature index 86ab76278e..4d843e94ca 100644 --- a/tests/acceptance/features/apiAntivirus/antivirus.feature +++ b/tests/acceptance/features/apiAntivirus/antivirus.feature @@ -481,3 +481,13 @@ Feature: antivirus | Virus found in text.txt. Upload not possible. Virus: Eicar-Signature | And for user "Brian" the content of the file "/text.txt" of the space "new-space" should be "hello world" And for user "Alice" the content of the file "/text.txt" of the space "new-space" should be "hello world" + + + Scenario Outline: try adding a photo of the user containing the virus + When user "Alice" sets profile photo to "filesForUpload/filesWithVirus/eicar-image.jpeg" using the Graph API + Then the HTTP status code should be "200" + And user "Alice" should get a notification with subject "Virus found" and message: + | message | + | Virus found in eicar-image.jpeg. Upload not possible. Virus: Eicar-Signature | + When user "Alice" tries to get a profile photo using the Graph API + Then the HTTP status code should be "404" diff --git a/tests/acceptance/features/apiGraphUserGroup/userProfilePhoto.feature b/tests/acceptance/features/apiGraphUserGroup/userProfilePhoto.feature new file mode 100644 index 0000000000..6415577cfe --- /dev/null +++ b/tests/acceptance/features/apiGraphUserGroup/userProfilePhoto.feature @@ -0,0 +1,64 @@ +Feature: user profile photo + As a user, I want to provide my avatar to make my actions more visible + + Background: + Given user "Alice" has been created with default attributes + + + Scenario Outline: add profile photo + When user "Alice" sets profile photo to "" using the Graph API + Then the HTTP status code should be "" + And for user "Alice" the profile photo should contain file "" + Examples: + | file | http-status-code | + | filesForUpload/testavatar.jpg | 200 | + | filesForUpload/testavatar.png | 200 | + | filesForUpload/example.gif | 200 | + | filesForUpload/lorem.txt | 400 | + | filesForUpload/simple.pdf | 400 | + | filesForUpload/broken-image-file.png | 400 | + + + Scenario: user tries to get profile photo when none is set + When user "Alice" tries to get a profile photo using the Graph API + Then the HTTP status code should be "404" + + + Scenario Outline: get profile photo + Given user "Alice" has set the profile photo to "" + When user "Alice" gets a profile photo using the Graph API + Then the HTTP status code should be "200" + And the profile photo should contain file "" + Examples: + | file | + | filesForUpload/testavatar.jpg | + | filesForUpload/testavatar.png | + | filesForUpload/example.gif | + + + Scenario Outline: change profile photo + Given user "Alice" has set the profile photo to "filesForUpload/testavatar.jpg" + When user "Alice" changes the profile photo to "" using the Graph API + Then the HTTP status code should be "" + And for user "Alice" the profile photo should contain file "" + Examples: + | file | http-status-code | + | filesForUpload/testavatar.jpg | 200 | + | filesForUpload/testavatar.png | 200 | + | filesForUpload/example.gif | 200 | + | filesForUpload/lorem.txt | 400 | + | filesForUpload/simple.pdf | 400 | + | filesForUpload/broken-image-file.png | 400 | + + + Scenario Outline: delete profile photo + Given user "Alice" has set the profile photo to "" + When user "Alice" deletes the profile photo using the Graph API + Then the HTTP status code should be "200" + When user "Alice" tries to get a profile photo using the Graph API + Then the HTTP status code should be "404" + Examples: + | file | + | filesForUpload/testavatar.jpg | + | filesForUpload/testavatar.png | + | filesForUpload/example.gif | diff --git a/tests/acceptance/filesForUpload/broken-image-file.png b/tests/acceptance/filesForUpload/broken-image-file.png new file mode 100644 index 0000000000000000000000000000000000000000..5b27010b6b47952acd60fffe7335bab168580819 GIT binary patch literal 500 zcmV4Tx07!|Imj_Uj*%rs|_oat~me5=1O?nF;C4?40K{|*^NCJcsVjwg-vbX{( ziiilP3%XPVSEa~W5DT_NQL*cSEDFlHSg?WmK41vD`{unj?_^HSZ%)oV_uM<*H}ekw zWExv2Oo3$qNEKv=f_*$_kx|h!+!(+BB|reufXe2i3mHK{0RXZ2_;-7M2S79Nmf1|N zfB*fznjDvx&H(@lLO70_&Phf1IO5YBp(q0Y)Da}Fo0TD);oAsPMaX~%kIrzy98b@1 z+$^JIXKO-(nF#9vfG4rpq67d)mPnqK$w@%^kY@I>6maQ%o^U4pDs*c=lq}7@xLuKMLfTUA>yP&k#8^(m61F9 zJvq%^!h*P^f%DAg&eqRzqS!ZVo;m5v=y^_031Q7Mm+k2<>65ZFV4mao-jW)A1}k)) zdFfsu^DIgWmh2lZVlw8LEn1jCoE#>}C-PX5{<(>v5%ZkM4__*oEjh%0p@u2Ri^Rc_ qv+x8y9t-<=OYR{x{V!+dvm|RX5<`6@_rPA5pC@1}^h}SG%*N$xU*bam literal 0 HcmV?d00001 diff --git a/tests/acceptance/filesForUpload/filesWithVirus/eicar-image.jpeg b/tests/acceptance/filesForUpload/filesWithVirus/eicar-image.jpeg new file mode 100644 index 0000000000..704cac859b --- /dev/null +++ b/tests/acceptance/filesForUpload/filesWithVirus/eicar-image.jpeg @@ -0,0 +1 @@ +X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*