File: test_s3control_access_points.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 (151 lines) | stat: -rw-r--r-- 5,314 bytes parent folder | download
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import re
from uuid import uuid4

import boto3
import pytest
from botocore.client import ClientError

from moto import mock_aws
from tests.test_s3 import s3_aws_verified


@mock_aws
def test_get_unknown_access_point():
    client = boto3.client("s3control", region_name="ap-southeast-1")

    with pytest.raises(ClientError) as exc:
        client.get_access_point(AccountId="111111111111", Name="ap_name")
    err = exc.value.response["Error"]
    assert err["Code"] == "NoSuchAccessPoint"
    assert err["Message"] == "The specified accesspoint does not exist"
    assert err["AccessPointName"] == "ap_name"


@mock_aws
def test_get_access_point_minimal():
    client = boto3.client("s3control", region_name="ap-southeast-1")
    client.create_access_point(
        AccountId="111111111111", Name="ap_name", Bucket="mybucket"
    )

    resp = client.get_access_point(AccountId="111111111111", Name="ap_name")

    assert resp["Name"] == "ap_name"
    assert resp["Bucket"] == "mybucket"
    assert resp["NetworkOrigin"] == "Internet"
    assert resp["PublicAccessBlockConfiguration"] == {
        "BlockPublicAcls": True,
        "IgnorePublicAcls": True,
        "BlockPublicPolicy": True,
        "RestrictPublicBuckets": True,
    }
    assert "CreationDate" in resp
    assert "Alias" in resp
    assert re.match("ap_name-[a-z0-9]+-s3alias", resp["Alias"])
    assert resp["AccessPointArn"] == (
        "arn:aws:s3:us-east-1:111111111111:accesspoint/ap_name"
    )
    assert "Endpoints" in resp

    assert resp["Endpoints"]["ipv4"] == "s3-accesspoint.us-east-1.amazonaws.com"
    assert resp["Endpoints"]["fips"] == "s3-accesspoint-fips.us-east-1.amazonaws.com"
    assert resp["Endpoints"]["fips_dualstack"] == (
        "s3-accesspoint-fips.dualstack.us-east-1.amazonaws.com"
    )
    assert resp["Endpoints"]["dualstack"] == (
        "s3-accesspoint.dualstack.us-east-1.amazonaws.com"
    )


@mock_aws
def test_get_access_point_full():
    client = boto3.client("s3control", region_name="ap-southeast-1")
    client.create_access_point(
        AccountId="111111111111",
        Name="ap_name",
        Bucket="mybucket",
        VpcConfiguration={"VpcId": "sth"},
        PublicAccessBlockConfiguration={
            "BlockPublicAcls": False,
            "IgnorePublicAcls": False,
            "BlockPublicPolicy": False,
            "RestrictPublicBuckets": False,
        },
    )

    resp = client.get_access_point(AccountId="111111111111", Name="ap_name")

    assert resp["Name"] == "ap_name"
    assert resp["Bucket"] == "mybucket"
    assert resp["NetworkOrigin"] == "VPC"
    assert resp["VpcConfiguration"] == {"VpcId": "sth"}
    assert resp["PublicAccessBlockConfiguration"] == {
        "BlockPublicAcls": False,
        "IgnorePublicAcls": False,
        "BlockPublicPolicy": False,
        "RestrictPublicBuckets": False,
    }


@mock_aws
def test_list_access_points():
    region = "us-east-1"
    account_id = "111111111111"
    client = boto3.client("s3control", region_name=region)
    s3_client = boto3.client("s3", region_name=region)

    resp = client.list_access_points(AccountId=account_id)
    assert not resp.get("AccessPointList")

    s3_client.create_bucket(Bucket="bucket-a")
    s3_client.create_bucket(Bucket="bucket-b")
    client.create_access_point(AccountId=account_id, Name="ap1-a", Bucket="bucket-a")
    client.create_access_point(AccountId=account_id, Name="ap2-a", Bucket="bucket-a")
    client.create_access_point(AccountId=account_id, Name="ap3-b", Bucket="bucket-b")

    resp = client.list_access_points(AccountId=account_id)
    assert len(resp["AccessPointList"]) == 3

    resp = client.list_access_points(AccountId=account_id, Bucket="bucket-a")
    aps = resp["AccessPointList"]
    assert len(aps) == 2
    assert {ap["Name"] for ap in aps} == {"ap1-a", "ap2-a"}

    resp = client.list_access_points(AccountId=account_id, MaxResults=2)
    assert len(resp["AccessPointList"]) == 2
    assert "NextToken" in resp

    next_token = resp["NextToken"]
    resp2 = client.list_access_points(AccountId=account_id, NextToken=next_token)
    assert len(resp2["AccessPointList"]) == 1
    assert "NextToken" not in resp2


@pytest.mark.aws_verified
@s3_aws_verified
def test_delete_access_point(bucket_name=None):
    sts = boto3.client("sts", "us-east-1")
    account_id = sts.get_caller_identity()["Account"]

    client = boto3.client("s3control", region_name="us-east-1")
    ap_name = "ap-" + str(uuid4())[0:6]
    expected_arn = f"arn:aws:s3:us-east-1:{account_id}:accesspoint/{ap_name}"

    create = client.create_access_point(
        AccountId=account_id, Name=ap_name, Bucket=bucket_name
    )
    assert create["Alias"].startswith(ap_name)
    assert create["Alias"].endswith("-s3alias")
    assert create["AccessPointArn"] == expected_arn

    get = client.get_access_point(AccountId=account_id, Name=ap_name)
    assert get["Alias"] == create["Alias"]
    assert get["AccessPointArn"] == expected_arn

    client.delete_access_point(AccountId=account_id, Name=ap_name)

    with pytest.raises(ClientError) as exc:
        client.get_access_point(AccountId=account_id, Name=ap_name)
    err = exc.value.response["Error"]
    assert err["Code"] == "NoSuchAccessPoint"
    assert err["Message"] == "The specified accesspoint does not exist"