def test_user_fullname(self): user = PWS().get_person_by_netid('javerage') name = user_fullname(user) self.assertEquals(len(name), 2) self.assertEquals(name[0], 'Jamesy') self.assertEquals(name[1], 'McJamesy') user = PWS().get_person_by_regid('8BD26A286A7D11D5A4AE0004AC494FFE') name = user_fullname(user) self.assertEquals(len(name), 2) self.assertEquals(name[0], 'BILL AVERAGE') self.assertEquals(name[1], 'TEACHER') # non-personal netid user = PWS().get_entity_by_netid('somalt') name = user_fullname(user) self.assertEquals(len(name), 1) self.assertEquals(name[0], user.display_name) user = get_person_by_gmail_id('*****@*****.**') name = user_fullname(user) self.assertEquals(len(name), 1) self.assertEquals(name[0], 'john.smith') user = InvalidPerson() self.assertRaises(UserPolicyException, user_fullname, user)
def test_add_user(self): person = PWS().get_person_by_netid('javerage') user = User.objects.add_user(person) self.assertEquals(user.reg_id, person.uwregid) self.assertEquals(user.net_id, person.uwnetid) self.assertEquals(user.priority, User.PRIORITY_HIGH) User.objects.all().delete() person = PWS().get_person_by_netid('javerage') user = User.objects.add_user(person, priority=User.PRIORITY_NONE) self.assertEquals(user.reg_id, person.uwregid) self.assertEquals(user.net_id, person.uwnetid) self.assertEquals(user.priority, User.PRIORITY_NONE) User.objects.all().delete()
def test_user_csv(self): user = PWS().get_person_by_netid('javerage') self.assertEquals(str(UserCSV(user, 'active')), ('9136CCB8F66711D5BE060004AC494FFE,javerage,,Jamesy,' 'McJamesy,,,,[email protected],active\n')) user = PWS().get_entity_by_netid('somalt') self.assertEquals( str(UserCSV(user, 'active')), ('605764A811A847E690F107D763A4B32A,somalt,,,,' 'SOM ACADEMIC LRNG TECHNOLOGY,,,[email protected],active\n')) user = get_person_by_gmail_id('*****@*****.**') self.assertEquals(str(UserCSV(user, 'active')), ('[email protected],[email protected],,,,' 'john.smith,,,[email protected],active\n'))
def test_user_sis_id(self): user = PWS().get_person_by_netid('javerage') self.assertEquals(user_sis_id(user), '9136CCB8F66711D5BE060004AC494FFE') user = get_person_by_gmail_id('*****@*****.**') self.assertEquals(user_sis_id(user), '*****@*****.**')
def get_person_by_regid(regid): pws = PWS() try: person = pws.get_person_by_regid(regid) valid_net_id(person.uwnetid) if person.is_test_entity: valid_nonpersonal_net_id(person.uwnetid) except DataFailureException as err: if err.status == 404: # Non-personal regid? person = pws.get_entity_by_regid(regid) valid_nonpersonal_net_id(person.uwnetid) else: raise return person
def test_update_priority(self): person = PWS().get_person_by_netid('javerage') user = User.objects.add_user(person) self.assertEquals(user.priority, User.PRIORITY_HIGH) user = User.objects.update_priority(person, User.PRIORITY_NONE) self.assertEquals(user.priority, User.PRIORITY_NONE) User.objects.all().delete()
def valid_net_id(login_id): if not login_id: raise MissingLoginIdException('Missing UWNetID') if RE_TEMPORARY_NETID.match(login_id): raise TemporaryNetidException('Temporary UWNetID not permitted') if not PWS().valid_uwnetid(login_id): raise InvalidLoginIdException('Not a valid UWNetID')
def get_photo(photo_key): data = cache.get(cache_key(photo_key)) cache.delete(cache_key(photo_key)) if data is None: raise ObjectDoesNotExist() return PWS().get_idcard_photo(data.get('reg_id'), size=data.get('image_size'))
def test_users(self): user = PWS().get_person_by_netid('javerage') csv = Collector() self.assertEquals(len(csv.users), 0) self.assertEquals(csv.add(UserCSV(user, 'active')), True) self.assertEquals(len(csv.users), 1) self.assertEquals(csv.add(UserCSV(user, 'active')), False) self.assertEquals(csv.has_data(), True)
def test_with_kwargs(self): user = PWS().get_person_by_netid('javerage') data = {'section_id': 'abc', 'role': 'Student', 'person': user} # No status self.assertRaises(EnrollmentPolicyException, EnrollmentCSV, **data) # Invalid status data['status'] = 'status' self.assertRaises(EnrollmentPolicyException, EnrollmentCSV, **data) data['status'] = 'active' self.assertEquals( str(EnrollmentCSV(**data)), ',,9136CCB8F66711D5BE060004AC494FFE,student,,abc,active,\n') data['status'] = 'deleted' self.assertEquals( str(EnrollmentCSV(**data)), ',,9136CCB8F66711D5BE060004AC494FFE,student,,abc,deleted,\n') data = {'status': 'active', 'role': 'Student', 'person': user} # No course or section self.assertRaises(EnrollmentPolicyException, EnrollmentCSV, **data) data['course_id'] = 'abc' self.assertEquals( str(EnrollmentCSV(**data)), 'abc,,9136CCB8F66711D5BE060004AC494FFE,student,,,active,\n') data['section_id'] = 'abc--' self.assertEquals( str(EnrollmentCSV(**data)), 'abc,,9136CCB8F66711D5BE060004AC494FFE,student,,abc--,active,\n') # Missing role data['role'] = '' self.assertRaises(EnrollmentPolicyException, EnrollmentCSV, **data) data['role'] = 'TaEnrollment' self.assertEquals( str(EnrollmentCSV(**data)), 'abc,,9136CCB8F66711D5BE060004AC494FFE,ta,,abc--,active,\n') data['role'] = 'Librarian' # Known custom role self.assertEquals( str(EnrollmentCSV(**data)), 'abc,,9136CCB8F66711D5BE060004AC494FFE,Librarian,,abc--,active,\n') data['role'] = 'Custom' # Unknown custom role self.assertEquals( str(EnrollmentCSV(**data)), 'abc,,9136CCB8F66711D5BE060004AC494FFE,Custom,,abc--,active,\n')
def test_enrollments(self): user = PWS().get_person_by_netid('javerage') csv = Collector() self.assertEquals(len(csv.enrollments), 0) self.assertEquals( csv.add( EnrollmentCSV(section_id='abc', person=user, role='Student', status='active')), True) self.assertEquals(len(csv.enrollments), 1) section = get_section_by_label('2013,winter,DROP_T,100/B') for registration in get_registrations_by_section(section): self.assertEquals( csv.add(EnrollmentCSV(registration=registration)), True) self.assertEquals(len(csv.enrollments), 3) section = get_section_by_label('2013,spring,TRAIN,101/A') for user in section.get_instructors(): self.assertEquals( csv.add( EnrollmentCSV(section=section, instructor=user, status='active')), True) # Duplicate self.assertEquals( csv.add( EnrollmentCSV(section=section, instructor=user, status='active')), False) self.assertEquals(len(csv.enrollments), 5) self.assertEquals(csv.has_data(), True) # Ad-hoc enrollment self.assertEquals( csv.add( EnrollmentCSV(course_id='course_123', section_id='section_123', person=user, role='Observer', status='active')), True) self.assertEquals(len(csv.enrollments), 6) # Duplicate self.assertEquals( csv.add( EnrollmentCSV(course_id='course_123', section_id='section_123', person=user, role='Observer', status='active')), False)
def test_json_data(self): person = PWS().get_person_by_netid('javerage') user = User.objects.add_user(person) json = user.json_data() self.assertEquals(json['net_id'], 'javerage') self.assertEquals(json['reg_id'], '9136CCB8F66711D5BE060004AC494FFE') self.assertEquals(json['queue_id'], None) self.assertEquals(json['provisioned_date'], None) self.assertEquals(json['priority'], 'high') User.objects.all().delete()
def get_photo_url(reg_id, image_size): """ Returns a url for the IDPhoto """ if PWS().valid_uwregid(reg_id): photo_key = ''.join( random.SystemRandom().choice(string.ascii_lowercase + string.digits) for _ in range(16)) data = {'reg_id': reg_id, 'image_size': image_size} expires = getattr(settings, 'IDCARD_TOKEN_EXPIRES', 60 * 60) cache.set(cache_key(photo_key), data, timeout=expires) return reverse('photo', kwargs={'photo_key': photo_key})
def test_user_email(self): user = PWS().get_person_by_netid('javerage') self.assertEquals(user_email(user), '*****@*****.**') # non-personal netid user = PWS().get_entity_by_netid('somalt') self.assertEquals(user_email(user), '*****@*****.**') user = get_person_by_gmail_id('*****@*****.**') self.assertEquals(user_email(user), '*****@*****.**') user = PWS().get_entity_by_netid('somalt') user.uwnetid = None self.assertRaises(UserPolicyException, user_email, user) user = InvalidPerson() self.assertRaises(UserPolicyException, user_email, user)
""" This module encapsulates the interactions with the uw_pws, """ import logging import traceback from uw_pws import PWS from sis_provisioner.dao import DataFailureException, InvalidNetID from sis_provisioner.util.log import log_exception, log_resp_time, Timer logger = logging.getLogger(__name__) pws = PWS() def get_person(uwnetid): """ Retrieve the Person object of the current uwnetid for the given netid Raise: DataFailureException if PWS returns a non-404 error """ try: return pws.get_person_by_netid(uwnetid) except InvalidNetID: logger.error("Invalid uwnetid: {0}".format(uwnetid)) except DataFailureException as ex: if ex.status == 404: logger.warning("{0} is not in PWS, skip!".format(uwnetid)) else: log_exception(logger, "get_person_by_netid({0}) ".format(uwnetid), traceback.format_exc(chain=False))
def valid_reg_id(regid): if not PWS().valid_uwregid(regid): raise InvalidLoginIdException('UWNetID not permitted')
def handle(self, *args, **options): subaccount_id = options.get('subaccount_id') sis_term_id = options.get('term_id') accounts = Accounts() reports = Reports() pws = PWS() outpath = "{}/{}-{}-{}.csv".format( dirname(__file__), "active-instructors", subaccount_id, sis_term_id) outfile = open(outpath, "w") csv.register_dialect('unix_newline', lineterminator='\n') writer = csv.writer(outfile, dialect='unix_newline') writer.writerow(['email', 'first_name', 'last_name']) account = accounts.get_account(subaccount_id) term = reports.get_term_by_sis_id(sis_term_id) enrollment_report = reports.create_enrollments_provisioning_report( account.account_id, term.term_id) enrollment_data = reports.get_report_data(enrollment_report) all_instructors = {} enrollment_csv_data = csv.reader(enrollment_data) header = next(enrollment_csv_data) course_id_idx = header.index("course_id") sis_user_id_idx = header.index("user_id") role_idx = header.index("role") status_idx = header.index("status") for row in enrollment_csv_data: if not len(row): continue course_id = row[course_id_idx] sis_user_id = row[sis_user_id_idx] role = row[role_idx] status = row[status_idx] if (sis_user_id != "" and role.lower() == "teacher" and status.lower() == "active"): if course_id not in all_instructors: all_instructors[course_id] = [] all_instructors[course_id].append(sis_user_id) course_report = reports.create_course_provisioning_report( account.account_id, term.term_id) course_data = reports.get_report_data(course_report) course_csv_data = csv.reader(course_data) header = next(course_csv_data) course_id_idx = header.index("course_id") sis_account_id_idx = header.index("account_id") status_idx = header.index("status") active_instructors = {} for row in course_csv_data: if not len(row): continue course_id = row[course_id_idx] sis_account_id = row[sis_account_id_idx] status = row[status_idx] if (sis_account_id != "" and status.lower() == "active" and course_id in all_instructors): for sis_user_id in all_instructors[course_id]: if sis_user_id in active_instructors: continue try: person = pws.get_person_by_regid(sis_user_id) active_instructors[sis_user_id] = [ person.uwnetid + "@uw.edu", person.preferred_first_name or person.first_name, person.preferred_surname or person.surname] except InvalidRegID: continue except DataFailureException as err: if err.status == 404: continue else: raise for csv_data in active_instructors.values(): writer.writerow(csv_data) outfile.close() reports.delete_report(enrollment_report) reports.delete_report(course_report) print(outpath)
def test_get_all_users_for_person(self, mock_method): person = PWS().get_person_by_netid('javerage') r = get_all_users_for_person(person) mock_method.assert_called_with('9136CCB8F66711D5BE060004AC494FF0', params={'include': 'last_login'})