def test_401(self): """Test 401.""" self.mock.send_request.side_effect = error.ClusterFuzzError( 401, 'resp') with self.assertRaises(error.UnauthorizedError) as cm: reproduce.get_testcase_info('12345') self.assertIn('12345', cm.exception.message) self.mock.send_request.assert_called_once_with( reproduce.CLUSTERFUZZ_TESTCASE_INFO_URL, '{"testcaseId": "12345"}')
def test_error(self): """Test other error.""" self.mock.send_request.side_effect = error.ClusterFuzzError( 500, 'resp') with self.assertRaises(error.ClusterFuzzError) as cm: reproduce.get_testcase_info('12345') self.assertEqual(500, cm.exception.status_code) self.assertIn('resp', cm.exception.message) self.mock.send_request.assert_called_once_with( reproduce.CLUSTERFUZZ_TESTCASE_INFO_URL, '{"testcaseId": "12345"}')
def test_correct_verification_auth(self): """Tests grabbing testcase info when the local header is invalid.""" response_headers = {'x-clusterfuzz-authorization': 'Bearer 12345'} response_dict = { 'id': '12345', 'crash_type': 'Bad Crash', 'crash_state': ['Halted'] } self.mock.get_stored_auth_header.return_value = None self.mock.get_verification_header.return_value = 'VerificationCode 12345' self.mock.fetch.return_value = mock.Mock( status=200, body=json.dumps(response_dict), headers=response_headers) response = reproduce.get_testcase_info('12345') self.assert_exact_calls(self.mock.get_stored_auth_header, [mock.call()]) self.assert_exact_calls(self.mock.get_verification_header, [mock.call()]) self.assert_exact_calls(self.mock.store_auth_header, [mock.call('Bearer 12345')]) self.assert_exact_calls(self.mock.fetch, [ mock.call(headers={'Authorization': 'VerificationCode 12345'}, url=reproduce.CLUSTERFUZZ_TESTCASE_INFO_URL % '12345') ]) self.assertEqual(response, response_dict)
def test_correct_stored_authorization(self): """Ensures that the testcase info is returned when stored auth is correct""" response_headers = {'x-clusterfuzz-authorization': 'Bearer 12345'} response_dict = { 'id': '12345', 'crash_type': 'Bad Crash', 'crash_state': ['Halted'] } self.mock.get_stored_auth_header.return_value = 'Bearer 12345' self.mock.fetch.return_value = mock.Mock( status=200, body=json.dumps(response_dict), headers=response_headers) response = reproduce.get_testcase_info('12345') self.assert_exact_calls(self.mock.get_stored_auth_header, [mock.call()]) self.assert_exact_calls(self.mock.store_auth_header, [mock.call('Bearer 12345')]) self.assert_exact_calls(self.mock.fetch, [ mock.call(url=reproduce.CLUSTERFUZZ_TESTCASE_INFO_URL % '12345', headers={'Authorization': 'Bearer 12345'}) ]) self.assertEqual(response, response_dict)
def test_correct_verification_auth(self): """Tests grabbing testcase info when the local header is invalid.""" response_headers = {'x-clusterfuzz-authorization': 'Bearer 12345'} response_dict = { 'id': '12345', 'crash_type': 'Bad Crash', 'crash_state': ['Halted'] } self.mock.get_stored_auth_header.return_value = None self.mock.get_verification_header.return_value = 'VerificationCode 12345' self.mock.post.return_value = mock.Mock(status_code=200, text=json.dumps(response_dict), headers=response_headers) response = reproduce.get_testcase_info(999) self.assert_exact_calls(self.mock.get_stored_auth_header, [mock.call()]) self.assert_exact_calls(self.mock.get_verification_header, [mock.call()]) self.assert_exact_calls(self.mock.store_auth_header, [mock.call('Bearer 12345')]) self.assert_exact_calls(self.mock.post, [ mock.call(headers={ 'Authorization': 'VerificationCode 12345', 'User-Agent': 'clusterfuzz-tools' }, allow_redirects=True, data=json.dumps({'testcaseId': 999}), url=reproduce.CLUSTERFUZZ_TESTCASE_INFO_URL) ]) self.assertEqual(response, response_dict)
def test_correct_stored_authorization(self): """Ensures that the testcase info is returned when stored auth is correct""" response_headers = {'x-clusterfuzz-authorization': 'Bearer 12345'} response_dict = { 'id': '12345', 'crash_type': 'Bad Crash', 'crash_state': ['Halted'] } self.mock.get_stored_auth_header.return_value = 'Bearer 12345' self.mock.post.return_value = mock.Mock(status_code=200, text=json.dumps(response_dict), headers=response_headers) response = reproduce.get_testcase_info(999) self.assert_exact_calls(self.mock.get_stored_auth_header, [mock.call()]) self.assert_exact_calls(self.mock.store_auth_header, [mock.call('Bearer 12345')]) self.assert_exact_calls(self.mock.post, [ mock.call(url=reproduce.CLUSTERFUZZ_TESTCASE_INFO_URL, headers={ 'Authorization': 'Bearer 12345', 'User-Agent': 'clusterfuzz-tools' }, data=json.dumps({'testcaseId': 999}), allow_redirects=True) ]) self.assertEqual(response, response_dict)
def test_succeed(self): """Test succeed.""" self.mock.send_request.return_value = mock.Mock(text='{"test": "ok"}') self.assertEqual({'test': 'ok'}, reproduce.get_testcase_info('12345')) self.mock.send_request.assert_called_once_with( reproduce.CLUSTERFUZZ_TESTCASE_INFO_URL, '{"testcaseId": "12345"}')
def test_incorrect_authorization(self): """Ensures that when auth is incorrect the right exception is thrown""" response_headers = {'x-clusterfuzz-authorization': 'Bearer 12345'} response_dict = { 'status': 401, 'type': 'UnauthorizedException', 'message': { 'Invalid verification code (12345)': { 'error': 'invalid_grant', 'error_description': 'Bad Request' } }, 'params': { 'testcaseId': ['999'] }, 'email': '*****@*****.**' } self.mock.get_stored_auth_header.return_value = 'Bearer 12345' self.mock.get_verification_header.return_value = 'VerificationCode 12345' self.mock.post.return_value = mock.Mock(status_code=401, text=json.dumps(response_dict), headers=response_headers) with self.assertRaises(common.ClusterfuzzAuthError) as cm: reproduce.get_testcase_info(999) self.assertIn('Invalid verification code (12345)', cm.exception.message) self.assert_exact_calls(self.mock.post, [ mock.call(allow_redirects=True, url=reproduce.CLUSTERFUZZ_TESTCASE_INFO_URL, data=json.dumps({'testcaseId': 999}), headers={ 'Authorization': 'Bearer 12345', 'User-Agent': 'clusterfuzz-tools' }), mock.call(allow_redirects=True, headers={ 'Authorization': 'VerificationCode 12345', 'User-Agent': 'clusterfuzz-tools' }, url=reproduce.CLUSTERFUZZ_TESTCASE_INFO_URL, data=json.dumps({'testcaseId': 999})) ])
def test_incorrect_testcase_id(self): """Ensures that when auth is incorrect the right exception is thrown""" response_headers = {'x-clusterfuzz-authorization': 'Bearer 12345'} response_dict = {'status': 404} self.mock.get_stored_auth_header.return_value = '' self.mock.get_verification_header.return_value = 'VerificationCode 12345' self.mock.post.return_value = mock.Mock(status_code=404, text=json.dumps(response_dict), headers=response_headers) with self.assertRaises(common.ClusterfuzzAuthError) as cm: reproduce.get_testcase_info(999) self.assertIn('404', cm.exception.message) self.assert_exact_calls(self.mock.post, [ mock.call(allow_redirects=True, headers={ 'Authorization': 'VerificationCode 12345', 'User-Agent': 'clusterfuzz-tools' }, url=reproduce.CLUSTERFUZZ_TESTCASE_INFO_URL, data=json.dumps({'testcaseId': 999})) ])