Ejemplo n.º 1
0
def test_cancel_group_member(client):
    """attempt to cancel one group member's applications
        test: Application and GroupMember are correctly deleted from DB
        target_url: /lotteries/<id> [DELETE]
    """
    lottery_id = 1
    with client.application.app_context():
        target = Lottery.query.get(lottery_id)
        index = target.index
        members = [test_user1, test_user2]
        members_app_id = [
            make_application(client, user['secret_id'], lottery_id)
            for user in members]
        rep_app_id = make_application(client, test_user['secret_id'],
                                      lottery_id,
                                      group_member_apps=members_app_id)

        assert Application.query.count() == 3
        assert GroupMember.query.count() == 2

        with mock.patch('api.routes.api.get_draw_time_index',
                        return_value=index):
            token = login(client, test_user['secret_id'],
                          test_user['g-recaptcha-response'])['token']
            resp = client.delete(f'/applications/{members_app_id[0]}',
                                 headers={'Authorization': f'Bearer {token}'})

        assert resp.status_code == 200
        assert Application.query.count() == 2
        result = Application.query.all()
        assert all(app.id in (members_app_id[1], rep_app_id)
                   for app in result)
        assert GroupMember.query.count() == 1
        assert GroupMember.query.one().own_application_id == members_app_id[1]
Ejemplo n.º 2
0
def test_cancel_group(client):
    """attempt to cancel group applications
        target_url: /lotteries/<id> [DELETE]
    """
    lottery_id = 1
    members = (test_user1, test_user2)
    rep = test_user

    token = login(client,
                  rep['secret_id'],
                  rep['g-recaptcha-response'])['token']

    with client.application.app_context():
        target_lottery = Lottery.query.get(lottery_id)
        index = target_lottery.index

        first_gm_len = len(GroupMember.query.all())

        members_app_id = [
            make_application(client, user['secret_id'], lottery_id)
            for user in members]
        rep_app_id = make_application(client, rep['secret_id'], lottery_id,
                                      group_member_apps=members_app_id)

        with mock.patch('api.routes.api.get_draw_time_index',
                        return_value=index):
            client.delete(f'/applications/{rep_app_id}',
                          headers={'Authorization': f'Bearer {token}'})

        app_ids = db.session.query(Application.id).all()
        assert rep_app_id not in app_ids
        assert all(member_app not in app_ids for member_app in members_app_id)

        after_gm_len = len(GroupMember.query.all())
        assert after_gm_len == first_gm_len
Ejemplo n.º 3
0
def test_get_allapplications_admin(client):
    """test 403 is returned from the API to admin
        target_url: /applications
    """
    lottery_id = 1
    make_application(client, admin['secret_id'], lottery_id)

    resp = as_user_get(client, admin['secret_id'],
                       admin['g-recaptcha-response'], '/applications')

    assert resp.status_code == 403
Ejemplo n.º 4
0
def test_get_allapplications(client):
    """test proper infomation is returned from the API to a normal user
        target_url: /applications
    """
    lottery_id = 1
    make_application(client, test_user['secret_id'], lottery_id)

    resp = as_user_get(client, test_user['secret_id'],
                       test_user['g-recaptcha-response'], '/applications')

    with client.application.app_context():
        db_status = Application.query.all()
        application_list = applications_schema.dump(db_status)[0]

    assert resp.get_json() == application_list
Ejemplo n.º 5
0
def test_cancel_already_done_normal(client):
    """attempt to cancel application that already-done lottery
        1. create 'done' application
        2. attempt to cancel that application
        target_url: /lotteries/<id> [DELETE]
    """
    token = login(client, test_user['secret_id'],
                  test_user['g-recaptcha-response'])['token']
    lottery_id = 1
    application_id = make_application(client, test_user['secret_id'],
                                      lottery_id)

    with client.application.app_context():
        target_application = Application.query.filter_by(
            id=application_id).first()
        target_application.status = 'lose'
        db.session.add(target_application)
        db.session.commit()

    resp = client.delete(f'/applications/{application_id}',
                         headers={'Authorization': f'Bearer {token}'})

    assert resp.status_code == 400
    assert 'The Application has already fullfilled' in resp.get_json(
    )['message']
Ejemplo n.º 6
0
def test_cancel_admin(client):
    """test: return 403 for canceling by admin
    """
    lottery_id = 1
    application_id = make_application(client, admin['secret_id'], lottery_id)

    token = login(client, admin['secret_id'],
                  admin['g-recaptcha-response'])['token']
    resp = client.delete(f'/applications/{application_id}',
                         headers={'Authorization': f'Bearer {token}'})

    assert resp.status_code == 403
Ejemplo n.º 7
0
def test_get_specific_application_admin(client):
    """test 403 is returned from the API to admin
        target_url: /applications/<id>
    """
    lottery_id = 1
    application_id = make_application(client, admin['secret_id'], lottery_id)

    resp = as_user_get(client, admin['secret_id'],
                       admin['g-recaptcha-response'],
                       f'/applications/{application_id}')

    assert resp.status_code == 403
Ejemplo n.º 8
0
def test_get_specific_application_invaild_id(client):
    """test proper errpr is returned from the API
        target_url: /applications/<id>
    """
    lottery_id = 1
    application_id = make_application(client, test_user['secret_id'],
                                      lottery_id)

    # application id to test
    idx = application_id + 1
    resp = as_user_get(client, test_user['secret_id'],
                       test_user['g-recaptcha-response'],
                       f'/applications/{idx}')

    assert resp.status_code == 404
    assert 'Not found' in resp.get_json()['message']
Ejemplo n.º 9
0
def test_get_specific_application_normal(client):
    """test proper infromation is returned from the API to a normal user
        target_url: /applications/<id>
    """
    lottery_id = 1
    application_id = make_application(client, test_user['secret_id'],
                                      lottery_id)

    resp = as_user_get(client, test_user['secret_id'],
                       test_user['g-recaptcha-response'],
                       f'/applications/{application_id}')

    with client.application.app_context():
        db_status = Application.query.filter_by(id=application_id).first()
        application = application_schema.dump(db_status)[0]

    assert resp.get_json() == application
Ejemplo n.º 10
0
def test_cancel_normal(client):
    """test: cancel added application
        1. add new application to db
        2. send request to cancel as a normal user
        3. check response's status_code and db status
        target_url: /applications/<id> [DELETE]
    """
    lottery_id = 1
    application_id = make_application(client, test_user['secret_id'],
                                      lottery_id)

    token = login(client, test_user['secret_id'],
                  test_user['g-recaptcha-response'])['token']
    user_resp = client.get('/status',
                           headers={'Authorization': f'Bearer {token}'})
    user_id = user_resp.get_json()['id']
    resp = client.delete(f'/applications/{application_id}',
                         headers={'Authorization': f'Bearer {token}'})
    with client.application.app_context():
        application = Application.query.filter_by(lottery_id=lottery_id,
                                                  user_id=user_id).first()

    assert resp.status_code == 200
    assert application is None