def main(argv=sys.argv): if len(argv) < 2: usage(argv) config_uri = argv[1] options = parse_vars(argv[2:]) setup_logging(config_uri) settings = get_appsettings(config_uri, name='testscaffold', options=options) engine = get_engine(settings) session_factory = get_session_factory(engine) dbsession = get_tm_session(session_factory, transaction.manager) with transaction.manager: user = User(user_name="admin", email='foo@localhost') user.set_password('admin') admin_object = Group(group_name='Administrators') group_permission = GroupPermission(perm_name='root_administration') dbsession.add(admin_object) admin_object.permissions.append(group_permission) admin_object.users.append(user) test_group = Group(group_name='Other group') dbsession.add(test_group) for x in range(1, 25): user = User(user_name="test{}".format(x), email='foo{}@localhost'.format(x)) user.set_password('test') test_group.users.append(user)
def post(self): schema = UserCreateSchema(context={'request': self.request}) data = schema.load(self.request.unsafe_json_body).data user = User() self.shared.populate_instance(user, data) user.persist(flush=True, db_session=self.request.dbsession) return schema.dump(user).data
def test_get_found(self, sqla_session): from testscaffold.views.api.users import UserAPIView from testscaffold.models.user import User with tmp_session_context(sqla_session) as session: request = dummy_request(session) user = User(id=5, email='foo', user_name='bar') user.persist(flush=True, db_session=request.dbsession) request.matchdict['object_id'] = 5 result = UserAPIView(request).get() assert result['id'] == 5
def test_patch_found_valid(self, sqla_session): from testscaffold.views.api.users import UserAPIView from testscaffold.models.user import User with tmp_session_context(sqla_session) as session: request = dummy_request(session) request.json_body = { 'user_name': 'changed', 'email': '*****@*****.**', } user = User(id=1, email='foo', user_name='bar') user.persist(flush=True, db_session=request.dbsession) request.matchdict['object_id'] = 1 result = UserAPIView(request).patch() assert result['user_name'] == 'changed' assert result['email'] == '*****@*****.**'
def post(self): request = self.request user_form = UserAdminCreateForm(request.POST, context={'request': request}) if request.method == "POST" and user_form.validate(): user = User() self.shared.populate_instance(user, user_form.data) user.persist(flush=True, db_session=request.dbsession) log.info('users_post', extra={ 'user_id': user.id, 'user_name': user.user_name }) request.session.flash({ 'msg': self.translate(_('User created.')), 'level': 'success' }) location = request.route_url('admin_objects', object='users', verb='GET') return pyramid.httpexceptions.HTTPFound(location=location) return {"user_form": user_form}
def test_cleanup(self, sqla_session): from testscaffold.models.user import User with session_context(sqla_session) as session: user = User(id=1, email='foasfsfao', user_name='barafsf') user.persist(flush=True, db_session=session)
def register(self): """ Render register page with form Also handles oAuth flow for registration """ request = self.request login_url = request.route_url('ziggurat.routes.sign_in') login_form = UserLoginForm(request.POST, context={'request': request}) # some logic to handle came_from variable that we can use # to redirect user that tried to access forbidden resource if request.query_string: query_string = '?%s' % request.query_string else: query_string = '' referrer = '%s%s' % (request.path, query_string) for url in [login_url, '/register']: if url in referrer: # never use the login form itself as came_from # or we may end up with a redirect loop referrer = '/' break registration_form = UserCreateForm(request.POST, context={'request': request}) # populate form from oAuth session data returned by velruse social_data = request.session.get('zigg.social_auth') if request.method != 'POST' and social_data: log.info('social_auth', extra={'social_data': social_data}) form_data = {'email': social_data['user'].get('email')} form_data['user_password'] = str(uuid.uuid4()) # repopulate form this time from oauth data registration_form = UserCreateForm(context={'request': request}, **form_data) if request.method == "POST" and registration_form.validate(): # insert new user here new_user = User() registration_form.populate_obj(new_user) new_user.persist(flush=True, db_session=request.dbsession) new_user.regenerate_security_code() new_user.status = 1 new_user.set_password(new_user.password) new_user.registration_ip = request.environ.get('REMOTE_ADDR') log.info('register', extra={'new_user': new_user.user_name}) # bind 3rd party identity if social_data: request.registry.notify( SocialAuthEvent(request, new_user, social_data)) title = _("${project} :: Start information", mapping={'project': 'testscaffold'}) email_vars = { 'user': new_user, 'email_title': self.translate(title) } ev = EmailEvent(request, recipients=[new_user.email], tmpl_vars=email_vars, tmpl_loc='testscaffold:' 'templates/emails/registered.jinja2') request.registry.notify(ev) request.session.flash({ 'msg': self.translate(_('You have successfully registered.')), 'level': 'success' }) headers = remember(request, new_user.id) return HTTPFound(location=request.route_url('/'), headers=headers) return { "registration_form": registration_form, 'login_form': login_form }