Example #1
0
    def test_create_group(self):
        # TODO: test with picture file upload for the group
        url = reverse('create_user_group')
        # test passing privacy_level = 'public'
        grp_data = {
            'name': 'Test Group-1',
            'description': 'This is a cool group-1',
            'privacy_level': 'public'
        }
        request = self.factory.post(url, data=grp_data)

        self.set_request_message_attributes(request)
        request.user = self.john
        response = create_user_group(request)
        new_group = Group.objects.filter(name='Test Group-1').first()
        self.assertNotEqual(new_group, None)
        self.assertEqual(new_group.gaccess.description,
                         'This is a cool group-1')
        self.assertEqual(new_group.gaccess.public, True)
        self.assertEqual(new_group.gaccess.discoverable, True)
        self.assertEqual(response.status_code, status.HTTP_302_FOUND)
        self.assertEqual(response['Location'],
                         reverse('group', args=[new_group.id]))

        # test passing privacy_level = 'private'
        grp_data = {
            'name': 'Test Group-2',
            'description': 'This is a cool group-2',
            'privacy_level': 'private'
        }
        request = self.factory.post(url, data=grp_data)
        self.set_request_message_attributes(request)

        request.user = self.john
        create_user_group(request)
        new_group = Group.objects.filter(name='Test Group-2').first()
        self.assertNotEqual(new_group, None)
        self.assertEqual(new_group.gaccess.description,
                         'This is a cool group-2')
        self.assertEqual(new_group.gaccess.public, False)
        self.assertEqual(new_group.gaccess.discoverable, False)

        # test passing privacy_level = 'discoverable'
        grp_data = {
            'name': 'Test Group-3',
            'description': 'This is a cool group-3',
            'privacy_level': 'discoverable'
        }
        request = self.factory.post(url, data=grp_data)
        self.set_request_message_attributes(request)

        request.user = self.john
        create_user_group(request)
        new_group = Group.objects.filter(name='Test Group-3').first()
        self.assertNotEqual(new_group, None)
        self.assertEqual(new_group.gaccess.description,
                         'This is a cool group-3')
        self.assertEqual(new_group.gaccess.public, False)
        self.assertEqual(new_group.gaccess.discoverable, True)
Example #2
0
    def _create_group(self):
        url = reverse('create_user_group')
        # test passing privacy_level = 'public'
        grp_data = {'name': 'Test Group', 'description': 'This is a cool group', 'privacy_level': 'public'}
        request = self.factory.post(url, data=grp_data)

        self.set_request_message_attributes(request)
        request.user = self.john
        create_user_group(request)
        new_group = Group.objects.filter(name='Test Group').first()
        return new_group
Example #3
0
    def _create_group(self):
        url = reverse('create_user_group')
        # test passing privacy_level = 'public'
        grp_data = {'name': 'Test Group', 'description': 'This is a cool group', 'privacy_level': 'public'}
        request = self.factory.post(url, data=grp_data)

        self._set_request_message_attributes(request)
        request.user = self.john
        create_user_group(request)
        new_group = Group.objects.filter(name='Test Group').first()
        return new_group
Example #4
0
    def test_group_update_failure(self):
        # test that post data for 'name', 'description', and 'privacy_level' are required
        # when updating a group

        # first create a group to test updating group
        url = reverse('create_user_group')
        grp_data = {'name': 'Test Group-1', 'description': 'This is a cool group-1',
                    'purpose': 'This group has purpose', 'privacy_level': 'public'}
        request = self.factory.post(url, data=grp_data)

        self.set_request_message_attributes(request)
        request.user = self.john
        response = create_user_group(request)
        new_group = Group.objects.filter(name='Test Group-1').first()

        # now test updating new_group
        url_params = {'group_id': new_group.id}
        url = reverse('update_user_group', kwargs=url_params)
        # test name is required -> update should fail
        grp_data = {'description': 'This is a cool group-2', 'purpose': 'This group has purpose'}
        request = self.factory.post(url, data=grp_data)

        self.set_request_message_attributes(request)
        request.user = self.john
        request.META['HTTP_REFERER'] = "/some_url/"
        response = update_user_group(request, group_id=new_group.id)
        # description has not changed proves update failed
        self.assertNotEqual(new_group.gaccess.description, 'This is a cool group-2')
        self.assertEqual(new_group.gaccess.description, 'This is a cool group-1')
        self.assertEqual(response.status_code, status.HTTP_302_FOUND)
        self.assertEqual(response['Location'], request.META['HTTP_REFERER'])

        # test description is required -> update should fail
        grp_data = {'name': 'Test Group-2', 'purpose': 'This group has purpose',
                    'privacy_level': 'public'}
        request = self.factory.post(url, data=grp_data)

        self._update_failure(new_group, request)

        # test privacy_level is required -> update should fail
        grp_data = {'name': 'Test Group-2', 'description': 'This is a cool group',
                    'purpose': 'This group has purpose'}
        request = self.factory.post(url, data=grp_data)

        self._update_failure(new_group, request)

        # test trying to update group with a duplicate name ('HydroShare Author') should fail
        grp_data = {'name': 'Hydroshare Author', 'description': 'This is a cool group-1',
                    'purpose': 'This group has purpose'}
        request = self.factory.post(url, data=grp_data)
        self.set_request_message_attributes(request)
        request.user = self.john
        request.META['HTTP_REFERER'] = "/some_url/"
        response = update_user_group(request, group_id=new_group.id)
        # name has not changed proves update failed
        self.assertEqual(Group.objects.filter(name='Hydroshare Author').count(), 1)
        updated_group = Group.objects.filter(name='Hydroshare Author').first()
        self.assertNotEqual(updated_group.id, new_group.id)
        self.assertEqual(response.status_code, status.HTTP_302_FOUND)
        self.assertEqual(response['Location'], request.META['HTTP_REFERER'])
Example #5
0
    def test_group_update_failure(self):
        # test that post data for 'name', 'description', and 'privacy_level' are required
        # when updating a group

        # first create a group to test updating group
        url = reverse('create_user_group')
        grp_data = {'name': 'Test Group-1', 'description': 'This is a cool group-1',
                    'purpose': 'This group has purpose', 'privacy_level': 'public'}
        request = self.factory.post(url, data=grp_data)

        self._set_request_message_attributes(request)
        request.user = self.john
        response = create_user_group(request)
        new_group = Group.objects.filter(name='Test Group-1').first()

        # now test updating new_group
        url_params = {'group_id': new_group.id}
        url = reverse('update_user_group', kwargs=url_params)
        # test name is required -> update should fail
        grp_data = {'description': 'This is a cool group-2', 'purpose': 'This group has purpose'}
        request = self.factory.post(url, data=grp_data)

        self._set_request_message_attributes(request)
        request.user = self.john
        request.META['HTTP_REFERER'] = "/some_url/"
        response = update_user_group(request, group_id=new_group.id)
        # description has not changed proves update failed
        self.assertNotEqual(new_group.gaccess.description, 'This is a cool group-2')
        self.assertEqual(new_group.gaccess.description, 'This is a cool group-1')
        self.assertEqual(response.status_code, status.HTTP_302_FOUND)
        self.assertEqual(response['Location'], request.META['HTTP_REFERER'])

        # test description is required -> update should fail
        grp_data = {'name': 'Test Group-2', 'purpose': 'This group has purpose',
                    'privacy_level': 'public'}
        request = self.factory.post(url, data=grp_data)

        self._update_failure(new_group, request)

        # test privacy_level is required -> update should fail
        grp_data = {'name': 'Test Group-2', 'description': 'This is a cool group',
                    'purpose': 'This group has purpose'}
        request = self.factory.post(url, data=grp_data)

        self._update_failure(new_group, request)

        # test trying to update group with a duplicate name ('HydroShare Author') should fail
        grp_data = {'name': 'Hydroshare Author', 'description': 'This is a cool group-1',
                    'purpose': 'This group has purpose'}
        request = self.factory.post(url, data=grp_data)
        self._set_request_message_attributes(request)
        request.user = self.john
        request.META['HTTP_REFERER'] = "/some_url/"
        response = update_user_group(request, group_id=new_group.id)
        # name has not changed proves update failed
        self.assertEqual(Group.objects.filter(name='Hydroshare Author').count(), 1)
        updated_group = Group.objects.filter(name='Hydroshare Author').first()
        self.assertNotEqual(updated_group.id, new_group.id)
        self.assertEqual(response.status_code, status.HTTP_302_FOUND)
        self.assertEqual(response['Location'], request.META['HTTP_REFERER'])
Example #6
0
    def test_delete_restore_group(self):
        # test a group can be deleted or restored

        # first create a group to test updating group
        url = reverse('create_user_group')
        grp_data = {
            'name': 'Test Group-1',
            'description': 'This is a cool group-1',
            'purpose': 'This group has no purpose',
            'privacy_level': 'public'
        }
        request = self.factory.post(url, data=grp_data)

        self.set_request_message_attributes(request)
        request.user = self.john
        create_user_group(request)
        new_group = Group.objects.filter(name='Test Group-1').first()
        self.assertEqual(new_group.gaccess.active, True)

        post_data = {'group_id': new_group.id}
        url = reverse('delete_user_group', kwargs=post_data)
        request = self.factory.post(url, data=post_data)

        self.set_request_message_attributes(request)
        request.user = self.john
        request.META['HTTP_REFERER'] = "/some_url/"
        response = delete_user_group(request, group_id=new_group.id)
        new_group = Group.objects.filter(name='Test Group-1').first()
        self.assertEqual(new_group.gaccess.active, False)

        self.assertEqual(response.status_code, status.HTTP_302_FOUND)
        self.assertEqual(response['Location'], request.META['HTTP_REFERER'])

        # test undeleting the group
        url = reverse('restore_user_group', kwargs=post_data)
        request = self.factory.post(url, data=post_data)

        self.set_request_message_attributes(request)
        request.user = self.john
        request.META['HTTP_REFERER'] = "/some_url/"
        response = restore_user_group(request, group_id=new_group.id)
        new_group = Group.objects.filter(name='Test Group-1').first()
        self.assertEqual(new_group.gaccess.active, True)

        self.assertEqual(response.status_code, status.HTTP_302_FOUND)
        self.assertEqual(response['Location'], request.META['HTTP_REFERER'])
Example #7
0
    def test_create_group(self):
        # TODO: test with picture file upload for the group
        url = reverse('create_user_group')
        # test passing privacy_level = 'public'
        grp_data = {'name': 'Test Group-1', 'description': 'This is a cool group-1', 'privacy_level': 'public'}
        request = self.factory.post(url, data=grp_data)

        self.set_request_message_attributes(request)
        request.user = self.john
        response = create_user_group(request)
        new_group = Group.objects.filter(name='Test Group-1').first()
        self.assertNotEqual(new_group, None)
        self.assertEqual(new_group.gaccess.description, 'This is a cool group-1')
        self.assertEqual(new_group.gaccess.public, True)
        self.assertEqual(new_group.gaccess.discoverable, True)
        self.assertEqual(response.status_code, status.HTTP_302_FOUND)
        self.assertEqual(response['Location'], reverse('group', args=[new_group.id]))

        # test passing privacy_level = 'private'
        grp_data = {'name': 'Test Group-2', 'description': 'This is a cool group-2', 'privacy_level': 'private'}
        request = self.factory.post(url, data=grp_data)
        self.set_request_message_attributes(request)

        request.user = self.john
        create_user_group(request)
        new_group = Group.objects.filter(name='Test Group-2').first()
        self.assertNotEqual(new_group, None)
        self.assertEqual(new_group.gaccess.description, 'This is a cool group-2')
        self.assertEqual(new_group.gaccess.public, False)
        self.assertEqual(new_group.gaccess.discoverable, False)

        # test passing privacy_level = 'discoverable'
        grp_data = {'name': 'Test Group-3', 'description': 'This is a cool group-3', 'privacy_level': 'discoverable'}
        request = self.factory.post(url, data=grp_data)
        self.set_request_message_attributes(request)

        request.user = self.john
        create_user_group(request)
        new_group = Group.objects.filter(name='Test Group-3').first()
        self.assertNotEqual(new_group, None)
        self.assertEqual(new_group.gaccess.description, 'This is a cool group-3')
        self.assertEqual(new_group.gaccess.public, False)
        self.assertEqual(new_group.gaccess.discoverable, True)
Example #8
0
    def test_delete_restore_group(self):
        # test a group can be deleted or restored

        # first create a group to test updating group
        url = reverse('create_user_group')
        grp_data = {'name': 'Test Group-1', 'description': 'This is a cool group-1',
                    'purpose': 'This group has no purpose', 'privacy_level': 'public'}
        request = self.factory.post(url, data=grp_data)

        self.set_request_message_attributes(request)
        request.user = self.john
        create_user_group(request)
        new_group = Group.objects.filter(name='Test Group-1').first()
        self.assertEqual(new_group.gaccess.active, True)

        post_data = {'group_id': new_group.id}
        url = reverse('delete_user_group', kwargs=post_data)
        request = self.factory.post(url, data=post_data)

        self.set_request_message_attributes(request)
        request.user = self.john
        request.META['HTTP_REFERER'] = "/some_url/"
        response = delete_user_group(request, group_id=new_group.id)
        new_group = Group.objects.filter(name='Test Group-1').first()
        self.assertEqual(new_group.gaccess.active, False)

        self.assertEqual(response.status_code, status.HTTP_302_FOUND)
        self.assertEqual(response['Location'], request.META['HTTP_REFERER'])

        # test undeleting the group
        url = reverse('restore_user_group', kwargs=post_data)
        request = self.factory.post(url, data=post_data)

        self.set_request_message_attributes(request)
        request.user = self.john
        request.META['HTTP_REFERER'] = "/some_url/"
        response = restore_user_group(request, group_id=new_group.id)
        new_group = Group.objects.filter(name='Test Group-1').first()
        self.assertEqual(new_group.gaccess.active, True)

        self.assertEqual(response.status_code, status.HTTP_302_FOUND)
        self.assertEqual(response['Location'], request.META['HTTP_REFERER'])
Example #9
0
    def test_create_group(self):
        # here we are testing the create_user_group view function

        # there should be 1 group at this point
        self.assertEqual(Group.objects.count(), 1)
        post_data = {'name': "Test Group", 'description': "This is great group",
                     'purpose': "To have fun", 'auto_approve': False, 'privacy_level': 'public'}
        url = reverse('create_user_group', kwargs={})

        request = self.factory.post(url, data=post_data)
        request.user = self.john
        expected_new_group_id = Group.objects.all().order_by("-id").first().id + 1
        request.META['HTTP_REFERER'] = '/group/{}'.format(expected_new_group_id)
        self.set_request_message_attributes(request)
        self.add_session_to_request(request)
        response = create_user_group(request)
        self.assertEqual(response.status_code, status.HTTP_302_FOUND)
        self.assertEqual(response['Location'], request.META['HTTP_REFERER'])
        flag_messages = get_messages(request)
        success_messages = [m for m in flag_messages if m.tags == 'success']
        self.assertNotEqual(len(success_messages), 0)
        # there should be 2 group at this point
        self.assertEqual(Group.objects.count(), 2)
Example #10
0
    def test_update_group(self):
        # TODO: test with picture file upload for the group

        # first create a group to test updating group
        url = reverse('create_user_group')
        grp_data = {
            'name': 'Test Group-1',
            'description': 'This is a cool group-1',
            'purpose': 'This group has no purpose',
            'privacy_level': 'public'
        }
        request = self.factory.post(url, data=grp_data)

        self.set_request_message_attributes(request)
        request.user = self.john
        response = create_user_group(request)
        new_group = Group.objects.filter(name='Test Group-1').first()
        self.assertEqual(new_group.gaccess.active, True)
        self.assertNotEqual(new_group, None)
        self.assertEqual(new_group.gaccess.description,
                         'This is a cool group-1')
        self.assertEqual(new_group.gaccess.purpose,
                         'This group has no purpose')
        self.assertEqual(new_group.gaccess.public, True)
        self.assertEqual(new_group.gaccess.discoverable, True)
        self.assertEqual(new_group.gaccess.active, True)
        self.assertEqual(new_group.gaccess.shareable, True)
        self.assertEqual(response.status_code, status.HTTP_302_FOUND)
        self.assertEqual(response['Location'],
                         reverse('group', args=[new_group.id]))

        # now test updating new_group
        url_params = {'group_id': new_group.id}
        url = reverse('update_user_group', kwargs=url_params)
        # update name, description, purpose
        grp_data = {
            'name': 'Test Group-2',
            'description': 'This is a cool group-2',
            'purpose': 'This group now has purpose',
            'privacy_level': 'public'
        }
        request = self.factory.post(url, data=grp_data)

        self.set_request_message_attributes(request)
        request.user = self.john
        request.META['HTTP_REFERER'] = "/some_url/"
        response = update_user_group(request, group_id=new_group.id)
        new_group = Group.objects.filter(name='Test Group-2').first()
        self.assertNotEqual(new_group, None)
        self.assertEqual(new_group.gaccess.description,
                         'This is a cool group-2')
        self.assertEqual(new_group.gaccess.purpose,
                         'This group now has purpose')
        self.assertEqual(new_group.gaccess.public, True)
        self.assertEqual(new_group.gaccess.discoverable, True)
        self.assertEqual(new_group.gaccess.active, True)

        self.assertEqual(response.status_code, status.HTTP_302_FOUND)
        self.assertEqual(response['Location'], request.META['HTTP_REFERER'])

        # update group to remove purpose
        grp_data = {
            'name': 'Test Group-2',
            'description': 'This is a cool group-2',
            'privacy_level': 'public'
        }
        request = self.factory.post(url, data=grp_data)

        self.set_request_message_attributes(request)
        request.user = self.john
        request.META['HTTP_REFERER'] = "/some_url/"
        response = update_user_group(request, group_id=new_group.id)
        new_group = Group.objects.filter(name='Test Group-2').first()
        self.assertNotEqual(new_group, None)
        self.assertEqual(new_group.gaccess.description,
                         'This is a cool group-2')
        self.assertEqual(new_group.gaccess.purpose, '')
        self.assertEqual(new_group.gaccess.public, True)
        self.assertEqual(new_group.gaccess.discoverable, True)
        self.assertEqual(new_group.gaccess.active, True)

        self.assertEqual(response.status_code, status.HTTP_302_FOUND)
        self.assertEqual(response['Location'], request.META['HTTP_REFERER'])

        # update privacy_level (set to private)- this set public to false and discoverable to false
        grp_data = {
            'name': 'Test Group-2',
            'description': 'This is a cool group-2',
            'privacy_level': 'private'
        }
        request = self.factory.post(url, data=grp_data)

        self.set_request_message_attributes(request)
        request.user = self.john
        request.META['HTTP_REFERER'] = "/some_url/"
        response = update_user_group(request, group_id=new_group.id)
        new_group = Group.objects.filter(name='Test Group-2').first()
        self.assertNotEqual(new_group, None)
        self.assertEqual(new_group.gaccess.description,
                         'This is a cool group-2')
        self.assertEqual(new_group.gaccess.purpose, '')
        self.assertEqual(new_group.gaccess.public, False)
        self.assertEqual(new_group.gaccess.discoverable, False)
        self.assertEqual(new_group.gaccess.active, True)

        self.assertEqual(response.status_code, status.HTTP_302_FOUND)
        self.assertEqual(response['Location'], request.META['HTTP_REFERER'])

        # update privacy_level (set to public) - this set public to true and discoverable to true
        grp_data = {
            'name': 'Test Group-2',
            'description': 'This is a cool group-2',
            'privacy_level': 'public'
        }
        request = self.factory.post(url, data=grp_data)

        self.set_request_message_attributes(request)
        request.user = self.john
        request.META['HTTP_REFERER'] = "/some_url/"
        response = update_user_group(request, group_id=new_group.id)
        new_group = Group.objects.filter(name='Test Group-2').first()
        self.assertNotEqual(new_group, None)
        self.assertEqual(new_group.gaccess.description,
                         'This is a cool group-2')
        self.assertEqual(new_group.gaccess.purpose, '')
        self.assertEqual(new_group.gaccess.public, True)
        self.assertEqual(new_group.gaccess.discoverable, True)
        self.assertEqual(new_group.gaccess.active, True)

        self.assertEqual(response.status_code, status.HTTP_302_FOUND)
        self.assertEqual(response['Location'], request.META['HTTP_REFERER'])

        # update privacy_level (set to discoverable) - this should set discoverable to
        # true and public to false
        grp_data = {
            'name': 'Test Group-2',
            'description': 'This is a cool group-2',
            'privacy_level': 'discoverable'
        }
        request = self.factory.post(url, data=grp_data)

        self.set_request_message_attributes(request)
        request.user = self.john
        request.META['HTTP_REFERER'] = "/some_url/"
        response = update_user_group(request, group_id=new_group.id)
        new_group = Group.objects.filter(name='Test Group-2').first()
        self.assertNotEqual(new_group, None)
        self.assertEqual(new_group.gaccess.description,
                         'This is a cool group-2')
        self.assertEqual(new_group.gaccess.purpose, '')
        self.assertEqual(new_group.gaccess.public, False)
        self.assertEqual(new_group.gaccess.discoverable, True)
        self.assertEqual(new_group.gaccess.active, True)

        self.assertEqual(response.status_code, status.HTTP_302_FOUND)
        self.assertEqual(response['Location'], request.META['HTTP_REFERER'])
Example #11
0
    def test_group_create_failures(self):
        # test that post data for 'name' and 'description' are required
        # for creating a group. Also post data must have a key 'privacy_level'
        # with one of these values ('public', 'private', 'discoverable'). Duplicate group names are
        # not allowed

        # at this point there should be only one group
        self.assertEqual(Group.objects.count(), 1)
        url = reverse('create_user_group')
        # test 'name' is required
        grp_data = {
            'description': 'This is a cool group',
            'privacy_level': 'public'
        }

        request = self.factory.post(url, data=grp_data)
        self.set_request_message_attributes(request)

        request.user = self.john
        request.META['HTTP_REFERER'] = "/some_url/"
        response = create_user_group(request)
        self.assertEqual(response.status_code, status.HTTP_302_FOUND)
        self.assertEqual(response['Location'], request.META['HTTP_REFERER'])
        new_group = Group.objects.filter(
            gaccess__description='This is a cool group').first()
        self.assertEqual(new_group, None)
        # at this point there should be only one group
        self.assertEqual(Group.objects.count(), 1)

        # test 'description' is required
        grp_data = {'name': 'Test Group', 'privacy_level': 'public'}

        request = self.factory.post(url, data=grp_data)
        self.set_request_message_attributes(request)

        request.user = self.john
        request.META['HTTP_REFERER'] = "/some_url/"
        response = create_user_group(request)
        self.assertEqual(response.status_code, status.HTTP_302_FOUND)
        self.assertEqual(response['Location'], request.META['HTTP_REFERER'])
        new_group = Group.objects.filter(name='Test Group').first()
        self.assertEqual(new_group, None)
        # at this point there should be only one group
        self.assertEqual(Group.objects.count(), 1)

        # test 'privacy_level' is required
        grp_data = {
            'name': 'Test Group',
            'description': 'This is a cool group'
        }

        request = self.factory.post(url, data=grp_data)
        self.set_request_message_attributes(request)

        request.user = self.john
        request.META['HTTP_REFERER'] = "/some_url/"
        response = create_user_group(request)
        self.assertEqual(response.status_code, status.HTTP_302_FOUND)
        self.assertEqual(response['Location'], request.META['HTTP_REFERER'])
        new_group = Group.objects.filter(name='Test Group').first()
        self.assertEqual(new_group, None)
        # at this point there should be only one group
        self.assertEqual(Group.objects.count(), 1)

        # test 'privacy_level' should have one of these values (public, private, discoverable)
        grp_data = {
            'name': 'Test Group',
            'description': 'This is a cool group',
            'privacy_level': 'some-level'
        }

        request = self.factory.post(url, data=grp_data)
        self.set_request_message_attributes(request)

        request.user = self.john
        request.META['HTTP_REFERER'] = "/some_url/"
        response = create_user_group(request)
        self.assertEqual(response.status_code, status.HTTP_302_FOUND)
        self.assertEqual(response['Location'], request.META['HTTP_REFERER'])
        new_group = Group.objects.filter(name='Test Group').first()
        self.assertEqual(new_group, None)
        # at this point there should be only one group
        self.assertEqual(Group.objects.count(), 1)

        # test that duplicate group names are not allowed
        grp_data = {
            'name': 'Test Group',
            'description': 'This is a cool group',
            'privacy_level': 'private'
        }
        request = self.factory.post(url, data=grp_data)
        self.set_request_message_attributes(request)

        request.user = self.john
        response = create_user_group(request)
        self.assertEqual(response.status_code, status.HTTP_302_FOUND)
        # at this point there should be 2 groups
        self.assertEqual(Group.objects.count(), 2)

        # create a group with duplicate name
        grp_data = {
            'name': 'Test Group',
            'description': 'This is a very cool group',
            'privacy_level': 'private'
        }
        request = self.factory.post(url, data=grp_data)
        self.set_request_message_attributes(request)

        request.user = self.john
        request.META['HTTP_REFERER'] = "/some_url/"
        # had to do this as a transaction for some reason, otherwise the last statement
        # of this function generates a transaction error
        with transaction.atomic():
            response = create_user_group(request)
            self.assertEqual(response.status_code, status.HTTP_302_FOUND)
            self.assertEqual(response['Location'],
                             request.META['HTTP_REFERER'])
            # at this point there should be still 2 groups
        self.assertEqual(Group.objects.count(), 2)
Example #12
0
    def test_update_group(self):
        # TODO: test with picture file upload for the group

        # first create a group to test updating group
        url = reverse('create_user_group')
        grp_data = {'name': 'Test Group-1', 'description': 'This is a cool group-1',
                    'purpose': 'This group has no purpose', 'privacy_level': 'public'}
        request = self.factory.post(url, data=grp_data)

        self.set_request_message_attributes(request)
        request.user = self.john
        response = create_user_group(request)
        new_group = Group.objects.filter(name='Test Group-1').first()
        self.assertEqual(new_group.gaccess.active, True)
        self.assertNotEqual(new_group, None)
        self.assertEqual(new_group.gaccess.description, 'This is a cool group-1')
        self.assertEqual(new_group.gaccess.purpose, 'This group has no purpose')
        self.assertEqual(new_group.gaccess.public, True)
        self.assertEqual(new_group.gaccess.discoverable, True)
        self.assertEqual(new_group.gaccess.active, True)
        self.assertEqual(new_group.gaccess.shareable, True)
        self.assertEqual(response.status_code, status.HTTP_302_FOUND)
        self.assertEqual(response['Location'], reverse('group', args=[new_group.id]))

        # now test updating new_group
        url_params = {'group_id': new_group.id}
        url = reverse('update_user_group', kwargs=url_params)
        # update name, description, purpose
        grp_data = {'name': 'Test Group-2', 'description': 'This is a cool group-2',
                    'purpose': 'This group now has purpose', 'privacy_level': 'public'}
        request = self.factory.post(url, data=grp_data)

        self.set_request_message_attributes(request)
        request.user = self.john
        request.META['HTTP_REFERER'] = "/some_url/"
        response = update_user_group(request, group_id=new_group.id)
        new_group = Group.objects.filter(name='Test Group-2').first()
        self.assertNotEqual(new_group, None)
        self.assertEqual(new_group.gaccess.description, 'This is a cool group-2')
        self.assertEqual(new_group.gaccess.purpose, 'This group now has purpose')
        self.assertEqual(new_group.gaccess.public, True)
        self.assertEqual(new_group.gaccess.discoverable, True)
        self.assertEqual(new_group.gaccess.active, True)

        self.assertEqual(response.status_code, status.HTTP_302_FOUND)
        self.assertEqual(response['Location'], request.META['HTTP_REFERER'])

        # update group to remove purpose
        grp_data = {'name': 'Test Group-2', 'description': 'This is a cool group-2',
                    'privacy_level': 'public'}
        request = self.factory.post(url, data=grp_data)

        self.set_request_message_attributes(request)
        request.user = self.john
        request.META['HTTP_REFERER'] = "/some_url/"
        response = update_user_group(request, group_id=new_group.id)
        new_group = Group.objects.filter(name='Test Group-2').first()
        self.assertNotEqual(new_group, None)
        self.assertEqual(new_group.gaccess.description, 'This is a cool group-2')
        self.assertEqual(new_group.gaccess.purpose, '')
        self.assertEqual(new_group.gaccess.public, True)
        self.assertEqual(new_group.gaccess.discoverable, True)
        self.assertEqual(new_group.gaccess.active, True)

        self.assertEqual(response.status_code, status.HTTP_302_FOUND)
        self.assertEqual(response['Location'], request.META['HTTP_REFERER'])

        # update privacy_level (set to private)- this set public to false and discoverable to false
        grp_data = {'name': 'Test Group-2', 'description': 'This is a cool group-2',
                    'privacy_level': 'private'}
        request = self.factory.post(url, data=grp_data)

        self.set_request_message_attributes(request)
        request.user = self.john
        request.META['HTTP_REFERER'] = "/some_url/"
        response = update_user_group(request, group_id=new_group.id)
        new_group = Group.objects.filter(name='Test Group-2').first()
        self.assertNotEqual(new_group, None)
        self.assertEqual(new_group.gaccess.description, 'This is a cool group-2')
        self.assertEqual(new_group.gaccess.purpose, '')
        self.assertEqual(new_group.gaccess.public, False)
        self.assertEqual(new_group.gaccess.discoverable, False)
        self.assertEqual(new_group.gaccess.active, True)

        self.assertEqual(response.status_code, status.HTTP_302_FOUND)
        self.assertEqual(response['Location'], request.META['HTTP_REFERER'])

        # update privacy_level (set to public) - this set public to true and discoverable to true
        grp_data = {'name': 'Test Group-2', 'description': 'This is a cool group-2',
                    'privacy_level': 'public'}
        request = self.factory.post(url, data=grp_data)

        self.set_request_message_attributes(request)
        request.user = self.john
        request.META['HTTP_REFERER'] = "/some_url/"
        response = update_user_group(request, group_id=new_group.id)
        new_group = Group.objects.filter(name='Test Group-2').first()
        self.assertNotEqual(new_group, None)
        self.assertEqual(new_group.gaccess.description, 'This is a cool group-2')
        self.assertEqual(new_group.gaccess.purpose, '')
        self.assertEqual(new_group.gaccess.public, True)
        self.assertEqual(new_group.gaccess.discoverable, True)
        self.assertEqual(new_group.gaccess.active, True)

        self.assertEqual(response.status_code, status.HTTP_302_FOUND)
        self.assertEqual(response['Location'], request.META['HTTP_REFERER'])

        # update privacy_level (set to discoverable) - this should set discoverable to
        # true and public to false
        grp_data = {'name': 'Test Group-2', 'description': 'This is a cool group-2',
                    'privacy_level': 'discoverable'}
        request = self.factory.post(url, data=grp_data)

        self.set_request_message_attributes(request)
        request.user = self.john
        request.META['HTTP_REFERER'] = "/some_url/"
        response = update_user_group(request, group_id=new_group.id)
        new_group = Group.objects.filter(name='Test Group-2').first()
        self.assertNotEqual(new_group, None)
        self.assertEqual(new_group.gaccess.description, 'This is a cool group-2')
        self.assertEqual(new_group.gaccess.purpose, '')
        self.assertEqual(new_group.gaccess.public, False)
        self.assertEqual(new_group.gaccess.discoverable, True)
        self.assertEqual(new_group.gaccess.active, True)

        self.assertEqual(response.status_code, status.HTTP_302_FOUND)
        self.assertEqual(response['Location'], request.META['HTTP_REFERER'])
Example #13
0
    def test_group_create_failures(self):
        # test that post data for 'name' and 'description' are required
        # for creating a group. Also post data must have a key 'privacy_level'
        # with one of these values ('public', 'private', 'discoverable'). Duplicate group names are
        # not allowed

        # at this point there should be only one group
        self.assertEqual(Group.objects.count(), 1)
        url = reverse('create_user_group')
        # test 'name' is required
        grp_data = {'description': 'This is a cool group', 'privacy_level': 'public'}

        request = self.factory.post(url, data=grp_data)
        self.set_request_message_attributes(request)

        request.user = self.john
        request.META['HTTP_REFERER'] = "/some_url/"
        response = create_user_group(request)
        self.assertEqual(response.status_code, status.HTTP_302_FOUND)
        self.assertEqual(response['Location'], request.META['HTTP_REFERER'])
        new_group = Group.objects.filter(gaccess__description='This is a cool group').first()
        self.assertEqual(new_group, None)
        # at this point there should be only one group
        self.assertEqual(Group.objects.count(), 1)

        # test 'description' is required
        grp_data = {'name': 'Test Group', 'privacy_level': 'public'}

        request = self.factory.post(url, data=grp_data)
        self.set_request_message_attributes(request)

        request.user = self.john
        request.META['HTTP_REFERER'] = "/some_url/"
        response = create_user_group(request)
        self.assertEqual(response.status_code, status.HTTP_302_FOUND)
        self.assertEqual(response['Location'], request.META['HTTP_REFERER'])
        new_group = Group.objects.filter(name='Test Group').first()
        self.assertEqual(new_group, None)
        # at this point there should be only one group
        self.assertEqual(Group.objects.count(), 1)

        # test 'privacy_level' is required
        grp_data = {'name': 'Test Group', 'description': 'This is a cool group'}

        request = self.factory.post(url, data=grp_data)
        self.set_request_message_attributes(request)

        request.user = self.john
        request.META['HTTP_REFERER'] = "/some_url/"
        response = create_user_group(request)
        self.assertEqual(response.status_code, status.HTTP_302_FOUND)
        self.assertEqual(response['Location'], request.META['HTTP_REFERER'])
        new_group = Group.objects.filter(name='Test Group').first()
        self.assertEqual(new_group, None)
        # at this point there should be only one group
        self.assertEqual(Group.objects.count(), 1)

        # test 'privacy_level' should have one of these values (public, private, discoverable)
        grp_data = {'name': 'Test Group', 'description': 'This is a cool group', 'privacy_level': 'some-level'}

        request = self.factory.post(url, data=grp_data)
        self.set_request_message_attributes(request)

        request.user = self.john
        request.META['HTTP_REFERER'] = "/some_url/"
        response = create_user_group(request)
        self.assertEqual(response.status_code, status.HTTP_302_FOUND)
        self.assertEqual(response['Location'], request.META['HTTP_REFERER'])
        new_group = Group.objects.filter(name='Test Group').first()
        self.assertEqual(new_group, None)
        # at this point there should be only one group
        self.assertEqual(Group.objects.count(), 1)

        # test that duplicate group names are not allowed
        grp_data = {'name': 'Test Group', 'description': 'This is a cool group', 'privacy_level': 'private'}
        request = self.factory.post(url, data=grp_data)
        self.set_request_message_attributes(request)

        request.user = self.john
        response = create_user_group(request)
        self.assertEqual(response.status_code, status.HTTP_302_FOUND)
        # at this point there should be 2 groups
        self.assertEqual(Group.objects.count(), 2)

        # create a group with duplicate name
        grp_data = {'name': 'Test Group', 'description': 'This is a very cool group', 'privacy_level': 'private'}
        request = self.factory.post(url, data=grp_data)
        self.set_request_message_attributes(request)

        request.user = self.john
        request.META['HTTP_REFERER'] = "/some_url/"
        # had to do this as a transaction for some reason, otherwise the last statement
        # of this function generates a transaction error
        with transaction.atomic():
            response = create_user_group(request)
            self.assertEqual(response.status_code, status.HTTP_302_FOUND)
            self.assertEqual(response['Location'], request.META['HTTP_REFERER'])
            # at this point there should be still 2 groups
        self.assertEqual(Group.objects.count(), 2)