~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to ivle/webapp/testing/__init__.py

  • Committer: William Grant
  • Date: 2009-02-23 23:47:02 UTC
  • mfrom: (1099.1.211 new-dispatch)
  • Revision ID: grantw@unimelb.edu.au-20090223234702-db4b1llly46ignwo
Merge from lp:~ivle-dev/ivle/new-dispatch.

Pretty much everything changes. Reread the setup docs. Backup your databases.
Every file is now in a different installed location, the configuration system
is rewritten, the dispatch system is rewritten, URLs are different, the
database is different, worksheets and exercises are no longer on the
filesystem, we use a templating engine, jail service protocols are rewritten,
we don't repeat ourselves, we have authorization rewritten, phpBB is gone,
and probably lots of other things that I cannot remember.

This is certainly the biggest commit I have ever made, and hopefully
the largest I ever will.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import datetime
 
2
 
 
3
from ivle.database import User
 
4
from ivle.dispatch.request import Request
 
5
 
 
6
class FakeUser(User):
 
7
    login = u'fakeuser'
 
8
    state = u'enabled'
 
9
    rolenm = u'student'
 
10
    unixid = 5000
 
11
    nick = u'Fake User'
 
12
    pass_exp = None
 
13
    acct_exp = None
 
14
    last_login = datetime.datetime.now()
 
15
    svn_pass = u'somepass'
 
16
    email = u'fakeuser@example.com'
 
17
    fullname = u'Fake Fixture User'
 
18
    studentid = u'1234'
 
19
 
 
20
class FakeRequest(Request):
 
21
    '''A fake request object, for use as a fixture in tests.
 
22
 
 
23
    This tries to behave fairly closely to an ivle.dispatch.request.Request,
 
24
    but without needing a web server.
 
25
    '''
 
26
    def __init__(self):
 
27
        '''Set sane defaults.'''
 
28
 
 
29
        # Some fields are omitted because they make no sense in the new model.
 
30
        self.headers_written = False
 
31
        self.publicmode = False
 
32
        self.method = 'GET'
 
33
        self.uri = '/'
 
34
        self.user = FakeUser()
 
35
        self.hostname = 'fakehost'
 
36
        self.headers_in = {}
 
37
        self.headers_out = {}
 
38
 
 
39
        # We don't want DB access in tests (by default)
 
40
        self.store = None
 
41
 
 
42
        # Default values for the output members
 
43
        self.status = Request.HTTP_OK
 
44
        self.content_type = None        # Use Apache's default
 
45
        self.location = None
 
46
        self.styles = []
 
47
        self.scripts = []
 
48
        self.scripts_init = []
 
49
        self.request_body = ''
 
50
        self.response_body = ''
 
51
        # In some cases we don't want the template JS (such as the username
 
52
        # and public FQDN) in the output HTML. In that case, set this to 0.
 
53
        self.write_javascript_settings = True
 
54
        self.got_common_vars = False
 
55
 
 
56
    def __del__(self):
 
57
        '''Cleanup, but don't close the nonexistent store.'''
 
58
        if self.store is not None:
 
59
            self.store.close()
 
60
 
 
61
    def ensure_headers_written(self):
 
62
        '''Fake a write of the HTTP and HTML headers if they haven't already
 
63
           been written.'''
 
64
        pass
 
65
 
 
66
    def read(self, len=None):
 
67
        if len is None:
 
68
            data = self.request_body
 
69
            self.request_body = ''
 
70
        else:
 
71
            data = self.request_body[:len]
 
72
            self.request_body = self.request_body[len:]
 
73
        return data
 
74
 
 
75
    def write(self, string, flush=1):
 
76
        '''Write a string to the internal output storage.'''
 
77
        self.ensure_headers_written()
 
78
        if isinstance(string, unicode):
 
79
            self.response_body += string.encode('utf8')
 
80
        else:
 
81
            self.response_body += string
 
82
 
 
83
    def flush(self):
 
84
        '''Fake a flush.'''
 
85
        pass
 
86