This repository has been archived by the owner on Jan 29, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added in new v3 API tests (to be hooked up in CI later).
Updated CircleCI job to run v1 API tests in their new location
- Loading branch information
Showing
29 changed files
with
773 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,26 @@ | ||
import urllib3 | ||
|
||
# Disable any warnings about SSL connections | ||
urllib3.disable_warnings() | ||
import pytest | ||
import subprocess | ||
|
||
from faker import Faker | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def headers(): | ||
# Create a test user | ||
fake = Faker() | ||
email = fake.company_email() | ||
user = fake.user_name() | ||
|
||
# Add them as a superuser to the system running in Docker | ||
docker_compose = ( | ||
subprocess.check_output("which docker-compose", shell=True).decode("ascii").strip("\n") | ||
) | ||
subprocess.call( | ||
"{} run app python manage.py createsuperuser --noinput --email={} --user={}".format( | ||
docker_compose, email, user | ||
), | ||
shell=True, | ||
) | ||
|
||
# Return the authorization header that we need | ||
return {"Authorization": "Insecure {}".format(email)} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import urllib3 | ||
import uuid | ||
|
||
from faker import Faker | ||
from urllib.parse import urljoin | ||
|
||
|
||
def approve_approval_request(requests_session, server, approval_id, headers): | ||
return requests_session.post( | ||
urljoin(server, "/api/v3/approval_request/{}/approve/".format(approval_id)), | ||
data={"comment": "r+"}, | ||
headers=headers, | ||
) | ||
|
||
|
||
def create_approval_request(requests_session, server, latest_revision_id, headers): | ||
return requests_session.post( | ||
urljoin(server, "/api/v3/recipe_revision/{}/request_approval/".format(latest_revision_id)), | ||
headers=headers, | ||
) | ||
|
||
|
||
def create_new_user(requests_session, server, headers): | ||
# Get a list of groups and grab the ID of the first one | ||
response = requests_session.get(urljoin(server, "/api/v3/group/"), headers=headers) | ||
group_id = response.json()["results"][0]["id"] | ||
group_name = response.json()["results"][0]["name"] | ||
fake = Faker() | ||
|
||
# Create a user, assigning them to the group we obtained | ||
user_data = { | ||
"first_name": fake.first_name(), | ||
"last_name": fake.last_name(), | ||
"email": fake.company_email(), | ||
"groups": {"id": group_id, "name": group_name}, | ||
} | ||
response = requests_session.post( | ||
urljoin(server, "/api/v3/user/"), headers=headers, data=user_data | ||
) | ||
|
||
return { | ||
"id": response.json()["id"], | ||
"first_name": response.json()["first_name"], | ||
"last_name": response.json()["last_name"], | ||
"group_id": group_id, | ||
} | ||
|
||
|
||
def enable_recipe(requests_session, server, recipe_id, headers): | ||
return requests_session.post( | ||
urljoin(server, "/api/v3/recipe/{}/enable/".format(recipe_id)), headers=headers | ||
) | ||
|
||
|
||
def new_recipe(requests_session, action_id, server, headers): | ||
urllib3.disable_warnings() | ||
|
||
# Create a recipe | ||
recipe_data = { | ||
"action_id": action_id, | ||
"arguments": '{"learnMoreMessage":"This field may not be blank.","learnMoreUrl":"This field may not be blank.","message":"This field may not be blank.","postAnswerUrl":"This field may not be blank.","surveyId":"' | ||
+ str(uuid.uuid4()) | ||
+ '","thanksMessage":"This field may not be blank."}', | ||
"name": "test recipe", | ||
"extra_filter_expression": "counter == 0", | ||
"enabled": "false", | ||
} | ||
|
||
response = requests_session.post( | ||
urljoin(server, "/api/v3/recipe/"), data=recipe_data, headers=headers | ||
) | ||
data = response.json() | ||
return {"id": data["id"], "latest_revision_id": data["latest_revision"]["id"]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from support.helpers import new_recipe | ||
from support.assertions import assert_valid_schema | ||
from urllib.parse import urljoin | ||
|
||
|
||
def test_approval_request(conf, requests_session, headers): | ||
# Get a list of actions and pick the first one | ||
action_response = requests_session.get( | ||
urljoin(conf.getoption("server"), "/api/v3/action/"), headers=headers | ||
) | ||
data = action_response.json() | ||
action_id = data["results"][0]["id"] | ||
|
||
# Then create an approval request and submit it | ||
recipe_details = new_recipe(requests_session, action_id, conf.getoption("server"), headers) | ||
response = requests_session.post( | ||
urljoin( | ||
conf.getoption("server"), | ||
"/api/v3/recipe_revision/{}/request_approval/".format( | ||
recipe_details["latest_revision_id"] | ||
), | ||
), | ||
headers=headers, | ||
) | ||
data = response.json() | ||
approval_id = data["id"] | ||
approver_email = data["creator"]["email"] | ||
assert response.status_code != 404 | ||
assert_valid_schema(response.json()) | ||
|
||
# Verify that the request was actually created | ||
response = requests_session.get( | ||
urljoin(conf.getoption("server"), "/api/v3/approval_request/{}".format(approval_id)) | ||
) | ||
data = response.json() | ||
assert response.status_code != 404 | ||
assert_valid_schema(response.json()) | ||
assert data["id"] == approval_id | ||
assert data["creator"]["email"] == approver_email | ||
|
||
# Approve the approval request | ||
response = requests_session.post( | ||
urljoin( | ||
conf.getoption("server"), "/api/v3/approval_request/{}/approve/".format(approval_id) | ||
), | ||
data={"comment": "r+"}, | ||
headers=headers, | ||
) | ||
assert response.status_code != 404 | ||
assert_valid_schema(response.json()) | ||
|
||
# Look at the recipe and make sure that the recipe has been approved and our comment shows up | ||
response = requests_session.get( | ||
urljoin(conf.getoption("server"), "/api/v3/recipe/{}".format(recipe_details["id"])) | ||
) | ||
assert response.status_code not in (404, 500) | ||
assert_valid_schema(response.json()) | ||
approval_request = response.json()["latest_revision"]["approval_request"] | ||
assert approval_request["approved"] is True | ||
assert approval_request["comment"] == "r+" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from support.assertions import assert_valid_schema | ||
from support.helpers import new_recipe | ||
from urllib.parse import urljoin | ||
|
||
|
||
def test_approval_request_close(conf, requests_session, headers): | ||
# Get an action we can work with | ||
action_response = requests_session.get( | ||
urljoin(conf.getoption("server"), "/api/v3/action/"), headers=headers | ||
) | ||
data = action_response.json() | ||
action_id = data["results"][0]["id"] | ||
|
||
# Create a recipe | ||
recipe_details = new_recipe(requests_session, action_id, conf.getoption("server"), headers) | ||
|
||
# Create an approval request | ||
response = requests_session.post( | ||
urljoin( | ||
conf.getoption("server"), | ||
"/api/v3/recipe_revision/{}/request_approval/".format( | ||
recipe_details["latest_revision_id"] | ||
), | ||
), | ||
headers=headers, | ||
) | ||
data = response.json() | ||
approval_id = data["id"] | ||
assert response.status_code != 404 | ||
assert_valid_schema(response.json()) | ||
|
||
# Close the approval request | ||
response = requests_session.post( | ||
urljoin( | ||
conf.getoption("server"), "/api/v3/approval_request/{}/close/".format(approval_id) | ||
), | ||
headers=headers, | ||
) | ||
assert response.status_code == 204 | ||
|
||
# Verify that is no longer exists | ||
response = requests_session.get( | ||
urljoin(conf.getoption("server"), "/api/v3/approval_request/{}/".format(approval_id)), | ||
headers=headers, | ||
) | ||
assert response.status_code == 404 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from support.assertions import assert_valid_schema | ||
from support.helpers import new_recipe | ||
from urllib.parse import urljoin | ||
|
||
|
||
def test_approval_request_reject(conf, requests_session, headers): | ||
# Get an action we can work with | ||
action_response = requests_session.get( | ||
urljoin(conf.getoption("server"), "/api/v3/action/"), headers=headers | ||
) | ||
data = action_response.json() | ||
action_id = data["results"][0]["id"] | ||
|
||
# Create a recipe associated with that action | ||
recipe_details = new_recipe(requests_session, action_id, conf.getoption("server"), headers) | ||
|
||
# Create a approval request | ||
response = requests_session.post( | ||
urljoin( | ||
conf.getoption("server"), | ||
"/api/v3/recipe_revision/{}/request_approval/".format( | ||
recipe_details["latest_revision_id"] | ||
), | ||
), | ||
headers=headers, | ||
) | ||
data = response.json() | ||
approval_id = data["id"] | ||
assert response.status_code != 404 | ||
assert_valid_schema(response.json()) | ||
|
||
# Reject the approval | ||
response = requests_session.post( | ||
urljoin( | ||
conf.getoption("server"), "/api/v3/approval_request/{}/reject/".format(approval_id) | ||
), | ||
data={"comment": "r-"}, | ||
headers=headers, | ||
) | ||
assert response.status_code == 200 | ||
assert_valid_schema(response.json()) | ||
|
||
# Look at the recipe and make sure it the approval status has been set to False and our comment shows up | ||
response = requests_session.get( | ||
urljoin(conf.getoption("server"), "/api/v3/recipe/{}/".format(recipe_details["id"])) | ||
) | ||
assert response.status_code != 404 | ||
assert_valid_schema(response.json()) | ||
approval_request = response.json()["latest_revision"]["approval_request"] | ||
assert approval_request["approved"] is False | ||
assert approval_request["comment"] == "r-" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import uuid | ||
|
||
from support.assertions import assert_valid_schema | ||
from urllib.parse import urljoin | ||
|
||
|
||
def test_group_create(conf, requests_session, headers): | ||
# Create a new group | ||
data = {"name": str(uuid.uuid4())} | ||
response = requests_session.post( | ||
urljoin(conf.getoption("server"), "/api/v3/group/"), headers=headers, data=data | ||
) | ||
assert response.status_code == 201 | ||
assert_valid_schema(response.json()) | ||
group_data = response.json() | ||
group_id = group_data["id"] | ||
|
||
# Verify group was stored and contains expected data | ||
response = requests_session.get( | ||
urljoin(conf.getoption("server"), "/api/v3/group/{}/".format(group_id)), headers=headers | ||
) | ||
group_data = response.json() | ||
assert response.status_code == 200 | ||
assert_valid_schema(response.json()) | ||
assert group_data["id"] == group_id |
Oops, something went wrong.