Skip to content

Instantly share code, notes, and snippets.

@larsks
Last active December 14, 2021 23:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save larsks/99a8c7ef3d0ba0c1d57e024fb0f25de7 to your computer and use it in GitHub Desktop.
Save larsks/99a8c7ef3d0ba0c1d57e024fb0f25de7 to your computer and use it in GitHub Desktop.
import logging
import os
import pytest
import requests
ENDPOINT = os.environ.get("ACCT_MGR_API_ENDPOINT", "http://localhost:8080")
ADMIN_PASSWORD = os.environ.get("ACCT_MGR_ADMIN_PASSWORD", "pass")
NS = os.environ.get("ACCT_MGR_NAMESPACE", "onboarding")
logging.basicConfig(level="DEBUG")
@pytest.fixture
def session():
"""Create a requests.Session configure with appropriate credentials"""
s = requests.Session()
s.auth = ("admin", ADMIN_PASSWORD)
s.verify = False
return s
@pytest.fixture
def a_user(session):
"""Create a user test-user and remove it afterwards"""
# First assert that the user *does not* exist
res = session.get(f"{ENDPOINT}/users/test-user")
assert res.status_code == 400
# Create the user and assert that the operation is successful
res = session.put(f"{ENDPOINT}/users/test-user")
assert res.status_code == 200
# Return the fixture to the caller
yield res
# Delete the user and assert the operation is successful
res = session.delete(f"{ENDPOINT}/users/test-user")
assert res.status_code == 200
# Verify that the user has been deleted
res = session.get(f"{ENDPOINT}/users/test-user")
assert res.status_code == 400
def test_user(a_user, session):
"""Verify that we can fetch information about an existing user
and get the expected response"""
res = session.get(f"{ENDPOINT}/users/test-user")
assert res.status_code == 200
data = res.json()
assert data["msg"] == "user (test-user) exists"
def test_user_exists(a_user, session):
"""Verify that we can't create a user if one with the same name
already exists."""
res = session.put(f"{ENDPOINT}/users/test-user")
assert res.status_code == 400
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment