"""Tests for the Cloud Storage emulator."""


def test_create_and_get_bucket(client):
    resp = client.post(
        "/storage/v1/b?project=test-proj",
        json={"name": "my-bucket", "location": "US"},
    )
    assert resp.status_code == 200
    bucket = resp.get_json()
    assert bucket["name"] == "my-bucket"
    assert bucket["location"] == "US"

    resp = client.get("/storage/v1/b/my-bucket")
    assert resp.status_code == 200
    assert resp.get_json()["name"] == "my-bucket"


def test_delete_bucket(client):
    client.post("/storage/v1/b?project=test-proj", json={"name": "temp-bucket"})
    resp = client.delete("/storage/v1/b/temp-bucket")
    assert resp.status_code == 204


def test_upload_and_get_object(client):
    client.post("/storage/v1/b?project=test-proj", json={"name": "obj-bucket"})

    resp = client.post(
        "/upload/storage/v1/b/obj-bucket/o?name=hello.txt",
        data=b"Hello, gcplocal!",
        content_type="text/plain",
    )
    assert resp.status_code == 200
    obj = resp.get_json()
    assert obj["name"] == "hello.txt"
    assert obj["size"] == "16"

    resp = client.get("/storage/v1/b/obj-bucket/o/hello.txt?alt=media")
    assert resp.status_code == 200
    assert resp.data == b"Hello, gcplocal!"


def test_bucket_iam(client):
    client.post("/storage/v1/b?project=test-proj", json={"name": "iam-bucket"})

    resp = client.get("/storage/v1/b/iam-bucket/iam")
    assert resp.status_code == 200
    policy = resp.get_json()
    assert policy["bindings"] == []
