class TestLTIAuthMiddleware(unittest.TestCase):
    longMessage = True

    def setUp(self):
        self.mw = LTIAuthMiddleware()

    def build_lti_launch_request(self, post_data):
        """
        Utility method that builds a fake lti launch request with custom data.
        """
        # Add message type to post data
        post_data.update(lti_message_type='basic-lti-launch-request')
        # Add resource_link_id to post data
        post_data.update(resource_link_id='d202fb112a14f27107149ed874bf630aa8e029a5')

        request = RequestFactory().post('/fake/lti/launch', post_data)
        request.user = mock.Mock(name='User', spec=models.User)
        request.session = {}
        return request

    @patch('django_auth_lti.middleware.auth')
    def test_roles_merged_with_custom_roles(self, mock_auth, mock_logger):
        """
        Assert that 'roles' list in session contains merged set of roles when custom role key is
        defined and values have been passed in.
        """
        request = self.build_lti_launch_request({
            'roles': 'RoleOne,RoleTwo',
            'test_custom_role_key': 'My,Custom,Roles',
        })
        with patch('django_auth_lti.middleware.settings', LTI_CUSTOM_ROLE_KEY='test_custom_role_key'):
            self.mw.process_request(request)
        self.assertEqual(request.LTI.get('roles'), ['RoleOne', 'RoleTwo', 'My', 'Custom', 'Roles'])

    @patch('django_auth_lti.middleware.auth')
    def test_roles_merge_with_empty_custom_roles(self, mock_auth, mock_logger):
        """
        Assert that 'roles' list in session contains original set when custom role key is defined with empty data.
        """
        request = self.build_lti_launch_request({
            'roles': 'RoleOne,RoleTwo',
            'test_custom_role_key': '',
        })
        with patch('django_auth_lti.middleware.settings', LTI_CUSTOM_ROLE_KEY='test_custom_role_key'):
            self.mw.process_request(request)
        self.assertEqual(request.LTI.get('roles'), ['RoleOne', 'RoleTwo'])

    @patch('django_auth_lti.middleware.auth')
    def test_roles_not_merged_with_no_role_key(self, mock_auth, mock_logger):
        """
        Assert that 'roles' list in session contains original set when no custom role key is defined.
        """
        request = self.build_lti_launch_request({
            'roles': 'RoleOne,RoleTwo',
            'test_custom_role_key': 'My,Custom,Roles',
        })
        self.mw.process_request(request)
        self.assertEqual(request.LTI.get('roles'), ['RoleOne', 'RoleTwo'])
Beispiel #2
0
class TestLTIAuthMiddleware(unittest.TestCase):
    longMessage = True

    def setUp(self):
        self.mw = LTIAuthMiddleware()

    def build_lti_launch_request(self, post_data):
        return helpers.build_lti_launch_request(post_data)

    @patch('django_auth_lti.middleware.auth')
    def test_roles_merged_with_custom_roles(self, mock_auth, mock_logger):
        """
        Assert that 'roles' list in session contains merged set of roles when custom role key is
        defined and values have been passed in.
        """
        request = self.build_lti_launch_request({
            'roles':
            'RoleOne,RoleTwo',
            'test_custom_role_key':
            'My,Custom,Roles',
        })
        with patch('django_auth_lti.middleware.settings',
                   LTI_CUSTOM_ROLE_KEY='test_custom_role_key'):
            self.mw.process_request(request)
        self.assertEqual(request.LTI.get('roles'),
                         ['RoleOne', 'RoleTwo', 'My', 'Custom', 'Roles'])

    @patch('django_auth_lti.middleware.auth')
    def test_roles_merge_with_empty_custom_roles(self, mock_auth, mock_logger):
        """
        Assert that 'roles' list in session contains original set when custom role key is defined with empty data.
        """
        request = self.build_lti_launch_request({
            'roles': 'RoleOne,RoleTwo',
            'test_custom_role_key': '',
        })
        with patch('django_auth_lti.middleware.settings',
                   LTI_CUSTOM_ROLE_KEY='test_custom_role_key'):
            self.mw.process_request(request)
        self.assertEqual(request.LTI.get('roles'), ['RoleOne', 'RoleTwo'])

    @patch('django_auth_lti.middleware.auth')
    def test_roles_not_merged_with_no_role_key(self, mock_auth, mock_logger):
        """
        Assert that 'roles' list in session contains original set when no custom role key is defined.
        """
        request = self.build_lti_launch_request({
            'roles':
            'RoleOne,RoleTwo',
            'test_custom_role_key':
            'My,Custom,Roles',
        })
        self.mw.process_request(request)
        self.assertEqual(request.LTI.get('roles'), ['RoleOne', 'RoleTwo'])
class TestLTIAuthMiddleware(unittest.TestCase):
    longMessage = True

    def setUp(self):
        self.mw = LTIAuthMiddleware()

    def build_lti_launch_request(self, post_data):
        return helpers.build_lti_launch_request(post_data)

    @patch('django_auth_lti.middleware.auth')
    def test_roles_merged_with_custom_roles(self, mock_auth, mock_logger):
        """
        Assert that 'roles' list in session contains merged set of roles when custom role key is
        defined and values have been passed in.
        """
        request = self.build_lti_launch_request({
            'roles': 'RoleOne,RoleTwo',
            'test_custom_role_key': 'My,Custom,Roles',
        })
        with patch('django_auth_lti.middleware.settings', LTI_CUSTOM_ROLE_KEY='test_custom_role_key'):
            self.mw.process_request(request)
        self.assertEqual(request.LTI.get('roles'), ['RoleOne', 'RoleTwo', 'My', 'Custom', 'Roles'])

    @patch('django_auth_lti.middleware.auth')
    def test_roles_merge_with_empty_custom_roles(self, mock_auth, mock_logger):
        """
        Assert that 'roles' list in session contains original set when custom role key is defined with empty data.
        """
        request = self.build_lti_launch_request({
            'roles': 'RoleOne,RoleTwo',
            'test_custom_role_key': '',
        })
        with patch('django_auth_lti.middleware.settings', LTI_CUSTOM_ROLE_KEY='test_custom_role_key'):
            self.mw.process_request(request)
        self.assertEqual(request.LTI.get('roles'), ['RoleOne', 'RoleTwo'])

    @patch('django_auth_lti.middleware.auth')
    def test_roles_not_merged_with_no_role_key(self, mock_auth, mock_logger):
        """
        Assert that 'roles' list in session contains original set when no custom role key is defined.
        """
        request = self.build_lti_launch_request({
            'roles': 'RoleOne,RoleTwo',
            'test_custom_role_key': 'My,Custom,Roles',
        })
        self.mw.process_request(request)
        self.assertEqual(request.LTI.get('roles'), ['RoleOne', 'RoleTwo'])
 def setUp(self):
     self.mw = LTIAuthMiddleware()
Beispiel #5
0
class TestLTIAuthMiddleware(unittest.TestCase):
    longMessage = True

    def setUp(self):
        self.mw = LTIAuthMiddleware()

    def build_lti_launch_request(self, post_data):
        """
        Utility method that builds a fake lti launch request with custom data.
        """
        # Add message type to post data
        post_data.update(lti_message_type='basic-lti-launch-request')
        # Add resource_link_id to post data
        post_data.update(
            resource_link_id='d202fb112a14f27107149ed874bf630aa8e029a5')

        request = RequestFactory().post('/fake/lti/launch', post_data)
        request.user = mock.Mock(name='User', spec=models.User)
        request.session = {}
        return request

    @patch('django_auth_lti.middleware.auth')
    def test_roles_merged_with_custom_roles(self, mock_auth, mock_logger):
        """
        Assert that 'roles' list in session contains merged set of roles when custom role key is
        defined and values have been passed in.
        """
        request = self.build_lti_launch_request({
            'roles':
            'RoleOne,RoleTwo',
            'test_custom_role_key':
            'My,Custom,Roles',
        })
        with patch('django_auth_lti.middleware.settings',
                   LTI_CUSTOM_ROLE_KEY='test_custom_role_key'):
            self.mw.process_request(request)
        self.assertEqual(request.LTI.get('roles'),
                         ['RoleOne', 'RoleTwo', 'My', 'Custom', 'Roles'])

    @patch('django_auth_lti.middleware.auth')
    def test_roles_merge_with_empty_custom_roles(self, mock_auth, mock_logger):
        """
        Assert that 'roles' list in session contains original set when custom role key is defined with empty data.
        """
        request = self.build_lti_launch_request({
            'roles': 'RoleOne,RoleTwo',
            'test_custom_role_key': '',
        })
        with patch('django_auth_lti.middleware.settings',
                   LTI_CUSTOM_ROLE_KEY='test_custom_role_key'):
            self.mw.process_request(request)
        self.assertEqual(request.LTI.get('roles'), ['RoleOne', 'RoleTwo'])

    @patch('django_auth_lti.middleware.auth')
    def test_roles_not_merged_with_no_role_key(self, mock_auth, mock_logger):
        """
        Assert that 'roles' list in session contains original set when no custom role key is defined.
        """
        request = self.build_lti_launch_request({
            'roles':
            'RoleOne,RoleTwo',
            'test_custom_role_key':
            'My,Custom,Roles',
        })
        self.mw.process_request(request)
        self.assertEqual(request.LTI.get('roles'), ['RoleOne', 'RoleTwo'])