def handle(self, *args, **options): num_partners = options["num"][0] tag_list = [ "science_tag", "humanities_tag", "social-sciences_tag", "history_tag", "law_tag", "video_tag", "multidisciplinary_tag", ] coordinators = User.objects.filter(groups__name="coordinators") for _ in range(num_partners): partner = PartnerFactory( company_location=random.choice(list(countries)), renewals_available=random.choice([True, False]), send_instructions=Faker( random.choice(settings.FAKER_LOCALES) ).paragraph(nb_sentences=2), coordinator=random.choice(coordinators), real_name=self.chance(True, False, 40), country_of_residence=self.chance(True, False, 20), specific_title=self.chance(True, False, 10), specific_stream=self.chance(True, False, 10), occupation=self.chance(True, False, 10), affiliation=self.chance(True, False, 10), agreement_with_terms_of_use=self.chance(True, False, 10), mutually_exclusive=False, ) # ManyToMany relationships can't be set until the partner object has # been created. random_languages = random.sample( list(Language.objects.all()), random.randint(1, 2) ) for lang in random_languages: partner.languages.add(lang) new_tags = {} partner_tags = [] for tag in random.sample(tag_list, random.randint(1, 4)): partner_tags.append(tag) new_tags["tags"] = partner_tags partner.new_tags = new_tags partner.save() all_partners = Partner.even_not_available.all() # Set 5 partners to need a registration URL. We do this separately # because it requires both the account_email and registration_url # fields to be set concurrently. for registration_partner in random.sample(list(all_partners), 5): registration_partner.account_email = True registration_partner.registration_url = Faker( random.choice(settings.FAKER_LOCALES) ).uri() registration_partner.save() # While most fields can be set at random, we want to make sure we # get partners with certain fields set to particular values. # Set 5 random partners to be unavailable for unavailable_partner in random.sample(list(all_partners), 5): unavailable_partner.status = Partner.NOT_AVAILABLE unavailable_partner.save() # Set 5% random partners to have excerpt limit in words for words in random.sample(list(all_partners), 10): words.excerpt_limit = random.randint(100, 250) words.save() # Set 5% random partners to have excerpt limit in words for percentage in random.sample(list(all_partners), 10): percentage.excerpt_limit_percentage = random.randint(5, 50) percentage.save() # Set 1 random partner to have excerpt limits both in words and percentage for percentage_words in random.sample(list(all_partners), 1): percentage_words.excerpt_limit_percentage = random.randint(5, 50) percentage_words.excerpt_limit = random.randint(100, 250) percentage_words.save() available_partners = all_partners.exclude(status=Partner.NOT_AVAILABLE) # Set 10 random available partners to be waitlisted for waitlisted_partner in random.sample(list(available_partners), 10): waitlisted_partner.status = Partner.WAITLIST waitlisted_partner.save() # Set 25 random partners to have a long description for long_description in random.sample(list(all_partners), 25): long_description.description = Faker( random.choice(settings.FAKER_LOCALES) ).paragraph(nb_sentences=10) long_description.save() # Set 10 random available partners to be featured for featured_partner in random.sample(list(available_partners), 10): featured_partner.featured = True featured_partner.save() # Give any specific_stream flagged partners streams. stream_partners = all_partners.filter(specific_stream=True) # Random number of accounts available for all partners without streams for accounts in all_partners: if not accounts.specific_stream: accounts.accounts_available = random.randint(10, 550) accounts.save() # If we happened to not create any partners with streams, # create one deliberately. if stream_partners.count() == 0: stream_partners = random.sample(list(all_partners), 1) stream_partners[0].specific_stream = True stream_partners[0].save() for partner in stream_partners: for _ in range(3): stream = StreamFactory( partner=partner, name=Faker(random.choice(settings.FAKER_LOCALES)).sentence( nb_words=3 )[ :-1 ], # [:-1] removes full stop description=Faker(random.choice(settings.FAKER_LOCALES)).paragraph( nb_sentences=2 ), ) # Set 15 partners to have somewhere between 1 and 5 video tutorial URLs for partner in random.sample(list(all_partners), 15): for _ in range(random.randint(1, 5)): VideoFactory( partner=partner, tutorial_video_url=Faker( random.choice(settings.FAKER_LOCALES) ).url(), ) # Random number of accounts available for all streams all_streams = Stream.objects.all() for each_stream in all_streams: each_stream.accounts_available = random.randint(10, 100) each_stream.save() # Generate a few number of suggestions with upvotes all_users = User.objects.exclude(is_superuser=True) author_user = random.choice(all_users) for _ in range(random.randint(3, 10)): suggestion = SuggestionFactory( description=Faker(random.choice(settings.FAKER_LOCALES)).paragraph( nb_sentences=10 ), author=author_user, ) # Truncate company name to 40 characters so it doesn't error out suggestion.suggested_company_name = ( suggestion.suggested_company_name[:40] if len(suggestion.suggested_company_name) > 40 else suggestion.suggested_company_name ) suggestion.save() suggestion.upvoted_users.add(author_user) random_users = random.sample(list(all_users), random.randint(1, 10)) suggestion.upvoted_users.add(*random_users) # Set 5 partners use the access code authorization method, # and generate a bunch of codes for each. for partner in random.sample(list(available_partners), 5): partner.authorization_method = Partner.CODES partner.save() for i in range(25): new_access_code = AccessCode() new_access_code.code = "".join( random.choice(string.ascii_uppercase + string.digits) for _ in range(10) ) new_access_code.partner = partner new_access_code.save() # Set 5 partners use the access code authorization method, # and generate a bunch of codes for each. for partner in random.sample(list(available_partners), 5): partner.authorization_method = Partner.CODES partner.save() for i in range(25): new_access_code = AccessCode() new_access_code.code = "".join( random.choice(string.ascii_uppercase + string.digits) for _ in range(10) ) new_access_code.partner = partner new_access_code.save()
def setUp(self): super(AuthorizationBaseTestCase, self).setUp() self.partner1 = PartnerFactory(authorization_method=Partner.EMAIL, status=Partner.AVAILABLE) self.partner2 = PartnerFactory( authorization_method=Partner.PROXY, status=Partner.AVAILABLE, requested_access_duration=True, ) self.partner3 = PartnerFactory(authorization_method=Partner.CODES, status=Partner.AVAILABLE) self.partner4 = PartnerFactory(authorization_method=Partner.EMAIL, status=Partner.AVAILABLE) self.partner5 = PartnerFactory( authorization_method=Partner.EMAIL, status=Partner.AVAILABLE, specific_stream=True, ) self.partner5_stream1 = StreamFactory( partner=self.partner5, authorization_method=Partner.EMAIL) self.partner5_stream2 = StreamFactory( partner=self.partner5, authorization_method=Partner.EMAIL) self.editor1 = EditorFactory() self.editor1.user.email = Faker(random.choice( settings.FAKER_LOCALES)).email() self.editor1.user.save() self.editor2 = EditorFactory() self.editor3 = EditorFactory() # Editor 4 is a coordinator with a session. self.editor4 = EditorCraftRoom(self, Terms=True, Coordinator=True) # Editor 4 is the designated coordinator for all partners. self.partner1.coordinator = self.editor4.user self.partner1.account_length = timedelta(days=180) self.partner1.target_url = "http://test.localdomain" self.partner1.save() self.partner2.coordinator = self.editor4.user self.partner2.save() self.partner3.coordinator = self.editor4.user self.partner3.save() self.partner4.coordinator = self.editor4.user self.partner4.save() self.partner5.coordinator = self.editor4.user self.partner5.save() # Editor 5 is a coordinator without a session and with no designated partners. self.editor5 = EditorFactory() coordinators.user_set.add(self.editor5.user) # Create applications. self.app1 = ApplicationFactory(editor=self.editor1, partner=self.partner1, status=Application.PENDING) self.app2 = ApplicationFactory(editor=self.editor2, partner=self.partner1, status=Application.PENDING) self.app3 = ApplicationFactory(editor=self.editor3, partner=self.partner1, status=Application.PENDING) self.app4 = ApplicationFactory(editor=self.editor1, partner=self.partner2, status=Application.PENDING) self.app5 = ApplicationFactory(editor=self.editor2, partner=self.partner2, status=Application.PENDING) self.app6 = ApplicationFactory(editor=self.editor3, partner=self.partner2, status=Application.PENDING) self.app7 = ApplicationFactory(editor=self.editor1, partner=self.partner3, status=Application.PENDING) self.app8 = ApplicationFactory(editor=self.editor1, partner=self.partner4, status=Application.PENDING) self.app9 = ApplicationFactory(editor=self.editor2, partner=self.partner3, status=Application.PENDING) self.app10 = ApplicationFactory( editor=self.editor1, partner=self.partner5, specific_stream=self.partner5_stream1, status=Application.PENDING, ) self.app11 = ApplicationFactory( editor=self.editor1, partner=self.partner5, specific_stream=self.partner5_stream2, status=Application.PENDING, ) # Editor 4 will update status on applications to partners 1, 2, and 5. # Send the application self.client.post( reverse("applications:evaluate", kwargs={"pk": self.app1.pk}), data={"status": Application.SENT}, follow=True, ) self.app1.refresh_from_db() self.auth_app1 = Authorization.objects.get( authorizer=self.editor4.user, user=self.editor1.user, partners=self.partner1) self.client.post( reverse("applications:evaluate", kwargs={"pk": self.app10.pk}), data={"status": Application.SENT}, follow=True, ) self.app10.refresh_from_db() self.auth_app10 = Authorization.objects.get( authorizer=self.editor4.user, user=self.editor1.user, partners=self.partner5, stream=self.partner5_stream1, ) self.client.post( reverse("applications:evaluate", kwargs={"pk": self.app11.pk}), data={"status": Application.SENT}, follow=True, ) self.app11.refresh_from_db() self.auth_app11 = Authorization.objects.get( authorizer=self.editor4.user, user=self.editor1.user, partners=self.partner5, stream=self.partner5_stream2, ) # Send the application self.client.post( reverse("applications:evaluate", kwargs={"pk": self.app2.pk}), data={"status": Application.SENT}, follow=True, ) self.app2.refresh_from_db() self.auth_app2 = Authorization.objects.get( authorizer=self.editor4.user, user=self.editor2.user, partners=self.partner1) # Send the application self.client.post( reverse("applications:evaluate", kwargs={"pk": self.app3.pk}), data={"status": Application.SENT}, follow=True, ) self.app3.refresh_from_db() self.auth_app3 = Authorization.objects.get( authorizer=self.editor4.user, user=self.editor3.user, partners=self.partner1) # PROXY authorization methods don't set .SENT on the evaluate page; # .APPROVED will automatically update them to .SENT # This app was created with a factory, which doesn't create a revision. # Let's update the status so that we have one. self.client.post( reverse("applications:evaluate", kwargs={"pk": self.app4.pk}), data={"status": Application.QUESTION}, follow=True, ) # Approve the application self.client.post( reverse("applications:evaluate", kwargs={"pk": self.app4.pk}), data={"status": Application.APPROVED}, follow=True, ) self.app4.refresh_from_db() self.auth_app4 = Authorization.objects.get( authorizer=self.editor4.user, user=self.editor1.user, partners=self.partner2) # This app was created with a factory, which doesn't create a revision. # Let's update the status so that we have one. self.client.post( reverse("applications:evaluate", kwargs={"pk": self.app5.pk}), data={"status": Application.QUESTION}, follow=True, ) # Approve the application self.client.post( reverse("applications:evaluate", kwargs={"pk": self.app5.pk}), data={"status": Application.APPROVED}, follow=True, ) self.app5.refresh_from_db() self.auth_app5 = Authorization.objects.get( authorizer=self.editor4.user, user=self.editor2.user, partners=self.partner2) # Set up an access code to distribute self.access_code = AccessCode(code="ABCD-EFGH-IJKL", partner=self.partner3) self.access_code.save() self.message_patcher = patch( "TWLight.applications.views.messages.add_message") self.message_patcher.start()
def setUp(self): super(AuthorizationBaseTestCase, self).setUp() self.partner1 = PartnerFactory(authorization_method=Partner.EMAIL, status=Partner.AVAILABLE) self.partner2 = PartnerFactory(authorization_method=Partner.PROXY, status=Partner.AVAILABLE) self.partner3 = PartnerFactory(authorization_method=Partner.CODES, status=Partner.AVAILABLE) self.partner4 = PartnerFactory(authorization_method=Partner.EMAIL, status=Partner.AVAILABLE) self.editor1 = EditorFactory() self.editor1.user.email = fake.email() self.editor1.user.save() self.editor2 = EditorFactory() self.editor3 = EditorFactory() # Editor 4 is a coordinator with a session. self.editor4 = EditorCraftRoom(self, Terms=True, Coordinator=True) # Editor 4 is the designated coordinator for all partners. self.partner1.coordinator = self.editor4.user self.partner1.account_length = timedelta(days=180) self.partner1.target_url = 'http://test.localdomain' self.partner1.save() self.partner2.coordinator = self.editor4.user self.partner2.save() self.partner3.coordinator = self.editor4.user self.partner3.save() self.partner4.coordinator = self.editor4.user self.partner4.save() # Editor 5 is a coordinator without a session and with no designated partners. self.editor5 = EditorFactory() coordinators.user_set.add(self.editor5.user) # Create applications. self.app1 = ApplicationFactory(editor=self.editor1, partner=self.partner1, status=Application.PENDING) self.app2 = ApplicationFactory(editor=self.editor2, partner=self.partner1, status=Application.PENDING) self.app3 = ApplicationFactory(editor=self.editor3, partner=self.partner1, status=Application.PENDING) self.app4 = ApplicationFactory(editor=self.editor1, partner=self.partner2, status=Application.PENDING) self.app5 = ApplicationFactory(editor=self.editor2, partner=self.partner2, status=Application.PENDING) self.app6 = ApplicationFactory(editor=self.editor3, partner=self.partner2, status=Application.PENDING) self.app7 = ApplicationFactory(editor=self.editor1, partner=self.partner3, status=Application.PENDING) self.app8 = ApplicationFactory(editor=self.editor1, partner=self.partner4, status=Application.PENDING) self.app9 = ApplicationFactory(editor=self.editor2, partner=self.partner3, status=Application.PENDING) # Editor 4 will update status on applications to partners 1 and 2. # Send the application self.client.post( reverse("applications:evaluate", kwargs={"pk": self.app1.pk}), data={"status": Application.SENT}, follow=True, ) self.app1.refresh_from_db() self.auth_app1 = Authorization.objects.get( authorizer=self.editor4.user, authorized_user=self.editor1.user, partner=self.partner1, ) # Approve the application self.client.post( reverse("applications:evaluate", kwargs={"pk": self.app2.pk}), data={"status": Application.APPROVED}, follow=True, ) self.app2.refresh_from_db() self.auth_app2 = Authorization( authorizer=self.editor4.user, authorized_user=self.editor2.user, partner=self.partner1, ) # Send the application self.client.post( reverse("applications:evaluate", kwargs={"pk": self.app3.pk}), data={"status": Application.SENT}, follow=True, ) self.app3.refresh_from_db() self.auth_app3 = Authorization.objects.get( authorizer=self.editor4.user, authorized_user=self.editor3.user, partner=self.partner1, ) # Send the application # PROXY authorization methods don't set .SENT on the evaluate page; # .APPROVED will automatically update them to .SENT self.client.post( reverse("applications:evaluate", kwargs={"pk": self.app4.pk}), data={"status": Application.APPROVED}, follow=True, ) self.app4.refresh_from_db() self.auth_app4 = Authorization.objects.get( # https://phabricator.wikimedia.org/T233508 # authorizer=self.editor4.user, authorized_user=self.editor1.user, partner=self.partner2, ) # Send the application self.client.post( reverse("applications:evaluate", kwargs={"pk": self.app5.pk}), data={"status": Application.APPROVED}, follow=True, ) self.app5.refresh_from_db() self.auth_app5 = Authorization.objects.get( # https://phabricator.wikimedia.org/T233508 # authorizer=self.editor4.user, authorized_user=self.editor2.user, partner=self.partner2, ) # Set up an access code to distribute self.access_code = AccessCode(code="ABCD-EFGH-IJKL", partner=self.partner3) self.access_code.save() self.message_patcher = patch( "TWLight.applications.views.messages.add_message") self.message_patcher.start()
class AuthorizationBaseTestCase(TestCase): """ Setup class for Authorization Object tests. Could possibly achieve the same effect via a new factory class. """ def setUp(self): super(AuthorizationBaseTestCase, self).setUp() self.partner1 = PartnerFactory(authorization_method=Partner.EMAIL, status=Partner.AVAILABLE) self.partner2 = PartnerFactory(authorization_method=Partner.PROXY, status=Partner.AVAILABLE, requested_access_duration=True) self.partner3 = PartnerFactory(authorization_method=Partner.CODES, status=Partner.AVAILABLE) self.partner4 = PartnerFactory(authorization_method=Partner.EMAIL, status=Partner.AVAILABLE) self.editor1 = EditorFactory() self.editor1.user.email = fake.email() self.editor1.user.save() self.editor2 = EditorFactory() self.editor3 = EditorFactory() # Editor 4 is a coordinator with a session. self.editor4 = EditorCraftRoom(self, Terms=True, Coordinator=True) # Editor 4 is the designated coordinator for all partners. self.partner1.coordinator = self.editor4.user self.partner1.account_length = timedelta(days=180) self.partner1.target_url = 'http://test.localdomain' self.partner1.save() self.partner2.coordinator = self.editor4.user self.partner2.save() self.partner3.coordinator = self.editor4.user self.partner3.save() self.partner4.coordinator = self.editor4.user self.partner4.save() # Editor 5 is a coordinator without a session and with no designated partners. self.editor5 = EditorFactory() coordinators.user_set.add(self.editor5.user) # Create applications. self.app1 = ApplicationFactory(editor=self.editor1, partner=self.partner1, status=Application.PENDING) self.app2 = ApplicationFactory(editor=self.editor2, partner=self.partner1, status=Application.PENDING) self.app3 = ApplicationFactory(editor=self.editor3, partner=self.partner1, status=Application.PENDING) self.app4 = ApplicationFactory(editor=self.editor1, partner=self.partner2, status=Application.PENDING) self.app5 = ApplicationFactory(editor=self.editor2, partner=self.partner2, status=Application.PENDING) self.app6 = ApplicationFactory(editor=self.editor3, partner=self.partner2, status=Application.PENDING) self.app7 = ApplicationFactory(editor=self.editor1, partner=self.partner3, status=Application.PENDING) self.app8 = ApplicationFactory(editor=self.editor1, partner=self.partner4, status=Application.PENDING) self.app9 = ApplicationFactory(editor=self.editor2, partner=self.partner3, status=Application.PENDING) # Editor 4 will update status on applications to partners 1 and 2. # Send the application self.client.post( reverse("applications:evaluate", kwargs={"pk": self.app1.pk}), data={"status": Application.SENT}, follow=True, ) self.app1.refresh_from_db() self.auth_app1 = Authorization.objects.get( authorizer=self.editor4.user, user=self.editor1.user, partner=self.partner1, ) # Approve the application self.client.post( reverse("applications:evaluate", kwargs={"pk": self.app2.pk}), data={"status": Application.APPROVED}, follow=True, ) self.app2.refresh_from_db() self.auth_app2 = Authorization( authorizer=self.editor4.user, user=self.editor2.user, partner=self.partner1, ) # Send the application self.client.post( reverse("applications:evaluate", kwargs={"pk": self.app3.pk}), data={"status": Application.SENT}, follow=True, ) self.app3.refresh_from_db() self.auth_app3 = Authorization.objects.get( authorizer=self.editor4.user, user=self.editor3.user, partner=self.partner1, ) # PROXY authorization methods don't set .SENT on the evaluate page; # .APPROVED will automatically update them to .SENT # This app was created with a factory, which doesn't create a revision. # Let's update the status so that we have one. self.client.post( reverse("applications:evaluate", kwargs={"pk": self.app4.pk}), data={"status": Application.QUESTION}, follow=True, ) # Approve the application self.client.post( reverse("applications:evaluate", kwargs={"pk": self.app4.pk}), data={"status": Application.APPROVED}, follow=True, ) self.app4.refresh_from_db() self.auth_app4 = Authorization.objects.get( authorizer=self.editor4.user, user=self.editor1.user, partner=self.partner2, ) # This app was created with a factory, which doesn't create a revision. # Let's update the status so that we have one. self.client.post( reverse("applications:evaluate", kwargs={"pk": self.app5.pk}), data={"status": Application.QUESTION}, follow=True, ) # Approve the application self.client.post( reverse("applications:evaluate", kwargs={"pk": self.app5.pk}), data={"status": Application.APPROVED}, follow=True, ) self.app5.refresh_from_db() self.auth_app5 = Authorization.objects.get( authorizer=self.editor4.user, user=self.editor2.user, partner=self.partner2, ) # Set up an access code to distribute self.access_code = AccessCode(code="ABCD-EFGH-IJKL", partner=self.partner3) self.access_code.save() self.message_patcher = patch( "TWLight.applications.views.messages.add_message") self.message_patcher.start() def tearDown(self): super(AuthorizationBaseTestCase, self).tearDown() self.partner1.delete() self.partner2.delete() self.partner3.delete() self.partner4.delete() self.access_code.delete() self.editor1.delete() self.editor2.delete() self.editor3.delete() self.editor4.delete() self.app1.delete() self.app2.delete() self.app3.delete() self.app4.delete() self.app5.delete() self.app6.delete() self.app7.delete() self.app8.delete() self.app9.delete()