import hashlib

from lettuce import world
from tornado.escape import json_decode
from tornado.httpclient import HTTPClient, HTTPRequest

from newebe.settings import TORNADO_PORT
from newebe.profile.models import UserManager, User

ROOT_URL = "http://localhost:%d/" % TORNADO_PORT

class NewebeClient(HTTPClient):
    '''
    Tornado client wrapper to write requests to Newebe faster.
    '''

    def login(self, password):
        '''
        Grab authentication cookie from login request.
        '''
        response = self.fetch(ROOT_URL + "login/json/", 
                method="POST", body='{"password":"%s"}' % password)
        
        assert response.headers["Set-Cookie"].startswith("password=")
        self.cookie = response.headers["Set-Cookie"]


    def set_default_user(self):
        user = UserManager.getUser()
        if user:
            user.delete()
        
        user = User(
            name = "John Doe",
            password = hashlib.sha224("password").hexdigest(),
            key = None,
            authorKey = "authorKey",
            url = "url",
            description = "my description"
        )
        user.save()

    def get(self, url):
        '''
        Perform a GET request.
        '''
        request = HTTPRequest(url)
        if hasattr(self, "cookie") and self.cookie:
            request.headers["Cookie"] = self.cookie
        return HTTPClient.fetch(self, request)


    def post(self, url, body):
        '''
        Perform a POST request.
        '''

        request = HTTPRequest(url, method="POST", body=body)
        if hasattr(self, "cookie") and self.cookie:
            request.headers["Cookie"] = self.cookie
        return HTTPClient.fetch(self, request)


    def put(self, url, body):
        '''
        Perform a PUT request.
        '''

        request = HTTPRequest(url, method="PUT", body=body)
        if hasattr(self, "cookie") and self.cookie:
            request.headers["Cookie"] = self.cookie
        return HTTPClient.fetch(self, request)


    def delete(self, url):
        '''
        Perform a DELETE request.
        '''

        request = HTTPRequest(url, method="DELETE")
        if self.cookie:
            request.headers["Cookie"] = self.cookie
        return HTTPClient.fetch(self, request)


    def fetch_documents_from_url(self, url):
        '''
        Retrieve newebe documents from a givent url
        '''

        response = self.get(url)

        assert response.code == 200
        assert response.headers["Content-Type"] == "application/json"
 
        world.data = json_decode(response.body)
        return world.data["rows"]


    def fetch_documents(self, path):
        '''
        Retrieve documents from path located on localhost server.
        '''

        return self.fetch_documents_from_url(ROOT_URL + path)    
        

@@ -382,24 +382,25 @@
 r.%0A    '''%0A%0A
+%0A
     def logi
@@ -527,24 +527,29 @@
 f.fetch(
-ROOT_URL
+self.root_url
  + %22logi
@@ -790,18 +790,153 @@
 ser(self
-):
+, url=ROOT_URL):%0A        '''%0A        Set to DB default user. This is useful for automatic login.%0A        '''%0A        self.root_url = url%0A
 %0A       
@@ -1215,13 +1215,11 @@
 l = 
-%22
 url
-%22
 ,%0A  
@@ -1290,16 +1290,17 @@
 save()%0A%0A
+%0A
     def 
@@ -2414,32 +2414,338 @@
 elf, request)%0A%0A%0A
+    def fetch_document_from_url(self, url):%0A        '''%0A        Retrieve newebe document from a givent url%0A        '''%0A%0A        response = self.get(url)%0A%0A        assert response.code == 200%0A        assert response.headers%5B%22Content-Type%22%5D == %22application/json%22%0A %0A        return json_decode(response.body)%0A%0A%0A
     def fetch_do
@@ -2813,25 +2813,29 @@
 ebe document
-s
+ list
  from a give
@@ -3076,24 +3076,218 @@
 a%5B%22rows%22%5D%0A%0A%0A
+    def fetch_document(self, path):%0A        '''%0A        Retrieve document from path located on localhost server.%0A        '''%0A%0A        return self.fetch_document_from_url(self.root_url + path)%0A%0A%0A
     def fetc
@@ -3348,17 +3348,21 @@
 document
-s
+ list
  from pa
@@ -3451,24 +3451,29 @@
 rom_url(
-ROOT_URL
+self.root_url
  + path)

add features to make testing easier notably about grabing document and login