def test_end_to_end(self): inp = 'hi my name is pat what is your name' encoded = simple_encoding.encode(inp) decoded = simple_encoding.decode(encoded) self.assertEqual(decoded, inp)
def test_decode_answer(self): answer_str = app.get_answer_str('patins') answer_bits = simple_encoding.encode(answer_str) answer = app.app.delorean.encode_without_permutation(answer_bits) r = self.decode(' '.join(answer)) self.assertIsNotNone(r['answer']) self.assertTrue(r['well_formed']) self.assertEqual(r['message'], answer_str) self.assertEqual(r['message_bits'], answer_bits)
def challenge(): if 'username' not in request.args: abort(400) username = request.args['username'] answer_str = get_answer_str(username) incr_stat('challenge') response = { 'message': answer_str, 'message_bits': simple_encoding.encode(answer_str) } return jsonify(response)
def test_example(self): rv = self.app.get('/api/examples') examples = json.loads(rv.data) for example in examples: self.assertEqual(example['message_bits'], simple_encoding.encode(example['message'])) self.assertEqual(simple_encoding.decode(example['message_bits']), example['message']) self.assertCountEqual( app.app.delorean.encode_without_permutation( example['message_bits']), example['codeword'].split(' ')) self.assertEqual( app.app.delorean.decode(example['codeword'].split(' ')), example['message_bits'])
def examples(): incr_stat('examples') examples = [] for example in EXAMPLE_STRS: example_bits = simple_encoding.encode(example) delorean_message = app.delorean.encode_without_permutation( example_bits) random.shuffle(delorean_message) examples.append({ 'message': example, 'message_bits': example_bits, 'codeword': ' '.join(delorean_message) }) return jsonify(examples)
def decode(): if 'codeword' not in request.form or 'username' not in request.form: abort(400) username = request.form['username'] word_list = request.form['codeword'].lower().split(' ') message_bits = None well_formed = False answer = None incr_stat('decode') try: message_bits = app.delorean.decode(word_list) well_formed = True incr_stat('well_formed_decode') except NotWellFormedException: pass # the_message_they_shouldve_sent = # app.delorean.encode_without_permutation(get_answer(username)) answer_str = get_answer_str(username) answer_bits = simple_encoding.encode(answer_str) if message_bits == answer_bits: incr_stat('correct_decode') answer = date_hash(app.secret_key, username) message_str = None try: if message_bits is not None: message_str = simple_encoding.decode(message_bits) except: pass response = { 'well_formed': well_formed, 'message_bits': message_bits, # bitstring 'message': message_str, 'answer': answer } return jsonify(response)
def test_char_to_bits(self): b = simple_encoding.encode('a') self.assertEqual(b, '00000') b = simple_encoding.encode('b') self.assertEqual(b, '00001')
def test_challenge(self): rv = self.app.get('/api/challenge?username=patins') j = json.loads(rv.data) answer_str = app.get_answer_str('patins') self.assertEqual(j['message'], answer_str) self.assertEqual(j['message_bits'], simple_encoding.encode(answer_str))