test_utils.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. from ...models import Agreement, UserAgreement
  2. from ..utils import disable_agreement, set_agreement_as_active
  3. def test_activating_inactive_agreement_updates_its_flag_but_doesnt_commit_to_db(db):
  4. agreement = Agreement.objects.create(
  5. type=Agreement.TYPE_PRIVACY, link="https://somewhre.com", text="Lorem ipsum"
  6. )
  7. set_agreement_as_active(agreement)
  8. assert agreement.is_active
  9. agreement.refresh_from_db()
  10. assert not agreement.is_active
  11. def test_activating_inactive_agreement_with_commit_updates_its_flag_in_db(db):
  12. agreement = Agreement.objects.create(
  13. type=Agreement.TYPE_PRIVACY, link="https://somewhre.com", text="Lorem ipsum"
  14. )
  15. set_agreement_as_active(agreement, commit=True)
  16. assert agreement.is_active
  17. agreement.refresh_from_db()
  18. assert agreement.is_active
  19. def test_activating_agreement_deactivates_other_active_agreement_of_same_type(db):
  20. old_agreement = Agreement.objects.create(
  21. type=Agreement.TYPE_PRIVACY,
  22. link="https://somewhre.com",
  23. text="Lorem ipsum",
  24. is_active=True,
  25. )
  26. new_agreement = Agreement.objects.create(
  27. type=Agreement.TYPE_PRIVACY, link="https://somewhre.com", text="Lorem ipsum"
  28. )
  29. set_agreement_as_active(new_agreement, commit=True)
  30. old_agreement.refresh_from_db()
  31. new_agreement.refresh_from_db()
  32. assert not old_agreement.is_active
  33. assert new_agreement.is_active
  34. def test_activating_agreement_doesnt_deactivate_agreement_of_other_type(db):
  35. agreement = Agreement.objects.create(
  36. type=Agreement.TYPE_PRIVACY, link="https://somewhre.com", text="Lorem ipsum"
  37. )
  38. other_type_agreement = Agreement.objects.create(
  39. type=Agreement.TYPE_TOS,
  40. link="https://somewhre.com",
  41. text="Lorem ipsum",
  42. is_active=True,
  43. )
  44. set_agreement_as_active(agreement, commit=True)
  45. agreement.refresh_from_db()
  46. other_type_agreement.refresh_from_db()
  47. assert agreement.is_active
  48. assert other_type_agreement.is_active
  49. def test_disabling_active_agreement_updates_its_flag_but_doesnt_commit_to_db(db):
  50. agreement = Agreement.objects.create(
  51. type=Agreement.TYPE_PRIVACY,
  52. link="https://somewhre.com",
  53. text="Lorem ipsum",
  54. is_active=True,
  55. )
  56. disable_agreement(agreement)
  57. assert not agreement.is_active
  58. agreement.refresh_from_db()
  59. assert agreement.is_active
  60. def test_disabling_active_agreement_with_commit_updates_its_flag_in_db(db):
  61. agreement = Agreement.objects.create(
  62. type=Agreement.TYPE_PRIVACY,
  63. link="https://somewhre.com",
  64. text="Lorem ipsum",
  65. is_active=True,
  66. )
  67. disable_agreement(agreement, commit=True)
  68. assert not agreement.is_active
  69. agreement.refresh_from_db()
  70. assert not agreement.is_active