def test_shows_msg_if_already_has_disclaimer(self): self.login(self.student_user) make_online_disclaimer(user=self.student_user, version=self.content.version) url = reverse('accounts:disclaimer_form', args=(self.student_user.id,)) # user has disclaimer and gets redirected resp = self.client.get(url) assert resp.status_code == 302 assert resp.url == reverse("accounts:profile")
def test_disclaimers_not_deleted_if_updated_in_past_6_years(self): # make a user with a disclaimer created > yr ago but updated in past yr user = baker.make(User) make_online_disclaimer( user=user, date=timezone.now() - timedelta(2200), date_updated=timezone.now() - timedelta(2000), ) assert OnlineDisclaimer.objects.count() == 3 management.call_command('delete_expired_disclaimers') assert OnlineDisclaimer.objects.count() == 1
def test_disclaimers_not_deleted_if_created_in_past_6_years(self): # make a user with a disclaimer created today user = baker.make(User) make_online_disclaimer(user=user) make_nonregistered_disclaimer() make_archived_disclaimer() assert OnlineDisclaimer.objects.count() == 3 assert NonRegisteredDisclaimer.objects.count() == 2 assert ArchivedDisclaimer.objects.count() == 2 # disclaimer should not be deleted because it was created < 3 yrs ago. # All others will be. management.call_command('delete_expired_disclaimers') assert OnlineDisclaimer.objects.count() == 1
def test_cannot_create_new_active_disclaimer(self): # disclaimer is out of date, so inactive disclaimer = make_online_disclaimer(user=self.student_user, date=datetime(2015, 2, 10, 19, 0, tzinfo=timezone.utc), version=self.content.version) assert disclaimer.is_active is False # can make a new disclaimer make_online_disclaimer(user=self.student_user, version=self.content.version) # can't make new disclaimer when one is already active with pytest.raises(ValidationError): make_online_disclaimer(user=self.student_user, version=self.content.version)
def test_new_online_disclaimer_with_current_version_is_active(self): disclaimer_content = make_disclaimer_content( version=None ) # ensure version is incremented from any existing ones disclaimer = make_online_disclaimer(user=self.student_user, version=disclaimer_content.version) assert disclaimer.is_active make_disclaimer_content(version=None) assert disclaimer.is_active is False
def setUp(self): self.create_users() make_online_disclaimer(user=self.student_user, date=timezone.now() - timedelta(2200)) # > 6 yrs make_online_disclaimer( user=self.student_user1, date=timezone.now() - timedelta(2200) # > 6 yrs ) make_nonregistered_disclaimer( first_name='Test', last_name='Nonreg', date=timezone.now() - timedelta(2200) # > 6 yrs ) make_archived_disclaimer( name='Test Archived', date=timezone.now() - timedelta(2200) # > 6 yrs )
def test_delete_online_disclaimer(self): assert ArchivedDisclaimer.objects.exists() is False disclaimer = make_online_disclaimer(user=self.student_user, version=self.content.version) disclaimer.delete() assert ArchivedDisclaimer.objects.exists() is True archived = ArchivedDisclaimer.objects.first() assert archived.name == f"{disclaimer.user.first_name} {disclaimer.user.last_name}" assert archived.date == disclaimer.date
def test_delete_online_disclaimer_older_than_6_yrs(self): assert ArchivedDisclaimer.objects.exists() is False # disclaimer created > 6yrs ago disclaimer = make_online_disclaimer(user=self.student_user, date=timezone.now() - timedelta(2200), version=self.content.version) disclaimer.delete() # no archive created assert ArchivedDisclaimer.objects.exists() is False # disclaimer created > 6yrs ago, update < 6yrs ago disclaimer = make_online_disclaimer( user=self.student_user, date=timezone.now() - timedelta(2200), date_updated=timezone.now() - timedelta(1000), version=self.content.version) disclaimer.delete() # archive created assert ArchivedDisclaimer.objects.exists() is True
def test_online_disclaimer_str(self, ): disclaimer = make_online_disclaimer(user=self.student_user, version=self.content.version) assert str(disclaimer) == '[email protected] - V5.0 - {}'.format( disclaimer.date.astimezone( pytz.timezone('Europe/London')).strftime('%d %b %Y, %H:%M'))
def test_updating_disclaimer_health_questionnaire(self): # health questionnaire fields that exist on the new disclaimer are prepopulated # skip choices fields that are different now # health form fields are extracted and set to expired disclaimer content_with_questionnaire = make_disclaimer_content( form=[ { 'type': 'text', 'required': False, 'label': 'Say something', 'name': 'text-1', 'subtype': 'text' }, { 'type': 'select', 'required': True, 'label': 'What is your favourite colour?', 'name': 'text-2', 'values': [ {"label": "Red", "value": "red"}, {"label": "Green", "value": "green"}, {"label": "Blue", "value": "blue"}, ], 'subtype': 'text' } ] ) # make expired disclaimers with existing entries make_online_disclaimer( user=self.student_user, version=content_with_questionnaire.version, date=timezone.now() - timedelta(days=370), health_questionnaire_responses={ "Say something": "OK", 'What is your favourite colour?': ["blue"] } ) make_online_disclaimer( user=self.student_user1, version=content_with_questionnaire.version, date=timezone.now() - timedelta(days=370), health_questionnaire_responses={ "Say something": "Boo", 'What is your favourite colour?': ["purple"] # not in new disclaimer choices } ) self.login(self.student_user) url = reverse('accounts:disclaimer_form', args=(self.student_user.id,)) resp = self.client.get(url) form = resp.context_data["form"] # disclaimer content questionnaire fields have been prepopulated questionnaire_fields = form.fields['health_questionnaire_responses'].fields assert questionnaire_fields[0].initial == "OK" assert questionnaire_fields[1].initial == ["blue"] self.login(self.student_user1) url = reverse('accounts:disclaimer_form', args=(self.student_user1.id,)) resp = self.client.get(url) form = resp.context_data["form"] # disclaimer content questionnaire fields have been prepopulated questionnaire_fields = form.fields['health_questionnaire_responses'].fields assert questionnaire_fields[0].initial == "Boo" assert questionnaire_fields[1].initial is None