aboutsummaryrefslogtreecommitdiffstats
path: root/python/tests/subentity_state.py
blob: 2843176d1a4b28879afd8a524357b48cbae24136 (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

import json

from fatcat_openapi_client import *
from fixtures import *

"""
This file has API and webface tests for when an entity references another
entity which is deleted/redirected/wip. For example, a release points to a
container (via container_id) which is deleted.

Current set of such references:

    => release -> collection
    => release -> creator
    => release -> file
    => file -> release
    => release -> work
    => work -> release
"""

def test_relation_states(api, app, mocker):

    j1 = ContainerEntity(name="test journal")
    j2 = ContainerEntity(name="another test journal")
    c1 = CreatorEntity(display_name="test person")
    r1 = ReleaseEntity(title="test article", ext_ids=ReleaseExtIds())
    r2 = ReleaseEntity(title="another test article", ext_ids=ReleaseExtIds())
    f1 = FileEntity(md5="c60f5b093ef5e6caad9d7b45268be409")
    f2 = FileEntity(md5="0000000000000000ad9d7b45268be409")

    # WIP container
    # these are basic ES stats for the container view pages
    es_raw = mocker.patch('elasticsearch.connection.Urllib3HttpConnection.perform_request')
    es_raw.side_effect = [
        (200, {}, json.dumps(ES_CONTAINER_STATS_RESP)),
        (200, {}, json.dumps(ES_CONTAINER_RANDOM_RESP)),
    ]
    eg = quick_eg(api)
    j2 = api.get_container(api.create_container(eg.editgroup_id, j2).ident)
    rv = app.get('/container/{}'.format(j2.ident))
    assert rv.status_code == 200

    # create inter-related entities
    eg = quick_eg(api)
    j1 = api.get_container(api.create_container(eg.editgroup_id, j1).ident)
    c1 = api.get_creator(api.create_creator(eg.editgroup_id, c1).ident)
    r1.container_id = j1.ident
    r1.contribs = [ReleaseContrib(creator_id=c1.ident)]
    r1 = api.get_release(api.create_release(eg.editgroup_id, r1).ident)
    r2 = api.get_release(api.create_release(eg.editgroup_id, r2).ident)
    f1.release_ids = [r1.ident]
    f1 = api.get_file(api.create_file(eg.editgroup_id, f1).ident)
    api.accept_editgroup(eg.editgroup_id)

    r1 = api.get_release(r1.ident, expand="container,creators,files")
    assert r1.container.name == "test journal"
    assert r1.files[0].md5 == "c60f5b093ef5e6caad9d7b45268be409"
    assert r1.contribs[0].creator_id == c1.ident
    assert r1.contribs[0].creator.display_name == "test person"
    assert r1.contribs[0].creator.state == "active"
    rv = app.get('/release/{}'.format(r1.ident))
    assert rv.status_code == 200

    # delete creator
    eg = quick_eg(api)
    api.delete_creator(eg.editgroup_id, c1.ident)
    api.accept_editgroup(eg.editgroup_id)
    rv = app.get('/creator/{}'.format(c1.ident))
    assert rv.status_code == 200 # TODO: HTTP status "Gone"?

    c1_deleted = api.get_creator(c1.ident)
    assert c1_deleted.state == "deleted"
    assert c1_deleted.display_name is None

    r1 = api.get_release(r1.ident, expand="container,creators,files")
    assert r1.container.name == "test journal"
    assert r1.files[0].md5 == "c60f5b093ef5e6caad9d7b45268be409"
    assert r1.contribs[0].creator_id == c1.ident
    assert r1.contribs[0].creator.display_name is None
    assert r1.contribs[0].creator.state == "deleted"
    rv = app.get('/release/{}'.format(r1.ident))
    assert rv.status_code == 200

    # wip container
    eg = quick_eg(api)
    r1.container_id = j2.ident
    api.update_release(eg.editgroup_id, r1.ident, r1)
    api.accept_editgroup(eg.editgroup_id)

    r1 = api.get_release(r1.ident, expand="container,creators,files")
    assert r1.container_id == j2.ident
    assert r1.container.name == "another test journal"
    assert r1.container.state == "wip"
    assert r1.files[0].md5 == "c60f5b093ef5e6caad9d7b45268be409"
    assert r1.contribs[0].creator_id == c1.ident
    assert r1.contribs[0].creator.display_name is None
    assert r1.contribs[0].creator.state == "deleted"
    rv = app.get('/release/{}'.format(r1.ident))
    assert rv.status_code == 200

    # redirect release
    r2 = api.get_release(r2.ident, expand="container,creators,files")
    assert r2.files == []
    eg = quick_eg(api)
    api.update_release(eg.editgroup_id, r2.ident, ReleaseEntity(redirect=r1.ident, ext_ids=ReleaseExtIds()))
    f2.release_ids = [r2.ident]
    f2 = api.get_file(api.create_file(eg.editgroup_id, f2).ident)
    api.accept_editgroup(eg.editgroup_id)
    r2 = api.get_release(r2.ident, expand="container,creators,files")
    assert r2.container_id == j2.ident
    assert r2.container.name == "another test journal"
    assert r2.container.state == "wip"
    # fetching for *target*; tricky!
    assert r2.files[0].md5 == "c60f5b093ef5e6caad9d7b45268be409"
    assert r2.contribs[0].creator_id == c1.ident
    assert r2.contribs[0].creator.display_name is None
    assert r2.contribs[0].creator.state == "deleted"
    rv = app.get('/release/{}'.format(r2.ident))
    assert rv.status_code == 302
    rv = app.get('/file/{}'.format(f2.ident))
    assert rv.status_code == 200

    # delete release
    eg = quick_eg(api)
    api.delete_release(eg.editgroup_id, r2.ident)
    api.accept_editgroup(eg.editgroup_id)
    r2 = api.get_release(r2.ident, expand="container,creators,files")
    assert r2.container_id is None
    assert r2.container is None
    assert r2.files is None
    assert r2.contribs is None
    rv = app.get('/release/{}'.format(r2.ident))
    assert rv.status_code == 200 # TODO: HTTP Gone?
    rv = app.get('/file/{}'.format(f2.ident))
    print(rv.data)
    assert rv.status_code == 200


def test_app_entity_states(api, app, mocker):

    # these are basic ES stats for the container view pages
    es_raw = mocker.patch('elasticsearch.connection.Urllib3HttpConnection.perform_request')
    es_raw.side_effect = [
        (200, {}, json.dumps(ES_CONTAINER_STATS_RESP)),
        (200, {}, json.dumps(ES_CONTAINER_RANDOM_RESP)),
    ]

    j1 = ContainerEntity(name="test journal")
    j2 = ContainerEntity(name="another test journal")
    c1 = CreatorEntity(display_name="test person")
    c2 = CreatorEntity(display_name="another test person")
    r1 = ReleaseEntity(title="test article", ext_ids=ReleaseExtIds())
    r2 = ReleaseEntity(title="another test article", ext_ids=ReleaseExtIds())
    f1 = FileEntity(md5="c60f5b093ef5e6caad9d7b45268be409")
    f2 = FileEntity(md5="0000000000000000ad9d7b45268be409")

    # create inter-related entities
    eg = quick_eg(api)
    j1 = api.get_container(api.create_container(eg.editgroup_id, j1).ident)
    j2 = api.get_container(api.create_container(eg.editgroup_id, j2).ident)
    c1 = api.get_creator(api.create_creator(eg.editgroup_id, c1).ident)
    c2 = api.get_creator(api.create_creator(eg.editgroup_id, c2).ident)
    r1.container_id = j1.ident
    r1.contribs = [ReleaseContrib(creator_id=c1.ident)]
    r1 = api.get_release(api.create_release(eg.editgroup_id, r1).ident)
    r2 = api.get_release(api.create_release(eg.editgroup_id, r2).ident)
    f1.release_ids = [r1.ident]
    f1 = api.get_file(api.create_file(eg.editgroup_id, f1).ident)
    f2 = api.get_file(api.create_file(eg.editgroup_id, f2).ident)
    api.accept_editgroup(eg.editgroup_id)

    # create redirects
    eg = quick_eg(api)
    api.update_container(eg.editgroup_id, j2.ident, ContainerEntity(redirect=j1.ident))
    api.update_creator(eg.editgroup_id, c2.ident, CreatorEntity(redirect=c1.ident))
    api.update_file(eg.editgroup_id, f2.ident, FileEntity(redirect=f1.ident))
    api.update_release(eg.editgroup_id, r2.ident, ReleaseEntity(redirect=r1.ident, ext_ids=ReleaseExtIds()))
    api.update_work(eg.editgroup_id, r2.work_id, WorkEntity(redirect=r1.work_id))
    api.accept_editgroup(eg.editgroup_id)

    # all entities
    rv = app.get('/container/{}'.format(j1.ident))
    assert rv.status_code == 200
    rv = app.get('/container/{}'.format(j2.ident))
    assert rv.status_code == 302
    rv = app.get('/creator/{}'.format(c1.ident))
    assert rv.status_code == 200
    rv = app.get('/creator/{}'.format(c2.ident))
    assert rv.status_code == 302
    rv = app.get('/file/{}'.format(f1.ident))
    assert rv.status_code == 200
    rv = app.get('/file/{}'.format(f2.ident))
    assert rv.status_code == 302
    rv = app.get('/release/{}'.format(r1.ident))
    assert rv.status_code == 200
    rv = app.get('/release/{}'.format(r2.ident))
    assert rv.status_code == 302
    rv = app.get('/work/{}'.format(r1.work_id))
    assert rv.status_code == 200
    rv = app.get('/work/{}'.format(r2.work_id))
    assert rv.status_code == 302

    # delete targets
    eg = quick_eg(api)
    api.delete_container(eg.editgroup_id, j1.ident)
    api.delete_creator(eg.editgroup_id, c1.ident)
    api.delete_file(eg.editgroup_id, f1.ident)
    api.delete_release(eg.editgroup_id, r1.ident)
    api.delete_work(eg.editgroup_id, r1.work_id)
    api.accept_editgroup(eg.editgroup_id)

    # all entities
    rv = app.get('/container/{}'.format(j1.ident))
    assert rv.status_code == 200
    rv = app.get('/container/{}'.format(j2.ident))
    assert rv.status_code == 302
    rv = app.get('/creator/{}'.format(c1.ident))
    assert rv.status_code == 200
    rv = app.get('/creator/{}'.format(c2.ident))
    assert rv.status_code == 302
    rv = app.get('/file/{}'.format(f1.ident))
    assert rv.status_code == 200
    rv = app.get('/file/{}'.format(f2.ident))
    assert rv.status_code == 302
    rv = app.get('/release/{}'.format(r1.ident))
    assert rv.status_code == 200
    rv = app.get('/release/{}'.format(r2.ident))
    assert rv.status_code == 302
    rv = app.get('/work/{}'.format(r1.work_id))
    assert rv.status_code == 200
    rv = app.get('/work/{}'.format(r2.work_id))
    assert rv.status_code == 302