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

« back to all changes in this revision

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

ivle.webapp.testing: Add, with fake request and user.
ivle.webapp.base.test: Add! Test the JSONRESTView, using the new mocks.

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.title = None     # Will be set by dispatch before passing to app
 
47
        self.styles = []
 
48
        self.scripts = []
 
49
        self.scripts_init = []
 
50
        self.write_html_head_foot = False
 
51
        self.request_body = ''
 
52
        self.response_body = ''
 
53
        # In some cases we don't want the template JS (such as the username
 
54
        # and public FQDN) in the output HTML. In that case, set this to 0.
 
55
        self.write_javascript_settings = True
 
56
        self.got_common_vars = False
 
57
 
 
58
    def __del__(self):
 
59
        '''Cleanup, but don't close the nonexistent store.'''
 
60
        if self.store is not None:
 
61
            self.store.close()
 
62
 
 
63
    def ensure_headers_written(self):
 
64
        '''Fake a write of the HTTP and HTML headers if they haven't already
 
65
           been written.'''
 
66
        pass
 
67
 
 
68
    def read(self, len=None):
 
69
        if len is None:
 
70
            data = self.request_body
 
71
            self.request_body = ''
 
72
        else:
 
73
            data = self.request_body[:len]
 
74
            self.request_body = self.request_body[len:]
 
75
        return data
 
76
 
 
77
    def write(self, string, flush=1):
 
78
        '''Write a string to the internal output storage.'''
 
79
        self.ensure_headers_written()
 
80
        if isinstance(string, unicode):
 
81
            self.response_body += string.encode('utf8')
 
82
        else:
 
83
            self.response_body += string
 
84
 
 
85
    def flush(self):
 
86
        '''Fake a flush.'''
 
87
        pass
 
88