<commit_before># -*- encoding: utf-8 -*-
from twisted.internet.defer import inlineCallbacks

from globaleaks.anomaly import Alarm
from globaleaks.handlers import token
from globaleaks.tests import helpers
from globaleaks.tests.test_anomaly import pollute_events_for_testing
from globaleaks.utils.token import Token
from globaleaks.rest import errors
from globaleaks.settings import GLSettings

class Test_TokenCreate(helpers.TestHandlerWithPopulatedDB):
    _handler = token.TokenCreate

    def assert_default_token_values(self, token):
        self.assertEqual(token['type'], u'submission')
        self.assertEqual(token['remaining_uses'], Token.MAX_USES)
        self.assertEqual(token['human_captcha_answer'], 0)

    @inlineCallbacks
    def test_post(self):
        yield Alarm.compute_activity_level()

        handler = self.request({'type': 'submission'})

        yield handler.post()

        token = self.responses[0]

        self.assert_default_token_values(token)


class Test_TokenInstance(helpers.TestHandlerWithPopulatedDB):
    _handler = token.TokenInstance

    @inlineCallbacks
    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, 'solved': False}
        token.proof_of_work['solved'] = True

        request_payload = token.serialize()
        request_payload['human_captcha_answer'] = 1

        handler = self.request(request_payload)

        yield handler.put(token.id)

        token.use()

        self.assertFalse(self.responses[0]['human_captcha'])
        self.assertTrue(token.human_captcha['solved'])

    @inlineCallbacks
    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)<commit_msg>Fixes broken import in test_token<commit_after># -*- encoding: utf-8 -*-
from twisted.internet.defer import inlineCallbacks

from globaleaks.anomaly import Alarm
from globaleaks.handlers import token
from globaleaks.tests import helpers
from globaleaks.utils.token import Token
from globaleaks.rest import errors
from globaleaks.settings import GLSettings

class Test_TokenCreate(helpers.TestHandlerWithPopulatedDB):
    _handler = token.TokenCreate

    def assert_default_token_values(self, token):
        self.assertEqual(token['type'], u'submission')
        self.assertEqual(token['remaining_uses'], Token.MAX_USES)
        self.assertEqual(token['human_captcha_answer'], 0)

    @inlineCallbacks
    def test_post(self):
        yield Alarm.compute_activity_level()

        handler = self.request({'type': 'submission'})

        yield handler.post()

        token = self.responses[0]

        self.assert_default_token_values(token)


class Test_TokenInstance(helpers.TestHandlerWithPopulatedDB):
    _handler = token.TokenInstance

    @inlineCallbacks
    def test_put_right_answer(self):
        self.pollute_events()
        yield Alarm.compute_activity_level()

        token = Token('submission')
        token.human_captcha = {'question': 'XXX','answer': 1, 'solved': False}
        token.proof_of_work['solved'] = True

        request_payload = token.serialize()
        request_payload['human_captcha_answer'] = 1

        handler = self.request(request_payload)

        yield handler.put(token.id)

        token.use()

        self.assertFalse(self.responses[0]['human_captcha'])
        self.assertTrue(token.human_captcha['solved'])

    @inlineCallbacks
    def test_put_wrong_answer(self):
        self.pollute_events()
        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)
{'fraction_non_alphanumeric': 0.10238591614547139, 'fraction_numerical': 0.0046328468844104706, 'mean_word_length': 3.6580366774541533, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '363067', 'n_tokens_mistral': 1298, 'n_tokens_neox': 1225, 'n_words': 229}