|
| 1 | +import pytest |
| 2 | +from rest_framework import serializers |
| 3 | + |
| 4 | +from plane.app.serializers.user import UserSerializer |
| 5 | + |
| 6 | + |
| 7 | +@pytest.mark.unit |
| 8 | +class TestUserSerializer: |
| 9 | + """Test the UserSerializer""" |
| 10 | + |
| 11 | + def test_validate_first_name_valid(self): |
| 12 | + """Test that valid first names are accepted""" |
| 13 | + |
| 14 | + serializer = UserSerializer() |
| 15 | + valid_names = [ |
| 16 | + "John", |
| 17 | + "John Doe", |
| 18 | + "John-Doe", |
| 19 | + "John_Doe", |
| 20 | + "John123", |
| 21 | + ] |
| 22 | + |
| 23 | + for name in valid_names: |
| 24 | + result = serializer.validate_first_name(name) |
| 25 | + |
| 26 | + assert result == name |
| 27 | + |
| 28 | + def test_validate_first_name_with_url(self): |
| 29 | + """Test that first names containing URLs are rejected""" |
| 30 | + |
| 31 | + serializer = UserSerializer() |
| 32 | + invalid_names = [ |
| 33 | + "http://example.com", |
| 34 | + "John https://test.com", |
| 35 | + "www.test.com", |
| 36 | + ] |
| 37 | + |
| 38 | + for name in invalid_names: |
| 39 | + with pytest.raises(serializers.ValidationError) as exc_info: |
| 40 | + serializer.validate_first_name(name) |
| 41 | + |
| 42 | + assert str(exc_info.value.detail[0]) == "First name cannot contain a URL." |
| 43 | + |
| 44 | + def test_validate_first_name_with_special_chars(self): |
| 45 | + """Test that first names with special characters are rejected""" |
| 46 | + |
| 47 | + serializer = UserSerializer() |
| 48 | + invalid_names = [ |
| 49 | + "John@Doe", |
| 50 | + "John#Doe", |
| 51 | + "John$Doe", |
| 52 | + "John!Doe", |
| 53 | + "John&Doe", |
| 54 | + ] |
| 55 | + |
| 56 | + for name in invalid_names: |
| 57 | + with pytest.raises(serializers.ValidationError) as exc_info: |
| 58 | + serializer.validate_first_name(name) |
| 59 | + |
| 60 | + assert str(exc_info.value.detail[0]) == ( |
| 61 | + "first name can only contain letters, numbers, " |
| 62 | + "hyphens (-), and underscores (_)" |
| 63 | + ) |
| 64 | + |
| 65 | + def test_validate_last_name_valid(self): |
| 66 | + """Test that valid last names are accepted""" |
| 67 | + |
| 68 | + serializer = UserSerializer() |
| 69 | + valid_names = [ |
| 70 | + "Smith", |
| 71 | + "Smith Jr", |
| 72 | + "Smith-Jr", |
| 73 | + "Smith_Jr", |
| 74 | + "Smith123", |
| 75 | + "", |
| 76 | + ] |
| 77 | + |
| 78 | + for name in valid_names: |
| 79 | + result = serializer.validate_last_name(name) |
| 80 | + |
| 81 | + assert result == name |
| 82 | + |
| 83 | + def test_validate_last_name_with_url(self): |
| 84 | + """Test that last names containing URLs are rejected""" |
| 85 | + |
| 86 | + serializer = UserSerializer() |
| 87 | + invalid_names = [ |
| 88 | + "http://example.com", |
| 89 | + "Smith https://test.com", |
| 90 | + "www.test.com", |
| 91 | + ] |
| 92 | + |
| 93 | + for name in invalid_names: |
| 94 | + with pytest.raises(serializers.ValidationError) as exc_info: |
| 95 | + serializer.validate_last_name(name) |
| 96 | + |
| 97 | + assert str(exc_info.value.detail[0]) == "Last name cannot contain a URL." |
| 98 | + |
| 99 | + def test_validate_last_name_with_special_chars(self): |
| 100 | + """Test that last names with special characters are rejected""" |
| 101 | + |
| 102 | + serializer = UserSerializer() |
| 103 | + invalid_names = [ |
| 104 | + "Smith@Jr", |
| 105 | + "Smith#Jr", |
| 106 | + "Smith$Jr", |
| 107 | + "Smith!Jr", |
| 108 | + "Smith&Jr", |
| 109 | + ] |
| 110 | + |
| 111 | + for name in invalid_names: |
| 112 | + with pytest.raises(serializers.ValidationError) as exc_info: |
| 113 | + serializer.validate_last_name(name) |
| 114 | + |
| 115 | + assert str(exc_info.value.detail[0]) == ( |
| 116 | + "last name can only contain letters, numbers, " |
| 117 | + "hyphens (-), and underscores (_)" |
| 118 | + ) |
0 commit comments