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 ):
@@ -81,15 +120,17 @@ def test_connect_unauthorized(self):
81120 def test_connect_unknown_failure (self ):
82121 httpretty .register_uri (
83122 httpretty .GET ,
84- 'https://{host}/dvn/api/data-deposit/v1.1/swordv2/service-document' .format (host = TEST_HOST ),
123+ 'https://{host}/dvn/api/data-deposit/v1.1/swordv2/service-document' .format (
124+ host = TEST_HOST
125+ ),
85126 status = 400 ,
86127 )
87128
88129 with pytest .raises (exceptions .ConnectionError ):
89- Connection (TEST_HOST , TEST_TOKEN )
130+ Connection (TEST_HOST , self . token )
90131
91132 def test_create_dataverse (self ):
92- connection = Connection (TEST_HOST , TEST_TOKEN )
133+ connection = Connection (TEST_HOST , self . token )
93134 alias = str (uuid .uuid1 ()) # must be unique
94135 connection .create_dataverse (
95136 alias ,
@@ -105,7 +146,7 @@ def test_create_dataverse(self):
105146 connection .delete_dataverse (dataverse )
106147
107148 def test_delete_dataverse (self ):
108- connection = Connection (TEST_HOST , TEST_TOKEN )
149+ connection = Connection (TEST_HOST , self . token )
109150 alias = str (uuid .uuid1 ()) # must be unique
110151 dataverse = connection .create_dataverse (
111152 alias ,
@@ -119,7 +160,7 @@ def test_delete_dataverse(self):
119160 assert dataverse is None
120161
121162 def test_get_dataverses (self ):
122- connection = Connection (TEST_HOST , TEST_TOKEN )
163+ connection = Connection (TEST_HOST , self . token )
123164 original_dataverses = connection .get_dataverses ()
124165 assert isinstance (original_dataverses , list )
125166
@@ -142,7 +183,7 @@ def test_get_dataverses(self):
142183 assert [dv .alias for dv in current_dataverses ] == [dv .alias for dv in original_dataverses ]
143184
144185 def test_get_dataverse (self ):
145- connection = Connection (TEST_HOST , TEST_TOKEN )
186+ connection = Connection (TEST_HOST , self . token )
146187 alias = str (uuid .uuid1 ()) # must be unique
147188 assert connection .get_dataverse (alias ) is None
148189
@@ -190,15 +231,18 @@ def test_init_from_xml(self):
190231 tag = 'rights'
191232 ).text
192233 assert title == 'Roasting at Home'
193- assert publisher == 'Creative Commons CC-BY 3.0 (unported) http://creativecommons.org/licenses/by/3.0/'
234+ assert publisher == 'Creative Commons CC-BY 3.0 (unported) ' \
235+ 'http://creativecommons.org/licenses/by/3.0/'
194236
195237
196- class TestDatasetOperations (object ):
238+ class TestDatasetOperations (DataverseServerTestBase ):
197239
198240 @classmethod
199241 def setup_class (cls ):
242+ super (TestDatasetOperations , cls ).setup_class ()
243+
200244 print ('Connecting to Dataverse host at {0}' .format (TEST_HOST ))
201- cls .connection = Connection (TEST_HOST , TEST_TOKEN )
245+ cls .connection = Connection (TEST_HOST , cls . token )
202246
203247 print ('Creating test Dataverse' )
204248 cls .alias = str (uuid .uuid1 ())
@@ -212,6 +256,7 @@ def setup_class(cls):
212256
213257 @classmethod
214258 def teardown_class (cls ):
259+ super (TestDatasetOperations , cls ).setup_class ()
215260
216261 print ('Removing test Dataverse' )
217262 cls .connection .delete_dataverse (cls .dataverse )
0 commit comments