def test_get_learning_context(db_conn, users_table): """ Expect to get the learning context. """ users_table.insert({ 'id': 'abcd1234', 'name': 'Dalton', 'email': '*****@*****.**', 'password': '******', }).run(db_conn) user = User.get(id='abcd1234') redis.set('learning_context_abcd1234', json.dumps({ 'card': {'entity_id': 'A'}, 'unit': {'entity_id': 'B'}, 'set': {'entity_id': 'C'}, })) assert user.get_learning_context() == { 'card': {'entity_id': 'A'}, 'unit': {'entity_id': 'B'}, 'set': {'entity_id': 'C'}, } redis.delete('learning_context_abcd1234') assert user.get_learning_context() == {}
def test_learn_card(db_conn, session, cards_table): """ Expect to get a card for learn mode. (200) """ cards_table.insert({ 'entity_id': 'tyui4567', 'unit_id': 'vbnm7890', 'created': r.now(), 'modified': r.now(), 'status': 'accepted', 'kind': 'choice', 'name': 'Meaning of Life', 'body': 'What is the meaning of life?', 'options': [{ 'value': '42', 'correct': True, 'feedback': 'Yay!', }, { 'value': 'love', 'correct': False, 'feedback': 'Boo!', }], 'order': 'set', 'max_options_to_show': 4, }).run(db_conn) redis.set( 'learning_context_abcd1234', json.dumps({ 'unit': { 'entity_id': 'vbnm7890' }, 'subject': { 'entity_id': 'jkl;1234' }, })) request = {'cookies': {'session_id': session}, 'db_conn': db_conn} code, response = routes.card.learn_card_route(request, 'tyui4567') 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 redis.delete('learning_context_abcd1234')
def log_out_user(request): """ Log out the given user. """ cookies = request.get('cookies', {}) session_id = cookies.get('session_id') if session_id: redis.delete(session_id)
def test_memoize_redis(): """ Expect to memoize the result of a function into Redis. """ def a(): return {'a': 1, 'z': 1} key = 'test_memoize_redis' redis.delete(key) assert memoize_redis(key, a) == {'a': 1, 'z': 1} redis.delete(key)
def test_respond_card(db_conn, units_table, cards_table, cards_parameters_table, responses_table, session): """ Expect to respond to a card. (200) """ cards_table.insert({ 'entity_id': 'tyui4567', 'unit_id': 'vbnm7890', 'created': r.now(), 'modified': r.now(), 'status': 'accepted', 'kind': 'choice', 'name': 'Meaning of Life', 'body': 'What is the meaning of life?', 'options': [{ 'value': '42', 'correct': True, 'feedback': 'Yay!', }, { 'value': 'love', 'correct': False, 'feedback': 'Boo!', }], 'order': 'set', 'max_options_to_show': 4, }).run(db_conn) cards_parameters_table.insert({ 'entity_id': 'tyui4567' }).run(db_conn) units_table.insert({ 'entity_id': 'vbnm7890', 'created': r.now(), }).run(db_conn) redis.set('learning_context_abcd1234', json.dumps({ 'set': {'entity_id': 'jkl;1234'}, 'card': {'entity_id': 'tyui4567'}, })) request = { 'params': {'response': '42'}, 'cookies': {'session_id': session}, 'db_conn': db_conn, } code, response = routes.card.respond_to_card_route(request, 'tyui4567') assert code == 200 assert 'response' in response assert 'feedback' in response redis.delete('learning_context_abcd1234')
def is_valid_token(user, token): """ Ensure the given token is valid. """ key = 'user_password_token_{id}'.format(id=user['id']) entoken = redis.get(key) redis.delete(key) if entoken: entoken = entoken.decode() return bcrypt.verify(user['id'] + token, entoken) return False
def test_learn_card_400(db_conn, cards_table, session): """ Expect the card for learn mode to make sense, given the learner context. (400) """ cards_table.insert({ 'entity_id': 'tyui4567', 'unit_id': 'vbnm7890', 'created': r.now(), 'modified': r.now(), 'status': 'accepted', 'kind': 'choice', 'name': 'Meaning of Life', 'body': 'What is the meaning of life?', 'options': [{ 'value': '42', 'correct': True, 'feedback': 'Yay!', }, { 'value': 'love', 'correct': False, 'feedback': 'Boo!', }], 'order': 'set', 'max_options_to_show': 4, }).run(db_conn) redis.set( 'learning_context_abcd1234', json.dumps({ 'unit': { 'entity_id': 'gfds6543' }, 'subject': { 'entity_id': '6543hgfs' }, })) request = {'cookies': {'session_id': session}, 'db_conn': db_conn} code, response = routes.card.learn_card_route(request, 'tyui4567') assert code == 400 redis.delete('learning_context_abcd1234')
def is_valid_token(self, token): """ Ensure the given token is valid. """ key = "user_password_token_{id}".format(id=self["id"]) entoken = redis.get(key) redis.delete(key) if entoken: entoken = entoken.decode() return bcrypt.verify(self["id"] + token, entoken) return False
def test_respond_card(db_conn, units_table, cards_table, cards_parameters_table, responses_table, session): """ Expect to respond to a card. (200) """ cards_table.insert({ 'entity_id': 'tyui4567', 'unit_id': 'vbnm7890', 'created': r.now(), 'modified': r.now(), 'status': 'accepted', 'kind': 'choice', 'name': 'Meaning of Life', 'body': 'What is the meaning of life?', 'options': [{ 'value': '42', 'correct': True, 'feedback': 'Yay!', }, { 'value': 'love', 'correct': False, 'feedback': 'Boo!', }], 'order': 'set', 'max_options_to_show': 4, }).run(db_conn) units_table.insert({ 'entity_id': 'vbnm7890', 'created': r.now(), }).run(db_conn) redis.set('learning_context_abcd1234', json.dumps({ 'set': {'entity_id': 'jkl;1234'}, 'card': {'entity_id': 'tyui4567'}, })) request = { 'params': {'response': '42'}, 'cookies': {'session_id': session}, 'db_conn': db_conn, } code, response = routes.card.respond_to_card_route(request, 'tyui4567') assert code == 200 assert 'response' in response assert 'feedback' in response redis.delete('learning_context_abcd1234')
def test_learn_card(db_conn, session, cards_table): """ Expect to get a card for learn mode. (200) """ cards_table.insert({ 'entity_id': 'tyui4567', 'unit_id': 'vbnm7890', 'created': r.now(), 'modified': r.now(), 'status': 'accepted', 'kind': 'choice', 'name': 'Meaning of Life', 'body': 'What is the meaning of life?', 'options': [{ 'value': '42', 'correct': True, 'feedback': 'Yay!', }, { 'value': 'love', 'correct': False, 'feedback': 'Boo!', }], 'order': 'set', 'max_options_to_show': 4, }).run(db_conn) redis.set('learning_context_abcd1234', json.dumps({ 'unit': {'entity_id': 'vbnm7890'}, 'set': {'entity_id': 'jkl;1234'}, })) request = {'cookies': {'session_id': session}} code, response = routes.card.learn_card_route(request, 'tyui4567') 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 'set' in response assert 'unit' in response redis.delete('learning_context_abcd1234')
def test_respond_card_400a(db_conn, session, cards_table): """ Expect the card being responded to make sense, given the learner context. (400) """ cards_table.insert({ 'entity_id': 'tyui4567', 'unit_id': 'vbnm7890', 'created': r.now(), 'modified': r.now(), 'status': 'accepted', 'kind': 'choice', 'name': 'Meaning of Life', 'body': 'What is the meaning of life?', 'options': [{ 'value': '42', 'correct': True, 'feedback': 'Yay!', }, { 'value': 'love', 'correct': False, 'feedback': 'Boo!', }], 'order': 'set', 'max_options_to_show': 4, }).run(db_conn) redis.set('learning_context_abcd1234', json.dumps({ 'unit': {'entity_id': 'vbnm7890'}, 'set': {'entity_id': 'jkl;1234'}, 'card': {'entity_id': 'gfds3456'}, })) request = { 'params': {'response': '42'}, 'cookies': {'session_id': session} } code, response = routes.card.respond_to_card_route(request, 'tyui4567') assert code == 400 redis.delete('learning_context_abcd1234')
def test_get_learning_context(db_conn, users_table): """ Expect to get the learning context. """ users_table.insert({"id": "abcd1234", "name": "Dalton", "email": "*****@*****.**", "password": "******"}).run( db_conn ) user = get_user({"id": "abcd1234"}, db_conn) redis.set( "learning_context_abcd1234", json.dumps({"card": {"entity_id": "A"}, "unit": {"entity_id": "B"}, "set": {"entity_id": "C"}}), ) assert get_learning_context(user) == { "card": {"entity_id": "A"}, "unit": {"entity_id": "B"}, "set": {"entity_id": "C"}, } redis.delete("learning_context_abcd1234") assert get_learning_context(user) == {}