示例#1
0
    def testCheckPassword(self):

        '''
        Test password checking.
        '''

        user = User('username', 'password', True)
        self.assertTrue(user.checkPassword('password'))
        self.assertFalse(user.checkPassword('wrong'))
示例#2
0
    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')
示例#3
0
    def testUserExists(self):

        '''
        userExists() should check if there is at least one admin.
        '''

        # False with no administrators.
        self.assertFalse(User.userExists())

        admin = User('username', 'password', True)
        db.session.add(admin)
        db.session.commit()

        # True with an administrator.
        self.assertTrue(User.userExists())
示例#4
0
    def testCreateHaiku(self):

        '''
        createHaiku() should create and save a new record.
        '''

        # At start, no records.
        self.assertEquals(Haiku.query.count(), 0)

        # Create records.
        user = User.createAdministrator('username', 'password')
        haiku = Haiku.createHaiku(user.id, 'test', 1000, 1, 5, 100, 30, 1000)

        # Get time delta.
        timeDelta = (dt.datetime.now() - haiku.created_on).total_seconds()

        # Check for the new record, fetch.
        self.assertEquals(Haiku.query.count(), 1)
        retrievedHaiku = Haiku.query.get(haiku.id)

        # Check attribute assignments.
        self.assertEqual(retrievedHaiku.created_by, user.id)
        self.assertEqual(retrievedHaiku.url_slug, 'test')
        self.assertEqual(retrievedHaiku.word_round_length, 1000)
        self.assertEqual(retrievedHaiku.slicing_interval, 1)
        self.assertEqual(retrievedHaiku.min_blind_submissions, 5)
        self.assertEqual(retrievedHaiku.blind_submission_value, 100)
        self.assertEqual(retrievedHaiku.decay_mean_lifetime, 30 / math.log(2))
        self.assertEqual(retrievedHaiku.seed_capital, 1000)
        self.assertLess(timeDelta, 0.1)
示例#5
0
    def testStop(self):

        '''
        A GET request should stop the slicer for a haiku.
        '''

        # Create an admin and haiku.
        admin = User.createAdministrator('username', 'password')
        haiku = Haiku.createHaiku(admin.id, 'test', 1000, 1, 5, 100, 30, 1000)

        # Start the slicer
        sched.createSlicer(haiku, slicer.slice)

        with self.app as c:

            # Re-get the haiku.
            haiku = Haiku.getHaikuBySlug('test')

            # Push in a user id.
            with c.session_transaction() as s:
                s['user_id'] = admin.id

            # Hit the route, check for redirect back to browse.
            rv = c.get('/admin/stop/' + str(haiku.id), follow_redirects=True)
            self.assertTemplateUsed('admin/browse.html')

            # Check that the slicer is not present.
            job = sched.getJobByHaiku(haiku)
            self.assertFalse(job)
示例#6
0
    def testFailure(self):

        '''
        Flash errors when fields are empty.
        '''

        # Create an admin.
        admin = User.createAdministrator('username', 'password')

        with self.app as c:

            # Empty fields
            rv = c.post('/admin/login', data=dict(
                username = '',
                password = ''
            ), follow_redirects=True)

            # Should re-render login template.
            self.assertTemplateUsed('admin/login.html')

            # Check for errors.
            assert e['noUsername'] in rv.data
            assert e['noPassword'] in rv.data

            # Confirm that there is no id.
            with c.session_transaction() as s:
                self.assertNotIn('user_id', s)
示例#7
0
    def testGetUserByName(self):

        '''
        getUserByName() should retrieve a user with a given name and return
        False if there is no user with the name.
        '''

        # False with no user.
        self.assertFalse(User.getUserByName('user'))

        user = User('user', 'password', False)
        db.session.add(user)
        db.session.commit()

        # When user exists, returns record.
        userRecord = User.getUserByName('user')
        self.assertEquals(user.id, userRecord.id)
示例#8
0
    def testUserIsAdmin(self):

        '''
        userIsAdmin() should check if user with a given id is an admin.
        '''

        admin = User('admin', 'password', True)
        user = User('user', 'password', False)
        db.session.add(admin)
        db.session.add(user)
        db.session.commit()

        # True with an administrator, false with user.
        self.assertTrue(User.userIsAdmin(admin.id))
        self.assertFalse(User.userIsAdmin(user.id))

        # False if an id is passed for which there is no record.
        self.assertFalse(User.userIsAdmin(3))
示例#9
0
    def setUp(self):

        '''
        Create existing haiku record to test against.
        '''

        super(ValidateHaikuUnitTest, self).setUp()

        admin = User.createAdministrator('username', 'password')
        haiku = Haiku.createHaiku(admin.id, 'slug', 1000, 1, 5, 100, 20, 1000)
示例#10
0
    def testForm(self):

        '''
        With a GET request, /admin/login should show the form.
        '''

        # Create an admin.
        admin = User.createAdministrator('username', 'password')

        # Hit with GET.
        rv = self.app.get('/admin/login')
        self.assertTemplateUsed('admin/login.html')
示例#11
0
    def testRedirect(self):

        '''
        Logout route should redirect to login.
        '''

        # Create admin.
        admin = User.createAdministrator('username', 'password')

        # With no user_id in the session hash.
        rv = self.app.get('/admin/logout')
        self.assertRedirect(rv, '/admin/login')
示例#12
0
    def testGetHaikuBySlug(self):

        '''
        getHaikuBySlug() should get a record by the url slug.
        '''

        # Create records.
        user = User.createAdministrator('username', 'password')
        haiku = Haiku.createHaiku(user.id, 'test', 1000, 1, 5, 100, 30, 1000)

        # Fetch and check.
        retrievedHaiku = Haiku.getHaikuBySlug('test')
        self.assertEquals(haiku.id, retrievedHaiku.id)
    def setUp(self):

        '''
        Instantiate the scheduler.
        '''

        super(HaikuSchedulerUnitTest, self).setUp()

        # Create admin and haiku.
        self.user = User.createAdministrator('username', 'password')
        self.haiku = Haiku.createHaiku(self.user.id, 'test', 1000, 1, 5, 100, 30, 1000)

        # Instantiate the scheduler.
        self.sched = HaikuScheduler()
        self.sched.start()
示例#14
0
    def testIsNotInstalledWithUsers(self):

        '''
        If there are users in the database, redirect to /admin.
        '''

        # Test method.
        @app.route('/test')
        @auth.isNotInstalled
        def test():
            return 'executed'

        admin = User.createAdministrator('username', 'password')

        rv = self.app.get('/test')
        self.assertRedirect(rv, '/admin')
示例#15
0
    def testIsInstalledWithUsers(self):

        '''
        If there are users in the database, execute.
        '''

        # Test method.
        @app.route('/test')
        @auth.isInstalled
        def test():
            return 'executed'

        admin = User.createAdministrator('username', 'password')

        rv = self.app.get('/test')
        self.assertEquals(rv.status_code, 200)
        assert 'executed' in rv.data
示例#16
0
    def testForm(self):

        '''
        With a GET request, /admin/new should show the form.
        '''

        # Create an admin.
        admin = User.createAdministrator('username', 'password')

        with self.app as c:

            # Push in a user id.
            with c.session_transaction() as s:
                s['user_id'] = admin.id

            # Hit the route.
            rv = c.get('/admin/new')
            self.assertTemplateUsed('admin/new.html')
示例#17
0
    def testSuccess(self):

        '''
        Valid form should create new haiku.
        '''

        # Create an admin.
        admin = User.createAdministrator('username', 'password')

        with self.app as c:

            # Push in a user id.
            with c.session_transaction() as s:
                s['user_id'] = admin.id

            # At start, no haiku.
            self.assertEquals(Haiku.query.count(), 0)

            # Empty fields
            rv = c.post('/admin/new', data=dict(
                url_slug = 'test',
                word_round_length = 1000,
                slicing_interval = 1,
                min_blind_submissions = 5,
                blind_submission_value = 100,
                decay_half_life = 30,
                seed_capital = 1000
            ), follow_redirects=True)

            # Should re-render register template.
            self.assertTemplateUsed('admin/browse.html')

            # No user should have been created.
            self.assertEquals(Haiku.query.count(), 1)

            # Get the haiku and test the attributes.
            haiku = Haiku.query.first()
            self.assertEquals(haiku.url_slug, 'test')
            self.assertEquals(haiku.word_round_length, 1000)
            self.assertEquals(haiku.slicing_interval, 1)
            self.assertEquals(haiku.min_blind_submissions, 5)
            self.assertEquals(haiku.blind_submission_value, 100)
            self.assertEquals(haiku.decay_mean_lifetime, 30 / math.log(2))
            self.assertEquals(haiku.seed_capital, 1000)
示例#18
0
    def testDeleteHaiku(self):

        '''
        deleteHaiku() should delete a record.
        '''

        # Create records.
        user = User.createAdministrator('username', 'password')
        haiku1 = Haiku.createHaiku(user.id, 'test1', 1000, 1, 5, 100, 30, 1000)
        haiku2 = Haiku.createHaiku(user.id, 'test2', 1000, 1, 5, 100, 30, 1000)

        # At start, two records.
        self.assertEquals(Haiku.query.count(), 2)

        # Delete and check.
        Haiku.deleteHaiku(haiku1.id)
        self.assertEquals(Haiku.query.count(), 1)
        retrievedHaiku = Haiku.query.first()
        self.assertEquals(retrievedHaiku.id, haiku2.id)
示例#19
0
    def testFailure(self):

        '''
        Flash errors for invalid submission.
        '''

        # Create an admin.
        admin = User.createAdministrator('username', 'password')

        with self.app as c:

            # Push in a user id.
            with c.session_transaction() as s:
                s['user_id'] = admin.id

            # At start, no haiku.
            self.assertEquals(Haiku.query.count(), 0)

            # Empty fields
            rv = c.post('/admin/new', data=dict(
                url_slug = '',
                word_round_length = '',
                slicing_interval = '',
                min_blind_submissions = '',
                blind_submission_value = '',
                decay_half_life = '',
                seed_capital = ''
            ), follow_redirects=True)

            # Should re-render register template.
            self.assertTemplateUsed('admin/new.html')

            # Check for errors.
            assert e['noSlug'] in rv.data
            assert e['noRoundLength'] in rv.data
            assert e['noInterval'] in rv.data
            assert e['noMinSubmissions'] in rv.data
            assert e['noSubmissionValue'] in rv.data
            assert e['noHalfLife'] in rv.data
            assert e['noCapital'] in rv.data

            # No user should have been created.
            self.assertEquals(Haiku.query.count(), 0)
示例#20
0
    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)
示例#21
0
    def testSuccess(self):

        '''
        Valid credentials should log the admin in.
        '''

        # Create an admin.
        admin = User.createAdministrator('username', 'password')

        with self.app as c:

            # Empty fields
            rv = c.post('/admin/login', data=dict(
                username = '******',
                password = '******'
            ), follow_redirects=True)

            # Should re-render login template.
            self.assertTemplateUsed('admin/browse.html')

            # Confirm that there is no id.
            with c.session_transaction() as s:
                self.assertEquals(s['user_id'], admin.id)
示例#22
0
    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')
示例#23
0
    def testRecordInitialization(self):

        '''
        __init__() should create the object and set starting attributes.
        '''

        # Create user and haiku.
        user = User.createAdministrator('username', 'password')
        haiku = Haiku(user.id, 'test', 1000, 1, 5, 100, 30, 1000)

        # Get time delta.
        timeDelta = (dt.datetime.now() - haiku.created_on).total_seconds()

        # Check attribute assignments.
        self.assertEqual(haiku.created_by, user.id)
        self.assertEqual(haiku.url_slug, 'test')
        self.assertEqual(haiku.word_round_length, 1000)
        self.assertEqual(haiku.slicing_interval, 1)
        self.assertEqual(haiku.min_blind_submissions, 5)
        self.assertEqual(haiku.blind_submission_value, 100)
        self.assertEqual(haiku.decay_mean_lifetime, 30 / math.log(2))
        self.assertEqual(haiku.seed_capital, 1000)
        self.assertLess(timeDelta, 0.1)
示例#24
0
    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
示例#25
0
    def testUserIdKeyPop(self):

        '''
        Existing user_id should be popped off of session.
        '''

        # Create admin.
        admin = User.createAdministrator('username', 'password')

        with self.app as c:

            # Push in a user id.
            with c.session_transaction() as s:
                s['user_id'] = admin.id

            # Check for the presence of the id.
            self.assertEquals(s['user_id'], 1)

            # Hit the logout route.
            rv = c.get('/admin/logout')

            # Confirm that the id is gone.
            with c.session_transaction() as s:
                self.assertNotIn('user_id', s)
示例#26
0
    def testIsAdminWithAdminUser(self):

        '''
        If there is an admin in the session, execute the method.
        '''

        # Test method.
        @app.route('/test')
        @auth.isAdmin
        def test(admin):
            return 'executed'

        # With admin user_id in session, execute.
        with self.app as c:

            admin = User.createAdministrator('username', 'password')

            # Push in a user id.
            with c.session_transaction() as s:
                s['user_id'] = admin.id

            rv = c.get('/test')
            self.assertEquals(rv.status_code, 200)
            assert 'executed' in rv.data
示例#27
0
    def testDelete(self):

        '''
        A GET request should delete the haiku.
        '''

        # Create an admin and haiku.
        admin = User.createAdministrator('username', 'password')

        # Create 2 haiku.
        haiku1 = Haiku.createHaiku(admin.id, 'test1', 1000, 1, 5, 100, 30, 1000)
        haiku2 = Haiku.createHaiku(admin.id, 'test2', 1000, 1, 5, 100, 30, 1000)

        with self.app as c:

            # Re-get the haiku.
            haiku1 = Haiku.getHaikuBySlug('test1')
            haiku2 = Haiku.getHaikuBySlug('test2')

            # Push in a user id.
            with c.session_transaction() as s:
                s['user_id'] = admin.id

            # At the start, 2 records.
            self.assertEquals(Haiku.query.count(), 2)

            # Hit the route.
            rv = c.get('/admin/delete/' + str(haiku1.id), follow_redirects=True)
            self.assertTemplateUsed('admin/browse.html')

            # 1 record.
            self.assertEquals(Haiku.query.count(), 1)

            # Check that the right record was deleted.
            haiku = Haiku.query.first()
            self.assertEquals(haiku.id, haiku2.id)