def factory_document_model(version_id, doc_type, content): """Produce a Document model.""" document = DocumentsModel(version_id=version_id, type=doc_type, content=content) document.save() return document
def test_documents_with_insert_some_type(session): """Assert that a Documents can be stored in the service. Start with a blank document. """ html_content = '<HTML>' doc = Documents(version_id=2, type='sometype', content=html_content) session.add(doc) session.commit() doc_latest = Documents.fetch_latest_document_by_type('sometype') assert doc_latest.content == html_content
def test_documents_with_insert_some_type(session): """Assert that a Documents can be stored in the service. Start with a blank document. """ html_content = '<HTML>' # putting higher numbers so that version number doesnt collide with existing in db doc = Documents(version_id=20, type='sometype', content=html_content, content_type='text/html') session.add(doc) session.commit() doc_latest = Documents.fetch_latest_document_by_type('sometype') assert doc_latest.content == html_content
def test_documents_with_insert(session): """Assert that a Documents can be stored in the service. Start with a blank document. """ doc_latest = Documents.fetch_latest_document_by_type('termsofuse') assert doc_latest.version_id == '3'
def fetch_latest_document(cls, document_type): """Get a membership type by the given code.""" doc = DocumentsModel.fetch_latest_document_by_type( file_type=document_type) if doc: return Documents(doc) return None
def accept_invitation(invitation_id, user: UserService, origin, add_membership: bool = True, token_info: Dict = None): """Add user, role and org from the invitation to membership.""" current_app.logger.debug('>accept_invitation') invitation: InvitationModel = InvitationModel.find_invitation_by_id(invitation_id) if invitation is None: raise BusinessException(Error.DATA_NOT_FOUND, None) if invitation.invitation_status_code == 'ACCEPTED': raise BusinessException(Error.ACTIONED_INVITATION, None) if invitation.invitation_status_code == 'EXPIRED': raise BusinessException(Error.EXPIRED_INVITATION, None) if getattr(token_info, 'loginSource', None) is not None: # bcros comes with out token login_source = token_info.get('loginSource', None) if invitation.login_source != login_source: raise BusinessException(Error.INVALID_USER_CREDENTIALS, None) if add_membership: for membership in invitation.membership: membership_model = MembershipModel() membership_model.org_id = membership.org_id membership_model.user_id = user.identifier membership_model.membership_type = membership.membership_type # check to ensure an invitation for this user/org has not already been processed existing_membership = MembershipService \ .get_membership_for_org_and_user(org_id=membership_model.org_id, user_id=membership_model.user_id) if existing_membership: raise BusinessException(Error.DATA_ALREADY_EXISTS, None) org_model: OrgModel = OrgModel.find_by_org_id(membership.org_id) # GOVM users gets direct approval since they are IDIR users. membership_model.status = Invitation._get_status_based_on_org(org_model) membership_model.save() try: Invitation.notify_admin(user, invitation_id, membership_model.id, origin) except BusinessException as exception: current_app.logger.error('<send_notification_to_admin failed', exception.message) invitation.accepted_date = datetime.now() invitation.invitation_status = InvitationStatusModel.get_status_by_code('ACCEPTED') invitation.save() # Call keycloak to add the user to the group. if user: group_name: str = KeycloakService.join_users_group(token_info) KeycloakService.join_account_holders_group(user.keycloak_guid) if group_name == GROUP_GOV_ACCOUNT_USERS: # TODO Remove this if gov account users needs Terms of Use. tos_document = DocumentsModel.fetch_latest_document_by_type(DocumentType.TERMS_OF_USE.value) user.update_terms_of_use(token_info, True, tos_document.version_id) # Add contact to the user. user.add_contact(token_info, dict(email=token_info.get('email', None))) current_app.logger.debug('<accept_invitation') return Invitation(invitation)
def test_as_dict(session): # pylint: disable=unused-argument """Assert that a document is rendered correctly as a dictionary.""" _model = DocumentsModel.fetch_latest_document_by_type('termsofuse') termsofuse = DocumentService(_model) dictionary = termsofuse.as_dict() assert dictionary['type'] == 'termsofuse'
def find_latest_version_by_type(document_type): """Get the latest version for the given document type.""" return DocumentsModel.find_latest_version_by_type( file_type=document_type)