Пример #1
0
def test_register(client, session):
    dev1 = 'txemac'
    dev2 = 'jobandtalent'
    data_1 = RealtimePost(
        user_1=dev1,
        user_2=dev2,
        connected=True,
        organisations=['organisation_1'],
    )
    Realtime.create(db_session=session, data=data_1)
    data_2 = RealtimePost(
        user_1=dev2,
        user_2=dev1,
        connected=False,
    )
    Realtime.create(db_session=session, data=data_2)

    response = client.get(f'/api/v1/connected/register/{dev1}/{dev2}')
    assert response.status_code == HTTP_200_OK
    expected = [
        dict(
            registered_at='*',
            connected=data_1.connected,
            organisations=data_1.organisations,
        ),
        dict(
            registered_at='*',
            connected=data_2.connected,
        ),
    ]
    assert_lists(original=response.json(), expected=expected)
Пример #2
0
def test_create_creates(session):
    count_realtime_1 = session.query(Realtime).count()
    count_user_1 = session.query(User).count()
    count_organisations_1 = session.query(Organisation).count()
    data = RealtimePost(
        user_1='txemac',
        user_2='jobandtalent',
        connected=True,
        organisations=['new_1', 'new_2'],
    )
    Realtime.create(db_session=session, data=data)
    count_realtime_2 = session.query(Realtime).count()
    count_user_2 = session.query(User).count()
    count_organisations_2 = session.query(Organisation).count()
    assert count_realtime_1 + 1 == count_realtime_2
    assert count_user_1 + 2 == count_user_2
    assert count_organisations_1 + 2 == count_organisations_2

    realtime = session.query(Realtime).order_by(Realtime.id.desc()).first()
    assert realtime.user_1_id == User.get_or_create_by_name(
        db_session=session, name=data.user_1).id
    assert realtime.user_2_id == User.get_or_create_by_name(
        db_session=session, name=data.user_2).id
    assert realtime.connected == data.connected
    assert list(map(
        int, realtime.organisations)) == Organisation.get_ids_from_names(
            db_session=session, names=data.organisations)
Пример #3
0
def test_realtime_post_wrong(name1, name2, connected):
    with pytest.raises(ValidationError):
        RealtimePost(
            user_1=name1,
            user_2=name2,
            connected=connected,
            organisations=['organisation_1'])
Пример #4
0
def get_real_time(
        *,
        db_session: Session = Depends(get_db),
        dev1: str,
        dev2: str,
) -> Dict[str, Union[bool, List[str]]]:
    github_user_1 = github_api_client.get_organisations(username=dev1)
    github_user_2 = github_api_client.get_organisations(username=dev2)
    twitter_user_1 = twitter_api_client.get_friends(username=dev1)
    twitter_user_2 = twitter_api_client.get_friends(username=dev2)

    # not valid user
    errors = [x for x in [github_user_1, github_user_2, twitter_user_1, twitter_user_2] if isinstance(x, str)]
    if errors:
        return dict(errors=errors)

    connected, organisations = get_connections(
        dev1=dev1,
        dev2=dev2,
        twitter_user_1=twitter_user_1,
        twitter_user_2=twitter_user_2,
        github_user_1=github_user_1,
        github_user_2=github_user_2,
    )

    # create response
    response = dict(connected=connected)
    if connected is True:
        response.update(organisations=organisations)

    # save
    Realtime.create(
        db_session=db_session,
        data=RealtimePost(
            user_1=dev1,
            user_2=dev2,
            connected=connected,
            organisations=organisations,
        )
    )

    return response
Пример #5
0
def test_create_false(session, new_user):
    count_realtime_1 = session.query(Realtime).count()
    count_user_1 = session.query(User).count()
    count_organisations_1 = session.query(Organisation).count()
    data = RealtimePost(
        user_1=new_user.name,
        user_2='jobandtalent',
        connected=False,
    )
    Realtime.create(db_session=session, data=data)
    count_realtime_2 = session.query(Realtime).count()
    count_user_2 = session.query(User).count()
    count_organisations_2 = session.query(Organisation).count()
    assert count_realtime_1 + 1 == count_realtime_2
    assert count_user_1 + 1 == count_user_2
    assert count_organisations_1 == count_organisations_2

    realtime = session.query(Realtime).order_by(Realtime.id.desc()).first()
    assert realtime.user_1_id == User.get_or_create_by_name(
        db_session=session, name=data.user_1).id
    assert realtime.user_2_id == User.get_or_create_by_name(
        db_session=session, name=data.user_2).id
    assert realtime.connected == data.connected
    assert realtime.organisations is None
Пример #6
0
def test_realtime_post_ok(connected):
    assert RealtimePost(
        user_1='txemac',
        user_2='jobandtalent',
        connected=connected,
        organisations=['organisation_1']) is not None