Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Jochem Bijlard
fractalis
Commits
c9cbef74
Commit
c9cbef74
authored
Feb 05, 2018
by
Sascha Herzinger
Browse files
Fixed code to make several new unit tests pass (not all)
parent
b8f0c058
Changes
2
Hide whitespace changes
Inline
Side-by-side
fractalis/state/controller.py
View file @
c9cbef74
...
...
@@ -27,8 +27,9 @@ def save_state() -> Tuple[Response, int]:
:return: UUID linked to the saved state.
"""
logger
.
debug
(
"Received POST request on /state."
)
payload
=
request
.
get_json
(
force
=
True
)
# check if task ids in payload are valid
matches
=
re
.
findall
(
'\$.+?\$'
,
request
.
data
)
matches
=
re
.
findall
(
'\$.+?\$'
,
str
(
payload
)
)
if
not
matches
:
error
=
"This state cannot be saved because it contains no data "
\
"task ids. These are used to verify access to the state and "
\
...
...
@@ -36,7 +37,7 @@ def save_state() -> Tuple[Response, int]:
logger
.
error
(
error
)
return
jsonify
({
'error'
:
error
}),
400
for
match
in
matches
:
task_id
=
AnalyticTask
.
parse_value
(
match
)
task_id
,
_
=
AnalyticTask
.
parse_value
(
match
)
value
=
redis
.
get
(
'data:{}'
.
format
(
task_id
))
if
value
is
None
:
error
=
"Data task id is {} could not be found in redis. "
\
...
...
@@ -50,9 +51,8 @@ def save_state() -> Tuple[Response, int]:
"no valid data state. "
\
"State cannot be saved."
.
format
(
task_id
)
return
jsonify
({
'error'
:
error
}),
400
payload
=
json
.
loads
(
request
.
data
)
uuid
=
uuid4
()
redis
.
set
(
name
=
'state:{}'
.
format
(
uuid
),
value
=
payload
)
redis
.
set
(
name
=
'state:{}'
.
format
(
uuid
),
value
=
json
.
dumps
(
payload
)
)
logger
.
debug
(
"Successfully saved data to redis. Sending response."
)
return
jsonify
({
'state_id'
:
uuid
}),
201
...
...
@@ -78,7 +78,7 @@ def request_state_access(state_id: UUID) -> Tuple[Response, int]:
descriptors
=
[]
matches
=
re
.
findall
(
'\$.+?\$'
,
value
)
for
match
in
matches
:
task_id
=
AnalyticTask
.
parse_value
(
match
)
task_id
,
_
=
AnalyticTask
.
parse_value
(
match
)
if
redis
.
get
(
'data:{}'
.
format
(
task_id
))
is
None
:
error
=
"The state with id {} exists, but one or more of the "
\
"associated data task ids are missing. Hence this state "
\
...
...
tests/functional/test_state.py
View file @
c9cbef74
"""This module tests the state controller module."""
import
json
from
uuid
import
UUID
,
uuid4
import
flask
import
pytest
...
...
@@ -22,14 +24,14 @@ class TestState:
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
())
assert
rv
.
status_code
==
400
,
body
assert
400
==
rv
.
status_code
,
body
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
())
assert
rv
.
status_code
==
400
,
body
assert
400
==
rv
.
status_code
,
body
assert
'error'
in
body
assert
'could not be found in redis'
in
body
[
'error'
]
...
...
@@ -37,34 +39,38 @@ class TestState:
redis
.
set
(
'data:123'
,
''
)
rv
=
test_client
.
post
(
'/state'
,
data
=
flask
.
json
.
dumps
(
'$123$'
))
body
=
flask
.
json
.
loads
(
rv
.
get_data
())
assert
rv
.
status_code
==
400
,
body
assert
400
==
rv
.
status_code
,
body
assert
'error'
in
body
assert
'no
t
valid data state'
in
body
[
'error'
]
assert
'no valid data state'
in
body
[
'error'
]
def
test_save_state_saves_and_returns
(
self
,
test_client
):
rv
=
test_client
.
post
(
'/state'
,
data
=
flask
.
json
.
dumps
(
'$123$'
))
redis
.
set
(
'data:123'
,
json
.
dumps
({
'meta'
:
{
'descriptor'
:
'foo'
}}))
rv
=
test_client
.
post
(
'/state'
,
data
=
flask
.
json
.
dumps
({
'test'
:
[
'$123$'
]}))
body
=
flask
.
json
.
loads
(
rv
.
get_data
())
assert
rv
.
status_code
==
201
,
body
assert
201
==
rv
.
status_code
,
body
assert
UUID
(
body
[
'state_id'
])
assert
redis
.
get
(
'state:{}'
.
format
(
body
[
'state_id'
]))
==
'test'
state
=
json
.
loads
(
redis
.
get
(
'state:{}'
.
format
(
body
[
'state_id'
])))
assert
'test'
in
state
assert
[
'$123$'
]
==
state
[
'test'
]
def
test_404_if_request_invalid_state_id
(
self
,
test_client
):
rv
=
test_client
.
post
(
'/state/{}'
.
format
(
str
(
uuid4
())),
data
=
flask
.
json
.
dumps
(
{
'handler'
:
''
,
'server'
:
''
,
'auth'
:
{
'token'
:
''
}}))
assert
rv
.
status_code
==
404
assert
404
==
rv
.
status_code
body
=
flask
.
json
.
loads
(
rv
.
get_data
())
assert
'error'
in
body
assert
'not find state associated with id'
in
body
[
'error'
]
def
test_404_if_state_id_is_no_uuid
(
self
,
test_client
):
rv
=
test_client
.
post
(
'/state/123'
)
assert
rv
.
status_code
==
404
assert
404
==
rv
.
status_code
def
test_400_if_payload_schema_incorrect
(
self
,
test_client
):
rv
=
test_client
.
post
(
'/state/{}'
.
format
(
str
(
uuid4
())),
data
=
flask
.
json
.
dumps
({
'foo'
:
123
}))
assert
rv
.
status_code
==
400
assert
400
==
rv
.
status_code
def
test_error_if_task_id_is_no_etl_id
(
self
,
test_client
):
uuid
=
str
(
uuid4
())
...
...
@@ -72,18 +78,19 @@ class TestState:
rv
=
test_client
.
post
(
'/state/{}'
.
format
(
uuid
),
data
=
flask
.
json
.
dumps
(
{
'handler'
:
''
,
'server'
:
''
,
'auth'
:
{
'token'
:
''
}}))
body
=
flask
.
json
.
loads
(
rv
.
get_data
())
assert
rv
.
status_code
==
400
,
body
assert
400
==
rv
.
status_code
,
body
assert
'error'
in
body
assert
'data task ids are missing'
in
body
[
'error'
]
def
test_202_create_valid_state_if_valid_conditions
(
self
,
test_client
):
uuid
=
str
(
uuid4
())
redis
.
set
(
'data:{}'
.
format
(
uuid
),
{
'meta'
:
{
'descriptor'
:
'foo'
}})
redis
.
set
(
name
=
'data:{}'
.
format
(
uuid
),
value
=
json
.
dumps
({
'meta'
:
{
'descriptor'
:
'foo'
}}))
rv
=
test_client
.
post
(
'/state/{}'
.
format
(
uuid
),
data
=
flask
.
json
.
dumps
(
{
'handler'
:
''
,
'server'
:
''
,
'auth'
:
{
'token'
:
''
}}))
body
=
flask
.
json
.
loads
(
rv
.
get_data
())
assert
rv
.
status_code
==
202
,
body
assert
202
==
rv
.
status_code
,
body
assert
not
body
with
test_client
.
session_transaction
()
as
sess
:
assert
sess
[
'data_tasks'
]
...
...
@@ -96,29 +103,29 @@ class TestState:
rv
=
test_client
.
post
(
'/state/{}'
.
format
(
uuid
),
data
=
flask
.
json
.
dumps
(
{
'handler'
:
''
,
'server'
:
''
,
'auth'
:
{
'token'
:
''
}}))
assert
rv
.
status_code
==
404
assert
404
==
rv
.
status_code
def
test_400_if_get_non_uuid_state
(
self
,
test_client
):
rv
=
test_client
.
post
(
'/state/123'
)
assert
rv
.
status_code
==
400
assert
400
==
rv
.
status_code
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
())
assert
rv
.
status_code
==
201
,
body
assert
201
==
rv
.
status_code
,
body
state_id
=
body
[
'state_id'
]
rv
=
test_client
.
post
(
'/state/{}'
.
format
(
state_id
),
data
=
flask
.
json
.
dumps
(
{
'handler'
:
''
,
'server'
:
''
,
'auth'
:
{
'token'
:
''
}}))
body
=
flask
.
json
.
loads
(
rv
.
get_data
())
assert
rv
.
status_code
==
202
,
body
assert
202
==
rv
.
status_code
,
body
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
())
assert
rv
.
status_code
==
403
,
body
assert
403
==
rv
.
status_code
,
body
assert
'error'
in
body
def
test_return_state
(
self
,
test_client
):
...
...
@@ -126,15 +133,15 @@ class TestState:
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
())
assert
rv
.
status_code
==
201
,
body
assert
201
==
rv
.
status_code
,
body
state_id
=
body
[
'state_id'
]
rv
=
test_client
.
post
(
'/state/{}'
.
format
(
state_id
),
data
=
flask
.
json
.
dumps
(
{
'handler'
:
''
,
'server'
:
''
,
'auth'
:
{
'token'
:
''
}}))
body
=
flask
.
json
.
loads
(
rv
.
get_data
())
assert
rv
.
status_code
==
202
,
body
assert
202
==
rv
.
status_code
,
body
rv
=
test_client
.
get
(
'/state/{}'
.
format
(
state_id
))
body
=
flask
.
json
.
loads
(
rv
.
get_data
())
assert
rv
.
status_code
==
200
,
body
assert
200
==
rv
.
status_code
,
body
assert
'state'
in
body
assert
body
[
'state'
]
==
'test'
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment