def test_find_user(app, datastore): init_app_with_options(app, datastore) with app.app_context(): user_id = datastore.find_user(email="*****@*****.**").fs_uniquifier current_nqueries = get_num_queries(datastore) assert user_id == datastore.find_user( security_number=889900).fs_uniquifier end_nqueries = get_num_queries(datastore) if current_nqueries is not None: if is_sqlalchemy(datastore): # This should have done just 1 query across all attrs. assert end_nqueries == (current_nqueries + 1) assert user_id == datastore.find_user(username="******").fs_uniquifier
def test_token_query(in_app_context): # Verify that when authenticating with auth token (and not session) # that there is just one DB query to get user. app = in_app_context populate_data(app) client_nc = app.test_client(use_cookies=False) response = json_authenticate(client_nc) token = response.json["response"]["user"]["authentication_token"] current_nqueries = get_num_queries(app.security.datastore) response = client_nc.get( "/token", headers={"Content-Type": "application/json", "Authentication-Token": token}, ) assert response.status_code == 200 end_nqueries = get_num_queries(app.security.datastore) assert current_nqueries is None or end_nqueries == (current_nqueries + 1)
def test_create_user_with_roles(app, datastore): init_app_with_options(app, datastore) with app.app_context(): role = datastore.find_role("admin") datastore.commit() user = datastore.create_user(email="*****@*****.**", username="******", password="******", roles=[role]) datastore.commit() current_nqueries = get_num_queries(datastore) user = datastore.find_user(email="*****@*****.**") assert user.has_role("admin") is True end_nqueries = get_num_queries(datastore) # Verify that getting user and role is just one DB query assert current_nqueries is None or end_nqueries == (current_nqueries + 1)
def test_session_query(in_app_context): # Verify that when authenticating with auth token (but also sending session) # that there are 2 DB queries to get user. # This is since the session will load one - but auth_token_required needs to # verify that the TOKEN is valid (and it is possible that the user_id in the # session is different that the one in the token (huh?) app = in_app_context populate_data(app) client = app.test_client() response = json_authenticate(client) token = response.json["response"]["user"]["authentication_token"] current_nqueries = get_num_queries(app.security.datastore) response = client.get( "/token", headers={"Content-Type": "application/json", "Authentication-Token": token}, ) assert response.status_code == 200 end_nqueries = get_num_queries(app.security.datastore) assert current_nqueries is None or end_nqueries == (current_nqueries + 2)