2024-07-12 00:17:15 -05:00
|
|
|
# -*- coding: utf-8; -*-
|
|
|
|
|
2024-08-05 21:58:31 -05:00
|
|
|
from pyramid.httpexceptions import HTTPFound, HTTPForbidden, HTTPNotFound
|
2024-07-12 00:17:15 -05:00
|
|
|
|
2024-08-16 22:52:24 -05:00
|
|
|
from wuttaweb.views import base as mod
|
2024-08-04 20:35:41 -05:00
|
|
|
from wuttaweb.forms import Form
|
2024-08-16 22:52:24 -05:00
|
|
|
from wuttaweb.grids import Grid, GridAction
|
|
|
|
from tests.util import WebTestCase
|
2024-07-12 00:17:15 -05:00
|
|
|
|
|
|
|
|
2024-08-16 22:52:24 -05:00
|
|
|
class TestView(WebTestCase):
|
2024-07-12 00:17:15 -05:00
|
|
|
|
2024-08-16 22:52:24 -05:00
|
|
|
def make_view(self):
|
|
|
|
return mod.View(self.request)
|
2024-08-04 20:35:41 -05:00
|
|
|
|
2024-07-12 00:17:15 -05:00
|
|
|
def test_basic(self):
|
2024-08-16 22:52:24 -05:00
|
|
|
view = self.make_view()
|
|
|
|
self.assertIs(view.request, self.request)
|
|
|
|
self.assertIs(view.config, self.config)
|
|
|
|
self.assertIs(view.app, self.app)
|
2024-08-04 20:35:41 -05:00
|
|
|
|
2024-08-05 14:21:54 -05:00
|
|
|
def test_forbidden(self):
|
2024-08-16 22:52:24 -05:00
|
|
|
view = self.make_view()
|
|
|
|
error = view.forbidden()
|
2024-08-05 14:21:54 -05:00
|
|
|
self.assertIsInstance(error, HTTPForbidden)
|
|
|
|
|
2024-08-04 20:35:41 -05:00
|
|
|
def test_make_form(self):
|
2024-08-16 22:52:24 -05:00
|
|
|
view = self.make_view()
|
|
|
|
form = view.make_form()
|
2024-08-04 20:35:41 -05:00
|
|
|
self.assertIsInstance(form, Form)
|
|
|
|
|
2024-08-07 14:00:53 -05:00
|
|
|
def test_make_grid(self):
|
2024-08-16 22:52:24 -05:00
|
|
|
view = self.make_view()
|
|
|
|
grid = view.make_grid()
|
2024-08-07 14:00:53 -05:00
|
|
|
self.assertIsInstance(grid, Grid)
|
|
|
|
|
2024-08-16 22:52:24 -05:00
|
|
|
def test_make_grid_action(self):
|
|
|
|
view = self.make_view()
|
|
|
|
action = view.make_grid_action('view')
|
|
|
|
self.assertIsInstance(action, GridAction)
|
|
|
|
|
2024-08-05 21:58:31 -05:00
|
|
|
def test_notfound(self):
|
2024-08-16 22:52:24 -05:00
|
|
|
view = self.make_view()
|
|
|
|
error = view.notfound()
|
2024-08-05 21:58:31 -05:00
|
|
|
self.assertIsInstance(error, HTTPNotFound)
|
|
|
|
|
2024-08-04 20:35:41 -05:00
|
|
|
def test_redirect(self):
|
2024-08-16 22:52:24 -05:00
|
|
|
view = self.make_view()
|
|
|
|
error = view.redirect('/')
|
2024-08-04 20:35:41 -05:00
|
|
|
self.assertIsInstance(error, HTTPFound)
|
|
|
|
self.assertEqual(error.location, '/')
|
2024-08-16 22:52:24 -05:00
|
|
|
|
2024-08-25 12:20:28 -05:00
|
|
|
def test_file_response(self):
|
|
|
|
view = self.make_view()
|
|
|
|
|
|
|
|
# default uses attachment behavior
|
|
|
|
datfile = self.write_file('dat.txt', 'hello')
|
|
|
|
response = view.file_response(datfile)
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertEqual(response.content_disposition, 'attachment; filename="dat.txt"')
|
|
|
|
|
|
|
|
# but can disable attachment behavior
|
|
|
|
datfile = self.write_file('dat.txt', 'hello')
|
|
|
|
response = view.file_response(datfile, attachment=False)
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertIsNone(response.content_disposition)
|
|
|
|
|
|
|
|
# path not found
|
|
|
|
crapfile = '/does/not/exist'
|
|
|
|
response = view.file_response(crapfile)
|
|
|
|
self.assertEqual(response.status_code, 404)
|
|
|
|
|
2024-08-16 22:52:24 -05:00
|
|
|
def test_json_response(self):
|
|
|
|
view = self.make_view()
|
|
|
|
response = view.json_response({'foo': 'bar'})
|
|
|
|
self.assertEqual(response.status_code, 200)
|