Пример #1
0
def test_inactivate_match_noop(app):
    with app.app_context():
        g.user_id = '1'
        auth_service = AuthService('secret', None, None)
        match_service = MatchService()

        # Check user 2 is as an active match for user 1
        mock_matches_1 = {
            '2': {
                'active': True,
                'percent_unlocked': 0,
                'profile': {
                    'animal': '',
                    'color': '',
                    'gender': '',
                    'preferred_gender': '',
                    'first_name': '',
                    'last_name': '',
                    'profile_picture': '',
                    'interests': {}
                }
            }
        }
        matches_for_1 = match_service.get_current_matches()
        assert matches_for_1 == mock_matches_1

        matched_deleted = match_service.inactivate_match(3)
        assert matched_deleted == False

        # Since user 3 is not a match for user 1, there is no change to matches
        matches_for_1 = match_service.get_current_matches()
        assert matches_for_1 == mock_matches_1
Пример #2
0
def test_find_match_3(app):
    with app.app_context():
        g.user_id = '1'
        auth_service = AuthService('secret')
        match_service = MatchService()

        # Check user 1 and user 3 are currently not matched
        matches_for_1 = match_service.get_current_matches()
        assert '3' not in matches_for_1.keys()
        g.user_id = '3'
        matches_for_3 = match_service.get_current_matches()
        assert '1' not in matches_for_3.keys()
        g.user_id = '1'

        # Check that only one match is generated because user 2 is
        # already in user 1's match history
        find_match_for_1 = match_service.find_match(1, 2)
        assert len(find_match_for_1) == 1

        # Check user 3 got added as an active match for user 1
        matches_for_1 = match_service.get_current_matches()
        assert len(matches_for_1) == 2
        assert matches_for_1['3']['profile']['interests'] == {'': ''}

        # Check user 1 got added as an active match for user 3
        g.user_id = '3'
        matches_for_3 = match_service.get_current_matches()
        assert len(matches_for_3) == 1
        assert matches_for_3['1']['profile']['interests'] == {'': ''}
Пример #3
0
def test_find_match_1(app):
    with app.app_context():
        g.user_id = '3'
        auth_service = AuthService('secret')
        match_service = MatchService()

        # Check user 3 and user 4 are currently not matched
        matches_for_3 = match_service.get_current_matches()
        assert '4' not in matches_for_3.keys()
        g.user_id = '4'
        matches_for_4 = match_service.get_current_matches()
        assert '3' not in matches_for_4.keys()
        g.user_id = '3'

        # Check users 3 and 4 got matched on interest3
        find_match_for_3 = match_service.find_match(3, 1)
        match_result = (4, {'interest3': 'value3'})
        assert len(find_match_for_3) == 1
        assert match_result in find_match_for_3

        # Check user 4 got added as an active match for user 3
        matches_for_3 = match_service.get_current_matches()
        assert len(matches_for_3) == 1
        assert matches_for_3['4']['profile']['interests'] == {
            'interest3': 'value3'
        }

        # Check user 3 got added as an active match for user 4
        g.user_id = '4'
        matches_for_4 = match_service.get_current_matches()
        assert len(matches_for_4) == 1
        assert matches_for_4['3']['profile']['interests'] == {
            'interest3': 'value3'
        }
Пример #4
0
def test_inactivate_match_1(app):
    with app.app_context():
        g.user_id = '1'
        auth_service = AuthService('secret', None, None)
        match_service = MatchService()

        # Check user 2 is as an active match for user 1
        mock_matches_1 = {
            '2': {
                'active': True,
                'percent_unlocked': 0,
                'profile': {
                    'animal': '',
                    'color': '',
                    'gender': '',
                    'preferred_gender': '',
                    'first_name': '',
                    'last_name': '',
                    'profile_picture': '',
                    'interests': {}
                }
            }
        }
        matches_for_1 = match_service.get_current_matches()
        assert matches_for_1 == mock_matches_1

        # Check user 1 is as an active match for user 2
        g.user_id = '2'
        mock_matches_2 = {
            '1': {
                'active': True,
                'percent_unlocked': 0,
                'profile': {
                    'animal': '',
                    'color': '',
                    'gender': '',
                    'preferred_gender': '',
                    'first_name': '',
                    'last_name': '',
                    'profile_picture': '',
                    'interests': {}
                }
            }
        }
        matches_for_2 = match_service.get_current_matches()
        assert matches_for_2 == mock_matches_2
        g.user_id = '1'

        match_deleted = match_service.inactivate_match(2)
        assert match_deleted == True

        # Since both users only have one match each, they should no longer have any
        matches_for_1 = match_service.get_current_matches()
        assert matches_for_1 == {}
        g.user_id = '2'
        matches_for_2 = match_service.get_current_matches()
        assert matches_for_2 == {}
Пример #5
0
def test_find_match_2(app):
    with app.app_context():
        g.user_id = '3'
        auth_service = AuthService('secret')
        match_service = MatchService()

        # Check user 3 and users 1 and 4 are currently not matched
        matches_for_3 = match_service.get_current_matches()
        assert '1' not in matches_for_3.keys()
        assert '4' not in matches_for_3.keys()
        g.user_id = '4'
        matches_for_4 = match_service.get_current_matches()
        assert '3' not in matches_for_4.keys()
        g.user_id = '1'
        matches_for_1 = match_service.get_current_matches()
        assert '3' not in matches_for_1.keys()
        g.user_id = '3'

        find_match_for_3 = match_service.find_match(3, 2)
        assert len(find_match_for_3) == 2

        # Check users 1 and 4 got added as an active match for user 3
        # User 4 is matched based on the shared interest3
        # User 1 is matched even without common interests because it is the only
        # other user in the database satisying user 3's gender preferences
        matches_for_3 = match_service.get_current_matches()
        assert len(matches_for_3) == 2
        assert matches_for_3['4']['profile']['interests'] == {
            'interest3': 'value3'
        }
        assert matches_for_3['1']['profile']['interests'] == {'': ''}

        # Check user 3 got added as an active match for user 4
        g.user_id = '4'
        matches_for_4 = match_service.get_current_matches()
        assert len(matches_for_4) == 1
        assert matches_for_4['3']['profile']['interests'] == {
            'interest3': 'value3'
        }

        # Check user 3 got added as an active match for user 1
        g.user_id = '1'
        matches_for_1 = match_service.get_current_matches()
        assert len(matches_for_1) == 2
        assert matches_for_1['3']['profile']['interests'] == {'': ''}
Пример #6
0
def test_get_current_matches(app):
    with app.app_context():
        g.user_id = '3'
        auth_service = AuthService('secret', None, None)
        match_service = MatchService()
        no_matches = match_service.get_current_matches()
        assert no_matches == {}

    with app.app_context():
        g.user_id = '1'
        auth_service = AuthService('secret', None, None)
        match_service = MatchService()
        matches = match_service.get_current_matches()

        mock_matches = {
            '2': {
                'active': True,
                'percent_unlocked': 0,
                'profile': {
                    'animal': '',
                    'color': '',
                    'gender': '',
                    'preferred_gender': '',
                    'first_name': '',
                    'last_name': '',
                    'profile_picture': '',
                    'interests': {}
                }
            }
        }

        assert matches == mock_matches
Пример #7
0
def test_add_match_to_profile_with_duplicate_match(app):
    with app.app_context():
        g.user_id = '1'
        auth_service = AuthService('secret', None, None)
        match_service = MatchService()

        # Check user 1 and user 2 are currently matched
        matches_for_1 = match_service.get_current_matches()
        assert '2' in matches_for_1.keys()
        g.user_id = '2'
        matches_for_2 = match_service.get_current_matches()
        assert '1' in matches_for_2.keys()
        g.user_id = '1'

        # Simulate duplicate match between user 1 and user 2 on interest, should fail
        matched_interest = {'test_key': 'test_val'}
        created_match = match_service.add_match_to_profile(2, matched_interest)
        assert created_match == False

        # Check user 2 did not get added as a duplicate match for user 1
        mock_matches_1 = {
            '2': {
                'active': True,
                'percent_unlocked': 0,
                'profile': {
                    'animal': '',
                    'color': '',
                    'gender': '',
                    'preferred_gender': '',
                    'first_name': '',
                    'last_name': '',
                    'profile_picture': '',
                    'interests': {}
                }
            }
        }
        matches_for_1 = match_service.get_current_matches()
        assert len(matches_for_1) == 1
        assert matches_for_1['2'] == mock_matches_1['2']
Пример #8
0
def add_routes(app, socketio):
    """
  Adds callable endpoints to Flask.

  :param app: The configured Flask backend application to add endpoints to.
  :param socketio: The configured socketio instance
  :return: None.
  """
    auth_service = AuthService(app.config['SECRET_KEY'])

    @app.before_request
    def check_for_token():
        if request.headers.get('Authorization') is not None:
            token = request.headers.get('Authorization')
            auth_service.validate_token(token)

    # register user views
    user_view = UserAPI.as_view('user_api', auth_service)
    profile_view = auth_service.login_required(
        ProfileAPI.as_view('profile_api', auth_service))
    app.add_url_rule('/api/users', view_func=user_view, methods=['POST'])
    app.add_url_rule('/api/users',
                     view_func=profile_view,
                     methods=['GET', 'PUT'])

    # register thread views
    thread_service = ThreadService()
    thread_view = auth_service.login_required(
        ThreadAPI.as_view('thread_api', auth_service))
    thread_message_view = auth_service.login_required(
        MessageAPI.as_view('message_api', thread_service, auth_service))
    app.add_url_rule('/api/threads', view_func=thread_view, methods=['GET'])
    app.add_url_rule('/api/threads/<int:thread_id>/messages',
                     view_func=thread_message_view,
                     methods=['GET'])
    app.add_url_rule('/api/threads/<int:user_id>',
                     view_func=thread_message_view,
                     methods=['DELETE'])

    # register match views
    match_service = MatchService()
    match_view = auth_service.login_required(
        MatchAPI.as_view('match_api', match_service, auth_service,
                         thread_service))
    app.add_url_rule('/api/matches', view_func=match_view, methods=['GET'])
    app.add_url_rule('/api/matches/<int:user_id>',
                     view_func=match_view,
                     methods=['DELETE'])

    # register socket
    socketio.on_namespace(
        ThreadSockets(None, auth_service, thread_service, match_service,
                      app.logger))

    @socketio.on_error_default
    def handle_socket_error(e):
        emit('error', e.to_dict())

    @app.route('/')
    def health_check():
        return jsonify({'health': 'ok'})

    @app.route('/api/force_match/<int:user_id>')
    def force_match(user_id):
        """
    Processes a HTTP GET request for the force match debugging REST API endpoint.

    .. code-block:: bash

      GET localhost:8000/api/force_match/2

    Example response:

    .. code-block:: json

      {
        "success": true
      }

    :return: a Flask HTTP response with a forced match success boolean.
    """
        user1 = auth_service.get_current_user()
        user2 = auth_service.get_user_for_user_id(user_id)
        user1_dict = user1.profile._asdict()
        user2_dict = user2.profile._asdict()
        user1_interests = set(user1_dict['interests'].keys())
        user2_interests = set(user2_dict['interests'].keys())
        shared_interests = user1_interests & user2_interests
        if len(shared_interests) == 0:
            return jsonify(success=False)
        shared_interest_key = shared_interests.pop()
        interest = {
            shared_interest_key: user1_dict['interests'][shared_interest_key]
        }
        success = match_service.add_match_to_profile(user_id,
                                                     interest,
                                                     force=True)
        thread = thread_service.create_thread(user1, user2, force=True)
        return jsonify(success=success and thread is not None)

    @app.route('/api/force_match_delete/<int:user_id>')
    def force_match_delete(user_id):
        """
    Processes a HTTP GET request for the force delete debugging REST API endpoint.

    .. code-block:: bash

      GET localhost:8000/api/force_match_delete/2

    Example response:

    .. code-block:: json

      {
        "success": true
      }

    :return: a Flask HTTP response with a forced delete success boolean.
    """
        delete_match = match_service.inactivate_match(user_id, purge=True)
        delete_thread = thread_service.delete_thread(user_id, purge=True)
        return jsonify(status=delete_match and delete_thread)

    @app.route('/api/force_unlock/<int:user_id>')
    def force_unlock(user_id):
        """
    Processes a HTTP GET request for the force profile unlock debugging REST API endpoint.

    .. code-block:: bash

      GET localhost:8000/api/force_match_delete/2

    Example response:

    .. code-block:: json

      {
        "progress": {
            "user_percent_unlocked": 25,
            "matched_user_percent_unlocked": 14,
            "user_unlocked_feature": {"interest5": "value5"}
            "matched_user_unlocked_feature": {"interest2": "value2"}
        }
      }

    :return: a Flask HTTP response with the next unlocked feature for the matched user and
    current user.
    """
        data = match_service.unlock_next_profile_feature(user_id)
        return jsonify(progress=data)
Пример #9
0
def test_unlock_next_profile_feature_2(app):
    with app.app_context():
        g.user_id = '1'
        auth_service = AuthService('secret', None, None)
        match_service = MatchService()

        # Check user 1 and user 2 are currently matched
        matches_for_1 = match_service.get_current_matches()
        assert '2' in matches_for_1.keys()
        g.user_id = '2'
        matches_for_2 = match_service.get_current_matches()
        assert '1' in matches_for_2.keys()
        g.user_id = '1'

        # Unlock next two profile features for user 1 and user 2
        #
        # Each user has 1 interest, so the unlocked percentages should be 25% after
        # first unlock because they have not yet unlocked each other's first_name,
        # last_name, or profile_picture; after the second unlock, the unlocked
        # percentages should be 50%.
        data = match_service.unlock_next_profile_feature(2)
        assert data['user_percent_unlocked'] == 25
        assert data['matched_user_percent_unlocked'] == 25
        data = match_service.unlock_next_profile_feature(2)
        assert data['user_percent_unlocked'] == 50
        assert data['matched_user_percent_unlocked'] == 50

        # Check user 2's interest and first_name show in the active match for user 1
        mock_matches_1 = {
            '2': {
                'active': True,
                'percent_unlocked': 50,
                'profile': {
                    'animal': '',
                    'color': '',
                    'gender': '',
                    'preferred_gender': '',
                    'first_name': 'Josephine',
                    'last_name': '',
                    'profile_picture': '',
                    'interests': {
                        'interest2': 'value2'
                    }
                }
            }
        }
        matches_for_1 = match_service.get_current_matches()
        assert matches_for_1['2'] == mock_matches_1['2']

        # Check user 1's interest and first_name show in the active match for user 2
        mock_matches_2 = {
            '1': {
                'active': True,
                'percent_unlocked': 50,
                'profile': {
                    'animal': '',
                    'color': '',
                    'gender': '',
                    'preferred_gender': '',
                    'first_name': 'Joe',
                    'last_name': '',
                    'profile_picture': '',
                    'interests': {
                        'interest1': 'value1'
                    }
                }
            }
        }
        g.user_id = '2'
        matches_for_2 = match_service.get_current_matches()
        assert matches_for_2['1'] == mock_matches_2['1']
Пример #10
0
def test_add_match_to_profile(app):
    with app.app_context():
        g.user_id = '1'
        auth_service = AuthService('secret', None, None)
        match_service = MatchService()

        # Check user 1 and user 3 are currently not matched
        matches_for_1 = match_service.get_current_matches()
        assert '3' not in matches_for_1.keys()
        g.user_id = '3'
        matches_for_3 = match_service.get_current_matches()
        assert '1' not in matches_for_3.keys()
        g.user_id = '1'

        # Simulate match between user 1 and user 3 on interest
        matched_interest = {'test_key': 'test_val'}
        created_match = match_service.add_match_to_profile(3, matched_interest)
        assert created_match == True

        # Check user 3 got added as an active match for user 1
        mock_matches_1 = {
            '3': {
                'active': True,
                'percent_unlocked': 25,
                'profile': {
                    'animal': '',
                    'color': '',
                    'gender': 'Female',
                    'preferred_gender': 'Male',
                    'first_name': '',
                    'last_name': '',
                    'profile_picture': '',
                    'interests': {
                        'test_key': 'test_val'
                    }
                }
            }
        }
        matches_for_1 = match_service.get_current_matches()
        assert len(matches_for_1) == 2
        assert matches_for_1['3'] == mock_matches_1['3']

        # Check user 1 got added as an active match for user 3
        mock_matches_3 = {
            '1': {
                'active': True,
                'percent_unlocked': 25,
                'profile': {
                    'animal': '',
                    'color': '',
                    'gender': 'Male',
                    'preferred_gender': 'Female',
                    'first_name': '',
                    'last_name': '',
                    'profile_picture': '',
                    'interests': {
                        'test_key': 'test_val'
                    }
                }
            }
        }
        g.user_id = '3'
        matches_for_3 = match_service.get_current_matches()
        assert len(matches_for_3) == 1
        assert matches_for_3['1'] == mock_matches_3['1']