import pytest from hypothesis import given, provisional, settings from hypothesis import strategies as st from ralph.models.converter import convert_dict_event, convert_str_event from ralph.models.edx.base import BaseContextField from ralph.models.edx.converters.xapi.server import ServerEventToPageViewed from ralph.models.edx.server import ServerEvent, ServerEventField @settings(max_examples=1) @given( st.builds(ServerEvent, event=st.builds(ServerEventField), referer=provisional.urls()), provisional.urls(), ) @pytest.mark.parametrize("uuid_namespace", ["ee241f8b-174f-5bdb-bae9-c09de5fe017f"]) def test_models_edx_converters_xapi_server_server_event_to_xapi_convert_constant_uuid( uuid_namespace, event, platform_url): """Tests that ServerEventToPageViewed.convert returns a JSON string with a constant UUID.""" event_str = event.json() event = json.loads(event_str) xapi_event1 = convert_str_event( event_str, ServerEventToPageViewed(uuid_namespace, platform_url)) xapi_event2 = convert_dict_event( event, event_str, ServerEventToPageViewed(uuid_namespace, platform_url))
multiple_params=[], csv_params=[]) @example(params=["--hypothesis-max-examples=0"], flags=[], multiple_params=[], csv_params=[]) @pytest.mark.usefixtures("mocked_schema") def test_valid_parameters_combos(cli, schema_url, params, flags, multiple_params, csv_params): result = cli.run(schema_url, *params, *multiple_params, *flags, *csv_params) check_result(result) @settings(deadline=None) @given(schema=urls() | paths() | st.text(), base_url=urls() | paths() | st.text() | st.none()) @example(schema="//bla", base_url=None) @pytest.mark.usefixtures("mocked_schema") def test_schema_validity(cli, schema, base_url): args = () if base_url: args = (f"--base-url={base_url}", ) result = cli.run(schema, *args) check_result(result) def check_result(result): assert not (result.exception and not isinstance(result.exception, SystemExit)), result.stdout
# v. 2.0. If a copy of the MPL was not distributed with this file, You can # obtain one at https://mozilla.org/MPL/2.0/. # # END HEADER from __future__ import absolute_import, division, print_function import re import string from binascii import unhexlify from hypothesis import given from hypothesis.provisional import ip4_addr_strings, ip6_addr_strings, urls @given(urls()) def test_is_URL(url): allowed_chars = set(string.ascii_letters + string.digits + "$-_.+!*'(),%/") url_schemeless = url.split("://", 1)[1] path = url_schemeless.split("/", 1)[1] if "/" in url_schemeless else "" assert all(c in allowed_chars for c in path) assert all( re.match("^[0-9A-Fa-f]{2}", after_perc) for after_perc in path.split("%")[1:] ) @given(ip4_addr_strings()) def test_is_IP4_addr(address): as_num = [int(n) for n in address.split(".")] assert len(as_num) == 4 assert all(0 <= n <= 255 for n in as_num)
import json import pytest from hypothesis import given, provisional, settings from hypothesis import strategies as st from ralph.exceptions import UnknownEventException from ralph.models.edx.server import ServerEvent, ServerEventField from ralph.models.selector import ModelSelector @settings(max_examples=1) @given( st.builds( ServerEvent, referer=provisional.urls(), event=st.builds(ServerEventField) ) ) def test_model_selector_server_event_get_model_with_valid_event(event): """Tests given a server event, the get_model method should return the corresponding model.""" event = json.loads(event.json()) assert ModelSelector(module="ralph.models.edx").get_model(event) is ServerEvent def test_model_selector_server_event_get_model_with_invalid_event(): """Tests given a server event, the get_model method should raise UnknownEventException.""" with pytest.raises(UnknownEventException): ModelSelector(module="ralph.models.edx").get_model({"invalid": "event"})
# # END HEADER import re import string from binascii import unhexlify import pytest from hypothesis import given from hypothesis.errors import InvalidArgument from hypothesis.provisional import domains, ip4_addr_strings, ip6_addr_strings, urls from tests.common.debug import find_any @given(urls()) def test_is_URL(url): allowed_chars = set(string.ascii_letters + string.digits + "$-_.+!*'(),%/") url_schemeless = url.split("://", 1)[1] path = url_schemeless.split("/", 1)[1] if "/" in url_schemeless else "" assert all(c in allowed_chars for c in path) assert all( re.match("^[0-9A-Fa-f]{2}", after_perc) for after_perc in path.split("%")[1:] ) @given(ip4_addr_strings()) def test_is_IP4_addr(address): as_num = [int(n) for n in address.split(".")] assert len(as_num) == 4 assert all(0 <= n <= 255 for n in as_num)
return inner # Mapping of field types, to strategy objects or functions of (type) -> strategy _global_field_lookup = { dm.SmallIntegerField: integers_for_field(-32768, 32767), dm.IntegerField: integers_for_field(-2147483648, 2147483647), dm.BigIntegerField: integers_for_field(-9223372036854775808, 9223372036854775807), dm.PositiveIntegerField: integers_for_field(0, 2147483647), dm.PositiveSmallIntegerField: integers_for_field(0, 32767), dm.BooleanField: st.booleans(), dm.DateField: st.dates(), dm.EmailField: emails(), dm.FloatField: st.floats(), dm.NullBooleanField: st.one_of(st.none(), st.booleans()), dm.URLField: urls(), dm.UUIDField: st.uuids(), df.DateField: st.dates(), df.DurationField: st.timedeltas(), df.EmailField: emails(), df.FloatField: lambda field: st.floats( *numeric_bounds_from_validators(field), allow_nan=False, allow_infinity=False ), df.IntegerField: integers_for_field(-2147483648, 2147483647), df.NullBooleanField: st.one_of(st.none(), st.booleans()), df.URLField: urls(), df.UUIDField: st.uuids(), } # type: Dict[Type[AnyField], Union[st.SearchStrategy, Callable[[Any], st.SearchStrategy]]] _ipv6_strings = st.one_of( st.ip_addresses(v=6).map(str),
import quarchive as sut from datetime import datetime import pytest from hypothesis import given from hypothesis import strategies as st from hypothesis.provisional import urls from .conftest import make_bookmark UrlStrategy = st.shared(urls()) TagStrategy = st.one_of(st.just("tag_a"), st.just("tag_b"), st.just("tag_c")) @st.composite def tag_triples(draw, tags=TagStrategy, dts=st.datetimes(), bools=st.booleans()): as_list = draw( st.lists(st.tuples(tags, dts, bools), unique_by=lambda tt: tt[0])) return frozenset(as_list) BookmarkStrategy = st.builds( sut.Bookmark, url=UrlStrategy, created=st.datetimes(), deleted=st.booleans(), description=st.text(),
dm.PositiveIntegerField: integers_for_field(0, 2147483647), dm.PositiveSmallIntegerField: integers_for_field(0, 32767), dm.BooleanField: st.booleans(), dm.DateField: st.dates(), dm.EmailField: emails(), dm.FloatField: st.floats(), dm.NullBooleanField: st.one_of(st.none(), st.booleans()), dm.URLField: urls(), dm.UUIDField: st.uuids(), df.DateField: st.dates(), df.DurationField: st.timedeltas(), df.EmailField: emails(), df.FloatField: lambda field: st.floats(*numeric_bounds_from_validators(field), allow_nan=False, allow_infinity=False), df.IntegerField: integers_for_field(-2147483648, 2147483647), df.NullBooleanField:
from hypothesis import strategies as st from ralph.models.xapi.fields.actors import ActorAccountField, ActorField from ralph.models.xapi.navigation.fields.objects import PageObjectField from ralph.models.xapi.navigation.statements import PageTerminated, PageViewed @settings(max_examples=1) @given( st.builds( PageTerminated, actor=st.builds( ActorField, account=st.builds(ActorAccountField, name=st.just("username"), homePage=provisional.urls()), ), object=st.builds(PageObjectField, id=provisional.urls()), )) def test_models_xapi_page_terminated_statement(statement): """Tests that a page_terminated statement has the expected verb.id and object.definition.""" assert statement.verb.id == "http://adlnet.gov/expapi/verbs/terminated" assert statement.object.definition.type == "http://activitystrea.ms/schema/1.0/page" @settings(max_examples=1) @given( st.builds( PageViewed, actor=st.builds(
import json import re import pytest from hypothesis import given, provisional, settings from hypothesis import strategies as st from pydantic.error_wrappers import ValidationError from ralph.models.edx.base import BaseContextField, BaseEvent @settings(max_examples=1) @given( st.builds(BaseEvent, context=st.builds(BaseContextField), referer=provisional.urls())) def test_models_edx_base_event_with_valid_content(event): """Tests that a valid base event does not raise a ValidationError.""" assert len(event.username) == 0 or (len(event.username) in range(2, 31, 1)) assert (re.match(r"^course-v1:.+\+.+\+.+$", event.context.course_id) or event.context.course_id == "") @pytest.mark.parametrize( "course_id,error", [ ( "course-v1:+course+not_empty", "course_id\n string does not match regex", ),
if isinstance(result, list): result = result[0] assert result == expected def test_inline_many_wrong_type(): with pytest.raises(TypeError): css_inline.inline_many([1]) def test_invalid_base_url(): with pytest.raises(ValueError): css_inline.CSSInliner(base_url="foo") @given( document=st.text(), remove_style_tags=st.booleans(), base_url=provisional.urls(), load_remote_stylesheets=st.booleans(), ) @settings(max_examples=1000) def test_random_input(document, remove_style_tags, base_url, load_remote_stylesheets): with suppress(ValueError): inliner = css_inline.CSSInliner( remove_style_tags=remove_style_tags, base_url=base_url, load_remote_stylesheets=load_remote_stylesheets) inliner.inline(document)
"""""" from typing import Iterable, TypeVar from hypothesis import given import hypothesis.strategies as st from hypothesis import given import hypothesis.provisional as stp from .context import helpers as hf from collections.abc import Iterator from itertools import tee T = TypeVar("T") @given(stp.urls()) def test_is_valid_url_valid_example(url: str): assert hf.is_valid_url(url) == True @given(st.text()) def test_is_valid_url_invalid_example(url: str): assert hf.is_valid_url(url) == False @given(st.iterables(st.integers(), unique=True, min_size=1)) def test_tail_only_removes_first_item(iterable: Iterable[T]): # If iterable is an iterator then it will be consumed so we need a copy iterable1, iterable2 = tee(iterable) # Force to list for easy testing iterable1 = list(iterable1) # type: ignore
from hypothesis import provisional from amqtt.session import Session from amqtt import utils @given(st.text()) def test_format_client_message(client_id): test_session = Session() test_session.client_id = client_id client_message = utils.format_client_message(session=test_session) assert client_message == f"(client id={client_id})" @given(provisional.urls(), st.integers()) def test_format_client_message_valid(url, port): client_message = utils.format_client_message(address=url, port=port) assert client_message == f"(client @={url}:{port})" def test_format_client_message_unknown(): client_message = utils.format_client_message() assert client_message == "(unknown client)" def test_client_id(): client_id = utils.gen_client_id() assert isinstance(client_id, str) assert client_id.startswith("amqtt/")
def test_inline_many_wrong_type(): with pytest.raises(TypeError): css_inline.inline_many([1]) def test_invalid_base_url(): with pytest.raises(ValueError): css_inline.CSSInliner(base_url="foo") @given( document=st.text(), inline_style_tags=st.booleans() | st.none(), remove_style_tags=st.booleans() | st.none(), base_url=provisional.urls() | st.none(), load_remote_stylesheets=st.booleans() | st.none(), extra_css=st.text() | st.none(), ) @settings(max_examples=1000) def test_random_input( document, inline_style_tags, remove_style_tags, base_url, load_remote_stylesheets, extra_css, ): with suppress(ValueError): inliner = css_inline.CSSInliner( inline_style_tags=inline_style_tags,
capsys, monkeypatch): monkeypatch.delenv('APIKEY', raising=False) with pt.raises(SystemExit): get_api_key() _, err = capsys.readouterr() assert err == "'APIKEY' is not among environment variables!\n" @given(st.text(alphabet=printable, min_size=1)) def test_build_get_request__test_get_api_key__env_var_set_returns_env_var( capsys, monkeypatch, apikey): monkeypatch.setenv('APIKEY', apikey, prepend=False) assert get_api_key() == apikey @given(urls(), st.text(alphabet=printable, min_size=1)) def test_build_get_request__get_api_key_False_per_page_DEFAULT_page_DEFAULT( monkeypatch, url, apikey): monkeypatch.setenv('APIKEY', apikey, prepend=False) r = build_get_request(url, get_api_key=False) assert isinstance(r, Request) assert r.params == {'per_page': 100} @given(urls(), st.text(alphabet=printable, min_size=1)) def test_build_get_request__get_api_key_True_per_page_DEFAULT_page_DEFAULT( monkeypatch, url, apikey): monkeypatch.setenv('APIKEY', apikey, prepend=False) r = build_get_request(url, get_api_key=True) assert isinstance(r, Request) assert r.url == url
# END HEADER import ipaddress import re import string import pytest from hypothesis import given from hypothesis.errors import InvalidArgument from hypothesis.provisional import domains, ip4_addr_strings, ip6_addr_strings, urls from tests.common.debug import find_any from tests.common.utils import checks_deprecated_behaviour @given(urls()) def test_is_URL(url): allowed_chars = set(string.ascii_letters + string.digits + "$-_.+!*'(),~%/") url_schemeless = url.split("://", 1)[1] path = url_schemeless.split("/", 1)[1] if "/" in url_schemeless else "" assert all(c in allowed_chars for c in path) assert all( re.match("^[0-9A-Fa-f]{2}", after_perc) for after_perc in path.split("%")[1:]) @checks_deprecated_behaviour @given(ip4_addr_strings()) def test_is_IP4_addr(address): as_num = [int(n) for n in address.split(".")]
@api.get("/v1/me") async def current_user_mock() -> JSONResponse: return JSONResponse({ "id": user_id, "href": "", "type": "user", "uri": "", "external_urls": { "spotify": "" }, }) @given( state=text(), next_url=urls(), user=emails(), scopes=permutations(list(spotify_scope.every)), ) # type: ignore @settings(deadline=800) # type: ignore @pytest.mark.asyncio async def test_full_auth_sequence(state: str, next_url: str, user: str, scopes: List[str], auth_client: AsyncClient) -> None: global scope, user_id scope = " ".join(sorted(scopes)) user_id = user auth_client.cookies.clear() # Check initial /login redirection to mocked Spotify OAuth login_params = {"scope": scope, "state": state, "next_url": next_url} login_resp = await auth_client.get("/login",
from hypothesis import given from hypothesis.provisional import urls from stock import download @st.composite def photo(draw): photographer = draw(st.text()) main_url = draw(urls()) image_url = draw(urls()) return { "photographer": photographer, "url": main_url, "src": { download.PHOTO_SIZE: image_url } } @given(urls()) def test_decode_inverts_encode(url): assert download.decode_url(download.encode_url(url)) == url @given(photo()) def test_decode_photo_object_inverts_encode(photo_obj): assert download.decode_photo_object( download.encode_photo_object(photo_obj)) == photo_obj
"""Tests given an event that matches a pydantic model but fails at the conversion step, the convert method should raise a ConversionException. """ result = Converter(platform_url="", uuid_namespace=valid_uuid).convert( [event], ignore_errors=False, fail_on_unknown=True ) with caplog.at_level(logging.ERROR): with pytest.raises(ConversionException): list(result) @settings(max_examples=1, suppress_health_check=(HealthCheck.function_scoped_fixture,)) @pytest.mark.parametrize("valid_uuid", ["ee241f8b-174f-5bdb-bae9-c09de5fe017f"]) @pytest.mark.parametrize("invalid_platform_url", ["", "not an URL"]) @given(st.builds(UIPageClose, referer=provisional.urls(), page=provisional.urls())) def test_converter_convert_with_invalid_arguments_writes_an_error_message( valid_uuid, invalid_platform_url, caplog, event ): """Tests given invalid arguments causing the conversion to fail at the validation step, the convert method should write an error message. """ event_str = event.json() result = Converter( platform_url=invalid_platform_url, uuid_namespace=valid_uuid ).convert([event_str], ignore_errors=True, fail_on_unknown=True) with caplog.at_level(logging.ERROR): assert list(result) == [] model_name = "<class 'ralph.models.xapi.navigation.statements.PageTerminated'>" errors = [f"Converted event is not a valid ({model_name}) model"]
try: rfc3987.parse(url, rule='URI') except ValueError: legacy = False new = validate_rfc3986(url) assert legacy == bool(new) @pytest.mark.parametrize('url', INVALID_URIS + VALID_URIS) def test_against_legacy_ref(url): legacy = True try: rfc3987.parse(url, rule='URI_reference') except ValueError: legacy = False new = validate_rfc3986(url, rule='URI_reference') assert legacy == bool(new) @settings(max_examples=1000) @given(url=prov_st.urls()) def test_against_legacy_hypothesis(url): print(url) legacy = True try: rfc3987.parse(url, rule='URI') except ValueError: legacy = False new = validate_rfc3986(url) assert legacy == bool(new)
class TestGenerateHtmlLink: @given(urls()) def test_url(self, link): assert html._generate_html_link(link) == f"<a href={link}>{link}</a>"
# Mapping of field types, to strategy objects or functions of (type) -> strategy _global_field_lookup = { dm.SmallIntegerField: st.integers(-32768, 32767), dm.IntegerField: st.integers(-2147483648, 2147483647), dm.BigIntegerField: st.integers(-9223372036854775808, 9223372036854775807), dm.PositiveIntegerField: st.integers(0, 2147483647), dm.PositiveSmallIntegerField: st.integers(0, 32767), dm.BinaryField: st.binary(), dm.BooleanField: st.booleans(), dm.DateField: st.dates(), dm.EmailField: emails(), dm.FloatField: st.floats(), dm.NullBooleanField: st.one_of(st.none(), st.booleans()), dm.URLField: urls(), dm.UUIDField: st.uuids(), df.DateField: st.dates(), df.DurationField: st.timedeltas(), df.EmailField: emails(), df.FloatField: st.floats(allow_nan=False, allow_infinity=False), df.IntegerField: st.integers(-2147483648, 2147483647), df.NullBooleanField: st.one_of(st.none(), st.booleans()), df.URLField: urls(), df.UUIDField: st.uuids(), } def register_for(field_type): def inner(func): _global_field_lookup[field_type] = func
assert ( "Options:\n" " -f, --format [edx] Input events format to validate [required]\n" " -I, --ignore-errors Continue validating regardless of raised errors\n" " -F, --fail-on-unknown Stop validating at first unknown event\n" ) in result.output result = runner.invoke(cli, ["validate"]) assert result.exit_code > 0 assert ("Error: Missing option '-f' / '--format'. Choose from:\n\tedx\n" ) in result.output @settings(max_examples=1) @given( st.builds(UIPageClose, referer=provisional.urls(), page=provisional.urls()), ) def test_cli_validate_command_with_edx_format(event): """Tests the validate command using the edx format.""" event_str = event.json() runner = CliRunner() result = runner.invoke(cli, ["validate", "-f", "edx"], input=event_str) assert event_str in result.output def test_cli_convert_command_usage(): """Tests ralph convert command usage.""" runner = CliRunner() result = runner.invoke(cli, ["convert", "--help"])