Skip to content

Commit

Permalink
Tests for changes in the model and serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
José Redrejo committed Oct 17, 2023
1 parent b2e2c2e commit 57c6aa3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
26 changes: 26 additions & 0 deletions kolibri/core/auth/test/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,32 @@ def test_creating_user_same_username_case_insensitive(self):
response.data[0]["id"], error_constants.USERNAME_ALREADY_EXISTS
)

def test_do_not_allow_emails_in_usernames(self):
data = {
"username": "[email protected]",
"password": DUMMY_PASSWORD,
"facility": self.facility.id,
}
response = self.client.post(
reverse("kolibri:core:facilityuser-list"), data, format="json"
)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(response.data[0]["id"], error_constants.INVALID)
self.assertEqual(response.data[0]["metadata"]["field"], "username")

def test_max_length_username_in_api(self):
data = {
"username": 32 * "gh",
"password": DUMMY_PASSWORD,
"facility": self.facility.id,
}
response = self.client.post(
reverse("kolibri:core:facilityuser-list"), data, format="json"
)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(response.data[0]["id"], error_constants.MAX_LENGTH)
self.assertEqual(response.data[0]["metadata"]["field"], "username")


class UserUpdateTestCase(APITestCase):
@classmethod
Expand Down
20 changes: 20 additions & 0 deletions kolibri/core/auth/test/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,26 @@ def test_deserialize_empty_password(self):
self.assertEqual("bob", user.username)
self.assertEqual(NOT_SPECIFIED, user.password)

def test_username_validation(self):
self.facility = Facility.objects.create()
self.device_settings = DeviceSettings.objects.create()
user1 = FacilityUser.objects.create(
username="[email protected]",
password="password",
facility=self.facility,
)
user1.full_clean()
user2 = FacilityUser.objects.create(
username="@bob", password="password", facility=self.facility
)
with self.assertRaises(ValidationError):
user2.full_clean()
user3 = FacilityUser.objects.create(
username=32 * "gh", password="password", facility=self.facility
)
with self.assertRaises(ValidationError):
user3.full_clean()


class CollectionHierarchyTestCase(TestCase):
def test_facility_with_parent(self):
Expand Down

0 comments on commit 57c6aa3

Please sign in to comment.