示例#1
0
def test_home_page_allows_signin():
    b = Bolacha()
    headers, html = b.get('http://0.0.0.0:8888/')

    assert '200' in headers['status']
    assert 'Enter' in html
    assert 'access' in html
    assert 'secret' in html
示例#2
0
def test_post_with_login():
    b = Bolacha()
    head, body = b.request('http://localhost:5050/', 'POST',
                           body={'username': '******',
                                 'password': '******'})

    assert_equals(body, 'Welcome to the website!')
    head, body = b.request('http://localhost:5050/', 'GET')
    assert_equals(body, 'Welcome to the website!')
示例#3
0
def test_headers_are_instance_scoped():
    b1 = Bolacha()
    b2 = Bolacha()
    b1.headers['foo'] = 'foo foo'
    b2.headers['bar'] = 'bar bar'

    b3 = Bolacha()

    assert_equals(b1.headers, {'foo': 'foo foo'})
    assert_equals(b2.headers, {'bar': 'bar bar'})
    assert_equals(b3.headers, {})
示例#4
0
def test_signin_redirects_and_sets_cookie():
    signin = {
        'access': 'abcdef',
        'secret': 'sdfghj'
    }

    b = Bolacha()
    headers, html = b.post('http://0.0.0.0:8888/signin', signin)

    assert '302' in headers['status']
    assert '/' in headers['location']
    assert 'access=' in headers['set-cookie']
    assert 'secret=' in headers['set-cookie']
示例#5
0
def test_set_content_type_urlencoded_when_body_dict_and_none_was_given():
    mocker = Mox()

    klass_mock = mocker.CreateMockAnything()
    http_mock = mocker.CreateMockAnything()
    klass_mock().AndReturn(http_mock)

    request_headers = {'Content-type': 'application/x-www-form-urlencoded'}

    http_mock.request('http://somewhere.com', 'GET', '', request_headers). \
        AndReturn(({}, ''))

    mocker.ReplayAll()

    b = Bolacha(klass_mock)
    b.request('http://somewhere.com', 'GET', body={})
    mocker.VerifyAll()
示例#6
0
def test_upload_after_login():
    b = Bolacha()

    head, body = b.request('http://localhost:5050/', 'GET')
    assert_equals(body, 'You are not authenticated!')

    b.request('http://localhost:5050/', 'POST',
              body={'username': '******',
                    'password': '******'})

    gpl2 = open(LOCAL_FILE('data', 'gpl-2.0.tex'))
    head, body = b.request('http://localhost:5050/upload', 'POST',
                           body={'file': gpl2})

    assert_equals(head['status'], '200')
    gpl2.seek(0)
    assert_equals(body, gpl2.read())
示例#7
0
def test_when_body_dict_and_content_type_is_specified():
    mocker = Mox()

    klass_mock = mocker.CreateMockAnything()
    http_mock = mocker.CreateMockAnything()
    klass_mock().AndReturn(http_mock)

    request_headers = {'Content-type': 'text/plain'}

    http_mock.request('http://somewhere.com', 'GET', '', request_headers). \
        AndReturn(({}, ''))

    mocker.ReplayAll()

    b = Bolacha(klass_mock)
    b.request('http://somewhere.com', 'GET', body={}, headers=request_headers)
    mocker.VerifyAll()
示例#8
0
def test_request_calls_http_request():
    mocker = Mox()

    klass_mock = mocker.CreateMockAnything()
    http_mock = mocker.CreateMockAnything()
    klass_mock().AndReturn(http_mock)

    response_headers = {}
    response_body = "should be my html content"

    http_mock.request('http://somewhere.com', 'GET', '', {}). \
        AndReturn((response_headers, response_body))

    mocker.ReplayAll()

    b = Bolacha(klass_mock)
    assert_equals(b.request('http://somewhere.com', 'GET'), (response_headers,
                                                             response_body))
    mocker.VerifyAll()
示例#9
0
def test_request_when_persistent():
    mocker = Mox()

    klass_mock = mocker.CreateMockAnything()
    http_mock = mocker.CreateMockAnything()
    klass_mock().AndReturn(http_mock)

    response_headers = {'connection': 'close'}

    http_mock.request('http://somewhere.com', 'GET', '', {}). \
        AndReturn((response_headers, ''))

    http_mock.request('http://somewhere.com', 'GET', '', {}).AndReturn(({}, ''))

    mocker.ReplayAll()

    b = Bolacha(klass_mock, persistent=True)
    b.request('http://somewhere.com', 'GET')
    b.request('http://somewhere.com', 'GET')
    mocker.VerifyAll()
示例#10
0
def test_request_keep_sending_last_cookies():
    mocker = Mox()

    http_mock = mocker.CreateMockAnything()

    response_headers1 = {'set-cookie': 5}
    response_headers2 = {'good': 10}
    response_headers3 = {'set-cookie': 20}

    request_headers1 = {}
    request_headers2 = {'Cookie': 5}
    request_headers3 = {'Cookie': 5}
    request_headers4 = {'Cookie': 20}

    # 1st request
    http_mock.request('http://somewhere.com', 'GET',
                      '', request_headers1). \
        AndReturn((response_headers1, ''))

    # 2nd request
    http_mock.request('http://somewhere.com', 'GET',
                      '', request_headers2). \
        AndReturn((response_headers2, ''))

    # 3rd request
    http_mock.request('http://somewhere.com', 'GET',
                      '', request_headers3). \
        AndReturn((response_headers3, ''))

    # 4th request
    http_mock.request('http://somewhere.com', 'GET',
                      '', request_headers4). \
        AndReturn(({}, ''))

    mocker.ReplayAll()
    bol = Bolacha()
    bol.http = http_mock
    for x in range(4):
        bol.request('http://somewhere.com', 'GET')

    mocker.VerifyAll()
示例#11
0
def test_request_with_multiple_files_with_same_name_will_upload_multipart():
    mocker = Mox()

    klass_mock = mocker.CreateMockAnything()
    http_mock = mocker.CreateMockAnything()
    klass_mock().AndReturn(http_mock)

    class FileStub(object):
        name = '/path/to/file'
        def __init__(self, num):
            self.num = num
        def read(self):
            return 'FileStubContent-%s' % self.num

    request_headers = {}
    body = {
        'pictures': (FileStub(1), FileStub(2)),
    }

    expected_body = '--%(boundary)s\r\nContent-Disposition: form-data; ' \
                    'name="pictures"; filename="file"\r\nContent-Type: ' \
                    'application/octet-stream\r\n\r\nFileStubContent-1\r\n' \
                    '--%(boundary)s\r\nContent-Disposition: form-data; ' \
                    'name="pictures"; filename="file"\r\nContent-Type: ' \
                    'application/octet-stream\r\n\r\nFileStubContent-2\r\n' \
                    '--%(boundary)s--\r\n' \
                    % {'boundary': BOUNDARY}
    expected_header = {'content-length': '364',
                       'Content-type': 'multipart/form-data; ' \
                       'boundary=%s' % BOUNDARY}

    http_mock.request('http://somewhere.com', 'POST',
                      expected_body, expected_header). \
        AndReturn(({}, ''))

    mocker.ReplayAll()

    b = Bolacha(klass_mock)
    b.request('http://somewhere.com', 'POST', body=body, headers=request_headers)
    mocker.VerifyAll()
示例#12
0
def test_request_handle_cookies():
    mocker = Mox()

    http_mock = mocker.CreateMockAnything()
    request_headers1 = {}
    response_headers1 = {'set-cookie': 'Will log in'}

    request_headers2 = {'Cookie': 'Will log in'}
    response_headers2 = {'set-cookie': 'Already logged as root'}

    request_headers3 = {'Cookie': 'Already logged as root'}
    response_headers3 = {'set-cookie': 'Just logged out'}

    request_headers4 = {'Cookie': 'Just logged out'}
    response_headers4 = {'set-cookie': 'Will log in'}

    http_mock.request('http://somewhere.com', 'GET',
                      '', request_headers1). \
        AndReturn((response_headers1, ''))

    http_mock.request('http://somewhere.com', 'GET',
                      '', request_headers2). \
        AndReturn((response_headers2, ''))

    http_mock.request('http://somewhere.com', 'GET',
                      '', request_headers3). \
        AndReturn((response_headers3, ''))

    http_mock.request('http://somewhere.com', 'GET',
                      '', request_headers4). \
        AndReturn((response_headers4, ''))

    mocker.ReplayAll()
    bol = Bolacha()
    bol.http = http_mock
    for x in range(4):
        bol.request('http://somewhere.com', 'GET')

    mocker.VerifyAll()
示例#13
0
def test_request_with_file_will_upload_multipart():
    mocker = Mox()

    klass_mock = mocker.CreateMockAnything()
    http_mock = mocker.CreateMockAnything()
    klass_mock().AndReturn(http_mock)

    class FileStub(object):
        name = '/path/to/file'
        def read(self):
            return 'FileStubContent'

    request_headers = {}
    body = {
        'name': 'John Doe',
        'picture': FileStub(),
    }

    expected_body = '--%(boundary)s\r\nContent-Disposition: form-data; ' \
                    'name="picture"; filename="file"\r\nContent-Type: ' \
                    'application/octet-stream\r\n\r\nFileStubContent\r\n' \
                    '--%(boundary)s\r\nContent-Disposition: form-data; ' \
                    'name="name"\r\n\r\nJohn Doe\r\n' \
                    '--%(boundary)s--\r\n' % {'boundary': BOUNDARY}
    expected_header = {'content-length': '291',
                       'Content-type': 'multipart/form-data; ' \
                       'boundary=%s' % BOUNDARY}

    http_mock.request('http://somewhere.com', 'POST',
                      expected_body, expected_header). \
        AndReturn(({}, ''))

    mocker.ReplayAll()

    b = Bolacha(klass_mock)
    b.request('http://somewhere.com', 'POST', body=body, headers=request_headers)
    mocker.VerifyAll()
示例#14
0
def test_request_with_body_dict():
    mocker = Mox()

    klass_mock = mocker.CreateMockAnything()
    http_mock = mocker.CreateMockAnything()
    klass_mock().AndReturn(http_mock)

    response_headers = {'header1': 'value of header'}
    response_body = {'param1': 'value1', 'foo': 'bar'}

    http_mock.request('http://somewhere.com', 'GET',
                      'foo=bar&param1=value1',
                      prepare_header(response_headers)). \
        AndReturn((response_headers, response_body))

    mocker.ReplayAll()

    b = Bolacha(klass_mock)
    got = b.request('http://somewhere.com', 'GET',
                    response_body,
                    prepare_header(response_headers))
    assert_equals(got, (response_headers,
                        response_body))
    mocker.VerifyAll()
示例#15
0
def test_request_with_body_dict_with_multiple_value_key():
    mocker = Mox()

    klass_mock = mocker.CreateMockAnything()
    http_mock = mocker.CreateMockAnything()
    klass_mock().AndReturn(http_mock)

    response_headers = {'header1': 'value of header'}
    response_body = {'multiple': ('choice1', 'choice2')}

    http_mock.request('http://somewhere.com', 'GET',
                      'multiple=choice1&multiple=choice2',
                      prepare_header(response_headers)). \
        AndReturn((response_headers, response_body))

    mocker.ReplayAll()

    b = Bolacha(klass_mock)
    got = b.request('http://somewhere.com', 'GET',
                    response_body,
                    prepare_header(response_headers))
    assert_equals(got, (response_headers,
                        response_body))
    mocker.VerifyAll()
示例#16
0
def test_head_shortcut():
    mocker = Mox()

    b = Bolacha()
    b.request = mocker.CreateMockAnything()

    b.request('host', 'HEAD', body={'name': 'foo', 'age': 30}, headers=None)
    mocker.ReplayAll()

    b.head('host', {'name': 'foo', 'age': 30})

    mocker.VerifyAll()
示例#17
0
class Crocodoc():
    API_URL = 'https://crocodoc.com/api/v1/' #need trailing slash
    API_TOKEN = ''

    def __init__(self, api_token):
        self.API_TOKEN = api_token
        self.conn = Bolacha()

    def _merge_params(self, params, whitelist):
        pass
    
    @process_json
    def upload(self, url_or_file, **options):
        '''
        Upload and convert a file referenced by URL or uploaded via a POST
        request.
        '''
        options['token'] = self.API_TOKEN

        #Check if we have a url or file
        if multipart.is_file(url_or_file):
            options['file'] = url_or_file
            return self.conn.post(
                    urlparse.urljoin(self.API_URL, 'document/upload'), 
                    body = options)

        #Otherwise, we just have a url
        options['url'] = url_or_file
        return self.conn.get(
                urlparse.urljoin(self.API_URL, 'document/upload'), 
                body = options
                )
        
    @process_json
    def status(self, uuids):
        '''
        Given a single or list of uuids, checks the conversion status of the
        document(s).
        '''
        is_single = False #flag to indicate if we have a single uuid
        options = { 'token': self.API_TOKEN }

        #If we have a string, make it into a list
        if isinstance(uuids, types.StringTypes):
            uuids = [uuids, ]
            is_single = True

        #Convert our list of uuids into a comma-deliminated list of uuids
        options['uuids'] = ','.join(uuids)

        headers, content = self.conn.get(
                urlparse.urljoin(self.API_URL, 'document/status'), 
                body = options
                )
        #If only a single uuid was queried, then modify the response content
        #by removing the '[' and ']' that denote a list.
        if is_single:
            return headers, content[1:-1]
        return headers, content


    @process_json
    def delete(self, uuid):
        '''Given a single uuid, deletes the uploaded file.'''
        options = { 
                'token': self.API_TOKEN,
                'uuid': uuid
                }

        return self.conn.get(
                urlparse.urljoin(self.API_URL, 'document/delete'), 
                body = options
                )


    # TODO: Currently not implemented because raw file should be saved to
    #       filesystem.
    def download(self, uuid, **options):
        options['token'] = self.API_TOKEN
        raise NotImplementedError

    # TODO: Since we know share will only return a shortID, we should just pull
    #       that out and return the shortID value directly.
    @process_json
    def share(self, uuid, **options):
        '''
        Given a uuid, creates a new "short ID" that can be used to share a 
        document.
        '''
        options['token'] = self.API_TOKEN
        options['uuid'] = uuid

        return self.conn.get(
                urlparse.urljoin(self.API_URL, 'document/share'), 
                body = options
                )

    # TODO: Since we know this will only return a sessionId, we should just pull
    #       that out and return the sessionId value directly.
    @process_json
    def get_session(self, uuid, **options):
        '''
        Given a uuid, creates a session ID for session-based document viewing.
        Each session ID may only be used once.
        '''
        options['token'] = self.API_TOKEN
        options['uuid'] = uuid

        return self.conn.get(
                urlparse.urljoin(self.API_URL, 'session/get'), 
                body = options
                )

    # Helper methods (static):

    @staticmethod
    def embeddable_viewer_url(shortId):
        '''Given a shortId, returns the embeddable URL.'''
        return 'http://crocodoc.com/%s?embedded=true' % shortId

    @staticmethod
    def session_based_viewer_url(sessionId):
        '''Given a sessionId, returns the session-based viewing URL.'''
        return 'https://crocodoc.com/view/?sessionId=%s' % sessionId
示例#18
0
def test_get():
    b = Bolacha()
    head, body = b.request('http://localhost:5050/', 'GET')
    assert_equals(body, 'You are not authenticated!')
示例#19
0
 def __init__(self, api_token):
     self.API_TOKEN = api_token
     self.conn = Bolacha()