33from __future__ import division
44from __future__ import absolute_import
55from builtins import object , open
6- from future import standard_library
7- standard_library .install_aliases ()
8-
9- import json , os , pytest , responses , sys
6+ import json
7+ import os
8+ import pytest
9+ import responses
10+ import sys
1011
1112
1213# make files under helpers available for import
@@ -25,76 +26,76 @@ def manager():
2526
2627
2728class Mock (object ):
28- base_url = 'https://api.upcloud.com/1.2'
29-
30- @staticmethod
31- def read_from_file (filename ):
32-
33- filename = filename .replace ("/" , "_" )
34-
35- cwd = os .path .dirname (__file__ )
36- f = open (cwd + '/json_data/' + filename , 'r' )
37- return f .read ()
38-
39- @staticmethod
40- def mock_get (target , response_file = None ):
41- if not response_file :
42- response_file = target + '.json'
43-
44- data = Mock .read_from_file (response_file )
45- responses .add (responses .GET , Mock .base_url + '/' + target ,
46- body = data ,
47- status = 200 ,
48- content_type = 'application/json' )
49- return data
50-
51- @staticmethod
52- def __put_post_callback (request , target , data ):
53- data_field = target .split ("/" )[0 ]
54- payload = json .loads (request .body )
55-
56- for field in data [data_field ]:
57- if field in payload [data_field ]:
58- data [data_field ][field ] = payload [data_field ][field ]
59- return (200 , {}, json .dumps (data ))
60-
61- @staticmethod
62- def mock_post (target ):
63- data = json .loads ( Mock .read_from_file (target + '_post.json' ) )
64-
65- def callback (request ):
66- return Mock .__put_post_callback (request , target , data )
67-
68- responses .add_callback (responses .POST , Mock .base_url + '/' + target ,
69- callback = callback ,
70- content_type = 'application/json' )
71-
72- @staticmethod
73- def mock_put (target ):
74- data = json .loads ( Mock .read_from_file (target + '.json' ) )
75-
76- def callback (request ):
77- return Mock .__put_post_callback (request , target , data )
78-
79- responses .add_callback (responses .PUT , Mock .base_url + '/' + target ,
80- callback = callback ,
81- content_type = 'application/json' )
82- @ staticmethod
83- def mock_delete ( target ):
84- # print(Mock.base_url + "/" + target)
85- responses .add (responses .DELETE , Mock .base_url + "/" + target ,
86- status = 204 )
87-
88- @staticmethod
89- def mock_server_operation (target ):
90- # drop third (last) part of a string divided by two slashes ("/"); e.g "this/is/string" -> "this/is"
91- targetsplit = target .split ("/" )
92- targetfile = "/" .join ( targetsplit [:2 ] )
93-
94- data = json .loads ( Mock .read_from_file (targetfile + '.json' ) )
95-
96- # API will always respond state: "started", see: Server.stop, Server.start, Server,restart
97- data ['server' ]['state' ] = 'started'
98-
99- data = json .dumps ( data )
100- responses .add (responses .POST , Mock .base_url + "/" + target , status = 200 , body = data , content_type = 'application/json' )
29+ base_url = 'https://api.upcloud.com/1.2'
30+
31+ @staticmethod
32+ def read_from_file (filename ):
33+
34+ filename = filename .replace ("/" , "_" )
35+
36+ cwd = os .path .dirname (__file__ )
37+ f = open (cwd + '/json_data/' + filename , 'r' )
38+ return f .read ()
39+
40+ @staticmethod
41+ def mock_get (target , response_file = None ):
42+ if not response_file :
43+ response_file = target + '.json'
44+
45+ data = Mock .read_from_file (response_file )
46+ responses .add (responses .GET , Mock .base_url + '/' + target ,
47+ body = data ,
48+ status = 200 ,
49+ content_type = 'application/json' )
50+ return data
51+
52+ @staticmethod
53+ def __put_post_callback (request , target , data ):
54+ data_field = target .split ("/" )[0 ]
55+ payload = json .loads (request .body )
56+
57+ for field in data [data_field ]:
58+ if field in payload [data_field ]:
59+ data [data_field ][field ] = payload [data_field ][field ]
60+ return (200 , {}, json .dumps (data ))
61+
62+ @staticmethod
63+ def mock_post (target ):
64+ data = json .loads (Mock .read_from_file (target + '_post.json' ))
65+
66+ def callback (request ):
67+ return Mock .__put_post_callback (request , target , data )
68+
69+ responses .add_callback (responses .POST , Mock .base_url + '/' + target ,
70+ callback = callback ,
71+ content_type = 'application/json' )
72+
73+ @staticmethod
74+ def mock_put (target ):
75+ data = json .loads (Mock .read_from_file (target + '.json' ))
76+
77+ def callback (request ):
78+ return Mock .__put_post_callback (request , target , data )
79+
80+ responses .add_callback (responses .PUT , Mock .base_url + '/' + target ,
81+ callback = callback ,
82+ content_type = 'application/json' )
83+
84+ @ staticmethod
85+ def mock_delete ( target ):
86+ responses .add (responses .DELETE , Mock .base_url + '/' + target ,
87+ status = 204 )
88+
89+ @staticmethod
90+ def mock_server_operation (target ):
91+ # drop third (last) part of a string divided by two slashes ("/"); e.g "this/is/string" -> "this/is"
92+ targetsplit = target .split ('/' )
93+ targetfile = '/' .join ( targetsplit [:2 ] )
94+
95+ data = json .loads ( Mock .read_from_file (targetfile + '.json' ) )
96+
97+ # API will always respond state: "started", see: Server.stop, Server.start, Server,restart
98+ data ['server' ]['state' ] = 'started'
99+
100+ data = json .dumps ( data )
101+ responses .add (responses .POST , Mock .base_url + "/" + target , status = 200 , body = data , content_type = 'application/json' )
0 commit comments