# python-qa-testing-utils

A collection of utilities for use in testing jobs in CI.

## fetch_oa_grass_token

Fetches a grass token for the `automation_qa` OA user from the QA instance of Grass.

The command requires that the following environment variables be set:
- `QA_MYSQL_USER`: a user with read access to the QA `art_relations` db.
- `QA_MYSQL_PASSWORD`: the user's password.

Usage:
```
fetch_oa_grass_token
```
Output:
```
GRASS_TOKEN=<...fetched grass token...>
```

You can pipe this output into a `.properties` file that can be used by a subsequent build step.

## fetch_oa_vapi_creds

Fetches the VAPI token, user id, and client id for the `automation_qa` OA user.

The command requires that the following environment variables be set:
- `QA_MYSQL_USER`: a user with read access to the QA `art_relations` db.
- `QA_MYSQL_PASSWORD`: the user's password.

Usage:
```
fetch_oa_vapi_creds
```
Output:
```
VAPI_TOKEN=<...VAPI token...>
OA_USER_ID=<...user id...>
CLIENT_ID=<...client id...>
```

You can pipe this output into a `.properties` file that can be used by a subsequent build step.

## patience

This command polls a list of services and waits for healthy responses from each of them. 
The number of times it tries defaults to 120, but can be overridden. The polling interval is 1 second.
If all services respond within the specified number of tries, the command exits with a status of 0, otherwise 
it throws an exception and exits with a status of 1.

A typical use would be to wait for services in a local Docker Compose cluster to 
be up before running tests against them in CI.

Example usage:
```
patience --services tcp://db:5432 http://localhost:8080 https://somewhereelse.io:8085/hello --tries 200
```
Example output:

```
Trying tcp://db:5432... SUCCEEDED
Trying http://localhost:8080... UNAVAILABLE
Trying https://somewhereelse.io:8085/hello... UNAVAILABLE
Trying http://localhost:8080... UNAVAILABLE
Trying https://somewhereelse.io:8085/hello... UNAVAILABLE
Trying http://localhost:8080... SUCCEEDED
Trying https://somewhereelse.io:8085/hello... UNAVAILABLE
Trying https://somewhereelse.io:8085/hello... UNAVAILABLE
Trying https://somewhereelse.io:8085/hello... UNAVAILABLE
Trying https://somewhereelse.io:8085/hello... UNAVAILABLE
Trying https://somewhereelse.io:8085/hello... SUCCEEDED
```

Currently, only `tcp`, `http`, and `https` are supported as schemes.

## Generating graphQl mock queries

To generate mock queries first ensure the `user_data.json` file is donwloaded from aws:

```make download_user_data```

Checkout the desired app into this repo and rename the folder `graphQl-refresh-app`

If you wish to output to a local cucumber-cypress-tests you can export the below env variable, otherwise files will go to the output folder.

```export CYPRESS_CUKES_REPO_PATH=/path/to/cucumber-cypress-tests```

To generate the queries run the below, subbing in the app into the variable, e.g insights is used here:

```make refresh_graphQl_mocks app=insights```

# Adding new apps and queries

To add a new app, first add it to `application_headers.json` and fill in the default headers you need.

Add the app to `application_operations.json` and add desired operations, linking them to the `.gql` filename of the query from the app repo

Create an app variables file following the naming convention `[app]-variables.json` and add the desired variables

In some cases where the source gql files are not from the specific app (e.g. orchard-suite) you can add an additional `source_app` argument to utilise the base apps headers and users, but outputting to a separate output folder.
If no `source_app` is passed it is assumed the `app` is also the `source_app`:

```make refresh_graphQl_mocks app=insights source_app=orchard-suite```

# Uploading mocks to s3

To upload mocks from the output folder to s3, use the below makefile command, subbing in the app into the variable:

``` make upload_graphQl_responses_to_s3 app=insights```