def test_update_note_template_attachments(self, app, client, fake_auth, mock_note_template): """Update note attachments.""" user = AuthorizedUser.find_by_id(mock_note_template.creator_id) fake_auth.login(user.uid) base_dir = app.config['BASE_DIR'] attachment_id = mock_note_template.attachments[0].id filename = 'mock_advising_note_attachment_2.txt' path_to_new_attachment = f'{base_dir}/fixtures/{filename}' updated_note = self._api_note_template_update( app, client, note_template_id=mock_note_template.id, title=mock_note_template.title, subject=mock_note_template.subject, body=mock_note_template.body, attachments=[path_to_new_attachment], delete_attachment_ids=[attachment_id], ) assert mock_note_template.id == updated_note['attachments'][0][ 'noteTemplateId'] assert len(updated_note['attachments']) == 1 assert filename == updated_note['attachments'][0]['displayName'] assert filename == updated_note['attachments'][0]['filename'] assert updated_note['attachments'][0]['id'] != attachment_id # Verify db attachments = NoteTemplateAttachment.query.filter( and_( NoteTemplateAttachment.note_template_id == mock_note_template.id, NoteTemplateAttachment.deleted_at == None, # noqa: E711 ), ).all() assert len(attachments) == 1 assert filename in attachments[0].path_to_attachment assert not NoteTemplateAttachment.find_by_id(attachment_id)
def create(cls, creator_id, subject, title, attachments=(), body='', is_private=False, topics=()): creator = AuthorizedUser.find_by_id(creator_id) if creator: note_template = cls(body=body, creator_id=creator_id, is_private=is_private, subject=subject, title=title) for topic in topics: note_template.topics.append( NoteTemplateTopic.create( note_template.id, titleize(vacuum_whitespace(topic))), ) for byte_stream_bundle in attachments: note_template.attachments.append( NoteTemplateAttachment.create( note_template_id=note_template.id, name=byte_stream_bundle['name'], byte_stream=byte_stream_bundle['byte_stream'], uploaded_by=creator.uid, ), ) db.session.add(note_template) std_commit() return note_template
def create_or_update_user_profile(): params = request.get_json() profile = params.get('profile', None) memberships = params.get('memberships', None) delete_action = to_bool_or_none(util.get(params, 'deleteAction')) if not profile or not profile.get('uid') or memberships is None: raise errors.BadRequestError('Required parameters are missing') authorized_user = _update_or_create_authorized_user(memberships, profile, include_deleted=True) _delete_existing_memberships(authorized_user) _create_department_memberships(authorized_user, memberships) if delete_action is True and not authorized_user.deleted_at: AuthorizedUser.delete(authorized_user.uid) elif delete_action is False and authorized_user.deleted_at: AuthorizedUser.un_delete(authorized_user.uid) user_id = authorized_user.id UserSession.flush_cache_for_id(user_id) updated_user = AuthorizedUser.find_by_id(user_id, include_deleted=True) users_json = authorized_users_api_feed([updated_user]) return tolerant_jsonify(users_json and users_json[0])
def update( cls, body, note_template_id, subject, attachments=(), delete_attachment_ids=(), is_private=False, topics=(), ): note_template = cls.find_by_id(note_template_id) if note_template: creator = AuthorizedUser.find_by_id(note_template.creator_id) note_template.body = body note_template.is_private = is_private note_template.subject = subject cls._update_note_template_topics(note_template, topics) if delete_attachment_ids: cls._delete_attachments(note_template, delete_attachment_ids) for byte_stream_bundle in attachments: cls._add_attachment(note_template, byte_stream_bundle, creator.uid) std_commit() db.session.refresh(note_template) return note_template else: return None
def test_feature_flag(self, client, fake_auth, app): """Appointments feature is false.""" dept_code = 'QCADV' advisor = DropInAdvisor.advisors_for_dept_code(dept_code)[0] fake_auth.login( AuthorizedUser.find_by_id(advisor.authorized_user_id).uid) appointment = AppointmentTestUtil.create_appointment(client, dept_code) with override_config(app, 'FEATURE_FLAG_ADVISOR_APPOINTMENTS', False): self._check_in(client, appointment['id'], expected_status_code=401)
def test_unreserve_appointment_reserved_by_other(self, app, client, fake_auth): """Returns 401 if user un-reserves an appointment which is reserved by another.""" waiting = Appointment.query.filter( and_(Appointment.status == 'waiting', Appointment.deleted_at == None), ).first() # noqa: E711 advisor = AuthorizedUser.find_by_id(waiting.created_by) fake_auth.login(advisor.uid) self._reserve_appointment(client, waiting.id) fake_auth.login(l_s_college_advisor_uid) self._unreserve_appointment(client, 1, expected_status_code=401)
def set_demo_mode(): if app.config['DEMO_MODE_AVAILABLE']: in_demo_mode = request.get_json().get('demoMode', None) if in_demo_mode is None: raise errors.BadRequestError('Parameter \'demoMode\' not found') user = AuthorizedUser.find_by_id(current_user.get_id()) user.in_demo_mode = bool(in_demo_mode) current_user.flush_cached() app.login_manager.reload_user() return tolerant_jsonify(current_user.to_api_json()) else: raise errors.ResourceNotFoundError('Unknown path')
def test_reserve_appointment(self, app, client, fake_auth): """Drop-in advisor can reserve an appointment.""" dept_code = 'QCADV' advisor = DropInAdvisor.advisors_for_dept_code(dept_code)[0] user = AuthorizedUser.find_by_id(advisor.authorized_user_id) fake_auth.login(user.uid) waiting = AppointmentTestUtil.create_appointment(client, dept_code) appointment = self._reserve_appointment(client, waiting['id']) assert appointment['status'] == 'reserved' assert appointment['statusDate'] is not None assert appointment['statusBy']['id'] == user.id Appointment.delete(appointment['id'])
def test_update_note_template_topics(self, app, client, fake_auth, mock_note_template): """Update note template title.""" user = AuthorizedUser.find_by_id(mock_note_template.creator_id) fake_auth.login(user.uid) expected_title = 'As cool as Kim Deal' api_json = self._api_note_template_rename( client, note_template_id=mock_note_template.id, title=expected_title, ) assert api_json['title'] == expected_title assert NoteTemplate.find_by_id( mock_note_template.id).title == expected_title
def test_steal_appointment_reservation(self, app, client, fake_auth): """Reserve an appointment that another advisor has reserved.""" dept_code = 'COENG' advisor_1 = DropInAdvisor.advisors_for_dept_code(dept_code)[0] user_1 = AuthorizedUser.find_by_id(advisor_1.authorized_user_id) fake_auth.login(user_1.uid) waiting = AppointmentTestUtil.create_appointment(client, dept_code) appointment = self._reserve_appointment(client, waiting['id']) assert appointment['status'] == 'reserved' assert appointment['statusDate'] is not None assert appointment['statusBy']['id'] == user_1.id client.get('/api/auth/logout') # Another advisor comes along... advisor_2 = DropInAdvisor.advisors_for_dept_code(dept_code)[1] user_2 = AuthorizedUser.find_by_id(advisor_2.authorized_user_id) fake_auth.login(user_2.uid) appointment = self._reserve_appointment(client, waiting['id']) assert appointment['status'] == 'reserved' assert appointment['statusDate'] is not None assert appointment['statusBy']['id'] == user_2.id # Clean up Appointment.delete(appointment['id'])
def _can_current_user_view_curated_group(curated_group): if current_user.is_admin: return True owner = AuthorizedUser.find_by_id(curated_group.owner_id) if not owner: return False curated_group_dept_codes = [ m.university_dept.dept_code for m in owner.department_memberships ] if len(curated_group_dept_codes): user_dept_codes = dept_codes_where_advising(current_user) return len( [c for c in user_dept_codes if c in curated_group_dept_codes]) else: return False
def test_remove_note_template_topics(self, app, client, fake_auth, mock_note_template): """Delete note template topics.""" user = AuthorizedUser.find_by_id(mock_note_template.creator_id) fake_auth.login(user.uid) api_json = self._api_note_template_update( app, client, note_template_id=mock_note_template.id, title=mock_note_template.title, subject=mock_note_template.subject, body=mock_note_template.body, topics=(), ) assert not api_json['topics']
def test_update_note_template_topics(self, app, client, fake_auth, mock_note_template): """Update note template topics.""" user = AuthorizedUser.find_by_id(mock_note_template.creator_id) fake_auth.login(user.uid) expected_topics = ['this', 'that'] api_json = self._api_note_template_update( app, client, note_template_id=mock_note_template.id, title=mock_note_template.title, subject=mock_note_template.subject, topics=expected_topics, ) assert len(api_json['topics']) == 2 assert sorted(api_json['topics']) == ['That', 'This']
def set_drop_in_advising_status(dept_code): user = AuthorizedUser.find_by_id(current_user.get_id()) drop_in_membership = next((d for d in user.drop_in_departments if d.dept_code == dept_code.upper()), None) if not drop_in_membership: raise errors.ResourceNotFoundError( f'No drop-in advisor membership found: (uid={current_user.get_uid()}, dept_code={dept_code})' ) params = request.get_json() if 'status' not in params: raise errors.BadRequestError('Missing status') if params['status'] and len(params['status']) > 255: raise errors.BadRequestError('Invalid status') drop_in_membership.update_status(params['status']) UserSession.flush_cache_for_id(user.id) return tolerant_jsonify(drop_in_membership.to_api_json())
def test_appointment_cancel(self, app, client, fake_auth): """Drop-in advisor can cancel appointment.""" dept_code = 'QCADV' advisor = DropInAdvisor.advisors_for_dept_code(dept_code)[0] user = AuthorizedUser.find_by_id(advisor.authorized_user_id) fake_auth.login(user.uid) waiting = AppointmentTestUtil.create_appointment(client, dept_code) appointment = self._cancel_appointment(client, waiting['id'], 'Canceled by wolves') appointment_id = appointment['id'] assert appointment_id == waiting['id'] assert appointment['status'] == 'canceled' assert appointment['statusBy']['id'] == user.id assert appointment['statusBy']['uid'] == user.uid assert appointment['statusDate'] is not None Appointment.delete(appointment_id)
def load_user(cls, user_id): return cls._get_api_json(user=AuthorizedUser.find_by_id(user_id))
def disable_same_day_advising(dept_code): user = AuthorizedUser.find_by_id(current_user.get_id()) SameDayAdvisor.delete(authorized_user_id=user.id, dept_code=dept_code) UserSession.flush_cache_for_id(user.id) return tolerant_jsonify( {'message': 'Same-day advisor status has been disabled'}, status=200)
def disable_drop_in_advising(dept_code): user = AuthorizedUser.find_by_id(current_user.get_id()) _delete_drop_in_advisor_status(user, dept_code) UserSession.flush_cache_for_id(user.id) return tolerant_jsonify( {'message': 'Drop-in advisor status has been disabled'}, status=200)
def calnet_profile_by_user_id(user_id): user = AuthorizedUser.find_by_id(user_id) if user: return tolerant_jsonify(calnet.get_calnet_user_for_uid(app, user.uid)) else: raise errors.ResourceNotFoundError('User not found')