def test_cannot_checkin_to_maxed_location(app, client, auth): # enter location with another user access_headers_other = get_access_headers( client.post('/auth/login', json={ 'username': '******', 'password': '******' })) client.post('/checkin', headers=access_headers_other, json={'location_id': 7}) access_headers = get_access_headers(auth.login()) response = client.post('/checkin', headers=access_headers, json={'location_id': 7}) response_json = response.get_json() assert response.status_code == 200 assert response_json['message'] == "Cannot enter, location is full." assert not response_json['success'] with app.app_context(): db = get_db() current_location = db.execute( 'SELECT current_location FROM user' ' WHERE id = 1').fetchone()['current_location'] assert current_location is None checks_data = db.execute('SELECT * FROM checks').fetchall() assert len(checks_data) == 1 assert checks_data[0]['author_id'] == 3
def test_user_is_not_in_risk_if_does_not_share_space_with_infected( app, client, auth): # enter location with another user access_headers_other = get_access_headers( client.post('/auth/login', json={ 'username': '******', 'password': '******' })) client.post('/checkin', headers=access_headers_other, json={'location_id': 1}) client.post('/checkout', headers=access_headers_other, json={'location_id': 1}) sleep(1.0) access_headers = get_access_headers(auth.login()) client.post('/checkin', headers=access_headers, json={'location_id': 1}) client.post('/checkout', headers=access_headers, json={'location_id': 1}) client.post('/inform/infection', headers=access_headers, json={'date': date.today()}) with app.app_context(): db = get_db() being_in_risk_since = db.execute( 'SELECT being_in_risk_since FROM user' ' WHERE id = 3').fetchone()['being_in_risk_since'] assert being_in_risk_since is None
def test_user_cannot_be_in_risk_if_it_is_infected(app, client, auth): access_headers = get_access_headers(auth.login()) client.post('/checkin', headers=access_headers, json={'location_id': 1}) access_headers_other = get_access_headers( client.post('/auth/login', json={ 'username': '******', 'password': '******' })) client.post('/checkin', headers=access_headers_other, json={'location_id': 1}) access_headers = get_access_headers(auth.login()) client.post('/checkin', headers=access_headers, json={'location_id': 1}) client.post('/checkout', headers=access_headers, json={'location_id': 1}) client.post('/inform/infection', headers=access_headers, json={'date': date.today()}) client.post('/checkout', headers=access_headers_other, json={'location_id': 1}) client.post('/inform/infection', headers=access_headers_other, json={'date': date.today()}) with app.app_context(): db = get_db() users_data = db.execute('SELECT * FROM user').fetchall() assert users_data[0]['being_in_risk_since'] is None assert users_data[0]['is_infected'] assert users_data[2]['being_in_risk_since'] is None assert users_data[2]['is_infected']
def test_user_gets_risk_from_most_recent_contact_with_infected( app, client, auth): access_headers = get_access_headers(auth.login()) access_headers_other = get_access_headers( client.post('/auth/login', json={ 'username': '******', 'password': '******' })) access_headers_another = get_access_headers( client.post('/auth/login', json={ 'username': '******', 'password': '******' })) client.post('/checkin', headers=access_headers_another, json={'location_id': 2}) client.post('/checkin', headers=access_headers_other, json={'location_id': 2}) client.post('/checkout', headers=access_headers_other, json={'location_id': 2}) client.post('/checkout', headers=access_headers_another, json={'location_id': 2}) client.post('/checkin', headers=access_headers, json={'location_id': 1}) client.post('/checkin', headers=access_headers_other, json={'location_id': 1}) client.post('/checkout', headers=access_headers_other, json={'location_id': 1}) before = datetime.now() sleep(1.0) client.post('/checkin', headers=access_headers_other, json={'location_id': 1}) client.post('/checkout', headers=access_headers_other, json={'location_id': 1}) client.post('/checkout', headers=access_headers, json={'location_id': 1}) client.post('/inform/infection', headers=access_headers, json={'date': date.today()}) client.post('/inform/infection', headers=access_headers_another, json={'date': date.today()}) with app.app_context(): db = get_db() being_in_risk_since = db.execute( 'SELECT being_in_risk_since FROM user' ' WHERE id = 3').fetchone()['being_in_risk_since'] assert string_to_datetime(being_in_risk_since) > before
def test_checkout(app, client, auth): access_headers = get_access_headers(auth.login()) client.post('/checkin', headers=access_headers, json={'location_id': 1}) response = client.post('/checkout', headers=access_headers, json={'location_id': 1}) response_json = response.get_json() assert response.status_code == 200 assert response_json['message'] == "Checkout successful." assert response_json['success'] with app.app_context(): db = get_db() user_data = db.execute('SELECT * FROM user' ' WHERE id = 1').fetchone() assert user_data['current_location'] is None people_inside_location = db.execute( 'SELECT people_inside FROM location' ' WHERE id = 1').fetchone()['people_inside'] assert people_inside_location == 0 check_data = db.execute('SELECT * FROM checks' ' WHERE author_id = 1').fetchone() assert check_data['location_id'] == 1 assert check_data['check_in_time'] is not None assert check_data['check_out_time'] is not None
def test_logout(client, auth): access_headers = get_access_headers(auth.login()) response = auth.logout(access_headers) assert response.status_code == 200 response = auth.logout(access_headers) assert response.status_code == 401
def test_create_check_fields(client, auth, json, error): access_headers = get_access_headers(auth.login()) response = client.post('/location/create', headers=access_headers, json=json) assert response.status_code == 400 assert error in response.data
def test_date_required(client, auth, url): access_headers = get_access_headers(auth.login()) response = client.post(url, headers=access_headers) assert response.status_code == 400 assert b'Missing json.' in response.data response = client.post(url, headers=access_headers, json={}) assert response.status_code == 400 assert b'Missing field: date.' in response.data
def test_author_not_required(client, auth, app): # change the location author to another user with app.app_context(): db = get_db() db.execute('UPDATE location SET author_id = 2 WHERE id = 1') db.commit() access_headers = get_access_headers(auth.login()) # current user can get other user's location assert client.get('/location/1', headers=access_headers).status_code == 200
def test_get(client, auth, app): access_headers = get_access_headers(auth.login()) response = client.get('/location/1', headers=access_headers) assert response.status_code == 200 json_data = response.get_json() assert 'test location 1' == json_data['name'] assert 10 == json_data['maximum_capacity'] assert 31.0 == json_data['latitude'] assert 61.0 == json_data['longitude'] assert 1 == json_data['author_id'] assert 1 == json_data['id']
def test_create_validate(client, auth, name, maximum_capacity, latitude, longitude, error): access_headers = get_access_headers(auth.login()) response = client.post('/location/create', headers=access_headers, json={ 'name': name, 'maximum_capacity': maximum_capacity, 'latitude': latitude, 'longitude': longitude }) assert response.status_code == 400 assert error in response.data
def test_infection(app, client, auth): access_headers = get_access_headers(auth.login()) assert client.post('/inform/infection', headers=access_headers, json={ 'date': date.today() }).status_code == 200 with app.app_context(): db = get_db() is_infected = db.execute('SELECT is_infected FROM user' ' WHERE id = 1').fetchone()['is_infected'] assert is_infected
def test_cannot_discharge_if_user_is_not_infected(app, client, auth): access_headers = get_access_headers(auth.login()) response = client.post('/inform/discharge', headers=access_headers, json={'date': date.today()}) assert response.status_code == 200 assert response.get_json()['message'] == 'User is not infected' with app.app_context(): db = get_db() is_infected = db.execute('SELECT is_infected FROM user' ' WHERE id = 1').fetchone()['is_infected'] assert not is_infected
def test_get_users_data(client, auth): # login with admin access_headers_admin = get_access_headers( client.post('/auth/login', json={ 'username': '******', 'password': '******' })) response = client.get('/admin/users', headers=access_headers_admin) users = response.get_json() assert len(users) == 4 assert users[0]['username'] == 'usertest'
def test_create(client, auth, app): access_headers = get_access_headers(auth.login()) assert client.post('/location/create', headers=access_headers, json={ 'name': 'created', 'maximum_capacity': 10, 'latitude': 5.0, 'longitude': 10.0 }).status_code == 201 with app.app_context(): db = get_db() count = db.execute('SELECT COUNT(id) FROM location').fetchone()[0] assert count == 8
def test_cannot_checkin_to_location_that_does_not_exist(app, client, auth): access_headers = get_access_headers(auth.login()) response = client.post('/checkin', headers=access_headers, json={'location_id': 8}) assert response.status_code == 404 assert b"Location id 8 doesn't exist." in response.data with app.app_context(): db = get_db() current_location = db.execute( 'SELECT current_location FROM user' ' WHERE id = 1').fetchone()['current_location'] assert current_location is None checks_amount = db.execute( 'SELECT COUNT(id) FROM checks').fetchone()[0] assert checks_amount == 0
def test_cannot_checkout_if_user_is_not_in_location(app, client, auth): access_headers = get_access_headers(auth.login()) response = client.post('/checkout', headers=access_headers, json={'location_id': 1}) response_json = response.get_json() assert response.status_code == 200 assert response_json['message'] == "Not current location." assert not response_json['success'] with app.app_context(): db = get_db() current_location = db.execute( 'SELECT current_location FROM user' ' WHERE id = 1').fetchone()['current_location'] assert current_location is None checks_amount = db.execute( 'SELECT COUNT(id) FROM checks').fetchone()[0] assert checks_amount == 0
def test_qrcode(client, auth): access_headers = get_access_headers(auth.login()) response = client.get('/location/1/qrcode', headers=access_headers) assert response.status_code == 200 assert response.mimetype == "image/png"
def test_exists_required(client, auth): access_headers = get_access_headers(auth.login()) assert client.get('/location/8', headers=access_headers).status_code == 404
def test_admin_required(client, auth): access_headers = get_access_headers(auth.login()) assert client.get('/admin/users', headers=access_headers).status_code == 403