Пример #1
0
    def test_get_token(self, fixture_registry_url, fixture_auth,
                       fixture_auth_token):
        expected_realm = "https://myhost/v2/token"
        expected_service = "docker-registry.myhost.com"
        expected_scope = "registry:catalog:*"

        authenticate_header_val = \
            'Bearer realm="{realm}",' \
            'service="{service}",' \
            'scope="{scope}"'.format(
                realm=expected_realm,
                service=expected_service,
                scope=expected_scope
            )
        headers = {'Www-Authenticate': authenticate_header_val}

        mock_response = mock.MagicMock()
        mock_response.status_code = 200
        mock_response.headers = headers

        auth_get_token_inner_get_result = mock.MagicMock()
        auth_get_token_inner_get_result.status_code = 200
        auth_get_token_inner_get_result.json = mock.MagicMock(
            return_value={'token': fixture_auth_token})

        client = Client(fixture_registry_url, verbose=False)
        client.auth = fixture_auth

        with mock.patch('requests.get',
                        return_value=auth_get_token_inner_get_result) as mo:
            client._auth_get_token(mock_response)
            assert 'token' in client.auth and \
                client.auth['token'] == fixture_auth_token

        no_token_msg = "Get token request: no token found in response"
        auth_get_token_inner_get_result.json = mock.MagicMock(
            return_value={'token': ''})
        with mock.patch('requests.get',
                        return_value=auth_get_token_inner_get_result) as mo:
            with pytest.raises(DRegCliException) as excinfo:
                client._auth_get_token(mock_response)
            assert str(excinfo.value) == no_token_msg

        # no token use case
        auth_get_token_inner_get_result.json = mock.MagicMock(return_value={})
        with mock.patch('requests.get',
                        return_value=auth_get_token_inner_get_result) as mo:
            with pytest.raises(DRegCliException) as excinfo:
                client._auth_get_token(mock_response)
            assert str(excinfo.value) == no_token_msg
Пример #2
0
    def test_decorate_headers(self, fixture_registry_url, fixture_auth_token):
        client = Client(fixture_registry_url, verbose=False)
        client.auth = {'token': fixture_auth_token}

        headers = {'foobar': 'foobar2000'}
        original_headers = headers.copy()
        expected_headers = headers.copy()
        expected_headers['Authorization'] = \
            Client.Meta.auth_bearer_pattern.format(token=fixture_auth_token)

        decorated_headers = client._auth_decorate_headers(headers)
        # header should be unchanged
        assert headers == original_headers
        # decorated headers should be the expected
        decorated_headers == expected_headers
Пример #3
0
    def test_request(self, fixture_registry_url, fixture_repositories_url,
                     fixture_auth, fixture_auth_token):
        expected_url = fixture_registry_url + fixture_repositories_url
        expected_code = 200

        client = Client(fixture_registry_url, verbose=False)
        client.auth = fixture_auth
        client._auth_get_token = mock.MagicMock(
            return_value=fixture_auth_token)
        client.decorate_headers = mock.MagicMock(return_value={})

        # default
        mock_res = mock.MagicMock()
        mock_res.json = mock.MagicMock(return_value=fixture_repositories)
        mock_res.status_code = 200
        with mock.patch('requests.get', return_value=mock_res) as mo:
            res = client._request(expected_url)
            assert res.status_code == expected_code and \
                res.json() == fixture_repositories
Пример #4
0
    def test_set_auth(self, fixture_registry_url, fixture_auth):
        client = Client(fixture_registry_url)
        client.auth = fixture_auth

        client.reset_auth()
        assert not client.auth and isinstance(client.auth, bool)