Skip to content

Commit

Permalink
DRY up submitting values logic
Browse files Browse the repository at this point in the history
  • Loading branch information
symroe committed Aug 7, 2024
1 parent b011b62 commit 0a7329a
Showing 1 changed file with 12 additions and 32 deletions.
44 changes: 12 additions & 32 deletions ynr/apps/people/tests/test_person_form_identifier_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,30 +152,6 @@ def _submit_values(self, value, value_type="twitter_username"):
form["tmp_person_identifiers-0-value"] = value
return form.submit()

def _submit_mastodon_values(self, value, value_type="mastodon_username"):
resp = self.app.get(
reverse("person-update", kwargs={"person_id": self.person.pk}),
user=self.user,
)

form = resp.forms[1]
form["source"] = "They changed their username"
form["tmp_person_identifiers-0-value_type"] = value_type
form["tmp_person_identifiers-0-value"] = value
return form.submit()

def _submit_instagram_values(self, value, value_type="instagram_url"):
resp = self.app.get(
reverse("person-update", kwargs={"person_id": self.person.pk}),
user=self.user,
)

form = resp.forms[1]
form["source"] = "They changed their username"
form["tmp_person_identifiers-0-value_type"] = value_type
form["tmp_person_identifiers-0-value"] = value
return form.submit()

def test_twitter_bad_url(self):
resp = self._submit_values("http://example.com/blah")
form = resp.context["identifiers_formset"]
Expand All @@ -196,8 +172,8 @@ def test_twitter_full_url(self):
)

def test_clean_instagram_url(self):
resp = self._submit_instagram_values(
"https://www.instagr.am/disco_dude"
resp = self._submit_values(
"https://www.instagr.am/disco_dude", value_type="instagram_url"
)
self.assertEqual(resp.status_code, 302)
instagram_url_qs = PersonIdentifier.objects.filter(
Expand All @@ -206,11 +182,13 @@ def test_clean_instagram_url(self):
self.assertEqual(instagram_url_qs.count(), 1)
self.assertEqual(
instagram_url_qs[0].value,
"https://www.instagr.am/disco_dude",
"https://www.instagram.com/disco_dude/",
)

def test_bad_instagram_domain(self):
resp = self._submit_instagram_values("www.instagl.am/blah")
resp = self._submit_values(
"www.instbad.am/blah", value_type="instagram_url"
)
form = resp.context["identifiers_formset"]
self.assertFalse(form.is_valid())
self.assertEqual(
Expand All @@ -219,20 +197,22 @@ def test_bad_instagram_domain(self):
)

def test_bad_instagram_username(self):
resp = self._submit_instagram_values(
"https://www.instagr.am/________blah"
resp = self._submit_values(
"https://www.instagr.am/___@_____blah", value_type="instagram_url"
)
self.assertEqual(resp.status_code, 200)
form = resp.context["identifiers_formset"]
self.assertFalse(form.is_valid())
self.assertEqual(
form[0].non_field_errors(),
["This is not a valid Instagram username. Please try again."],
["This is not a valid Instagram URL. Please try again."],
)

def test_mastodon_bad_url(self):
# submit a username missing the `@` symbol
resp = self._submit_mastodon_values("https://mastodon.social/joe")
resp = self._submit_values(
"https://mastodon.social/joe", value_type="mastodon_username"
)
form = resp.context["identifiers_formset"]
self.assertFalse(form.is_valid())
self.assertEqual(
Expand Down

0 comments on commit 0a7329a

Please sign in to comment.