1
# -*- coding: iso-8859-1 -*-
3
MoinMoin - tests of cache action functions
5
@copyright: 2008 MoinMoin:ThomasWaldmann
6
@license: GNU GPL, see COPYING for details.
11
from MoinMoin import caching
12
from MoinMoin.action import AttachFile, cache
14
from MoinMoin._tests import become_trusted, create_page, nuke_page
17
""" testing action cache """
18
pagename = u"AutoCreatedSillyPageToTestAttachments"
20
def test_cache_key_content(self):
21
request = self.request
22
result1 = cache.key(request, content='foo', secret='bar')
23
result2 = cache.key(request, content='foo', secret='baz')
24
assert result1 # not empty
25
assert result1 != result2 # different for different secret
26
result3 = cache.key(request, content='foofoo', secret='baz')
27
assert result3 != result2 # different for different content
28
result4 = cache.key(request, content='foo'*1000, secret='baz')
29
assert len(result4) == len(result3) # same length of key for different input lengths
31
def test_cache_key_attachment(self):
32
request = self.request
33
pagename = self.pagename
34
attachname = 'foo.txt'
36
become_trusted(request)
37
create_page(request, pagename, u"Foo!")
39
AttachFile.add_attachment(request, pagename, attachname, "Test content1", True)
41
result1 = cache.key(request, itemname=pagename, attachname=attachname, secret='bar')
42
result2 = cache.key(request, itemname=pagename, attachname=attachname, secret='baz')
43
assert result1 # not empty
44
assert result1 != result2 # different for different secret
46
# test below does not work, because mtime is often same, inode can be same due to how add_attachment
47
# works, file size is same, attachment name is same, wikiname/pagename is same.
48
# In practice, this should rather rarely cause problems:
49
#AttachFile.add_attachment(request, pagename, attachname, "Test content2", True)
50
#result3 = cache.key(request, itemname=pagename, attachname=attachname, secret='baz')
51
#assert result3 != result2 # different for different content
53
AttachFile.add_attachment(request, pagename, attachname, "Test content33333", True)
54
result4 = cache.key(request, itemname=pagename, attachname=attachname, secret='baz')
55
assert len(result4) == len(result2) # same length of key for different input lengths
56
nuke_page(request, pagename)
58
def test_put_cache_minimal(self):
59
"""Test if put_cache() works"""
60
request = self.request
63
cache.put(request, key, data)
64
url = cache.url(request, key)
67
meta_cache = caching.CacheEntry(request,
68
arena=cache.cache_arena,
69
scope=cache.cache_scope,
70
key=key+'.meta', use_pickle=True)
71
meta = meta_cache.content()
72
assert meta['httpdate_last_modified'].endswith(' GMT') # only a very rough check, it has used cache mtime as last_modified
73
assert ("Content-Type", "application/octet-stream") in meta['headers']
74
assert ("Content-Length", len(data)) in meta['headers']
76
def test_put_cache_guess_ct_give_lm(self):
77
"""Test if put_cache() works, when we give filename (so it guesses content_type) and last_modified"""
78
request = self.request
82
cache.put(request, key, data, filename=filename, last_modified=1)
83
url = cache.url(request, key)
86
meta_cache = caching.CacheEntry(request,
87
arena=cache.cache_arena,
88
scope=cache.cache_scope,
89
key=key+'.meta', use_pickle=True)
90
meta = meta_cache.content()
91
assert meta['httpdate_last_modified'] == 'Thu, 01 Jan 1970 00:00:01 GMT'
92
assert ("Content-Type", "image/png") in meta['headers']
93
assert ("Content-Length", len(data)) in meta['headers']
95
def test_put_cache_file_like_data(self):
96
"""Test if put_cache() works when we give it a file like object for the content"""
97
request = self.request
100
data = "dontcareatall"
101
data_file = StringIO.StringIO(data)
102
cache.put(request, key, data_file)
103
url = cache.url(request, key)
106
meta_cache = caching.CacheEntry(request,
107
arena=cache.cache_arena,
108
scope=cache.cache_scope,
109
key=key+'.meta', use_pickle=True)
110
meta = meta_cache.content()
111
assert meta['httpdate_last_modified'].endswith(' GMT') # only a very rough check, it has used cache mtime as last_modified
112
assert ("Content-Type", "application/octet-stream") in meta['headers']
113
assert ("Content-Length", len(data)) in meta['headers']
115
data_cache = caching.CacheEntry(request,
116
arena=cache.cache_arena,
117
scope=cache.cache_scope,
119
cached = data_cache.content()
120
assert data == cached
122
def test_put_cache_complex(self):
123
"""Test if put_cache() works for a more complex, practical scenario:
125
As 'source' we just use some random integer as count value.
127
The 'rendered representation' of it is just the word "spam" repeated
128
count times, which we cache.
130
The cache key calculation (for the 'non-guessable' keys) is also
133
In real world, source would be likely some big image, rendered
134
representation of it a thumbnail / preview of it. Or some LaTeX
135
source and its rendered representation as png image.
136
Key calculation could be some MAC or some other hard to guess and
140
request = self.request
141
render = lambda data: "spam" * data
143
keycalc = lambda data: str(data * secret)
145
source = random.randint(1, 100)
146
rendered1 = render(source)
147
key1 = keycalc(source)
149
cache.put(request, key1, rendered1)
150
url1 = cache.url(request, key1)
151
assert 'key=%s' % key1 in url1
153
data_cache = caching.CacheEntry(request,
154
arena=cache.cache_arena,
155
scope=cache.cache_scope,
157
cached1 = data_cache.content()
159
assert render(source) == cached1
160
# if that succeeds, we have stored the rendered representation of source in the cache under key1
162
# now we use some different source, render it and store it in the cache
164
rendered2 = render(source)
165
key2 = keycalc(source)
167
cache.put(request, key2, rendered2)
168
url2 = cache.url(request, key2)
169
assert 'key=%s' % key2 in url2
171
data_cache = caching.CacheEntry(request,
172
arena=cache.cache_arena,
173
scope=cache.cache_scope,
175
cached2 = data_cache.content()
177
assert render(source) == cached2
178
# if that succeeds, we have stored the rendered representation of updated source in the cache under key2
180
assert url2 != url1 # URLs must be different for different source (implies different keys)
183
coverage_modules = ['MoinMoin.action.cache']