def _save_new_user(request, form): """ form - must be a valid form We persist account to LDAP. If all goes well, we log the user in and persist their password to the session. """ # Email in the form is the "username" we'll use. username = form.cleaned_data['email'] password = form.cleaned_data['password'] registrar = RegistrarSession.connect(request) d = form.cleaned_data uniq_id = registrar.create_person(d) voucher = None if d['code']: try: invite = get_invite(d['code']) voucher = invite.inviter except Invite.DoesNotExist: pass if voucher: registrar.record_vouch(voucher=voucher, vouchee=uniq_id) invite.redeemed = datetime.datetime.now() invite.redeemer = uniq_id invite.save() user = auth.authenticate(username=username, password=password) auth.login(request, user) return uniq_id
def _with_temp_user(fn): """ Runs a function in the context of a newly created user. It will cleanup the user after fn is run, unless it has already been deleted. fn - A callable function. First parameter should be a unique_id string. """ regi = RegistrarSession.connect( _mock_request('/en-US/search?q=David')) username = '******' % str(uuid4())[0:8] data = dict(first_name='Jane', last_name='Doe', email=username, password='******', biography='Keeping it real.', irc_nickname='', irc_nickname_unique_id='') new_unique_id = regi.create_person(data) fn(new_unique_id) directory = UserSession.connect(_mock_request('/en-US/search?q=David')) try: directory.get_by_unique_id(new_unique_id) admin = AdminSession.connect(_mock_request('/en-US/search?q=David')) admin.delete_person(new_unique_id) except: pass finally: pass
def _with_temp_user(fn): """ Runs a function in the context of a newly created user. It will cleanup the user after fn is run, unless it has already been deleted. fn - A callable function. First parameter should be a unique_id string. """ regi = RegistrarSession.connect(_mock_request('/en-US/search?q=David')) username = '******' % str(uuid4())[0:8] data = dict(first_name='Jane', last_name='Doe', email=username, password='******', biography='Keeping it real.', irc_nickname='', irc_nickname_unique_id='') new_unique_id = regi.create_person(data) fn(new_unique_id) directory = UserSession.connect(_mock_request('/en-US/search?q=David')) try: directory.get_by_unique_id(new_unique_id) admin = AdminSession.connect(_mock_request('/en-US/search?q=David')) admin.delete_person(new_unique_id) except: pass finally: pass
def test_connection_pooling(self): # Don't use _mock_request, nor TestLarper since we want full control # over the request to find pooling bugs rf = RequestFactory() request = rf.get('/en-US') request.session = {} request.user = MockUser('*****@*****.**', '7f3a67u000001') larper.store_password(request, 'secret') R = larper.READ W = larper.WRITE directory = UserSession.connect(request) self.assertFalse(hasattr(request, 'larper_conns')) regi = RegistrarSession.connect(request) self.assertFalse(hasattr(request, 'larper_conns')) regi_W_conn = regi._ensure_conn(W) regi_W_conn2 = regi._ensure_conn(W) self.assertIs(regi_W_conn, regi_W_conn2) self.assertTrue(hasattr(request, 'larper_conns')) self.assertEqual(len(request.larper_conns[R].keys()), 0) self.assertEqual(len(request.larper_conns[W].keys()), 1) dir_W_conn = directory._ensure_conn(W) dir_W_conn2 = directory._ensure_conn(W) self.assertIs(dir_W_conn, dir_W_conn2) self.assertEqual(len(request.larper_conns[R].keys()), 0) self.assertEqual(len(request.larper_conns[W].keys()), 2) dir_R_conn = directory._ensure_conn(R) admin = AdminSession.connect(request) admin_R_conn = admin._ensure_conn(R) admin_R_conn2 = admin._ensure_conn(R) admin_W_conn = admin._ensure_conn(W) self.assertIs(admin_R_conn, admin_R_conn2) self.assertIsNot(admin_R_conn, admin_W_conn) for conn in (regi_W_conn, dir_R_conn, admin_R_conn, admin_W_conn): # nor is it dir_R_conn2 or admin_R_conn2 self.assertIsNot(dir_W_conn, conn) self.assertEqual(len(request.larper_conns[R].keys()), 2) self.assertEqual(len(request.larper_conns[W].keys()), 3) directory.disconnect(request) self.assertEqual(len(request.larper_conns[R].keys()), 0) self.assertEqual(len(request.larper_conns[W].keys()), 0)
def _save_new_user(request, form): """ form - must be a valid form We persist account to LDAP. If all goes well, we log the user in and persist their BID assertion to the session. """ # Email in the form is the "username" we'll use. email = request.session['verified_email'] username = email registrar = RegistrarSession.connect(request) code = request.session.get('invite-code') d = form.cleaned_data d['email'] = email uniq_id = registrar.create_person(d) voucher = None if code: try: invite = get_invite(code) voucher = invite.inviter except Invite.DoesNotExist: msg = 'Bad code in form [%s], skipping pre-vouch' % d['code'] log.warning(msg) # we need to authenticate them... with their assertion assertion_hash, assertion = get_assertion(request) for i in range(1, 10): try: user = auth.authenticate(request=request, assertion=assertion) # Should never happen if not user or not user.is_authenticated(): msg = 'Authentication for new user (%s) failed' % username # TODO: make this a unique exception. raise Exception(msg) statsd.incr('user.successful_registration') statsd.incr('user.successful_registration_attempt_%s' % i) break except Exception, e: statsd.incr('user.errors.registration_failed') statsd.incr('user.errors.registration_failed_attempt_%s' % i) log.warning(e) # All hope is lost. if i == 10: statsd.incr('user.errors.user_record_never_created') raise Exception(e)
def _save_new_user(request, form): """ form - must be a valid form We persist account to LDAP. If all goes well, we log the user in and persist their password to the session. """ # Email in the form is the "username" we'll use. username = form.cleaned_data['email'] password = form.cleaned_data['password'] registrar = RegistrarSession.connect(request) d = form.cleaned_data uniq_id = registrar.create_person(d) voucher = None if d['code']: try: invite = get_invite(d['code']) voucher = invite.inviter except Invite.DoesNotExist: msg = 'Bad code in form [%s], skipping pre-vouch' % d['code'] log.warning(msg) if voucher: registrar.record_vouch(voucher=voucher, vouchee=uniq_id) invite.redeemed = datetime.datetime.now() invite.redeemer = uniq_id invite.save() # auto vouch moz.com: elif any(username.endswith('@' + x) for x in settings.AUTO_VOUCH_DOMAINS): registrar.record_vouch(voucher='ZUUL', vouchee=uniq_id) user = auth.authenticate(username=username, password=password) # Should never happen if not user or not user.is_authenticated(): msg = 'Authentication for new user (%s) failed' % username # TODO: make this a unique exception. raise Exception(msg) auth.login(request, user) return uniq_id
def test_create_then_delete_person(self): regi = self.d = RegistrarSession.connect( _mock_request('/en-US/search?q=David')) username = '******' % str(uuid4())[0:8] data = dict(first_name='Jane', last_name='Doe', email=username, password='******', biography='Keeping it real.', irc_nickname='hobart', irc_nickname_unique_id='') new_unique_id = regi.create_person(data) directory = UserSession.connect( _mock_request('/en-US/search?q=David')) newbie = directory.get_by_unique_id(new_unique_id) eq_(username, newbie.username) admin = AdminSession.connect( _mock_request('/en-US/search?q=David')) admin.delete_person(new_unique_id) self.assertRaises(larper.NO_SUCH_PERSON, lambda:\ directory.get_by_unique_id(new_unique_id))
def test_create_then_delete_person(self): regi = self.d = RegistrarSession.connect( _mock_request('/en-US/search?q=David')) username = '******' % str(uuid4())[0:8] data = dict(first_name='Jane', last_name='Doe', email=username, password='******', biography='Keeping it real.', irc_nickname='hobart', irc_nickname_unique_id='') new_unique_id = regi.create_person(data) directory = UserSession.connect(_mock_request('/en-US/search?q=David')) newbie = directory.get_by_unique_id(new_unique_id) eq_(username, newbie.username) admin = AdminSession.connect(_mock_request('/en-US/search?q=David')) admin.delete_person(new_unique_id) self.assertRaises(larper.NO_SUCH_PERSON, lambda:\ directory.get_by_unique_id(new_unique_id))