def test_result_creation_with_invalid_winner(self): card_a, card_b, invalid_winner = random_cards(3) pairing = { 'cards': [card_a, card_b], 'uuid': '1234', 'iat': datetime.now() - timedelta(minutes=5), 'exp': datetime.now() - timedelta(minutes=5) + timedelta(days=30) } with self.assertRaises(InvalidWinner): Result(invalid_winner, pairing, self.result_storage)
def test_duplicate_result_creation(self): winner, loser = random_cards(2) pairing = { 'cards': [winner, loser], 'uuid': '1234', 'iat': datetime.now() - timedelta(minutes=5), 'exp': datetime.now() - timedelta(minutes=5) + timedelta(days=30) } Result(winner, pairing, self.result_storage) with self.assertRaises(DuplicateResult): Result(loser, pairing, self.result_storage)
def test_lookup(self): winner, loser = random_cards(2) result_storage = InMemoryResultStorage() result_storage.register({ 'winner': winner, 'pairing': { 'cards': [winner, loser], 'uuid': '1234567890', } }) self.assertIsNotNone(result_storage.lookup({'pairing.uuid': '1234567890'})) self.assertIsNone(result_storage.lookup({'pairing.uuid': 'notthere'}))
def test_submit_pairing_with_expired_token(self): cards = random_cards(2) expired_jwt = jwt.encode( { 'exp': datetime.utcnow() - timedelta(days=30), 'cards': cards }, app.config['SIGNING_KEY'], algorithm='HS256' ).decode('utf-8') headers = {'authorization': 'bearer ' + expired_jwt} result = {'winner': cards[0]} response = self.client.post('/result', json=result, headers=headers) self.assertEqual(response.status_code, 401)
def test_several_item_ranking(self): first, second, third = random_cards(3) results = [ { 'winner': first, 'claims': { 'cards': [first, second], 'uuid': '1', 'iat': datetime.now() - timedelta(minutes=5), 'exp': datetime.now() - timedelta(minutes=5) + timedelta(days=30) } }, { 'winner': first, 'claims': { 'cards': [first, third], 'uuid': '2', 'iat': datetime.now() - timedelta(minutes=5), 'exp': datetime.now() - timedelta(minutes=5) + timedelta(days=30) } }, { 'winner': second, 'claims': { 'cards': [second, third], 'uuid': '3', 'iat': datetime.now() - timedelta(minutes=5), 'exp': datetime.now() - timedelta(minutes=5) + timedelta(days=30) } } ] for result in results: Result(result['winner'], result['claims'], self.result_storage) ranking = generate_ranking(self.card_storage, self.result_storage) expected_ranking = [ { 'card': first, 'score': 2 }, { 'card': second, 'score': 1 } ] self.assertEqual(ranking, expected_ranking)
def test_single_item_ranking(self): winner, loser = random_cards(2) pairing = { 'cards': [winner, loser], 'uuid': '1', 'iat': datetime.now() - timedelta(minutes=5), 'exp': datetime.now() - timedelta(minutes=5) + timedelta(days=30) } Result(winner, pairing, self.result_storage) ranking = generate_ranking(self.card_storage, self.result_storage) expected_ranking = [ { 'card': winner, 'score': 1 } ] self.assertEqual(ranking, expected_ranking)
def test_result_creation(self): winner, loser = random_cards(2) pairing = { 'cards': [winner, loser], 'uuid': '1234', 'iat': datetime.now() - timedelta(minutes=5), 'exp': datetime.now() - timedelta(minutes=5) + timedelta(days=30) } try: result = Result(winner, pairing, self.result_storage) except: self.fail('Could not create result.') stored_results = list(result._storage._results) self.assertEqual(len(stored_results), 1) result = stored_results[0] self.assertIn('winner', result.keys()) self.assertIn('pairing', result.keys()) self.assertEqual(result['winner'], winner) self.assertEqual(result['pairing'], pairing)
def test_multiple_result_ranking(self): first, second, third = random_cards(3) results = [ { 'winner': first, 'claims': { 'cards': [first, second], 'uuid': '1', 'iat': datetime.now() - timedelta(minutes=5), 'exp': datetime.now() - timedelta(minutes=5) + timedelta(days=30) } }, { 'winner': first, 'claims': { 'cards': [first, third], 'uuid': '2', 'iat': datetime.now() - timedelta(minutes=5), 'exp': datetime.now() - timedelta(minutes=5) + timedelta(days=30) } }, { 'winner': second, 'claims': { 'cards': [second, third], 'uuid': '3', 'iat': datetime.now() - timedelta(minutes=5), 'exp': datetime.now() - timedelta(minutes=5) + timedelta(days=30) } } ] for result in results: headers = { 'authorization': 'bearer ' + jwt.encode( result['claims'], app.config['HMAC_KEY'], algorithm='HS256' ).decode('utf-8') } self.client.post( '/result', json={'winner': result['winner']}, headers=headers ) result = self.client.get('/ranking') self.assertEqual(result.status_code, 200) # self.maxDiff = None ranking = result.json['ranking'] expected_ranking = [ { 'score': 2, 'card': first }, { 'score': 1, 'card': second }, ] self.assertEqual(len(ranking), len(expected_ranking)) for actual, expected in zip(ranking, expected_ranking): self.assertEqual(actual['score'], expected['score']) self.assertEqual(actual['card']['title'], expected['card']['title'])