Consider the following code that transfers a file from AWS S3 to Box using the boxsdk Client:
from boxsdk import JWTAuth, Clientimport boto3import jsonimport osimport tempfileclass BoxTransfer: def __init__(self, bucket_name=None, file_name=None, folders=None) -> None: self.bucket_name = bucket_name self.file_name = file_name self.folders = folders or {} json_data = json.loads(os.environ['BOX_AUTHENTICATION']) auth = JWTAuth( client_id=json_data['boxAppSettings']['clientID'], client_secret=json_data['boxAppSettings']['clientSecret'], enterprise_id=json_data['enterpriseID'], jwt_key_id=json_data['boxAppSettings']['appAuth']['publicKeyID'], rsa_private_key_data=json_data['boxAppSettings']['appAuth']['privateKey'], rsa_private_key_passphrase=json_data['boxAppSettings']['appAuth']['passphrase'] ) self.box_client = Client(auth) self.folder = None def get_user_id(self): my_user = self.box_client.user().get() def upload_file(self): self.get_user_id() with tempfile.NamedTemporaryFile() as tmp: s3 = boto3.client('s3') s3.download_fileobj(self.bucket_name, self.file_name, tmp) tmp.seek(0) s3_folder_name = self.file_name.split("/")[0] s3_file_name = self.file_name.split("/")[-1] s3_box_folder = self.folders[s3_folder_name]['box_folder'] self.folder = self.box_client.folder(s3_box_folder) self.folder.upload(file_path=tmp.name, file_name=s3_file_name)
How would I write a unit test (in pytest) to test?
self.box_client = Client(auth)
The issue is the auth part. I've tried mocking the Oauth2 token:
@patch('os.environ', {'BOX_AUTHENTICATION': '{"boxAppSettings": {"clientID": "id", "clientSecret": "secret", "appAuth": {"publicKeyID": "key_id", "privateKey": "private_key", "passphrase": "passphrase"}}, "enterpriseID": "enterprise_id"}'}) @patch('boxsdk.Client') def setUp(self, mock_box_client): self.bucket_name = 'test_bucket' self.file_name = 'test_file' self.folders = {'test_folder': {'box_folder': 'box_folder_id'}} self.box_transfer = BoxTransfer(self.bucket_name, self.file_name, self.folders) self.mock_box_client = mock_box_client
But, it errors with the message:
('Could not deserialize key data. The data may be in an incorrectformat, it may be encrypted with an unsupported algorithm, or it maybe an unsupported key type (e.g. EC curves with explicitparameters).', [<OpenSSLError(code=503841036, lib=60, reason=524556,reason_text=unsupported)>])