summaryrefslogtreecommitdiffstats
path: root/python/tests/api_entity_state.py
blob: 4b49083ec117ab40b4da2b9eea80756d7eeebb46 (plain)
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470

import pytest

from fatcat_openapi_client import *
from fatcat_openapi_client.rest import ApiException
from fixtures import *


def test_redirect_entity(api):
    """
    Create two creators; merge
        => get both by ident
        => lookup by orcid; should not get old/merged one
        => update first; check that get on second by ident returns updated record
        => split second back out and re-get by ident/orcid
    """

    offset = 0
    while True:
        offset += 1
        o1 = '0000-0000-1111-%04d' % offset
        o2 = '0000-0000-2222-%04d' % offset
        try:
            api.lookup_creator(orcid=o1)
            continue
        except ApiException:
            pass
        try:
            api.lookup_creator(orcid=o2)
            continue
        except ApiException:
            pass
        break

    c1 = CreatorEntity(display_name="test one", orcid=o1)
    c2 = CreatorEntity(display_name="test two", orcid=o2)

    # create two creators
    eg = quick_eg(api)
    c1_edit = api.create_creator(eg.editgroup_id, c1)
    c2_edit = api.create_creator(eg.editgroup_id, c2)
    c1 = api.get_creator(c1_edit.ident)
    c2 = api.get_creator(c2_edit.ident)
    assert c1_edit.prev_revision is None
    assert c2_edit.prev_revision is None
    api.accept_editgroup(eg.editgroup_id)
    redirs = api.get_creator_redirects(c1.ident)
    assert redirs == []

    # merge second into first
    c2_redirect = CreatorEntity(redirect=c1.ident)
    eg = quick_eg(api)
    merge_edit = api.update_creator(eg.editgroup_id, c2.ident, c2_redirect)
    assert merge_edit.prev_revision == c2.revision
    redirs = api.get_creator_redirects(c1.ident)
    assert redirs == []
    api.accept_editgroup(eg.editgroup_id)
    redirs = api.get_creator_redirects(c1.ident)
    assert redirs == [c2.ident, ]

    # get both by ident
    res = api.get_creator(c1.ident)
    assert res.state == "active"
    res = api.get_creator(c2.ident)
    assert res.state == "redirect"
    assert res.revision == c1.revision
    assert res.redirect == c1.ident
    assert res.display_name == "test one"

    # get by orcid
    res = api.lookup_creator(orcid=o1)
    assert res.ident == c1.ident
    with pytest.raises(fatcat_openapi_client.rest.ApiException):
        res = api.lookup_creator(orcid=o2)

    # update first; check that get on second updates
    c1.display_name = "test one one"
    eg = quick_eg(api)
    update_edit = api.update_creator(eg.editgroup_id, c1.ident, c1)
    assert update_edit.prev_revision == c1.revision
    api.accept_editgroup(eg.editgroup_id)
    res = api.get_creator(c2.ident)
    assert res.state == "redirect"
    assert res.display_name == "test one one"

    # delete first; check that second is deleted (but state is redirect)
    eg = quick_eg(api)
    del_edit = api.delete_creator(eg.editgroup_id, c1.ident)
    assert del_edit.prev_revision == update_edit.revision
    api.accept_editgroup(eg.editgroup_id)
    res = api.get_creator(c1.ident)
    assert res.state == "deleted"
    assert res.display_name is None
    res = api.get_creator(c2.ident)
    assert res.state == "redirect"
    assert res.display_name is None
    assert res.revision is None
    redirs = api.get_creator_redirects(c1.ident)
    assert redirs == [c2.ident, ]

    # undelete first; check that second is a redirect
    eg = quick_eg(api)
    undelete_edit = api.update_creator(eg.editgroup_id, c1.ident, c1)
    assert undelete_edit.prev_revision is None
    api.accept_editgroup(eg.editgroup_id)
    res = api.get_creator(c2.ident)
    assert res.state == "redirect"
    assert res.display_name == "test one one"
    redirs = api.get_creator_redirects(c1.ident)
    assert redirs == [c2.ident, ]

    # split second entity back out
    assert c2.revision
    assert c2.redirect is None
    eg = quick_eg(api)
    update_edit = api.update_creator(eg.editgroup_id, c2.ident, c2)
    # prev_revision should be none after an un-redirect
    assert update_edit.prev_revision is None
    redirs = api.get_creator_redirects(c1.ident)
    assert redirs == [c2.ident, ]
    api.accept_editgroup(eg.editgroup_id)
    res = api.get_creator(c2.ident)
    assert res.state == "active"
    assert res.display_name == "test two"
    res = api.lookup_creator(orcid=o2)
    assert res.display_name == "test two"
    redirs = api.get_creator_redirects(c1.ident)
    assert redirs == []

    # cleanup
    eg = quick_eg(api)
    api.delete_creator(eg.editgroup_id, c1.ident)
    api.delete_creator(eg.editgroup_id, c2.ident)
    api.accept_editgroup(eg.editgroup_id)


def test_delete_entity(api):

    offset = 0
    while True:
        offset += 1
        o1 = '0000-0000-1111-%04d' % offset
        try:
            api.lookup_creator(orcid=o1)
            continue
        except ApiException:
            pass
        break

    c1 = CreatorEntity(display_name="test deletable", orcid=o1)

    # create
    eg = quick_eg(api)
    c1 = api.get_creator(api.create_creator(eg.editgroup_id, c1).ident)
    api.accept_editgroup(eg.editgroup_id)
    res = api.get_creator(c1.ident)
    assert res.state == "active"
    assert res.display_name == "test deletable"
    res = api.lookup_creator(orcid=c1.orcid)
    assert res.state == "active"
    assert res.display_name == "test deletable"

    # delete
    eg = quick_eg(api)
    api.delete_creator(eg.editgroup_id, c1.ident)
    with pytest.raises(fatcat_openapi_client.rest.ApiException):
        # can't re-delete in same editgroup
        api.delete_creator(eg.editgroup_id, c1.ident)
    api.accept_editgroup(eg.editgroup_id)
    res = api.get_creator(c1.ident)
    assert res.state == "deleted"
    assert res.display_name is None
    with pytest.raises(fatcat_openapi_client.rest.ApiException):
        res = api.lookup_creator(orcid=c1.orcid)

    # re-delete
    eg = quick_eg(api)
    try:
        # can't re-delete an entity
        api.delete_creator(eg.editgroup_id, c1.ident)
        #api.accept_editgroup(eg.editgroup_id)
        assert False
    except fatcat_openapi_client.rest.ApiException as e:
        assert 400 <= e.status < 500 # error is 4xx

    # undelete
    eg = quick_eg(api)
    api.update_creator(eg.editgroup_id, c1.ident, c1)
    api.accept_editgroup(eg.editgroup_id)
    res = api.get_creator(c1.ident)
    assert res.state == "active"
    assert res.display_name == "test deletable"
    res = api.lookup_creator(orcid=c1.orcid)
    assert res.state == "active"
    assert res.display_name == "test deletable"

    # cleanup
    eg = quick_eg(api)
    api.delete_creator(eg.editgroup_id, c1.ident)
    api.accept_editgroup(eg.editgroup_id)


def test_recursive_redirects_entity(api):

    offset = 0
    while True:
        offset += 1
        o1 = '0000-0000-1111-%04d' % offset
        o2 = '0000-0000-2222-%04d' % offset
        o3 = '0000-0000-3333-%04d' % offset
        try:
            api.lookup_creator(orcid=o1)
            continue
        except ApiException:
            pass
        try:
            api.lookup_creator(orcid=o2)
            continue
        except ApiException:
            pass
        try:
            api.lookup_creator(orcid=o3)
            continue
        except ApiException:
            pass
        break

    c1 = CreatorEntity(display_name="test one", orcid=o1)
    c2 = CreatorEntity(display_name="test two", orcid=o2)
    c3 = CreatorEntity(display_name="test three", orcid=o3)

    # create three creators
    eg = quick_eg(api)
    c1 = api.get_creator(api.create_creator(eg.editgroup_id, c1).ident)
    c2 = api.get_creator(api.create_creator(eg.editgroup_id, c2).ident)
    c3 = api.get_creator(api.create_creator(eg.editgroup_id, c3).ident)
    api.accept_editgroup(eg.editgroup_id)
    res = api.get_creator(c3.ident)
    assert res.display_name == "test three"

    # redirect third to second
    c3_redirect = CreatorEntity(redirect=c2.ident)
    eg = quick_eg(api)
    api.update_creator(eg.editgroup_id, c3.ident, c3_redirect)
    api.accept_editgroup(eg.editgroup_id)
    res = api.get_creator(c3.ident)
    assert res.display_name == "test two"

    # redirect second to first: should be an error at merge time
    c2_redirect = CreatorEntity(redirect=c1.ident)
    eg = quick_eg(api)
    api.update_creator(eg.editgroup_id, c2.ident, c2_redirect)
    with pytest.raises(fatcat_openapi_client.rest.ApiException):
        api.accept_editgroup(eg.editgroup_id)
    res = api.get_creator(c2.ident)
    assert res.display_name == "test two"
    with pytest.raises(fatcat_openapi_client.rest.ApiException):
        res = api.lookup_creator(orcid=o3)
    res = api.lookup_creator(orcid=o2)
    assert res.ident == c2.ident

    # redirect first to third: should be an error at merge time
    c1_redirect = CreatorEntity(redirect=c3.ident)
    eg = quick_eg(api)
    api.update_creator(eg.editgroup_id, c1.ident, c1_redirect)
    with pytest.raises(fatcat_openapi_client.rest.ApiException):
        api.accept_editgroup(eg.editgroup_id)
    res = api.get_creator(c1.ident)
    assert res.display_name == "test one"

    # update second; check that third updated
    c2.display_name = "test two updated"
    eg = quick_eg(api)
    api.update_creator(eg.editgroup_id, c2.ident, c2)
    api.accept_editgroup(eg.editgroup_id)
    res = api.get_creator(c2.ident)
    c2 = res
    assert res.display_name == "test two updated"
    res = api.get_creator(c3.ident)
    assert res.display_name == "test two updated"
    assert res.state == "redirect"
    res = api.lookup_creator(orcid=o2)
    assert res.ident == c2.ident

    # delete second; check that third updated
    eg = quick_eg(api)
    api.delete_creator(eg.editgroup_id, c2.ident)
    api.accept_editgroup(eg.editgroup_id)
    res = api.get_creator(c2.ident)
    assert res.state == "deleted"
    res = api.get_creator(c3.ident)
    assert res.state == "redirect"
    assert res.display_name is None
    with pytest.raises(fatcat_openapi_client.rest.ApiException):
        res = api.lookup_creator(orcid=o2)

    # undelete second; check that third updated
    eg = quick_eg(api)
    c2_undelete = CreatorEntity(revision=c2.revision)
    api.update_creator(eg.editgroup_id, c2.ident, c2_undelete)
    api.accept_editgroup(eg.editgroup_id)
    res = api.get_creator(c2.ident)
    assert res.state == "active"
    assert res.display_name == "test two updated"
    res = api.get_creator(c3.ident)
    assert res.state == "redirect"
    assert res.display_name == "test two updated"

    # delete third (a redirect)
    eg = quick_eg(api)
    api.delete_creator(eg.editgroup_id, c3.ident)
    api.accept_editgroup(eg.editgroup_id)
    res = api.get_creator(c3.ident)
    assert res.state == "deleted"
    assert res.display_name is None

    # re-redirect third
    eg = quick_eg(api)
    api.update_creator(eg.editgroup_id, c3.ident, c3_redirect)
    api.accept_editgroup(eg.editgroup_id)
    res = api.get_creator(c3.ident)
    assert res.state == "redirect"
    assert res.display_name == "test two updated"

    # delete second, then delete third
    eg = quick_eg(api)
    api.delete_creator(eg.editgroup_id, c2.ident)
    api.accept_editgroup(eg.editgroup_id)
    res = api.get_creator(c3.ident)
    assert res.state == "redirect"
    assert res.display_name is None
    with pytest.raises(fatcat_openapi_client.rest.ApiException):
        res = api.lookup_creator(orcid=o2)
    eg = quick_eg(api)
    api.delete_creator(eg.editgroup_id, c3.ident)
    api.accept_editgroup(eg.editgroup_id)
    res = api.get_creator(c3.ident)
    assert res.state == "deleted"
    assert res.display_name is None

    # cleanup
    eg = quick_eg(api)
    api.delete_creator(eg.editgroup_id, c1.ident)
    # c2 already deleted
    # c3 already deleted
    api.accept_editgroup(eg.editgroup_id)


def test_self_redirect(api):

    c1 = CreatorEntity(display_name="test self-redirect")

    # create creator
    eg = quick_eg(api)
    c1 = api.get_creator(api.create_creator(eg.editgroup_id, c1).ident)
    api.accept_editgroup(eg.editgroup_id)

    # redirect first to itself; should error on PUT
    c1_redirect = CreatorEntity(redirect=c1.ident)
    eg = quick_eg(api)
    with pytest.raises(fatcat_openapi_client.rest.ApiException):
        api.update_creator(eg.editgroup_id, c1.ident, c1_redirect)


def test_wip_redirect(api):

    # create first
    c1 = CreatorEntity(display_name="test one")
    eg = quick_eg(api)
    c1 = api.get_creator(api.create_creator(eg.editgroup_id, c1).ident)
    api.accept_editgroup(eg.editgroup_id)

    # start creating second entity
    c2 = CreatorEntity(display_name="test two")
    eg = quick_eg(api)
    c2 = api.get_creator(api.create_creator(eg.editgroup_id, c2).ident)
    assert c2.state == "wip"

    # redirect first to second (should fail)
    eg = quick_eg(api)
    c1_redirect = CreatorEntity(redirect=c2.ident)
    try:
        api.update_creator(eg.editgroup_id, c1.ident, c1_redirect)
        assert False
    except fatcat_openapi_client.rest.ApiException as e:
        assert 400 <= e.status < 500
        assert "WIP" in e.body


def test_create_redirect(api):

    # create first
    c1 = CreatorEntity(display_name="test one")
    eg = quick_eg(api)
    c1 = api.get_creator(api.create_creator(eg.editgroup_id, c1).ident)
    api.accept_editgroup(eg.editgroup_id)

    # create second
    c2 = CreatorEntity(display_name="blah", redirect=c1.ident)
    eg = quick_eg(api)
    try:
        api.create_creator(eg.editgroup_id, c2)
        assert False
    except fatcat_openapi_client.rest.ApiException as e:
        assert 400 <= e.status < 500
        assert "redirect" in e.body

    # again with releases
    r1 = ReleaseEntity(title="test one", ext_ids=ReleaseExtIds())
    eg = quick_eg(api)
    r1 = api.get_release(api.create_release(eg.editgroup_id, r1).ident)
    api.accept_editgroup(eg.editgroup_id)
    r2 = ReleaseEntity(title="blah", redirect=c1.ident, ext_ids=ReleaseExtIds())
    eg = quick_eg(api)
    try:
        api.create_release(eg.editgroup_id, r2)
        assert False
    except fatcat_openapi_client.rest.ApiException as e:
        assert 400 <= e.status < 500
        assert "redirect" in e.body


def test_required_entity_fields(api):
    eg = quick_eg(api)

    # Creator
    try:
        c1 = CreatorEntity()
        api.create_creator(eg.editgroup_id, c1)
        assert False
    except fatcat_openapi_client.rest.ApiException as e:
        assert 400 <= e.status < 500
        assert "display_name" in e.body

    # Container
    try:
        c1 = ContainerEntity()
        api.create_container(eg.editgroup_id, c1)
        assert False
    except fatcat_openapi_client.rest.ApiException as e:
        assert 400 <= e.status < 500
        assert "name" in e.body

    # Release
    try:
        c1 = ReleaseEntity(ext_ids=ReleaseExtIds())
        api.create_release(eg.editgroup_id, c1)
        assert False
    except fatcat_openapi_client.rest.ApiException as e:
        assert 400 <= e.status < 500
        assert "title" in e.body

def test_revert_current_status(api):

    c1 = CreatorEntity(display_name="test updates")

    # create
    eg = quick_eg(api)
    c1 = api.get_creator(api.create_creator(eg.editgroup_id, c1).ident)
    api.accept_editgroup(eg.editgroup_id)

    # try to "revert" to current revision
    eg = quick_eg(api)
    c1_revert = CreatorEntity(revision=c1.revision)
    try:
        api.update_creator(eg.editgroup_id, c1.ident, c1_revert)
        assert False
    except fatcat_openapi_client.rest.ApiException as e:
        assert 400 <= e.status < 500
        assert "current" in e.body