def authenticate(fake_session): url = config.get_api_url() _credential = {"username": "******", "password": "******"} fake_session.post(f'{url}/api/credentials', json=_credential) fake_session.post(f'{url}/api/login', json=_credential)
def wait_for_webapp_to_come_up(): deadline = time.time() + 10 url = config.get_api_url() while time.time() < deadline: try: return requests.get(url) except ConnectionError: time.sleep(0.5) pytest.fail('API never came up')
def test_unhappy_path_delete_returns_404_and_message(postgres_session): url = config.get_api_url() fake_session = requests.Session() authenticate(fake_session) r = fake_session.delete(f'{url}/api/articles/an-article') assert r.status_code == 404 assert r.json()['message'] == 'Article not found.'
def test_happy_path_returns_201_and_creates_credential(postgres_session): url = config.get_api_url() _credential = {"username": "******", "password": "******"} r = requests.post(f'{url}/api/credentials', json=_credential) assert r.status_code == 201 assert r.json()['message'] == f'Credential created for user ' \ f'{_credential["username"]}'
def test_unhappy_path_returns_400_and_error_dict(postgres_session): url = config.get_api_url() _credential = {"username": "******", "password": "******"} r = requests.post(f'{url}/api/credentials', json=_credential) assert r.status_code == 400 assert r.json()['message'] == 'A strong password should contain at least' \ ' 8 characters, 1 digit, 1 symbol, 1' \ ' uppercase letter, and 1 lowercase letter'
def test_happy_path_post_returns_201_and_tag_name(postgres_session): url = config.get_api_url() fake_session = requests.Session() authenticate(fake_session) tag = {'tag_name': 'verbos'} r = fake_session.post(f'{url}/api/tags', json=tag) assert r.status_code == 201 assert r.json()['message'] == tag['tag_name']
def test_unhappy_path_post_returns_409_and_error_message(postgres_session): url = config.get_api_url() fake_session = requests.Session() authenticate(fake_session) tag = {'tag_name': 'verbos'} fake_session.post(f'{url}/api/tags', json=tag) r = fake_session.post(f'{url}/api/tags', json=tag) assert r.status_code == 409 assert r.json()['message'] == f'Tag {tag["tag_name"]} is duplicate'
def test_happy_path_returns_201_and_success_message_when_logging_succeeds( postgres_session): url = config.get_api_url() _credential = { "username": "******", "password": "******" } requests.post(f'{url}/api/credentials', json=_credential) r = requests.post(f'{url}/api/login', json=_credential) assert r.status_code == 200 assert r.json()['message'] == 'Logging successful!'
def test_happy_path_post_returns_201_and_article_title(postgres_session): url = config.get_api_url() fake_session = requests.Session() authenticate(fake_session) article = { "title": "An article", "description": "A remarkable description", "content": "Some very useful content", "tags": [], "category_id": 1 } r = fake_session.post(f'{url}/api/articles', json=article) assert r.status_code == 201 assert r.json()['message'] == 'An article'
def test_unhappy_path_post_returns_404_and_error_message(postgres_session): url = config.get_api_url() fake_session = requests.Session() authenticate(fake_session) article = { "title": "An article", "description": "A remarkable description", "content": "Some very useful content", "tags": [], "category_id": 1 } fake_session.post(f'{url}/api/articles', json=article) r = fake_session.post(f'{url}/api/articles', json=article) assert r.status_code == 404 assert r.json()['message'] == 'Can\'t create article. Title duplicate.'
def test_unhappy_path_returns_401_and_fail_message_when_username_is_wrong( postgres_session): url = config.get_api_url() _credential = { "username": "******", "password": "******" } fail_credential = { "username": "******", "password": "******" } requests.post(f'{url}/api/credentials', json=_credential) r = requests.post(f'{url}/api/login', json=fail_credential) assert r.status_code == 401 assert r.json()['message'] == 'Invalid credential. Username not found.'