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

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