def test_accept_group_invitation(data_fixture): group = data_fixture.create_group() group_2 = data_fixture.create_group() group_invitation = data_fixture.create_group_invitation( email='*****@*****.**', permissions='MEMBER', group=group ) user_1 = data_fixture.create_user(email='*****@*****.**') user_2 = data_fixture.create_user(email='*****@*****.**') handler = CoreHandler() with pytest.raises(GroupInvitationEmailMismatch): handler.accept_group_invitation(user=user_2, invitation=group_invitation) assert GroupInvitation.objects.all().count() == 1 group_user = handler.accept_group_invitation( user=user_1, invitation=group_invitation ) assert group_user.group_id == group.id assert group_user.permissions == 'MEMBER' assert GroupInvitation.objects.all().count() == 0 assert GroupUser.objects.all().count() == 1 group_invitation = data_fixture.create_group_invitation( email='*****@*****.**', permissions='ADMIN', group=group ) group_user = handler.accept_group_invitation( user=user_1, invitation=group_invitation ) assert group_user.group_id == group.id assert group_user.permissions == 'ADMIN' assert GroupInvitation.objects.all().count() == 0 assert GroupUser.objects.all().count() == 1 group_invitation = data_fixture.create_group_invitation( email='*****@*****.**', permissions='MEMBER', group=group_2 ) group_user = handler.accept_group_invitation( user=user_1, invitation=group_invitation ) assert group_user.group_id == group_2.id assert group_user.permissions == 'MEMBER' assert GroupInvitation.objects.all().count() == 0 assert GroupUser.objects.all().count() == 2
def create_user(self, name, email, password, group_invitation_token=None): """ Creates a new user with the provided information and creates a new group and application for him. If the optional group invitation is provided then the user joins that group without creating a new one. :param name: The name of the new user. :type name: str :param email: The e-mail address of the user, this is also the username. :type email: str :param password: The password of the user. :type password: str :param group_invitation_token: If provided and valid, the invitation will be accepted and and initial group will not be created. :type group_invitation_token: str :raises: UserAlreadyExist: When a user with the provided username (email) already exists. :raises GroupInvitationEmailMismatch: If the group invitation email does not match the one of the user. :return: The user object. :rtype: User """ email = normalize_email_address(email) if User.objects.filter(Q(email=email) | Q(username=email)).exists(): raise UserAlreadyExist( f'A user with username {email} already exists.') core_handler = CoreHandler() group_invitation = None group_user = None if group_invitation_token: group_invitation = core_handler.get_group_invitation_by_token( group_invitation_token) if email != group_invitation.email: raise GroupInvitationEmailMismatch( 'The email address of the invitation does not match the one of the ' 'user.') user = User(first_name=name, email=email, username=email) user.set_password(password) user.save() if group_invitation_token: group_user = core_handler.accept_group_invitation( user, group_invitation) if not group_user: group_user = core_handler.create_group(user=user, name=f"{name}'s group") # Call the user_created method for each plugin that is un the registry. for plugin in plugin_registry.registry.values(): plugin.user_created(user, group_user.group, group_invitation) return user
def create_user(self, name, email, password, group_invitation_token=None, template=None): """ Creates a new user with the provided information and creates a new group and application for him. If the optional group invitation is provided then the user joins that group without creating a new one. :param name: The name of the new user. :type name: str :param email: The e-mail address of the user, this is also the username. :type email: str :param password: The password of the user. :type password: str :param group_invitation_token: If provided and valid, the invitation will be accepted and and initial group will not be created. :type group_invitation_token: str :param template: If provided, that template will be installed into the newly created group. :type template: Template :raises: UserAlreadyExist: When a user with the provided username (email) already exists. :raises GroupInvitationEmailMismatch: If the group invitation email does not match the one of the user. :raises SignupDisabledError: If signing up is disabled. :return: The user object. :rtype: User """ core_handler = CoreHandler() if not core_handler.get_settings().allow_new_signups: raise DisabledSignupError("Sign up is disabled.") email = normalize_email_address(email) if User.objects.filter(Q(email=email) | Q(username=email)).exists(): raise UserAlreadyExist( f"A user with username {email} already exists.") group_invitation = None group_user = None if group_invitation_token: group_invitation = core_handler.get_group_invitation_by_token( group_invitation_token) if email != group_invitation.email: raise GroupInvitationEmailMismatch( "The email address of the invitation does not match the one of the " "user.") user = User(first_name=name, email=email, username=email) user.set_password(password) if not User.objects.exists(): # This is the first ever user created in this baserow instance and # therefore the administrator user, lets give them staff rights so they # can set baserow wide settings. user.is_staff = True user.save() if group_invitation_token: group_user = core_handler.accept_group_invitation( user, group_invitation) if not group_user: group_user = core_handler.create_group(user=user, name=f"{name}'s group") if not group_invitation_token and template: core_handler.install_template(user, group_user.group, template) # Call the user_created method for each plugin that is un the registry. for plugin in plugin_registry.registry.values(): plugin.user_created(user, group_user.group, group_invitation, template) return user