def retrieve_user(token, astakos_auth_url, logger=None, client_ip=None): """Return user_info retrieved from astakos for the given token""" astakos_url = astakos_auth_url if astakos_url is None: try: astakos_url = settings.ASTAKOS_AUTH_URL except AttributeError: if logger: logger.error("Cannot authenticate without having" " an Astakos Authentication URL") raise if not token: return None headers = None if client_ip: headers = {'X-Client-IP': client_ip} astakos = AstakosClient(token, astakos_url, use_pool=True, retry=2, logger=logger, headers=headers) user_info = astakos.authenticate() return user_info
def get(cls): if cls._object is None: cls._object = AstakosClient(ASTAKOS_BASE_URL, use_pool=True, retry=3, logger=log) return cls._object
def get_user(request, astakos_url, fallback_token=None, usage=False, logger=None): request.user = None request.user_uniq = None client = AstakosClient(astakos_url, retry=2, use_pool=True, logger=logger) # Try to find token in a parameter or in a request header. user = user_for_token(client, request.GET.get('X-Auth-Token'), usage=usage) if not user: user = user_for_token(client, request.META.get('HTTP_X_AUTH_TOKEN'), usage=usage) if not user: user = user_for_token(client, fallback_token, usage=usage) if not user: return None # use user uuid, instead of email, keep email/displayname reference # to user_id request.user_uniq = user['uuid'] request.user = user request.user_id = user.get('displayname') return user
def _auth_user(self, pool): global token, endpoints_with_info, auth_url try: client = AstakosClient(token['id'], auth_url, use_pool=pool) auth_info = client.authenticate() except Exception as err: self.fail("Shouldn't raise an Exception: %s" % err) self.assertEqual(endpoints_with_info, auth_info)
def test_reject_commission(self): """Test function call of reject_commission""" global token, auth_url try: client = AstakosClient(token['id'], auth_url) client.reject_commission(57) except Exception as err: self.fail("Shouldn't raise Exception %s" % err)
def _auth_user(self, token, user_info, usage, pool): _mock_request([_request_ok]) try: client = AstakosClient("https://example.com", use_pool=pool) auth_info = client.get_user_info(token, usage=usage) except: self.fail("Shouldn't raise an Exception") self.assertEqual(user_info, auth_info)
def test_reject_commission(self): """Test function call of reject_commission""" global token_1 _mock_request([_request_ok]) try: client = AstakosClient("https://example.com") client.reject_commission(token_1, 57) except Exception as err: self.fail("Shouldn't raise Exception %s" % err)
def retrieve_uuids(token, displaynames, return_dict=False, fail_silently=True): astakos = AstakosClient(token, ASTAKOS_AUTH_URL, retry=2, use_pool=True, logger=logger) catalog = astakos.get_uuids(displaynames) or {} missing = list(set(displaynames) - set(catalog)) if missing and not fail_silently: raise ItemNotExists('Unknown uuids: %s' % ', '.join(missing)) return catalog if return_dict else [catalog.get(i) for i in displaynames]
def test_resolve_commissions(self): """Test function call of resolve_commissions""" global token, auth_url try: client = AstakosClient(token['id'], auth_url) result = client.resolve_commissions([56, 57], [56, 58, 59]) except Exception as err: self.fail("Shouldn't raise Exception %s" % err) self.assertEqual(result, resolve_commissions_rep)
def test_username(self): """Test get_username""" global token, user, auth_url try: client = AstakosClient(token['id'], auth_url, use_pool=False, retry=2) info = client.get_username(user['id']) except Exception, e: self.fail("Shouldn't raise an Exception: %s" % e)
def test_get_resources(self): """Test function call of get_resources""" global resources, auth_url, token try: client = AstakosClient(token['id'], auth_url, retry=1) result = client.get_resources() except Exception as err: self.fail("Shouldn't raise Exception %s" % err) self.assertEqual(resources, result)
def test_get_quotas(self): """Test function call of get_quotas""" global quotas, token, auth_url try: client = AstakosClient(token['id'], auth_url) result = client.get_quotas() except Exception as err: self.fail("Shouldn't raise Exception %s" % err) self.assertEqual(quotas, result)
def _invalid_url(self, pool): global token, auth_url try: client = AstakosClient(token['id'], auth_url, use_pool=pool) client._call_astakos("/astakos/api/misspelled") except NotFound: pass except Exception, e: self.fail("Got \"%s\" instead of 404" % e)
def test_get_pending_commissions(self): """Test function call of get_pending_commissions""" global token, pending_commissions, auth_url try: client = AstakosClient(token['id'], auth_url) response = client.get_pending_commissions() except Exception as err: self.fail("Shouldn't raise Exception %s" % err) self.assertEqual(response, pending_commissions)
def test_get_uuid(self): """Test get_uuid""" global token, user, auth_url try: client = AstakosClient(token['id'], auth_url, retry=1) catalog = client.get_uuids([user['name']]) except: self.fail("Shouldn't raise an Exception") self.assertEqual(catalog[user['name']], user['id'])
def test_get_endpoints(self): """Test function call of get_endpoints""" global token_1, user_info_endpoints _mock_request([_request_ok]) try: client = AstakosClient("https://example.com") response = client.get_endpoints(token_1) except Exception as err: self.fail("Shouldn't raise Exception %s" % err) self.assertEqual(response, user_info_endpoints)
def test_issue_commission(self): """Test function call of issue_commission""" global token_1, commission_request, commission_successful_reqsponse _mock_request([_request_ok]) try: client = AstakosClient("https://example.com") response = client.issue_commission(token_1, commission_request) except Exception as err: self.fail("Shouldn't raise Exception %s" % err) self.assertEqual(response, commission_successful_response['serial'])
def test_get_commission_info(self): """Test function call of get_commission_info""" global token, commission_description, auth_url try: client = AstakosClient(token['id'], auth_url, use_pool=True, pool_size=2) response = client.get_commission_info(57) except Exception as err: self.fail("Shouldn't raise Exception %s" % err) self.assertEqual(response, commission_description)
def test_get_resources(self): """Test function call of get_resources""" global resources _mock_request([_request_offline, _request_ok]) try: client = AstakosClient("https://example.com", retry=1) result = client.get_resources() except Exception as err: self.fail("Shouldn't raise Exception %s" % err) self.assertEqual(resources, result)
def test_get_uuid_user_two(self): """Test get_uuid for User Two""" global token_1, user_2 _mock_request([_request_offline, _request_ok]) try: client = AstakosClient("https://example.com", retry=1) info = client.get_uuid(token_2, user_1['username']) except: self.fail("Shouldn't raise an Exception") self.assertEqual(info, user_1['uuid'])
def test_offline_retry(self): """Test retry functionality for getUserInfo""" global token_1, user_1 _mock_request([_request_offline, _request_offline, _request_ok]) try: client = AstakosClient("https://example.com", retry=2) auth_info = client.get_user_info(token_1, usage=True) except: self.fail("Shouldn't raise an Exception") self.assertEqual(user_1, auth_info)
def test_resolve_commissions(self): """Test function call of resolve_commissions""" global token_1 _mock_request([_request_ok]) try: client = AstakosClient("https://example.com") result = client.resolve_commissions(token_1, [56, 57], [56, 58, 59]) except Exception as err: self.fail("Shouldn't raise Exception %s" % err) self.assertEqual(result, resolve_commissions_rep)
def test_get_quotas(self): """Test function call of get_quotas""" global quotas, token_1 _mock_request([_request_ok]) try: client = AstakosClient("https://example.com") result = client.get_quotas(token_1) except Exception as err: self.fail("Shouldn't raise Exception %s" % err) self.assertEqual(quotas, result)
def get_pithos_usage(token): """Get Pithos Usage from astakos.""" astakos = AstakosClient(ASTAKOS_BASE_URL, retry=2, use_pool=True, logger=logger) quotas = astakos.get_quotas(token)['system'] pithos_resources = [r['name'] for r in resources] map(quotas.pop, filter(lambda k: k not in pithos_resources, quotas.keys())) return quotas.popitem()[-1] # assume only one resource
def _offline(self, pool): global token_1 _mock_request([_request_offline]) try: client = AstakosClient("https://example.com", use_pool=pool) client._call_astakos(token_1, astakosclient.API_AUTHENTICATE) except AstakosClientException: pass else: self.fail("Should have raised AstakosClientException")
def test_issue_commission(self): """Test function call of issue_commission""" global token, commission_request, commission_successful_reqsponse global auth_url try: client = AstakosClient(token['id'], auth_url) response = client._issue_commission(commission_request) except Exception as err: self.fail("Shouldn't raise Exception %s" % err) self.assertEqual(response, commission_successful_response['serial'])
def _get_authenticate(self, pool): global token, auth_url try: client = AstakosClient(token['id'], auth_url, use_pool=pool) client._call_astakos(api_tokens, method="GET") except BadRequest: pass except Exception: self.fail("Should have returned 400 (Method not allowed)") else: self.fail("Should have returned 400 (Method not allowed)")
def _get_user_catalogs(self, pool): global token, auth_url, api_usercatalogs try: client = AstakosClient(token['id'], auth_url, use_pool=pool) client._call_astakos(api_usercatalogs) except BadRequest: pass except Exception: self.fail("Should have returned 400 (Method not allowed)") else: self.fail("Should have returned 400 (Method not allowed)")
def test_no_username(self): global token, auth_url try: client = AstakosClient(token['id'], auth_url) client.get_username("1234") except NoUserName: pass except: self.fail("Should have raised NoDisplayName exception") else: self.fail("Should have raised NoDisplayName exception")
def test_no_uuid(self): global token, auth_url try: client = AstakosClient(token['id'], auth_url) client.get_uuid("1234") except NoUUID: pass except: self.fail("Should have raised NoUUID exception") else: self.fail("Should have raised NoUUID exception")