class TestAuth(unittest.TestCase): def setUp(self): self.endpoint = Mock() self.username = Mock() self.api_key = Mock() self.auth_token = Mock() self.auth = QueueAuth(self.endpoint, self.username, self.api_key, auth_token=self.auth_token) def test_init(self): self.auth.endpoint = self.endpoint self.auth.username = self.username self.auth.api_key = self.api_key self.auth.auth_token = self.auth_token @patch("requests.post") def test_auth(self, res): self.auth.auth() res.assert_called_once_with(self.endpoint, headers={"X-Auth-User": self.username, "X-Auth-Key": self.api_key}) self.assertNotEqual(self.auth_token, self.auth.auth_token, "auth token was set") @patch("requests.post") def test_auth_raises_error(self, res): response = Mock() response.ok = False res.return_value = response self.assertRaises(Unauthenticated, self.auth.auth) def test_auth_handle_error_resend_on_503(self): m = Mock() m.status_code = 503 self.auth.handle_error(m) m.connection.send.assert_called_once_with(m.request) def test_auth_handle_error_reauth_on_401(self): self.auth.auth = Mock() m = Mock() m.status_code = 401 m.request.headers = {} self.auth.handle_error(m) self.auth.auth.assert_called_once() m.connection.send.assert_called_once_with(m.request) def test_call(self): request = Mock() request.headers = {} self.auth(request) self.assertEqual(request.headers, {"X-Auth-Token": self.auth_token}) def test_call_unauthed(self): self.auth.auth = Mock() request = Mock() request.headers = {} self.auth.auth_token = None self.auth(request) self.auth.auth.assert_called_once_with() self.assertEqual(request.headers, {"X-Auth-Token": None})
class QueueClient(softlayer_messaging.resource.Resource): def __init__(self, endpoint, account, **kwargs): self.account = account self.endpoint = endpoint self.auth = None super(QueueClient, self).__init__( '/'.join((endpoint, 'v1', account)), **kwargs) self.headers['Content-Type'] = 'application/json' def authenticate(self, username, api_key, auth_token=None): auth_endpoint = '/'.join( (self.endpoint, 'v1', self.account, 'auth')) self.auth = QueueAuth(auth_endpoint, username, api_key, auth_token=auth_token) self.auth.auth() def ping(self): try: r = requests.get('%s/v1/ping' % self.endpoint) except: return False return 200 <= r.status_code <= 299 def request(self, method, *args, **kwargs): kwargs['auth'] = self.auth resp = super(QueueClient, self).request(method, *args, **kwargs) resp.raise_for_status() return resp def queue(self, name): return softlayer_messaging.queue.Queue(self.url, name, self.auth) def topic(self, name): return softlayer_messaging.topic.Topic(self.url, name, self.auth) def queues(self, tags=None): params = {} if tags: params['tags'] = ','.join(tags) r = self.get('queues', params=params) return r.json def topics(self, tags=None): params = {} if tags: params['tags'] = ','.join(tags) r = self.get('topics', params=params) return r.json def create_queue(self, name, **kwargs): queue = {} queue.update(kwargs) data = json.dumps(queue) r = self.put("queues/%s" % (name, ), data=data) return r.json def create_topic(self, name, **kwargs): topic = {} topic.update(kwargs) data = json.dumps(topic) r = self.put("topics/%s" % (name, ), data=data) return r.json def stats(self, period='hour'): r = self.get("stats/%s" % (period, )) return r.json def __repr__(self): return '<QueueClient [%s]>' % (self.url)