def test_age_and_dob_required(self):
     form = EditProfileForm(data={})
     self.assertFalse(form.is_valid())
     self.assertEqual(
         form.errors, {
             "__all__": ["Enter either birth date or age"],
             "gender": ["This field is required."],
         })
 def test_min_required_age(self):
     form = EditProfileForm(data={
         "age": constants.CONSENT_AGE - 1,
     })
     self.assertFalse(form.is_valid())
     self.assertEqual(form.errors, {
         "gender": ["This field is required."],
         "age": [
             "We are sorry, " \
             f"users under the age of {constants.CONSENT_AGE}" \
             " cannot create an account."
         ]
     })
     with mock.patch("authentication_service.forms.date") as mocked_date:
         mocked_date.today.return_value = datetime.date(2018, 1, 2)
         mocked_date.side_effect = lambda *args, **kw: datetime.date(
             *args, **kw)
         form = EditProfileForm(data={
             "birth_date":
             datetime.date(2018 - constants.CONSENT_AGE, 1, 3),
         })
         self.assertFalse(form.is_valid())
         form = EditProfileForm(
             data={
                 "birth_date":
                 datetime.date(2018 - constants.CONSENT_AGE + 1, 1, 3),
             })
         self.assertFalse(form.is_valid())
 def test_edit_profile(self):
     user = get_user_model().objects.create_user(
         username="******",
         email="*****@*****.**",
         password="******",
         birth_date=datetime.date(2001, 1, 1))
     form = EditProfileForm(instance=user)
     html = form.as_div()
     for name, field in form.fields.items():
         if field.required:
             self.assertIn("*", field.label)
             self.assertIn(field.label, html)
    def test_default_state(self):
        form = EditProfileForm(instance=self.user)

        initial_dict = {"email": "*****@*****.**"}

        # Check initial values
        self.assertTrue(
            set(initial_dict.items()).issubset(set(form.initial.items())))
    def test_invalid_form(self):
        data = {"email": "not_an_email", "gender": "no", "country": "abc"}

        form = EditProfileForm(instance=self.user, data=data)

        self.assertFalse(form.is_valid())
        self.assertEqual(
            form.errors, {
                "email": ["Enter a valid email address."],
                "gender": [
                    "Select a valid choice. no is not one of the available "
                    "choices."
                ],
                "country": [
                    "Select a valid choice. That choice is not one of the "
                    "available choices."
                ],
            })
 def test_on_required_age(self):
     form = EditProfileForm(data={
         "age": constants.CONSENT_AGE,
         "gender": "female"
     })
     self.assertTrue(form.is_valid())
     with mock.patch("authentication_service.forms.date") as mocked_date:
         mocked_date.today.return_value = datetime.date(2018, 1, 2)
         mocked_date.side_effect = lambda *args, **kw: datetime.date(
             *args, **kw)
         form = EditProfileForm(
             data={
                 "birth_date": datetime.date(2018 -
                                             constants.CONSENT_AGE, 1, 2),
                 "gender": "female"
             })
         self.assertTrue(form.is_valid())
    def test_update_profile(self):
        data = {
            "email": "*****@*****.**",
            "msisdn": "+27821234567",
            "age": 34,
            "gender": "female"
        }

        form = EditProfileForm(instance=self.user, data=data)
        self.assertTrue(form.has_changed())
        self.assertTrue(form.is_valid())
        form.save()

        user = get_user_model().objects.get(username=self.user.username)
        self.assertNotEqual(datetime.date(2000, 1, 1), user.birth_date)
        self.assertEqual(data["email"], user.email)
        self.assertEqual(data["msisdn"], user.msisdn)
 def test_min_required_age_dob(self):
     form = EditProfileForm(
         data={
             "birth_date": datetime.date.today() - relativedelta(years=10),
         })
     self.assertFalse(form.is_valid())
    def test_nothing_updated(self):
        data = model_to_dict(self.user)

        form = EditProfileForm(instance=self.user, data=data)
        self.assertFalse(form.has_changed())
        self.assertTrue(form.is_valid())