示例#1
0
def consumer(request):
	if request.method == 'POST':
		form = ProxylogForm(request.POST)
		if not form.is_valid():
			resp = HttpResponse('Invalid data')
			resp.status_code = 400
			return resp
		obj = Proxylog(**form.cleaned_data)
		obj.save()
		return HttpResponse(unicode(obj))
	return HttpResponse('proxylog.consumer')
class ProxyLogTest(TestCase):
	def setUp(self):
		self.obj = Proxylog(
			access_time = datetime(2013,2,11,17,40,12).replace(tzinfo=utc),
			url = 'http://webeholder.com.br/index.html',
			net_location = 'webeholder.com.br',
			client_ip = '192.168.10.12',
			request_status = 'TCP_MEM_HIT',
			response_size = 1432,
			response_time = 312,
			response_code =  200,
			method = 'GET',
			user_name = 'ulysses.almeida',
			user_group = 'ADMIN',
			user_agent = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)'
			)

	def test_create(self):
		'''Proxylog must have following attributes:
		access_time, url, client_ip, request_status, 
		response_size, response_time, response_code, 
		method, user_name, user_group, user_agent
		'''
		self.obj.save()
		self.assertEqual(1, self.obj.id)

	def test_net_location(self):
		'obj.net_location must contain only site domain based on url'
		self.obj.url = 'http://webeholder.com.br/index.html'
		self.obj.save()
		self.assertEqual(self.obj.net_location, 'webeholder.com.br')

	def test_inconsistent_net_location(self):
		'obj.net_location must be the domain on obj.url'
		self.obj.url = 'http://webeholder.com.br/index.html'
		self.obj.net_location = 'othersite.com.br'
		self.assertRaises(IntegrityError, self.obj.save)

	def test_client_ip_invalid(self):
		'Save invalid client_ip should raise error'
		self.obj.client_ip = '200.300.400.500'
		self.assertRaises(ValidationError, self.obj.clean_fields)

	def test_unicode(self):
		'''Log string representation should have the following format:
		2013-02-11 17:40:12\thttp://webeholder.com.br/index.html\t192.168.10.12\t1432\t200\tGET\tulysses.almeida\tADMIN
		'''
		self.obj.save()
		self.assertEqual(u"2013-02-11 17:40:12\thttp://webeholder.com.br/index.html\t192.168.10.12\t1432\t200\tGET\tulysses.almeida\tADMIN",unicode(self.obj))