def test_query_with_owner_users(
    mock_box_session,
    test_search,
    make_mock_box_request,
    search_query,
    search_response,
    search_entries,
):
    # pylint:disable=redefined-outer-name
    user1 = User(mock_box_session, '987')
    user2 = User(mock_box_session, '654')
    mock_box_session.get.return_value, _ = make_mock_box_request(
        response=search_response)
    response = test_search.query(
        search_query,
        owner_users=[user1, user2],
    )
    for actual, expected in zip(
            response,
        [File(mock_box_session, item['id'], item) for item in search_entries]):
        assert actual == expected

    mock_box_session.get.assert_called_once_with(
        test_search.get_url(),
        params=Matcher(compare_params, {
            'query': search_query,
            'owner_user_ids': '987,654'
        }))
Example #2
0
def test_events_endpoint_as_user_causes_as_user_header_to_be_added(
    box_client,
    mock_box_network,
    generic_successful_response,
    mock_user_id,
    as_user_headers,
):
    # pylint:disable=redefined-outer-name
    mock_box_network.session.request.side_effect = [
        generic_successful_response,
    ]
    stream_position = 1348790499819
    options = {
        'url': '{0}/events'.format(API.BASE_API_URL),
        'retry_timeout': 60
    }
    box_client.events().as_user(User(None, mock_user_id)).long_poll(
        options, stream_position)
    assert mock_box_network.session.request.mock_calls == [
        call(
            'GET',
            options['url'],
            headers=as_user_headers,
            timeout=options['retry_timeout'],
            params={'stream_position': stream_position},
        ),
    ]
Example #3
0
def test_authenticate_app_user_sends_post_request_with_correct_params(
        mock_network_layer,
        successful_token_response,
        jti_length,
        jwt_algorithm,
        jwt_key_id,
        rsa_passphrase,
):
    # pylint:disable=redefined-outer-name
    fake_user_id = 'fake_user_id'
    with jwt_auth_init_mocks(mock_network_layer, successful_token_response, jwt_algorithm, jwt_key_id, rsa_passphrase) as params:
        with jwt_auth_auth_mocks(jti_length, jwt_algorithm, jwt_key_id, fake_user_id, 'user', *params) as oauth:
            oauth.authenticate_app_user(User(None, fake_user_id))
def test_client_as_user_causes_as_user_header_to_be_added(
        box_client,
        mock_box_network,
        generic_successful_response,
        mock_user_id,
        as_user_headers,
):
    # pylint:disable=redefined-outer-name
    mock_box_network.session.request.side_effect = [generic_successful_response]
    box_client.as_user(User(None, mock_user_id)).folder('0').get()
    assert mock_box_network.session.request.mock_calls == [
        call(
            'GET',
            '{0}/folders/0'.format(API.BASE_API_URL),
            headers=as_user_headers,
            params=None,
        ),
    ]
Example #5
0
def test_metadata_endpoint_as_user_causes_as_user_header_to_be_added(
    box_client,
    mock_box_network,
    generic_successful_response,
    mock_user_id,
    as_user_headers,
):
    # pylint:disable=redefined-outer-name
    mock_box_network.session.request.side_effect = [
        generic_successful_response,
    ]
    box_client.file('0').metadata().as_user(User(None, mock_user_id)).get()
    assert mock_box_network.session.request.mock_calls == [
        call(
            'GET',
            '{0}/files/0/metadata/global/properties'.format(API.BASE_API_URL),
            headers=as_user_headers,
        ),
    ]
Example #6
0
    def test_oauth2_callback(self, m_box_oauth_authenticate, m_user_get):
        """
        Tests the Box OAuth2 Callback handler. After completing the OAuth auth code flow,
        the user has a BoxUserToken and BoxUserStreamPosition object and calls can be made
        to the Box API on the user's behalf.

        Args:
            m_box_oauth_authenticate: mock for boxsdk OAuth2.authenticate
            m_user_get: mock for boxsdk User.get
        """
        self.client.login(username='******', password='******')

        session = self.client.session
        session['box'] = {'state': 'box_csrf_state_123'}
        session.save()

        # patch return_values
        m_box_oauth_authenticate.return_value = ('asdf', 'asdf1234')
        m_user_get.return_value = User(
            None, u'me', {
                'id': '200000000',
                'name': 'DS User',
                'login': '******'
            })

        response = self.client.get(reverse('box_integration:oauth2_callback'),
                                   {
                                       'code': 'box_authorization_code_123',
                                       'state': 'box_csrf_state_123'
                                   })

        user = get_user_model().objects.get(username='******')
        self.assertEqual('asdf', user.box_user_token.access_token)

        self.assertRedirects(response,
                             reverse('box_integration:index'),
                             fetch_redirect_response=False)

        response = self.client.get(reverse('box_integration:index'))
        self.assertContains(response, 'Box.com Enabled')
        self.assertContains(
            response,
            'Box.com as <em>DS User ([email protected])</em>')
Example #7
0
def test_group_membership_object_as_user_causes_as_user_header_to_be_added(
        box_client,
        mock_box_network,
        generic_successful_response,
        mock_user_id,
        as_user_headers,
):
    # pylint:disable=redefined-outer-name
    mock_box_network.session.request.side_effect = [
        generic_successful_response,
    ]
    with patch.object(GroupMembership, '_init_user_and_group_instances') as init:
        init.return_value = None, None
        box_client.group_membership('0').as_user(User(None, mock_user_id)).get()
    assert mock_box_network.session.request.mock_calls == [
        call(
            'GET',
            '{0}/group_memberships/0'.format(API.BASE_API_URL),
            headers=as_user_headers,
            params=None,
        ),
    ]
@pytest.fixture
def jwt_auth_init_and_auth_mocks(jwt_auth_init_mocks, jwt_auth_auth_mocks):

    @contextmanager
    def _jwt_auth_init_and_auth_mocks(sub, sub_type, *jwt_auth_init_mocks_args, **jwt_auth_init_mocks_kwargs):
        assert_authed = jwt_auth_init_mocks_kwargs.pop('assert_authed', True)
        with jwt_auth_init_mocks(*jwt_auth_init_mocks_args, assert_authed=assert_authed, **jwt_auth_init_mocks_kwargs) as params:
            with jwt_auth_auth_mocks(sub, sub_type, *params, assert_authed=assert_authed) as oauth:
                yield oauth

    return _jwt_auth_init_and_auth_mocks


@pytest.mark.parametrize(
    ('user', 'pass_in_init'),
    list(product([str('fake_user_id'), text_type('fake_user_id'), User(None, 'fake_user_id')], [False, True])),
)
def test_authenticate_user_sends_post_request_with_correct_params(jwt_auth_init_and_auth_mocks, user, pass_in_init):
    # pylint:disable=redefined-outer-name
    if isinstance(user, User):
        user_id = user.object_id
    elif isinstance(user, string_types):
        user_id = user
    else:
        raise NotImplementedError
    init_kwargs = {}
    authenticate_params = []
    if pass_in_init:
        init_kwargs['user'] = user
    else:
        authenticate_params.append(user)
Example #9
0
            with jwt_auth_auth_mocks(sub,
                                     sub_type,
                                     *params,
                                     assert_authed=assert_authed) as oauth:
                yield oauth

    return _jwt_auth_init_and_auth_mocks


@pytest.mark.parametrize(
    ('user', 'pass_in_init'),
    list(
        product([
            str('fake_user_id'),
            text_type('fake_user_id'),
            User(None, 'fake_user_id')
        ], [False, True])),
)
def test_authenticate_user_sends_post_request_with_correct_params(
        jwt_auth_init_and_auth_mocks, user, pass_in_init):
    # pylint:disable=redefined-outer-name
    if isinstance(user, User):
        user_id = user.object_id
    elif isinstance(user, string_types):
        user_id = user
    else:
        raise NotImplementedError
    init_kwargs = {}
    authenticate_params = []
    if pass_in_init:
        init_kwargs['user'] = user
Example #10
0
def mock_user(mock_box_session, mock_user_id):
    user = User(mock_box_session, mock_user_id)
    return user
Example #11
0
def mock_user(mock_box_session, mock_user_id):
    # pylint:disable=redefined-outer-name
    user = User(mock_box_session, mock_user_id)
    return user
Example #12
0
def mock_user_list(mock_box_session):
    user_list = []
    first_user = User(mock_box_session, '33333')
    second_user = User(mock_box_session, '44444')
    user_list = [first_user, second_user]
    return user_list
Example #13
0
def mock_user(mock_box_session):
    user = User(mock_box_session, '12345')
    return user