File: test_server.py

package info (click to toggle)
python-moto 5.1.18-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 116,520 kB
  • sloc: python: 636,725; javascript: 181; makefile: 39; sh: 3
file content (108 lines) | stat: -rw-r--r-- 3,104 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
from flask.testing import FlaskClient

import moto.server as server


class AuthenticatedClient(FlaskClient):
    def open(self, *args, **kwargs):
        kwargs["headers"] = kwargs.get("headers", {})
        kwargs["headers"]["Authorization"] = "Any authorization header"
        kwargs["content_length"] = 0  # Fixes content-length complaints.
        return super().open(*args, **kwargs)


def authenticated_client():
    backend = server.create_backend_app("s3")
    backend.test_client_class = AuthenticatedClient
    return backend.test_client()


def test_s3_server_get():
    test_client = authenticated_client()

    res = test_client.get("/")

    assert b"ListAllMyBucketsResult" in res.data


def test_s3_server_bucket_create():
    test_client = authenticated_client()

    res = test_client.put("/foobar", "http://localhost:5000")
    assert res.status_code == 200

    res = test_client.get("/")
    assert b"<Name>foobar</Name>" in res.data

    res = test_client.get("/foobar", "http://localhost:5000")
    assert res.status_code == 200
    assert b"ListBucketResult" in res.data

    res = test_client.put("/foobar2/", "http://localhost:5000")
    assert res.status_code == 200

    res = test_client.get("/")
    assert b"<Name>foobar2</Name>" in res.data

    res = test_client.get("/foobar2/", "http://localhost:5000")
    assert res.status_code == 200
    assert b"ListBucketResult" in res.data

    res = test_client.get("/missing-bucket", "http://localhost:5000")
    assert res.status_code == 404

    res = test_client.put("/foobar/bar", "http://localhost:5000", data="test value")
    assert res.status_code == 200

    res = test_client.get("/foobar/bar", "http://localhost:5000")
    assert res.status_code == 200
    assert res.data == b"test value"


def test_s3_server_post_to_bucket():
    test_client = authenticated_client()

    res = test_client.put("/foobar2", "http://localhost:5000/")
    assert res.status_code == 200

    test_client.post(
        "/foobar2",
        "https://localhost:5000/",
        data={"key": "the-key", "file": "nothing"},
    )

    res = test_client.get("/foobar2/the-key", "http://localhost:5000/")
    assert res.status_code == 200
    assert res.data == b"nothing"


def test_s3_server_put_ipv6():
    test_client = authenticated_client()

    res = test_client.put("/foobar2", "http://[::]:5000/")
    assert res.status_code == 200

    test_client.post(
        "/foobar2", "https://[::]:5000/", data={"key": "the-key", "file": "nothing"}
    )

    res = test_client.get("/foobar2/the-key", "http://[::]:5000/")
    assert res.status_code == 200
    assert res.data == b"nothing"


def test_s3_server_put_ipv4():
    test_client = authenticated_client()

    res = test_client.put("/foobar2", "http://127.0.0.1:5000/")
    assert res.status_code == 200

    test_client.post(
        "/foobar2",
        "https://127.0.0.1:5000/",
        data={"key": "the-key", "file": "nothing"},
    )

    res = test_client.get("/foobar2/the-key", "http://127.0.0.1:5000/")
    assert res.status_code == 200
    assert res.data == b"nothing"