def create_user(role=None, id=1): u =User() u._key = ndb.Key('User', id) if role is not None: u.roles = [role] u.put() return u
def test_handle_request(self): # No User or Profile p_count0 = UserProfile.query().count() u_count0 = User.query().count() self.assertEqual(p_count0, 0) self.assertEqual(u_count0, 0) # Create New User provider = 'gmail.com' req = Request.blank('/auth/appengine_openid?provider=' + provider) resp = req.get_response(application) self.assertEqual(resp.location, 'https://www.google.com/accounts/' 'Login?continue=http%3A//localhost/' 'auth/appengine_openid/callback')
def post(self): email = self.request.POST.get('email') if not email or "@" not in email or "." not in email: self.request.add_message( 'Please enter a valid email address', 'error') return self.get() existing_account = UserProfile.get_key('password', email).get() if not existing_account: self.request.add_message( 'The email address you provided was not found. ' 'Please try again.', 'error') return self.get() user = User.get_by_auth_id(existing_account.key.id()) taskqueue.add(url='/account/tasks/password-recovery-email', params={ 'recipient_id': user.key.id(), }) return self.sent(email)
def post(self, token): if self.form.validate(): token = UserToken.get_by_id(token) # test current password user = User.get_by_id(token.user_id) if token is not None and user is not None: # updated the Users password UserProfile with the new password p = user.get_auth_id("password").get() p.set_password(self.form.password.data) p.put() # Delete token token.key.delete() # Login User self.add_message('Password updated successfully. ' 'Login with your new password', 'success') return self.redirect_to('auth-login') self.add_message('Please correct the form errors.', 'error') return self.get(token)
def password_recovery(self): recipient_id = self.request.POST.get('recipient_id') if recipient_id is None: return recipient = User.get_by_id(int(recipient_id)) token = UserToken.create(recipient.key.id(), 'password_reset').key.id() # uses the application_title set in the config subject = "{}: Password Assistance".format( Config.get('application').title) template = '/account/emails/password_reset.html' reset_url = self.uri_for( 'account-recovery-verify', token=token, _full=True) # Create the email email = mail.EmailMessage() email.sender = Config.get('application').default_from_email email.subject = subject email.to = '{} <{}>'.format(recipient.name, recipient.email) email.body = self.render_template(template, { 'recipient': recipient, 'reset_url': reset_url, }) email.send()
def test_post(self): email = '*****@*****.**' password = '******' # No User u_count0 = User.query().count() self.assertEqual(u_count0, 0) # Create New User req = Request.blank('/auth/password', POST={ 'password': password, 'email': email, 'name': 'Kyle Finley', 'givenName': 'Kyle', 'additionalName': '"Danger"', 'familyName': 'Finley', 'gender': 'male', 'affiliations-1.name': 'Aff 1 name', 'affiliations-1.department': 'Aff 1 department', 'affiliations-1.description': 'Aff 1 description', 'affiliations-1.location': 'Aff 1 location', 'affiliations-2.name': 'Aff 2 name', 'affiliations-2.department': 'Aff 2 department', 'affiliations-2.description': 'Aff 2 description', 'affiliations-2.location': 'Aff 2 location', }) req._load_session() resp = req.get_response(application) # Retrieve user from datastore user = User.query().get() p = user.data self.assertEqual(p.name, 'Kyle Finley') self.assertEqual(p.givenName, 'Kyle') self.assertEqual(p.additionalName, '"Danger"') self.assertEqual(p.familyName, 'Finley') self.assertEqual(p.gender, 'male') self.assertEqual(p.affiliations[0].name, 'Aff 1 name') self.assertEqual(p.affiliations[0].department, 'Aff 1 department') self.assertEqual(p.affiliations[0].description, 'Aff 1 description') self.assertEqual(p.affiliations[0].location.name, 'Aff 1 location') self.assertEqual(p.affiliations[1].name, 'Aff 2 name') self.assertEqual(p.affiliations[1].department, 'Aff 2 department') self.assertEqual(p.affiliations[1].description, 'Aff 2 description') self.assertEqual(p.affiliations[1].location.name, 'Aff 2 location') u_count1 = User.query().count() self.assertEqual(p_count1, 1) self.assertEqual(u_count1, 1) # Login User req = Request.blank('/auth/password', POST={'password': password, 'email': email}) resp = req.get_response(application) # Make sure a new User is not created. p_count2 = UserProfile.query().count() u_count2 = User.query().count() self.assertEqual(p_count2, 1) self.assertEqual(u_count2, 1) # Wrong password req = Request.blank('/auth/password', POST={'password': '******', 'email': email}) resp = req.get_response(application)
def create_user(role='admin', id=1): u =User() u._key = ndb.Key('User', id) u.roles = [role] u.put() return u