Beispiel #1
0
    def test_associates_the_subscription_type_to_the_request_in_case_of_referer_header(
            self):
        # Setup
        request = self.factory.get('/')
        request.user = AnonymousUser()
        request.session = dict()
        request.META['HTTP_REFERER'] = 'http://www.umontreal.ca'

        middleware = SubscriptionMiddleware()
        valid_period = ValidJournalAccessSubscriptionPeriodFactory()
        subscription = valid_period.subscription
        InstitutionRefererFactory(subscription=subscription,
                                  referer="http://www.umontreal.ca")

        article = EmbargoedArticleFactory()
        subscription.journals.add(article.issue.journal)
        subscription.save()

        middleware.process_request(request)

        assert request.subscription_type == 'institution-referer'

        request = self.factory.get('/')
        request.user = AnonymousUser()
        request.session = dict()
        request.META['HTTP_REFERER'] = None
        middleware.process_request(request)
        assert request.subscription_type == 'open_access'
    def test_associates_the_subscription_type_to_the_request_in_case_of_institution(self):
        # Setup
        now_dt = dt.datetime.now()
        organisation = OrganisationFactory.create()
        subscription = JournalAccessSubscriptionFactory(
            organisation=organisation)
        JournalAccessSubscriptionPeriodFactory.create(
            subscription=subscription,
            start=now_dt - dt.timedelta(days=10),
            end=now_dt + dt.timedelta(days=8))
        InstitutionIPAddressRangeFactory.create(
            subscription=subscription,
            ip_start='192.168.1.2', ip_end='192.168.1.4')

        request = self.factory.get('/')
        parameters = request.META.copy()
        parameters['HTTP_X_FORWARDED_FOR'] = '192.168.1.3'
        request.META = parameters

        middleware = SubscriptionMiddleware()

        # Run
        middleware.process_request(request)

        # Check
        self.assertEqual(request.subscription_type, 'institution')
        self.assertEqual(request.subscription, subscription)
Beispiel #3
0
    def test_associates_the_subscription_type_to_the_request_in_case_of_institution(
            self):
        # Setup
        now_dt = dt.datetime.now()
        organisation = OrganisationFactory.create()
        subscription = JournalAccessSubscriptionFactory(
            organisation=organisation)
        JournalAccessSubscriptionPeriodFactory.create(
            subscription=subscription,
            start=now_dt - dt.timedelta(days=10),
            end=now_dt + dt.timedelta(days=8))
        InstitutionIPAddressRangeFactory.create(subscription=subscription,
                                                ip_start='192.168.1.2',
                                                ip_end='192.168.1.4')

        request = self.factory.get('/')
        request.user = AnonymousUser()
        request.session = dict()
        parameters = request.META.copy()
        parameters['HTTP_X_FORWARDED_FOR'] = '192.168.1.3'
        request.META = parameters

        middleware = SubscriptionMiddleware()

        # Run
        middleware.process_request(request)

        # Check
        self.assertEqual(request.subscription_type, 'institution')
        self.assertEqual(request.subscription, subscription)
Beispiel #4
0
    def test_subscription_middleware_log_requests_in_case_of_referer(
            self, mock_log):
        request = self.factory.get('/revues/shortname/issue/article.html')
        request.user = AnonymousUser()
        request.session = dict()
        request.META['HTTP_REFERER'] = 'http://www.umontreal.ca'

        middleware = SubscriptionMiddleware()
        valid_period = ValidJournalAccessSubscriptionPeriodFactory()
        subscription = valid_period.subscription
        InstitutionRefererFactory(subscription=subscription,
                                  referer="http://www.umontreal.ca")

        article = EmbargoedArticleFactory()
        subscription.journals.add(article.issue.journal)
        subscription.save()

        middleware.process_request(request)
        assert mock_log.info.call_count == 0
        middleware.process_response(request, HttpResponse())
        assert mock_log.info.call_count == 1

        request = self.factory.get('/')
        request.user = AnonymousUser()
        request.session = dict()
        request.META['HTTP_REFERER'] = 'http://www.no-referer.ca'

        middleware.process_request(request)
        middleware.process_response(request, HttpResponse())
        assert mock_log.info.call_count == 1
    def test_associates_the_subscription_type_to_the_request_in_case_of_open_access(self):
        # Setup
        request = self.factory.get('/')
        request.user = AnonymousUser()
        middleware = SubscriptionMiddleware()

        # Run
        middleware.process_request(request)

        # Check
        self.assertTrue(request.subscription_type == 'open_access')
Beispiel #6
0
    def test_associates_the_subscription_type_to_the_request_in_case_of_open_access(
            self):
        # Setup
        request = self.factory.get('/')
        request.user = AnonymousUser()
        request.session = dict()
        middleware = SubscriptionMiddleware()

        # Run
        middleware.process_request(request)

        # Check
        self.assertTrue(request.subscription_type == 'open_access')
Beispiel #7
0
    def test_associates_the_subscription_type_to_the_request_in_case_of_individual_access(self):
        # Setup
        now_dt = dt.datetime.now()
        user = UserFactory()
        subscription = JournalAccessSubscriptionFactory.create(user=user, journal=self.journal)
        JournalAccessSubscriptionPeriodFactory.create(
            subscription=subscription,
            start=now_dt - dt.timedelta(days=10),
            end=now_dt + dt.timedelta(days=8))

        request = self.factory.get('/')
        request.user = user
        middleware = SubscriptionMiddleware()

        # Run
        middleware.process_request(request)

        # Check
        self.assertTrue(request.subscription_type == 'individual')
Beispiel #8
0
    def test_associates_the_subscription_type_to_the_request_in_case_of_individual_access(
            self):
        # Setup
        now_dt = dt.datetime.now()
        user = UserFactory()
        subscription = JournalAccessSubscriptionFactory.create(
            user=user, journal=self.journal)
        JournalAccessSubscriptionPeriodFactory.create(
            subscription=subscription,
            start=now_dt - dt.timedelta(days=10),
            end=now_dt + dt.timedelta(days=8))

        request = self.factory.get('/')
        request.user = user
        request.session = dict()
        middleware = SubscriptionMiddleware()

        # Run
        middleware.process_request(request)

        # Check
        self.assertTrue(request.subscription_type == 'individual')