Ejemplo n.º 1
0
    def test_login_success_created_user_4(self):
        print(
            '- Test: login success with a created user - change student number and employee number'
        )

        created_user = userApi.create_user({
            'first_name': 'User600',
            'last_name': 'Test',
            'email': '*****@*****.**',
            'username': '******',
            'student_number': None,
            'employee_number': None,
            #'puid': 'TEST00000600'
        })
        self.assertIsNotNone(created_user)
        self.assertIsNotNone(userApi.has_user_profile_created(created_user))
        self.assertIsNotNone(
            userApi.has_user_confidentiality_created(created_user))

        saml_data = {
            'auth': True,
            'attrs': {
                'first_name': created_user.first_name,
                'last_name': created_user.last_name,
                'email': created_user.email,
                'username': created_user.username,
                'student_number': '58684500',
                'employee_number': '8456300',
                #'puid': 'TEST00000600'
            }
        }
        user = self.saml_authenticate(saml_data)
        self.assertIsNotNone(user)
        self.assertEqual(user.username, saml_data['attrs']['username'])
        self.assertEqual(user.email, saml_data['attrs']['email'])
        self.assertEqual(user.first_name, saml_data['attrs']['first_name'])
        self.assertEqual(user.last_name, saml_data['attrs']['last_name'])
        self.assertIsNotNone(user.profile)
        self.assertEqual(user.profile.student_number,
                         saml_data['attrs']['student_number'])
        #self.assertEqual(user.profile.puid, saml_data['attrs']['puid'])

        roles = userApi.get_user_roles(user)
        self.assertEqual(roles, ['Student'])
        self.assertIsNotNone(user.confidentiality)
        self.assertEqual(user.confidentiality.employee_number,
                         saml_data['attrs']['employee_number'])
        self.assertFalse(user.confidentiality.is_new_employee)
Ejemplo n.º 2
0
    def saml_authenticate(self, saml_auth):
        ''' test saml authenticate function '''
        if not saml_auth:
            return None

        if saml_auth['auth']:
            user_data = saml_auth['attrs']

            if user_data['username'] == None:
                return 'SuspiciousOperation'

            if userApi.contain_user_duplicated_info(user_data) == True:
                return 'SuspiciousOperation'

            user = userApi.user_exists(user_data)
            if user == None:
                user = userApi.create_user(user_data)
            return user

        return None
Ejemplo n.º 3
0
def authenticate(saml_authentication=None):
    """ Create a new user if the user does not exist; otherwise, return a user """

    if not saml_authentication:
        return None

    if saml_authentication.is_authenticated():
        user_data = {
            'first_name': None,
            'last_name': None,
            'username': None,
            'email': None,
            'employee_number': None,
            'student_number': None
        }

        for key, value in saml_authentication.get_attributes().items():
            if '100.1.1' in key:
                user_data['username'] = value[0]
            elif '100.1.3' in key:
                user_data['email'] = value[0]
            elif '2.5.4.42' in key:
                user_data['first_name'] = value[0]
            elif '2.5.4.4' in key:
                user_data['last_name'] = value[0]
            elif '3.1.3' in key:
                user_data['employee_number'] = value[0]
            elif 'ubcEduStudentNumber' in key:
                user_data['student_number'] = value[0]

        if user_data['username'] == None:
            raise SuspiciousOperation

        if userApi.contain_user_duplicated_info(user_data) == True:
            raise SuspiciousOperation

        user = userApi.user_exists(user_data)
        if user == None:
            user = userApi.create_user(user_data)
        return user
    return None