def test_cannot_return_organisations_with_non_ongoing_subscriptions(self): # Setup now_dt = dt.datetime.now() collection = CollectionFactory.create() journal = JournalFactory(collection=collection) org_1 = OrganisationFactory.create() org_2 = OrganisationFactory.create() org_3 = OrganisationFactory.create() OrganisationFactory.create() subscription_1 = JournalAccessSubscriptionFactory.create( organisation=org_1, journal=journal) JournalAccessSubscriptionPeriodFactory.create( subscription=subscription_1, start=now_dt - dt.timedelta(days=10), end=now_dt + dt.timedelta(days=8)) subscription_2 = JournalAccessSubscriptionFactory.create( organisation=org_2, collection=journal.collection) JournalAccessSubscriptionPeriodFactory.create( subscription=subscription_2, start=now_dt - dt.timedelta(days=10), end=now_dt - dt.timedelta(days=5)) subscription_3 = JournalAccessSubscriptionFactory.create(organisation=org_3) subscription_3.journals.add(journal) JournalAccessSubscriptionPeriodFactory.create( subscription=subscription_3, start=now_dt - dt.timedelta(days=10), end=now_dt + dt.timedelta(days=8)) # Run & check assert set(get_journal_organisation_subscribers(journal)) == set([org_1, org_3, ])
def test_provides_only_subscriptions_associated_with_the_current_journal(self): # Setup AuthorizationFactory.create( content_type=ContentType.objects.get_for_model(self.journal), object_id=self.journal.id, user=self.user, authorization_codename=AC.can_manage_individual_subscription.codename, ) plan = JournalManagementPlanFactory.create(max_accounts=10) JournalManagementSubscriptionFactory.create(journal=self.journal, plan=plan) other_journal = JournalFactory.create(collection=self.collection) subscription_1 = JournalAccessSubscriptionFactory.create(user=self.user, journal=self.journal) JournalAccessSubscriptionFactory.create(user=self.user, journal=other_journal) self.client.login(username="******", password="******") url = reverse("userspace:journal:subscription:list", kwargs={"journal_pk": self.journal.pk}) # Run response = self.client.get(url) # Check self.assertEqual(response.status_code, 200) self.assertEqual(list(response.context["subscriptions"]), [subscription_1])
def test_cannot_return_organisations_with_non_ongoing_subscriptions(self): # Setup now_dt = dt.datetime.now() collection = CollectionFactory.create() journal = JournalFactory(collection=collection) org_1 = OrganisationFactory.create() org_2 = OrganisationFactory.create() org_3 = OrganisationFactory.create() OrganisationFactory.create() subscription_1 = JournalAccessSubscriptionFactory.create( organisation=org_1, journal=journal) JournalAccessSubscriptionPeriodFactory.create( subscription=subscription_1, start=now_dt - dt.timedelta(days=10), end=now_dt + dt.timedelta(days=8)) subscription_2 = JournalAccessSubscriptionFactory.create( organisation=org_2, collection=journal.collection) JournalAccessSubscriptionPeriodFactory.create( subscription=subscription_2, start=now_dt - dt.timedelta(days=10), end=now_dt - dt.timedelta(days=5)) subscription_3 = JournalAccessSubscriptionFactory.create( organisation=org_3) subscription_3.journals.add(journal) JournalAccessSubscriptionPeriodFactory.create( subscription=subscription_3, start=now_dt - dt.timedelta(days=10), end=now_dt + dt.timedelta(days=8)) # Run & check assert set(get_journal_organisation_subscribers(journal)) == set([ org_1, org_3, ])
def test_cannot_grant_access_to_an_article_if_it_is_associated_to_an_individual_subscription_that_is_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) JournalAccessSubscriptionFactory.create(user=self.user, journal=self.journal) class MyView(ArticleAccessCheckMixin): def get_article(self): return article request = self.factory.get("/") request.user = self.user request.subscription = None view = MyView() view.request = request # Run # check self.assertFalse(view.has_access())
def test_can_return_only_organisations_that_are_associated_with_a_valid_subscription( self): # Setup org_1 = OrganisationFactory.create() org_2 = OrganisationFactory.create() org_3 = OrganisationFactory.create() user = UserFactory.create() org_1.members.add(user) org_2.members.add(user) org_3.members.add(user) subscription_1 = JournalAccessSubscriptionFactory.create( organisation=org_1) subscription_2 = JournalAccessSubscriptionFactory.create( organisation=org_2) JournalAccessSubscriptionFactory.create(organisation=org_3) now_dt = dt.datetime.now() JournalAccessSubscriptionPeriodFactory.create( subscription=subscription_1, start=now_dt - dt.timedelta(days=10), end=now_dt + dt.timedelta(days=8)) JournalAccessSubscriptionPeriodFactory.create( subscription=subscription_2, start=now_dt - dt.timedelta(days=10), end=now_dt - dt.timedelta(days=8)) # Run & check self.assertEqual(list(get_managed_organisations(user)), [ org_1, ])
def test_can_return_only_organisations_that_have_the_considered_users_in_their_members(self): # Setup org_1 = OrganisationFactory.create() org_2 = OrganisationFactory.create() org_3 = OrganisationFactory.create() user = UserFactory.create() org_1.members.add(user) subscription_1 = JournalAccessSubscriptionFactory.create(organisation=org_1) subscription_2 = JournalAccessSubscriptionFactory.create(organisation=org_2) subscription_3 = JournalAccessSubscriptionFactory.create(organisation=org_3) now_dt = dt.datetime.now() JournalAccessSubscriptionPeriodFactory.create( subscription=subscription_1, start=now_dt - dt.timedelta(days=10), end=now_dt + dt.timedelta(days=8)) JournalAccessSubscriptionPeriodFactory.create( subscription=subscription_2, start=now_dt - dt.timedelta(days=10), end=now_dt + dt.timedelta(days=8)) JournalAccessSubscriptionPeriodFactory.create( subscription=subscription_3, start=now_dt - dt.timedelta(days=10), end=now_dt + dt.timedelta(days=8)) # Run & check self.assertEqual(list(get_managed_organisations(user)), [org_1, ])
def test_cannot_allow_the_creation_of_subscription_if_the_plan_limit_has_been_reached( self): # Setup plan = JournalManagementPlanFactory.create(code='test', max_accounts=3) JournalManagementSubscriptionFactory.create(journal=self.journal, plan=plan) JournalAccessSubscriptionFactory.create(user=self.user, journal=self.journal) token_1 = AccountActionTokenFactory.create(content_object=self.journal) token_2 = AccountActionTokenFactory.create( content_object=self.journal) # noqa token_1.consume(self.user) AuthorizationFactory.create( content_type=ContentType.objects.get_for_model(self.journal), object_id=self.journal.id, user=self.user, authorization_codename=AC.can_manage_individual_subscription. codename) self.client.login(username='******', password='******') url = reverse('userspace:journal:subscription:create', kwargs={'journal_pk': self.journal.pk}) # Run response = self.client.get(url) # Check self.assertEqual(response.status_code, 302)
def test_can_return_only_organisations_that_have_the_considered_users_in_their_members( self): # Setup org_1 = OrganisationFactory.create() org_2 = OrganisationFactory.create() org_3 = OrganisationFactory.create() user = UserFactory.create() org_1.members.add(user) subscription_1 = JournalAccessSubscriptionFactory.create( organisation=org_1) subscription_2 = JournalAccessSubscriptionFactory.create( organisation=org_2) subscription_3 = JournalAccessSubscriptionFactory.create( organisation=org_3) now_dt = dt.datetime.now() JournalAccessSubscriptionPeriodFactory.create( subscription=subscription_1, start=now_dt - dt.timedelta(days=10), end=now_dt + dt.timedelta(days=8)) JournalAccessSubscriptionPeriodFactory.create( subscription=subscription_2, start=now_dt - dt.timedelta(days=10), end=now_dt + dt.timedelta(days=8)) JournalAccessSubscriptionPeriodFactory.create( subscription=subscription_3, start=now_dt - dt.timedelta(days=10), end=now_dt + dt.timedelta(days=8)) # Run & check self.assertEqual(list(get_managed_organisations(user)), [ org_1, ])
def test_provides_only_subscriptions_associated_with_the_current_journal( self): # Setup AuthorizationFactory.create( content_type=ContentType.objects.get_for_model(self.journal), object_id=self.journal.id, user=self.user, authorization_codename=AC.can_manage_individual_subscription. codename) plan = JournalManagementPlanFactory.create(max_accounts=10) JournalManagementSubscriptionFactory.create(journal=self.journal, plan=plan) other_journal = JournalFactory.create(collection=self.collection) subscription_1 = JournalAccessSubscriptionFactory.create( user=self.user, journal=self.journal) JournalAccessSubscriptionFactory.create(user=self.user, journal=other_journal) self.client.login(username='******', password='******') url = reverse('userspace:journal:subscription:list', kwargs={ 'journal_pk': self.journal.pk, }) # Run response = self.client.get(url) # Check self.assertEqual(response.status_code, 200) self.assertEqual(list(response.context['subscriptions']), [ subscription_1, ])
def test_cannot_validate_if_the_email_is_already_used_by_another_subscription(self): # Setup JournalAccessSubscriptionFactory.create(user=self.user, journal=self.journal) form_data = {"email": self.user.email, "first_name": faker.first_name(), "last_name": faker.last_name()} form = JournalAccessSubscriptionCreateForm(form_data, journal=self.journal) # Run & check self.assertFalse(form.is_valid()) self.assertTrue("email" in form.errors)
def setUp(self): super(TestOrganisationScopeMixin, self).setUp() self.factory = RequestFactory() self.organisation = OrganisationFactory.create() self.organisation.members.add(self.user) self.subscription = JournalAccessSubscriptionFactory.create(organisation=self.organisation) 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))
def test_knows_its_underlying_journals(self): # Setup subscription_1 = JournalAccessSubscriptionFactory.create(full_access=True) subscription_2 = JournalAccessSubscriptionFactory.create(journal=self.journal) subscription_3 = JournalAccessSubscriptionFactory.create(collection=self.collection) subscription_4 = JournalAccessSubscriptionFactory.create() subscription_4.journals.add(self.journal) # Run & check assert list(subscription_1.get_journals()) == list(Journal.objects.all()) assert list(subscription_2.get_journals()) == [self.journal, ] assert list(subscription_3.get_journals()) == [self.journal, ] assert list(subscription_4.get_journals()) == [self.journal, ]
def test_can_return_only_the_valid_subscriptions(self): # Setup now_dt = dt.datetime.now() subscription_1 = JournalAccessSubscriptionFactory.create() subscription_2 = JournalAccessSubscriptionFactory.create() JournalAccessSubscriptionPeriodFactory.create( subscription=subscription_1, start=now_dt - dt.timedelta(days=10), end=now_dt + dt.timedelta(days=8)) JournalAccessSubscriptionPeriodFactory.create( subscription=subscription_2, start=now_dt + dt.timedelta(days=10), end=now_dt + dt.timedelta(days=8)) # Run & check assert list(JournalAccessSubscription.valid_objects.all()) == [subscription_1, ]
def test_cannot_validate_if_the_email_is_already_used_by_another_subscription( self): # Setup JournalAccessSubscriptionFactory.create(user=self.user, journal=self.journal) form_data = { 'email': self.user.email, 'first_name': faker.first_name(), 'last_name': faker.last_name(), } form = JournalAccessSubscriptionCreateForm(form_data, journal=self.journal) # Run & check self.assertFalse(form.is_valid()) self.assertTrue('email' in form.errors)
def test_cannot_handle_a_request_with_an_invalid_report_type(self): # Setup self.organisation.members.add(self.user) self.organisation.members.add(self.user) subscription = JournalAccessSubscriptionFactory.create(organisation=self.organisation) now_dt = dt.datetime.now() JournalAccessSubscriptionPeriodFactory.create( subscription=subscription, start=now_dt - dt.timedelta(days=10), end=now_dt + dt.timedelta(days=8) ) data = self.get_report_request_body( start="2016-01-01", end="2016-02-01", requestor_id=self.organisation.id, customer_reference=self.organisation.id, report_type="bad", ) request = self.factory.post( "/", data=data, content_type="text/xml", **{"HTTP_SOAPACTION": "SushiService:GetReportIn"} ) # Run response = SushiWebServiceView.as_view()(request) # Check dom = et.fromstring(response.content) faultcode = dom.find(".//faultcode") assert faultcode.text == "SOAP-ENV:Server" faultstring = dom.find(".//faultstring") assert faultstring.text == "bad reports are not provided"
def test_knows_if_it_is_ongoing_or_not(self): # Setup now_dt = dt.datetime.now() subscription_1 = JournalAccessSubscriptionFactory.create() subscription_2 = JournalAccessSubscriptionFactory.create() JournalAccessSubscriptionPeriodFactory.create( subscription=subscription_1, start=now_dt - dt.timedelta(days=10), end=now_dt + dt.timedelta(days=8)) JournalAccessSubscriptionPeriodFactory.create( subscription=subscription_2, start=now_dt + dt.timedelta(days=10), end=now_dt + dt.timedelta(days=8)) # Run & check assert subscription_1.is_ongoing assert not subscription_2.is_ongoing
def test_can_properly_delete_a_subscription(self): # Setup AuthorizationFactory.create( content_type=ContentType.objects.get_for_model(self.journal), object_id=self.journal.id, user=self.user, authorization_codename=AC.can_manage_individual_subscription. codename) plan = JournalManagementPlanFactory.create(max_accounts=10) JournalManagementSubscriptionFactory.create(journal=self.journal, plan=plan) subscription = JournalAccessSubscriptionFactory.create( user=self.user, journal=self.journal) self.client.login(username='******', password='******') url = reverse('userspace:journal:subscription:delete', kwargs={ 'journal_pk': self.journal.pk, 'pk': subscription.pk, }) # Run response = self.client.post(url, follow=False) # Check self.assertEqual(response.status_code, 302) self.assertFalse(JournalAccessSubscription.objects.exists())
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_can_grant_access_to_an_article_if_it_is_associated_to_an_individual_subscription(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) now_dt = dt.datetime.now() subscription = JournalAccessSubscriptionFactory.create(user=self.user, journal=self.journal) JournalAccessSubscriptionPeriodFactory.create( subscription=subscription, start=now_dt - dt.timedelta(days=10), end=now_dt + dt.timedelta(days=8)) class MyView(ArticleAccessCheckMixin): def get_article(self): return article request = self.factory.get('/') request.user = self.user request.session = dict() view = MyView() view.request = request middleware.process_request(request) # Run # check self.assertTrue(view.has_access())
def test_can_grant_access_to_an_article_if_it_is_associated_to_an_individual_subscription(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) now_dt = dt.datetime.now() subscription = JournalAccessSubscriptionFactory.create(user=self.user, journal=self.journal) JournalAccessSubscriptionPeriodFactory.create( subscription=subscription, start=now_dt - dt.timedelta(days=10), end=now_dt + dt.timedelta(days=8) ) class MyView(ArticleAccessCheckMixin): def get_article(self): return article request = self.factory.get("/") request.user = self.user view = MyView() view.request = request middleware.process_request(request) # Run # check self.assertTrue(view.has_access())
def test_can_properly_delete_a_subscription(self): # Setup AuthorizationFactory.create( content_type=ContentType.objects.get_for_model(self.journal), object_id=self.journal.id, user=self.user, authorization_codename=AC.can_manage_individual_subscription.codename, ) plan = JournalManagementPlanFactory.create(max_accounts=10) JournalManagementSubscriptionFactory.create(journal=self.journal, plan=plan) subscription = JournalAccessSubscriptionFactory.create(user=self.user, journal=self.journal) self.client.login(username="******", password="******") url = reverse( "userspace:journal:subscription:delete", kwargs={"journal_pk": self.journal.pk, "pk": subscription.pk} ) # Run response = self.client.post(url, follow=False) # Check self.assertEqual(response.status_code, 302) self.assertFalse(JournalAccessSubscription.objects.exists())
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_cannot_handle_a_request_with_an_invalid_report_type(self): # Setup self.organisation.members.add(self.user) self.organisation.members.add(self.user) subscription = JournalAccessSubscriptionFactory.create( organisation=self.organisation) now_dt = dt.datetime.now() JournalAccessSubscriptionPeriodFactory.create( subscription=subscription, start=now_dt - dt.timedelta(days=10), end=now_dt + dt.timedelta(days=8)) data = self.get_report_request_body( start='2016-01-01', end='2016-02-01', requestor_id=self.organisation.id, customer_reference=self.organisation.id, report_type='bad') request = self.factory.post( '/', data=data, content_type='text/xml', **{'HTTP_SOAPACTION': 'SushiService:GetReportIn'}) # Run response = SushiWebServiceView.as_view()(request) # Check dom = et.fromstring(response.content) faultcode = dom.find('.//faultcode') assert faultcode.text == 'SOAP-ENV:Server' faultstring = dom.find('.//faultstring') assert faultstring.text == 'bad reports are not provided'
def setUp(self): super(TestInstitutionIPAddressRangeCreateView, 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))
def test_cannot_clean_an_incoherent_period(self): # Setup now_dt = dt.datetime.now() subscription = JournalAccessSubscriptionFactory.create() period = JournalAccessSubscriptionPeriodFactory.build( subscription=subscription, start=now_dt + dt.timedelta(days=10), end=now_dt + dt.timedelta(days=8)) # Run & check with pytest.raises(ValidationError): period.clean()
def setUp(self): super(TestInstitutionIPAddressRangeCreateView, 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))
def test_knows_its_underlying_journals(self): # Setup subscription_1 = JournalAccessSubscriptionFactory.create( full_access=True) subscription_2 = JournalAccessSubscriptionFactory.create( journal=self.journal) subscription_3 = JournalAccessSubscriptionFactory.create( collection=self.collection) subscription_4 = JournalAccessSubscriptionFactory.create() subscription_4.journals.add(self.journal) # Run & check assert list(subscription_1.get_journals()) == list( Journal.objects.all()) assert list(subscription_2.get_journals()) == [ self.journal, ] assert list(subscription_3.get_journals()) == [ self.journal, ] assert list(subscription_4.get_journals()) == [ self.journal, ]
def test_can_return_only_organisations_that_are_associated_with_a_valid_subscription(self): # Setup org_1 = OrganisationFactory.create() org_2 = OrganisationFactory.create() org_3 = OrganisationFactory.create() user = UserFactory.create() org_1.members.add(user) org_2.members.add(user) org_3.members.add(user) subscription_1 = JournalAccessSubscriptionFactory.create(organisation=org_1) subscription_2 = JournalAccessSubscriptionFactory.create(organisation=org_2) JournalAccessSubscriptionFactory.create(organisation=org_3) now_dt = dt.datetime.now() JournalAccessSubscriptionPeriodFactory.create( subscription=subscription_1, start=now_dt - dt.timedelta(days=10), end=now_dt + dt.timedelta(days=8)) JournalAccessSubscriptionPeriodFactory.create( subscription=subscription_2, start=now_dt - dt.timedelta(days=10), end=now_dt - dt.timedelta(days=8)) # Run & check self.assertEqual(list(get_managed_organisations(user)), [org_1, ])
def test_cannot_be_accessed_by_a_user_who_cannot_manage_individual_subscriptions(self): # Setup subscription = JournalAccessSubscriptionFactory.create(user=self.user, journal=self.journal) self.client.login(username="******", password="******") url = reverse( "userspace:journal:subscription:delete", kwargs={"journal_pk": self.journal.pk, "pk": subscription.pk} ) # Run response = self.client.get(url) # Check self.assertEqual(response.status_code, 403)
def test_cannot_clean_a_period_that_has_a_younger_concurrent_period(self): # Setup now_dt = dt.datetime.now() subscription = JournalAccessSubscriptionFactory.create() JournalAccessSubscriptionPeriodFactory.create( subscription=subscription, start=now_dt + dt.timedelta(days=11), end=now_dt + dt.timedelta(days=15)) period = JournalAccessSubscriptionPeriodFactory.build( subscription=subscription, start=now_dt + dt.timedelta(days=10), end=now_dt + dt.timedelta(days=12)) # Run & check with pytest.raises(ValidationError): period.clean()
def test_embeds_a_boolean_indicating_if_the_user_is_subscribed_to_the_current_journal(self): # Setup now_dt = dt.datetime.now() subscription = JournalAccessSubscriptionFactory.create(user=self.user, journal=self.journal) JournalAccessSubscriptionPeriodFactory.create( subscription=subscription, start=now_dt - dt.timedelta(days=10), end=now_dt + dt.timedelta(days=8)) self.client.login(username='******', password='******') url = reverse('public:journal:journal_detail', kwargs={'code': self.journal.code}) # Run response = self.client.get(url) # Check self.assertEqual(response.status_code, 200) self.assertTrue(response.context['user_has_access_to_journal'])
def test_cannot_allow_the_creation_of_subscription_if_the_plan_limit_has_been_reached(self): # Setup plan = JournalManagementPlanFactory.create(code="test", max_accounts=3) JournalManagementSubscriptionFactory.create(journal=self.journal, plan=plan) JournalAccessSubscriptionFactory.create(user=self.user, journal=self.journal) token_1 = AccountActionTokenFactory.create(content_object=self.journal) token_2 = AccountActionTokenFactory.create(content_object=self.journal) # noqa token_1.consume(self.user) AuthorizationFactory.create( content_type=ContentType.objects.get_for_model(self.journal), object_id=self.journal.id, user=self.user, authorization_codename=AC.can_manage_individual_subscription.codename, ) self.client.login(username="******", password="******") url = reverse("userspace:journal:subscription:create", kwargs={"journal_pk": self.journal.pk}) # Run response = self.client.get(url) # Check self.assertEqual(response.status_code, 302)
def test_cannot_grant_access_to_an_article_if_it_is_associated_to_an_individual_subscription_that_is_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) JournalAccessSubscriptionFactory.create(user=self.user, journal=self.journal) class MyView(ArticleAccessCheckMixin): def get_article(self): return article request = self.factory.get('/') request.user = self.user request.session = dict() request.subscription = None view = MyView() view.request = request # Run # check self.assertFalse(view.has_access())
def test_cannot_be_accessed_by_a_user_who_cannot_manage_individual_subscriptions( self): # Setup subscription = JournalAccessSubscriptionFactory.create( user=self.user, journal=self.journal) self.client.login(username='******', password='******') url = reverse('userspace:journal:subscription:delete', kwargs={ 'journal_pk': self.journal.pk, 'pk': subscription.pk, }) # Run response = self.client.get(url) # Check self.assertEqual(response.status_code, 403)
def test_embeds_a_boolean_indicating_if_the_user_is_subscribed_to_the_current_journal( self): # Setup now_dt = dt.datetime.now() subscription = JournalAccessSubscriptionFactory.create( user=self.user, journal=self.journal) JournalAccessSubscriptionPeriodFactory.create( subscription=subscription, start=now_dt - dt.timedelta(days=10), end=now_dt + dt.timedelta(days=8)) self.client.login(username='******', password='******') url = reverse('public:journal:journal_detail', kwargs={'code': self.journal.code}) # Run response = self.client.get(url) # Check self.assertEqual(response.status_code, 200) self.assertTrue(response.context['user_has_access_to_journal'])
def test_knows_if_a_simple_user_can_manage_organisation_subscription_ips(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) self.organisation.members.add(self.user) subscription = JournalAccessSubscriptionFactory.create( journal=self.journal, organisation=self.organisation) now_dt = dt.datetime.now() JournalAccessSubscriptionPeriodFactory.create( subscription=subscription, start=now_dt - dt.timedelta(days=8), end=now_dt + dt.timedelta(days=11)) # Run & check self.assertTrue( self.user.has_perm( 'subscription.manage_organisation_subscription_ips', self.organisation))
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')
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 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')
def test_knows_if_a_simple_user_can_manage_organisation_subscription_ips( 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) self.organisation.members.add(self.user) subscription = JournalAccessSubscriptionFactory.create( journal=self.journal, organisation=self.organisation) now_dt = dt.datetime.now() JournalAccessSubscriptionPeriodFactory.create( subscription=subscription, start=now_dt - dt.timedelta(days=8), end=now_dt + dt.timedelta(days=11)) # Run & check self.assertTrue( self.user.has_perm( 'subscription.manage_organisation_subscription_ips', self.organisation))
def setup(self): self.client = Client() self.user = UserFactory.create(username='******') self.user.set_password('notsecret') self.user.save() 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)) # Set up some images used for doing image tests images_dict = {} # Fetch an image aimed to be resized f = open(settings.MEDIA_ROOT + "/200x200.png", "rb") images_dict['200x200'] = File(f) self.images_dict = images_dict yield # teardown # -- for img in self.images_dict.values(): img.close() try: self.organisation.badge.delete() except: pass self.organisation.delete()
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())
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): self.organisation = OrganisationFactory.create() self.subscription = JournalAccessSubscriptionFactory( organisation=self.organisation)