Esempio n. 1
0
    def test_application_view(self):
        from eportfolio.models.app import Application
        from eportfolio.views.application import application_view

        root = Application()

        # Redirect to login view if not authenticated
        request = testing.DummyRequest(root=root)
        response = application_view(root, request)
        self.assertEquals(302, response.code)
        self.assertEquals('http://example.com/login.html',
                          response.headers['Location'])

        # Logged in student
        student = self._add_student()
        self.config.testing_securitypolicy(userid=student.email)
        response = application_view(root, request)
        self.assertEquals(302, response.status_int)
        url = 'http://example.com/users/%s/' % student.id
        self.assertEquals(url, response.headers['Location'])

        # Logged in teacher
        teacher = self._add_teacher()
        self.config.testing_securitypolicy(userid=teacher.email)
        response = application_view(root, request)
        self.assertEquals(302, response.status_int)
        self.assertEquals('http://example.com/dashboard.html',
                          response.headers['Location'])
Esempio n. 2
0
    def test_journal_add_view_indicators(self):
        from eportfolio.models.app import Application
        from eportfolio.views.journal import journal_add_view
        renderer = self.config.testing_add_template('templates/journal_add.pt')

        root = Application()

        project = self._add_project()
        student = self._add_student()
        project.students.append(student)
        indicator_set = self._add_indicator_set()
        indicator1 = self._add_indicator(title=u'First indicator',
                                         indicator_set=indicator_set)
        indicator2 = self._add_indicator(title=u'Second indicator',
                                         indicator_set=indicator_set)
        # Competences have to be added to project over objectives
        objective = self._add_objective(project=project)
        objective.competences.append(indicator_set.competence)

        # Student is logged in
        self.config.testing_securitypolicy(userid=student.email)

        request = testing.DummyRequest(root=root)
        journal_add_view(project, request)
        indicator_sets = renderer.indicator_sets
        self.assertEquals(1, len(indicator_sets))
        self.assertEquals(indicator_set.title, indicator_sets[0]['title'])
        self.assertEquals(2, len(indicator_sets[0]['indicators']))
        self.assertEquals(str(indicator1.id),
                          indicator_sets[0]['indicators'][0]['id'])
        self.assertEquals(str(indicator2.id),
                          indicator_sets[0]['indicators'][1]['id'])

        request = testing.DummyRequest(root=root)
        request.POST['text'] = u'My first journal entry!'
        request.POST['image'] = u''
        request.POST['indicators'] = [
            str(indicator1.id),
        ]
        request.POST['form.submitted'] = 1
        journal_add_view(project, request)

        self.assertEquals(1, student.journal_entries.count())
        self.assertEquals(1, project.journal_entries.count())
        entry = student.journal_entries.first()
        self.assertEquals([indicator1], entry.indicators.all())

        # Indicator can be tagged only once, thus it's not in the list anymore.
        request = testing.DummyRequest(root=root)
        journal_add_view(project, request)
        indicator_sets = renderer.indicator_sets
        self.assertEquals(1, len(indicator_sets[0]['indicators']))
Esempio n. 3
0
    def test_members_view(self):
        from eportfolio.models.app import Application
        from eportfolio.models.project import Project
        from eportfolio.views.members import members_view

        root = Application()
        project = Project()

        # No members assigned yet
        request = testing.DummyRequest(root=root)
        response = members_view(project, request)
        self.assertEquals([], response['all_students'])
        self.assertEquals([], response['all_teachers'])

        # Student in list of students that can be assigned
        student = self._add_student()
        response = members_view(project, request)
        self.assertEquals([student], response['all_students'])

        # Assign student to project
        request.POST['student_id'] = student.id
        request.POST['form.submitted'] = 1
        response = members_view(project, request)
        self.assertEquals([student], project.students.all())

        request = testing.DummyRequest(root=root)
        response = members_view(project, request)
        # Student cannot be assigned a second time
        self.assertEquals([], response['all_students'])
        self.assertEquals([student], response['students'])

        # Teacher in the list of teachers than can be assigned
        teacher = self._add_teacher()
        response = members_view(project, request)
        self.assertEquals([teacher], response['all_teachers'])

        # Assign teacher to project
        request.POST['teacher_id'] = teacher.id
        request.POST['form.submitted'] = 1
        response = members_view(project, request)
        self.assertEquals([teacher], project.teachers.all())

        request = testing.DummyRequest(root=root)
        response = members_view(project, request)
        # Teacher cannot be assigned a second time
        self.assertEquals([], response['all_teachers'])
        self.assertEquals([teacher], response['teachers'])
Esempio n. 4
0
 def test_competences_container(self):
     from eportfolio.models.app import Application
     from eportfolio.views.container import remove_item
     
     root = Application()
     
     # Add competence
     meta_competence = self._add_meta_competence()
     competence = self._add_competence(meta_competence=meta_competence)
     self.assertEquals(1, len(root['competences']))
     # Remove competence
     request = testing.DummyRequest(root=root)
     request.POST['item'] = competence.id
     response = remove_item(root['competences'], request)
     self.assertEquals(200, response.status_int)
     self.assertEquals('success', response.body)
     self.assertEquals(0, len(root['competences']))
     
Esempio n. 5
0
    def test_projects_container(self):
        from eportfolio.models.app import Application
        from eportfolio.views.container import remove_item

        root = Application()

        # Try to remove not existing project
        request = testing.DummyRequest(root=root)
        request.POST['item'] = 'test'
        response = remove_item(root['projects'], request)
        self.assertEquals(400, response.status_int)

        # Add project
        project = self._add_project()
        self.assertEquals(1, len(root['projects']))
        request.POST['item'] = project.id
        response = remove_item(root['projects'], request)
        self.assertEquals(200, response.status_int)
        self.assertEquals(0, len(root['projects']))
Esempio n. 6
0
    def test_teacher_edit_view(self):
        from eportfolio.models.app import Application
        from eportfolio.views.teacher import teacher_edit_view

        root = Application()
        teacher = self._add_teacher()

        request = testing.DummyRequest(root=root)
        response = teacher_edit_view(root['users'][teacher.id], request)
        self.failUnless('Edit' in response.body)

        request = testing.DummyRequest(root=root)
        request.params['form.submitted'] = 1
        request.params['first_name'] = u'Leopold'
        request.params['last_name'] = u'Bloom'
        request.params['email'] = u'*****@*****.**'
        request.params['portrait'] = u''
        teacher_edit_view(root['users'][teacher.id], request)
        self.assertEquals(u'Leopold', teacher.first_name)
        self.assertEquals(u'Bloom', teacher.last_name)
        self.assertEquals(u'*****@*****.**', teacher.email)
Esempio n. 7
0
    def test_journal_add_view(self):
        from eportfolio.models.app import Application
        from eportfolio.views.journal import journal_add_view

        root = Application()

        project = self._add_project()
        student = self._add_student()
        project.students.append(student)

        # Student is logged in
        self.config.testing_securitypolicy(userid=student.email)
        request = testing.DummyRequest(root=root)
        request.POST['text'] = u'My first journal entry!'
        request.POST['image'] = u''
        request.POST['form.submitted'] = 1
        response = journal_add_view(project, request)
        self.assertEquals(1, project.journal_entries.count())
        entry = project.journal_entries[0]
        self.assertEquals(u'My first journal entry!', entry.text)
        self.assertEquals(student, entry.user)
        # Check redirection
        self.assertEquals(302, response.status_int)
        url = 'http://example.com/users/%s/' % student.id
        self.assertEquals(url, response.headers['Location'])

        teacher = self._add_teacher()
        project.teachers.append(teacher)
        # Teacher is logged in
        self.config.testing_securitypolicy(userid=teacher.email)
        request = testing.DummyRequest(root=root)
        request.POST['text'] = u"Teacher's journal entry!"
        request.POST['image'] = u''
        request.POST['form.submitted'] = 1
        response = journal_add_view(project, request)
        self.assertEquals(1, teacher.journal_entries.count())
        # Check redirection
        self.assertEquals(302, response.status_int)
        url = 'http://example.com/projects/%s/' % project.id
        self.assertEquals(url, response.headers['Location'])
Esempio n. 8
0
    def test_projects_view(self):
        from eportfolio.models.app import Application
        from eportfolio.views.projects import projects_view

        root = Application()

        # No projects yet
        request = testing.DummyRequest(root=root)
        response = projects_view(root['projects'], request)
        self.assertEquals([], response['current_projects'])
        self.assertEquals([], response['past_projects'])

        # Add an active project
        current_project = self._add_project()
        current_project.start_date = datetime.date.today(
        ) - datetime.timedelta(days=10)
        current_project.end_date = datetime.date.today() + datetime.timedelta(
            days=10)
        response = projects_view(root['projects'], request)
        self.assertEquals([current_project], response['current_projects'])
        self.assertEquals([], response['past_projects'])

        # Add a past project
        past_project = self._add_project()
        past_project.start_date = datetime.date.today() - datetime.timedelta(
            days=30)
        past_project.end_date = datetime.date.today() - datetime.timedelta(
            days=20)
        response = projects_view(root['projects'], request)
        self.assertEquals([current_project], response['current_projects'])
        self.assertEquals([past_project], response['past_projects'])

        # Check whether projects are traversal wrapped
        self.failUnless(response['current_projects'][0].__parent__)
        self.assertEquals(str(current_project.id),
                          response['current_projects'][0].__name__)
        self.failUnless(response['past_projects'][0].__parent__)
        self.assertEquals(str(past_project.id),
                          response['past_projects'][0].__name__)
Esempio n. 9
0
    def test_logged_in_view(self):
        from eportfolio.models.app import Application
        from eportfolio.views.login import logged_in_view

        root = Application()

        # Not logged in
        request = testing.DummyRequest(root=root)
        response = logged_in_view(root, request)
        self.assertEquals(302, response.status_int)
        url = 'http://example.com/login.html?login_failed=1'
        self.assertEquals(url, response.headers['Location'])

        # Menu's 'Home' entry is used to find the homepage
        from eportfolio.views.menu import GlobalMenu
        from eportfolio.views.menu import StudentHomeEntry
        self.config.registry.registerAdapter(GlobalMenu)
        self.config.registry.registerAdapter(StudentHomeEntry, name='home')

        # Logged in student
        student = self._add_student()
        self.config.testing_securitypolicy(userid=student.email)
        request = testing.DummyRequest(root=root)
        response = logged_in_view(root, request)
        self.assertEquals(302, response.status_int)
        url = 'http://example.com/users/%s/' % student.id
        self.assertEquals(url, response.headers['Location'])

        from eportfolio.views.menu import TeacherHomeEntry
        self.config.registry.registerAdapter(TeacherHomeEntry, name='home')

        # Logged in teacher
        teacher = self._add_teacher()
        self.config.testing_securitypolicy(userid=teacher.email)
        request = testing.DummyRequest(root=root)
        response = logged_in_view(root, request)
        self.assertEquals(302, response.status_int)
        url = 'http://example.com/dashboard.html'
        self.assertEquals(url, response.headers['Location'])
Esempio n. 10
0
    def test_journal_add_view_image(self):
        from eportfolio.models.app import Application
        from eportfolio.views.journal import journal_add_view
        from cgi import MiniFieldStorage

        root = Application()

        project = self._add_project()
        student = self._add_student()
        project.students.append(student)

        # 'upload_directory' setting has to be set
        self.config.add_settings(
            upload_directory=join(dirname(__file__), 'data'))

        # Dummy repoze.filesafe data manager
        from repoze.filesafe.testing import setupDummyDataManager, cleanupDummyDataManager
        setupDummyDataManager()

        # Image file to upload
        image_path = join(dirname(__file__), 'data', 'image.jpg')
        fd = open(image_path, 'rb')
        storage = MiniFieldStorage('image', 'image.jpg')
        storage.file = fd
        storage.filename = 'image.jpg'

        # Student is logged in
        self.config.testing_securitypolicy(userid=student.email)
        request = testing.DummyRequest(root=root)
        request.POST['text'] = u'Entry with an image'
        request.POST['image'] = storage
        request.POST['form.submitted'] = 1
        journal_add_view(project, request)
        self.assertEquals(1, project.journal_entries.count())
        entry = project.journal_entries[0]
        self.assertEqual('image/jpeg', entry.image.content_type)

        cleanupDummyDataManager()
Esempio n. 11
0
    def test_teacher_add_view(self):
        from eportfolio.models.app import Application
        from eportfolio.views.teacher import teacher_edit_view

        root = Application()

        request = testing.DummyRequest(root=root)
        response = teacher_edit_view(root['users'], request)
        self.failUnless('Add' in response.body)

        request = testing.DummyRequest(root=root)
        request.params['form.submitted'] = 1
        request.params['first_name'] = u'Buck'
        request.params['last_name'] = u'Mulligan'
        request.params['email'] = u'*****@*****.**'
        request.params['password'] = u''
        request.params['portrait'] = u''
        # Empty password field triggers registration mail
        teacher_edit_view(root['users'], request)
        registration_mail = self.mailer.mails[-1]
        mail_address = '%s %s <%s>' % (u'Buck', u'Mulligan',
                                       u'*****@*****.**')
        self.assertEquals(mail_address, registration_mail['To'])
Esempio n. 12
0
    def test_view_pw_reset(self):
        from eportfolio.models.app import Application
        from eportfolio.views.pw_reset import view_pw_reset

        root = Application()

        # Call the view
        request = testing.DummyRequest(root=root)
        view_pw_reset(root, request)

        # No email address given
        request = testing.DummyRequest(root=root)
        request.params['form.submitted'] = 1
        response = view_pw_reset(root, request)
        self.failUnless('class="error"' in response.body)

        # Email address not found
        request = testing.DummyRequest(root=root)
        request.params['form.submitted'] = 1
        request.params['email'] = u'*****@*****.**'
        response = view_pw_reset(root, request)
        self.failUnless('class="error"' in response.body)
        self.failUnless('Username not found.' in response.body)

        student = self._add_student()
        student.first_name = u'Buck'
        student.last_name = u'Mulligan'
        student.email = u'*****@*****.**'
        request = testing.DummyRequest(root=root)
        request.params['form.submitted'] = 1
        request.params['email'] = student.email
        response = view_pw_reset(root, request)
        # Check whether mail has been sent
        retrieval_mail = self.mailer.mails[-1]
        mail_address = '%s %s <%s>' % (student.first_name, student.last_name,
                                       student.email)
        self.assertEquals(mail_address, retrieval_mail['To'])
        self.failUnless(
            student.password_reset_key() in retrieval_mail.get_payload(
                decode=True))

        # Try to reset with wrong key
        request = testing.DummyRequest(root=root)
        request.params['form.submitted'] = 1
        request.params['email'] = student.email
        request.params['password'] = u'123456'
        request.params['key'] = '068e16b2e986c41d19ee7bba54cf40ed0d8dfc46'
        response = view_pw_reset(root, request)
        self.failUnless('Reset Password' in response.body)

        # Try to set an empty password
        request = testing.DummyRequest(root=root)
        request.params['form.submitted'] = 1
        request.params['email'] = student.email
        request.params['password'] = u''
        request.params['key'] = student.password_reset_key() + 'e'
        response = view_pw_reset(root, request)
        self.failUnless('Reset Password' in response.body)

        # Reset with correct key
        request = testing.DummyRequest(root=root)
        request.params['form.submitted'] = 1
        request.params['email'] = student.email
        request.params['password'] = u'müller1234'
        request.params['key'] = student.password_reset_key()
        # Dummy auth plugin to check direct login.
        auth_plugin = AuthTktCookiePluginDummy()
        request.environ['repoze.who.plugins'] = dict(auth_tkt=auth_plugin)
        response = view_pw_reset(root, request)
        self.assertEquals(302, response.status_int)
        self.assertEquals('http://example.com/', response.headers['Location'])
        self.assertEquals({'repoze.who.userid': student.email},
                          auth_plugin.identity)
Esempio n. 13
0
DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))
Base = declarative_base()

from eportfolio.models.uuid_type import UUID
from eportfolio.models.domain import DomainObject
from eportfolio.models.file import File
from eportfolio.models.project import Project
from eportfolio.models.user import User
from eportfolio.models.student import Student
from eportfolio.models.journal import JournalEntry
from eportfolio.models.meta_competence import MetaCompetence
from eportfolio.models.competence import Competence
from eportfolio.models.indicator_set import IndicatorSet
from eportfolio.models.indicator import Indicator
from eportfolio.models.comment import Comment
from eportfolio.models.app import Application
from eportfolio.models.teacher import Teacher
from eportfolio.models.objective import Objective

def initialize_sql(db_string):
    engine = create_engine(db_string)
    DBSession.configure(bind=engine)
    Base.metadata.bind = engine
    Base.metadata.create_all(engine)
    
root = Application()

def get_root(request):
    return root