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-22 20:06:54 +00:00
|
|
|
self.settings = {}
|
2011-08-22 13:39:02 +00:00
|
|
|
self.ignore('get_settings', 'template', 'request', 'model', 'url',
|
|
|
|
'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):
|
|
|
|
if 'DJANGO_SETTINGS_MODULE' in os.environ:
|
|
|
|
return os.environ['DJANGO_SETTINGS_MODULE']
|
|
|
|
else:
|
|
|
|
return '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
|
|
|
|
|
|
|
def get_url(self, path):
|
|
|
|
ctx = self.parent
|
|
|
|
while ctx:
|
|
|
|
if hasattr(ctx, 'get_url'):
|
|
|
|
return ctx.get_url(path)
|
|
|
|
ctx = ctx.parent
|
2011-08-18 21:47:23 +00:00
|
|
|
return ""
|
2011-08-18 21:44:43 +00:00
|
|
|
|
|
|
|
class DjangoHTTPContext(DjangoContext):
|
|
|
|
|
2011-08-22 14:00:19 +00:00
|
|
|
def start_server(self, host=None, port=None):
|
|
|
|
if not port: port = DEFAULT_PORT
|
|
|
|
if not host: host = DEFAULT_HOST
|
|
|
|
|
|
|
|
self.address = (host, port)
|
|
|
|
self.server = DjangoServer(host, port)
|
2011-08-22 21:51:39 +00:00
|
|
|
self.server.start(self.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]
|
|
|
|
|
|
|
|
if self.parent and hasattr(self.parent, 'host'):
|
|
|
|
return self.parent.host
|
|
|
|
|
|
|
|
raise RuntimeError('Host could not be found in the context or any of its parents')
|
|
|
|
|
|
|
|
@property
|
|
|
|
def port(self):
|
|
|
|
if hasattr(self, 'address'):
|
|
|
|
return self.address[1]
|
|
|
|
|
|
|
|
if self.parent and hasattr(self.parent, 'port'):
|
|
|
|
return self.parent.port
|
|
|
|
|
|
|
|
raise RuntimeError('Port could not be found in the context or any of its parents')
|
|
|
|
|
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
|
|
|
|