def test_get_service_for_user(notify_api, notify_db, notify_db_session, sample_service): second_user = create_sample_user(notify_db, notify_db_session, '*****@*****.**') create_sample_service(notify_db, notify_db_session, service_name='Second Service', user=second_user) create_sample_service(notify_db, notify_db_session, service_name='Another Service', user=sample_service.users[0]) with notify_api.test_request_context(): with notify_api.test_client() as client: auth_header = create_authorization_header(path='/service', method='GET') resp = client.get('/service?user_id={}'.format( sample_service.users[0].id), headers=[auth_header]) assert resp.status_code == 200 json_resp = json.loads(resp.get_data(as_text=True)) assert len(json_resp['data']) == 2 print(x for x in json_resp['data']) assert 'Another Service' in [ x.get('name') for x in json_resp['data'] ] assert 'Sample service' in [ x.get('name') for x in json_resp['data'] ] assert 'Second Service' not in [ x.get('name') for x in json_resp['data'] ]
def test_post_service_multiple_users(notify_api, notify_db, notify_db_session, sample_user, sample_admin_service_id): """ Tests POST endpoint '/' to create a service with multiple users. """ with notify_api.test_request_context(): with notify_api.test_client() as client: another_user = create_sample_user( notify_db, notify_db_session, "*****@*****.**") assert Service.query.count() == 1 data = { 'name': 'created service', 'users': [sample_user.id, another_user.id], 'limit': 1000, 'restricted': False, 'active': False } auth_header = create_authorization_header( service_id=sample_admin_service_id, path=url_for('service.create_service'), method='POST', request_body=json.dumps(data)) headers = [('Content-Type', 'application/json'), auth_header] resp = client.post(url_for('service.create_service'), data=json.dumps(data), headers=headers) assert resp.status_code == 201 service = Service.query.filter_by(name='created service').first() json_resp = json.loads(resp.get_data(as_text=True)) assert json_resp['data']['name'] == service.name assert json_resp['data']['limit'] == service.limit assert len(service.users) == 2
def test_post_service_multiple_users(notify_api, notify_db, notify_db_session, sample_user, sample_admin_service_id): """ Tests POST endpoint '/' to create a service with multiple users. """ with notify_api.test_request_context(): with notify_api.test_client() as client: another_user = create_sample_user( notify_db, notify_db_session, "*****@*****.**") assert Service.query.count() == 1 data = { 'name': 'created service', 'users': [sample_user.id, another_user.id], 'limit': 1000, 'restricted': False, 'active': False} auth_header = create_authorization_header(service_id=sample_admin_service_id, path=url_for('service.create_service'), method='POST', request_body=json.dumps(data)) headers = [('Content-Type', 'application/json'), auth_header] resp = client.post( url_for('service.create_service'), data=json.dumps(data), headers=headers) assert resp.status_code == 201 service = Service.query.filter_by(name='created service').first() json_resp = json.loads(resp.get_data(as_text=True)) assert json_resp['data']['name'] == service.name assert json_resp['data']['limit'] == service.limit assert len(service.users) == 2
def test_get_all_users(notify_api, notify_db, notify_db_session, sample_user): assert User.query.count() == 1 assert len(get_user_by_id()) == 1 email = "*****@*****.**" another_user = create_sample_user(notify_db, notify_db_session, email=email) assert User.query.count() == 2 assert len(get_user_by_id()) == 2
def test_get_all_users(notify_api, notify_db, notify_db_session, sample_user): assert User.query.count() == 1 assert len(get_model_users()) == 1 email = "*****@*****.**" another_user = create_sample_user(notify_db, notify_db_session, email=email) assert User.query.count() == 2 assert len(get_model_users()) == 2
def test_get_services_for_user(notify_api, notify_db, notify_db_session, sample_service): assert Service.query.count() == 1 service_name = "Random service" second_user = create_sample_user(notify_db, notify_db_session, '*****@*****.**') create_sample_service(notify_db, notify_db_session, service_name='another service', user=second_user) sample_service = create_sample_service(notify_db, notify_db_session, service_name=service_name, user=sample_service.users[0]) assert Service.query.count() == 3 services = get_model_services(user_id=sample_service.users[0].id) assert len(services) == 2 assert service_name in [x.name for x in services] assert 'Sample service' in [x.name for x in services]
def test_get_service_for_user(notify_api, notify_db, notify_db_session, sample_service): second_user = create_sample_user(notify_db, notify_db_session, '*****@*****.**') create_sample_service(notify_db, notify_db_session, service_name='Second Service', user=second_user) create_sample_service(notify_db, notify_db_session, service_name='Another Service', user=sample_service.users[0]) with notify_api.test_request_context(): with notify_api.test_client() as client: auth_header = create_authorization_header( path='/service', method='GET') resp = client.get('/service?user_id={}'.format(sample_service.users[0].id), headers=[auth_header]) assert resp.status_code == 200 json_resp = json.loads(resp.get_data(as_text=True)) assert len(json_resp['data']) == 2 print(x for x in json_resp['data']) assert 'Another Service' in [x.get('name') for x in json_resp['data']] assert 'Sample service' in [x.get('name') for x in json_resp['data']] assert 'Second Service' not in [x.get('name') for x in json_resp['data']]
def test_put_service_remove_user(notify_api, notify_db, notify_db_session, sample_service, sample_admin_service_id): """ Tests PUT endpoint '/<service_id>' add user to the service. """ with notify_api.test_request_context(): with notify_api.test_client() as client: sample_user = sample_service.users[0] another_user = create_sample_user( notify_db, notify_db_session, "*****@*****.**") data = { 'name': sample_service.name, 'users': [sample_user.id, another_user.id], 'limit': sample_service.limit, 'restricted': sample_service.restricted, 'active': sample_service.active } save_model_service(sample_service, update_dict=data) assert Service.query.count() == 2 data['users'] = [another_user.id] auth_header = create_authorization_header( service_id=sample_admin_service_id, path=url_for('service.update_service', service_id=sample_service.id), method='PUT', request_body=json.dumps(data)) headers = [('Content-Type', 'application/json'), auth_header] resp = client.put(url_for('service.update_service', service_id=sample_service.id), data=json.dumps(data), headers=headers) assert Service.query.count() == 2 assert resp.status_code == 200 updated_service = Service.query.get(sample_service.id) json_resp = json.loads(resp.get_data(as_text=True)) assert len(json_resp['data']['users']) == 1 assert sample_user.id not in json_resp['data']['users'] assert another_user.id in json_resp['data']['users'] assert sample_user not in updated_service.users assert another_user in updated_service.users
def test_put_service_remove_user(notify_api, notify_db, notify_db_session, sample_service, sample_admin_service_id): """ Tests PUT endpoint '/<service_id>' add user to the service. """ with notify_api.test_request_context(): with notify_api.test_client() as client: sample_user = sample_service.users[0] another_user = create_sample_user( notify_db, notify_db_session, "*****@*****.**") data = { 'name': sample_service.name, 'users': [sample_user.id, another_user.id], 'limit': sample_service.limit, 'restricted': sample_service.restricted, 'active': sample_service.active} save_model_service(sample_service, update_dict=data) assert Service.query.count() == 2 data['users'] = [another_user.id] auth_header = create_authorization_header(service_id=sample_admin_service_id, path=url_for('service.update_service', service_id=sample_service.id), method='PUT', request_body=json.dumps(data)) headers = [('Content-Type', 'application/json'), auth_header] resp = client.put( url_for('service.update_service', service_id=sample_service.id), data=json.dumps(data), headers=headers) assert Service.query.count() == 2 assert resp.status_code == 200 updated_service = Service.query.get(sample_service.id) json_resp = json.loads(resp.get_data(as_text=True)) assert len(json_resp['data']['users']) == 1 assert sample_user.id not in json_resp['data']['users'] assert another_user.id in json_resp['data']['users'] assert sample_user not in updated_service.users assert another_user in updated_service.users
def test_get_user(notify_api, notify_db, notify_db_session): email = "*****@*****.**" another_user = create_sample_user(notify_db, notify_db_session, email=email) assert get_user_by_id(user_id=another_user.id).email_address == email
def test_get_user(notify_api, notify_db, notify_db_session): email = "*****@*****.**" another_user = create_sample_user(notify_db, notify_db_session, email=email) assert get_model_users(user_id=another_user.id).email_address == email