def users(app, request, agencies): params = request.param if hasattr(request, 'param') else {} user_role = params['user_role'] if 'user_role' in params else 'buyer' email_domain = params[ 'email_domain'] if 'email_domain' in params else 'digital.gov.au' framework_slug = params[ 'framework_slug'] if 'framework_slug' in params else 'digital-marketplace' with app.app_context(): for i in range(1, 6): new_user = User(id=i, email_address='{}{}@{}'.format( fake.first_name(), i, email_domain).lower(), name=fake.name(), password=fake.password(), active=True, role=user_role, password_changed_at=utcnow()) if user_role == 'supplier': new_user.supplier_code = i db.session.add(new_user) db.session.flush() framework = Framework.query.filter( Framework.slug == framework_slug).first() db.session.add(UserFramework(user_id=i, framework_id=framework.id)) if user_role == 'buyer': db.session.add( User(id=7, email_address='*****@*****.**', name=fake.name(), password=encryption.hashpw('testpassword'), active=True, role='buyer', password_changed_at=utcnow(), agency_id=1)) db.session.flush() db.session.add(UserFramework(user_id=7, framework_id=framework.id)) db.session.commit() yield User.query.filter(User.role == user_role).all()
def buyer_dashboard_users(app, request): with app.app_context(): db.session.add(User( id=1, email_address='*****@*****.**', name=fake.name(), password=encryption.hashpw('testpassword'), active=True, role='buyer', password_changed_at=utcnow() )) db.session.add(User( id=2, email_address='*****@*****.**', name=fake.name(), password=encryption.hashpw('testpassword'), active=True, role='buyer', password_changed_at=utcnow() )) db.session.add(User( id=3, email_address='*****@*****.**', name=fake.name(), password=encryption.hashpw('testpassword'), active=True, role='buyer', password_changed_at=utcnow() )) db.session.flush() framework = Framework.query.filter(Framework.slug == "digital-marketplace").first() db.session.add(UserFramework(user_id=1, framework_id=framework.id)) db.session.add(UserFramework(user_id=2, framework_id=framework.id)) db.session.commit() yield User.query.all()
def supplier_user(app, request, suppliers): with app.app_context(): db.session.add( User(id=100, email_address='*****@*****.**', name=fake.name(), password=encryption.hashpw('testpassword'), active=True, role='supplier', supplier_code=suppliers[0].code, password_changed_at=utcnow())) db.session.commit() db.session.flush() framework = Framework.query.filter( Framework.slug == "digital-outcomes-and-specialists").first() db.session.add(UserFramework(user_id=100, framework_id=framework.id)) db.session.commit() yield User.query.first()
def supplier_user(app, request, suppliers): with app.app_context(): user = User.query.order_by(User.id.desc()).first() id = user.id + 1 if user else 1 db.session.add( User(id=id, email_address='*****@*****.**', name=fake.name(), password=encryption.hashpw('testpassword'), active=True, role='supplier', supplier_code=suppliers[0].code, password_changed_at=utcnow())) db.session.commit() framework = Framework.query.filter(Framework.slug == "orams").first() db.session.add(UserFramework(user_id=id, framework_id=framework.id)) db.session.commit() yield User.query.get(id)
def add_user(data): if data is None: raise DataError('create_user requires a data arg') name = data.get('name') password = data.get('password') role = data.get('user_type') email_address = data.get('email_address', None) framework_slug = data.get('framework', 'digital-marketplace') if email_address is None: email_address = data.get('emailAddress', None) if 'hashpw' in data and not data['hashpw']: password = password else: password = encryption.hashpw(password) if role == 'seller': role = 'applicant' now = datetime.utcnow() user = User(email_address=email_address.lower(), phone_number=data.get('phoneNumber', None), name=name, role=role, password=password, active=True, created_at=now, updated_at=now, password_changed_at=now) audit_data = {} if "supplier_code" in data: user.supplier_code = data['supplier_code'] audit_data['supplier_code'] = user.supplier_code if user.role == 'supplier' and user.supplier_code is None: raise ValueError( "'supplier_code' is required for users with 'supplier' role") if user.role != 'supplier' and user.supplier_code is not None: raise ValueError( "'supplier_code' is only valid for users with 'supplier' role, not '{}'" .format(user.role)) if "application_id" in data: user.application_id = data['application_id'] elif user.supplier_code is not None: appl = Application.query.filter_by( supplier_code=user.supplier_code).first() user.application_id = appl and appl.id or None if user.role == 'applicant' and user.application_id is None: raise ValueError( "'application id' is required for users with 'applicant' role") elif user.role != 'applicant' and user.role != 'supplier' and user.application_id is not None: raise ValueError( "'application_id' is only valid for users with applicant' or 'supplier' role, not '{}'" .format(user.role)) db.session.add(user) db.session.flush() framework = Framework.query.filter( Framework.slug == framework_slug).first() db.session.add(UserFramework(user_id=user.id, framework_id=framework.id)) audit = AuditEvent(audit_type=AuditTypes.create_user, user=email_address.lower(), data=audit_data, db_object=user) db.session.add(audit) db.session.commit() return user