def test_respond_card_400b(db_conn, session): """ Expect response to a card to make sense. (400) """ create_test_cards(db_conn) redis_key = 'learning_context_{user_id}'.format( user_id=convert_uuid_to_slug(user_id)) red.set( redis_key, json.dumps({ 'card': { 'entity_id': convert_uuid_to_slug(card_id) }, 'unit': { 'entity_id': convert_uuid_to_slug(unit_id) }, })) request = { 'params': { 'response': convert_uuid_to_slug(uuid.uuid4()) }, 'cookies': { 'session_id': session }, 'db_conn': db_conn, } code, response = routes.card.respond_to_card_route(request, card_id) red.delete(redis_key) assert code == 400 assert 'errors' in response
def test_respond_card(db_conn, session): """ Expect to respond to a card. (200) """ create_test_cards(db_conn) redis_key = 'learning_context_{user_id}'.format( user_id=convert_uuid_to_slug(user_id)) red.set( redis_key, json.dumps({ 'card': { 'entity_id': convert_uuid_to_slug(card_id) }, 'unit': { 'entity_id': convert_uuid_to_slug(unit_id) }, })) request = { 'params': { 'response': convert_uuid_to_slug(good_response_id) }, 'cookies': { 'session_id': session }, 'db_conn': db_conn, } code, response = routes.card.respond_to_card_route(request, card_id) red.delete(redis_key) assert not response.get('errors') assert code == 200 assert 'response' in response assert 'feedback' in response
def test_learn_card(db_conn, session): """ Expect to get a card for learn mode. (200) """ create_test_cards(db_conn) redis_key = 'learning_context_{user_id}'.format( user_id=convert_uuid_to_slug(user_id)) red.set( redis_key, json.dumps({ 'unit': { 'entity_id': convert_uuid_to_slug(unit_id) }, })) request = {'cookies': {'session_id': session}, 'db_conn': db_conn} code, response = routes.card.learn_card_route( request, convert_uuid_to_slug(card_id)) red.delete(redis_key) assert not response.get('errors') assert code == 200 assert 'order' not in response['card'] # TODO-3 assert 'correct' not in response['card']['options'][0] # TODO-3 assert 'feedback' not in response['card']['options'][0] assert 'subject' in response assert 'unit' in response
def log_out_user(request): """ Log out the given user. """ cookies = request.get('cookies', {}) session_id = cookies.get('session_id') if session_id: red.delete(session_id)
def is_valid_token(user, token): """ Ensure the given token is valid. """ slugged_user_id = convert_uuid_to_slug(user['id']) key = 'user_password_token_{id}'.format(id=slugged_user_id) entoken = red.get(key) red.delete(key) if entoken: entoken = entoken.decode() return bcrypt.verify(slugged_user_id + token, entoken) return False
def test_learn_card_400(db_conn, session): """ Expect the card for learn mode to make sense, given the learner context. (400) """ create_test_cards(db_conn) redis_key = 'learning_context_{user_id}'.format( user_id=convert_uuid_to_slug(user_id)) red.set( redis_key, json.dumps({ 'unit': { 'entity_id': convert_uuid_to_slug(uuid.uuid4()) }, })) request = {'cookies': {'session_id': session}, 'db_conn': db_conn} code, _ = routes.card.learn_card_route(request, convert_uuid_to_slug(card_id)) red.delete(redis_key) assert code == 400
def test_memoize_redis(): """ Expect to memoize the result of a function into Redis. """ value = {'a': 1, 'z': 1} def a(): return value key = 'test_memoize_redis' red.delete(key) # try memoization assert memoize_redis(key, a) == value assert memoize_redis(key, a) == value # a second time, memoized # try a bad value red.setex(key, 24 * 60 * 60, b'\x80abc') assert memoize_redis(key, a) == b'\x80abc' # clean up red.delete(key)