def test_get_instructor(instructor_api, expected_instructor): instructor_api.instructor_persistence.get_instructor = MagicMock( return_value=maybe(expected_instructor)) user_name = expected_instructor.user_name if expected_instructor else fake.pystr( ) assert instructor_api.get_instructor(user_name) == maybe( expected_instructor) instructor_api.instructor_persistence.get_instructor.called_once_with( user_name)
def authenticate_user(reqeust: HttpRequest) -> Option[User]: """ Authenticate a user for a given HTTP request Args: reqeust: The HTTP reqeust Returns: Some(User) if authentication was successful NONE otherwise """ auth_header = get_http_header(reqeust, 'AUTHORIZATION').unwrap_or('') if not auth_header: return NONE if not auth_header.startswith('Basic '): return NONE auth_string = auth_header[6:] try: email_pass = b64decode(auth_string, validate=True).decode() except (binascii.Error, UnicodeDecodeError): return NONE email, _, password = email_pass.partition(':') user = authenticate(username=email, password=password) return maybe(user)
def get_section(self, section_identity: SectionIdentity) -> Option[Section]: c = self.connection.cursor() term = ( section_identity.course.course_code, section_identity.year, str(section_identity.semester.value), section_identity.section_code, ) c.execute( "SELECT * FROM sections WHERE course=%s AND year=%s AND semester=%s AND " "section_code=%s", term, ) section = None res = c.fetchone() if res: def get_inst(user_name): c.execute("SELECT * FROM instructors WHERE user_name=%s", (str(user_name), )) res = c.fetchone() if res: return Instructor(res[0], res[1], res[3]) return None section = Section( Course(res[0]), res[1], Semester(int(res[2])), res[3], get_inst(res[4]), res[5], ) return maybe(section)
def get_note(self, note_id: uuid.UUID) -> Option[Note]: c = self.connection.cursor() c.execute("SELECT * FROM notes WHERE note_id=%s", (str(note_id),)) note = None res = c.fetchone() if res: note = Note(note_id, uuid.UUID(res[1]), res[2], res[3]) return maybe(note)
def get_meeting(self, meeting_id: uuid.UUID) -> Option[Meeting]: c = self.connection.cursor() c.execute("SELECT * FROM meetings WHERE meeting_id=%s", (str(meeting_id),)) meeting = None res = c.fetchone() if res: meeting = self._res_to_meeting(res) return maybe(meeting)
def get_comment(self, comment_id: uuid.UUID) -> Option[Comment]: c = self.connection.cursor() c.execute("SELECT * FROM comments WHERE comment_id=%s", (str(comment_id),)) comment = None res = c.fetchone() if res: comment = self._res_to_comment(res) return maybe(comment)
def get_course(self, course_code: str) -> Option[Course]: c = self.connection.cursor() term = (course_code,) c.execute("SELECT * FROM courses WHERE course_code=%s", term) course = None res = c.fetchone() if res: course = Course(res[0]) return maybe(course)
def get_instructor(self, user_identity: str) -> Option[Instructor]: c = self.connection.cursor() term = (user_identity, ) c.execute("SELECT * FROM instructors WHERE user_name=%s", term) instructor = None res = c.fetchone() if res: instructor = Instructor(res[0], res[1], res[3]) return maybe(instructor)
def get_student(self, user_identity: str) -> Option[Student]: c = self.connection.cursor() term = (user_identity, ) c.execute("SELECT * FROM students WHERE student_number=%s", term) student = None res = c.fetchone() if res: student = Student(res[0], res[1], res[3]) return maybe(student)
def get_officehour(self, office_hour_id: UUID, mp: MeetingPersistence) -> Option[OfficeHour]: c = self.connection.cursor() term = (office_hour_id, ) c.execute("SELECT * FROM officehours WHERE office_hour_id=%s", term) officehour = None res = c.fetchone() if res: officehour = self._res_to_officehour(res, mp) return maybe(officehour)
def get_password_hash(self, user_identity: str) -> Result[str, str]: c = self.connection.cursor() term = (user_identity, ) c.execute("SELECT * FROM instructors WHERE user_name=%s", term) pass_hash = None res = c.fetchone() if res: pass_hash = res[2] else: return Err(f"Instructor {user_identity} does not exist") return maybe(pass_hash)
def test_get_officehour(schema, expected): context = MagicMock() context.api.course_api.get_officehour.return_value = maybe(expected) office_hour_id = expected.office_hour_id if expected else uuid4() query = """ query getOfficeHour($officeHourId: UUID!) { officehour(officeHourId: $officeHourId) { officeHourId } } """ result = schema.execute( query, context=context, variables={"officeHourId": str(office_hour_id)} ) assert not result.errors if expected: assert result.data["officehour"] == {"officeHourId": str(office_hour_id)} else: assert result.data["officehour"] is None
assert obj.is_some is True assert obj.is_none is False assert bool(obj) assert obj._val == 1 @parametrize('obj', [Option.NONE(), NONE]) def test_none(obj): assert obj.is_some is False assert obj.is_none is True assert bool(obj) is False assert obj._val is None @parametrize('obj,is_some', [ (maybe(0), True), (maybe(0.0), True), (maybe(''), True), (maybe([]), True), (maybe({}), True), (maybe(()), True), (maybe(set()), True), (maybe(None), False), (maybe(1), True), (maybe(True), True), (maybe(False), True) ]) def test_maybe(obj, is_some): if is_some: assert obj.is_some else: