def test_register_monkey(self): """Test register_monkey().""" form = ProfileEditForm() form['first_name'].data = 'Test' form['last_name'].data = 'User' form['email'].data = '*****@*****.**' form['date_of_birth'].data = '' form['password'].data = '123' form['confirm'].data = '123' monkey = dba.register_monkey(form) assert isinstance(monkey, Monkey) assert monkey.id > 0
def register(): """Register a new monkey.""" if 'id' in session: return redirect(url_for('index')) form = ProfileEditForm(request.form) if request.method == 'POST' and form.validate(): # Check for email uniqueness by querying for another monkey # with the same address: if dba.check_unique_email(form['email'].data): # If no duplicate found: monkey = dba.register_monkey(form) if (monkey): # If registered, log in and go to index: session['id'] = monkey.id return redirect(url_for('index')) else: flash('Error registering!') else: form.email.errors.append( 'This e-mail address is registered with another user') return render_template('edit.html', form=form)
def edit(): """Edit monkey's own profile.""" monkey_self = dba.get_monkey_by_id(session['id']) if monkey_self is None: return redirect(url_for('index')) # If no formdata is present in the request, the form is populated # from the object: form = ProfileEditForm(request.form, monkey_self) if request.method == 'POST' and form.validate(): # Check for email uniqueness by querying for another monkey # with the same address: if dba.check_unique_email(form['email'].data, monkey_self.id): # If no duplicate found: if dba.edit_monkey_profile(monkey_self, form): flash('Changes saved') else: flash('Error saving changes!') else: form.email.errors.append( 'This e-mail address is registered with another user') return render_template('edit.html', form=form, monkey_self=monkey_self)
def test_edit_monkey_profile(self, test_monkey): """Test edit_monkey_profile().""" monkey = test_monkey old_id = monkey.id form = ProfileEditForm() form['first_name'].data = 'Bill' form['last_name'].data = 'Gates' form['email'].data = '*****@*****.**' form['date_of_birth'].data = '' form['password'].data = 'secret' form['confirm'].data = 'secret' assert dba.edit_monkey_profile(monkey, form) assert monkey.id == old_id assert monkey.first_name == 'Bill' assert monkey.last_name == 'Gates' assert type(monkey.password) == \ sqlalchemy_utils.types.password.Password assert monkey.email == '*****@*****.**' assert monkey.age() is None