Testing

Pyourls3 has a series of tests built with pytest, and flask to act as a simulated YOURLS endpoint. In order to run these tests, run the command pytest in the root directory of the project.

Contained at the base level of the project is an empty pytest.ini file, to ensure that pytest begins searching for tests from the base directory.

Structure

All tests are contained within the "tests" directory of the GitHub repository. The contents of this folder is set out as a Python package, to enable the webserver to be run with ease just by importing the package, as well as changing the current working directory to enable the tests to import the Pyourls3 package.

Automated testing

Pyourls3 makes use of [Travis CI] to provide automated testing for every commit to the master branch. It is configured to test across multiple versions of Python and multiple different operating systems. The tests run by Travis are those in the "tests" directory. When working on a branch or fork, these tests should be run locally on your host operating system before submitting a pull request. At this point, Travis will then test the contents of the pull request on all the operating systems and supported versions of Python. In case this fails, you should close your pull request, and make the required changes to your source code before then submitting a new pull request.

The webserver

Flask is the single greatest Python package that exists.

Upon initialisation of the tests package, an instance of the webserver is initialised, running on 127.0.0.1 and port 5000. This is created inside of a separate thread, with daemon set to true, meaning that it does not require any special input to make it stop. It just stops when everything else does. :)

The webserver is setup to take modifiers from tests in order to produce differing results for various tests. This is done using sharevar.py, an empty module bar two variables - last_request and modifier. Each of these should be defined before use, and given the value None. After each test, the value of modifier should be reset to None, and after each web request, the value of last_request will be all POST arguments passed in the request to the webserver, along with their values. standard_url is also contained here, which is constant and defines the address that requests should be made to.

The webserver itself is designed to respond like a YOURLS installation. It will respond with predefined JSON, depending on the modifier used. All requests should be pointed at /yourls-api.php, and as with the YOURLS API, the action should be defined in the action POST argument. The following modifiers are available for each action:

| Action    | garbledjson | urlerror | othererror | error | badauth | None |
|-----------|-------------|----------|------------|-------|---------|------|
| None      | No          | No       | No         | No    | Yes     | Yes  |
| shorturl  | Yes         | Yes      | Yes        | No    | No      | Yes  |
| expand    | Yes         | No       | No         | Yes   | No      | Yes  |
| stats     | Yes         | No       | No         | Yes   | No      | Yes  |
| url_stats | Yes         | No       | No         | Yes   | No      | Yes  |

If the modifier is set to None, a normal response will be generated, potentially missing some arguments that are not required for the package to function.

The function of each modifier is explained below:

  • garbledjson
    • returns invalid JSON, usually literally a string reading "this is not valid json"
  • urlerror
    • returns a reponse indicating that a redirect with the requested long URL already exists
  • othererror
    • returns a generic error-type thing
  • error
    • see above
  • badauth
    • Returns a 403 response, and appropriate JSON