def test_batch_get_or_create_user_ids_wrong_type(self):
     """
     Test if batch_get_or_create returns None if wrong type given
     """
     users = [UserFactory() for _ in range(2)]
     external_ids = ExternalId.batch_get_or_create_user_ids(
         users, 'invalid')
     assert external_ids is None
Ejemplo n.º 2
0
 def get_external_user_id(self, type_name):
     """
     Returns an external user id of the given type.
     Raises ValueError if the type doesn't exist.
     """
     external_id, _ = ExternalId.add_new_user_id(self._django_user, type_name)
     if not external_id:
         raise ValueError("External ID type: %s does not exist" % type_name)
     return str(external_id.external_user_id)
    def test_batch_get_or_create_user_ids_existing_ids(self):
        """
        Test batch creation output when there are existing ids for some user
        """
        id_type = ExternalIDTypeFactory.create(name='test')

        # first let's create some user and externalids for them
        users = [UserFactory() for _ in range(10)]

        with self.assertNumQueries(self.EXPECTED_NUM_OF_QUERIES):
            result = ExternalId.batch_get_or_create_user_ids(users, id_type)

        # now create some new user and try to create externalids for all user
        new_users = [UserFactory() for _ in range(5)]
        all_users = users + new_users

        with self.assertNumQueries(self.EXPECTED_NUM_OF_QUERIES):
            result = ExternalId.batch_get_or_create_user_ids(
                all_users, id_type)

        assert len(result) == len(all_users)
Ejemplo n.º 4
0
def batch_get_or_create_externalids(users):
    """
    Given a list of user, returns corresponding external id's

    External ID's are created when a student actually launches
    LTI from LMS. But when providing course member information
    to a third party tool, not every member has External ID's
    available. To create one by one would be a performance issue.
    This method provides a faster way to create ExternalIds in batch.
    """
    # pylint: disable=import-error,import-outside-toplevel
    from openedx.core.djangoapps.external_user_ids.models import ExternalId
    return ExternalId.batch_get_or_create_user_ids(users, 'lti')
    def test_batch_get_or_create_user_ids(self):
        """
        Test if batch_get_or_create creates ExternalIds in batch
        """
        id_type = ExternalIDTypeFactory.create(name='test')
        users = [UserFactory() for _ in range(10)]

        with self.assertNumQueries(self.EXPECTED_NUM_OF_QUERIES):
            result = ExternalId.batch_get_or_create_user_ids(users, id_type)

        assert len(result) == len(users)

        for user in users:
            assert result[user.id].external_id_type.name == 'test'
            assert result[user.id].user == user
Ejemplo n.º 6
0
def get_anonymous_user_id(username_or_id):
    """
    Generate anonymous user id.

    Generate anonymous id for student.
    In case of anonymous user, return random uuid.

    Arguments:
        username_or_id (str):     username for the learner

    Returns:
        str
    """
    user = user_id = username = None
    if username_or_id:
        try:
            user_id = int(username_or_id)
        except ValueError:
            username = username_or_id

    if username:
        user = User.objects.filter(username=username).first()
    elif user_id:
        user = User.objects.filter(id=user_id).first()

    if not user:
        logger.info('User with username "%s" does not exist. '
                    'Cannot generate anonymous ID', username_or_id)

        anonymous_id = str(uuid4())
    else:
        type_name = ExternalIdType.LTI
        external_id, _ = ExternalId.add_new_user_id(user, type_name)
        if not external_id:
            raise ValueError("External ID type: %s does not exist" % type_name)

        anonymous_id = str(external_id.external_user_id)

    return anonymous_id