Ejemplo n.º 1
0
def test_create_participant(flask_client, dummy_newdle, dummy_uid):
    assert (Participant.query.filter_by(newdle=dummy_newdle,
                                        auth_uid=dummy_uid).first() is None)

    nb_participant = Participant.query.count()
    resp = flask_client.put(url_for('api.create_participant', code='dummy'),
                            **make_test_auth(dummy_uid))

    participant = Participant.query.filter_by(newdle=dummy_newdle,
                                              auth_uid=dummy_uid).first()

    code = resp.json.pop('code')
    id_ = resp.json.pop('id')

    assert participant.code == code
    assert participant.id == id_
    assert resp.status_code == 200
    assert resp.json == add_avatar({
        'answers': {},
        'auth_uid': 'user123',
        'email': '*****@*****.**',
        'name': 'Guinea Pig',
        'comment': '',
    })
    assert Participant.query.count() == nb_participant + 1
    assert Stats.get_value(StatKey.participants_created) == 1
Ejemplo n.º 2
0
def test_create_unknown_participant(flask_client):
    name = 'Unknown participant'
    now = datetime.utcnow()
    num_participants = Participant.query.count()
    resp = flask_client.post(url_for('api.create_unknown_participant',
                                     code='dummy'),
                             json={'name': name})
    assert resp.status_code == 200
    data = resp.json
    code = data.pop('code')
    id_ = data.pop('id')
    participant = Participant.query.filter_by(name=name).first()
    assert data == add_avatar({
        'answers': {},
        'auth_uid': None,
        'email': None,
        'name': name,
        'comment': '',
    })
    newdle = Newdle.query.filter_by(code='dummy').first()
    assert Participant.query.count() == num_participants + 1
    assert participant.code == code
    assert participant.id == id_
    assert newdle.last_update > now
    assert Stats.get_value(StatKey.participants_created) == 1
Ejemplo n.º 3
0
def test_create_unknown_participant_newdle_invalid(flask_client):
    resp = flask_client.post(
        url_for('api.create_unknown_participant', code='xxx'),
        json={'name': 'Unknown participant'},
    )
    assert resp.status_code == 404
    assert resp.json == {'error': 'Specified newdle does not exist'}
    assert Stats.get_value(
        StatKey.participants_created) == 0  # no participants added
Ejemplo n.º 4
0
def test_create_participant_newdle_invalid(flask_client, dummy_uid):
    resp = flask_client.put(
        url_for('api.create_participant', code='xxx'),
        json={'name': 'New participant'},
        **make_test_auth(dummy_uid),
    )
    assert resp.status_code == 404
    assert resp.json == {'error': 'Specified newdle does not exist'}
    assert Stats.get_value(
        StatKey.participants_created) == 0  # no participants added
Ejemplo n.º 5
0
def test_update_newdle_participants(flask_client, dummy_newdle, dummy_uid):
    auth = make_test_auth(dummy_uid)
    resp = flask_client.post(
        url_for('api.create_unknown_participant', code='dummy'),
        **auth,
        json={'name': 'John'},
    )
    assert Stats.get_value(StatKey.participants_created) == 1
    participant = resp.json
    resp = flask_client.patch(
        url_for('api.update_newdle', code='dummy'),
        **auth,
        json={
            'code':
            'xxx',
            'participants': [
                {
                    'name': 'Guinea Pig',
                    'email': '*****@*****.**',
                    'auth_uid': 'pig',
                    'signature': '-',
                },
                participant,
                {
                    'name': 'Invalid participant',
                },
            ],
        },
    )

    assert resp.status_code == 200
    print(resp.json['participants'])
    resp.json['participants'].sort(key=itemgetter('name'))
    assert [p['name'] for p in resp.json['participants']] == [
        'Guinea Pig',
        participant['name'],
    ]
    ids = [participant.pop('id') for participant in resp.json['participants']]
    assert ids == [
        p.id for p in sorted(dummy_newdle.participants, key=attrgetter('name'))
    ]
    assert Stats.get_value(StatKey.participants_created) == 2
Ejemplo n.º 6
0
def test_create_unknown_participant_newdle_finished(flask_client,
                                                    dummy_newdle):
    name = 'Unknown participant'
    dummy_newdle.final_dt = datetime(2019, 9, 12, 13, 30)
    resp = flask_client.post(url_for('api.create_unknown_participant',
                                     code='dummy'),
                             json={'name': name})
    assert resp.status_code == 403
    assert resp.json == {'error': 'This newdle has finished'}
    assert Stats.get_value(
        StatKey.participants_created) == 0  # no participants added
Ejemplo n.º 7
0
def test_create_newdle_participant_signing(flask_client, dummy_uid):
    resp = flask_client.post(
        url_for('api.create_newdle'),
        **make_test_auth(dummy_uid),
        json={
            'title':
            'My Newdle',
            'duration':
            120,
            'timezone':
            'Europe/Zurich',
            'timeslots': ['2019-09-11T13:00'],
            'participants': [{
                'name': 'Guinea Pig',
                'email': '*****@*****.**',
                'auth_uid': 'guineapig',
                'signature': 'YeJMFxKqMAxdINW23mcuHL0ufsA',
            }],
        },
    )
    assert resp.status_code == 422
    assert Stats.get_value(StatKey.newdles_created) == 0
    assert Stats.get_value(StatKey.participants_created) == 0
Ejemplo n.º 8
0
def test_create_participant_newdle_no_duplicate(flask_client, dummy_newdle,
                                                dummy_uid):
    dummy_newdle.participants.add(
        Participant(
            code='part4',
            name='Guinea Pig',
            email='*****@*****.**',
            auth_uid=dummy_uid,
        ))
    nb_participant = Participant.query.count()
    resp = flask_client.put(url_for('api.create_participant', code='dummy'),
                            **make_test_auth(dummy_uid))
    assert resp.status_code == 200
    assert Participant.query.count() == nb_participant
    assert Stats.get_value(
        StatKey.participants_created) == 0  # no participants added
Ejemplo n.º 9
0
def test_create_newdle_invalid(flask_client, dummy_uid):
    resp = flask_client.post(url_for('api.create_newdle'),
                             **make_test_auth(dummy_uid),
                             json={})
    assert resp.status_code == 422
    assert resp.json == {
        'error': 'invalid_args',
        'messages': {
            'duration': ['Missing data for required field.'],
            'private': ['Missing data for required field.'],
            'notify': ['Missing data for required field.'],
            'timeslots': ['Missing data for required field.'],
            'timezone': ['Missing data for required field.'],
            'title': ['Missing data for required field.'],
        },
    }
    assert Stats.get_value(StatKey.newdles_created) == 0
Ejemplo n.º 10
0
def test_update_participant_empty(flask_client, dummy_newdle):
    resp = flask_client.patch(
        url_for('api.update_participant',
                code='dummy',
                participant_code='part1'))
    id_ = resp.json.pop('id')
    assert id_ == next(p.id for p in dummy_newdle.participants
                       if p.code == 'part1')
    assert resp.status_code == 200
    assert resp.json == add_avatar({
        'answers': {},
        'auth_uid': None,
        'email': None,
        'name': 'Tony Stark',
        'comment': '',
        'code': 'part1',
    })
    assert Stats.get_value(
        StatKey.participants_created) == 0  # no participants added
Ejemplo n.º 11
0
def test_create_newdle_duplicate_timeslot(flask_client, dummy_uid):
    resp = flask_client.post(
        url_for('api.create_newdle'),
        **make_test_auth(dummy_uid),
        json={
            'title': 'My Newdle',
            'duration': 120,
            'timezone': 'Europe/Zurich',
            'private': True,
            'notify': True,
            'timeslots': ['2019-09-11T13:00', '2019-09-11T13:00'],
        },
    )
    assert resp.status_code == 422
    assert resp.json == {
        'error': 'invalid_args',
        'messages': {
            'timeslots': ['Time slots are not unique']
        },
    }
    assert Stats.get_value(StatKey.newdles_created) == 0
Ejemplo n.º 12
0
def test_create_newdle(flask_client, dummy_uid, with_participants):
    assert not Newdle.query.count()
    now = datetime.utcnow()
    resp = flask_client.post(
        url_for('api.create_newdle'),
        **make_test_auth(dummy_uid),
        json={
            'title':
            'My Newdle',
            'duration':
            120,
            'timezone':
            'Europe/Zurich',
            'timeslots': ['2019-09-11T13:00', '2019-09-11T15:00'],
            'participants': [{
                'name': 'Guinea Pig',
                'auth_uid': 'guineapig',
                'email': '*****@*****.**',
                'signature': '-',
            }] if with_participants else [],
            'private':
            True,
            'notify':
            True,
        },
    )
    assert resp.status_code == 200
    data = resp.json
    id_ = data.pop('id')
    code = data.pop('code')
    participant_id = data['participants'][0].pop(
        'id') if with_participants else None
    del data['url']
    expected_participants = ([
        add_avatar({
            'answers': {},
            'auth_uid': 'guineapig',
            'email': '*****@*****.**',
            'name': 'Guinea Pig',
            'comment': '',
            'signature': '-',
        })
    ] if with_participants else [])
    assert data == {
        'creator_name': 'Guinea Pig',
        'creator_uid': dummy_uid,
        'duration': 120,
        'final_dt': None,
        'participants': expected_participants,
        'timeslots': ['2019-09-11T13:00', '2019-09-11T15:00'],
        'timezone': 'Europe/Zurich',
        'private': True,
        'notify': True,
        'title': 'My Newdle',
        'deleted': False,
        'deletion_dt': None,
    }
    newdle = Newdle.query.one()
    assert newdle.last_update > now
    assert newdle.id == id_
    assert newdle.code == code
    assert newdle.title == 'My Newdle'
    assert newdle.duration == timedelta(minutes=120)
    assert newdle.timezone == 'Europe/Zurich'
    assert newdle.timeslots == [
        datetime(2019, 9, 11, 13, 0),
        datetime(2019, 9, 11, 15, 0),
    ]
    assert Stats.get_value(StatKey.newdles_created) == 1
    if with_participants:
        assert len(newdle.participants) == 1
        participant = next(iter(newdle.participants))
        assert participant.name == 'Guinea Pig'
        assert participant.id == participant_id
        assert Stats.get_value(StatKey.participants_created) == 1
    else:
        assert Stats.get_value(StatKey.participants_created) == 0
        assert not newdle.participants
Ejemplo n.º 13
0
def test_update_newdle(flask_client, dummy_newdle, dummy_uid):
    final_dt = '2019-09-12T13:30'
    expected_json = {
        'code':
        'dummy',
        'creator_name':
        'Dummy',
        'creator_uid':
        dummy_newdle.creator_uid,
        'duration':
        120,
        'id':
        dummy_newdle.id,
        'private':
        True,
        'notify':
        False,
        'timeslots': [
            '2019-08-11T13:00',
            '2019-08-11T14:00',
            '2019-09-12T13:00',
            '2019-09-12T13:30',
        ],
        'participants': [
            add_avatar({
                'answers': {},
                'auth_uid': None,
                'email': None,
                'name': 'Albert Einstein',
                'comment': '',
            }),
            add_avatar({
                'answers': {},
                'auth_uid': 'pig',
                'email': '*****@*****.**',
                'name': 'Guinea Pig',
                'comment': '',
                'signature': '-',
            }),
            add_avatar({
                'answers': {},
                'auth_uid': None,
                'email': None,
                'name': 'Tony Stark',
                'comment': '',
            }),
        ],
        'timezone':
        'Europe/Paris',
        'title':
        'Test event1',
        'url':
        'http://flask.test/newdle/dummy',
        'deleted':
        False,
        'deletion_dt':
        None,
    }
    resp = flask_client.patch(
        url_for('api.update_newdle', code='dummy'),
        **make_test_auth(dummy_uid),
        json={
            'code':
            'xxx',
            'creator_name':
            'someone',
            'duration':
            120,
            'final_dt':
            final_dt,
            'id':
            10,
            'timeslots': [
                '2019-08-11T13:00',
                '2019-08-11T14:00',
                '2019-09-12T13:00',
                '2019-09-12T13:30',
            ],
            'timezone':
            'Europe/Paris',
            'title':
            'Test event1',
            'url':
            'http://flask.test/newdle/dummy1',
        },
    )

    resp.json['participants'].sort(key=itemgetter('name'))
    ids = [participant.pop('id') for participant in resp.json['participants']]
    assert ids == [
        p.id for p in sorted(dummy_newdle.participants, key=attrgetter('name'))
    ]
    assert resp.json['final_dt'] == final_dt
    assert resp.status_code == 200
    del resp.json['final_dt']
    assert resp.json == expected_json
    assert Stats.get_value(
        StatKey.participants_created) == 0  # no participants added