Beispiel #1
0
def test_deploy_ramp_event(session_scope_function):
    database_config = read_config(database_config_template())
    event_config_filename = ramp_config_template()
    event_config = read_config(event_config_filename)
    ramp_config = generate_ramp_config(event_config)
    deploy_ramp_event(database_config_template(), ramp_config_template())

    # simulate that we add users and sign-up for the event and that they
    # submitted the starting kit
    with session_scope(database_config['sqlalchemy']) as session:
        add_users(session)
        sign_up_team(session, ramp_config['event_name'], 'test_user')
        submit_starting_kits(session, ramp_config['event_name'], 'test_user',
                             ramp_config['ramp_kit_submissions_dir'])

    # run the dispatcher on the event which are in the dataset
    dispatcher = Dispatcher(config=database_config,
                            event_config=event_config,
                            worker=CondaEnvWorker,
                            n_workers=-1,
                            hunger_policy='exit')
    dispatcher.launch()

    # the iris kit contain a submission which should fail for a user
    with session_scope(database_config['sqlalchemy']) as session:
        submission = get_submissions(session,
                                     event_config['ramp']['event_name'],
                                     'training_error')
        assert len(submission) == 1
Beispiel #2
0
def test_user_event_status(client_session):
    client, session = client_session

    add_user(session,
             'new_user',
             'new_user',
             'new_user',
             'new_user',
             'new_user',
             access_level='user')
    add_event(session,
              'iris',
              'iris_new_event',
              'new_event',
              'starting_kit',
              '/tmp/databoard_test/submissions',
              is_public=True)

    # user signed up, not approved for the event
    ask_sign_up_team(session, 'iris_new_event', 'new_user')
    with login_scope(client, 'new_user', 'new_user') as client:
        rv = client.get('/problems')
        assert rv.status_code == 200
        assert b'user-waiting' in rv.data
        assert b'user-signed' not in rv.data

    # user signed up and approved for the event
    sign_up_team(session, 'iris_new_event', 'new_user')
    with login_scope(client, 'new_user', 'new_user') as client:
        rv = client.get('/problems')
        assert rv.status_code == 200
        assert b'user-signed' in rv.data
        assert b'user-waiting' not in rv.data
Beispiel #3
0
def test_sign_up_team(session_scope_function):
    event_name, username = '******', 'test_user'

    sign_up_team(session_scope_function, event_name, username)
    event_team = session_scope_function.query(EventTeam).all()
    assert len(event_team) == 1
    event_team = event_team[0]

    # when signing up a team, the team is approved and the sandbox is setup:
    # the starting kit is submitted without training it.
    assert event_team.last_submission_name == 'starting_kit'
    assert event_team.approved is True
    # check the status of the sandbox submission
    submission = session_scope_function.query(Submission).all()
    assert len(submission) == 1
    submission = submission[0]
    assert submission.name == 'starting_kit'
    assert submission.event_team == event_team
    submission_file = submission.files[0]
    assert submission_file.name == 'classifier'
    assert submission_file.extension == 'py'
    assert (os.path.join('submission_000000001', 'classifier.py')
            in submission_file.path)
    # check the submission on cv fold
    cv_folds = session_scope_function.query(SubmissionOnCVFold).all()
    for fold in cv_folds:
        assert fold.state == 'new'
        assert fold.best is False
        assert fold.contributivity == pytest.approx(0)
Beispiel #4
0
def approve_sign_up_for_event(event_name, user_name):
    """Approve a user for a specific event.

    This way of approval is usually used by clicking in an email sent to the
    admin.

    Parameters
    ----------
    event_name : str
        The name of the event.
    user_name : str
        The name of the user.
    """
    event = get_event(db.session, event_name)
    user = User.query.filter_by(name=user_name).one_or_none()
    if not is_admin(db.session, event_name, flask_login.current_user.name):
        return redirect_to_user(
            u'Sorry {}, you do not have admin rights'.format(
                flask_login.current_user.firstname),
            is_error=True)
    if not event or not user:
        return redirect_to_user(u'No event {} or no user {}'.format(
            event_name, user_name),
                                is_error=True)
    sign_up_team(db.session, event.name, user.name)

    subject = ('Signed up for the RAMP event {}'.format(event.name))
    body = ('{}, you have been registered to the RAMP event {}. '
            'You can now proceed to your sandbox and make submission.'
            '\nHave fun!!!'.format(flask_login.current_user.name, event.name))
    send_mail(to=flask_login.current_user.email, subject=subject, body=body)

    return redirect_to_user(u'{} is signed up for {}.'.format(user, event),
                            is_error=False,
                            category='Successful sign-up')
Beispiel #5
0
def approve_users():
    """Approve new user to log-in and sign-up to events."""
    if not flask_login.current_user.access_level == 'admin':
        return redirect_to_user(
            'Sorry {}, you do not have admin rights'.format(
                flask_login.current_user.firstname),
            is_error=True)
    if request.method == 'GET':
        # TODO: replace by some get_functions
        asked_users = User.query.filter_by(access_level='asked').all()
        asked_sign_up = EventTeam.query.filter_by(approved=False).all()
        return render_template('approve.html',
                               asked_users=asked_users,
                               asked_sign_up=asked_sign_up,
                               admin=True)
    elif request.method == 'POST':
        users_to_be_approved = request.form.getlist('approve_users')
        event_teams_to_be_approved = request.form.getlist(
            'approve_event_teams')
        message = "{}d users:\n".format(request.form["submit_button"][:-1])
        for asked_user in users_to_be_approved:
            user = select_user_by_name(db.session, asked_user)
            if request.form["submit_button"] == "Approve!":
                approve_user(db.session, asked_user)

                subject = 'Your RAMP account has been approved'
                body = ('{}, your account has been approved. You can now '
                        'sign-up for any open RAMP event.'.format(user.name))
                send_mail(to=user.email, subject=subject, body=body)
            elif request.form["submit_button"] == "Remove!":
                delete_user(db.session, asked_user)
            message += "{}\n".format(asked_user)

        message += "{}d event_team:\n".format(
            request.form["submit_button"][:-1])
        for asked_id in event_teams_to_be_approved:
            asked_event_team = EventTeam.query.get(asked_id)
            user = select_user_by_name(db.session, asked_event_team.team.name)

            if request.form["submit_button"] == "Approve!":
                sign_up_team(db.session, asked_event_team.event.name,
                             asked_event_team.team.name)

                subject = ('Signed up for the RAMP event {}'.format(
                    asked_event_team.event.name))
                body = ('{}, you have been registered to the RAMP event {}. '
                        'You can now proceed to your sandbox and make '
                        'submissions.\nHave fun!!!'.format(
                            user.name, asked_event_team.event.name))
                send_mail(to=user.email, subject=subject, body=body)
            elif request.form["submit_button"] == "Remove!":
                delete_event_team(db.session, asked_event_team.event.name,
                                  asked_event_team.team.name)
            message += "{}\n".format(asked_event_team)
        return redirect_to_user(message,
                                is_error=False,
                                category="{}d users".format(
                                    request.form["submit_button"][:-1]))
Beispiel #6
0
def test_sandbox_upload_file(client_session, makedrop_event, submission_dir,
                             filename):
    client, session = client_session
    sign_up_team(session, "iris_test_4event", "test_user")

    config = ramp_config_template()
    ramp_config = generate_ramp_config(read_config(config))

    # upload file in sandbox.html
    path_submissions = os.path.join(ramp_config["ramp_kit_dir"],
                                    submission_dir)

    with login_scope(client, "test_user", "test") as client:
        rv = client.get("http://localhost/events/iris_test_4event/sandbox")
        assert rv.status_code == 200

        # choose file and check if it was uploaded correctly
        path_submission = os.path.join(path_submissions, filename)
        assert os.path.isfile(path_submission)

        rv = client.post(
            "http://localhost/events/iris_test_4event/sandbox",
            headers={
                "Referer": "http://localhost/events/iris_test_4event/sandbox"
            },
            data={"file": (open(path_submission, "rb"), filename)},
            follow_redirects=False,
        )

        assert rv.status_code == 302
        assert (
            rv.location == "http://localhost/events/iris_test_4event/sandbox")

        # code of the saved file
        with open(path_submission, "r") as file:
            submitted_data = file.read()

        # code from the db
        event = get_event(session, "iris_test_4event")
        sandbox_submission = get_submission_by_name(session,
                                                    "iris_test_4event",
                                                    "test_user",
                                                    event.ramp_sandbox_name)
        submission_code = sandbox_submission.files[-1].get_code()

        # get user interactions from db and check if 'upload' was added
        user_interactions = get_user_interactions_by_name(session, "test_user")

        # check if the code of the submitted file in the 'submission_code'
        assert submitted_data is not None
        assert submitted_data in submission_code
        # check if the user_interaction was added to the db
        assert "upload" in user_interactions["interaction"].values
Beispiel #7
0
def test_delete_event_team(session_scope_function):
    event_name, username = '******', 'test_user'

    sign_up_team(session_scope_function, event_name, username)
    event_team = session_scope_function.query(EventTeam).all()
    assert len(event_team) == 1

    delete_event_team(session_scope_function, event_name, username)
    event_team = session_scope_function.query(EventTeam).all()
    assert len(event_team) == 0

    # check that the user still exist
    user = (session_scope_function.query(User).filter(
        User.name == username).all())
    assert len(user) == 1
    event = (session_scope_function.query(Event).filter(
        Event.name == event_name).all())
    assert len(event) == 1
Beispiel #8
0
def test_sandbox_save_file(client_session, makedrop_event):
    client, session = client_session
    sign_up_team(session, "iris_test_4event", "test_user")

    example_code = "example content"

    with login_scope(client, "test_user", "test") as client:
        rv = client.get("http://localhost/events/iris_test_4event/sandbox")
        assert rv.status_code == 200

        rv = client.post(
            "http://localhost/events/iris_test_4event/sandbox",
            headers={
                "Referer": "http://localhost/events/iris_test_4event/sandbox"
            },
            data={
                "estimator": example_code,
                "code-csrf_token": "temp_token"
            },
            follow_redirects=False,
        )
        assert rv.status_code == 200

        # code from the db
        event = get_event(session, "iris_test_4event")
        sandbox_submission = get_submission_by_name(session,
                                                    "iris_test_4event",
                                                    "test_user",
                                                    event.ramp_sandbox_name)
        submission_code = sandbox_submission.files[-1].get_code()

        # get user interactions from db and check if 'save' was added
        user_interactions = get_user_interactions_by_name(session, "test_user")

        assert "save" in user_interactions["interaction"].values
        assert example_code in submission_code

    # make sure that after changing the code example
    # and reloading the page the code is still changed
    with login_scope(client, "test_user", "test") as client:
        rv = client.get("http://localhost/events/iris_test_4event/sandbox")
        assert rv.status_code == 200
        assert example_code.encode() in rv.data
Beispiel #9
0
def sign_up_for_event(event_name):
    """Landing page to sign-up to a specific RAMP event.

    Parameters
    ----------
    event_name : str
        The name of the event.
    """
    event = get_event(db.session, event_name)
    if not is_accessible_event(db.session, event_name,
                               flask_login.current_user.name):
        return redirect_to_user('{}: no event named "{}"'
                                .format(flask_login.current_user.firstname,
                                        event_name))
    if app.config['TRACK_USER_INTERACTION']:
        add_user_interaction(db.session, interaction='signing up at event',
                             user=flask_login.current_user, event=event)

    ask_sign_up_team(db.session, event.name, flask_login.current_user.name)
    if event.is_controled_signup:
        admin_users = User.query.filter_by(access_level='admin')
        for admin in admin_users:
            subject = ('Request to sign-up {} to RAMP event {}'
                       .format(event.name, flask_login.current_user.name))
            body = body_formatter_user(flask_login.current_user)
            url_approve = ('http://{}/events/{}/sign_up/{}'
                           .format(
                               app.config['DOMAIN_NAME'], event.name,
                               flask_login.current_user.name
                           ))
            body += ('Click on this link to approve the sign-up request: {}'
                     .format(url_approve))
            send_mail(admin, subject, body)
        return redirect_to_user("Sign-up request is sent to event admins.",
                                is_error=False, category='Request sent')
    sign_up_team(db.session, event.name, flask_login.current_user.name)
    return redirect_to_sandbox(
        event,
        '{} is signed up for {}.'
        .format(flask_login.current_user.firstname, event),
        is_error=False,
        category='Successful sign-up'
    )
Beispiel #10
0
def test_submit_button_enabled_disabled(client_session, makedrop_event,
                                        opening_date, public_date,
                                        closing_date, expected):
    client, session = client_session

    event = get_event(session, 'iris_test_4event')
    event.opening_timestamp = opening_date
    event.public_opening_timestamp = public_date
    event.closing_timestamp = closing_date
    session.commit()
    sign_up_team(session, 'iris_test_4event', 'test_user')

    with login_scope(client, 'test_user', 'test') as client:
        rv = client.get('http://localhost/events/iris_test_4event/sandbox')
        assert rv.status_code == 200
        # check for update button status on the generated .html
        if expected == b'event-close':
            assert 'disabled' in str(rv.data)  # should to be disabled
        else:
            assert 'disabled' not in str(rv.data)  # should not be disabled
Beispiel #11
0
def test_correct_message_sandbox(client_session, makedrop_event, opening_date,
                                 public_date, closing_date, expected):
    client, session = client_session

    event = get_event(session, 'iris_test_4event')
    event.opening_timestamp = opening_date
    event.public_opening_timestamp = public_date
    event.closing_timestamp = closing_date
    session.commit()
    sign_up_team(session, 'iris_test_4event', 'test_user')

    with login_scope(client, 'test_user', 'test') as client:
        rv = client.get('http://localhost/events/iris_test_4event/sandbox')
        assert rv.status_code == 200

        if NOW < opening_date:
            assert "Event submissions will open on the " in str(rv.data)
        elif NOW < closing_date:
            assert "Event submissions are open until " in str(rv.data)
        else:
            assert "This event closed on the " in str(rv.data)
Beispiel #12
0
def test_sandbox_upload_file_dont_exist(client_session, makedrop_event,
                                        submission_dir, filename):
    client, session = client_session
    sign_up_team(session, "iris_test_4event", "test_user")

    config = ramp_config_template()
    ramp_config = generate_ramp_config(read_config(config))

    # upload file in sandbox.html
    path_submissions = os.path.join(ramp_config["ramp_kit_dir"], )

    with login_scope(client, "test_user", "test") as client:
        rv = client.get("http://localhost/events/iris_test_4event/sandbox")
        assert rv.status_code == 200

        # choose file and check if it was uploaded correctly
        path_submission = os.path.join(path_submissions, filename)
        assert not os.path.isfile(path_submission)

        with pytest.raises(FileNotFoundError):
            rv = client.post(
                "http://localhost/events/iris_test_4event/sandbox",
                headers={
                    "Referer":
                    "http://localhost/events/iris_test_4event/sandbox"
                },
                data={"file": (open(path_submission, "rb"), filename)},
                follow_redirects=False,
            )

        assert rv.status_code == 200

        with pytest.raises(FileNotFoundError):
            with open(path_submission, "r") as file:
                submitted_data = file.read()
                assert not submitted_data

        # get user interactions from db and check if 'upload' was added
        user_interactions = get_user_interactions_by_name(session, "test_user")
        assert "upload" not in user_interactions["interaction"].values
Beispiel #13
0
def test_is_accessible_code(session_toy_db):
    # create a third user
    add_user(
        session_toy_db, name='test_user_3', password='******',
        lastname='Test_3', firstname='User_3',
        email='*****@*****.**', access_level='user')
    approve_user(session_toy_db, 'test_user_3')
    event_name = 'iris_test'
    sign_up_team(session_toy_db, event_name, 'test_user_3')
    # simulate a user which is not authenticated
    user = get_user_by_name(session_toy_db, 'test_user_2')
    user.is_authenticated = False
    assert not is_accessible_code(session_toy_db, event_name, user.name)
    # simulate a user which authenticated and author of the submission to a
    # public event
    user.is_authenticated = True
    assert is_accessible_code(session_toy_db, event_name, user.name)
    # simulate an admin user
    user = get_user_by_name(session_toy_db, 'test_iris_admin')
    user.is_authenticated = True
    assert is_accessible_code(session_toy_db, event_name, 'test_iris_admin')
    # simulate a user which is not signed up to the event
    user = add_user(session_toy_db, 'xx', 'xx', 'xx', 'xx', 'xx', 'user')
    user.is_authenticated = True
    assert not is_accessible_code(session_toy_db, event_name, user.name)
    # simulate that the event is not publicly opened
    event = get_event(session_toy_db, event_name)
    past_public_opening = event.public_opening_timestamp
    tomorrow = datetime.datetime.utcnow() + datetime.timedelta(days=1)
    event.public_opening_timestamp = tomorrow
    session_toy_db.commit()
    assert is_accessible_code(session_toy_db, event_name, 'test_user_3')
    # Make a submission
    submission_name = 'random_forest_10_10'
    ramp_config = generate_ramp_config(read_config(ramp_config_template()))
    path_submission = os.path.join(
        os.path.dirname(ramp_config['ramp_sandbox_dir']), submission_name
    )
    sub = add_submission(
        session_toy_db, event_name, 'test_user_3', submission_name,
        path_submission
    )
    # check that the user submitting the submission could access it
    assert is_accessible_code(
        session_toy_db, event_name, 'test_user_3', sub.id
    )
    # change the admin of the team
    from ramp_database.model import Team, User
    team = (session_toy_db.query(Team)
                          .filter(Team.name == 'test_user_3')
                          .first())
    user = (session_toy_db.query(User)
                          .filter(User.name == 'test_user_2')
                          .first())
    team.admin_id = user.id
    team.admin = user
    session_toy_db.commit()
    # check that the admin can access the submission
    assert is_accessible_code(
        session_toy_db, event_name, 'test_user_2', sub.id
    )
    # but others cannot
    assert not is_accessible_code(
        session_toy_db, event_name, 'test_user_3', sub.id
    )
    event.public_opening_timestamp = past_public_opening
    session_toy_db.commit()