test_state.py 6.23 KB
Newer Older
1
2
"""This module tests the state controller module."""

3
4
import json

5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from uuid import UUID, uuid4
import flask
import pytest

from fractalis import redis, sync


# noinspection PyMissingOrEmptyDocstring,PyMissingTypeHints
class TestState:

    @pytest.fixture(scope='function')
    def test_client(self):
        sync.cleanup_all()
        from fractalis import app
        app.testing = True
        with app.test_client() as test_client:
            yield test_client
            sync.cleanup_all()

24
25
26
    def test_400_if_no_task_id_in_payload(self, test_client):
        rv = test_client.post('/state', data=flask.json.dumps('$...foo'))
        body = flask.json.loads(rv.get_data())
27
        assert 400 == rv.status_code, body
28
29
30
31
32
33
        assert 'error' in body
        assert 'contains no data task ids' in body['error']

    def test_400_if_task_id_not_in_redis(self, test_client):
        rv = test_client.post('/state', data=flask.json.dumps('$123$'))
        body = flask.json.loads(rv.get_data())
34
        assert 400 == rv.status_code, body
35
36
37
38
39
40
41
        assert 'error' in body
        assert 'could not be found in redis' in body['error']

    def test_400_if_task_id_in_redis_but_no_data_state(self, test_client):
        redis.set('data:123', '')
        rv = test_client.post('/state', data=flask.json.dumps('$123$'))
        body = flask.json.loads(rv.get_data())
42
        assert 400 == rv.status_code, body
43
        assert 'error' in body
44
        assert 'no valid data state' in body['error']
45

46
    def test_save_state_saves_and_returns(self, test_client):
47
48
49
        redis.set('data:123', json.dumps({'meta': {'descriptor': 'foo'}}))
        rv = test_client.post('/state',
                              data=flask.json.dumps({'test': ['$123$']}))
50
        body = flask.json.loads(rv.get_data())
51
        assert 201 == rv.status_code, body
52
        assert UUID(body['state_id'])
53
54
55
        state = json.loads(redis.get('state:{}'.format(body['state_id'])))
        assert 'test' in state
        assert ['$123$'] == state['test']
56
57

    def test_404_if_request_invalid_state_id(self, test_client):
58
59
60
        rv = test_client.post(
            '/state/{}'.format(str(uuid4())), data=flask.json.dumps(
                {'handler': '', 'server': '', 'auth': {'token': ''}}))
61
        assert 404 == rv.status_code
62
63
64
        body = flask.json.loads(rv.get_data())
        assert 'error' in body
        assert 'not find state associated with id' in body['error']
65

66
    def test_404_if_state_id_is_no_uuid(self, test_client):
67
        rv = test_client.post('/state/123')
68
        assert 404 == rv.status_code
69
70
71
72

    def test_400_if_payload_schema_incorrect(self, test_client):
        rv = test_client.post('/state/{}'.format(str(uuid4())),
                              data=flask.json.dumps({'foo': 123}))
73
        assert 400 == rv.status_code
74
75
76

    def test_error_if_task_id_is_no_etl_id(self, test_client):
        uuid = str(uuid4())
Sascha Herzinger's avatar
Sascha Herzinger committed
77
        redis.set('state:{}'.format(uuid), '$123$')
78
79
        rv = test_client.post('/state/{}'.format(uuid), data=flask.json.dumps(
            {'handler': '', 'server': '', 'auth': {'token': ''}}))
80
        body = flask.json.loads(rv.get_data())
Sascha Herzinger's avatar
Sascha Herzinger committed
81
        assert 403 == rv.status_code, body
82
        assert 'error' in body
83
        assert 'data task ids are missing' in body['error']
Sascha Herzinger's avatar
Sascha Herzinger committed
84
        assert not redis.exists('state:{}'.format(uuid))
85

86
    def test_202_create_valid_state_if_valid_conditions(self, test_client):
87
        uuid = str(uuid4())
Sascha Herzinger's avatar
Sascha Herzinger committed
88
89
90
91
        redis.set(name='data:123',
                  value=json.dumps(
                      {'meta': {'descriptor': {'data_type': 'default'}}}))
        redis.set(name='state:{}'.format(uuid), value='$123$')
92
93
        rv = test_client.post(
            '/state/{}'.format(uuid), data=flask.json.dumps(
Sascha Herzinger's avatar
Sascha Herzinger committed
94
                {'handler': 'test', 'server': 'foo', 'auth': {'token': ''}}))
95
        body = flask.json.loads(rv.get_data())
96
        assert 202 == rv.status_code, body
97
        assert not body
98
99
100
        with test_client.session_transaction() as sess:
            assert sess['data_tasks']
            assert sess['state_access']
Sascha Herzinger's avatar
Sascha Herzinger committed
101
            assert sess['data_tasks'] == sess['state_access'][uuid]
102
103
104
105
            assert [UUID(uuid) for uuid in sess['data_tasks']]

    def test_404_if_get_non_existing_state(self, test_client):
        uuid = str(uuid4())
106
107
108
        rv = test_client.post(
            '/state/{}'.format(uuid), data=flask.json.dumps(
                {'handler': '', 'server': '', 'auth': {'token': ''}}))
109
        assert 404 == rv.status_code
110
111
112

    def test_400_if_get_non_uuid_state(self, test_client):
        rv = test_client.post('/state/123')
113
        assert 400 == rv.status_code
114
115
116
117
118
119

    def test_403_if_get_not_previously_self_requested_state(self, test_client):
        uuid = str(uuid4())
        redis.set('data:{}'.format(uuid), {'meta': {'descriptor': 'foo'}})
        rv = test_client.post('/state', data=flask.json.dumps('test'))
        body = flask.json.loads(rv.get_data())
120
        assert 201 == rv.status_code, body
121
        state_id = body['state_id']
122
123
124
        rv = test_client.post(
            '/state/{}'.format(state_id), data=flask.json.dumps(
                {'handler': '', 'server': '', 'auth': {'token': ''}}))
125
        body = flask.json.loads(rv.get_data())
126
        assert 202 == rv.status_code, body
127
128
129
130
        with test_client.session_transaction() as sess:
            del sess['state_access'][state_id]
        rv = test_client.get('/state/{}'.format(state_id))
        body = flask.json.loads(rv.get_data())
131
        assert 403 == rv.status_code, body
132
133
134
135
136
137
138
        assert 'error' in body

    def test_return_state(self, test_client):
        uuid = str(uuid4())
        redis.set('data:{}'.format(uuid), {'meta': {'descriptor': 'foo'}})
        rv = test_client.post('/state', data=flask.json.dumps('test'))
        body = flask.json.loads(rv.get_data())
139
        assert 201 == rv.status_code, body
140
        state_id = body['state_id']
141
142
143
        rv = test_client.post(
            '/state/{}'.format(state_id), data=flask.json.dumps(
                {'handler': '', 'server': '', 'auth': {'token': ''}}))
144
        body = flask.json.loads(rv.get_data())
145
        assert 202 == rv.status_code, body
146
147
        rv = test_client.get('/state/{}'.format(state_id))
        body = flask.json.loads(rv.get_data())
148
        assert 200 == rv.status_code, body
149
150
        assert 'state' in body
        assert body['state'] == 'test'