Beispiel #1
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
Beispiel #2
0
    def test_request(self, fixture_registry_url, fixture_repositories_url,
                     fixture_repositories):
        expected_url = fixture_registry_url + fixture_repositories_url
        expected_code = 200

        client = Client(fixture_registry_url, verbose=False)
        headers = {}

        # 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)
            mo.assert_called_once_with(expected_url, headers=headers)
            assert res.status_code == expected_code and \
                res.json() == fixture_repositories

        # headers
        headers = {'foobar': 'foobar2000'}
        with mock.patch('requests.get', return_value=mock_res) as mo:
            res = client._request(expected_url, headers=headers)
            mo.assert_called_once_with(expected_url, headers=headers)
            assert res.status_code == expected_code and \
                res.json() == fixture_repositories

        # method
        headers = {}
        with mock.patch('requests.post', return_value=mock_res) as mo:
            res = client._request(expected_url,
                                  method=requests.post,
                                  verb='POST')
            mo.assert_called_once_with(
                expected_url,
                headers=headers,
            )
            assert res.status_code == expected_code and \
                res.json() == fixture_repositories

        # expected_code matching
        expected_code = 202
        mock_res.status_code = expected_code
        with mock.patch('requests.get', return_value=mock_res) as mo:
            res = client._request(expected_url, expected_code=expected_code)
            mo.assert_called_once_with(
                expected_url,
                headers=headers,
            )
            assert res.status_code == expected_code and \
                res.json() == fixture_repositories

        # expected code mismatch
        expected_code = 202
        other_code = 404  # other result than expected code
        other_code_msg = tools.get_error_status_message(other_code)
        mock_res.status_code = other_code
        with mock.patch('requests.get', return_value=mock_res) as mo:
            with pytest.raises(DRegCliException) as excinfo:
                res = client._request(expected_url,
                                      expected_code=expected_code)
                mo.assert_called_once_with(
                    expected_url,
                    headers=headers,
                )
                assert res.status_code == expected_code and \
                    res.json() == fixture_repositories
            assert str(excinfo.value) == other_code_msg