Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[QA] Check and remove the use of setResponse() and $this->response in the Given/Then steps #7082

Closed
5 of 6 tasks
saw-jan opened this issue Aug 21, 2023 · 14 comments
Closed
5 of 6 tasks
Assignees
Labels

Comments

@saw-jan
Copy link
Member

saw-jan commented Aug 21, 2023

We have used setResponse() and $this->response in the Given/Then steps and some helper functions (maybe to reuse existing available methods). But storing responses from Given/Then steps and helper functions is not a good idea because it can lead to a false positive assertion in the Then steps.

For example,

Scenario: upload file and no version is available using various chunking methods (except new chunking)
When user "Alice" uploads file "filesForUpload/davtest.txt" to filenames based on "/davtest.txt" with all mechanisms except new chunking using the WebDAV API
Then the HTTP status code should be "200"
And the version folder of file "/davtest.txt-olddav-regular" for user "Alice" should contain "0" elements

Here, the When step is upload resource and Then step checks that the When step's response code is 200 which is wrong here but the test passes. This is because of the saved response from Given step.

Open tasks:

  • check the use of setResponse() and $this->response in
    • Given steps
    • Then steps (Then steps can use $this->response but must prevent saving to it)
    • Helper functions
  • Prevent use of step-def functions in other steps-defs wherever possible (strictly prevent the reuse between cross steps).
  • Refactor theHTTPStatusCodeShouldBe() so that it can accept response object to get statuscode from [tests-only][full-CI] make httpresponse check function able to accept response object as parameter #7169
    theHTTPStatusCodeShouldBe($response)
    
@amrita-shrestha
Copy link
Contributor

amrita-shrestha commented Aug 22, 2023

maybe this old issue help owncloud/core#39512 and owncloud/QA#517

In the past, we emptied HTTP status code in the Given step to prevent such incidents. If that didn't prevent false positive then we have to discuss and brainstorm different ways. cc @saw-jan @SwikritiT @SagarGi @grgprarup @PrajwolAmatya

	/**
	 * @When user :user uploads file :source to :destination using the WebDAV API
	 *
	 * @param string $user
	 * @param string $source
	 * @param string $destination
	 *
	 * @return void
	 */
	public function userUploadsAFileTo(string $user, string $source, string $destination):void {
		$user = $this->getActualUsername($user);
		$file = \fopen($this->acceptanceTestsDirLocation() . $source, 'r');
		$this->pauseUploadDelete();
		$this->response = $this->makeDavRequest(
			$user,
			"PUT",
			$destination,
			[],
			$file
		);
		$this->lastUploadDeleteTime = \time();
		$this->setResponseXml(
			HttpRequestHelper::parseResponseAsXml($this->response)
		);
		$this->pushToLastHttpStatusCodesArray(
			(string) $this->getResponse()->getStatusCode()
		);
	}

	/**
	 * @Given user :user has uploaded file :source to :destination
	 *
	 * @param string $user
	 * @param string $source
	 * @param string $destination
	 *
	 * @return void
	 */
	public function userHasUploadedAFileTo(string $user, string $source, string $destination):void {
		$this->userUploadsAFileTo($user, $source, $destination);
		$this->theHTTPStatusCodeShouldBe(
			["201", "204"],
			"HTTP status code was not 201 or 204 while trying to upload file '$source' to '$destination' for user '$user'"
		);
		$this->emptyLastHTTPStatusCodesArray();
	}

@saw-jan
Copy link
Member Author

saw-jan commented Aug 22, 2023

Never save responses in Given steps. Let the response scope be only within the Given steps. That way we don't have to worry about clearing the response/statuscode stores.

@amrita-shrestha
Copy link
Contributor

so there is two tasks here

  • remove the response saved by setResponse()
  • clean previously saved HTTP step by pushToLastHttpStatusCodesArray()

Am I correct @saw-jan

@saw-jan
Copy link
Member Author

saw-jan commented Aug 22, 2023

If those are done in Given/Then steps or helper functions then yes. But if you mean everywhere in the test code then no.
setResponse() and pushToLastHttpStatusCodesArray() should not be used in Given/Then and helper functions

@amrita-shrestha
Copy link
Contributor

recently something catches my attention we call When step function in Given step. So Given and When are interlinked so how could we remove setResponse(). cc @saw-jan

@saw-jan
Copy link
Member Author

saw-jan commented Aug 22, 2023

Yeah, that should be part of this ticket.
we should not and never use step-def functions in other steps-defs wherever possible (strictly not between cross steps).
What we can do in that case:

  • create a helper function
  • use the helper function in the step definition function

@phil-davis
Copy link
Contributor

use the helper function in the step definition function

yes, I agree. We did this sort of pattern in quite a lot of places already. Forexample, I see in FavoritesContext (happens to be in core, but similar examples will be in ocis test code:

	public function userFavoritesElement(string $user, string $path):void {
		$response = $this->changeFavStateOfAnElement(
			$user,
			$path,
			1
		);
		$this->featureContext->setResponse($response);
	}
	/**
	 * @When user :user favorites element :path using the WebDAV API
	 */
	public function userFavoritesElementUsingWebDavApi(string $user, string $path):void {
		$this->userFavoritesElement($user, $path);
	}
	/**
	 * @Given user :user has favorited element :path
	 */
	public function userHasFavoritedElementUsingWebDavApi(string $user, string $path):void {
		$this->userFavoritesElement($user, $path);
		$this->featureContext->theHTTPStatusCodeShouldBeSuccess();
	}

The setResponse call can be moved into the When step code, so that only the When step remembers the response.

@saw-jan
Copy link
Member Author

saw-jan commented Aug 25, 2023

If you go on refactoring Given steps and helper functions then you will know which test suites are affected. So, IMO, this ticket cannot be divided by test suites. But I am just letting you know. You can continue your way.

@KarunAtreya
Copy link
Contributor

If you go on refactoring Given steps and helper functions then you will know which test suites are affected. So, IMO, this ticket cannot be divided by test suites. But I am just letting you know. You can continue your way.

should i go context wise then?

@saw-jan
Copy link
Member Author

saw-jan commented Aug 29, 2023

should i go context wise then?

maybe yes.

@KarunAtreya
Copy link
Contributor

KarunAtreya commented Sep 4, 2023

TODO:

Master

stable-4.0

@SwikritiT
Copy link
Contributor

SwikritiT commented Sep 6, 2023

@KarunAtreya I've updated the above checklist to include the backport checklist as well. Can you please backport your related PRs and link them there

@nirajacharya2
Copy link
Contributor

nirajacharya2 commented Dec 15, 2023

Prevent use of step-def functions in other steps-defs wherever possible (strictly prevent the reuse between cross steps).
TODO:

Master

  • AppConfigurationContext
  • ArchiverContext
  • AuthContext
  • CapabilitiesContext
  • ChecksumContext
  • FavoritesContext
  • FeatureContext
  • FilesVersionsContext
  • GraphContext
  • NotificationContext
  • OcisConfigContext
  • OCSContext
  • Provisioning
  • PublicWebDavContext
  • SearchContext
  • SettingContext
  • ShareesContext
  • SharingContext
  • SpacesContext
  • SpacesTUSContext
  • TagContext
  • TrashbinContext
  • TUSContext
  • WebDav
  • WebDavLockingContext
  • WebDavPropertiesContext

stable-5.0

  • AppConfigurationContext
  • ArchiverContext
  • AuthContext
  • CapabilitiesContext
  • ChecksumContext
  • FavoritesContext
  • FeatureContext
  • FilesVersionsContext
  • GraphContext
  • NotificationContext
  • OcisConfigContext
  • OCSContext
  • Provisioning
  • PublicWebDavContext
  • SearchContext
  • SettingContext
  • ShareesContext
  • SharingContext
  • SpacesContext
  • SpacesTUSContext
  • TagContext
  • TrashbinContext
  • TUSContext
  • WebDav
  • WebDavLockingContext
  • WebDavPropertiesContext

@Salipa-Gurung
Copy link
Contributor

Prevent use of step-def functions in other steps-defs wherever possible (strictly prevent the reuse between cross steps).
We will continue to work on this in next issue #8199

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

8 participants