File: test_client.py

package info (click to toggle)
aptly-api-client 0.2.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 208 kB
  • sloc: python: 1,493; makefile: 3
file content (117 lines) | stat: -rw-r--r-- 4,713 bytes parent folder | download | duplicates (2)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# -* encoding: utf-8 *-

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from typing import Any, cast
from unittest.case import TestCase

import requests
import requests_mock

from aptly_api import Client as AptlyClient


# as we're testing the individual parts, this is rather simple
from aptly_api.base import AptlyAPIException


class ClientTests(TestCase):
    def __init__(self, *args: Any) -> None:
        super().__init__(*args)
        self.client = AptlyClient("http://test/")

    def test_instantiate(self) -> None:
        cl = AptlyClient("http://test/")
        self.assertEqual(
            str(cl),
            "Client (Aptly API Client) <http://test/>"
        )

    @requests_mock.Mocker(kw='rmock')
    def test_api_subdir_get(self, *, rmock: requests_mock.Mocker) -> None:
        # register mock:// scheme with urllib.parse
        import urllib.parse
        urllib.parse.uses_netloc += ['mock']
        urllib.parse.uses_relative += ['mock']
        urllib.parse.uses_fragment += ['mock']
        urllib.parse.uses_params += ['mock']

        cl = AptlyClient("mock://test/basedir/")
        rmock.get("mock://test/basedir/api/test", status_code=200, text='')
        cl.files.do_get("api/test")
        self.assertTrue(rmock.called)

    def test_error_no_error(self) -> None:
        class MockResponse:
            def __init__(self, status_code: int = 200) -> None:
                self.status_code = status_code

        self.assertEqual(
            self.client.files._error_from_response(cast(requests.Response, MockResponse())),
            "no error (status 200)"
        )

    def test_error_no_json(self) -> None:
        adapter = requests_mock.Adapter()
        adapter.register_uri("GET", "mock://test/api", status_code=400, text="this is not json", reason="test")
        session = requests.session()
        session.mount("mock", adapter)
        resp = session.get("mock://test/api")

        self.assertEqual(
            self.client.files._error_from_response(resp),
            "400 test this is not json"
        )

    def test_error_dict(self) -> None:
        adapter = requests_mock.Adapter()
        adapter.register_uri("GET", "mock://test/api", status_code=400, text='{"error": "error", "meta": "meta"}',
                             reason="test")
        session = requests.session()
        session.mount("mock", adapter)
        resp = session.get("mock://test/api")
        self.assertEqual(
            self.client.files._error_from_response(resp),
            "400 - test - error (meta)"
        )

    def test_error_list(self) -> None:
        adapter = requests_mock.Adapter()
        adapter.register_uri("GET", "mock://test/api", status_code=400, text='[{"error": "error", "meta": "meta"}]',
                             reason="test")
        session = requests.session()
        session.mount("mock", adapter)
        resp = session.get("mock://test/api")
        self.assertEqual(
            self.client.files._error_from_response(resp),
            "400 - test - error (meta)"
        )

    @requests_mock.Mocker(kw='rmock')
    def test_error_get(self, *, rmock: requests_mock.Mocker) -> None:
        rmock.register_uri("GET", "mock://test/api", status_code=400, text='[{"error": "error", "meta": "meta"}]',
                           reason="test")
        with self.assertRaises(AptlyAPIException):
            self.client.files.do_get("mock://test/api")

    @requests_mock.Mocker(kw='rmock')
    def test_error_post(self, *, rmock: requests_mock.Mocker) -> None:
        rmock.register_uri("POST", "mock://test/api", status_code=400, text='[{"error": "error", "meta": "meta"}]',
                           reason="test")
        with self.assertRaises(AptlyAPIException):
            self.client.files.do_post("mock://test/api")

    @requests_mock.Mocker(kw='rmock')
    def test_error_put(self, *, rmock: requests_mock.Mocker) -> None:
        rmock.register_uri("PUT", "mock://test/api", status_code=400, text='[{"error": "error", "meta": "meta"}]',
                           reason="test")
        with self.assertRaises(AptlyAPIException):
            self.client.files.do_put("mock://test/api")

    @requests_mock.Mocker(kw='rmock')
    def test_error_delete(self, *, rmock: requests_mock.Mocker) -> None:
        rmock.register_uri("DELETE", "mock://test/api", status_code=400, text='[{"error": "error", "meta": "meta"}]',
                           reason="test")
        with self.assertRaises(AptlyAPIException):
            self.client.files.do_delete("mock://test/api")