Esempio n. 1
0
    async def test_session_exists_profile_exists():
        # Create Profile for Session
        new_profile = Profile.new()
        await new_profile.storage().save()

        # Create Session
        session_id = str(uuid4())
        session = Session(id=session_id)
        session.profile = new_profile
        await session.storage().save()

        tracker_payload = TrackerPayload(session=Entity(id=session.id),
                                         profile=new_profile,
                                         source=Entity(id="scope"))

        profile, session = await tracker_payload._get_profile_and_session()

        assert isinstance(profile, Profile)
        assert isinstance(session, Session)

        assert profile.id == new_profile.id  # profile id must be form session
        assert session.id == session_id

        # Remove
        await session.storage().delete()
        await profile.storage().delete()
Esempio n. 2
0
    async def test_session_exists_profile_not_exists():
        non_existent_profile_id = str(uuid4())

        # Create Session
        session_id = str(uuid4())
        session = Session(id=session_id)
        await session.storage().save()

        # Profile does not exist
        session.profile = Profile(id=non_existent_profile_id)

        # Session exists and has profile equal to some random profile that do not exists
        tracker_payload = TrackerPayload(session=Entity(id=session_id),
                                         profile=None,
                                         source=Entity(id="scope"))

        profile, session = await tracker_payload._get_profile_and_session()

        assert isinstance(profile, Profile)
        assert isinstance(session, Session)

        assert profile.id != non_existent_profile_id  # profile can not be generated from non existent profile_id
        assert session.id == session_id

        await session.storage().delete()
        await profile.storage().delete()
Esempio n. 3
0
    async def test_session_not_exists_profile_exists():
        # Session does not exist
        # Profile exists

        # Create Profile for Session
        profile_id = str(uuid4())
        profile = Profile(id=profile_id)
        await profile.storage().save()

        # Create Session
        session_id = str(uuid4())
        session = Session(id=session_id)
        session.profile = profile

        tracker_payload = TrackerPayload(session=Entity(id=session.id),
                                         profile=Entity(id=profile.id),
                                         source=Entity(id="scope"))

        profile, session = await tracker_payload._get_profile_and_session()

        assert isinstance(profile, Profile)
        assert isinstance(session, Session)

        assert profile.id == profile_id
        assert session.id == session_id

        # Remove
        await session.storage().delete()
        await profile.storage().delete()
Esempio n. 4
0
async def test_load_session_with_error(mocker):
    def exc(*args):
        raise elasticsearch.exceptions.ElasticsearchException("test")

    mocker.patch('app.domain.session.StorageCrud.load', exc)

    try:
        tracker_payload = TrackerPayload(session=Entity(id="non_existent"),
                                         profile=None,
                                         source=Entity(id="scope"))

        profile, session = await tracker_payload._get_profile_and_session()

    except elasticsearch.exceptions.ElasticsearchException as e:
        assert str(e) == "test"
Esempio n. 5
0
async def test_get_context_session_exists_profile_exists_conflict(mocker):
    mocker.patch('app.domain.session.StorageCrud', MockStorageCrud)
    mocker.patch('app.domain.session.StorageCrud', MockStorageCrud)
    mocker.patch('app.domain.entity.crud', app.tests.unit.mocks.mock_storage)
    mocker.patch('app.domain.profile.StorageCrud', MockStorageCrud)

    # Session exists and has profile equal to 1
    tracker_payload = TrackerPayload(session=Entity(id="1"),
                                     profile=Entity(id="2"),
                                     source=Entity(id="scope"))

    profile, session = await tracker_payload._get_profile_and_session()

    assert isinstance(profile, Profile)
    assert isinstance(session, Session)

    # profile from session is most important
    assert profile.id == "1"
    assert session.id == "1"
Esempio n. 6
0
async def test_save_events():
    tracker_payload = TrackerPayload(
        session=Entity(id="12345"),
        profile=Profile.new(),
        source=Entity(id="scope"),
        events=[
            EventPayload(type="click",
                         properties={"btn": [1, 2, 3]},
                         options={"save": True}),
            EventPayload(type="click",
                         properties={"btn": [3, 4, 5]},
                         options={"save": True})
        ])
    events = tracker_payload.get_events(Session.new(), Profile.new())
    result = await events.bulk().save()
    assert result.saved == 2  # profile id must be form session

    for id in result.ids:
        entity = Entity(id=id)
        await entity.storage("event").delete()
Esempio n. 7
0
async def test_get_context_session_not_exists_profile_not_exists(mocker):
    mocker.patch('app.domain.session.StorageCrud', MockStorageCrud)
    mocker.patch('app.domain.session.StorageCrud', MockStorageCrud)
    mocker.patch('app.domain.entity.crud', app.tests.unit.mocks.mock_storage)
    mocker.patch('app.domain.profile.StorageCrud', MockStorageCrud)

    tracker_payload = TrackerPayload(
        session=Entity(id="generated_not_exists-02"),
        profile=None,
        source=Entity(id="scope"))

    profile, session = await tracker_payload._get_profile_and_session()

    assert isinstance(profile, Profile)
    assert isinstance(session, Session)

    # Profile generated
    assert profile.id != "2"
    # Session generated
    assert session.id == "generated_not_exists-02"
Esempio n. 8
0
async def test_tracker_collect():

    source = Entity(id="scope")
    await source.storage("source").save({})

    session = Entity.new()
    profile = Entity.new()

    tracker_payload = TrackerPayload(
        session=session,
        profile=profile,
        source=source,
        events=[
            EventPayload(type="click",
                         properties={"btn": [1, 2, 3]},
                         options={"save": False}),
            EventPayload(type="click",
                         properties={"btn": [3, 4, 5]},
                         options={"save": True})
        ])

    profile, session, events, result = await tracker_payload.collect()

    # assert profile.id == result.profile.ids[0]
    assert session.id == result.session.ids[0]

    assert result.session.saved == 1
    # assert result.profile.saved == 1
    assert result.events.saved == 1  # Only one event is marked to saved

    for id in result.events.ids:
        entity = Entity(id=id)
        await entity.storage("event").delete()

    # for id in result.profile.ids:
    #     entity = Entity(id=id)
    #     await entity.storage("profile").delete()

    for id in result.session.ids:
        entity = Entity(id=id)
        await entity.storage("session").delete()
Esempio n. 9
0
    def encode(
        stat: Dict[str, List[Dict[str,
                                  DebugInfo]]]) -> List['EventDebugRecord']:

        for event_type, debugging in stat.items():
            for debug_infos in debugging:
                for rule_id, debug_info in debug_infos.items(
                ):  # type: DebugInfo
                    # todo - to pole jest za małe (wyskakuje błąd gdy debug infor ma powyżej 32000 znaków)
                    b64 = b64_encoder(debug_info.dict())
                    yield EventDebugRecord(
                        event=Entity(id=debug_info.event.id), content=b64)
Esempio n. 10
0
async def test_get_context_session_not_exists_profile_exists(mocker):
    mocker.patch('app.domain.session.StorageCrud', MockStorageCrud)
    mocker.patch('app.domain.session.StorageCrud', MockStorageCrud)
    mocker.patch('app.domain.entity.crud', app.tests.unit.mocks.mock_storage)
    mocker.patch('app.domain.profile.StorageCrud', MockStorageCrud)

    # Session 2 does not exist
    # Profile 2 exists
    tracker_payload = TrackerPayload(
        session=Entity(id="generated_not_exists-01"),
        profile=Entity(id="2"),
        source=Entity(id="scope"))

    profile, session = await tracker_payload._get_profile_and_session()

    assert isinstance(profile, Profile)
    assert isinstance(session, Session)

    # Do not generate profile copy it from param
    assert session.id == "generated_not_exists-01"
    assert profile.id == "2"
Esempio n. 11
0
    async def validate_source(self, source_id) -> Source:
        entity = Entity(id=source_id)

        source = await self.get(entity)  # type: Source
        if source is None:
            raise HTTPException(detail="Access denied. Invalid source.",
                                status_code=401)

        if not source.enabled:
            raise HTTPException(detail="Access denied. Source disabled.",
                                status_code=404)

        return source
Esempio n. 12
0
    async def test_session_not_exists_profile_not_exists():
        # Create Profile for Session
        profile_id = str(uuid4())
        profile = Profile(id=profile_id)

        # Create Session
        session_id = str(uuid4())
        session = Session(id=session_id)
        session.profile = profile

        tracker_payload = TrackerPayload(session=Entity(id=session.id),
                                         profile=Entity(id=profile.id),
                                         source=Entity(id="scope"))

        profile, session = await tracker_payload._get_profile_and_session()

        assert profile.id != profile_id  # Must generate new profile, this may be forged
        assert session.id == session_id

        # Remove
        await session.storage().delete()
        await profile.storage().delete()
Esempio n. 13
0
 async def main(obj, index):
     entity = Entity(id=str(uuid4()))
     result = await entity.storage(index).load(obj)
     assert result is None
     await entity.storage(index).save()
     sleep(1)
     object_loaded = await entity.storage(index).load(obj)
     assert object_loaded.id == entity.id
     result = await entity.storage(index).load_by("id", entity.id)
     for r in result:
         assert r['id'] == entity.id
     await entity.storage(index).delete()
     sleep(1)
     result = await entity.storage(index).load(obj)
     assert result is None
Esempio n. 14
0
    async def run(self, **kwargs):
        if self.debug:

            event_entity = Entity(id=self.event_id)
            event = await event_entity.storage('event').load(Event
                                                             )  # type: Event

            if event is None:
                raise ValueError(
                    "There is no event with id `{}`. Check configuration for correct event id."
                    .format(self.event_id))

            self.event.replace(event)

            profile_entity = Entity(id=self.event.profile.id)
            session_entity = Entity(id=self.event.session.id)
            profile_task = asyncio.create_task(
                profile_entity.storage('profile').load(Profile))
            session_task = asyncio.create_task(
                session_entity.storage('session').load(Session))

            profile = await profile_task
            session = await session_task

            if session is None:
                raise ValueError(
                    "Event id `{}` has reference to empty session id `{}`. Debug stopped. This event is corrupted."
                    .format(self.event_id, self.event.session.id))

            self.session.replace(session)

            if profile is None:
                raise ValueError(
                    "Event id `{}` has reference to empty profile id `{}`. Debug stopped. This event is corrupted."
                    .format(self.event_id, self.event.profile.id))

            self.profile.replace(profile)

            return Result(port="event", value=self.event.dict())

        return Result(port="event", value=self.event.dict())
Esempio n. 15
0
            "target": "763cbc92-5236-4d30-bff2-a10dbbf8c83f",
            "targetHandle": "payload",
            "id": "reactflow__edge-9f95ad82-9d7c-4f61-a439-580ba4c730e3payload-763cbc92-5236-4d30-bff2-a10dbbf8c83fpayload",
            "type": "default"
        }
    ]
}

converter = FlowGraphConverter(flow)
x = converter.convert_to_dag_graph()
print(x)
dag = DagProcessor(x)
exec_dag = dag.make_execution_dag()
# print(exec_dag.data)
print()
print(exec_dag.serialize())
print()
# exit()
event = Event(
    id="event-id",
    source=Entity(id="1"),
    session=Entity(id="2"),
    context=Context(),
    type="xxx"
)
session = Session(id="session-id")
profile = Profile(id="profile-id")
init = exec_dag.init(event, session, profile)
asyncio.run(exec_dag.run({}, "flow-1", "event-1"))