Example #1
0
class ClientGroupTests(unittest.TestCase):
    def setUp(self):
        # NOTE: shared_secret needs to be filled out to run the tests. Deleted because
        # it shouldn't be in the commit history of a repo that will later be made public.

        self.shared_secret = 'test'
        self.config = config

        self.go_rest_client = GlobusOnlineRestClient(config=self.config)
        # Random numbers added to avoid overwriting some real user since these
        # tests may be run against a real server.
        self.default_username = '******'
        self.created_users = []
        self.created_groups = []

    def tearDown(self):
        for user in self.created_users:
            self.go_rest_client.delete_user(user)

        self.go_rest_client.logout()
        if len(self.created_groups) > 0:
            self.go_rest_client.username_password_login('testuser', 'sikrit')

        for group in self.created_groups:
            self.go_rest_client.delete_group(group)
        self.go_rest_client.logout()

    @attr('go_rest_test')
    def test_group_management(self):

        # We need to be logged in as a user that has admin rights to the root-group.
        username = '******'
        password = '******'
        self.go_rest_client.username_password_login(username,
                                                    password=password)

        # Get root group:
        response, content = self.go_rest_client.get_group_list(
        )  # times out often
        self.assertEquals(response['status'], '200')

        parent_group = 'testgroup'
        response, content = self.go_rest_client.post_group(parent_group)
        root_id = content['id']
        self.created_groups.append(root_id)

        # Create a subroup:
        subgroup_name = "Mattias' sub-group"
        response, content = self.go_rest_client.post_group(subgroup_name,
                                                           parent=root_id)
        self.assertEquals(response['status'], '201')
        self.created_groups.append(content['id'])

        # Get subgroups:
        response, content = self.go_rest_client.get_group_tree(root_id, 2)
        self.assertEquals(response['status'], '200')
        children = content['children']
        subgroup_id = None
        for child in children:
            if child['name'] == subgroup_name:
                subgroup_id = child['id']
        self.assertNotEqual(
            subgroup_id,
            None,
            msg='Created subgroup not found among children of the root.')

        # Edit group and get group summary to check that that the edit sticks:
        new_name = 'New group name'
        new_description = 'New group description'
        response, content = self.go_rest_client.put_group_summary(
            subgroup_id, name=new_name, description=new_description)
        self.assertEquals(response['status'], '201')
        response, content = self.go_rest_client.get_group_summary(subgroup_id)
        self.assertEquals(content['name'], new_name)
        self.assertEquals(content['description'], new_description)

        # Test putting and getting group policies:
        policy_summary = {
            'approval': {
                'admin': True,
                'auto_if_admin': False,
                'auto': False,
            },
            'group_member_visibility': {
                'admin': True,
                'members': False,
                'parent': False,
                'public': False,
            },
            'group_visibility': {
                'parent': True,
                'private': False,
                'public': False,
                'site': False,
            },
            'join': {
                'anybody': True,
                'community': False,
                'none': False,
                'parent': False,
            },
            'invites': {
                'admin_only': True,
                'any_community_member': False,
                'group_members': False,
                'group_members_and_parent': False,
            },
            'sign_up_fields': {
                'first_name': True,
                'last_name': True,
                'institution': False,
                'current_project_name': False,
                'organization': False,
                'address': False,
                'address2': False,
                'city': False,
                'country': False,
                'state': False,
                'zip': False,
                'phone': False,
            }
        }

        policies = self.go_rest_client.build_policy_dictionary(
            **policy_summary)

        response, content = self.go_rest_client.put_group_policies(
            root_id, policies)

        # there are policies that exist before calling put_group_policies.
        # this causes a 200 status to be returend instead of a 201
        # (the policies field is being edited instead of added)
        # this also causes the returned content and the policies input to be different

        # self.assertEquals(response['status'], '201')
        self.assertEquals(response['status'], '200')
        # self.assertEquals(content, policies)

        response, content = self.go_rest_client.put_group_policies(
            subgroup_id, policies)

        # self.assertEquals(response['status'], '201')
        self.assertEquals(response['status'], '200')
        # self.assertEquals(content, policies)

        response, content = self.go_rest_client.get_group_policies(subgroup_id)
        self.assertEquals(response['status'], '200')
        self.assertTrue(content['approval']['value']['admin']['value'])
        self.assertFalse(content['approval']['value']['auto']['value'])
        self.assertTrue(
            content['sign_up_fields']['value']['first_name']['value'])
        self.assertFalse(content['sign_up_fields']['value']
                         ['current_project_name']['value'])

        # Test set_single_policy:
        response, content = self.go_rest_client.set_single_policy(
            subgroup_id, 'approval', 'auto')
        self.assertFalse(content['approval']['value']['admin']['value'])
        self.assertTrue(content['approval']['value']['auto']['value'])
        # Should alse work for multi-option policies like signup fields:
        response, content = self.go_rest_client.set_single_policy(
            root_id, 'sign_up_fields',
            ['zip', 'state'])  # to satisfy parent-policy requirements
        response, content = self.go_rest_client.set_single_policy(
            subgroup_id, 'sign_up_fields', ['zip', 'state'])
        self.assertTrue(content['sign_up_fields']['value']['zip']['value'])
        self.assertTrue(content['sign_up_fields']['value']['state']['value'])

        self.assertFalse(
            content['sign_up_fields']['value']['first_name']['value'])

        # Newly created group should have the same email templates as the parent.
        response, content = self.go_rest_client.get_group_email_templates(
            root_id)
        root_default_templates = sorted(content['templates'],
                                        key=lambda k: k['type'])
        response, content = self.go_rest_client.get_group_email_templates(
            subgroup_id)
        self.assertEquals(response['status'], '200')
        new_default_templates = sorted(content['templates'],
                                       key=lambda k: k['type'])
        for new, root in zip(new_default_templates, root_default_templates):
            self.assertEquals(new['type'], root['type'])
            self.assertEquals(new['subject'], root['subject'])

        # Test POSTing email templates:
        template_params = {
            "type": "Welcome",
            "subject": "GO REST client test template",
            "last_updated": "2011-05-06T00:00:00",
            "create_date": "2011-05-06T00:00:00",
            "message": [{
                "type": "static",
                "text": "Welcome to the group!"
            }]
        }
        response, content = self.go_rest_client.post_group_email_templates(
            subgroup_id, template_params)
        self.assertEquals(response['status'], '201')
        template_id = content['id']

        # Re-posting the same template type should be a 409 Conflict:
        response, content = self.go_rest_client.post_group_email_templates(
            subgroup_id, template_params)
        self.assertEquals(response['status'], '409')

        # Test GETting templates:
        response, content = self.go_rest_client.get_group_email_templates(
            subgroup_id)
        self.assertTrue(
            template_params['subject'] in
            [template['subject'] for template in content['templates']])

        # Test GETing a single template:
        response, content = self.go_rest_client.get_group_email_template(
            subgroup_id, template_id)
        self.assertEquals(response['status'], '200')
        self.assertEquals(content['message'][0]['text'],
                          template_params['message'][0]['text'])

        # Test PUTting (updating) a template:
        new_subject = 'This is the new subject'
        content['subject'] = new_subject
        response, content = self.go_rest_client.put_group_email_template(
            subgroup_id, template_id, content)
        self.assertEquals(response['status'], '200')
        self.assertEquals(content['subject'], new_subject)

        # Test GETting a rendered template:
        response, content = self.go_rest_client.get_group_email_template(
            subgroup_id, template_id)
        self.assertEquals(response['status'], '200')

    @attr('go_rest_test')
    def test_membership_management(self):

        # Log in as an admin, create a group and a user to play with.
        admin_username = '******'
        admin_password = '******'
        group_name = 'testgroup2'

        self.go_rest_client.username_password_login(admin_username,
                                                    password=admin_password)
        response, content = self.go_rest_client.post_group(group_name)

        group_id = content['id']
        self.created_groups.append(group_id)
        self.go_rest_client.set_single_policy(group_id, 'approval', 'admin')

        user = '******'
        self.go_rest_client.post_user(user, 'Test User',
                                      '*****@*****.**', 'sikrit')
        self.created_users.append(user)

        # Test that the group membership of a particular username doesn't persist
        # between test runs:
        response, content = self.go_rest_client.get_group_member(
            group_id, user)
        self.assertEquals(
            response['status'],
            '404',
            msg="A newly created user should never be part of a group.")

        self.go_rest_client.username_password_login(
            user, 'sikrit')  # logging in so the user can be editted
        # Test PUTing user custom fields:
        custom_fields = {
            'current_project_name': 'BIRN Community',
            'organization': 'Computation Institute',
        }
        self.go_rest_client.put_user_custom_fields(user, **custom_fields)
        response, content = self.go_rest_client.get_user(
            user, custom_fields=custom_fields.keys())
        self.assertEquals(custom_fields, content['custom_fields'])

        # Test GETting and PUTting user visibility:
        response, content = self.go_rest_client.get_user_policies(user)
        self.assertFalse(content['user_membership_visibility']['value']
                         ['community']['value'])
        self.go_rest_client.put_user_membership_visibility(user, 'community')
        response, content = self.go_rest_client.get_user_policies(user)
        self.assertTrue(content['user_membership_visibility']['value']
                        ['community']['value'])

        # About 90% of the rest of this test is broken because the smtp_mail_sink that was used is broken.
        # The smtp_mail_sink that was used only worked reliably with localhost, but the tests no longer
        # use localhost.
        # It might be a good idea to leave the code here because the only thing wrong is the
        # smtp_mail_sink and a working one would make the rest of the test work
        """