def test_ui_mappings__must_be_batch_user_post(client, faker): login(client, faker) u2 = create_user(faker) batch = create_batch_with_columns(faker, u2) resp = client.get(url_for('ui.edit_mappings', batch_id=batch.id)) assert resp.status_code == 403
def test_ui_upload__batch_post_file_missing(client, faker): login(client, faker) data = dict(name='frederick') resp = client.post("/upload", data=data) assert resp.status_code == 200 assert Batch.query.count() == 0 assert_field_in_error_display(resp, 'Participants File')
def test_ui_upload__batch_post_name_incorrect_length(client, faker, name): login(client, faker) data = dict( name=name, participant_file=faker.participant_file_details()['attachment']) resp = client.post("/upload", data=data) assert resp.status_code == 200 assert Batch.query.count() == 0 assert_field_in_error_display(resp, 'Name')
def test_ui_delete__batch_delete_not_correct_user(client, faker): login(client, faker) u2 = create_user(faker) batch = create_batches(u2, 1, faker)[0] resp = client.post(url_for('ui.delete'), data=dict(id=batch.id)) assert resp.status_code == 403 batch = Batch.query.filter(Batch.id == batch.id).one() assert batch.deleted == False
def test_ui_mappings__batch_post_duplicate(client, faker, columns): u = login(client, faker) batch = create_batch_with_columns(faker, u) create_columns(batch, columns.keys()) data = {} for i, c in enumerate(batch.columns): data['column_mappings-{}-column_id'.format(i)] = c.id data['column_mappings-{}-column_name'.format(i)] = c.name data['column_mappings-{}-mapping'.format(i)] = columns[c.name] resp = client.post(url_for('ui.edit_mappings', batch_id=batch.id), data=data) assert resp.status_code == 200 assert_field_in_error_display(resp, 'POSTCODE') for name in columns.keys(): assert_field_in_error_display(resp, name) assert Column.query.filter( Column.batch_id == batch.id).count() == len(columns) for name in columns.keys(): assert Column.query.filter(Column.batch_id == batch.id).filter( Column.name == name).filter(Column.mapping == name).count() == 1
def test_ui_mappings__forms_csrf_token(client_with_crsf, faker): u = login(client_with_crsf, faker) batch = create_batch_with_columns(faker, u) assert__forms_csrf_token(client_with_crsf, faker, url_for('ui.edit_mappings', batch_id=batch.id), login=False)
def test_ui_upload__batch_post(client, faker, upload_files, name): HEADERS = "FORENAMES,SURNAME,DOB,SEX,POSTCODE,NHS_NUMBER,SYSTEM_NUMBER,ADDRESS1,ADDRESS2,ADDRESS3,ADDRESS4,ADDRESS5,LOCAL_ID" filename = "text.csv" u = login(client, faker) data = dict(name=name, participant_file=( BytesIO(HEADERS.encode('utf-8')), filename, )) resp = client.post("/upload", data=data) assert resp.status_code == 302 assert resp.location == 'http://localhost/' assert Batch.query.count() == 1 batch = Batch.query.filter(Batch.name == name).filter( Batch.user == u).filter(Batch.filename == filename).filter( Batch.created_date > resp.requested_time).filter( Batch.created_date < resp.received_time).one() assert batch upload_files.assert_file_created(batch, HEADERS) assert {c.column_index: c.name for c in batch.columns } == {i: c for i, c in enumerate(HEADERS.split(','), 1)} assert {c.column_index: c.mapping for c in batch.columns } == {i: c for i, c in enumerate(HEADERS.split(','), 1)}
def test_ui_mappings__html_boilerplate(client, faker): u = login(client, faker) batch = create_batch_with_columns(faker, u) assert__html_boilerplate(client, faker, url_for('ui.edit_mappings', batch_id=batch.id), login=False)
def test_ui_mappings__html_menu(client, faker): u = login(client, faker) batch = create_batch_with_columns(faker, u) assert__html_menu(client, faker, url_for('ui.edit_mappings', batch_id=batch.id), user=u)
def test_ui_upload__batch_get(client, faker): login(client, faker) resp = client.get("/upload") assert resp.status_code == 200 assert resp.soup.find('form', {'method': 'POST'}) is not None assert resp.soup.find('input', { 'name': 'name', 'type': 'text' }) is not None assert resp.soup.find('input', { 'name': 'participant_file', 'type': 'file' }) is not None assert resp.soup.find('a', href='/', text='Cancel') is not None assert resp.soup.find('button', type='submit', text='Save') is not None
def test_ui_list__deleted_batch_missing(client, faker): u = login(client, faker) batches = create_batches(u, 1, faker) batches[0].deleted = True update_batch(batches[0]) resp = client.get('/') _assert__boilerplate_html(resp, 0, batches)
def test_ui_delete__batch_delete_correct(client, faker): u = login(client, faker) batch = create_batches(u, 1, faker)[0] resp = client.post(url_for('ui.delete'), data=dict(id=batch.id)) assert resp.status_code == 302 assert resp.location == url_for('ui.index', _external=True) batch = Batch.query.filter(Batch.id == batch.id).one() assert batch.deleted == True
def test_ui_list__user_batch_list_beyond_last_page(client, faker): page_size = current_app.config["PAGE_SIZE"] u = login(client, faker) u2 = create_user(faker) batches = page_size page = 2 create_batches(u, batches, faker) create_batches(u2, batches, faker) resp = client.get('/?page={}'.format(page)) assert resp.status_code == 200
def test_ui_list__user_batch_list_search_found(client, faker): page_size = current_app.config["PAGE_SIZE"] batches = page_size u = login(client, faker) u2 = create_user(faker) expected_data = create_batches(u, batches, faker) create_batches(u2, batches, faker) expected_data[0].name = "Known Elephant" update_batch(expected_data[0]) resp = client.get('/?search={}'.format("Elephant")) _assert__boilerplate_html(resp, 1, expected_data[0:1])
def test_ui_mappings__batch_get(client, faker): COLUMNS = {'FORENAMES': 'FORENAMES', 'STRANGE': '', 'POSTCODE': 'POSTCODE'} u = login(client, faker) batch = create_batch_with_columns(faker, u) create_columns(batch, COLUMNS.keys()) resp = client.get(url_for('ui.edit_mappings', batch_id=batch.id)) assert resp.status_code == 200 assert resp.soup.find('form', {'method': 'POST'}) is not None assert resp.soup.find('a', href='/', text='Cancel') is not None assert resp.soup.find('button', type='submit', text='Save') is not None for c, m in COLUMNS.items(): col_name = resp.soup.find( 'input', type='hidden', id=lambda x: x and x.endswith('column_name'), value=lambda x: x and x.endswith(c), ) assert col_name prefix = col_name['id'][:len(col_name['id']) - len('column_name')] col_id = resp.soup.find( 'input', type='hidden', id=lambda x: x and x == '{}column_id'.format(prefix), ) assert col_id mapping = resp.soup.find( 'select', id=lambda x: x and x == '{}mapping'.format(prefix), ) assert mapping assert len(mapping.find_all('option')) == 14 selected = mapping.find('option', selected=True, value=m) assert selected
def test_ui_list__user_batch_list_last_page(client, faker, pages): page_size = current_app.config["PAGE_SIZE"] u = login(client, faker) u2 = create_user(faker) batches = pages * page_size expected_data = create_batches(u, batches, faker) create_batches(u2, batches, faker) resp = client.get('/?page={}'.format(pages)) _assert__boilerplate_html(resp, page_size, expected_data) assert__paginator( resp=resp, prev_page=pages - 1, next_page=None, active_page=pages, )
def test__passwords__change_wrong_old_password(client, faker): user = login(client, faker) password = "******" wrong_password = "******" new_password = "******" user.password = password db.session.add(user) db.session.commit() resp = client.post( url_for("security.change_password"), data={ "password": wrong_password, "new_password": new_password, "new_password_confirm": new_password, }, ) assert resp.status_code == 200 assert len(resp.soup.find("div", "errors").find_all("div")) > 0
def test_ui_mappings__batch_post_ok(client, faker, columns): u = login(client, faker) batch = create_batch_with_columns(faker, u) create_columns(batch, columns.keys()) data = {} for i, c in enumerate(batch.columns): data['column_mappings-{}-column_id'.format(i)] = c.id data['column_mappings-{}-column_name'.format(i)] = c.name data['column_mappings-{}-mapping'.format(i)] = columns[c.name] resp = client.post(url_for('ui.edit_mappings', batch_id=batch.id), data=data) assert resp.status_code == 302 assert Column.query.filter( Column.batch_id == batch.id).count() == len(columns) for name, mapping in columns.items(): assert Column.query.filter(Column.batch_id == batch.id).filter( Column.name == name).filter(Column.mapping == mapping).count() == 1
def test_ui_list__user_batch_list(client, faker, my_batches, other_user_batches): u = login(client, faker) u2 = create_user(faker) expected_data = create_batches(u, my_batches, faker) create_batches(u2, other_user_batches, faker) resp = client.get('/') page_size = current_app.config["PAGE_SIZE"] items_on_page = min(page_size, my_batches) _assert__boilerplate_html(resp, items_on_page, expected_data) pages = my_batches // page_size if pages > 1: assert__paginator( resp=resp, prev_page=None, next_page=2, active_page=1, )
def test__passwords__change(client, faker, new_password, valid): user = login(client, faker) password = "******" user.password = password db.session.add(user) db.session.commit() resp = client.post( url_for("security.change_password"), data={ "password": password, "new_password": new_password, "new_password_confirm": new_password, }, ) if valid: assert resp.status_code == 302 assert resp.soup.find("div", "errors") is None else: assert resp.status_code == 200 assert len(resp.soup.find("div", "errors").find_all("div")) > 0
def test_ui_delete__batch_delete_not_exists(client, faker): login(client, faker) resp = client.post(url_for('ui.delete'), data=dict(id='1')) assert resp.status_code == 404