1import pytest
2from sure import expect
3from moto.s3.utils import (
4    bucket_name_from_url,
5    _VersionedKeyStore,
6    parse_region_from_url,
7    clean_key_name,
8    undo_clean_key_name,
9)
10from unittest.mock import patch
11
12
13def test_base_url():
14    expect(bucket_name_from_url("https://s3.amazonaws.com/")).should.equal(None)
15
16
17def test_localhost_bucket():
18    expect(bucket_name_from_url("https://wfoobar.localhost:5000/abc")).should.equal(
19        "wfoobar"
20    )
21
22
23def test_localhost_without_bucket():
24    expect(bucket_name_from_url("https://www.localhost:5000/def")).should.equal(None)
25
26
27def test_force_ignore_subdomain_for_bucketnames(monkeypatch):
28    with patch("moto.s3.utils.S3_IGNORE_SUBDOMAIN_BUCKETNAME", True):
29        expect(
30            bucket_name_from_url("https://subdomain.localhost:5000/abc/resource")
31        ).should.equal(None)
32
33
34def test_versioned_key_store():
35    d = _VersionedKeyStore()
36
37    d.should.have.length_of(0)
38
39    d["key"] = [1]
40
41    d.should.have.length_of(1)
42
43    d["key"] = 2
44    d.should.have.length_of(1)
45
46    d.should.have.key("key").being.equal(2)
47
48    d.get.when.called_with("key").should.return_value(2)
49    d.get.when.called_with("badkey").should.return_value(None)
50    d.get.when.called_with("badkey", "HELLO").should.return_value("HELLO")
51
52    # Tests key[
53    d.shouldnt.have.key("badkey")
54    d.__getitem__.when.called_with("badkey").should.throw(KeyError)
55
56    d.getlist("key").should.have.length_of(2)
57    d.getlist("key").should.be.equal([[1], 2])
58    d.getlist("badkey").should.be.none
59
60    d.setlist("key", 1)
61    d.getlist("key").should.be.equal([1])
62
63    d.setlist("key", (1, 2))
64    d.getlist("key").shouldnt.be.equal((1, 2))
65    d.getlist("key").should.be.equal([1, 2])
66
67    d.setlist("key", [[1], [2]])
68    d["key"].should.have.length_of(1)
69    d.getlist("key").should.be.equal([[1], [2]])
70
71
72def test_parse_region_from_url():
73    expected = "us-west-2"
74    for url in [
75        "http://s3-us-west-2.amazonaws.com/bucket",
76        "http://s3.us-west-2.amazonaws.com/bucket",
77        "http://bucket.s3-us-west-2.amazonaws.com",
78        "https://s3-us-west-2.amazonaws.com/bucket",
79        "https://s3.us-west-2.amazonaws.com/bucket",
80        "https://bucket.s3-us-west-2.amazonaws.com",
81    ]:
82        parse_region_from_url(url).should.equal(expected)
83
84    expected = "us-east-1"
85    for url in [
86        "http://s3.amazonaws.com/bucket",
87        "http://bucket.s3.amazonaws.com",
88        "https://s3.amazonaws.com/bucket",
89        "https://bucket.s3.amazonaws.com",
90    ]:
91        parse_region_from_url(url).should.equal(expected)
92
93
94@pytest.mark.parametrize(
95    "key,expected",
96    [
97        ("foo/bar/baz", "foo/bar/baz"),
98        ("foo", "foo"),
99        (
100            "foo/run_dt%3D2019-01-01%252012%253A30%253A00",
101            "foo/run_dt=2019-01-01%2012%3A30%3A00",
102        ),
103    ],
104)
105def test_clean_key_name(key, expected):
106    clean_key_name(key).should.equal(expected)
107
108
109@pytest.mark.parametrize(
110    "key,expected",
111    [
112        ("foo/bar/baz", "foo/bar/baz"),
113        ("foo", "foo"),
114        (
115            "foo/run_dt%3D2019-01-01%252012%253A30%253A00",
116            "foo/run_dt%253D2019-01-01%25252012%25253A30%25253A00",
117        ),
118    ],
119)
120def test_undo_clean_key_name(key, expected):
121    undo_clean_key_name(key).should.equal(expected)
122