def looker_test_users(sdk: methods.LookerSDK, test_users: List[sheets.User]): yield test_users for test_user in test_users: users = sdk.search_users(email=test_user.email) if len(users) > 0: assert users[0].id sdk.delete_user(user_id=users[0].id)
def set_user_attributes(*, sdk: methods.LookerSDK, user_id, hackathon): hackathon_attr_id = get_hackathon_attr_id(sdk=sdk) assert hackathon_attr_id sdk.set_user_attribute_user_value( user_id=user_id, user_attribute_id=hackathon_attr_id, body=models.WriteUserAttributeWithValue(value=hackathon), )
def set_user_group(*, sdk: methods.LookerSDK, user_id: int, hackathon: str): global LOOKER_GROUP_PREFIX # TODO - switch to sdk.search_groups once that method is live on # sandboxcl and hack instances groups = sdk.all_groups(fields="id,name") name = f"{LOOKER_GROUP_PREFIX}{hackathon}" for group in groups: if group.name == name: break else: role = get_hackathon_role(sdk=sdk) assert role.id role_groups = [] for g in sdk.role_groups(role_id=role.id, fields="id"): assert g.id role_groups.append(g.id) group = sdk.create_group(body=models.WriteGroup(name=name)) assert group.id role_groups.append(group.id) sdk.set_role_groups(role_id=role.id, body=role_groups) assert group.id sdk.add_group_user( group_id=group.id, body=models.GroupIdForGroupUserInclusion(user_id=user_id))
def find_or_create_user(*, sdk: methods.LookerSDK, first_name: str, last_name: str, email: str) -> models.User: try: users = sdk.search_users(email=email) if users: user = users[0] if (user.first_name != first_name or user.last_name != last_name or user.is_disabled): assert user.id user = sdk.update_user( user_id=user.id, body=models.WriteUser(first_name=first_name, last_name=last_name, is_disabled=False), ) else: user = sdk.create_user( models.WriteUser(first_name=first_name, last_name=last_name)) except error.SDKError as create_ex: raise RegisterError(f"Failed to find or create User ({create_ex})") return user
def get_hackathon_attr_id(*, sdk: methods.LookerSDK) -> int: global HACKATHON_ATTR_ID if HACKATHON_ATTR_ID is not None: return HACKATHON_ATTR_ID main_hackathon = "hackathon" user_attrs = sdk.all_user_attributes(fields="name,id") for user_attr in user_attrs: if user_attr.name == main_hackathon: HACKATHON_ATTR_ID = user_attr.id break else: attrib = sdk.create_user_attribute(body=models.WriteUserAttribute( name=main_hackathon, label="Looker Hackathon", type="string")) if not attrib: raise RegisterError( f"Could not find '{main_hackathon}' user attribute") else: HACKATHON_ATTR_ID = attrib.id return HACKATHON_ATTR_ID
def run_queries(looker_sdk: methods.LookerSDK, test_model): test_model_name = test_model["name"] for e in test_model["explores"]: if e.get("unused", False): continue fields_to_query = [ f["name"] for f in e["fields"] if not (f.get("unused", False) or f.get("filter_only", False)) ] filters = { f["name"]: f["value"] for f in e["fields"] if f.get("filter_only", False) } looker_sdk.run_inline_query( result_format="json", body=models.WriteQuery( model=test_model_name, view=e["name"], fields=fields_to_query, filters=filters, ), )
def get_hackathon_role(*, sdk: methods.LookerSDK) -> models.Role: global HACKATHON_ROLE if HACKATHON_ROLE is not None: return HACKATHON_ROLE for role in sdk.all_roles(fields="name,id"): if role.name == "Hackathon": HACKATHON_ROLE = role assert HACKATHON_ROLE.id break else: raise RegisterError("Hackathon role needs to be created") return HACKATHON_ROLE
def test_enable_users_by_hackathons(looker_test_users: List[sheets.User], sdk: methods.LookerSDK): test_user1, test_user2, test_user3, test_user4 = looker_test_users looker.register_user( hackathon="hack_1", first_name=test_user1.first_name, last_name=test_user1.last_name, email=test_user1.email, ) looker.register_user( hackathon="hack_2", first_name=test_user2.first_name, last_name=test_user2.last_name, email=test_user2.email, ) looker.register_user( hackathon="hack_1", first_name=test_user3.first_name, last_name=test_user3.last_name, email=test_user3.email, ) looker.register_user( hackathon="hack_2", first_name=test_user4.first_name, last_name=test_user4.last_name, email=test_user4.email, ) assert sdk.search_users(fields="is_disabled", email=test_user1.email)[0].is_disabled assert sdk.search_users(fields="is_disabled", email=test_user2.email)[0].is_disabled assert sdk.search_users(fields="is_disabled", email=test_user3.email)[0].is_disabled assert sdk.search_users(fields="is_disabled", email=test_user4.email)[0].is_disabled looker.enable_users_by_hackathons(hackathons=["hack_1", "hack_2"]) assert not sdk.search_users(fields="is_disabled", email=test_user1.email)[0].is_disabled assert not sdk.search_users(fields="is_disabled", email=test_user2.email)[0].is_disabled assert not sdk.search_users(fields="is_disabled", email=test_user3.email)[0].is_disabled assert not sdk.search_users(fields="is_disabled", email=test_user4.email)[0].is_disabled
def disable_user(*, sdk: methods.LookerSDK, user_id: int): sdk.update_user(user_id=user_id, body=models.WriteUser(is_disabled=True))
def create_api3_credentials(*, sdk: methods.LookerSDK, user_id: int) -> models.CredentialsApi3: return sdk.create_user_credentials_api3(user_id=user_id, body=models.CredentialsApi3())
def create_email_credentials(*, sdk: methods.LookerSDK, user_id: int, email: str): sdk.create_user_credentials_email( user_id=user_id, body=models.WriteCredentialsEmail(email=email))
def test_register_user( looker_test_users: List[sheets.User], sdk: methods.LookerSDK, register_twice: bool ): test_hackathon = "Some Hackathon" test_user = looker_test_users[0] looker.register_user( hackathon=test_hackathon, first_name=test_user.first_name, last_name=test_user.last_name, email=test_user.email, ) if register_twice: looker.register_user( hackathon=test_hackathon, first_name=test_user.first_name, last_name=test_user.last_name, email=test_user.email, ) users = sdk.search_users(email=test_user.email) assert len(users) > 0 actual_user = users[0] assert actual_user.first_name == test_user.first_name assert actual_user.last_name == test_user.last_name assert actual_user.credentials_email assert actual_user.credentials_api3 assert len(actual_user.credentials_api3) == 1 assert actual_user.group_ids assert len(actual_user.group_ids) == 2 assert actual_user.is_disabled groups = sdk.all_groups(ids=models.DelimSequence(actual_user.group_ids)) for group in groups: if group.name == f"Looker_Hack: {test_hackathon}": break else: pytest.fail(f"Failed to find or create 'Looker_Hack: {test_hackathon}'") for role in sdk.all_roles(fields="name,id"): if role.name == "Hackathon": break else: pytest.fail("Bad test setup, failed to find 'Hackathon' role") assert role.id role_groups = sdk.role_groups(role_id=role.id, fields="id") for role_group in role_groups: if role_group.id == group.id: break else: pytest.fail( f"Failed to assign group 'Looker_Hack: {test_hackathon}' to role 'Hackathon'" ) assert actual_user.id actual_attributes = sdk.user_attribute_user_values(user_id=actual_user.id) assert actual_attributes for actual_attribute in actual_attributes: if actual_attribute.name == "hackathon": assert actual_attribute.value == test_hackathon break else: assert False, "Not assigned hackathon role"