Beispiel #1
0
 def test_500(self):
     api = JiveApi('http://jive.example.com/', 'jiveuser', 'jivepass')
     req_t = namedtuple('MockRequest', ['method', 'url'])
     req = req_t(method='PUT', url='http://jive.example.com/')
     with patch('%s._put_json' % pb) as mock_put:
         mock_put.side_effect = RequestFailedException(
             MockResponse(500, 'Conflict', request=req))
         with pytest.raises(RequestFailedException):
             api.update_content('cid', {'foo': 'bar'})
Beispiel #2
0
 def test_success(self):
     api = JiveApi('http://jive.example.com/', 'jiveuser', 'jivepass')
     mock_sess = MagicMock(spec_set=Session)
     api._requests = mock_sess
     mock_sess.get.return_value = MockResponse(200, 'OK', content=b'1234')
     res = api.get_image('imgid')
     assert res == b'1234'
     assert mock_sess.mock_calls == [
         call.get('http://jive.example.com/core/v3/images/imgid')
     ]
Beispiel #3
0
 def test_success(self):
     api = JiveApi('http://jive.example.com/', 'jiveuser', 'jivepass')
     mock_sess = MagicMock(spec_set=Session)
     api._requests = mock_sess
     mock_sess.post.return_value = MockResponse(
         201,
         'Created',
         _json={'foo': 'bar'},
         headers={'Location': 'http://some.location/'})
     res = api.upload_image(b'1234', 'img.jpg', 'image/jpeg')
     assert res == ('http://some.location/', {'foo': 'bar'})
     assert mock_sess.mock_calls == [
         call.post('http://jive.example.com/core/v3/images',
                   files={'file': ('img.jpg', b'1234', 'image/jpeg')},
                   allow_redirects=False)
     ]
Beispiel #4
0
 def test_error(self):
     api = JiveApi('http://jive.example.com/', 'jiveuser', 'jivepass')
     mock_sess = MagicMock(spec_set=Session)
     api._requests = mock_sess
     req_t = namedtuple('MockRequest', ['method', 'url'])
     req = req_t(method='GET',
                 url='http://jive.example.com/core/v3/images/imgid')
     mock_sess.get.return_value = MockResponse(404,
                                               'Not Found',
                                               content=b'1234',
                                               request=req)
     with pytest.raises(RequestFailedException):
         api.get_image('imgid')
     assert mock_sess.mock_calls == [
         call.get('http://jive.example.com/core/v3/images/imgid')
     ]
Beispiel #5
0
def api_and_betamax(request, jive_domain, jive_host):
    conf = betamax.Betamax.configure()
    conf.default_cassette_options['serialize_with'] = 'prettyjson'
    conf.cassette_library_dir = cassette_dir
    betamax.Betamax.register_serializer(pretty_json.PrettyJSONSerializer)
    # keep creds secret
    conf.define_cassette_placeholder(
        '<JIVE-AUTH>',
        base64.b64encode('{0}:{1}'.format(
            os.environ['JIVE_USER'],
            os.environ['JIVE_PASS']).encode('utf-8')).decode())
    conf.define_cassette_placeholder('<JIVE-USER>', os.environ['JIVE_USER'])
    conf.define_cassette_placeholder('<JIVE-PASS>', os.environ['JIVE_PASS'])
    conf.define_cassette_placeholder('<JIVE-HOST>', jive_host)
    conf.define_cassette_placeholder('<JIVE-DOMAIN>', jive_domain)
    conf.before_record(callback=betamax_sanitizer)
    if request.config.getoption('--record'):
        conf.default_cassette_options['record_mode'] = 'once'
    else:
        conf.default_cassette_options['record_mode'] = 'none'
    cass_name = betamax.fixtures.pytest._casette_name(request,
                                                      parametrized=True)
    api = JiveApi(os.environ['JIVE_URL'], os.environ['JIVE_USER'],
                  os.environ['JIVE_PASS'])
    # Workaround for https://github.com/betamaxpy/betamax/issues/124
    # where secrets aren't stripped from gzipped responses. Fix is to tell the
    # server that we can't accept gzip.
    api._requests.headers.update({'Accept-Encoding': 'identity'})
    bmax = betamax.Betamax(api._requests)
    bmax.use_cassette(cass_name)
    bmax.start()
    request.addfinalizer(bmax.stop)
    return api, bmax
Beispiel #6
0
class TestRequestMethods(object):
    def setup(self):
        self.api = JiveApi('http://jive.example.com/', 'jiveuser', 'jivepass')
        self.mock_sess = MagicMock(spec_set=Session)
        self.api._requests = self.mock_sess

    def test_get_http_url(self):
        self.mock_sess.get.side_effect = [
            MockResponse(200,
                         'OK',
                         _json={
                             'links': {
                                 'next': 'http://jive.example.com/bar'
                             },
                             'list': ['one', 'two']
                         }),
            MockResponse(200, 'OK', _json={'list': ['three']})
        ]
        res = self.api._get('http://jive.example.com/foo', autopaginate=True)
        assert res == ['one', 'two', 'three']
        assert self.mock_sess.mock_calls == [
            call.get('http://jive.example.com/foo'),
            call.get('http://jive.example.com/bar')
        ]

    def test_post_json(self):
        self.mock_sess.post.side_effect = [
            MockResponse(201, 'Created', _json={'list': ['three']})
        ]
        res = self.api._post_json('http://jive.example.com/foo',
                                  {'foo': 'bar'})
        assert res == {'list': ['three']}
        assert self.mock_sess.mock_calls == [
            call.post('http://jive.example.com/foo', json={'foo': 'bar'})
        ]

    def test_put_json(self):
        self.mock_sess.put.side_effect = [
            MockResponse(201, 'Created', _json={'list': ['three']})
        ]
        res = self.api._put_json('http://jive.example.com/foo', {'foo': 'bar'})
        assert res == {'list': ['three']}
        assert self.mock_sess.mock_calls == [
            call.put('http://jive.example.com/foo', json={'foo': 'bar'})
        ]
Beispiel #7
0
 def test_update_date(self):
     api = JiveApi('http://jive.example.com/', 'jiveuser', 'jivepass')
     tz = FixedOffset(-60, 'foo')
     with patch('%s._put_json' % pb, autospec=True) as mock_put:
         mock_put.return_value = {'return': 'value'}
         res = api.update_content('cid', {'foo': 'bar'},
                                  update_date=datetime(2018,
                                                       2,
                                                       13,
                                                       11,
                                                       23,
                                                       52,
                                                       tzinfo=tz))
     assert res == {'return': 'value'}
     assert mock_put.mock_calls == [
         call(
             api, 'core/v3/contents/cid'
             '?updated=2018-02-13T11%3A23%3A52.000-0100', {'foo': 'bar'})
     ]
Beispiel #8
0
 def test_no_trailing_slash(self):
     mock_sess = MagicMock()
     type(mock_sess).hooks = {'response': []}
     with patch('jiveapi.api.requests') as mock_req:
         mock_req.Session.return_value = mock_sess
         cls = JiveApi('http://jive.example.com', 'uname', 'passwd')
     assert cls._base_url == 'http://jive.example.com/'
     assert cls._username == 'uname'
     assert cls._password == 'passwd'
     assert cls._requests == mock_sess
     assert mock_sess.hooks['response'] == [requests_hook]
Beispiel #9
0
 def test_error(self):
     api = JiveApi('http://jive.example.com/', 'jiveuser', 'jivepass')
     mock_sess = MagicMock(spec_set=Session)
     api._requests = mock_sess
     req_t = namedtuple('MockRequest', ['method', 'url'])
     req = req_t(method='POST',
                 url='http://jive.example.com/core/v3/images')
     mock_sess.post.return_value = MockResponse(
         400,
         'Image is too large.',
         _json={'foo': 'bar'},
         headers={'Location': 'http://some.location/'},
         request=req)
     with pytest.raises(RequestFailedException):
         api.upload_image(b'1234', 'img.jpg', 'image/jpeg')
     assert mock_sess.mock_calls == [
         call.post('http://jive.example.com/core/v3/images',
                   files={'file': ('img.jpg', b'1234', 'image/jpeg')},
                   allow_redirects=False)
     ]
Beispiel #10
0
 def test_abs_url(self):
     api = JiveApi('http://jive.example.com/api/', 'jiveuser', 'jivepass')
     assert api.abs_url('foo/bar') == 'http://jive.example.com/api/foo/bar'
Beispiel #11
0
 def setup(self):
     self.api = JiveApi('http://jive.example.com/', 'jiveuser', 'jivepass')
     self.mock_sess = MagicMock(spec_set=Session)
     self.api._requests = self.mock_sess