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)
def test_provides_all_the_ip_ranges_for_the_current_organisation(self): # Setup AuthorizationFactory.create( content_type=ContentType.objects.get_for_model(self.organisation), object_id=self.organisation.id, user=self.user, authorization_codename=AC.can_manage_organisation_subscription_ips. codename) ip_range_1 = InstitutionIPAddressRangeFactory.create( subscription=self.subscription, ip_start='10.0.0.0', ip_end='11.0.0.0') ip_range_2 = InstitutionIPAddressRangeFactory.create( subscription=self.subscription, ip_start='20.0.0.0', ip_end='21.0.0.0') self.client.login(username='******', password='******') url = reverse('userspace:library:subscription_ips:list', kwargs={ 'organisation_pk': self.organisation.pk, }) # Run response = self.client.get(url) # Check self.assertEqual(response.status_code, 200) self.assertEqual(list(response.context['subscription_ip_ranges']), [ ip_range_1, ip_range_2, ])
def test_cannot_grant_access_to_an_article_if_it_is_associated_to_an_institutional_account_that_is_not_not_ongoing( self ): # noqa # Setup self.journal.open_access = False self.journal.save() issue = IssueFactory.create( journal=self.journal, year=dt.datetime.now().year, date_published=dt.datetime.now(), localidentifier="test" ) article = ArticleFactory.create(issue=issue) organisation = OrganisationFactory.create() subscription = JournalAccessSubscriptionFactory.create(journal=self.journal, organisation=organisation) InstitutionIPAddressRangeFactory.create(subscription=subscription, ip_start="192.168.1.2", ip_end="192.168.1.4") class MyView(ArticleAccessCheckMixin): def get_article(self): return article request = self.factory.get("/") request.user = AnonymousUser() # FIXME call middleware request.subscription = None parameters = request.META.copy() parameters["HTTP_X_FORWARDED_FOR"] = "192.168.1.3" request.META = parameters view = MyView() view.request = request # Run # check self.assertFalse(view.has_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('/') 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)
def test_can_return_subscriptions_for_an_ip(self): subscription_period = ValidJournalAccessSubscriptionPeriodFactory.create() InstitutionIPAddressRangeFactory.create( subscription=subscription_period.subscription, ip_start='192.168.1.1', ip_end='192.168.1.2', ) assert list(JournalAccessSubscription.valid_objects.get_for_ip_address('192.168.1.1')) == [subscription_period.subscription]
def test_can_grant_access_to_an_article_if_it_is_associated_to_an_institutional_account(self): # Setup self.journal.open_access = False self.journal.save() issue = IssueFactory.create( journal=self.journal, year=dt.datetime.now().year, date_published=dt.datetime.now(), localidentifier='test') article = ArticleFactory.create(issue=issue) organisation = OrganisationFactory.create() now_dt = dt.datetime.now() subscription = JournalAccessSubscriptionFactory.create( journal=self.journal, 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') class MyView(ArticleAccessCheckMixin): def get_article(self): return article 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.process_request(request) view = MyView() view.request = request # Run # check self.assertTrue(view.has_access())
def setUp(self): super(TestInstitutionIPAddressRangeDeleteView, self).setUp() self.organisation = OrganisationFactory.create() self.organisation.members.add(self.user) self.subscription = JournalAccessSubscriptionFactory.create(organisation=self.organisation) now_dt = dt.datetime.now() JournalAccessSubscriptionPeriodFactory.create( subscription=self.subscription, start=now_dt - dt.timedelta(days=10), end=now_dt + dt.timedelta(days=8)) self.ip_range = InstitutionIPAddressRangeFactory.create( subscription=self.subscription, ip_start='10.0.0.0', ip_end='11.0.0.0')
def test_provides_all_the_ip_ranges_for_the_current_organisation(self): # Setup AuthorizationFactory.create( content_type=ContentType.objects.get_for_model(self.organisation), object_id=self.organisation.id, user=self.user, authorization_codename=AC.can_manage_organisation_subscription_ips.codename) ip_range_1 = InstitutionIPAddressRangeFactory.create( subscription=self.subscription, ip_start='10.0.0.0', ip_end='11.0.0.0') ip_range_2 = InstitutionIPAddressRangeFactory.create( subscription=self.subscription, ip_start='20.0.0.0', ip_end='21.0.0.0') self.client.login(username='******', password='******') url = reverse('userspace:library:subscription_ips:list', kwargs={ 'organisation_pk': self.organisation.pk, }) # Run response = self.client.get(url) # Check self.assertEqual(response.status_code, 200) self.assertEqual( list(response.context['subscription_ip_ranges']), [ip_range_1, ip_range_2, ])
def test_cannot_grant_access_to_an_article_if_it_is_associated_to_an_institutional_account_that_is_not_not_ongoing(self): # noqa # Setup self.journal.open_access = False self.journal.save() issue = IssueFactory.create( journal=self.journal, year=dt.datetime.now().year, date_published=dt.datetime.now(), localidentifier='test') article = ArticleFactory.create(issue=issue) organisation = OrganisationFactory.create() subscription = JournalAccessSubscriptionFactory.create( journal=self.journal, organisation=organisation) InstitutionIPAddressRangeFactory.create( subscription=subscription, ip_start='192.168.1.2', ip_end='192.168.1.4') class MyView(ArticleAccessCheckMixin): def get_article(self): return article request = self.factory.get('/') request.user = AnonymousUser() request.session = dict() # FIXME call middleware request.subscription = None parameters = request.META.copy() parameters['HTTP_X_FORWARDED_FOR'] = '192.168.1.3' request.META = parameters view = MyView() view.request = request # Run # check self.assertFalse(view.has_access())
def setUp(self): super(TestInstitutionIPAddressRangeDeleteView, self).setUp() self.organisation = OrganisationFactory.create() self.organisation.members.add(self.user) self.subscription = JournalAccessSubscriptionFactory.create( organisation=self.organisation) now_dt = dt.datetime.now() JournalAccessSubscriptionPeriodFactory.create( subscription=self.subscription, start=now_dt - dt.timedelta(days=10), end=now_dt + dt.timedelta(days=8)) self.ip_range = InstitutionIPAddressRangeFactory.create( subscription=self.subscription, ip_start='10.0.0.0', ip_end='11.0.0.0')
def test_can_grant_access_to_an_article_if_it_is_associated_to_an_institutional_account(self): # Setup self.journal.open_access = False self.journal.save() issue = IssueFactory.create( journal=self.journal, year=dt.datetime.now().year, date_published=dt.datetime.now(), localidentifier="test" ) article = ArticleFactory.create(issue=issue) organisation = OrganisationFactory.create() now_dt = dt.datetime.now() subscription = JournalAccessSubscriptionFactory.create(journal=self.journal, 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") class MyView(ArticleAccessCheckMixin): def get_article(self): return article request = self.factory.get("/") request.user = AnonymousUser() parameters = request.META.copy() parameters["HTTP_X_FORWARDED_FOR"] = "192.168.1.3" request.META = parameters middleware.process_request(request) view = MyView() view.request = request # Run # check self.assertTrue(view.has_access())