当前位置: 首页>>代码示例>>Python>>正文


Python MagicMock.status_code方法代码示例

本文整理汇总了Python中unittest.mock.MagicMock.status_code方法的典型用法代码示例。如果您正苦于以下问题:Python MagicMock.status_code方法的具体用法?Python MagicMock.status_code怎么用?Python MagicMock.status_code使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在unittest.mock.MagicMock的用法示例。


在下文中一共展示了MagicMock.status_code方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: get

# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import status_code [as 别名]
 def get(url, auth, **kwargs):
     response = MagicMock()
     if auth[1] == "correctpass":
         response.status_code = 200
         response.json.return_value = {"access_token": token}
     else:
         response.status_code = 401
     return response
开发者ID:pfigue,项目名称:zign,代码行数:10,代码来源:test_cli.py

示例2: test_get_scm_source

# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import status_code [as 别名]
def test_get_scm_source():
    response = MagicMock()
    response.status_code = 200
    response.json.return_value = {
        "author": "ckent",
        "created": "2038-11-06T14:58:58.792Z",
        "revision": "6e6bb8c5a95ebb8b447b5516c292467d098c2a758",
        "status": "",
        "url": "git:[email protected]:continuous-delivery/cdp-builder.git",
        "valid": True
    }


    image = DockerImage(registry='registry', team='foo', artifact='bar', tag='42')
    api = PierOne('registry')
    api.session.request = MagicMock(return_value=response)
    details = api.get_scm_source(image)

    assert details['author'] == 'ckent'
    assert details['valid'] == True

    api.session.request = MagicMock(return_value=make_error_response(404))
    with pytest.raises(ArtifactNotFound):
        api.get_scm_source(image)

    api.session.request = MagicMock(return_value=make_error_response(500))
    with pytest.raises(requests.HTTPError):
        api.get_scm_source(image)

    api.session.request = MagicMock(return_value=make_error_response(403))
    with pytest.raises(Forbidden):
        api.get_scm_source(image)
开发者ID:zalando-stups,项目名称:pierone-cli,代码行数:34,代码来源:test_api.py

示例3: test_get_tag_info

# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import status_code [as 别名]
def test_get_tag_info():
    response = MagicMock()
    response.status_code = 200
    response.json.return_value = {
        "artifact": "test",
        "created": "2018-08-27T13:15:28.968Z",
        "created_by": "credprov-cdp-controller-proxy_pierone-token",
        "image": "sha256:2ee6f0b4d7313903fdf3d326dacfc4c711da339ab3a12ccdd78e18e17daa6eb1",
        "name": "pr-1-1",
        "status": "test_status",
        "status_reason_details": "test_status",
        "status_reason_summary": "test_status",
        "status_received_at": "2018-08-27T13:15:28.968Z"
    }

    image = DockerImage(registry='registry', team='foo', artifact='bar', tag=None)
    api = PierOne('registry')
    api.session.request = MagicMock(return_value=response)
    details = api.get_tag_info(image)

    assert details['artifact'] == 'test'
    assert details['created_by'] == '[CDP]'

    api.session.request = MagicMock(return_value=make_error_response(404))
    with pytest.raises(ArtifactNotFound):
        api.get_tag_info(image)

    api.session.request = MagicMock(return_value=make_error_response(500))
    with pytest.raises(requests.HTTPError):
        api.get_tag_info(image)

    api.session.request = MagicMock(return_value=make_error_response(403))
    with pytest.raises(Forbidden):
        api.get_tag_info(image)
开发者ID:zalando-stups,项目名称:pierone-cli,代码行数:36,代码来源:test_api.py

示例4: test_get_image_tags

# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import status_code [as 别名]
def test_get_image_tags():
    response = MagicMock()
    response.status_code = 200
    response.json.return_value = [{'created': '2015-06-01T14:12:03.276+0000',
                                   'created_by': 'foobar',
                                   'name': '0.17'}]
    image = DockerImage(registry='registry', team='foo', artifact='bar', tag=None)
    api = PierOne('registry')
    api.session.request = MagicMock(return_value=response)
    image_tags = api.get_image_tags(image)
    tag = image_tags[0]

    assert tag['team'] == 'foo'
    assert tag['artifact'] == 'bar'
    assert tag['tag'] == '0.17'
    assert tag['created_by'] == 'foobar'

    api.session.request = MagicMock(return_value=make_error_response(404))
    with pytest.raises(ArtifactNotFound):
        api.get_image_tags(image)

    api.session.request = MagicMock(return_value=make_error_response(500))
    with pytest.raises(requests.HTTPError):
        api.get_image_tags(image)

    api.session.request = MagicMock(return_value=make_error_response(403))
    with pytest.raises(Forbidden):
        api.get_image_tags(image)
开发者ID:zalando-stups,项目名称:pierone-cli,代码行数:30,代码来源:test_api.py

示例5: test_get_latest_tag_IOException

# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import status_code [as 别名]
def test_get_latest_tag_IOException(monkeypatch):
    response = MagicMock()
    response.status_code = 200
    response.json.return_value = [{'created': '2015-06-01T14:12:03.276+0000',
                                   'created_by': 'foobar',
                                   'name': '0.17'},
                                  {'created': '2015-06-11T15:27:34.672+0000',
                                   'created_by': 'foobar',
                                   'name': '0.18'},
                                  {'created': '2015-06-11T16:13:29.152+0000',
                                   'created_by': 'foobar',
                                   'name': '0.22'},
                                  {'created': '2015-06-11T15:36:55.033+0000',
                                   'created_by': 'foobar',
                                   'name': '0.19'},
                                  {'created': '2015-06-11T15:45:50.225+0000',
                                   'created_by': 'foobar',
                                   'name': '0.20'},
                                  {'created': '2015-06-11T15:51:49.307+0000',
                                   'created_by': 'foobar',
                                   'name': '0.21'}]
    monkeypatch.setattr('pierone.api.session.get', MagicMock(side_effect=Exception(IOError), return_value=response))
    monkeypatch.setattr('pierone.api.get_existing_token', MagicMock(return_value={'access_token': 'tok123'}))
    token_name = 'dummy'
    image = DockerImage(registry='registry', team='foo', artifact='bar', tag='latest')
    data = get_latest_tag(token_name, image)

    assert data is None
开发者ID:jmcs,项目名称:pierone-cli,代码行数:30,代码来源:test_api.py

示例6: test_get_latest_tag

# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import status_code [as 别名]
def test_get_latest_tag(monkeypatch):
    response = MagicMock()
    response.status_code = 200
    response.json.return_value = [{'created': '2015-06-01T14:12:03.276+0000',
                                   'created_by': 'foobar',
                                   'name': '0.17'},
                                  {'created': '2015-06-11T15:27:34.672+0000',
                                   'created_by': 'foobar',
                                   'name': '0.18'},
                                  {'created': '2015-06-11T16:13:29.152+0000',
                                   'created_by': 'foobar',
                                   'name': '0.22'},
                                  {'created': '2015-06-11T15:36:55.033+0000',
                                   'created_by': 'foobar',
                                   'name': '0.19'},
                                  {'created': '2015-06-11T15:45:50.225+0000',
                                   'created_by': 'foobar',
                                   'name': '0.20'},
                                  {'created': '2015-06-11T15:51:49.307+0000',
                                   'created_by': 'foobar',
                                   'name': '0.21'}]
    monkeypatch.setattr('pierone.api.session.get', MagicMock(return_value=response))
    image = DockerImage(registry='registry', team='foo', artifact='bar', tag='latest')
    data = get_latest_tag(image)

    assert data == '0.22'
开发者ID:rafaelcaricio,项目名称:pierone-cli,代码行数:28,代码来源:test_api.py

示例7: test_get_bad_status

# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import status_code [as 别名]
    def test_get_bad_status(self, mock_get):
        url = "http://httpbin.org/get"
        resp = MagicMock()
        resp.status_code = 400
        resp.json = MagicMock(return_value={})
        mock_get.return_value = resp
        block = HTTPRequestsPostSignal()
        self.configure_block(block, {
            "http_method": "GET",
            "url": url
        })
        block.logger.warning = MagicMock()
        signals = [Signal()]

        block.start()
        block.process_signals(signals)
        block.stop()

        block.logger.warning.assert_called_with(
            'HTTPMethod.GET request to http://httpbin.org/get '
            'returned with response code: 400'
        )
        self.assertTrue(mock_get.called)
        self.assertEqual(self.last_notified[DEFAULT_TERMINAL][0].to_dict(),
                         signals[0].to_dict())
开发者ID:nio-blocks,项目名称:http_requests,代码行数:27,代码来源:test_http_requests_post_signal_block.py

示例8: test_keep_dockercfg_entries

# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import status_code [as 别名]
def test_keep_dockercfg_entries(monkeypatch, tmpdir):
    monkeypatch.setattr('os.path.expanduser', lambda x: x.replace('~', str(tmpdir)))
    response = MagicMock()
    response.status_code = 200
    response.json.return_value = {'access_token': '12377'}
    monkeypatch.setattr('requests.get', MagicMock(return_value=response))
    path = os.path.expanduser('~/.docker/config.json')

    key = 'https://old.example.org'
    existing_data = {
        key: {
            'auth': 'abc123',
            'email': '[email protected]'
        }
    }
    os.makedirs(os.path.dirname(path))
    with open(path, 'w') as fd:
        json.dump(existing_data, fd)

    docker_login('https://pierone.example.org', 'services', 'mytok',
                 'myuser', 'mypass', 'https://token.example.org', use_keyring=False)
    with open(path) as fd:
        data = yaml.safe_load(fd)
        assert {'auth': 'b2F1dGgyOjEyMzc3',
                'email': '[email protected]'} == data.get('auths', {}).get('https://pierone.example.org')
        assert existing_data.get(key) == data.get(key)
开发者ID:rafaelcaricio,项目名称:pierone-cli,代码行数:28,代码来源:test_api.py

示例9: fake_network_post_and_put

# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import status_code [as 别名]
 def fake_network_post_and_put(url, *args, **kwargs):
     if url == expected_results_api_url:
         subjob_done_event.set()  # Consider subjob finished once code posts to results url.
     elif url == expected_idle_api_url:
         teardown_done_event.set()
     mock_response = MagicMock(spec=requests.models.Response, create=True)
     mock_response.status_code = http.client.OK
     return mock_response
开发者ID:OspreyX,项目名称:ClusterRunner,代码行数:10,代码来源:test_cluster_slave.py

示例10: test_should_throw_exception_for_failed_login

# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import status_code [as 别名]
    def test_should_throw_exception_for_failed_login(self):

        response = MagicMock()
        response.status_code = 401
        self.account.login.side_effect = requests.HTTPError(response=response)

        self.assertRaises(requests.HTTPError,
                          lambda: Watcher('https://aker.cloudant.com', 'foo', 'bar', account_factory=self.account_factory))
开发者ID:physion,项目名称:aker,代码行数:10,代码来源:test_watcher.py

示例11: get_method_mock

# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import status_code [as 别名]
        def get_method_mock(*args, **kwargs):
            """
            Mocks called urls:

            1. /user/GatewayList
            2. /gateway/gatewaydata?gtMacID="..."&...

            :param args:
            :param kwargs:
            :return:
            """

            gatewaydata_path = 'gatewaydata'
            gateway_list_path = 'GatewayList'

            # comes in form 'http://test.com/section/command' - we want to split to get the command
            path_segment = args[0].split('/')[4].split('?')[0]
            mm = MagicMock()
            mm.status_code = 200

            content = '{"message": "not implemented"}'
            if path_segment == gatewaydata_path:

                if SecureAPITests.mock_call_counter % 2 == 0:
                    print('online')
                    gcs_data = SecureAPITests.gw_online

                if SecureAPITests.mock_call_counter % 2 == 1:
                    print('offline')
                    gcs_data = SecureAPITests.gw_offline

                # special case
                if SecureAPITests.mock_call_counter == 3:
                    gcs_data = SecureAPITests.capability_push_response
                    mm.status_code = 400

                content = gcs_data

                SecureAPITests.mock_call_counter += 1

            if path_segment == gateway_list_path:
                content = SecureAPITests.gateway_list

            mm.content = content

            return mm
开发者ID:dschien,项目名称:energy-aggregator,代码行数:48,代码来源:tests.py

示例12: test__get_resource_raises_non_200

# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import status_code [as 别名]
    def test__get_resource_raises_non_200(self, req_mock):
        response_mock = MagicMock()
        response_mock.status_code = 409

        req_mock.get.return_value = response_mock

        with self.assertRaises(FetcherException):
            self.fetcher._get_resource(self.fetcher.url)
开发者ID:flazzarini,项目名称:blocklister,代码行数:10,代码来源:test_fetcher.py

示例13: test_image_not_exists

# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import status_code [as 别名]
def test_image_not_exists(monkeypatch):
    response = MagicMock()
    response.status_code = 404
    response.json.return_value = {}
    monkeypatch.setattr('pierone.api.session.get', MagicMock(return_value=response))
    image = DockerImage(registry='registry', team='foo', artifact='bar', tag='latest')
    data = image_exists(image, 'tok123')

    assert data is False
开发者ID:rafaelcaricio,项目名称:pierone-cli,代码行数:11,代码来源:test_api.py

示例14: test_get_latest_tag_non_json

# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import status_code [as 别名]
def test_get_latest_tag_non_json(monkeypatch):
    response = MagicMock()
    response.status_code = 200
    response.json.return_value = None
    monkeypatch.setattr('pierone.api.session.get', MagicMock(return_value=response))
    image = DockerImage(registry='registry', team='foo', artifact='bar', tag='latest')
    data = get_latest_tag(image)

    assert data is None
开发者ID:rafaelcaricio,项目名称:pierone-cli,代码行数:11,代码来源:test_api.py

示例15: test_get_user_info

# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import status_code [as 别名]
 def test_get_user_info(self, mock_request_get):
     api_request = MagicMock()
     api_request.status_code = 200
     api_request.ok = PropertyMock(return_value=True)
     api_request.json = lambda: dict(success=True)
     mock_request_get.return_value = api_request
     self.assertIsInstance(
         self.remote_user_client.get_user_information('123'), dict
     )
开发者ID:CDE-UNIBE,项目名称:qcat,代码行数:11,代码来源:test_client.py


注:本文中的unittest.mock.MagicMock.status_code方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。