Skip to content

Commit

Permalink
Merge branch 'feature/convert_browser_tests'
Browse files Browse the repository at this point in the history
* feature/convert_browser_tests:
  Replace browser-errors.txt with equivalent webtest tests.
  Replace browser-cache.txt with equivalent webtest tests.
  Replace browser-errors.txt with equivalent webtest tests.
  Replace browser-cache.txt with equivalent webtest tests.
  • Loading branch information
disko committed Dec 11, 2015
2 parents dbf9bd0 + c7129bc commit 769ef34
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 181 deletions.
136 changes: 0 additions & 136 deletions kotti/tests/browser-cache.txt

This file was deleted.

45 changes: 0 additions & 45 deletions kotti/tests/browser-errors.txt

This file was deleted.

79 changes: 79 additions & 0 deletions kotti/tests/test_cache.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,34 @@
import datetime
import time

from mock import patch
from mock import MagicMock
import pytest

from kotti.resources import File
from kotti.resources import Image
from kotti.testing import asset
from kotti.testing import Dummy
from kotti.views.cache import set_max_age


def parse_expires(date_string):
return datetime.datetime(*(
time.strptime(date_string, "%a, %d %b %Y %H:%M:%S GMT")[0:6]))


def delta(date_string):
now = datetime.datetime.utcnow()
return parse_expires(date_string) - now


@pytest.fixture
def cachetest_content(root, filedepot):
image = asset('sendeschluss.jpg').read()
root['textfile'] = File("file contents", u"mytext.txt", u"text/plain")
root['image'] = Image(image, u"sendeschluss.jpg", u"image/jpeg")


class TestSetMaxAge:
def test_preserve_existing_headers(self):
response = Dummy(headers={
Expand Down Expand Up @@ -76,3 +97,61 @@ def test_request_has_no_context(self):
set_cache_headers(event)

assert chooser.call_count == 0


class TestBrowser:

def test_cache_unauth(self, webtest, cachetest_content):

# html
resp = webtest.app.get('/')
assert resp.headers.get('X-Caching-Policy') == 'Cache HTML'
assert resp.headers.get('Cache-Control') == 'max-age=0,s-maxage=3600'
d = delta(resp.headers.get('Expires'))
assert (d.days, d.seconds) < (0, 0)

# media content
resp = webtest.app.get('/textfile/inline-view')
assert resp.headers.get('X-Caching-Policy') == 'Cache Media Content'
assert resp.headers.get('Cache-Control') == 'max-age=14400'
d = delta(resp.headers.get('Expires'))
assert (d.days, d.seconds) > (0, 14000)
resp = webtest.app.get('/image/inline-view')
assert resp.headers.get('X-Caching-Policy') == 'Cache Media Content'

# resources
resp = webtest.app.get('/static-kotti/base.css')
assert resp.headers.get('X-Caching-Policy') == 'Cache Resource'
assert resp.headers.get('Cache-Control') == 'max-age=2764800,public'
d = delta(resp.headers.get('Expires'))
assert (d.days, d.seconds) > (30, 0)
assert 'Last-Modified' in resp.headers

# post
resp = webtest.app.post('/', '')
assert 'X-Caching-Policy' not in resp.headers

# 404
resp = webtest.app.get('/this-isnt-here', status=404)
assert 'X-Caching-Policy' not in resp.headers

@pytest.mark.user('admin')
def test_cache_auth(self, webtest, cachetest_content):

# html
resp = webtest.app.get('/')
assert resp.headers.get('X-Caching-Policy') == 'No Cache'

# media content
resp = webtest.app.get('/textfile/inline-view')
assert resp.headers.get('X-Caching-Policy') == 'No Cache'
resp = webtest.app.get('/image/inline-view')
assert resp.headers.get('X-Caching-Policy') == 'No Cache'

# resources
resp = webtest.app.get('/static-kotti/base.css')
assert resp.headers.get('X-Caching-Policy') == 'Cache Resource'

# 404
resp = webtest.app.get('/this-isnt-here', status=404)
assert 'X-Caching-Policy' not in resp.headers
16 changes: 16 additions & 0 deletions kotti/tests/test_httpexceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
""" Kotti HTTP Exception browser tests """

import pytest


def test_404_anon(webtest, root):

resp = webtest.app.get('/non-existent', status=404)
assert 'Not Found' in resp.text


@pytest.mark.user('admin')
def test_404_anon(webtest, root):

resp = webtest.app.get('/non-existent', status=404)
assert 'Not Found' in resp.text

0 comments on commit 769ef34

Please sign in to comment.