44
55import uuid
66import httpretty
7+ import requests
78
89from dataverse .connection import Connection
910from dataverse .dataset import Dataset
10- from dataverse .settings import TEST_HOST , TEST_TOKEN
11+ from dataverse .settings import TEST_HOST
1112from dataverse .test .config import PICS_OF_CATS_DATASET , ATOM_DATASET , EXAMPLE_FILES
1213from dataverse import exceptions
1314from dataverse import utils
1617logging .basicConfig (level = logging .ERROR )
1718
1819
20+ class DataverseServerTestBase (object ):
21+ """Create a temporary user on `TEST_SERVER` for testing purposes.
22+
23+ This attaches `username`, `password`, and `token` to the class.
24+ """
25+
26+ @classmethod
27+ def setup_class (cls ):
28+ """Create a temporary user"""
29+ cls .username = str (uuid .uuid1 ())
30+ cls .password = 'p4ssw0rd'
31+ key = 'burrito' # hardcoded on test servers
32+ user_url = 'https://{0}/api/builtin-users?key={1}&password={2}' .format (
33+ TEST_HOST , key , cls .password ,
34+ )
35+ user_json = {
36+ 'email' : '{0}@gmail.com' .format (cls .username ),
37+ 'firstName' : 'Namey' ,
38+ 'lastName' : 'Namington' ,
39+ 'userName' : cls .username ,
40+ }
41+
42+ resp = requests .post (user_url , json = user_json )
43+ cls .token = resp .json ()['data' ]['apiToken' ]
44+
45+ @classmethod
46+ def teardown_class (cls ):
47+ """Delete the temporary user.
48+
49+ Note that this will fail if the user has any non-deleted content.
50+ """
51+ delete_url = 'https://{0}/api/admin/authenticatedUsers/{1}/' .format (
52+ TEST_HOST , cls .username ,
53+ )
54+ resp = requests .delete (delete_url )
55+ assert resp .status_code == 200
56+
57+
1958class TestUtils (object ):
2059
2160 def test_get_element (self ):
@@ -64,13 +103,13 @@ def test_format_term_replace(self):
64103 assert formatted_term == '{http://purl.org/dc/terms/}identifier'
65104
66105
67- class TestConnection (object ):
106+ class TestConnection (DataverseServerTestBase ):
68107
69108 def test_connect (self ):
70- connection = Connection (TEST_HOST , TEST_TOKEN )
109+ connection = Connection (TEST_HOST , self . token )
71110
72111 assert connection .host == TEST_HOST
73- assert connection .token == TEST_TOKEN
112+ assert connection .token == self . token
74113 assert connection ._service_document
75114
76115 def test_connect_unauthorized (self ):
@@ -86,10 +125,10 @@ def test_connect_unknown_failure(self):
86125 )
87126
88127 with pytest .raises (exceptions .ConnectionError ):
89- Connection (TEST_HOST , TEST_TOKEN )
128+ Connection (TEST_HOST , self . token )
90129
91130 def test_create_dataverse (self ):
92- connection = Connection (TEST_HOST , TEST_TOKEN )
131+ connection = Connection (TEST_HOST , self . token )
93132 alias = str (uuid .uuid1 ()) # must be unique
94133 connection .create_dataverse (
95134 alias ,
@@ -105,7 +144,7 @@ def test_create_dataverse(self):
105144 connection .delete_dataverse (dataverse )
106145
107146 def test_delete_dataverse (self ):
108- connection = Connection (TEST_HOST , TEST_TOKEN )
147+ connection = Connection (TEST_HOST , self . token )
109148 alias = str (uuid .uuid1 ()) # must be unique
110149 dataverse = connection .create_dataverse (
111150 alias ,
@@ -119,7 +158,7 @@ def test_delete_dataverse(self):
119158 assert dataverse is None
120159
121160 def test_get_dataverses (self ):
122- connection = Connection (TEST_HOST , TEST_TOKEN )
161+ connection = Connection (TEST_HOST , self . token )
123162 original_dataverses = connection .get_dataverses ()
124163 assert isinstance (original_dataverses , list )
125164
@@ -142,7 +181,7 @@ def test_get_dataverses(self):
142181 assert [dv .alias for dv in current_dataverses ] == [dv .alias for dv in original_dataverses ]
143182
144183 def test_get_dataverse (self ):
145- connection = Connection (TEST_HOST , TEST_TOKEN )
184+ connection = Connection (TEST_HOST , self . token )
146185 alias = str (uuid .uuid1 ()) # must be unique
147186 assert connection .get_dataverse (alias ) is None
148187
@@ -193,12 +232,14 @@ def test_init_from_xml(self):
193232 assert publisher == 'Creative Commons CC-BY 3.0 (unported) http://creativecommons.org/licenses/by/3.0/'
194233
195234
196- class TestDatasetOperations (object ):
235+ class TestDatasetOperations (DataverseServerTestBase ):
197236
198237 @classmethod
199238 def setup_class (cls ):
239+ super (TestDatasetOperations , cls ).setup_class ()
240+
200241 print ('Connecting to Dataverse host at {0}' .format (TEST_HOST ))
201- cls .connection = Connection (TEST_HOST , TEST_TOKEN )
242+ cls .connection = Connection (TEST_HOST , cls . token )
202243
203244 print ('Creating test Dataverse' )
204245 cls .alias = str (uuid .uuid1 ())
@@ -212,6 +253,7 @@ def setup_class(cls):
212253
213254 @classmethod
214255 def teardown_class (cls ):
256+ super (TestDatasetOperations , cls ).setup_class ()
215257
216258 print ('Removing test Dataverse' )
217259 cls .connection .delete_dataverse (cls .dataverse )
0 commit comments