@@ -617,3 +617,87 @@ def test_register_with_sign_up_type(client, db, settings): # type: ignore[no-un
617617 assert response_json ["sign_up_type" ] == sign_up_type
618618
619619 assert FFAdminUser .objects .filter (email = email , sign_up_type = sign_up_type ).exists ()
620+
621+
622+ def test_can_create_superuser (
623+ db : None , api_client : APIClient , mocker : MockerFixture
624+ ) -> None :
625+ # Given
626+ mocker .patch ("custom_auth.serializers.is_saas" , return_value = False )
627+
628+ email = "test@example.com"
629+ password = FFAdminUser .objects .make_random_password ()
630+ register_data = {
631+ "email" : email ,
632+ "password" : password ,
633+ "re_password" : password ,
634+ "first_name" : "user" ,
635+ "last_name" : "test" ,
636+ "superuser" : True ,
637+ "other_field" : "meh" ,
638+ }
639+ url = reverse ("api-v1:custom_auth:ffadminuser-list" )
640+
641+ # When
642+ response = api_client .post (url , data = register_data )
643+
644+ # Then
645+ assert response .status_code == status .HTTP_201_CREATED
646+ user = FFAdminUser .objects .get (email = email )
647+ assert user .superuser is True
648+
649+
650+ def test_cannot_create_superuser_on_saas_build (
651+ db : None , api_client : APIClient , mocker : MockerFixture
652+ ) -> None :
653+ # Given
654+ mocker .patch ("custom_auth.serializers.is_saas" , return_value = True )
655+
656+ email = "test@example.com"
657+ password = FFAdminUser .objects .make_random_password ()
658+ register_data = {
659+ "email" : email ,
660+ "password" : password ,
661+ "re_password" : password ,
662+ "first_name" : "user" ,
663+ "last_name" : "test" ,
664+ "superuser" : True ,
665+ }
666+ url = reverse ("api-v1:custom_auth:ffadminuser-list" )
667+
668+ # When
669+ response = api_client .post (url , data = register_data )
670+
671+ # Then
672+ assert response .status_code == status .HTTP_201_CREATED
673+ user = FFAdminUser .objects .get (email = email )
674+ assert user .superuser is False
675+
676+
677+ def test_cannot_create_superuser_if_any_user_exists (
678+ admin_user : FFAdminUser , api_client : APIClient , mocker : MockerFixture
679+ ) -> None :
680+ # Given
681+ mocker .patch ("custom_auth.serializers.is_saas" , return_value = False )
682+
683+ email = "test@example.com"
684+ password = FFAdminUser .objects .make_random_password ()
685+ register_data = {
686+ "email" : email ,
687+ "password" : password ,
688+ "re_password" : password ,
689+ "first_name" : "user" ,
690+ "last_name" : "test" ,
691+ "superuser" : True ,
692+ }
693+ url = reverse ("api-v1:custom_auth:ffadminuser-list" )
694+
695+ # When
696+ response = api_client .post (url , data = register_data )
697+
698+ # Then
699+ assert response .status_code == status .HTTP_400_BAD_REQUEST
700+ assert response .json ()["superuser" ] == [
701+ "A superuser can only be created through this endpoint if no other users exist."
702+ ]
703+ assert FFAdminUser .objects .filter (email = email ).exists () is False
0 commit comments