def _process_apage(self, resp_data, bridge_users): custom_fields_value_dict = self._get_custom_fields_dict( resp_data.get("linked")) # a dict of {custom_field_value_id: BridgeCustomField} for user_data in resp_data.get("users"): try: user = BridgeUser( bridge_id=int(user_data["id"]), netid=re.sub('@uw.edu', '', user_data["uid"]), email=user_data.get("email", ""), full_name=user_data.get("full_name", ""), first_name=user_data.get("first_name"), last_name=user_data.get("last_name"), department=user_data.get("department"), job_title=user_data.get("job_title"), locale=user_data.get("locale", "en"), is_manager=user_data.get("is_manager"), unsubscribed=user_data.get("unsubscribed"), deleted_at=parse_date(user_data.get("deleted_at")), logged_in_at=parse_date(user_data.get("loggedInAt")), updated_at=parse_date(user_data.get("updated_at")), next_due_date=parse_date(user_data.get("next_due_date")), completed_courses_count=user_data.get( "completed_courses_count", -1)) if user_data.get("manager_id") is not None: user.manager_id = int(user_data["manager_id"]) if (user_data.get("links") is not None and len(user_data["links"]) > 0 and "custom_field_values" in user_data["links"]): values = user_data["links"]["custom_field_values"] for custom_field_value in values: if custom_field_value in custom_fields_value_dict: custom_field = custom_fields_value_dict[ custom_field_value] user.custom_fields[custom_field.name] =\ custom_field if user_data.get("roles") is not None: for role_data in user_data["roles"]: user.roles.append( self.user_roles.new_user_role_by_id(role_data)) bridge_users.append(user) except Exception as err: logger.error("{0} in {1}".format(str(err), user_data)) return bridge_users
def get_bridge_user_to_add(self, person, hrp_wkr): """ :param person: a valid Person object :return: a BridgeUser object """ user = BridgeUser(netid=person.uwnetid, email=get_email(person), full_name=get_full_name(person), first_name=normalize_name(person.first_name), last_name=normalize_name(person.surname), job_title=get_job_title(hrp_wkr), manager_id=get_supervisor_bridge_id(hrp_wkr)) self.add_custom_field(user, BridgeCustomField.REGID_NAME, person.uwregid) if person.employee_id is not None: self.add_custom_field(user, BridgeCustomField.EMPLOYEE_ID_NAME, person.employee_id) if person.student_number is not None: self.add_custom_field(user, BridgeCustomField.STUDENT_ID_NAME, person.student_number) for pos_num in range(self.total_work_positions): pos_field_names = WORK_POSITION_FIELDS[pos_num] for i in range(len(pos_field_names)): self.add_custom_field(user, pos_field_names[i], GET_POS_ATT_FUNCS[i](hrp_wkr, pos_num)) return user
def test_get_obj_from_list(self): users = [] self.assertIsNone( TestBridgeUser.users._get_obj_from_list("test", users)) users.append( BridgeUser(netid="eight", full_name="Eight Class Student", first_name="Eight Class", last_name="Student", email="*****@*****.**")) users.append( BridgeUser(netid="eight1", full_name="Eight1 Class Student", first_name="Eight1 Class", last_name="Student", email="*****@*****.**")) self.assertIsNotNone( TestBridgeUser.users._get_obj_from_list("test", users))
def get_mock_bridge_user(bridge_id, uwnetid, email, display_name, first_name, surname, uwregid): user = BridgeUser(bridge_id=bridge_id, netid=uwnetid, email=email, full_name=display_name, first_name=first_name, last_name=surname) user.custom_fields[BridgeCustomField.REGID_NAME] = \ new_custom_field(BridgeCustomField.REGID_NAME, uwregid) return user
def test_get_custom_field_value(self): bridge_acc = BridgeUser(netid='javerage') self.assertEqual( get_custom_field_value(bridge_acc, BridgeCustomField.REGID_NAME), "") bridge_acc.custom_fields[BridgeCustomField.REGID_NAME] = \ new_custom_field(BridgeCustomField.REGID_NAME, "1") self.assertEqual( get_custom_field_value(bridge_acc, BridgeCustomField.STUDENT_ID_NAME), "") self.assertEqual( get_custom_field_value(bridge_acc, BridgeCustomField.REGID_NAME), "1")
def test_update_user(self): orig_user = TestBridgeUser.users.get_user_by_id(17637, include_deleted=True) upded_user = TestBridgeUser.users.update_user(orig_user) self.verify_bill(upded_user) self.assertFalse(upded_user.has_custom_field()) self.assertFalse(upded_user.is_deleted()) self.assertEqual(str(upded_user.updated_at), '2016-09-08 13:58:20.635000-07:00') user = BridgeUser(netid='bill', first_name='Bill Average', last_name='Teacher', email='*****@*****.**', full_name='Bill Average Teacher') upded_user = TestBridgeUser.users.update_user(user) self.verify_bill(upded_user) self.assertFalse(upded_user.has_custom_field()) orig_user = TestBridgeUser.users.get_user('javerage') self.assertRaises(DataFailureException, TestBridgeUser.users.update_user, orig_user)
def test_add_user(self): regid = "12345678901234567890123456789012" cus_fie = TestBridgeUser.users.custom_fields.new_custom_field( BridgeCustomField.REGID_NAME, regid) user = BridgeUser() user.netid = "eight" user.full_name = "Eight Class Student" user.first_name = "Eight Class" user.last_name = "Student" user.email = "*****@*****.**" user.custom_fields[cus_fie.name] = cus_fie added = TestBridgeUser.users.add_user(user) self.assertEqual(added.bridge_id, 123) self.assertEqual(added.first_name, "Eight Class") self.assertEqual(added.last_name, "Student") self.assertEqual(added.full_name, "Eight Class Student") self.assertEqual(added.get_sortable_name(), "Student, Eight Class") self.assertEqual(added.email, "*****@*****.**") self.assertEqual(added.netid, "eight") self.assertEqual(added.get_uid(), "*****@*****.**") self.assertEqual(str(added.updated_at), '2016-09-06 21:42:48.821000-07:00')
def test_bridge_user(self): user = BridgeUser(netid="iamstudent", email="*****@*****.**", full_name="Iam Student", department="XYZ", job_title="y", manager_netid="mana", bridge_id=1) self.assertEqual( user.to_json(), { "id": 1, 'uid': '*****@*****.**', 'email': '*****@*****.**', 'full_name': 'Iam Student', 'manager_id': "uid:[email protected]", 'department': "XYZ", 'job_title': "y" }) self.assertEqual(user.get_uid(), "*****@*****.**") self.assertIsNotNone(str(user)) self.assertTrue(user.has_bridge_id()) self.assertFalse(user.has_last_name()) self.assertFalse(user.has_first_name()) self.assertTrue(user.has_manager()) self.assertFalse(user.is_deleted()) self.assertFalse(user.has_course_summary()) self.assertFalse(user.no_learning_history()) self.assertFalse(user.has_custom_field()) user.update_custom_field(BridgeCustomField.REGID_NAME, "1") self.assertIsNone(user.get_custom_field(BridgeCustomField.REGID_NAME)) user.updated_at = parse("2016-08-08T13:58:20.635-07:00") self.assertIsNotNone(str(user)) user.first_name = "Iam A" user.last_name = "Student" user.manager_netid = None user.manager_id = 0 user.bridge_id = 0 self.assertEqual(user.get_sortable_name(), "Student, Iam A") self.assertTrue(user.has_last_name()) self.assertTrue(user.has_first_name()) self.assertFalse(user.has_manager()) self.assertEqual( user.to_json(), { 'email': '*****@*****.**', 'first_name': 'Iam A', 'full_name': 'Iam Student', 'last_name': 'Student', 'department': "XYZ", 'job_title': "y", 'manager_id': None, 'sortable_name': 'Student, Iam A', 'uid': '*****@*****.**' }) user.completed_courses_count = 3 self.assertTrue(user.has_course_summary()) self.assertFalse(user.no_learning_history()) self.assertIsNotNone(str(user)) regid = BridgeCustomField(field_id="5", value_id="1", name=BridgeCustomField.REGID_NAME, value="12345678901234567890123456789012") eid = BridgeCustomField(value_id="2", field_id="6", name=BridgeCustomField.EMPLOYEE_ID_NAME, value="123456789") user.custom_fields[regid.name] = regid user.custom_fields[eid.name] = eid self.assertTrue(user.has_custom_field()) self.assertEqual(len(user.custom_fields), 2) self.assertEqual( user.get_custom_field(BridgeCustomField.REGID_NAME).value, "12345678901234567890123456789012") self.assertIsNotNone(str(user)) self.assertEqual(user.custom_fields_json(), [{ 'value': '12345678901234567890123456789012', 'custom_field_id': '5', 'id': '1' }, { 'value': '123456789', 'custom_field_id': '6', 'id': '2' }]) user.bridge_id = 123 self.assertEqual( user.to_json_patch(), { 'user': { 'uid': '*****@*****.**', 'id': 123, 'email': '*****@*****.**', 'first_name': 'Iam A', 'last_name': 'Student', 'full_name': 'Iam Student', 'sortable_name': 'Student, Iam A', 'department': 'XYZ', 'manager_id': None, 'job_title': 'y', 'custom_field_values': [{ 'value': '12345678901234567890123456789012', 'custom_field_id': '5', 'id': '1' }, { 'value': '123456789', 'custom_field_id': '6', 'id': '2' }] } }) user.manager_id = 1 self.assertEqual( user.to_json_post(), { 'users': [{ 'uid': '*****@*****.**', 'id': 123, 'email': '*****@*****.**', 'first_name': 'Iam A', 'last_name': 'Student', 'full_name': 'Iam Student', 'sortable_name': 'Student, Iam A', 'department': 'XYZ', 'job_title': 'y', 'manager_id': 1, 'custom_field_values': [{ 'value': '12345678901234567890123456789012', 'custom_field_id': '5', 'id': '1' }, { 'value': '123456789', 'custom_field_id': '6', 'id': '2' }] }] }) user.update_custom_field(BridgeCustomField.REGID_NAME, None) self.assertIsNone( user.get_custom_field(BridgeCustomField.REGID_NAME).value)
def test_bridge_user_role(self): user = BridgeUser(netid="iamstudent", email="*****@*****.**", full_name="Iam Student") role1 = BridgeUserRole(role_id='9a0d0b25', name='Campus Admin') self.assertEqual(role1.to_json(), { 'id': '9a0d0b25', 'name': 'Campus Admin' }) self.assertTrue(role1.is_campus_admin()) user.add_role(role1) self.assertEqual(len(user.roles), 1) user.add_role(role1) self.assertEqual(len(user.roles), 1) role = BridgeUserRole(role_id='author', name='Author') self.assertEqual(role.to_json(), {'id': 'author', 'name': 'Author'}) self.assertTrue(role.is_author()) self.assertIsNotNone(str(role)) user.add_role(role) role = BridgeUserRole(role_id="account_admin", name="Account Admin") self.assertTrue(role.is_account_admin()) user.add_role(role) role = BridgeUserRole(role_id="it_admin", name="IT Admin") self.assertTrue(role.is_it_admin()) user.add_role(role) role = BridgeUserRole(role_id="admin", name="Admin") self.assertTrue(role.is_admin()) user.add_role(role) self.assertFalse(role1 == role) self.assertIsNotNone(str(user)) self.assertEqual( user.roles_to_json(), ['9a0d0b25', 'author', 'account_admin', 'it_admin', 'admin']) user.delete_role(role1) self.assertEqual(user.roles_to_json(), ['author', 'account_admin', 'it_admin', 'admin']) user.delete_role(role1) self.assertEqual(len(user.roles), 4)