@@ -31,7 +31,7 @@ def read_file(filename, mode='r'):
3131 return content
3232
3333
34- def setup_mock_server ():
34+ def setup_mock_server (raise_5xx_errors = False ):
3535 """Configure a Mock server"""
3636
3737 def request_callback (request , uri , headers ):
@@ -88,6 +88,17 @@ def request_callback(request, uri, headers):
8888 ECLIPSE_API_URL + "/account/profile/jrae/employment-history" ,
8989 body = read_file ('data/eclipse_jrae_employment.json' ))
9090
91+ # Overwrite jsmith and jdoe profile replies to raise 5xx errors
92+ if raise_5xx_errors :
93+ httpretty .register_uri (httpretty .GET ,
94+ ECLIPSE_API_URL + "/account/profile/jsmith" ,
95+ body = "" ,
96+ status = 500 )
97+ httpretty .register_uri (httpretty .GET ,
98+ ECLIPSE_API_URL + "/account/profile/jdoe" ,
99+ body = "" ,
100+ status = 504 )
101+
91102 return requests , bodies
92103
93104
@@ -153,6 +164,7 @@ def test_backend_name(self):
153164 @patch ('sortinghat.core.importer.backends.eclipse.EclipseFoundationAPIClient.login' , return_value = "mocked_login" )
154165 @patch ('sortinghat.core.importer.backends.eclipse.datetime_utcnow' , return_value = MOCK_DATETIME_NOW )
155166 def test_import_identities (self , mock_login , mock_datetime_now ):
167+ """Check if identities are imported"""
156168
157169 # Set up a mock HTTP server
158170 requests , bodies = setup_mock_server ()
@@ -222,6 +234,8 @@ def test_import_identities(self, mock_login, mock_datetime_now):
222234 @patch ('sortinghat.core.importer.backends.eclipse.EclipseFoundationAPIClient.login' , return_value = "mocked_login" )
223235 @patch ('sortinghat.core.importer.backends.eclipse.datetime_utcnow' , return_value = MOCK_DATETIME_NOW )
224236 def test_import_no_identities (self , mock_login , mock_datetime_now ):
237+ """Check if all goes ok when there aren't identities to import"""
238+
225239 # Set up a mock HTTP server
226240 requests , bodies = setup_mock_server ()
227241
@@ -236,10 +250,49 @@ def test_import_no_identities(self, mock_login, mock_datetime_now):
236250 # No identities
237251 self .assertEqual (n , 0 )
238252
253+ @httpretty .activate
254+ @patch ('sortinghat.core.importer.backends.eclipse.EclipseFoundationAPIClient.login' , return_value = "mocked_login" )
255+ @patch ('sortinghat.core.importer.backends.eclipse.datetime_utcnow' , return_value = MOCK_DATETIME_NOW )
256+ def test_ignore_5xx_errors (self , mock_login , mock_datetime_now ):
257+ """Check if the importer ignores 5xx errors"""
258+
259+ # Set up a mock HTTP server
260+ requests , bodies = setup_mock_server (raise_5xx_errors = True )
261+
262+ importer = EclipseFoundationAccountsImporter (ctx = self .ctx , url = None , from_date = None )
263+ n = importer .import_identities ()
264+
265+ # In total, only 1 individual and 2 identities will be imported.
266+ # Individuals 'jsmith' and 'jdoe' profiles return 5xx errors.
267+ self .assertEqual (n , 2 )
268+
269+ individuals = Individual .objects .order_by ('mk' ).all ()
270+
271+ self .assertEqual (len (individuals ), 1 )
272+
273+ # Jane Rae
274+ jrae = individuals [0 ]
275+ self .assertEqual (jrae .profile .name , 'Jane Rae' )
276+ self .assertEqual (jrae .profile .email , 'jrae@example.com' )
277+
278+ ids = jrae .identities .order_by ('uuid' ).all ()
279+
280+ self .assertEqual (len (ids ), 2 )
281+ self .assertEqual (ids [0 ].name , 'Jane Rae' )
282+ self .assertEqual (ids [0 ].email , 'jrae@example.com' )
283+ self .assertEqual (ids [0 ].username , 'jrae' )
284+ self .assertEqual (ids [0 ].source , 'eclipsefdn' )
285+
286+ self .assertEqual (ids [1 ].name , 'Jane Rae' )
287+ self .assertEqual (ids [1 ].email , 'jrae@example.com' )
288+ self .assertEqual (ids [1 ].username , 'jrae' )
289+ self .assertEqual (ids [1 ].source , 'github' )
290+
239291 @httpretty .activate
240292 @patch ('sortinghat.core.importer.backends.eclipse.EclipseFoundationAPIClient.login' , return_value = "mocked_login" )
241293 @patch ('sortinghat.core.importer.backends.eclipse.datetime_utcnow' , return_value = MOCK_DATETIME_NOW )
242294 def test_import_merge_identities (self , mock_login , mock_datetime_now ):
295+ """Check if existing identities are merged"""
243296
244297 # Add individuals that share email and github handle
245298 api .add_identity (self .ctx , source = 'github' , username = 'jsmith' )
@@ -261,6 +314,7 @@ def test_import_merge_identities(self, mock_login, mock_datetime_now):
261314 @patch ('sortinghat.core.importer.backends.eclipse.EclipseFoundationAPIClient.login' , return_value = "mocked_login" )
262315 @patch ('sortinghat.core.importer.backends.eclipse.datetime_utcnow' , return_value = MOCK_DATETIME_NOW )
263316 def test_import_enrollments (self , mock_login , mock_datetime_now ):
317+ """Check if enrolments are imported"""
264318
265319 # Set up a mock HTTP server
266320 setup_mock_server ()
0 commit comments