def setUp(self): ''' Create user and administrator records to test against. ''' super(ValidateAdminLoginUnitTest, self).setUp() admin = User.createAdministrator('adminname', 'password') user = User.createUser('username', 'password')
def testCreateUser(self): ''' createUser() should create a new non-admin user. ''' # At start, no users. self.assertEquals(User.query.count(), 0) # Create the administrator. user = User.createUser('username', 'password') # 1 user. self.assertEquals(User.query.count(), 1) # Check attributes. self.assertEquals(user.username, 'username') self.assertTrue(check_password_hash(user.password_hash, 'password')) self.assertFalse(user.is_admin)
def testIsAdminWithNonAdminUser(self): ''' If there is a non-admin user in the session, redirect to /login. ''' # Test method. @app.route('/test') @auth.isAdmin def test(admin): return 'executed' # With non-admin user_id in session, redirect to login. with self.app as c: user = User.createUser('username', 'password') # Push in a user id. with c.session_transaction() as s: s['user_id'] = user.id rv = c.get('/test') self.assertRedirect(rv, '/admin/login')
def testIsNotAdminWithNonAdminUser(self): ''' If there is a non-admin user in the session, execute the method. ''' # Test method. @app.route('/test') @auth.isNotAdmin def test(): return 'executed' # With non-admin user_id in session, redirect to login. with self.app as c: user = User.createUser('username', 'password') # Push in a user id. with c.session_transaction() as s: s['user_id'] = user.id rv = c.get('/test') self.assertEquals(rv.status_code, 200) assert 'executed' in rv.data