def test_track_detail_app_removed_tracker(self): tracker1 = Tracker( id=1, name='Teemo', ) tracker1.save() application_handle1 = "com.handle.one" report1 = Report() report1.save() application1 = Application( handle=application_handle1, report=report1 ) application1.save() report1.found_trackers = [tracker1.id] report1.save() report2 = Report() report2.save() application2 = Application( handle=application_handle1, report=report2 ) application2.save() # Removing trackers in the version of the app report2.found_trackers = [] report2.save() c = Client() url = self.TRACKER_DETAIL_PATH.format(tracker1.id) response = c.get(url) self.assertEqual(response.status_code, 200) self.assertEqual(response.context['tracker'].id, tracker1.id) self.assertEqual(len(response.context['reports']), 0)
def test_track_detail_app_multiple_reports(self): tracker2 = Tracker( id=2, name='Exodus Super Tracker', ) tracker2.save() application_handle2 = "com.handle.two" report2 = Report() report2.save() application2 = Application( handle=application_handle2, report=report2 ) application2.save() report2.found_trackers = [tracker2.id] report2.save() report3 = Report() report3.save() application3 = Application( handle=application_handle2, report=report3 ) application3.save() report3.found_trackers = [tracker2.id] report3.save() c = Client() url = self.TRACKER_DETAIL_PATH.format(tracker2.id) response = c.get(url) self.assertEqual(response.status_code, 200) self.assertEqual(response.context['tracker'].id, tracker2.id) self.assertEqual(len(response.context['reports']), 1) self.assertEqual(response.context['reports'][0].application.handle, application_handle2) self.assertEqual(response.context['reports'][0], report3)
def test_should_raise_404_if_no_report(self): tracker = Tracker(name='Teemo') tracker.save() c = Client() response = c.get(self.STATS_PATH) self.assertEqual(response.status_code, 404)
def test_should_return_stats_with_multiple_reports_multiple_application(self): tracker1 = Tracker( id=1, name='Teemo', ) tracker1.save() tracker2 = Tracker( id=2, name='Exodus Super Tracker', ) tracker2.save() application_handle1 = "com.handle.one" application_handle2 = "com.handle.two" report1 = Report() report1.save() application1 = Application( handle=application_handle1, report=report1 ) application1.save() report1.found_trackers = [tracker2.id] report1.save() report2 = Report() report2.save() application2 = Application( handle=application_handle2, report=report2 ) application2.save() report2.found_trackers = [] report2.save() report3 = Report() report3.save() application3 = Application( handle=application_handle2, report=report3 ) application3.save() report3.found_trackers = [tracker1.id, tracker2.id] report3.save() c = Client() response = c.get(self.STATS_PATH) self.assertEqual(response.status_code, 200) self.assertContains(response, tracker1.name, 1) self.assertContains(response, tracker2.name, 1) self.assertEqual(response.context['trackers'][0].name, tracker2.name) # Only recent for an application is considered self.assertEqual(response.context['trackers'][0].count, 2) self.assertEqual(response.context['trackers'][0].score, 100) self.assertEqual(response.context['trackers'][1].name, tracker1.name) self.assertEqual(response.context['trackers'][1].count, 1) self.assertEqual(response.context['trackers'][1].score, 50)
def test_should_return_stats_with_1_tracker_not_found(self): tracker = Tracker(name='Teemo') tracker.save() report = Report() report.save() c = Client() response = c.get(self.STATS_PATH) self.assertEqual(response.status_code, 200) self.assertContains(response, tracker.name, 1) self.assertEqual(response.context['trackers'][0].name, tracker.name) self.assertEqual(response.context['trackers'][0].count, 0) self.assertEqual(response.context['trackers'][0].score, 0)
def handle(self, *args, **options): if (Tracker.objects.all()[:1].count() > 0): raise CommandError('Your trakers table in not empty, please truncate its before the import') json_str = self.read_file(options['filename']).decode('utf-8') trackers = json.loads(json_str) for id, tracker in trackers['trackers'].items(): model = Tracker( id=tracker['id'], name=tracker['name'], description=tracker['description'], creation_date=tracker['creation_date'], code_signature=tracker['code_signature'], network_signature=tracker['network_signature'], website=tracker['website'], ) model.save() print('%s saved' % model.name)
def test_should_not_include_more_than_X_trackers(self): tracker_limit = 21 for x in range(0, tracker_limit): tracker = Tracker(name='Tracker{}.'.format(x)) tracker.save() extra_tracker = Tracker(name='Exodus Super Tracker') extra_tracker.save() first_trackers = Tracker.objects.exclude(name=extra_tracker.name) report = Report() report.save() report.found_trackers = [t.id for t in first_trackers] report.save() c = Client() response = c.get(self.STATS_PATH) self.assertEqual(response.status_code, 200) self.assertNotContains(response, extra_tracker.name) for t in first_trackers: self.assertContains(response, t.name, 1)
def test_should_return_stats_with_multiple_trackers_found(self): tracker1 = Tracker( id=1, name='Teemo', ) tracker1.save() tracker2 = Tracker( id=2, name='Exodus Super Tracker', ) tracker2.save() report1 = Report() report1.save() report1.found_trackers = [tracker2.id] report1.save() report2 = Report() report2.save() report2.found_trackers = [] report2.save() report3 = Report() report3.save() report3.found_trackers = [tracker1.id, tracker2.id] report3.save() c = Client() response = c.get(self.STATS_PATH) self.assertEqual(response.status_code, 200) self.assertContains(response, tracker1.name, 1) self.assertContains(response, tracker2.name, 1) self.assertEqual(response.context['trackers'][0].name, tracker2.name) self.assertEqual(response.context['trackers'][0].count, 2) self.assertEqual(response.context['trackers'][0].score, 66) self.assertEqual(response.context['trackers'][1].name, tracker1.name) self.assertEqual(response.context['trackers'][1].count, 1) self.assertEqual(response.context['trackers'][1].score, 33)