|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""Tests for admin.loa.forms.LoAForm.""" |
| 3 | +import pytest |
| 4 | + |
| 5 | +from admin.loa.forms import LoAForm |
| 6 | +from osf.models.loa import LoA |
| 7 | +from osf_tests.factories import InstitutionFactory, AuthUserFactory |
| 8 | + |
| 9 | +pytestmark = pytest.mark.django_db |
| 10 | + |
| 11 | + |
| 12 | +class TestLoAForm: |
| 13 | + """Tests for LoAForm.""" |
| 14 | + |
| 15 | + def test_form_fields(self): |
| 16 | + form = LoAForm() |
| 17 | + assert 'aal' in form.fields |
| 18 | + assert 'ial' in form.fields |
| 19 | + assert 'is_mfa' in form.fields |
| 20 | + |
| 21 | + def test_form_valid_with_all_fields(self): |
| 22 | + form = LoAForm(data={'aal': '2', 'ial': '2', 'is_mfa': 'True'}) |
| 23 | + assert form.is_valid(), form.errors |
| 24 | + |
| 25 | + def test_form_valid_with_zero_values(self): |
| 26 | + """aal=0 and ial=0 represent the NULL choice.""" |
| 27 | + form = LoAForm(data={'aal': '0', 'ial': '0', 'is_mfa': 'False'}) |
| 28 | + assert form.is_valid(), form.errors |
| 29 | + |
| 30 | + def test_form_valid_with_empty_fields(self): |
| 31 | + """All fields are not required, so empty strings should be accepted.""" |
| 32 | + form = LoAForm(data={'aal': '', 'ial': '', 'is_mfa': ''}) |
| 33 | + assert form.is_valid(), form.errors |
| 34 | + |
| 35 | + def test_form_aal_choices(self): |
| 36 | + form = LoAForm() |
| 37 | + aal_values = [c[0] for c in form.fields['aal'].choices] |
| 38 | + assert 0 in aal_values |
| 39 | + assert 1 in aal_values |
| 40 | + assert 2 in aal_values |
| 41 | + |
| 42 | + def test_form_ial_choices(self): |
| 43 | + form = LoAForm() |
| 44 | + ial_values = [c[0] for c in form.fields['ial'].choices] |
| 45 | + assert 0 in ial_values |
| 46 | + assert 1 in ial_values |
| 47 | + assert 2 in ial_values |
| 48 | + |
| 49 | + def test_form_is_mfa_choices(self): |
| 50 | + form = LoAForm() |
| 51 | + mfa_values = [c[0] for c in form.fields['is_mfa'].choices] |
| 52 | + assert False in mfa_values |
| 53 | + assert True in mfa_values |
| 54 | + |
| 55 | + def test_form_widget_css_class(self): |
| 56 | + """All field widgets should have 'form-control form-control-sm' CSS class.""" |
| 57 | + form = LoAForm() |
| 58 | + for field in form.fields.values(): |
| 59 | + assert 'form-control form-control-sm' in field.widget.attrs.get('class', '') |
| 60 | + |
| 61 | + def test_form_with_instance(self): |
| 62 | + """Form should populate correctly from an existing LoA instance.""" |
| 63 | + institution = InstitutionFactory() |
| 64 | + modifier = AuthUserFactory() |
| 65 | + loa = LoA.objects.create( |
| 66 | + institution=institution, aal=2, ial=1, is_mfa=True, modifier=modifier, |
| 67 | + ) |
| 68 | + form = LoAForm(instance=loa) |
| 69 | + assert form.initial['aal'] == 2 |
| 70 | + assert form.initial['ial'] == 1 |
| 71 | + assert form.initial['is_mfa'] is True |
| 72 | + |
| 73 | + def test_form_meta_model(self): |
| 74 | + assert LoAForm.Meta.model is LoA |
| 75 | + |
| 76 | + def test_form_meta_fields(self): |
| 77 | + assert LoAForm.Meta.fields == ('aal', 'ial', 'is_mfa') |
| 78 | + |
| 79 | + def test_form_invalid_aal_choice(self): |
| 80 | + form = LoAForm(data={'aal': '99', 'ial': '1', 'is_mfa': 'False'}) |
| 81 | + assert not form.is_valid() |
| 82 | + assert 'aal' in form.errors |
0 commit comments