def test_validate_email(self): ''' This method tests email validity ''' result = helpers.validate_email("*****@*****.**") self.assertEqual(result, True) print("[test_validate_email] First test case passed") result = helpers.validate_email("invalid-email") self.assertEqual(result, False) print("[test_validate_email] Second test case passed")
def ajax_login(request): email = validate_email(request.POST["email"], for_login=True) password = validate_password(request.POST["password"]) user = authenticate(email=email, password=password) if user == None: sso = AuthRecord.objects.filter(user__email=email) if len(sso) >= 1: # could also be the password is wrong return { "status": "fail", "msg": "You use an identity service provider to log in. Click the %s log in button to sign into this site." % " or ".join( set([ providers.providers[p.provider]["displayname"] for p in sso ])) } return { "status": "fail", "msg": "That's not a username and password combination we have on file." } elif not user.is_active: return {"status": "fail", "msg": "Your account has been disabled."} else: login(request, user) return {"status": "success"}
def webinar(registrant): data = json.loads(registrant) name, email, phone = itemgetter('name', 'email', 'phone')(data['registrant']) #loop through the contacts to match email and phone for contact in contacts: #check if the registrant is already inthe contact List if email != 'None' and email == contact.email: #check if the phone field in the contact is empty but is privided in the registration form, then update if it is if contact.phone == 'None' and phone != 'None': contact.phone = phone return elif phone != 'None' and phone == contact.phone: #check if the email field in the contact is empty but is privided in the registration form, then update if it is if contact.email == 'None' and email != 'None': contact.email = email return #loop through the leads to match email and phone for lead in leads: #check if the registrant is already in the Leads List if email != 'None' and email == lead.email: #check if the phone field in the lead is not empty but is empty in the registration form, then update if it is if lead.phone != 'None' and phone == 'None': phone = lead.phone contacts.append(ContactsList(name, email, phone)) leads.remove(lead) return elif phone != 'None' and phone == lead.phone: #check if the email field in the lead is not empty but is empty in the registration form, then update if it is if lead.email != 'None' and phone == 'None': email = lead.email contacts.append(ContactsList(name, email, phone)) leads.remove(lead) return if email != 'None' and not validate_email(email): return elif phone != 'None' and not validate_phone(phone): return else: contacts.append(ContactsList(name, email, phone))
def ajax_login(request): email = validate_email(request.POST["email"], for_login=True) password = validate_password(request.POST["password"]) user = authenticate(email=email, password=password) if user == None: sso = AuthRecord.objects.filter(user__email=email) if len(sso) >= 1: # could also be the password is wrong return { "status": "fail", "msg": "You use an identity service provider to log in. Click the %s log in button to sign into this site." % " or ".join(set([providers.providers[p.provider]["displayname"] for p in sso])) } return { "status": "fail", "msg": "That's not a username and password combination we have on file." } elif not user.is_active: return { "status": "fail", "msg": "Your account has been disabled." } else: login(request, user) return { "status": "success" }
def profile(request): errors = { } if request.method == "POST": email = None if request.POST.get("email", "").strip() != request.user.email: try: email = validate_email(request.POST.get("email", "")) except Exception, e: errors["email"] = validation_error_message(e) password = None if request.POST.get("password", "").strip() != "": try: password = validate_password(request.POST.get("password", "")) except Exception, e: errors["email"] = validation_error_message(e)
def post(self): email = self.data['email'] password = self.data['password'] name = self.data['name'] if not validate_email(email): return False, {'msg': 'invalid_email'} if not validate_name(name): return True, {'msg': 'invalid_name'} new_user = User.create(email, password, name) return True, { 'id': new_user.key.id(), 'msg': 'user_created', 'token': new_user.api_token, }
def profile(request): errors = {} success = [] if request.method == "POST": email = None if request.POST.get("email", "").strip() != request.user.email: try: email = validate_email(request.POST.get("email", "")) except Exception, e: errors["email"] = validation_error_message(e) password = None if request.POST.get("password", "").strip() != "": try: password = validate_password(request.POST.get("password", "")) except Exception, e: errors["password"] = validation_error_message(e)
def signin(): if request.method == 'GET': return render_template('signin.html') email = request.form.get('email') name = request.form.get('name') password = request.form.get('password') password_check = request.form.get('password_check') user = User.query.filter_by(email=email).first() if password != password_check: flash('Both passwords must be the same') return redirect(url_for('signin')) if user: flash('User already exists') return redirect(url_for('signin')) if not validate_email(email): flash('E-mail address is not valid') return redirect(url_for('signin')) if not validate_pass(name): flash('Password is not valid') return redirect(url_for('signin')) if not validate_name(name): flash('Name name must be between 4 and 20 characters lenght') return redirect(url_for('signin')) new_user = User(email=email, name=name, password=generate_password_hash(password, method='sha256')) db.session.add(new_user) db.session.commit() flash('User has been created') return redirect(url_for('login'))
def validate_login(self, emailID, password, error): error['e'] = '' if not validate_email(emailID): error['e'] = "Tsk, tsk. That's an invalid email address." return None user = None try: user = self.users.find_one({'e': emailID}) except: pass if user is None: # if user not in database error['e'] = "What's happening? Something doesn't match." return None salt = user['p'].split(',')[1] if user['p'] != self.make_pw_hash(password, salt): # if password doesn't a match error['e'] = "What's happening? Something doesn't match." return None if 'c' in user: # if user has signed up but hasn't yet confirmed his/her email address error['e'] = "Please confirm your email address. A confirmation email was sent to %s." % user['e'] return None # if it looks good return user
def resolve_argv(cmd, args): if cmd == "signup": if not validate_email(args[0]): exit_with_stderr("invalid e-mail") return {"email": args[0], "signup_token": create_token(8)} elif cmd == "login": if not validate_auth_token(args[0]): exit_with_stderr("invalid auth token") return {"auth_token": args[0]} auth = get_auth() if cmd == "list": return auth elif cmd == "create": data = { "body": urllib.unquote(args[0]), "creation_token": create_token(8) } return add_content(auth, data=data) ppgn_token = args[0] if cmd in set(["view", "access"]): return add_content(auth, ppgn_token=ppgn_token) elif cmd == "edit": data = {"body": urllib.unquote(args[1])} return add_content(auth, ppgn_token=ppgn_token, data=data) elif cmd in set(["upgrade", "disable"]): data = {"src-access-token": args[1]} return add_content(auth, ppgn_token=ppgn_token, data=data)
def register(): if session.get("username") != None: return redirect("/") session.clear() if request.method == "GET": return render_template("register.html") username = request.form.get("username") password = request.form.get("password") password1 = request.form.get("password1") email = request.form.get("email") if not username or not password or not email or not password1: return render_template( "register.html", register_error="Input in all fields marked with *.") if not validate_email(email): return render_template("register.html", register_error="Invalid Email Address") if not validate_password(password): return render_template( "register.html", register_error="Alpha-numeric Password Required") if password != password1: return render_template("register.html", register_error="Passwords Don't Match") if not validate_username(username): return render_template("register.html", register_error="Invalid Username") username = username.strip() password = password.strip() email = email.strip() user = User.query.filter_by(username=username).first() if user != None: return render_template("register.html", register_error="This Username already exists.") user = User.query.filter_by(email=email).first() if user != None: return render_template( "register.html", register_error="This Email is associated with another account.") password = generate_password_hash(password) code = str(random.randint(100000, 999999)) session["user_registration"] = { "username": username, "password": password, "email": email, "code": code } try: sendmail(email, "Verify Email", code) except: return redirect("/process_verification") return redirect("/verification")
errors["username"] = validation_error_message(e) else: # make up a username that validates (i.e. not already taken) c = User.objects.count() + 100 while True: try: username = validate_username("Anonymous" + str(random.randint(c, c*5))) break except: continue elif request.method == "POST" and settings.REGISTRATION_ASK_USERNAME: errors["username"] = "******" if email: try: email = validate_email(email) except Exception, e: errors["email"] = validation_error_message(e) elif request.method == "POST": errors["email"] = "Provide an email address." password = None if not provider: if request.method == "POST": try: password = validate_password(request.POST.get("password", "")) except Exception, e: errors["password"] = validation_error_message(e) if len(errors) > 0 or request.method != "POST": # Show the form again with the last entered field values and the
def register(): """Register user""" if request.method == "POST": firstname = request.form.get("firstname").title() lastname = request.form.get("lastname").title() email = request.form.get("email") password = request.form.get("password") confirmation = request.form.get("confirmation") role = request.form.get("role").lower() # ensure that first name was submitted if not firstname: return render_template("register.html", error="firstname", message="Please enter your first name") # ensure that last name was submitted if not lastname: return render_template("register.html", error="lastname", message="Please enter your last name") # ensure that email was submitted if not email: return render_template("register.html", error="email", message="Please enter your email address") # ensure that email is valid if not validate_email(email): return render_template("register.html", error="email", message="That is not a valid email address") # ensure that email doesn't exist rows = db.execute("SELECT * FROM users WHERE email = :email", email=email) if len(rows) == 1: return render_template( "register.html", error="email", message="That email address is already registered") # ensure that password was submitted if not password: return render_template("register.html", error="password", message="Please enter a password") # ensure that password and confirmation match if confirmation != password: return render_template("register.html", error="password", message="Those passwords don't match") # ensure that role was selected if role == "select a role": return render_template("register.html", error="role", message="Please select a role") # create account - insert user data into database user = db.execute( "INSERT INTO users (email, hash, firstname, lastname, role) VALUES (:email, :hash, :firstname, :lastname, :role)", email=email, hash=generate_password_hash(password), firstname=firstname, lastname=lastname, role=role) # set default course assignment db.execute( "INSERT INTO courseAssignments (user_id, course_id) VALUES (:user, :course)", user=user, course=0) # set session for user and log in session["user_id"] = db.execute( "SELECT id FROM users WHERE email = :email", email=email)[0]['id'] # set viewMode to user's role global viewMode viewMode = role flash("Registered successfully! Welcome to your dashboard!") return redirect("/dashboard") else: return render_template("register.html")
else: # make up a username that validates (i.e. not already taken) c = User.objects.count() + 100 while True: try: username = validate_username( "Anonymous" + str(random.randint(c, c * 5))) break except: continue elif request.method == "POST" and REGISTRATION_ASK_USERNAME: errors["username"] = "******" if email: try: email = validate_email(email) except Exception, e: errors["email"] = validation_error_message(e) elif request.method == "POST": errors["email"] = "Provide an email address." password = None if not provider: if request.method == "POST": try: password = validate_password(request.POST.get("password", "")) except Exception, e: errors["password"] = validation_error_message(e) if len(errors) > 0 or request.method != "POST": # Show the form again with the last entered field values and the
def test_validate_email(self): assert validate_email("*****@*****.**") == True assert validate_email("*****@*****.**") == True assert validate_email("alice@example") == False assert validate_email("alice") == False
#Get all users except posts elif choice == 2: all_users = helpers.get_all_users(authors) pprint(all_users) #user_id = int(input("Enter the user_id from the above list: ")) #Hardcoding value for jenkins deployment USER_ID = 1 pprint(authors[USER_ID].get_posts()) # Authenticate user first and then add new post elif choice == 3: EMAIL = "*****@*****.**" PASSWORD = "******" if helpers.validate_email(EMAIL): user_id = helpers.authenticate_user(authors, EMAIL, PASSWORD) TITLE = "Welcome to Quantiphi!" post_id = helpers.generate_post_id(authors) authors[user_id - 1].add_post({ "post_id": post_id, "title": TITLE }) print("Post added successfully!") else: print("Please enter valid email") elif choice == -1: break except TypeError as cached_exception: print(cached_exception)