|
| 1 | +"""Stack API tests — fetch, settings, users, share/unshare.""" |
| 2 | + |
| 3 | +import os |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from framework import helpers as h |
| 8 | + |
| 9 | +pytestmark = pytest.mark.order(3) |
| 10 | + |
| 11 | + |
| 12 | +class TestStack: |
| 13 | + def test_fetch(self, ctx): |
| 14 | + resp = ctx.client.stack(ctx.stack_api_key).fetch() |
| 15 | + h.assert_status(resp, 200) |
| 16 | + stack = h.body(resp).get("stack", {}) |
| 17 | + h.tracked_assert(stack.get("api_key"), "api_key").equals(ctx.stack_api_key) |
| 18 | + |
| 19 | + def test_settings(self, ctx): |
| 20 | + resp = ctx.client.stack(ctx.stack_api_key).settings() |
| 21 | + h.assert_status(resp, 200) |
| 22 | + |
| 23 | + def test_create_settings(self, ctx): |
| 24 | + data = { |
| 25 | + "stack_settings": { |
| 26 | + "stack_variables": {"enforce_unique_urls": "true"}, |
| 27 | + } |
| 28 | + } |
| 29 | + resp = ctx.client.stack(ctx.stack_api_key).create_settings(data) |
| 30 | + h.assert_status(resp, 200, 201) |
| 31 | + |
| 32 | + def test_users(self, ctx): |
| 33 | + resp = ctx.client.stack(ctx.stack_api_key).users() |
| 34 | + h.assert_status(resp, 200) |
| 35 | + |
| 36 | + def test_update(self, ctx): |
| 37 | + data = {"stack": {"description": "updated by integration suite"}} |
| 38 | + resp = ctx.client.stack(ctx.stack_api_key).update(data) |
| 39 | + h.assert_status(resp, 200, 201) |
| 40 | + |
| 41 | + def test_reset_settings(self, ctx): |
| 42 | + resp = ctx.client.stack(ctx.stack_api_key).reset_settings({"stack_settings": {}}) |
| 43 | + h.assert_status(resp, 200, 201) |
| 44 | + |
| 45 | + |
| 46 | +class TestStackOwnership: |
| 47 | + """Ownership/role operations exercised safely (no real transfer occurs).""" |
| 48 | + |
| 49 | + def test_update_user_role(self, ctx): |
| 50 | + # Map the current user to a role; on a fresh single-user stack this may |
| 51 | + # be accepted (200) or rejected (422) — both confirm the SDK call works. |
| 52 | + roles = h.body(ctx.client.stack(ctx.stack_api_key).roles().find()).get("roles", []) |
| 53 | + role_uid = next((r["uid"] for r in roles), None) |
| 54 | + if not (role_uid and ctx.user_uid): |
| 55 | + pytest.skip("no role/user available") |
| 56 | + resp = ctx.client.stack(ctx.stack_api_key).update_user_role({"users": {ctx.user_uid: [role_uid]}}) |
| 57 | + # 404 when the user isn't a separately-added stack member (owner self-assign). |
| 58 | + h.assert_status(resp, 200, 201, 404, 422) |
| 59 | + |
| 60 | + def test_transfer_ownership_invalid(self, ctx): |
| 61 | + # Transferring to an invalid address must fail — exercises the endpoint |
| 62 | + # without actually handing the stack to anyone. |
| 63 | + resp = ctx.client.stack(ctx.stack_api_key).transfer_ownership({"transfer_to": "not-an-email"}) |
| 64 | + h.assert_status(resp, 400, 422) |
| 65 | + |
| 66 | + def test_accept_ownership_bogus_token(self, ctx): |
| 67 | + # Accepting with a bogus token must fail. |
| 68 | + resp = ctx.client.stack(ctx.stack_api_key).accept_ownership(ctx.user_uid or "uid", "bogus_token") |
| 69 | + h.assert_status(resp, 400, 404, 422) |
| 70 | + |
| 71 | + |
| 72 | +class TestStackSharing: |
| 73 | + def test_share(self, ctx): |
| 74 | + member = os.getenv("MEMBER_EMAIL") |
| 75 | + if not member: |
| 76 | + pytest.skip("MEMBER_EMAIL not set") |
| 77 | + # Sharing requires a valid role mapping per email — an empty roles object |
| 78 | + # is rejected with 422 "roles is not valid". |
| 79 | + roles = h.body(ctx.client.stack(ctx.stack_api_key).roles().find()).get("roles", []) |
| 80 | + role_uid = next((r["uid"] for r in roles if r.get("name") != "Admin"), |
| 81 | + roles[0]["uid"] if roles else None) |
| 82 | + if not role_uid: |
| 83 | + pytest.skip("no role available to share with") |
| 84 | + data = {"emails": [member], "roles": {member: [role_uid]}} |
| 85 | + resp = ctx.client.stack(ctx.stack_api_key).share(data) |
| 86 | + h.assert_status(resp, 200, 201) |
| 87 | + |
| 88 | + def test_unshare(self, ctx): |
| 89 | + member = os.getenv("MEMBER_EMAIL") |
| 90 | + if not member: |
| 91 | + pytest.skip("MEMBER_EMAIL not set") |
| 92 | + resp = ctx.client.stack(ctx.stack_api_key).unshare({"email": member}) |
| 93 | + h.assert_status(resp, 200, 201) |
0 commit comments