def test_settings_admin_set_survey_avatar_success(survey_client):
    credentials = {'email': '*****@*****.**', 'password': '******'}

    # get an admin jwt token
    jwt = get_jwt(survey_client, credentials)

    # get default and set custom avatar
    r = survey_client.get('/v1/profile/avatar',
                          headers={'Authorization': 'JWT ' + jwt},
                          content_type='multipart/form-data')
    expected = {
        'status': 'success',
        'type': 'SurveyProfileAvatar',
        'results': {
            'avatarUri': '/assets/static/defaultAvatar.png'
        }
    }
    assert json.loads(r.data) == expected

    with open(TEST_AVATAR_PATH, 'rb') as avatar_f:
        files = {'avatar': avatar_f}
        r = survey_client.post('/v1/profile/avatar',
                               headers={'Authorization': 'JWT ' + jwt},
                               data=files,
                               content_type='multipart/form-data')
        assert '/assets/user/avatars' in json.loads(
            r.data)['results']['avatarUri']
def test_admin_get_web_users(survey_client):
    credentials = {'email': '*****@*****.**', 'password': '******'}
    parameters = {
        'email': '*****@*****.**',
        'password': '******',
        'surveyName': 'TestSurvey',
        'jwt': get_jwt(survey_client, credentials)
    }
    create_researcher(survey_client, parameters)

    query = {
        'sorting': json.dumps({
            'column': 'created_at',
            'direction': 1
        }),
        'pageIndex': 1,
        'itemsPerPage': 10
    }
    r = survey_client.get(
        '/v1/webusers/table',
        query_string=query,
        headers={'Authorization': 'JWT ' + parameters['jwt']},
        content_type='application/json')
    assert r.status_code == 200
    results = json.loads(r.data)['results']
    assert results['pagination'] == {
        'totalPages': 1,
        'currentPage': 1,
        'totalItems': 2
    }
    assert len(results['data']) == 2
def test_settings_admin_set_prompts_parameters(survey_client):
    credentials = {'email': '*****@*****.**', 'password': '******'}

    # get an admin jwt token
    jwt = get_jwt(survey_client, credentials)

    # fetch the default survey settings and update prompts parameters
    r = survey_client.get('/v1/settings',
                          headers={'Authorization': 'JWT ' + jwt},
                          content_type='application/json')
    assert r.status_code == 200
    results = json.loads(r.data)['results']
    expected = {
        'tripbreakerSubwayStationBufferMeters': 300,
        'termsOfService': None,
        'aboutText': None,
        'surveyRecordMode': True,
        'surveyMaxDays': 14,
        'surveyRecordAcceleration': True,
        'contactEmail': '*****@*****.**',
        'surveyId': 1,
        'surveyStart': None,
        'tripbreakerIntervalSeconds': 360,
        'surveyMaxPrompts': 20
    }
    assert results == expected

    new_settings = {
        'aboutText': 'sample about text',
        'termsOfService': 'sample terms of service',
        'contactEmail': '*****@*****.**',
        'surveyMaxDays': 100,
        'surveyMaxPrompts': 50,
        'tripbreakerIntervalSeconds': 5,
        'tripbreakerSubwayStationBufferMeters': 20
    }
    r = survey_client.post('/v1/settings',
                           data=json.dumps(new_settings),
                           headers={'Authorization': 'JWT ' + jwt},
                           content_type='application/json')
    assert r.status_code == 201
    assert json.loads(r.data)['results'] == new_settings
def test_admin_delete_researcher_user(survey_client):
    credentials = {'email': '*****@*****.**', 'password': '******'}
    parameters = {
        'email': '*****@*****.**',
        'password': '******',
        'surveyName': 'TestSurvey',
        'jwt': get_jwt(survey_client, credentials)
    }
    create_researcher(survey_client, parameters)
    user = dashboard_db.web_user.find_by_email(parameters['email'])
    assert user

    # delete the created researcher user
    r = survey_client.delete(
        '/v1/webusers/{}'.format(parameters['email']),
        headers={'Authorization': 'JWT ' + parameters['jwt']},
        content_type='application/json')
    assert r.status_code == 201

    user = dashboard_db.web_user.find_by_email(parameters['email'])
    assert user is None
def test_admin_get_researcher_signup_token_success(app, survey_client):
    # get an admin jwt token
    credentials = {'email': '*****@*****.**', 'password': '******'}
    jwt = get_jwt(survey_client, credentials)

    # refresh jwt
    r = survey_client.post(
        '/v1/auth/refresh',
        headers={'Authentication':
                 jwt},  # note this uses Authentication vs Authorization
        # and does not include the JWT header
        content_type='application/json')
    response = json.loads(r.data)
    assert 'accessToken' in response and 'userLevel' in response
    jwt = response['accessToken']

    # retrieve and refresh researcher signup token
    r = survey_client.get('/v1/auth/signup/code',
                          headers={'Authorization': 'JWT ' + jwt},
                          content_type='application/json')
    token = json.loads(r.data)['results']['token']
    salt = app.config['SECURITY_PASSWORD_SALT']
    assert validate_registration_token(token, salt, expiration=5) is True
    assert validate_registration_token(token, salt, expiration=0) is False