import pytest from flask import g, json, url_for, request from sw.db import get_db from sw.auth import login_required, specific_user_required, json_input from werkzeug.datastructures import Headers def test_register(client, app): response = client.post( "/auth/register", data=json.dumps({"username": "a", "password": "a",}), content_type="application/json", ) # TODO assert "/u/a" in response.headers["Location"] with app.app_context(): db_result = ( get_db().execute("select * from users where username='a'",).fetchone() ) assert db_result is not None @pytest.mark.parametrize( ("username", "password", "message"), ( (None, "", b"Username required"), ("", "", b"Username must not be empty"), ("a", None, b"Password required"), ("a", "", b"Password must not be empty"), ("test", "test", b"Username test is taken"), ), ) def test_register_validate_input(client, username, password, message): response = client.post( "/auth/register", data=json.dumps({"username": username, "password": password}), headers={"Content-Type": "application/json"}, ) assert message in response.data @pytest.mark.parametrize( ("username", "password", "expected_status_code"), ( ("test", "test", 200), ("test", "other", 403), ("test", None, 403), (None, "test", 403), ), ) def test_login(client, username, auth, password, expected_status_code): response = auth.login(username=username, password=password) assert response.status_code == expected_status_code def test_login_required_no_user(client, app): test_endpoint = "/test/dummy_endpoint" @app.route(test_endpoint) @login_required def dummy_endpoint(): return "Should be unreachable", 200 response = client.get(test_endpoint) assert response.status_code == 403 def test_login_required_with_user(client, app, auth): test_endpoint = "/test/dummy_endpoint" @app.route(test_endpoint) @login_required def dummy_endpoint(): return "Should succeed", 200 auth.login() with client: response = client.get(test_endpoint) assert response.status_code == 200 assert b"Should succeed" in response.data def test_specific_user_required_no_user(client, app): test_endpoint = "/test/dummy_endpoint/" @app.route(test_endpoint) @specific_user_required def dummy_endpoint(username): return "Should be unreachable", 200 response = client.get("/test/dummy_endpoint/test") assert response.status_code == 403 assert b"Should be unreachable" not in response.data def test_specific_user_required_wrong_user(client, app, auth): test_endpoint = "/test/dummy_endpoint/" @app.route(test_endpoint) @specific_user_required def dummy_endpoint(username): return "Should be unreachable", 200 auth.login() with client: response = client.get("/test/dummy_endpoint/other") assert response.status_code == 403 assert b"Should be unreachable" not in response.data def test_specific_user_required_right_user(client, app, auth): test_endpoint = "/test/dummy_endpoint/" @app.route(test_endpoint) @specific_user_required def dummy_endpoint(username): return "Should succeed", 200 auth.login() with client: response = client.get("/test/dummy_endpoint/test") assert response.status_code == 200 assert b"Should succeed" in response.data @pytest.mark.parametrize( ("input_data", "content_type", "expected_response"), ( (None, None, 422), ("", None, 422), ("invalid JSON string", None, 422), ('{ "valid": "JSON"}', None, 422), (None, "", 422), ("", "", 422), ("invalid JSON string", "", 422), ('{ "valid": "JSON"}', "", 422), (None, "application/json", 400), ("", "application/json", 400), ("invalid JSON string", "application/json", 400), ('{ "valid": "JSON"}', "application/json", 201), (None, "application/ld+json", 400), ("", "application/ld+json", 400), ("invalid JSON string", "application/ld+json", 400), ('{ "valid": "JSON"}', "application/ld+json", 201), ), ) def test_json_input(client, app, input_data, content_type, expected_response): test_endpoint = "/test/dummy_endpoint" @app.route(test_endpoint, methods=["POST"]) @json_input def dummy_endpoint(): data = request.json return data, 201 with client: response = client.post( test_endpoint, data=input_data, headers={"Content-Type": content_type} ) assert response.status_code == expected_response