Example #1
0
    def test_save_anomalies(self):
        """
        How create anomalies: a lots of event + compute stress
        """

        # start test
        ANOMALIES_AMOUNT = 50
        pollute_events_for_testing(ANOMALIES_AMOUNT)

        Alarm.compute_activity_level()

        anomdet = GLSettings.RecentAnomaliesQ.values()[0]
        self.assertEqual(len(GLSettings.RecentAnomaliesQ.keys()), 1)
        original_date = datetime_to_ISO8601(
            GLSettings.RecentAnomaliesQ.keys()[0])

        self.assertTrue(isinstance(anomdet, list))
        self.assertTrue(len(anomdet), 2)

        # alarm level was 2, right ?
        self.assertEqual(anomdet[1], 2, "Alarm raised is not 2 anymore ?")

        # every stuff need to be ANOMALIES_AMOUNT * 2, because
        # pollute function put two event each
        for event, amount in anomdet[0].iteritems():
            self.assertEqual(amount, ANOMALIES_AMOUNT * 2)

        # scheduler happen to save these anomalies, along with stats
        yield StatisticsSchedule().operation()

        # now if we get our anomalies, we expect the same 10, right ?
        AH = yield get_anomaly_history(limit=10)
        self.assertEqual(original_date, AH[0]['date'])
Example #2
0
    def test_save_anomalies(self):
        """
        How create anomalies: a lots of event + compute stress
        """

        # start test
        ANOMALIES_COUNT = 50
        pollute_events_for_testing(ANOMALIES_COUNT)

        Alarm.compute_activity_level()

        anomdet = GLSettings.RecentAnomaliesQ.values()[0]
        self.assertEqual(len(GLSettings.RecentAnomaliesQ.keys()), 1)
        original_date = datetime_to_ISO8601(GLSettings.RecentAnomaliesQ.keys()[0])

        self.assertTrue(isinstance(anomdet, list))
        self.assertTrue(len(anomdet), 2)

        # alarm level was 2, right ?
        self.assertEqual(anomdet[1], 2, "Alarm raised is not 2 anymore ?")

        # every stuff need to be ANOMALIES_AMOUNT * 2, because
        # pollute function put two event each
        for event, count in anomdet[0].iteritems():
            self.assertEqual(count, ANOMALIES_COUNT * 2)

        # scheduler happen to save these anomalies, along with stats
        yield StatisticsSchedule().operation()

        # now if we get our anomalies, we expect the same 10, right ?
        AH = yield get_anomaly_history(limit=10)
        self.assertEqual(original_date, AH[0]['date'])
Example #3
0
    def setUp(self):
        yield helpers.TestGL.setUp(self)

        # This is at the beginning
        event.EventTrackQueue.reset()

        pollute_events_for_testing()
        yield anomaly.compute_activity_level()
Example #4
0
    def setUp(self):
        yield helpers.TestGL.setUp(self)

        pollute_events_for_testing()
        yield Alarm.compute_activity_level()

        # Token submission
        st = Token('submission')
        st.generate_token_challenge()
Example #5
0
    def setUp(self):
        yield helpers.TestGL.setUp(self)

        pollute_events_for_testing()
        yield Alarm.compute_activity_level()

        # Token submission
        st = Token('submission')
        st.generate_token_challenge()
Example #6
0
    def test_get(self):
        pollute_events_for_testing(50)
        yield AnomaliesSchedule().operation()
        yield StatisticsSchedule().operation()

        handler = self.request({}, role='admin')
        yield handler.get()

        self.assertTrue(isinstance(self.responses, list))
        self.assertEqual(len(self.responses), 1)
Example #7
0
    def test_get(self):
        pollute_events_for_testing(3)
        yield AnomaliesSchedule().operation()
        yield StatisticsSchedule().operation()

        handler = self.request({}, role='admin')
        yield handler.get()

        self.assertTrue(isinstance(self.responses, list))
        self.assertEqual(len(self.responses), 1)
Example #8
0
    def setUp(self):
        yield helpers.TestGL.setUp(self)

        # This is at the beginning
        event.EventTrackQueue.reset()

        pollute_events_for_testing()
        yield anomaly.compute_activity_level()

        # Token submission
        st = Token('submission')
        st.generate_token_challenge()
Example #9
0
    def setUp(self):
        yield helpers.TestGL.setUp(self)

        # This is at the beginning
        event.EventTrackQueue.reset()

        pollute_events_for_testing()
        yield anomaly.compute_activity_level()

        # Token submission
        st = Token('submission')
        st.generate_token_challenge()
Example #10
0
    def test_put_wrong_answer(self):
        pollute_events_for_testing()
        yield Alarm.compute_activity_level()

        token = Token('submission')

        token.human_captcha = {'question': 'XXX', 'answer': 1, 'solved': False}

        request_payload = token.serialize()

        request_payload['human_captcha_answer'] = 883

        handler = self.request(request_payload)
        self.assertRaises(errors.TokenFailure, handler.put, token.id)
Example #11
0
    def test_put_wrong_answer(self):
        pollute_events_for_testing()
        yield Alarm.compute_activity_level()

        token = Token('submission')

        token.human_captcha = {'question': 'XXX','answer': 1, 'solved': False}

        request_payload = token.serialize()

        request_payload['human_captcha_answer'] = 883

        handler = self.request(request_payload)
        self.assertRaises(errors.TokenFailure, handler.put, token.id)
Example #12
0
    def test_delete(self):
        pollute_events_for_testing(3)
        yield AnomaliesSchedule().operation()
        yield StatisticsSchedule().operation()

        # be sure that Stats is populated
        count = yield self.get_stats_count()
        self.assertEqual(count, 1)

        # delete stats
        handler = self.request({}, role='admin')
        yield handler.delete()

        # verify that stats are now empty
        count = yield self.get_stats_count()
        self.assertEqual(count, 0)
Example #13
0
    def test_delete(self):
        pollute_events_for_testing(50)
        yield AnomaliesSchedule().operation()
        yield StatisticsSchedule().operation()

        # be sure that Stats is populated
        count = yield self.get_stats_count()
        self.assertEqual(count, 1)

        # delete stats
        handler = self.request({}, role='admin')
        yield handler.delete()

        # verify that stats are now empty
        count = yield self.get_stats_count()
        self.assertEqual(count, 0)
Example #14
0
    def test_put_right_answer(self):
        pollute_events_for_testing()
        yield Alarm.compute_activity_level()

        token = Token('submission')

        token.human_captcha = {'question': 'XXX','answer': 1}
        token.proof_of_work = False

        request_payload = token.serialize()

        request_payload['human_captcha_answer'] = 1

        handler = self.request(request_payload)
        yield handler.put(token.id)

        self.assertEqual(self.responses[0]['human_captcha'], False)
Example #15
0
    def test_put_right_answer(self):
        pollute_events_for_testing()
        yield Alarm.compute_activity_level()

        token = Token('submission')

        token.human_captcha = {'question': 'XXX', 'answer': 1}
        token.proof_of_work = False

        request_payload = token.serialize()

        request_payload['human_captcha_answer'] = 1

        handler = self.request(request_payload)
        yield handler.put(token.id)

        self.assertEqual(self.responses[0]['human_captcha'], False)
Example #16
0
    def test_put_right_answer(self):
        event.EventTrackQueue.reset()

        pollute_events_for_testing()
        yield anomaly.compute_activity_level()

        token = Token('submission')

        token.human_captcha = {'question': 'XXX','answer': 1}

        request_payload = token.serialize()

        request_payload['human_captcha_answer'] = 1

        handler = self.request(request_payload)
        yield handler.put(token.id)

        self.assertEqual(self.responses[0]['human_captcha'], False)
Example #17
0
    def test_put_right_answer(self):
        event.EventTrackQueue.reset()

        pollute_events_for_testing()
        yield anomaly.compute_activity_level()

        token = Token('submission')

        token.human_captcha = {'question': 'XXX', 'answer': 1}

        request_payload = token.serialize()

        request_payload['human_captcha_answer'] = 1

        handler = self.request(request_payload)
        yield handler.put(token.id)

        self.assertEqual(self.responses[0]['human_captcha'], False)
Example #18
0
    def test_get(self):
        handler = self.request({}, role='admin')
        yield handler.get(0)

        self.assertTrue(isinstance(self.responses, list))
        self.assertEqual(len(self.responses[0]), 3)
        self.assertEqual(len(self.responses[0]['heatmap']), 7 * 24)

        pollute_events_for_testing(3)

        yield AnomaliesSchedule().operation()
        yield StatisticsSchedule().operation()

        for i in range(0, 2):
            handler = self.request({}, role='admin')
            yield handler.get(i)
            self.assertEqual(len(self.responses[1 + i]), 3)
            self.assertEqual(len(self.responses[1 + i]['heatmap']), 7 * 24)
Example #19
0
    def test_get(self):
        handler = self.request({}, role='admin')
        yield handler.get(0)

        self.assertTrue(isinstance(self.responses, list))
        self.assertEqual(len(self.responses[0]), 3)
        self.assertEqual(len(self.responses[0]['heatmap']), 7 * 24)

        pollute_events_for_testing(50)

        yield AnomaliesSchedule().operation()
        yield StatisticsSchedule().operation()

        for i in range(0, 2):
            handler = self.request({}, role='admin')
            yield handler.get(i)
            self.assertEqual(len(self.responses[1 + i]), 3)
            self.assertEqual(len(self.responses[1 + i]['heatmap']), 7 * 24)
Example #20
0
    def test_get(self):
        pollute_events_for_testing(3)
        yield StatisticsSchedule().operation()

        handler = self.request({}, role='admin')

        yield handler.get('details')
        yield handler.get('summary')

        self.assertTrue(isinstance(self.responses[0], list))
        self.assertTrue(isinstance(self.responses[1], dict))

        for k in ['id', 'duration', 'event', 'creation_date']:
            for elem in self.responses[0]:
                self.assertTrue(k in elem)

        for k in ['wb_messages', 'wb_comments',
                  'receiver_messages', 'receiver_comments',
                  'started_submissions', 'completed_submissions',
                  'successful_logins', 'failed_logins',
                  'uploaded_files', 'appended_files']:
            self.assertTrue(k in self.responses[1])
Example #21
0
    def test_put_wrong_answer(self):
        event.EventTrackQueue.reset()

        pollute_events_for_testing()
        yield Alarm.compute_activity_level()

        token = Token('submission')

        token.human_captcha = {'question': 'XXX', 'answer': 1}
        token.proof_of_work = False

        request_payload = token.serialize()

        request_payload['human_captcha_answer'] = 2

        handler = self.request(request_payload)
        yield handler.put(token.id)

        self.assertNotEqual(self.responses[0]['human_captcha'], False)

        # verify that the question is changed
        self.assertNotEqual(self.responses[0]['human_captcha'], 'XXX')
Example #22
0
    def test_put_wrong_answer(self):
        event.EventTrackQueue.reset()

        pollute_events_for_testing()
        yield Alarm.compute_activity_level()

        token = Token('submission')

        token.human_captcha = {'question': 'XXX','answer': 1}
        token.proof_of_work = False

        request_payload = token.serialize()

        request_payload['human_captcha_answer'] = 2

        handler = self.request(request_payload)
        yield handler.put(token.id)

        self.assertNotEqual(self.responses[0]['human_captcha'], False)

        # verify that the question is changed
        self.assertNotEqual(self.responses[0]['human_captcha'], 'XXX')
Example #23
0
    def test_get(self):
        pollute_events_for_testing(3)
        yield StatisticsSchedule().operation()

        handler = self.request({}, role='admin')

        yield handler.get('details')
        yield handler.get('summary')

        self.assertTrue(isinstance(self.responses[0], list))
        self.assertTrue(isinstance(self.responses[1], dict))

        for k in ['id', 'duration', 'event', 'creation_date']:
            for elem in self.responses[0]:
                self.assertTrue(k in elem)

        for k in [
                'wb_messages', 'wb_comments', 'receiver_messages',
                'receiver_comments', 'started_submissions',
                'completed_submissions', 'successful_logins', 'failed_logins',
                'uploaded_files', 'appended_files'
        ]:
            self.assertTrue(k in self.responses[1])
Example #24
0
    def test_delete(self):
        pollute_events_for_testing(3)
        yield AnomaliesSchedule().operation()
        yield StatisticsSchedule().operation()

        # be sure that AnomalyHistory is populated
        handler = self.request({}, role='admin')
        yield handler.get()
        self.assertEqual(len(self.responses), 1)
        self.assertTrue(isinstance(self.responses, list))

        self.responses = []

        # delete stats
        handler = self.request({}, role='admin')
        yield handler.delete()

        self.responses = []

        # test that AnomalyHistory is not populated anymore
        handler = self.request({}, role='admin')
        yield handler.get()
        self.assertEqual(len(self.responses), 0)
        self.assertTrue(isinstance(self.responses, list))
Example #25
0
    def test_delete(self):
        pollute_events_for_testing(50)
        yield AnomaliesSchedule().operation()
        yield StatisticsSchedule().operation()

        # be sure that AnomalyHistory is populated
        handler = self.request({}, role='admin')
        yield handler.get()
        self.assertEqual(len(self.responses), 1)
        self.assertTrue(isinstance(self.responses, list))

        self.responses = []

        # delete stats
        handler = self.request({}, role='admin')
        yield handler.delete()

        self.responses = []

        # test that AnomalyHistory is not populated anymore
        handler = self.request({}, role='admin')
        yield handler.get()
        self.assertEqual(len(self.responses), 0)
        self.assertTrue(isinstance(self.responses, list))
Example #26
0
    def setUp(self):
        yield helpers.TestGL.setUp(self)

        pollute_events_for_testing()
        yield Alarm.compute_activity_level()
Example #27
0
    def setUp(self):
        yield helpers.TestGL.setUp(self)

        pollute_events_for_testing()
        yield Alarm.compute_activity_level()
Example #28
0
class TestToken(helpers.TestGL):
    """
    This is an object testing class,
    to check the handler testing, see in
    test_anomalies
    """
    pollute_events_for_testing()

    shared_alarm_obj = anomaly.Alarm()
    stress_indicator = ['graph_captcha', 'human_captcha', 'proof_of_work']

    @inlineCallbacks
    def setUp(self):
        yield helpers.TestGL.setUp(self)

        # This is at the beginning
        event.EventTrackQueue.reset()

        pollute_events_for_testing()
        yield anomaly.compute_activity_level()

        # Token submission
        st = Token('submission')
        st.generate_token_challenge()

    def test_token(self):
        st = Token('submission')

        st_dict = st.serialize()

        self.assertEqual(st_dict['remaining_uses'], Token.MAX_USES)

        if st.graph_captcha:
            self.assertTrue(st.graph_captcha.has_key('answer'))
            self.assertTrue(isinstance(st.graph_captcha['answer'], list))

        if st.human_captcha:
            self.assertTrue(st.human_captcha.has_key('answer'))
            self.assertTrue(isinstance(st.human_captcha['answer'], unicode))

    @inlineCallbacks
    def test_token_create_and_get_upload_expire(self):
        file_list = []

        token_collection = []
        for i in xrange(20):
            st = Token('submission')

            token_collection.append(st)

        for t in token_collection:
            token = TokenList.get(t.id)

            yield self.emulate_file_upload(token, 3)

            for f in token.uploaded_files:
                self.assertTrue(os.path.exists(f['encrypted_path']))
                file_list.append(f['encrypted_path'])

            token.expire()

            self.assertRaises(errors.TokenFailure, TokenList.get, t.id)

            for f in file_list:
                self.assertFalse(os.path.exists(f))

    def test_token_update_right_answer(self):
        token = Token('submission')

        token.human_captcha = {'question': '1 + 0', 'answer': 1}
        token.proof_of_work = False

        # validate with right value: OK
        token.update({'human_captcha_answer': 1})

        # verify that the challenge is marked as solved
        self.assertFalse(token.human_captcha)

    def test_token_update_wrong_answer(self):
        token = Token('submission')

        token.human_captcha = {'question': 'XXX', 'answer': 1}

        token.update({'human_captcha_answer': 0})

        # verify that the challenge is changed
        self.assertNotEqual(token.human_captcha['question'], 'XXX')

    def test_token_uses_limit(self):
        token = Token('submission')

        token.human_captcha = False
        token.proof_of_work = False

        # validate with right value: OK
        token.update({'human_captcha_answer': 1})

        for i in range(0, token.MAX_USES):
            token.use()

        # validate with right value but with no additional
        # attempts available: FAIL
        self.assertRaises(errors.TokenFailure, token.use)

    def test_proof_of_work_right_answer(self):
        # This is at the beginning
        event.EventTrackQueue.reset()

        token = Token('submission')

        difficulty = {
            'human_captcha': False,
            'graph_captcha': False,
            'proof_of_work': False
        }

        token.generate_token_challenge(difficulty)

        token = TokenList.get(token.id)
        # Note, this solution works with two '00' at the end, if the
        # difficulty changes, also this dummy value has to.
        token.proof_of_work = {'question': "7GJ4Sl37AEnP10Zk9p7q"}

        # validate with right value: OK
        self.assertFalse(token.update({'proof_of_work_answer': 26}))

        # verify that the challenge is marked as solved
        self.assertFalse(token.proof_of_work)

    def test_proof_of_work_right_answer(self):
        token = Token('submission')

        difficulty = {
            'human_captcha': False,
            'graph_captcha': False,
            'proof_of_work': False
        }

        token.generate_token_challenge(difficulty)

        token = TokenList.get(token.id)
        # Note, this solution works with two '00' at the end, if the
        # difficulty changes, also this dummy value has to.
        token.proof_of_work = {'question': "7GJ4Sl37AEnP10Zk9p7q"}

        # validate with right value: OK
        self.assertTrue(token.update({'proof_of_work_answer': 0}))

        # verify that the challenge is changed
        self.assertNotEqual(token.human_captcha['question'], 'XXX')