class DocumentSavedSignalTestCase(TransactionTestCase): """ Tests the Collection and CollectionManager classes. """ fixtures = get_fixtures(['distilleries']) def setUp(self): self.distillery = Distillery.objects.get_by_natural_key( 'mongodb.test_database.test_posts') doc = {'text': 'this is a text post'} self.doc_obj = DocumentObj( data=doc, doc_id=2, collection=str(self.distillery) ) def test_document_saved_signal(self): """ Tests that the document_saved signal is sent with arguments. """ handler = Mock() document_saved.connect(handler, sender='test') document_saved.send(sender='test', doc_obj=self.doc_obj) handler.assert_called_once_with( sender='test', signal=document_saved, doc_obj=self.doc_obj ) def test_save_data_sends_signal(self): """ Tests that the document_saved signal is sent when a document is saved. """ handler = Mock() mock_doc_obj = self.doc_obj mock_doc_id = '1' self.distillery.collection.insert = Mock(return_value=mock_doc_id) self.distillery._create_doc_obj = Mock(return_value=mock_doc_obj) document_saved.connect(handler, sender=Distillery) doc_id = self.distillery._save_and_send_signal(self.doc_obj) # Assert the signal was called only once with the args handler.assert_called_once_with( sender=Distillery, signal=document_saved, doc_obj=mock_doc_obj ) self.assertEqual(mock_doc_id, doc_id)
class MailAttachmentTestCase(MailHelperTestCase): """ Tests the get_file_name helper function. """ fixtures = get_fixtures(['companies']) date = datetime.date(year=2016, month=12, day=10) def setUp(self): super(MailAttachmentTestCase, self).setUp() self.company = Company.objects.get(pk=1)
class CourierBaseTestCase(TestCase): """ Base class for testing the Alerts. """ fixtures = get_fixtures(['couriers']) def setUp(self): self.jira_courier = Courier.objects.get(pk=1) self.jira_action = Action.objects.get(pk=1) self.twitter_courier = Courier.objects.get(pk=3) self.twitter_action = Action.objects.get(pk=2)
class MailChuteTestCase(TestCase): """ Base class for testing MailChutes. """ fixtures = get_fixtures(['mailchutes']) def test_process_match(self): """ Tests the process method for a matching email. """ mock_doc_id = 1 email = {'Message-ID': 'abc', 'Subject': 'This is a Critical Alert'} doc_obj = DocumentObj(data=email) mailchute = MailChute.objects.get(pk=1) mailchute.munger.process = Mock(return_value=mock_doc_id) doc_id = mailchute.process(doc_obj) mailchute.munger.process.assert_called_once_with(doc_obj) self.assertEqual(doc_id, mock_doc_id), None def test_process_nonmatch(self): """ Tests the process method for a nonmatching email. """ email = {'Message-ID': 'abc', 'Subject': 'This is an Urgent Alert'} doc_obj = DocumentObj(data=email) mailchute = MailChute.objects.get(pk=1) mailchute.munger.process = Mock(return_value=None) doc_id = mailchute.process(doc_obj) self.assertEqual(doc_id, None) def test_process_no_sieve(self): """ Tests the process method for a chute with no sieve. """ mock_doc_id = 1 email = {'Message-ID': 'abc', 'Subject': 'This is an Urgent Alert'} doc_obj = DocumentObj(data=email) mailchute = MailChute.objects.get(pk=3) mailchute.enabled = True mailchute.munger.process = Mock(return_value=mock_doc_id) doc_id = mailchute.process(doc_obj) mailchute.munger.process.assert_called_once_with(doc_obj) self.assertEqual(doc_id, mock_doc_id)
class PumpBaseTestCase(TestCase): """ Base class for testing the Pump class. """ fixtures = get_fixtures( ['plumbers', 'gateways', 'funnels', 'pipes', 'streams', 'searchterms']) def setUp(self): self.subquery1 = Mock() self.subquery2 = Mock() self.query_list = [self.subquery1, self.subquery2] create_pumps(self)
class LogCondenserBaseTestCase(TestCase): """ Base class for testing the LogCondenser class and related classes. """ fixtures = get_fixtures(['logcondensers']) test_doc = 'Mar 29 13:02:58 0.0.0.0 [3:19187:7] THIS IS A TEST' @classmethod def setUpClass(cls): super(LogCondenserBaseTestCase, cls).setUpClass() cls.condenser = LogCondenser.objects.get(name='nested_log')
class TopicTestCase(TestCase): """ Base class for testing the Topic class. """ fixtures = get_fixtures(['tags']) def test_str(self): """ Tests the __str__ method. """ topic = Topic.objects.get(pk=1) self.assertEqual(str(topic), 'Animals')
class TagRelationTestCase(TestCase): """ Base class for testing the TagRelation class. """ fixtures = get_fixtures(['tags']) def test_str(self): """ Tests the __str__ method. """ tag_relation = TagRelation.objects.get(pk=1) self.assertEqual(str(tag_relation), 'cat <Alert: PK 1: Acme Supply Co>')
class CompanyTestCase(TestCase): """ Base class for testing the CodeBook class and related classes. """ fixtures = get_fixtures(['companies']) def test_str(self): """ Tests the __str__ method. """ company = Company.objects.get(pk=1) self.assertEqual(str(company), 'Acme')
class MuzzleTestCase(TestCase): """ Tests the Muzzle class. """ fixtures = get_fixtures(['watchdogs', 'alerts']) mock_data = { 'content': { 'text': 'foo', }, 'user': '******', } @classmethod def setUpClass(cls): super(MuzzleTestCase, cls).setUpClass() cls.watchdog = Watchdog.objects.get(pk=1) def setUp(self): self.muzzle = Muzzle.objects.get(pk=1) def test_str(self): """ Tests the __str__ method of a Muzzle. """ self.assertEqual(str(self.muzzle), 'inspect_emails') def test_get_fields_wo_spaces(self): """ Tests the _get_fields method when no spaces separate the fields. """ actual = self.muzzle.get_fields() expected = ['content.subject', 'to'] self.assertEqual(actual, expected) def test_get_fields_w_spaces(self): """ Tests the _get_fields method when spaces separate the fields. """ self.muzzle.matching_fields = ' message, source_ip ' actual = self.muzzle.get_fields() expected = ['message', 'source_ip'] self.assertEqual(actual, expected) def test_get_fields_single_field(self): """ Tests the _get_fields method for a single field. """ self.muzzle.matching_fields = ' message, ' actual = self.muzzle.get_fields() expected = ['message'] self.assertEqual(actual, expected)
class CategoryTestCase(TestCase): """ Base class for testing the CodeBook class and related classes. """ fixtures = get_fixtures(['categories']) def test_str(self): """ Tests the __str__ method. """ category = Category.objects.get(pk=1) self.assertEqual(str(category), 'reports')
class BottleFunctionalTest(ModelPreviewFunctionalTest): """ Base class for testing admin pages in the Bottles app. """ fixtures = get_fixtures(['bottles']) url = '/admin/bottles/bottle/3/change/' def test_change_form(self): """ Tests the Bottle change form. Makes sure that the dictionary in the preview field is indented properly. """ self.maxDiff = None actual = self.page.preview expected = ('{' '<br>' ' "content": {' '<br>' ' "image": "URLField",' '<br>' ' "link": "URLField",' '<br>' ' "text": "TextField (Keyword)",' '<br>' ' "title": "TextField (Keyword)",' '<br>' ' "video": "URLField"' '<br>' ' },' '<br>' ' "created_date": "DateTimeField (DateTime)",' '<br>' ' "location": "PointField (Location)",' '<br>' ' "user": {' '<br>' ' "email": "EmailField (Account)",' '<br>' ' "id": "CharField",' '<br>' ' "link": "URLField",' '<br>' ' "name": "CharField (Account)",' '<br>' ' "profile_pic": "URLField",' '<br>' ' "screen_name": "CharField (Account)"' '<br>' ' }' '<br>' '}') self.assertEqual(actual, expected)
class ContextAPITest(CyphonAPITestCase): """ Tests REST API pages for Contexts. """ fixtures = get_fixtures(['contexts']) model_url = 'contexts/' mock_data = {'host': 'foo', 'message': 'bar'} mock_results = {'count': 1, 'results': [{'foo2': 'bar2'}]} def test_get_contexts(self): """ Tests the Contexts REST API endpoint. """ response = self.get_api_response() self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data['count'], 6) @patch_find(mock_results) @patch_find_by_id(mock_data) def test_related_data_by_id(self): """ Tests the related_data_by_id view. """ url = self.url + '1/related-data-by-id/' self.authenticate() query = {'id': '1'} response = self.client.get(url, query) actual = response.json() actual = response.json() expected = { 'distillery': 'elasticsearch.test_index.test_docs', 'count': 1, 'results': [{ 'foo2': 'bar2' }] } self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(actual, expected) def test_related_data_by_id_no_id(self): """ Tests the related_data_by_id view when no id is provided. """ url = self.url + '1/related-data-by-id/' self.authenticate() query = {'foo': '1'} response = self.client.get(url, query) actual = response.json() expected = {'error': 'A document id must be provided.'} self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(actual, expected)
class DataSieveNodeTestCase(TestCase): """ Tests the DataSieve class. """ fixtures = get_fixtures(['datasieves']) def test_valid_rule(self): """ Tests the clean() method when the SieveNode refers to a Rule. """ sieve_1 = DataSieve.objects.get(pk=1) rule_2 = DataRule.objects.get(pk=2) node = DataSieveNode(sieve=sieve_1, node_object=rule_2) try: node.clean() except ValidationError: self.fail('DataSieveNode raised ValidationError unexpectedly') def test_valid_sieve(self): """ Tests the clean() method when the SieveNode refers to a Sieve that doesn't contain its parent Sieve. """ sieve_1 = DataSieve.objects.get(pk=1) sieve_2 = DataSieve.objects.get(pk=2) node = DataSieveNode(sieve=sieve_1, node_object=sieve_2) try: node.clean() except ValidationError: self.fail('DataSieveNode raised ValidationError unexpectedly') def test_invalid_direct_sieve(self): """ Tests the clean() method when the SieveNode refers to a Sieve that's the same as its parent Sieve. """ sieve_1 = DataSieve.objects.get(pk=1) node = DataSieveNode(sieve=sieve_1, node_object=sieve_1) with self.assertRaises(ValidationError): node.clean() def test_invalid_indirect_sieve(self): """ Tests the clean() method when the SieveNode refers to a Sieve that contains its parent Sieve. """ sieve_1 = DataSieve.objects.get(pk=1) sieve_2 = DataSieve.objects.get(pk=2) DataSieveNode.objects.create(sieve=sieve_2, node_object=sieve_1) node = DataSieveNode(sieve=sieve_1, node_object=sieve_2) with self.assertRaises(ValidationError): node.clean()
class TagAPITests(CyphonAPITestCase): """ Tests REST API endpoints for Distilleries. """ fixtures = get_fixtures(['tags']) model_url = 'tags/' obj_url = '1/' def test_get_tag(self): """ Tests the GET /api/v1/tags/1 REST API endpoint. """ response = self.get_api_response(self.obj_url) expected = { 'id': 1, 'name': 'bird', 'topic': { 'id': 1, 'name': 'Animals', 'url': 'http://testserver/api/v1/topics/1/' }, 'article': { 'id': 1, 'title': 'Birds', 'url': 'http://testserver/api/v1/articles/1/' }, } self.assertEqual(response.json(), expected) self.assertEqual(response.status_code, status.HTTP_200_OK) def test_get_tags(self): """ Tests the GET /api/v1/tag/ REST API endpoint. """ response = self.get_api_response() actual = response.data['results'] expected = [{ 'id': 1, 'name': 'bird' }, { 'id': 2, 'name': 'cat' }, { 'id': 3, 'name': 'dog' }, { 'id': 4, 'name': 'turtle' }] self.assertEqual(actual, expected) self.assertEqual(response.status_code, status.HTTP_200_OK)
class LabelBaseTestCase(TestCase): """ Base class for testing the Label and LabelField classes. """ fixtures = get_fixtures(['labels']) high_priority_msg = {'subject': '[CRIT-111]'} med_priority_msg = {'subject': '[WARN-222]'} low_priority_msg = {'subject': '[INFO-333]'} def setUp(self): self.label_field = LabelField.objects.get(pk=1) self.label = Label.objects.get_by_natural_key('mail')
class ContextFunctionalTest(ModelPreviewFunctionalTest): """ Base class for testing admin pages in the Contexts app. """ fixtures = get_fixtures(['contexts']) def setUp(self): super(ContextFunctionalTest, self).setUp() self.page = ContextPage(self.driver) self.syslog = Distillery.objects.get_by_natural_key( 'elasticsearch.test_time_series.test_syslogs') self.mail = Distillery.objects.get_by_natural_key( 'elasticsearch.test_index.test_mail')
class ProtocolTestCase(TestCase): """ Tests Protocol model methods. """ fixtures = get_fixtures(['procedures']) def setUp(self): self.protocol = Protocol.objects.get_by_natural_key('geoip') def test_str(self): """ Tests the __str__ method for a Protocol. """ self.assertEqual(str(self.protocol), 'geoip')
class StreamManagerTestCase(TestCase): """ Class for StreamManager test cases. """ fixtures = get_fixtures(['streams']) def close_all(self): """ Tests the __str__ method of the Stream class. """ stream = Stream.objects.get(pk=2) assert stream.active Stream.objects.close_all() stream = Stream.objects.get(pk=2) self.assertFalse(stream.active)
class ContextFiltersAPITest(CyphonAPITestCase): """ Tests REST API pages for ContextFilters. """ fixtures = get_fixtures(['contexts']) model_url = 'contextfilters/' def test_get_contexts(self): """ Tests the ContextFilters REST API endpoint. """ response = self.get_api_response() self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data['count'], 2)
class CommentTestCase(TestCase): """ Tests the Comment model methods. """ fixtures = get_fixtures(['comments']) def setUp(self): self.comment = Comment.objects.get(pk=1) def test_get_all_comments(self): """ Tests the get_all_comments method. """ results = self.comment.get_all_comments() self.assertEqual(len(results), 2) def test_get_alert_assignee(self): """ Tests the get_alert_assignee method. """ user_model = get_user_model() actual = self.comment.get_alert_assignee() expected = user_model.objects.get(pk=1) self.assertEqual(actual, expected) def test_get_other_contributors(self): """ Tests the get_other_contributors method. """ contributors = self.comment.get_other_contributors() self.assertEqual(len(contributors), 1) self.assertEqual(contributors[0].pk, 2) comment = Comment.objects.get(pk=2) contributors = comment.get_other_contributors() self.assertEqual(len(contributors), 1) self.assertEqual(contributors[0].pk, 1) def test_summary(self): """ Tests the summary method. """ comment = Comment.objects.get(pk=1) actual = comment.summary() expected = ( 'John Smith commented at 2015-03-01 02:41:24.468404+00:00:\n' 'I have something to say') self.assertEqual(actual, expected)
class AddLogFittingFunctionalTest(FittingFunctionalTest): """ Tests the Add LogFitting admin page. """ fixtures = get_fixtures(['logcondensers']) url = '/admin/logcondensers/logfitting/add/' def test_autocomplete(self): """ Tests autocomplete features on the Add LogFitting admin page. """ condenser_href = self.live_server_url + '/admin/logcondensers/logcondenser/?' parser_href = self.live_server_url + '/admin/logcondensers/logparser/?' # make sure default content_type is 'log parser' self.assertEqual(self.page.content_type, 'log parser') self.assertEqual(self.page.lookup, parser_href) # import time # time.sleep(180) # make sure target_field options are filtered self.assertEqual(self.page.target_field.count(), 0) self.page.condenser = 'nested_log' self.assertEqual(self.page.target_field.count(), 3) # select a condenser target_field field = BottleField.objects.get_by_natural_key('user') self.page.target_field.select(field.pk) # make sure content_type changes self.assertEqual(self.page.content_type, 'log condenser') # make sure the generic relation link has changed self.assertEqual(self.page.lookup, condenser_href) # make sure target_field options change when condenser changes self.page.target_field.delete() self.page.condenser = 'simple_log' self.assertEqual(self.page.target_field.count(), 1) # select a different target_field that's not an EmbeddedDocument field = BottleField.objects.get_by_natural_key('message') self.page.target_field.select(field.pk) # check that 'log parser' is automatically selected self.assertEqual(self.page.content_type, 'log parser') self.assertEqual(self.page.lookup, parser_href)
class GetNewMailTestCase(TestCase): """ Tests the get_new_mail task. """ fixtures = get_fixtures(['monitors']) def test_run_health_check(self): """ Tests the get_new_mail task. """ mailbox_count = Mailbox.objects.all().count() with patch( 'django_mailbox.models.Mailbox.get_new_mail') as mock_get_mail: get_new_mail() self.assertEqual(mock_get_mail.call_count, mailbox_count)
class ContextBaseTestCase(TestCase): """ Test case for the Context class. """ fixtures = get_fixtures(['contexts']) def setUp(self): logging.disable(logging.WARNING) self.context_w_filters = Context.objects.get(pk=1) self.context_wo_focal_taste = Context.objects.get(pk=2) self.context_wo_related_taste = Context.objects.get(pk=3) self.context_wo_filters = Context.objects.get(pk=4) self.context_wo_time_frame = Context.objects.get(pk=5) def tearDown(self): logging.disable(logging.NOTSET)
class AddDataFittingFunctionalTest(FittingFunctionalTest): """ Tests the DataFitting admin page. """ fixtures = get_fixtures(['datacondensers']) url = '/admin/datacondensers/datafitting/add/' def test_add_fitting(self): """ Tests autocomplete features on the Add DataFitting admin page. """ condenser_href = self.live_server_url + '/admin/datacondensers/datacondenser/?' parser_href = self.live_server_url + '/admin/datacondensers/dataparser/?' # make sure default content_type is 'data parser' self.assertEqual(self.page.content_type, 'data parser') self.assertEqual(self.page.lookup, parser_href) # make sure target_field options are filtered self.assertEqual(self.page.target_field.count(), 0) self.page.condenser = 'instagram__post' self.assertEqual(self.page.target_field.count(), 4) # select a condenser target_field field = BottleField.objects.get_by_natural_key('content') self.page.target_field.select(field.pk) # make sure content_type changes self.assertEqual(self.page.content_type, 'data condenser') # make sure the generic relation link has changed self.assertEqual(self.page.lookup, condenser_href) # make sure target_field options change when condenser changes self.page.target_field.delete() self.page.condenser = 'mail' self.assertEqual(self.page.target_field.count(), 5) # select a different target_field that's not an EmbeddedDocument field = BottleField.objects.get_by_natural_key('date') self.page.target_field.select(field.pk) # check that 'data parser' is automatically selected self.assertEqual(self.page.content_type, 'data parser') self.assertEqual(self.page.lookup, parser_href)
class RunHealthCheckTestCase(TestCase): """ Tests the run_health_check task. """ fixtures = get_fixtures(['monitors']) def test_run_health_check(self): """ Tests the run_health_check task. """ all_monitors_count = Monitor.objects.all().count() enabled_monitors_count = Monitor.objects.find_enabled().count() assert all_monitors_count > enabled_monitors_count with patch('monitors.models.Monitor.update_status') as mock_update: run_health_check() self.assertEqual(mock_update.call_count, enabled_monitors_count)
class AppUserViewTestCase(CyphonAPITestCase): """ Base class for testing the AppUser views. """ fixtures = get_fixtures(['users']) model_url = 'users/' def test_get_users_for_nonstaff(self): """ Tests the AppUsers REST API endpoint for user who is not staff. """ response = self.get_api_response(is_staff=False) count = AppUser.objects.count() self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(response.data['results']), count)
class StreamControllerTestCase(TestCase): """ Base class for testing the StreamController class. """ fixtures = get_fixtures( ['plumbers', 'gateways', 'funnels', 'pipes', 'streams', 'searchterms']) def setUp(self): create_pumps(self) pipe = Pipe.objects.get_by_natural_key('twitter', 'SearchAPI') user = User.objects.get(pk=1) self.faucet = Faucet(endpoint=pipe, user=user, task=BKGD_SRCH) self.faucet.process_request = Mock() # avoid NotImplementedError self.faucet.stop = Mock() self.controller = StreamController(faucet=self.faucet, query=self.query) self.stream = self.controller.stream
class AssociatedTagsTestCase(TestCase): """ Tests the associated_tags property of an Alert. """ fixtures = get_fixtures(['alerts', 'comments', 'tags']) def test_alert_w_comments(self): """ """ alert = Alert.objects.get(pk=3) tags = alert.associated_tags self.assertEqual(tags.count(), 4) self.assertTrue(tags.filter(name='bird').exists()) self.assertTrue(tags.filter(name='cat').exists()) self.assertTrue(tags.filter(name='dog').exists()) self.assertTrue(tags.filter(name='turtle').exists())
class SampleTestCase(TestCase): """ Tests the Sample class. """ fixtures = get_fixtures(['samples']) def test_str(self): """ Tests the __str__ method. """ pipe = Pipe.objects.get_by_natural_key('twitter', 'SearchAPI') sample = Sample(pipe=pipe, author='from', title='subject', content='body', location=None, datetime='date') self.assertEqual(str(sample), 'Twitter SearchAPI')