@@ -187,21 +187,56 @@ def test_mapping_update_models_from_rest_to_db():
187187 }
188188
189189
190- def test_my_profile_patch_username_validation ():
190+ def test_my_profile_patch_username_min_len ():
191191 # minimum length username is 4
192192 with pytest .raises (ValidationError ) as err_info :
193193 MyProfilePatch .model_validate ({"userName" : "abc" })
194194
195195 assert err_info .value .error_count () == 1
196196 assert err_info .value .errors ()[0 ]["type" ] == "too_short"
197197
198- MyProfilePatch .model_validate ({"userName" : "abcd" })
198+ MyProfilePatch .model_validate ({"userName" : "abcd" }) # OK
199199
200+
201+ def test_my_profile_patch_username_valid_characters ():
200202 # Ensure valid characters (alphanumeric + . _ -)
201203 with pytest .raises (ValidationError , match = "start with a letter" ) as err_info :
202204 MyProfilePatch .model_validate ({"userName" : "1234" })
203205
204206 assert err_info .value .error_count () == 1
205207 assert err_info .value .errors ()[0 ]["type" ] == "value_error"
206208
207- MyProfilePatch .model_validate ({"userName" : "u1234" })
209+ MyProfilePatch .model_validate ({"userName" : "u1234" }) # OK
210+
211+
212+ def test_my_profile_patch_username_special_characters ():
213+ # Ensure no consecutive special characters
214+ with pytest .raises (
215+ ValidationError , match = "consecutive special characters"
216+ ) as err_info :
217+ MyProfilePatch .model_validate ({"userName" : "u1__234" })
218+
219+ assert err_info .value .error_count () == 1
220+ assert err_info .value .errors ()[0 ]["type" ] == "value_error"
221+
222+ MyProfilePatch .model_validate ({"userName" : "u1_234" }) # OK
223+
224+ # Ensure it doesn't end with a special character
225+ with pytest .raises (ValidationError , match = "end with" ) as err_info :
226+ MyProfilePatch .model_validate ({"userName" : "u1234_" })
227+
228+ assert err_info .value .error_count () == 1
229+ assert err_info .value .errors ()[0 ]["type" ] == "value_error"
230+
231+ MyProfilePatch .model_validate ({"userName" : "u1_234" }) # OK
232+
233+
234+ def test_my_profile_patch_username_reserved_words ():
235+ # Check reserved words (example list; extend as needed)
236+ with pytest .raises (ValidationError , match = "cannot be used" ) as err_info :
237+ MyProfilePatch .model_validate ({"userName" : "admin" })
238+
239+ assert err_info .value .error_count () == 1
240+ assert err_info .value .errors ()[0 ]["type" ] == "value_error"
241+
242+ MyProfilePatch .model_validate ({"userName" : "midas" }) # OK
0 commit comments