| 1 |
import pytest |
| 2 |
import responses |
| 3 |
|
| 4 |
import globus_sdk |
| 5 |
from globus_sdk.testing import get_last_request, load_response |
| 6 |
|
| 7 |
IDENTITIES_MULTIPLE_RESPONSE = { |
| 8 |
"identities": [ |
| 9 |
{ |
| 10 |
"email": None, |
| 11 |
"id": "46bd0f56-e24f-11e5-a510-131bef46955c", |
| 12 |
"identity_provider": "927d7238-f917-4eb2-9ace-c523fa9ba34e", |
| 13 |
"name": None, |
| 14 |
"organization": None, |
| 15 |
"status": "unused", |
| 16 |
"username": "[email protected]", |
| 17 |
}, |
| 18 |
{ |
| 19 |
"email": "[email protected]", |
| 20 |
"id": "ae341a98-d274-11e5-b888-dbae3a8ba545", |
| 21 |
"identity_provider": "927d7238-f917-4eb2-9ace-c523fa9ba34e", |
| 22 |
"name": "Stephen Rosen", |
| 23 |
"organization": "Globus Team", |
| 24 |
"status": "used", |
| 25 |
"username": "[email protected]", |
| 26 |
}, |
| 27 |
] |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
def test_identity_map(service_client): |
| 32 |
meta = load_response(service_client.get_identities, case="sirosen").metadata |
| 33 |
idmap = globus_sdk.IdentityMap(service_client, [meta["username"]]) |
| 34 |
assert idmap[meta["username"]]["organization"] == meta["org"] |
| 35 |
|
| 36 |
# lookup by ID also works |
| 37 |
assert idmap[meta["id"]]["organization"] == meta["org"] |
| 38 |
|
| 39 |
# the last (only) API call was the one by username |
| 40 |
last_req = get_last_request() |
| 41 |
assert "ids" not in last_req.params |
| 42 |
assert last_req.params == {"usernames": meta["username"], "provision": "false"} |
| 43 |
|
| 44 |
|
| 45 |
def test_identity_map_initialization_no_values(service_client): |
| 46 |
idmap = globus_sdk.IdentityMap(service_client) |
| 47 |
assert idmap.unresolved_ids == set() |
| 48 |
assert idmap.unresolved_usernames == set() |
| 49 |
|
| 50 |
|
| 51 |
def test_identity_map_initialization_mixed_and_duplicate_values(service_client): |
| 52 |
# splits things up and deduplicates values into sets |
| 53 |
idmap = globus_sdk.IdentityMap( |
| 54 |
service_client, |
| 55 |
[ |
| 56 |
"[email protected]", |
| 57 |
"ae341a98-d274-11e5-b888-dbae3a8ba545", |
| 58 |
"[email protected]", |
| 59 |
"[email protected]", |
| 60 |
"[email protected]", |
| 61 |
"ae341a98-d274-11e5-b888-dbae3a8ba545", |
| 62 |
"[email protected]", |
| 63 |
"ae341a98-d274-11e5-b888-dbae3a8ba545", |
| 64 |
], |
| 65 |
) |
| 66 |
assert idmap.unresolved_ids == {"ae341a98-d274-11e5-b888-dbae3a8ba545"} |
| 67 |
assert idmap.unresolved_usernames == {"[email protected]", "[email protected]"} |
| 68 |
|
| 69 |
|
| 70 |
def test_identity_map_initialization_batch_size(service_client): |
| 71 |
idmap = globus_sdk.IdentityMap(service_client, id_batch_size=10) |
| 72 |
assert idmap.unresolved_ids == set() |
| 73 |
assert idmap.unresolved_usernames == set() |
| 74 |
assert idmap.id_batch_size == 10 |
| 75 |
|
| 76 |
|
| 77 |
def test_identity_map_add(service_client): |
| 78 |
idmap = globus_sdk.IdentityMap(service_client) |
| 79 |
assert idmap.add("[email protected]") is True |
| 80 |
assert idmap.add("[email protected]") is False |
| 81 |
assert idmap.add("46bd0f56-e24f-11e5-a510-131bef46955c") is True |
| 82 |
assert idmap.add("46bd0f56-e24f-11e5-a510-131bef46955c") is False |
| 83 |
|
| 84 |
|
| 85 |
def test_identity_map_add_after_lookup(service_client): |
| 86 |
meta = load_response(service_client.get_identities, case="sirosen").metadata |
| 87 |
idmap = globus_sdk.IdentityMap(service_client) |
| 88 |
x = idmap[meta["username"]]["id"] |
| 89 |
# this is the key: adding it will indicate that we've already seen this ID, perhaps |
| 90 |
# "unintuitively", and that's part of the value of `add()` returning a boolean value |
| 91 |
assert idmap.add(x) is False |
| 92 |
assert idmap[x] == idmap[meta["username"]] |
| 93 |
|
| 94 |
|
| 95 |
def test_identity_map_multiple(service_client): |
| 96 |
meta = load_response(service_client.get_identities, case="multiple").metadata |
| 97 |
idmap = globus_sdk.IdentityMap( |
| 98 |
service_client, ["[email protected]", "[email protected]"] |
| 99 |
) |
| 100 |
assert idmap["[email protected]"]["organization"] == "Globus Team" |
| 101 |
assert idmap["[email protected]"]["organization"] is None |
| 102 |
|
| 103 |
last_req = get_last_request() |
| 104 |
# order doesn't matter, but it should be just these two |
| 105 |
# if IdentityMap doesn't deduplicate correctly, it could send |
| 106 |
# `[email protected],[email protected],[email protected]` on the first lookup |
| 107 |
assert last_req.params["usernames"].split(",") in [ |
| 108 |
meta["usernames"], |
| 109 |
meta["usernames"][::-1], |
| 110 |
] |
| 111 |
assert last_req.params["provision"] == "false" |
| 112 |
|
| 113 |
|
| 114 |
def test_identity_map_keyerror(service_client): |
| 115 |
load_response(service_client.get_identities, case="sirosen") |
| 116 |
idmap = globus_sdk.IdentityMap(service_client) |
| 117 |
# a name which doesn't come back, indicating that it was not found, will KeyError |
| 118 |
with pytest.raises(KeyError): |
| 119 |
idmap["[email protected]"] |
| 120 |
|
| 121 |
last_req = get_last_request() |
| 122 |
assert last_req.params == {"usernames": "[email protected]", "provision": "false"} |
| 123 |
|
| 124 |
|
| 125 |
def test_identity_map_get_with_default(service_client): |
| 126 |
load_response(service_client.get_identities, case="sirosen") |
| 127 |
magic = object() # sentinel value |
| 128 |
idmap = globus_sdk.IdentityMap(service_client) |
| 129 |
# a name which doesn't come back, if looked up with `get()` should return the |
| 130 |
# default |
| 131 |
assert idmap.get("[email protected]", magic) is magic |
| 132 |
|
| 133 |
|
| 134 |
def test_identity_map_del(service_client): |
| 135 |
meta = load_response(service_client.get_identities).metadata |
| 136 |
idmap = globus_sdk.IdentityMap(service_client) |
| 137 |
identity_id = idmap[meta["username"]]["id"] |
| 138 |
del idmap[identity_id] |
| 139 |
assert idmap.get(meta["username"])["id"] == identity_id |
| 140 |
# we've only made one request so far |
| 141 |
assert len(responses.calls) == 1 |
| 142 |
# but a lookup by ID after a del is going to trigger another request because we've |
| 143 |
# invalidated the cached ID data and are asking the IDMap to look it up again |
| 144 |
assert idmap.get(identity_id)["username"] == meta["username"] |
| 145 |
assert len(responses.calls) == 2 |
| 146 |
|
| 147 |
|
| 148 |
@pytest.mark.parametrize( |
| 149 |
"lookup1,lookup2", |
| 150 |
[ |
| 151 |
("username", "username"), |
| 152 |
("username", "id"), |
| 153 |
("id", "username"), |
| 154 |
], |
| 155 |
) |
| 156 |
@pytest.mark.parametrize("initial_add", [True, False]) |
| 157 |
def test_identity_map_shared_cache_match(service_client, initial_add, lookup1, lookup2): |
| 158 |
meta = load_response(service_client.get_identities, case="sirosen").metadata |
| 159 |
lookup1, lookup2 = meta[lookup1], meta[lookup2] |
| 160 |
cache = {} |
| 161 |
idmap1 = globus_sdk.IdentityMap(service_client, cache=cache) |
| 162 |
idmap2 = globus_sdk.IdentityMap(service_client, cache=cache) |
| 163 |
if initial_add: |
| 164 |
idmap1.add(lookup1) |
| 165 |
idmap2.add(lookup2) |
| 166 |
# no requests yet... |
| 167 |
assert len(responses.calls) == 0 |
| 168 |
# do the first lookup, it should make one request |
| 169 |
assert idmap1[lookup1]["organization"] == meta["org"] |
| 170 |
assert len(responses.calls) == 1 |
| 171 |
# lookup more values and make sure that "everything matches" |
| 172 |
assert idmap2[lookup2]["organization"] == meta["org"] |
| 173 |
assert idmap1[lookup1]["id"] == idmap2[lookup2]["id"] |
| 174 |
assert idmap1[lookup2]["id"] == idmap2[lookup1]["id"] |
| 175 |
# we've only made one request, because the shared cache captured this info on the |
| 176 |
# very first call |
| 177 |
assert len(responses.calls) == 1 |
| 178 |
|
| 179 |
|
| 180 |
@pytest.mark.parametrize( |
| 181 |
"lookup_style", |
| 182 |
["usernames", "ids"], |
| 183 |
) |
| 184 |
def test_identity_map_shared_cache_mismatch(service_client, lookup_style): |
| 185 |
meta = load_response(service_client.get_identities, case="multiple").metadata |
| 186 |
lookup1, lookup2 = meta[lookup_style] |
| 187 |
cache = {} |
| 188 |
idmap1 = globus_sdk.IdentityMap(service_client, [lookup1, lookup2], cache=cache) |
| 189 |
idmap2 = globus_sdk.IdentityMap(service_client, cache=cache) |
| 190 |
# no requests yet... |
| 191 |
assert len(responses.calls) == 0 |
| 192 |
# do the first lookup, it should make one request |
| 193 |
assert lookup1 in (idmap1[lookup1]["id"], idmap1[lookup1]["username"]) |
| 194 |
assert len(responses.calls) == 1 |
| 195 |
# lookup more values and make sure that "everything matches" |
| 196 |
assert lookup2 in (idmap2[lookup2]["id"], idmap2[lookup2]["username"]) |
| 197 |
assert idmap1[lookup1]["id"] == idmap2[lookup1]["id"] |
| 198 |
assert idmap1[lookup2]["id"] == idmap2[lookup2]["id"] |
| 199 |
# we've only made one request, because the shared cache captured this info on the |
| 200 |
# very first call |
| 201 |
assert len(responses.calls) == 1 |
| 202 |
|
| 203 |
|
| 204 |
def test_identity_map_prepopulated_cache(service_client): |
| 205 |
meta = load_response(service_client.get_identities).metadata |
| 206 |
|
| 207 |
# populate the cache, even with nulls it should stop any lookups from happening |
| 208 |
cache = {meta["id"]: None, meta["username"]: None} |
| 209 |
idmap = globus_sdk.IdentityMap(service_client, cache=cache) |
| 210 |
# no requests yet... |
| 211 |
assert len(responses.calls) == 0 |
| 212 |
# do the lookups |
| 213 |
assert idmap[meta["id"]] is None |
| 214 |
assert idmap[meta["username"]] is None |
| 215 |
# still no calls made |
| 216 |
assert len(responses.calls) == 0 |
| 217 |
|
| 218 |
|
| 219 |
def test_identity_map_batch_limit(service_client): |
| 220 |
meta1 = load_response(service_client.get_identities).metadata |
| 221 |
meta2 = load_response(service_client.get_identities, case="sirosen").metadata |
| 222 |
|
| 223 |
# setup the ID map with a size limit of 1 |
| 224 |
idmap = globus_sdk.IdentityMap(service_client, id_batch_size=1) |
| 225 |
idmap.add(meta2["id"]) |
| 226 |
idmap.add(meta1["id"]) |
| 227 |
|
| 228 |
# no requests yet... |
| 229 |
assert len(responses.calls) == 0 |
| 230 |
|
| 231 |
# do the first lookup, using the second ID to be added |
| 232 |
# only one call should be made |
| 233 |
assert idmap[meta1["id"]]["username"] == meta1["username"] |
| 234 |
assert len(responses.calls) == 1 |
| 235 |
# 1 ID left unresolved |
| 236 |
assert len(idmap.unresolved_ids) == 1 |
| 237 |
# the last (only) API call was by ID with one ID |
| 238 |
last_req = get_last_request() |
| 239 |
assert "usernames" not in last_req.params |
| 240 |
assert last_req.params == {"ids": meta1["id"]} |
| 241 |
|
| 242 |
# second lookup works as well |
| 243 |
assert idmap[meta2["id"]]["username"] == meta2["username"] |
| 244 |
assert len(responses.calls) == 2 |
| 245 |
# no IDs left unresolved |
| 246 |
assert len(idmap.unresolved_ids) == 0 |
| 247 |
# the last API call was by ID with one ID |
| 248 |
last_req = get_last_request() |
| 249 |
assert "usernames" not in last_req.params |
| 250 |
assert last_req.params == {"ids": meta2["id"]} |