Example #1
0
class UsersTest(unittest.TestCase):
  def setUp(self):
    """ Using dicts instead of Shelve objects to speed up testing. """
    self.users = Users({})
    self.username = '******'
    self.password = '******'

  def test_confirm_passwords(self):
    self.assertTrue(confirmed_password_valid('foo', 'foo'))
    self.assertFalse(confirmed_password_valid('foo', 'bar'))

  def test_simple_add_users(self):
    self.assertFalse(self.users.has_user(self.username))
    self.users.register_user(self.username, self.password)
    self.assertTrue(self.users.has_user(self.username))
    self.assertTrue(self.users.login_is_valid(self.username, self.password))

  def test_login_before_register(self):
    self.assertFalse(self.users.login_is_valid(self.username, self.password))

  def test_login_with_wrong_password(self):
    self.users.register_user(self.username, self.password)
    self.assertFalse(self.users.login_is_valid(self.username, 'bad_password'))
Example #2
0
def login():
  """
  Sending a GET request gets the login form page.
  Sending a POST request tries to logs in the user.
  """
  if request.method == 'GET':
    return render_template('login.html')
  else:
    username = request.form['username']
    password = request.form['password']
    with closing(shelve.open(users_file)) as user_shelf:
      users = Users(user_shelf)
      if users.login_is_valid(username, password):
        session['username'] = username
        return redirect(url_for('notes'))
    flash('Login failed. Maybe you made a typo?')
    return render_template('login.html')