2011-05-06 02:53:07 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# django-pyvows extensions
|
|
|
|
# https://github.com/rafaelcaricio/django-pyvows
|
|
|
|
|
|
|
|
# Licensed under the MIT license:
|
|
|
|
# http://www.opensource.org/licenses/mit-license
|
|
|
|
# Copyright (c) 2011 Rafael Caricio rafael@caricio.com
|
|
|
|
|
2011-05-10 02:33:56 +00:00
|
|
|
import os
|
2011-08-22 13:39:02 +00:00
|
|
|
import re
|
2011-08-18 20:18:45 +00:00
|
|
|
|
2011-08-18 21:44:43 +00:00
|
|
|
from pyvows import Vows
|
|
|
|
from django.http import HttpRequest
|
2011-08-18 20:18:45 +00:00
|
|
|
|
2011-08-22 18:08:00 +00:00
|
|
|
from django_pyvows import http_helpers
|
2011-08-18 21:44:43 +00:00
|
|
|
from django_pyvows.assertions import Url, Model, Template
|
2011-08-19 14:57:24 +00:00
|
|
|
from django_pyvows.server import DjangoServer
|
2011-08-22 21:51:39 +00:00
|
|
|
from django_pyvows.settings_manager import settings_tracker
|
2011-08-18 20:18:45 +00:00
|
|
|
|
2011-08-22 14:00:19 +00:00
|
|
|
DEFAULT_PORT = 3331
|
|
|
|
DEFAULT_HOST = '127.0.0.1'
|
2011-08-18 20:18:45 +00:00
|
|
|
|
2011-08-18 21:44:43 +00:00
|
|
|
class DjangoContext(Vows.Context):
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def start_environment(cls, settings_path):
|
|
|
|
if not settings_path:
|
|
|
|
raise RuntimeError('The settings_path argument is required.')
|
|
|
|
os.environ['DJANGO_SETTINGS_MODULE'] = settings_path
|
2011-08-22 21:51:39 +00:00
|
|
|
settings_tracker.install()
|
2011-08-18 20:18:45 +00:00
|
|
|
|
2011-08-18 21:44:43 +00:00
|
|
|
def __init__(self, parent):
|
|
|
|
super(DjangoContext, self).__init__(parent)
|
2011-08-25 21:19:24 +00:00
|
|
|
self.ignore('get_settings', 'template', 'request', 'model', 'url', 'find_in_parent',
|
2011-08-22 13:39:02 +00:00
|
|
|
'start_environment', 'port', 'host', 'get_url', 'get', 'post')
|
2011-08-18 20:18:45 +00:00
|
|
|
|
2011-08-18 21:44:43 +00:00
|
|
|
def setup(self):
|
|
|
|
DjangoContext.start_environment(self.get_settings())
|
2011-08-18 20:18:45 +00:00
|
|
|
|
2011-08-18 21:44:43 +00:00
|
|
|
def get_settings(self):
|
2011-12-21 20:50:32 +00:00
|
|
|
return os.environ.get('DJANGO_SETTINGS_MODULE', 'settings')
|
2011-08-18 20:18:45 +00:00
|
|
|
|
2011-08-18 21:44:43 +00:00
|
|
|
def url(self, path):
|
|
|
|
return Url(self, path)
|
|
|
|
|
|
|
|
def template(self, template_name, context):
|
|
|
|
return Template(template_name, context)
|
|
|
|
|
|
|
|
def request(self, **kw):
|
|
|
|
return HttpRequest(**kw)
|
|
|
|
|
|
|
|
def model(self, model_class):
|
|
|
|
return Model(self, model_class)
|
|
|
|
|
2011-08-18 21:47:23 +00:00
|
|
|
def get(self, path):
|
2011-08-22 18:08:00 +00:00
|
|
|
return http_helpers.get(self.get_url(path))
|
2011-08-18 21:44:43 +00:00
|
|
|
|
2011-08-18 21:47:23 +00:00
|
|
|
def post(self, path, params):
|
2011-08-22 18:08:00 +00:00
|
|
|
return http_helpers.post(self.get_url(path), params)
|
2011-08-18 21:44:43 +00:00
|
|
|
|
2011-08-25 21:19:24 +00:00
|
|
|
def find_in_parent(self, attr_name):
|
2011-08-18 21:44:43 +00:00
|
|
|
ctx = self.parent
|
|
|
|
while ctx:
|
2011-08-25 21:19:24 +00:00
|
|
|
if hasattr(ctx, attr_name):
|
|
|
|
return getattr(ctx, attr_name)
|
2011-08-18 21:44:43 +00:00
|
|
|
ctx = ctx.parent
|
2011-08-25 21:19:24 +00:00
|
|
|
raise ValueError('Host could not be found in the context or any of its parents')
|
|
|
|
|
|
|
|
def get_url(self, path):
|
|
|
|
try:
|
|
|
|
return self.find_in_parent('get_url')(path)
|
|
|
|
except ValueError:
|
|
|
|
return path
|
2011-08-18 21:44:43 +00:00
|
|
|
|
2011-12-21 19:36:13 +00:00
|
|
|
|
2012-03-09 17:02:27 +00:00
|
|
|
|
2011-08-18 21:44:43 +00:00
|
|
|
class DjangoHTTPContext(DjangoContext):
|
|
|
|
|
2012-03-09 17:02:27 +00:00
|
|
|
def start_server(self, host=None, port=None, settings={}):
|
2011-08-22 14:00:19 +00:00
|
|
|
if not port: port = DEFAULT_PORT
|
|
|
|
if not host: host = DEFAULT_HOST
|
|
|
|
|
|
|
|
self.address = (host, port)
|
|
|
|
self.server = DjangoServer(host, port)
|
2012-03-09 17:02:27 +00:00
|
|
|
self.server.start(settings)
|
2011-08-18 20:18:45 +00:00
|
|
|
|
2011-08-18 21:44:43 +00:00
|
|
|
def __init__(self, parent):
|
|
|
|
super(DjangoHTTPContext, self).__init__(parent)
|
2011-08-22 13:39:02 +00:00
|
|
|
self.ignore('start_server', 'settings')
|
2011-08-18 21:44:43 +00:00
|
|
|
|
2011-08-22 14:00:19 +00:00
|
|
|
@property
|
|
|
|
def host(self):
|
|
|
|
if hasattr(self, 'address'):
|
|
|
|
return self.address[0]
|
2011-08-25 21:19:24 +00:00
|
|
|
return self.find_in_parent('host')
|
2011-08-22 14:00:19 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def port(self):
|
|
|
|
if hasattr(self, 'address'):
|
|
|
|
return self.address[1]
|
2011-08-25 21:19:24 +00:00
|
|
|
return self.find_in_parent('port')
|
2011-08-22 14:00:19 +00:00
|
|
|
|
2011-08-18 21:44:43 +00:00
|
|
|
def get_url(self, path):
|
2011-08-22 13:39:02 +00:00
|
|
|
if re.match('^https?:\/\/', path):
|
|
|
|
return path
|
2011-08-18 21:44:43 +00:00
|
|
|
return 'http://%s:%d%s' % (self.host, self.port, path)
|
2011-08-22 13:39:02 +00:00
|
|
|
|