-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_sharing.py
More file actions
276 lines (211 loc) · 8.37 KB
/
Copy pathtest_sharing.py
File metadata and controls
276 lines (211 loc) · 8.37 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
"""Tests for share-state replication (``unstract.clone.sharing``).
Covers payload building (user email mapping, group remap, org flag),
the skip-when-group-phase-excluded axis, the empty-state short circuit,
dry-run behaviour and the SERVER_MANAGED guarantee that ``shared_users``
never rides the create POST.
"""
from __future__ import annotations
import threading
from unstract.clone.context import CloneContext, CloneOptions, RemapTable
from unstract.clone.phases.base import build_post_payload
from unstract.clone.report import PhaseResult
from unstract.clone.sharing import _FETCH_FAILED, _cached, apply_share_state
class FakeClient:
def __init__(self, users: list[dict] | None = None):
self.users: list[dict] = list(users or [])
self.share_posts: list[tuple[str, dict]] = []
def list_users(self):
return list(self.users)
def share_resource(self, share_path, payload):
self.share_posts.append((share_path, payload))
def _ctx(source, target, **opt_overrides):
return CloneContext(
source=source,
target=target,
options=CloneOptions(**opt_overrides),
remap=RemapTable(),
)
def _apply(ctx, src, result=None, src_detail_fn=None):
result = result if result is not None else PhaseResult(name="adapter")
apply_share_state(
ctx,
share_path="adapter/tgt-1/share/",
entity_label="adapter 'demo'",
src=src,
result=result,
lock=threading.Lock(),
src_detail_fn=src_detail_fn,
)
return result
def test_share_payload_maps_users_groups_and_org_flag():
src_client = FakeClient(
users=[
{"id": "7", "email": "alice@x.com"},
# service accounts flagged by the backend; email alone wouldn't tell
{"id": "8", "email": "svc@x.com", "is_service_account": True},
{"id": "9", "email": "bot@x.com", "is_service_account": True},
]
)
tgt_client = FakeClient(
users=[
{"id": "70", "email": "ALICE@x.com"},
{"id": "71", "email": "bot@x.com"},
]
)
ctx = _ctx(src_client, tgt_client)
ctx.remap.record("group", "1", "10")
result = _apply(
ctx,
{
"created_by": 99,
"shared_users": [7, 8, 9, 99], # svc accounts + owner must be dropped
"shared_groups": [1],
"shared_to_org": True,
},
)
assert tgt_client.share_posts == [
(
"adapter/tgt-1/share/",
{"shared_to_org": True, "shared_groups": [10], "shared_users": [70]},
)
]
assert result.failed == 0
assert result.warnings == []
def test_share_skips_users_missing_on_target_with_warning():
src_client = FakeClient(users=[{"id": "7", "email": "ghost@x.com"}])
tgt_client = FakeClient(users=[])
ctx = _ctx(src_client, tgt_client)
result = _apply(
ctx,
{"shared_users": [7], "shared_groups": [], "shared_to_org": True},
)
# shared_to_org still forces a POST; the unmapped user is just dropped.
assert tgt_client.share_posts == [
(
"adapter/tgt-1/share/",
{"shared_to_org": True, "shared_groups": [], "shared_users": []},
)
]
assert any("ghost@x.com" in w for w in result.warnings)
def test_share_unmapped_group_id_is_skipped_with_warning():
ctx = _ctx(FakeClient(), FakeClient())
ctx.remap.record("group", "1", "10")
result = _apply(
ctx,
{"shared_users": [], "shared_groups": [1, 2], "shared_to_org": False},
)
assert ctx.target.share_posts == [
(
"adapter/tgt-1/share/",
{"shared_to_org": False, "shared_groups": [10], "shared_users": []},
)
]
assert any("group id 2" in w for w in result.warnings)
def test_share_group_axis_omitted_when_group_phase_excluded():
ctx = _ctx(FakeClient(), FakeClient(), exclude=("group",))
result = _apply(
ctx,
{"shared_users": [], "shared_groups": [1], "shared_to_org": True},
)
(path, payload) = ctx.target.share_posts[0]
assert "shared_groups" not in payload # axis untouched on target
assert payload["shared_to_org"] is True
assert any("group phase excluded" in w for w in result.warnings)
def test_share_empty_state_skips_the_post():
ctx = _ctx(FakeClient(), FakeClient())
result = _apply(
ctx,
# Owner-only shared_users counts as empty.
{
"created_by": 99,
"shared_users": [99],
"shared_groups": [],
"shared_to_org": False,
},
)
assert ctx.target.share_posts == []
assert result.failed == 0
def test_share_dry_run_never_posts():
src_client = FakeClient(users=[{"id": "7", "email": "alice@x.com"}])
tgt_client = FakeClient(users=[{"id": "70", "email": "alice@x.com"}])
ctx = _ctx(src_client, tgt_client, dry_run=True)
_apply(
ctx,
{"shared_users": [7], "shared_groups": [], "shared_to_org": True},
)
assert tgt_client.share_posts == []
def test_share_dry_run_planned_group_remap_does_not_crash():
# In dry-run the group remap resolves to a synthetic uuid (planned), not
# an int pk — building the payload must not int()-cast and blow up.
ctx = _ctx(FakeClient(), FakeClient(), dry_run=True)
ctx.remap.record_planned("group", "1")
planned = ctx.remap.resolve("group", "1")
result = _apply(
ctx,
{"shared_users": [], "shared_groups": [1], "shared_to_org": False},
)
assert ctx.target.share_posts == [] # dry-run never posts
assert not result.errors
assert ctx.remap.is_planned(planned)
def test_share_fetches_source_detail_when_axes_missing_from_list_row():
ctx = _ctx(FakeClient(), FakeClient())
detail = {
"shared_users": [],
"shared_groups": [],
"shared_to_org": True,
}
calls = []
def fetch_detail():
calls.append(1)
return detail
_apply(ctx, {"id": "src-1", "name": "demo"}, src_detail_fn=fetch_detail)
assert calls == [1]
assert ctx.target.share_posts[0][1]["shared_to_org"] is True
def test_share_users_listing_caches_across_resources():
src_client = FakeClient(users=[{"id": "7", "email": "alice@x.com"}])
tgt_client = FakeClient(users=[{"id": "70", "email": "alice@x.com"}])
src_calls = []
orig = src_client.list_users
src_client.list_users = lambda: (src_calls.append(1), orig())[1]
ctx = _ctx(src_client, tgt_client)
share = {"shared_users": [7], "shared_groups": [], "shared_to_org": False}
_apply(ctx, dict(share))
_apply(ctx, dict(share))
assert len(src_calls) == 1 # memoised per run, not per resource
assert len(tgt_client.share_posts) == 2
def test_cached_failure_does_not_shadow_a_raced_success():
# A peer thread committing _FETCH_FAILED mid-build must not shadow our
# success, else share replication silently no-ops for the rest of the run.
ctx = _ctx(FakeClient(), FakeClient())
def build_while_peer_fails():
ctx.share_cache["k"] = _FETCH_FAILED # racing thread commits first
return {"ok": 1}
assert _cached(ctx, "k", build_while_peer_fails) == {"ok": 1}
assert ctx.share_cache["k"] == {"ok": 1}
def test_cached_success_is_not_overwritten_by_a_raced_failure():
ctx = _ctx(FakeClient(), FakeClient())
def build_fails_while_peer_succeeds():
ctx.share_cache["k"] = {"ok": 1} # racing thread commits success first
raise RuntimeError("down")
assert _cached(ctx, "k", build_fails_while_peer_succeeds) == {"ok": 1}
def test_share_post_failure_lands_in_errors():
src_client = FakeClient(users=[{"id": "7", "email": "alice@x.com"}])
tgt_client = FakeClient(users=[{"id": "70", "email": "alice@x.com"}])
def boom(share_path, payload):
raise RuntimeError("503")
tgt_client.share_resource = boom
ctx = _ctx(src_client, tgt_client)
result = _apply(
ctx, {"shared_users": [7], "shared_groups": [], "shared_to_org": False}
)
assert result.failed == 1
assert any("share adapter 'demo'" in e for e in result.errors)
def test_server_managed_still_strips_shared_users_on_create():
src = {
"name": "demo",
"shared_users": [1, 2],
"shared_to_org": True,
}
# Even a (hypothetically) writable shared_users never rides the POST.
payload = build_post_payload(src, frozenset({"name", "shared_users"}))
assert payload == {"name": "demo"}