text: # Copyright © 2020 Province of British Columbia
  #
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  #     http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  
  """Tests to assure the RegistrationBootstrap Service.
  
  Test-Suite to ensure that the RegistrationBootstrap Service is working as expected.
  """
  import json
  import random
  import uuid
  from http import HTTPStatus
  
  import pytest
  import requests
  from flask import current_app
  
  from legal_api.services import RegistrationBootstrapService
  from legal_api.services.bootstrap import AccountService
  from tests import integration_affiliation
  
  
  @pytest.fixture(scope='function')
  def account(app):
      """Create an account to be used for testing."""
      with app.app_context():
          account_url = current_app.config.get('ACCOUNT_SVC_AFFILIATE_URL')
          account_url = account_url[:account_url.rfind('{') - 1]
  
          org_data = json.dumps({'name': str(uuid.uuid4())})
          token = AccountService.get_bearer_token()
  
          # with app.app_context():
          rv = requests.post(
              url=account_url,
              data=org_data,
              headers={**AccountService.CONTENT_TYPE_JSON,
                       'Authorization': AccountService.BEARER + token},
              timeout=20
          )
  
          account_id = rv.json()['id']
  
          yield account_id
  
          rv = requests.delete(url=f'{account_url}/{account_id}',
                               headers={'Authorization': AccountService.BEARER + token},
                               timeout=20
                               )
          print(rv)
  
  
  def test_create_bootstrap_registrations(session):
      """Assert the service creates registrations."""
      r = RegistrationBootstrapService.create_bootstrap(account=28)
      assert r.identifier
  
  
  @integration_affiliation
  def test_account_affiliation_integration(account, app_ctx):
      """Assert that the affiliation can be created."""
      business_registration = (f'T{random.SystemRandom().getrandbits(0x58)}')[:10]
      r = AccountService.create_affiliation(account=account,
                                            business_registration=business_registration,
                                            business_name='')
  
      assert r == HTTPStatus.OK
  
      r = AccountService.delete_affiliation(account=account,
                                            business_registration=business_registration)
  
      # @TODO change this next sprint when affiliation service is updated.
      assert r == HTTPStatus.OK
  