1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
######################################################################
#
# File: test/unit/v1/test_session.py
#
# Copyright 2019 Backblaze Inc. All Rights Reserved.
#
# License https://www.backblaze.com/using_b2_code.html
#
######################################################################
from __future__ import annotations
import unittest.mock as mock
from ..test_base import TestBase
from .deps import ALL_CAPABILITIES, B2Session
from .deps_exception import InvalidAuthToken, Unauthorized
class TestB2Session(TestBase):
def setUp(self):
self.account_info = mock.MagicMock()
self.account_info.get_account_auth_token.return_value = 'auth_token'
self.api = mock.MagicMock()
self.api.account_info = self.account_info
self.raw_api = mock.MagicMock()
self.raw_api.get_file_info_by_id.__name__ = 'get_file_info_by_id'
self.raw_api.get_file_info_by_id.side_effect = ['ok']
self.session = B2Session(self.account_info, raw_api=self.raw_api)
def test_works_first_time(self):
self.assertEqual('ok', self.session.get_file_info_by_id(None))
def test_works_second_time(self):
self.raw_api.get_file_info_by_id.side_effect = [
InvalidAuthToken('message', 'code'),
'ok',
]
self.assertEqual('ok', self.session.get_file_info_by_id(None))
def test_fails_second_time(self):
self.raw_api.get_file_info_by_id.side_effect = [
InvalidAuthToken('message', 'code'),
InvalidAuthToken('message', 'code'),
]
with self.assertRaises(InvalidAuthToken):
self.session.get_file_info_by_id(None)
def test_app_key_info_no_info(self):
self.account_info.get_allowed.return_value = dict(
bucketId=None,
bucketName=None,
capabilities=ALL_CAPABILITIES,
namePrefix=None,
)
self.raw_api.get_file_info_by_id.side_effect = Unauthorized('no_go', 'code')
with self.assertRaisesRegex(
Unauthorized, r'no_go for application key with no restrictions \(code\)'
):
self.session.get_file_info_by_id(None)
def test_app_key_info_no_info_no_message(self):
self.account_info.get_allowed.return_value = dict(
bucketId=None,
bucketName=None,
capabilities=ALL_CAPABILITIES,
namePrefix=None,
)
self.raw_api.get_file_info_by_id.side_effect = Unauthorized('', 'code')
with self.assertRaisesRegex(
Unauthorized, r'unauthorized for application key with no restrictions \(code\)'
):
self.session.get_file_info_by_id(None)
def test_app_key_info_all_info(self):
self.account_info.get_allowed.return_value = dict(
bucketId='123456',
bucketName='my-bucket',
capabilities=['readFiles'],
namePrefix='prefix/',
)
self.raw_api.get_file_info_by_id.side_effect = Unauthorized('no_go', 'code')
with self.assertRaisesRegex(
Unauthorized,
r"no_go for application key with capabilities 'readFiles', restricted to bucket 'my-bucket', restricted to files that start with 'prefix/' \(code\)",
):
self.session.get_file_info_by_id(None)
|