def create_remote_user(user_from, project, participant): headers = {'Authorization': 'Bearer ' + _build_jwt(user_from)} url = URL_CREATE_USER.format(settings.EXOLEVER_HOST) if settings.POPULATOR_MODE and not settings.TEST_MODE: return None logger.info('SyncParticipant: {}-{}'.format(participant.name, participant.email)) data = { 'short_name': participant.name, 'full_name': participant.name, 'email': participant.email, 'uuid': participant.user.uuid.__str__(), 'is_superuser': False, 'is_staff': False, 'is_active': True, 'password': project.settings.launch['default_password'] } try: response = requests.post(url, json=data, headers=headers) assert response.status_code == requests.codes.created return response.json() except Exception as err: message = 'Exception: {}-{}'.format(err, url) logger.error(message)
def test_create_request_success(self, mock_request, mock_email): # PREPARE DATA self.init_mock(mock_request) token = _build_jwt(self.user) self.client.credentials(HTTP_AUTHORIZATION='Bearer ' + token) data = { 'concept': faker.sentence(), 'amount': Decimal(10.2).quantize(Decimal('.01'), rounding=ROUND_DOWN), 'currency': settings.PAYMENTS_CH_USD, 'email': faker.email(), 'full_name': faker.name(), } secret_key = settings.PAYMENT_SECRET_KEY dig = hmac.new( secret_key.encode(), msg=str(data).encode(), digestmod=hashlib.sha256).digest() data['token'] = base64.b64encode(dig).decode() data['url'] = faker.uri() url = reverse('api:do-request') # DO ACTION response = self.client.post(url, data=data) # ASSERTS self.assertTrue(status.is_success(response.status_code)) self.assertTrue(mock_email.called) self.assertEqual(models.Payment.objects.count(), 1) payment = models.Payment.objects.first() self.assertEqual( payment.url_notification, data['url']) self.assertEqual( payment.created_by, self.user)
def create_internal_messages(self, resources, level): logger = logging.getLogger('library') for key, value in resources.items(): try: user = get_user_model().objects.get(pk=key) except ObjectDoesNotExist: continue token = _build_jwt(user) header = 'Bearer ' + token code = settings.RESOURCE_CH_CODE_MESSAGE_UPLOAD num_videos_ready = value variables = {'counter': num_videos_ready} data = { 'user': user.uuid, 'code': code, 'level': level, 'can_be_closed': True, 'read_when_login': True, 'variables': variables } url = '{}/api/internal-messages/'.format(settings.EXOLEVER_HOST) try: response = requests.post(url, json=data, headers={'Authorization': header}) if not response.ok: logger.error('Wrong response {} - {}'.format( response.status_code, response.content)) except exceptions.RequestException as e: logger.error('Exception during call {}'.format(e))
def get_config_param_remote(user_from): headers = { 'Authorization': '{} {} '.format( api_settings.JWT_AUTH_HEADER_PREFIX, _build_jwt(user_from))} url = URL_CONFIG_PARAMS.format(user_from.uuid.__str__()) try: response = requests.get( settings.EXOLEVER_HOST + url, headers=headers) assert response.status_code == requests.codes.ok return response.json() except Exception as e: logger.error(e) logger.error(settings.EXOLEVER_HOST + url)
def run(self, *args, **kwargs): uuid = kwargs.get('uuid') user_from = kwargs.get('user_from') token = _build_jwt(get_user_model().objects.get(pk=user_from)) header = 'Bearer ' + token server_url = self.get_url_for_deleting(uuid) if not server_url: logger.error('ExO Website not settings properly') raise Exception('Wrong website service settings') if settings.POPULATOR_MODE: return None try: requests.delete(server_url, headers={'Authorization': header}) except Exception as err: message = 'Exception: {}-{}'.format(err, server_url) logger.error(message)
def get_unread_messages(opp_uuids, user_from): token = _build_jwt(user_from) host = settings.EXOLEVER_HOST + settings.SERVICE_CONVERSATIONS_HOST url = host + 'api/total/' headers = {'Authorization': 'Bearer ' + token} data = { 'uuids': opp_uuids, } if not settings.POPULATOR_MODE: try: response = requests.post(url, data=data, headers=headers) return response.json() except Exception as err: message = 'requests.Exception: {}'.format(err) logger.error(message) return []
def get_unread_messages(self, user_from): num_messages = None token = _build_jwt(user_from) host = settings.EXOLEVER_HOST + settings.SERVICE_CONVERSATIONS_HOST url = host + 'api/{}/conversations/total/'.format(self.uuid.__str__()) headers = {'Authorization': 'Bearer ' + token} if not settings.POPULATOR_MODE: try: response = requests.get(url, headers=headers) num_messages = response.json() except Exception as err: message = 'requests.Exception: {}'.format(err) logger.error(message) return num_messages
def test_create_request_error(self, mock_request): # PREPARE DATA self.init_mock(mock_request) token = _build_jwt(self.user) self.client.credentials(HTTP_AUTHORIZATION='Bearer ' + token) data = { 'concept': faker.sentence(), 'amount': Decimal(10.2).quantize(Decimal('.01'), rounding=ROUND_DOWN), 'currency': settings.PAYMENTS_CH_USD, 'email': faker.email(), 'full_name': faker.name(), 'url': faker.uri(), 'token': 'aaaa', } url = reverse('api:do-request') # DO ACTION response = self.client.post(url, json=data) # ASSERTS self.assertTrue(status.is_client_error(response.status_code))
def run(self, *args, **kwargs): uuid = kwargs.get('uuid') slug = kwargs.get('slug') user_from = kwargs.get('user_from') token = _build_jwt(get_user_model().objects.get(pk=user_from)) header = 'Bearer ' + token data = {'uuid': uuid, 'slug': slug, 'page_type': 'event'} server_url = self.get_url_for_creating() if not server_url: logger.error('ExO Website not settings properly') raise Exception('Wrong website service settings') if settings.POPULATOR_MODE: return None try: requests.post(server_url, data=data, headers={'Authorization': header}) except Exception as err: message = 'Exception: {}-{}'.format(err, server_url) logger.error(message)
def do_login(self, user): token = _build_jwt(user) self.client.credentials(HTTP_AUTHORIZATION='Bearer ' + token)
def _get_headers(user_from): return { 'Authorization': '{} {}'.format(api_settings.JWT_AUTH_HEADER_PREFIX, _build_jwt(user_from)) }
def _get_authorization(self, user_from): token = _build_jwt(user_from) return {'Authorization': 'Bearer ' + token}